-
Notifications
You must be signed in to change notification settings - Fork 217
/
MoveFactory.java
96 lines (88 loc) · 3.87 KB
/
MoveFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package scorekeep;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.lang.Class;
import java.lang.Thread;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.amazonaws.xray.AWSXRay;
import com.amazonaws.xray.AWSXRayRecorder;
import com.amazonaws.xray.entities.Entity;
import com.amazonaws.xray.entities.Segment;
import com.amazonaws.xray.entities.Subsegment;
public class MoveFactory {
private static final Logger logger = LoggerFactory.getLogger(MoveFactory.class);
private final HashMap<String, Move> allMoves = new HashMap<String, Move>(1);
private final MoveModel moveModel = new MoveModel();
private final StateModel stateModel = new StateModel();
private final GameController gameController = new GameController();
private final StateController stateController = new StateController();
private final RulesFactory rulesFactory = new RulesFactory();
private final AWSXRayRecorder recorder = AWSXRay.getGlobalRecorder();
public MoveFactory(){
}
public Move newMove(String sessionId, String gameId, String userId, String moveText) throws SessionNotFoundException, GameNotFoundException, StateNotFoundException, RulesException {
String moveId = Identifiers.random();
String stateId = Identifiers.random();
Move move = new Move().setId(moveId)
.setSession(sessionId)
.setGame(gameId)
.setUser(userId)
.setMove(moveText);
String newStateText = "";
// load game state
Game game = gameController.getGame(sessionId, gameId);
List<String> states = game.getStates();
State oldState = stateController.getState(sessionId, gameId, states.get(states.size() - 1));
Set<String> oldTurn = oldState.getTurn();
// check turn
// if ( oldTurn.contains(userId) {}
// load game rules
// rules = rulesFactory.getRules(rulesId)
// apply move
// String stateText = rules.move(oldState, move)
Set<String> newTurn = game.getUsers();
if (newTurn.size() != 1) {
newTurn.remove(userId);
}
String rulesName = game.getRules();
if ( !rulesName.matches("[a-zA-Z]{1,16}") ) {
throw new RulesException(rulesName);
}
try {
Class<?> rules = Class.forName("scorekeep." + rulesName);
Method moveMethod = rules.getMethod("move", String.class, String.class);
newStateText = (String) moveMethod.invoke(null, oldState.getState(), moveText);
} catch ( ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { throw new RulesException(rulesName); }
// save new game state
State newState = new State(stateId, sessionId, gameId, newStateText, newTurn);
// send notification on game end
if ( newStateText.startsWith("A") || newStateText.startsWith("B")) {
Entity segment = recorder.getTraceEntity();
Thread comm = new Thread() {
public void run() {
recorder.setTraceEntity(segment);
Subsegment subsegment = AWSXRay.beginSubsegment("## Send notification");
Sns.sendNotification("Scorekeep game completed", "Winner: " + userId);
AWSXRay.endSubsegment();
}
};
comm.start();
}
// register state and move id to game
gameController.setGameMove(sessionId, gameId, moveId);
gameController.setGameState(sessionId, gameId, stateId);
moveModel.saveMove(move);
stateModel.saveState(newState);
return move;
}
public Move getMove(String sessionId, String gameId, String moveId) throws SessionNotFoundException, MoveNotFoundException {
return moveModel.loadMove(moveId);
}
public List<Move> getMoves(String sessionId, String gameId) throws SessionNotFoundException, GameNotFoundException {
return moveModel.loadMoves(sessionId, gameId);
}
}