2
2
3
3
import org .slf4j .Logger ;
4
4
import org .slf4j .LoggerFactory ;
5
+ import sc .api .plugins .IGameInstance ;
6
+ import sc .api .plugins .IGamePlugin ;
7
+ import sc .api .plugins .IGameState ;
5
8
import sc .api .plugins .exceptions .GameRoomException ;
6
9
import sc .api .plugins .exceptions .RescuableClientException ;
10
+ import sc .api .plugins .host .GameLoader ;
7
11
import sc .networking .InvalidScoreDefinitionException ;
8
12
import sc .protocol .requests .PrepareGameRequest ;
9
13
import sc .protocol .responses .GamePreparedResponse ;
15
19
import sc .server .plugins .UnknownGameTypeException ;
16
20
import sc .shared .*;
17
21
22
+ import java .io .File ;
23
+ import java .io .IOException ;
18
24
import java .math .BigDecimal ;
19
25
import java .math .MathContext ;
20
26
import java .util .*;
21
27
22
28
/**
23
29
* The GameManager is responsible to keep all games alive and kill them once
24
- * they are done. Additionally the GameManager has to detect and kill games,
25
- * which seem to be dead-locked or have caused a timeout.
30
+ * they are done. Additionally the GameManager has to detect and kill games
31
+ * which seem dead-locked or have caused a timeout.
26
32
*/
27
33
public class GameRoomManager {
28
34
private Map <String , GameRoom > rooms ;
@@ -46,66 +52,67 @@ private synchronized void add(GameRoom room) {
46
52
this .rooms .put (room .getId (), room );
47
53
}
48
54
55
+ public IGamePlugin findPlugin (String gameType ) throws RescuableClientException {
56
+ GamePluginInstance plugin = this .gamePluginManager .getPlugin (gameType );
57
+
58
+ if (plugin == null ) {
59
+ logger .warn ("Couldn't find a game of type " + gameType );
60
+ throw new UnknownGameTypeException (gameType , this .gamePluginManager .getPluginUUIDs ());
61
+ }
62
+
63
+ return plugin .getPlugin ();
64
+ }
65
+
49
66
/**
50
- * Create a not prepared {@link GameRoom GameRoom} of given type
51
- *
52
- * @param gameType String of current Game
67
+ * Create a not prepared {@link GameRoom GameRoom} of given type.
53
68
*
54
69
* @return Newly created GameRoom
55
70
*
56
71
* @throws RescuableClientException if creation of game failed
57
72
*/
58
- public synchronized GameRoom createGame (String gameType ) throws RescuableClientException {
59
- return createGame (gameType , false );
73
+ public synchronized GameRoom createGameRoom (String gameType ) throws RescuableClientException {
74
+ IGamePlugin plugin = findPlugin (gameType );
75
+ IGameInstance game ;
76
+
77
+ String gameFileLocation = Configuration .get (Configuration .GAMELOADFILE );
78
+ if (gameFileLocation != null && !gameFileLocation .equals ("" )) {
79
+ File gameFile = new File (gameFileLocation );
80
+ Integer turn = null ;
81
+ try {
82
+ turn = Integer .parseInt (Configuration .get (Configuration .TURN_TO_LOAD ));
83
+ } catch (NumberFormatException ignored ) {
84
+ }
85
+
86
+ // TODO skip to turn
87
+ // TODO implement tests
88
+ logger .info ("Loading game from file '{}' at turn {}" , gameFile , turn );
89
+ try {
90
+ game = plugin .createGameFromState ((IGameState ) new GameLoader (IGameState .class ).loadGame (gameFile ));
91
+ } catch (IOException e ) {
92
+ logger .error ("Failed to load game from file" , e );
93
+ game = plugin .createGame ();
94
+ }
95
+ } else {
96
+ game = plugin .createGame ();
97
+ }
98
+
99
+ return createGameRoom (plugin .getScoreDefinition (), game , false );
60
100
}
61
101
62
102
/**
63
- * Create a new GameRoom from the matching plugin.
64
- * If gameFile is set, load gameState from file.
103
+ * Create a new GameRoom with the given Game.
65
104
*
66
- * @param gameType id of the game plugin to use
67
- * @param prepared signals whether the game was prepared by gui or ..., false if player has to send JoinRoomRequest
105
+ * @param prepared signals whether the game was prepared by an administrative client,
106
+ * false if from a JoinRoomRequest
68
107
*
69
108
* @return newly created GameRoom
70
- *
71
- * @throws UnknownGameTypeException if no matching GamePlugin was found
72
109
*/
73
- public GameRoom createGame (String gameType , boolean prepared ) throws RescuableClientException {
74
- GamePluginInstance plugin = this .gamePluginManager .getPlugin (gameType );
75
-
76
- if (plugin == null ) {
77
- logger .warn ("Couldn't find a game of type " + gameType );
78
- throw new UnknownGameTypeException (gameType , this .gamePluginManager .getPluginUUIDs ());
79
- }
80
-
81
- logger .info ("Creating new game of type " + gameType );
82
-
83
- String roomId = generateRoomId ();
84
- GameRoom room = new GameRoom (roomId , this , plugin .getPlugin ().getScoreDefinition (), plugin .createGame (), prepared );
110
+ public GameRoom createGameRoom (ScoreDefinition scoreDefinition , IGameInstance game , boolean prepared ) {
111
+ GameRoom room = new GameRoom (generateRoomId (), this , scoreDefinition , game , prepared );
85
112
// pause room if specified in server.properties on joinRoomRequest
86
113
if (!prepared ) {
87
114
boolean paused = Boolean .parseBoolean (Configuration .get (Configuration .PAUSED ));
88
115
room .pause (paused );
89
- logger .info ("Pause is set to {}" , paused );
90
- }
91
-
92
- String gameFile = Configuration .get (Configuration .GAMELOADFILE );
93
- if (gameFile != null && !gameFile .equals ("" )) {
94
- logger .info ("Request plugin to load game from file: " + gameFile );
95
- int turn ;
96
- if (Configuration .get (Configuration .TURN_TO_LOAD ) != null ) {
97
- turn = Integer .parseInt (Configuration .get (Configuration .TURN_TO_LOAD ));
98
- } else {
99
- turn = 0 ;
100
- }
101
- logger .debug ("Turns is to load is: " + turn );
102
- if (turn > 0 ) {
103
- logger .debug ("Loading from non default turn" );
104
- room .game .loadFromFile (gameFile , turn );
105
- } else {
106
- logger .debug ("Loading first gameState found" );
107
- room .game .loadFromFile (gameFile );
108
- }
109
116
}
110
117
111
118
this .add (room );
@@ -126,7 +133,7 @@ private static synchronized String generateRoomId() {
126
133
*/
127
134
public synchronized RoomWasJoinedEvent createAndJoinGame (Client client , String gameType )
128
135
throws RescuableClientException {
129
- GameRoom room = createGame (gameType );
136
+ GameRoom room = createGameRoom (gameType );
130
137
if (room .join (client )) {
131
138
return roomJoined (room );
132
139
}
@@ -164,23 +171,22 @@ public GamePluginManager getPluginManager() {
164
171
}
165
172
166
173
/**
167
- * Creates a new GameRoom through {@link #createGame(String) createGame} with reserved PlayerSlots according to the
174
+ * Creates a new GameRoom with reserved PlayerSlots according to the
168
175
* descriptors and loads a game state from a file if provided.
169
176
*
170
177
* @return new PrepareGameProtocolMessage with roomId and slot reservations
171
178
*
172
179
* @throws RescuableClientException if game could not be created
173
180
*/
174
- public synchronized GamePreparedResponse prepareGame (String gameType , boolean paused , SlotDescriptor [] descriptors , Object loadGameInfo )
181
+ public synchronized GamePreparedResponse prepareGame (String gameType , boolean paused , SlotDescriptor [] descriptors , IGameState loadGameInfo )
175
182
throws RescuableClientException {
176
- GameRoom room = createGame (gameType , true );
183
+ IGamePlugin plugin = findPlugin (gameType );
184
+ IGameInstance game = loadGameInfo != null ? plugin .createGameFromState (loadGameInfo ) : plugin .createGame ();
185
+
186
+ GameRoom room = createGameRoom (plugin .getScoreDefinition (), game , true );
177
187
room .pause (paused );
178
188
room .openSlots (descriptors );
179
189
180
- if (loadGameInfo != null ) {
181
- room .game .loadGameInfo (loadGameInfo );
182
- }
183
-
184
190
return new GamePreparedResponse (room .getId (), room .reserveAllSlots ());
185
191
}
186
192
0 commit comments