MCPcopy Index your code
hub / github.com/aws-samples/eb-java-scorekeep / newMove

Method newMove

src/main/java/scorekeep/MoveFactory.java:26–76  ·  view source on GitHub ↗
(String sessionId, String gameId, String userId, String moveText)

Source from the content-addressed store, hash-verified

24 }
25
26 public Move newMove(String sessionId, String gameId, String userId, String moveText) throws SessionNotFoundException, GameNotFoundException, StateNotFoundException, RulesException {
27 String moveId = Identifiers.random();
28 String stateId = Identifiers.random();
29 Move move = new Move().setId(moveId)
30 .setSession(sessionId)
31 .setGame(gameId)
32 .setUser(userId)
33 .setMove(moveText);
34 String newStateText = "";
35 // load game state
36 Game game = gameController.getGame(sessionId, gameId);
37 List<String> states = game.getStates();
38 State oldState = stateController.getState(sessionId, gameId, states.get(states.size() - 1));
39 Set<String> oldTurn = oldState.getTurn();
40 // check turn
41 // if ( oldTurn.contains(userId) {}
42 // load game rules
43 // rules = rulesFactory.getRules(rulesId)
44 // apply move
45 // String stateText = rules.move(oldState, move)
46 Set<String> newTurn = game.getUsers();
47 if (newTurn.size() != 1) {
48 newTurn.remove(userId);
49 }
50 String rulesName = game.getRules();
51 if ( !rulesName.matches("[a-zA-Z]{1,16}") ) {
52 throw new RulesException(rulesName);
53 }
54 try {
55 Class<?> rules = Class.forName("scorekeep." + rulesName);
56 Method moveMethod = rules.getMethod("move", String.class, String.class);
57 newStateText = (String) moveMethod.invoke(null, oldState.getState(), moveText);
58 } catch ( ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { throw new RulesException(rulesName); }
59 // save new game state
60 State newState = new State(stateId, sessionId, gameId, newStateText, newTurn);
61 // send notification on game end
62 if ( newStateText.startsWith("A") || newStateText.startsWith("B")) {
63 Thread comm = new Thread() {
64 public void run() {
65 Sns.sendNotification("Scorekeep game completed", "Winner: " + userId);
66 }
67 };
68 comm.start();
69 }
70 // register state and move id to game
71 gameController.setGameMove(sessionId, gameId, moveId);
72 gameController.setGameState(sessionId, gameId, stateId);
73 moveModel.saveMove(move);
74 stateModel.saveState(newState);
75 return move;
76 }
77
78 public Move getMove(String sessionId, String gameId, String moveId) throws SessionNotFoundException, MoveNotFoundException {
79 return moveModel.loadMove(moveId);

Callers

nothing calls this directly

Calls 15

randomMethod · 0.95
getStatesMethod · 0.95
getStateMethod · 0.95
getTurnMethod · 0.95
getUsersMethod · 0.95
getRulesMethod · 0.95
setGameMoveMethod · 0.80
setGameStateMethod · 0.80
saveMoveMethod · 0.80
saveStateMethod · 0.80
setMoveMethod · 0.45
setUserMethod · 0.45

Tested by

no test coverage detected