Skip to content

Commit

Permalink
Add JS MapTool.player functions
Browse files Browse the repository at this point in the history
The interfaces return player names and take a player by name
rather than having Player proxy objects
because role checks could be confused by a player disconnecting and
rejoining with a different role.

A proxy object would need to listen to player connect/disconnect events
to keep its underlying player object in sync
and unless this proves to be a performance problem we can look up the
player by name each time.
  • Loading branch information
fishface60 committed Dec 28, 2024
1 parent 7e18a1f commit 1775a42
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public String serializeToString() {

@HostAccess.Export public final JSAPINetwork network = new JSAPINetwork();

@HostAccess.Export public final JSAPIPlayer player = new JSAPIPlayer();

@HostAccess.Export public final JSAPIServerInfo serverInfo = new JSAPIServerInfo();

@HostAccess.Export public final JSAPITokens tokens = new JSAPITokens();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* This software Copyright by the RPTools.net development team, and
* licensed under the Affero GPL Version 3 or, at your option, any later
* version.
*
* MapTool Source Code is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public
* License * along with this source Code. If not, please visit
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.maptool.client.script.javascript.api;

import java.util.*;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.model.player.Player;
import org.graalvm.polyglot.HostAccess;

public class JSAPIPlayer implements MapToolJSAPIInterface {
@Override
public String serializeToString() {
return "MapTool.player";
}

@HostAccess.Export
@Nonnull
public String getLocalPlayerName() {
return MapTool.getPlayer().getName();
}

@HostAccess.Export
@Nonnull
public List<String> getConnectedPlayerNames() {
return MapTool.getPlayerList().stream()
.map(player -> player.getName())
.collect(Collectors.toList());
}

@Nullable
private Player findPlayerByName(@Nonnull String playerName) {
Player player = null;
for (var p : MapTool.getPlayerList()) {
if (p.getName().equalsIgnoreCase(playerName)) {
player = p;
}
}
return player;
}

/**
* Get whether the named player is connected.
*
* @return true if the player is connected, false if not.
*/
@HostAccess.Export
public boolean isConnected(@Nonnull String playerName) {
return findPlayerByName(playerName) != null;
}

/**
* Get whether the named player is a Player.
*
* @return true if the player is connected and is a Player, false otherwise.
*/
@HostAccess.Export
public boolean isPlayer(@Nonnull String playerName) {
var player = findPlayerByName(playerName);
if (player == null) {
return false;
}
return player.getRole() == Player.Role.PLAYER;
}

/**
* Get whether the named player is a GM.
*
* @return true if the player is connected and is a GM, false otherwise.
*/
@HostAccess.Export
public boolean isGM(@Nonnull String playerName) {
var player = findPlayerByName(playerName);
if (player == null) {
return false;
}
return player.isGM();
}
}

0 comments on commit 1775a42

Please sign in to comment.