forked from aws-samples/eb-java-scorekeep
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveFactory.java
More file actions
58 lines (52 loc) · 2.33 KB
/
MoveFactory.java
File metadata and controls
58 lines (52 loc) · 2.33 KB
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
package scorekeep;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
public class MoveFactory {
private SecureRandom random = new SecureRandom();
private final HashMap<String, Move> allMoves = new HashMap<String, Move>(1);
private MoveModel moveModel = new MoveModel();
private StateModel stateModel = new StateModel();
private GameController gameController = new GameController();
private StateController stateController = new StateController();
private RulesFactory rulesFactory = new RulesFactory();
public MoveFactory(){
}
public Move newMove(String sessionId, String gameId, String userId, String moveText) throws SessionNotFoundException, GameNotFoundException, StateNotFoundException {
String moveId = new BigInteger(40, random).toString(32).toUpperCase();
String stateId = new BigInteger(40, random).toString(32).toUpperCase();
Move move = new Move(moveId, sessionId, gameId, userId, moveText);
// 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 newStateText = TicTacToe.move(oldState.getState(), moveText);
// save new game state
State newState = new State(stateId, sessionId, gameId, newStateText, newTurn);
// 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);
}
}