-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoomsMessagesCreator.java
118 lines (107 loc) · 4.22 KB
/
RoomsMessagesCreator.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package messages;
import check_fields.RoomChecker;
import game.model.Room;
import messages.fields_names.CommonFields;
import messages.fields_names.RoomFields;
import messages.fields_names.RoomsFields;
import messages.fields_names.ServicesFields;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import services.rooms.subservices.CreateRoom;
import services.rooms.subservices.JoinPlayer;
import java.util.ArrayList;
/**
* @author Micieli
* @date 2015/05/19
*/
public class RoomsMessagesCreator {
/**
* Generate a message to warn the client that the given <code>Room</code> name is invalid.
*
* @return the complete message to send
* @see RoomChecker
* @see CreateRoom
*/
public JSONObject generateNameInvalidResponse() {
String error = RoomsFields.ROOM_CREATE_ERROR_INVALID_NAME.toString();
return getCreateFailedMessage(error);
}
/**
* Generate a message to warn the client that the given <code>Room</code> name is already used.
*
* @return the complete message to send
* @see RoomChecker
* @see CreateRoom
*/
public JSONObject generateRoomExistMessage() {
String error = RoomsFields.ROOM_CREATE_ERROR_ALREADY_PRESENT.toString();
return getCreateFailedMessage(error);
}
/**
* Generate a message that contains all the rooms available in the server.
*
* @param rooms the list of room to send
* @return the complete message to send
*/
public JSONObject generateRoomsListMessage(ArrayList<Room> rooms) {
JSONObject response = new JSONObject();
try {
response.put(ServicesFields.SERVICE_TYPE.toString(), RoomsFields.ROOMS_LIST.toString());
response.put(ServicesFields.SERVICE.toString(), ServicesFields.ROOMS.toString());
response.put(CommonFields.NO_ERRORS.toString(), true);
JSONObject roomsList = formatRoomList(rooms);
response.put(RoomsFields.ROOMS_LIST.toString(), roomsList);
} catch (JSONException e) {
e.printStackTrace();
}
return response;
}
private JSONObject formatRoomList(ArrayList<Room> rooms) throws JSONException {
JSONObject roomsList = new JSONObject();
for (Room room : rooms) {
JSONObject roomDescription = new JSONObject();
roomDescription.put(RoomsFields.ROOM_MAX_PLAYERS.toString(), room.getMaxPlayers());
roomDescription.put(RoomFields.ROOM_CURRENT_PLAYERS.toString(), room.getSize());
roomsList.put(room.getName(), roomDescription);
}
return roomsList;
}
/**
* Generate a message advice the player about the join request result.
*
* @param result the join result
* @param roomName the room name
* @return the message to send
* @see JoinPlayer
*/
public JSONObject generateJoinResponse(boolean result, String roomName) {
JSONObject response = new JSONObject();
try {
response.put(ServicesFields.SERVICE.toString(), ServicesFields.ROOMS.toString());
response.put(ServicesFields.SERVICE_TYPE.toString(), RoomsFields.ROOM_JOIN.toString());
response.put(RoomFields.ROOM_NAME.toString(), roomName);
response.put(CommonFields.RESULT.toString(), result);
response.put(CommonFields.NO_ERRORS.toString(), result);
} catch (JSONException e) {
e.printStackTrace();
}
return response;
}
private JSONObject getCreateFailedMessage(String error) {
JSONObject response = new JSONObject();
try {
response.put(ServicesFields.SERVICE_TYPE.toString(), RoomsFields.ROOM_CREATE.toString());
response.put(ServicesFields.SERVICE.toString(), ServicesFields.ROOMS.toString());
response.put(CommonFields.NO_ERRORS.toString(), false);
JSONObject errorObj = new JSONObject();
JSONArray errors = new JSONArray();
errors.put(error);
errorObj.put(CommonFields.ERRORS.toString(), errors);
response.put(CommonFields.ERRORS.toString(), errorObj);
} catch (JSONException e) {
e.printStackTrace();
}
return response;
}
}