Skip to content

Commit 423a3b1

Browse files
committed
use reflection to invoke rules dynamically
1 parent 429de45 commit 423a3b1

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

bin/test-api.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ do
1414
GAMEID=$(curl --silent -X POST $API/game/$SESSIONID | jq -r .id)
1515
echo "configuring game, "
1616
curl --silent -X POST $API/game/$SESSIONID/$GAMEID/users -H "Content-Type: application/json" --data "[\"$USER1ID\",\"$USER2ID\"]"
17-
curl --silent -X PUT $API/game/$SESSIONID/$GAMEID/rules/TICTACTOE
17+
curl --silent -X PUT $API/game/$SESSIONID/$GAMEID/rules/TicTacToe
1818
curl --silent -X PUT $API/game/$SESSIONID/$GAMEID/name/tic-tac-toe-test
1919
EPOCH=$(date +%s)
2020
curl --silent -X PUT $API/game/$SESSIONID/$GAMEID/starttime/$EPOCH

src/main/java/scorekeep/MoveController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public List<Move> getMoves(@PathVariable String sessionId, @PathVariable String
3131
}
3232
/* POST /move/SESSION/GAME/USER ; move string */
3333
@RequestMapping(value="/{userId}", method=RequestMethod.POST)
34-
public Move newMove(@PathVariable String sessionId, @PathVariable String gameId, @PathVariable String userId, @RequestBody String move) throws SessionNotFoundException, GameNotFoundException, StateNotFoundException {
34+
public Move newMove(@PathVariable String sessionId, @PathVariable String gameId, @PathVariable String userId, @RequestBody String move) throws SessionNotFoundException, GameNotFoundException, StateNotFoundException, RulesException {
3535
return moveFactory.newMove(sessionId, gameId, userId, move);
3636
}
3737
/** GET /move/SESSION/GAME/MOVE **/

src/main/java/scorekeep/MoveFactory.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
55
import java.util.HashMap;
66
import java.util.List;
77
import java.util.Set;
8+
import java.lang.Class;
9+
import java.lang.reflect.Method;
10+
import java.lang.reflect.InvocationTargetException;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
813

914
public class MoveFactory {
15+
private static final Logger logger = LoggerFactory.getLogger("scorekeep.MoveFactory");
1016
private SecureRandom random = new SecureRandom();
1117
private final HashMap<String, Move> allMoves = new HashMap<String, Move>(1);
1218
private MoveModel moveModel = new MoveModel();
@@ -18,10 +24,11 @@ public class MoveFactory {
1824
public MoveFactory(){
1925
}
2026

21-
public Move newMove(String sessionId, String gameId, String userId, String moveText) throws SessionNotFoundException, GameNotFoundException, StateNotFoundException {
27+
public Move newMove(String sessionId, String gameId, String userId, String moveText) throws SessionNotFoundException, GameNotFoundException, StateNotFoundException, RulesException {
2228
String moveId = new BigInteger(40, random).toString(32).toUpperCase();
2329
String stateId = new BigInteger(40, random).toString(32).toUpperCase();
2430
Move move = new Move(moveId, sessionId, gameId, userId, moveText);
31+
String newStateText = "";
2532
// load game state
2633
Game game = gameController.getGame(sessionId, gameId);
2734
List<String> states = game.getStates();
@@ -37,7 +44,14 @@ public Move newMove(String sessionId, String gameId, String userId, String moveT
3744
if (newTurn.size() != 1) {
3845
newTurn.remove(userId);
3946
}
40-
String newStateText = TicTacToe.move(oldState.getState(), moveText);
47+
try {
48+
Class<?> rules = Class.forName("scorekeep." + game.getRules());
49+
Method moveMethod = rules.getMethod("move", String.class, String.class);
50+
newStateText = (String) moveMethod.invoke(null, oldState.getState(), moveText);
51+
} catch ( ClassNotFoundException e ) { throw new RulesException(game.getRules()); }
52+
catch ( NoSuchMethodException f ) { throw new RulesException(game.getRules()); }
53+
catch ( IllegalAccessException g ) { throw new RulesException(game.getRules()); }
54+
catch ( InvocationTargetException h ) { throw new RulesException(game.getRules()); }
4155
// save new game state
4256
State newState = new State(stateId, sessionId, gameId, newStateText, newTurn);
4357
// register state and move id to game
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package scorekeep;
2+
import org.springframework.web.bind.annotation.ResponseStatus;
3+
import org.springframework.http.HttpStatus;
4+
5+
@ResponseStatus(value=HttpStatus.BAD_REQUEST, reason="Rules move invocation failed.")
6+
public class RulesException extends Exception{
7+
private String rulesId;
8+
public RulesException(String rulesId)
9+
{
10+
this.rulesId = rulesId;
11+
}
12+
public String getRulesId()
13+
{
14+
return rulesId;
15+
}
16+
}

src/main/java/scorekeep/RulesFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class RulesFactory {
88
private final HashMap<String, Rules> allRules = new HashMap<String, Rules>(1);
99

1010
public RulesFactory(){
11-
String id = "TICTACTOE";
11+
String id = "TicTacToe";
1212
String name = "Tic Tac Toe";
1313
String[] categories = { "head to head", "quick" };
1414
Integer[] users = { 2 };

0 commit comments

Comments
 (0)