-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdatingMessagesCreator.java
86 lines (77 loc) · 2.83 KB
/
UpdatingMessagesCreator.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
package messages;
import game.model.Room;
import messages.fields_names.CommonFields;
import messages.fields_names.GameFields;
import messages.fields_names.RoomFields;
import messages.fields_names.ServicesFields;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Generate messages to update the player on room/game events.
*
* @author Micieli
* @date 2015/05/21
*/
public class UpdatingMessagesCreator {
private CurrentRoomMessagesCreator currentRoomMessagesCreator;
public UpdatingMessagesCreator() {
this.currentRoomMessagesCreator = new CurrentRoomMessagesCreator();
}
/**
* Creates the message who informs the player that the game is started.
*
* @return the message
*/
public JSONObject generateStartMessage() {
JSONObject message = new JSONObject();
try {
message.put(ServicesFields.SERVICE.toString(), ServicesFields.CURRENT_ROOM.toString());
message.put(ServicesFields.SERVICE_TYPE.toString(), RoomFields.ROOM_ACTIONS.toString());
message.put(RoomFields.ROOM_ACTION.toString(), RoomFields.ROOM_START.toString());
message.put(CommonFields.NO_ERRORS.toString(), true);
} catch (JSONException e) {
e.printStackTrace();
}
return message;
}
/**
* Creates the message who informs the player about a changing in {@link Room} players list.
*
* @return the message
*/
public JSONObject generatePlayersListMessage(Room room) {
return currentRoomMessagesCreator.generatePlayersListMessage(room);
}
/**
* Creates the message who inform the player that all the others player are ready to play, so the game can really starts.
*
* @return the message
*/
public JSONObject generateGoMessage() {
JSONObject message = new JSONObject();
try {
message.put(ServicesFields.SERVICE.toString(), ServicesFields.GAME.toString());
message.put(ServicesFields.SERVICE_TYPE.toString(), GameFields.GAME_STATUS.toString());
message.put(GameFields.GAME_GO.toString(), true);
} catch (JSONException e) {
e.printStackTrace();
}
return message;
}
/**
* Creates the message informing player that the game was interrupted
*
* @return the complete message
*/
public JSONObject generateExitMessage() {
JSONObject message = new JSONObject();
try {
message.put(ServicesFields.SERVICE.toString(), ServicesFields.GAME.toString());
message.put(ServicesFields.SERVICE_TYPE.toString(), GameFields.GAME_STATUS.toString());
message.put(GameFields.GAME_END.toString(), GameFields.GAME_INTERRUPTED.toString());
} catch (JSONException e) {
e.printStackTrace();
}
return message;
}
}