Skip to content

Commit 7d03dfc

Browse files
committed
refactor(sdk): simplify GameLoader
1 parent 8e959ae commit 7d03dfc

File tree

2 files changed

+14
-39
lines changed

2 files changed

+14
-39
lines changed

sdk/src/server-api/sc/api/plugins/host/GameLoader.java

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,29 @@
1212
import java.io.FileInputStream;
1313
import java.io.IOException;
1414
import java.io.InputStream;
15-
import java.util.Arrays;
16-
import java.util.List;
1715
import java.util.zip.GZIPInputStream;
1816

19-
public class GameLoader<T> implements IHistoryListener {
17+
public class GameLoader implements IHistoryListener {
2018
private static final Logger logger = LoggerFactory.getLogger(GameLoader.class);
2119
private volatile boolean finished;
22-
private T obj = null;
23-
private List<Class<T>> clazzes;
20+
private int turn = 0;
21+
private IGameState obj = null;
2422
private GameLoaderClient client;
2523

26-
public GameLoader(List<Class<T>> clazzes) {
27-
this.finished = false;
28-
this.clazzes = clazzes;
29-
}
30-
31-
public GameLoader(Class<T>... clazz) {
32-
this(Arrays.asList(clazz));
33-
}
34-
35-
public T loadGame(String filename) {
36-
try {
37-
return loadGame(new File(filename));
38-
} catch (IOException e) {
39-
e.printStackTrace();
40-
return null;
41-
}
42-
}
43-
44-
public T loadGame(File file) throws IOException {
24+
public IGameState loadGame(File file, int turn) throws IOException {
25+
this.turn = turn;
4526
return loadGame(new FileInputStream(file), file.getName().endsWith(".gz"));
4627
}
4728

48-
public T loadGame(FileInputStream stream, boolean gzip) throws IOException {
29+
public IGameState loadGame(FileInputStream stream, boolean gzip) throws IOException {
4930
if (gzip) {
5031
return loadGame(new GZIPInputStream(stream));
5132
} else {
5233
return loadGame(stream);
5334
}
5435
}
5536

56-
public T loadGame(InputStream file) throws IOException {
37+
public IGameState loadGame(InputStream file) throws IOException {
5738
client = new GameLoaderClient(file);
5839
client.addListener(this);
5940
client.start();
@@ -79,15 +60,10 @@ public void onGameOver(String roomId, GameResult result) {
7960
@Override
8061
public void onNewState(String roomId, IGameState state) {
8162
logger.debug("Received new state");
82-
if (!this.finished) {
83-
for (Class<T> clazz : this.clazzes) {
84-
if (clazz.isInstance(state)) {
85-
logger.debug("Received game info of type {}", clazz.getName());
86-
this.obj = clazz.cast(state);
87-
this.finished = true;
88-
this.client.stop();
89-
}
90-
}
63+
if (!this.finished && state.getTurn() >= turn) {
64+
this.obj = state;
65+
this.finished = true;
66+
this.client.stop();
9167
}
9268
}
9369

server/src/sc/server/gaming/GameRoomManager.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,16 @@ public synchronized GameRoom createGameRoom(String gameType) throws RescuableCli
7777
String gameFileLocation = Configuration.get(Configuration.GAMELOADFILE);
7878
if (gameFileLocation != null && !gameFileLocation.equals("")) {
7979
File gameFile = new File(gameFileLocation);
80-
Integer turn = null;
80+
int turn = 0;
8181
try {
8282
turn = Integer.parseInt(Configuration.get(Configuration.TURN_TO_LOAD));
8383
} catch(NumberFormatException ignored) {
8484
}
8585

86-
// TODO skip to turn
87-
// TODO implement tests
86+
// TODO test this
8887
logger.info("Loading game from file '{}' at turn {}", gameFile, turn);
8988
try {
90-
game = plugin.createGameFromState(new GameLoader<IGameState>(IGameState.class).loadGame(gameFile));
89+
game = plugin.createGameFromState(new GameLoader().loadGame(gameFile, turn));
9190
} catch(IOException e) {
9291
logger.error("Failed to load game from file", e);
9392
game = plugin.createGame();

0 commit comments

Comments
 (0)