-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
7e18a1f
commit 1775a42
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/main/java/net/rptools/maptool/client/script/javascript/api/JSAPIPlayer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |