Skip to content

Commit

Permalink
Fix execLink / execFunction not running locally immediately
Browse files Browse the repository at this point in the history
- Change so execLink / execFunction are ran immediately for the current player when defer is set to 0.
- Add target options "not-self", "not-gm", and "not-gm-self".
- Fix RPTools#820
  • Loading branch information
Merudo committed Oct 21, 2019
1 parent 7ce8c73 commit 8932aaa
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,13 @@ public void run() {
return;

case execFunction:
ExecFunction.receiveExecFunction((String) parameters[0], (String) parameters[1]);
ExecFunction.receiveExecFunction(
(String) parameters[0], (String) parameters[1], (String) parameters[2]);
return;

case execLink:
MacroLinkFunction.receiveExecLink((String) parameters[0], (String) parameters[1]);
MacroLinkFunction.receiveExecLink(
(String) parameters[0], (String) parameters[1], (String) parameters[2]);
return;

case showPointer:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import net.rptools.lib.MD5Key;
import net.rptools.maptool.client.functions.ExecFunction;
import net.rptools.maptool.client.functions.MacroLinkFunction;
import net.rptools.maptool.model.Asset;
import net.rptools.maptool.model.AssetManager;
import net.rptools.maptool.model.Campaign;
Expand Down Expand Up @@ -200,13 +202,21 @@ public void message(TextMessage message) {
}

@Override
public void execFunction(String functionText, String target) {
makeServerCall(COMMAND.execFunction, functionText, target);
public void execFunction(String functionText, String target, String source) {
ExecFunction.receiveExecFunction(functionText, target, source); // receive locally right away

if (ExecFunction.isMessageGlobal(target, source)) {
makeServerCall(COMMAND.execFunction, functionText, target, source);
}
}

@Override
public void execLink(String link, String target) {
makeServerCall(COMMAND.execLink, link, target);
public void execLink(String link, String target, String source) {
MacroLinkFunction.receiveExecLink(link, target, source); // receive locally right away

if (ExecFunction.isMessageGlobal(target, source)) {
makeServerCall(COMMAND.execLink, link, target, source);
}
}

public void showPointer(String player, Pointer pointer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private ExecFunction() {
@Override
public Object childEvaluate(Parser parser, String functionName, List<Object> args)
throws ParserException {
FunctionUtil.checkNumberParam(functionName, args, 1, 5);
FunctionUtil.checkNumberParam(functionName, args, 2, 5);
if (!MapTool.getParser().isMacroTrusted()) {
throw new ParserException(I18N.getText("macro.function.general.noPerm", functionName));
}
Expand Down Expand Up @@ -97,8 +97,7 @@ public Object childEvaluate(Parser parser, String functionName, List<Object> arg
* @param targets the list of targets
*/
private static void sendExecFunction(
final String execName, List<Object> execArgs, boolean defer, Collection<String> targets)
throws ParserException {
final String execName, List<Object> execArgs, boolean defer, Collection<String> targets) {
if (defer) {
EventQueue.invokeLater(
new Runnable() {
Expand All @@ -121,26 +120,10 @@ public void run() {
private static void sendExecFunction(
final String execName, List<Object> execArgs, Collection<String> targets) {
String functionText = getExecFunctionText(execName, execArgs);
boolean isGM = MapTool.getPlayer().isGM();
String selfName = MapTool.getPlayer().getName();
String source = MapTool.getPlayer().getName();

for (String target : targets) {
if (target.equals(selfName)) {
target = "self";
}
switch (target.toLowerCase()) {
case "gm-self":
MapTool.serverCommand().execFunction(functionText, "gm");
if (isGM) break; // FALLTHRU if not a GM
case "self":
runExecFunction(functionText);
break;
case "none":
break;
default:
MapTool.serverCommand().execFunction(functionText, target);
break;
}
MapTool.serverCommand().execFunction(functionText, target, source);
}
}

Expand All @@ -149,28 +132,55 @@ private static void sendExecFunction(
*
* @param functionText the text of the function call.
* @param target the target player.
* @param source the name of the player who sent the link.
*/
public static void receiveExecFunction(final String functionText, String target) {
public static void receiveExecFunction(final String functionText, String target, String source) {
if (isMessageForMe(target, source)) {
runExecFunction(functionText);
}
}

/**
* Determines if the message / execLink / execFunction should be ran on the client.
*
* @param target the target player.
* @param source the name of the player who sent the link.
* @return is the message for the player or not
*/
public static boolean isMessageForMe(String target, String source) {
boolean isGM = MapTool.getPlayer().isGM();
String selfName = MapTool.getPlayer().getName();
boolean fromSelf = source.equals(MapTool.getPlayer().getName());
boolean targetSelf = target.equals(MapTool.getPlayer().getName());

switch (target.toLowerCase()) {
case "gm":
if (isGM) {
runExecFunction(functionText);
}
break;
return isGM;
case "self":
return fromSelf;
case "gm-self":
return isGM || fromSelf;
case "not-self":
return !fromSelf;
case "not-gm":
return !isGM;
case "not-gm-self":
return !isGM && !fromSelf;
case "none":
return false;
case "all":
runExecFunction(functionText);
break;
return true;
default:
if (target.equals(selfName)) {
runExecFunction(functionText);
}
break;
return targetSelf;
}
}

public static boolean isMessageGlobal(String target, String source) {
if (target.equals(source)) return false;
if (target.equalsIgnoreCase("none")) return false;
if (target.equalsIgnoreCase("self")) return false;
return true;
}

/**
* Get a String corresponding to the function call from the function name and list of arguments.
* The text is then intended to be run through runMacroBlock. This is a workaround as it is not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,43 +207,23 @@ public void run() {
* @param targets the list of targets
*/
private static void sendExecLink(final String link, Collection<String> targets) {
boolean isGM = MapTool.getPlayer().isGM();
String source = MapTool.getPlayer().getName();

for (String target : targets) {
switch (target.toLowerCase()) {
case "gm-self":
MapTool.serverCommand().execLink(link, "gm");
if (isGM) break; // // FALLTHRU if not a GM
case "self":
runMacroLink(link);
break;
case "none":
break;
default:
MapTool.serverCommand().execLink(link, target);
}
MapTool.serverCommand().execLink(link, target, source);
}
}

/**
* Receive an execLink, and run it if the player is a target.
*
* @param link the macroLinkText
* @param target the target. Can also be "gm" or "all".
* @param target the target.
* @param source the name of the source.
*/
public static void receiveExecLink(final String link, String target) {
String playerName = MapTool.getPlayer().getName();
boolean isGM = MapTool.getPlayer().isGM();
switch (target.toLowerCase()) {
case "gm":
if (isGM) runMacroLink(link);
break;
case "all":
runMacroLink(link);
break;
default:
if (target.equals(playerName)) runMacroLink(link);
break;
public static void receiveExecLink(final String link, String target, String source) {
if (ExecFunction.isMessageForMe(target, source)) {
runMacroLink(link);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/rptools/maptool/server/ServerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ public void updateTokenProperty(

public void message(TextMessage message);

public void execFunction(String functionText, String target);
public void execFunction(String functionText, String target, String source);

public void execLink(String link, String target);
public void execLink(String link, String target, String source);

public void showPointer(String player, Pointer pointer);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ public void handleMethod(String id, String method, Object... parameters) {
message((TextMessage) context.get(0));
break;
case execFunction:
execFunction((String) context.get(0), (String) context.get(1));
execFunction((String) context.get(0), (String) context.get(1), (String) context.get(2));
break;
case execLink:
execLink((String) context.get(0), (String) context.get(1));
execLink((String) context.get(0), (String) context.get(1), (String) context.get(2));
break;
case putAsset:
putAsset((Asset) context.get(0));
Expand Down Expand Up @@ -549,13 +549,13 @@ public void message(TextMessage message) {
}

@Override
public void execFunction(String functionText, String target) {
forwardToAllClients();
public void execFunction(String functionText, String target, String source) {
forwardToClients();
}

@Override
public void execLink(String link, String target) {
forwardToAllClients();
public void execLink(String link, String target, String source) {
forwardToClients();
}

public void putAsset(Asset asset) {
Expand Down

0 comments on commit 8932aaa

Please sign in to comment.