12
12
import java .io .FileInputStream ;
13
13
import java .io .IOException ;
14
14
import java .io .InputStream ;
15
- import java .util .Arrays ;
16
- import java .util .List ;
17
15
import java .util .zip .GZIPInputStream ;
18
16
19
- public class GameLoader < T > implements IHistoryListener {
17
+ public class GameLoader implements IHistoryListener {
20
18
private static final Logger logger = LoggerFactory .getLogger (GameLoader .class );
21
19
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 ;
24
22
private GameLoaderClient client ;
25
23
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 ;
45
26
return loadGame (new FileInputStream (file ), file .getName ().endsWith (".gz" ));
46
27
}
47
28
48
- public T loadGame (FileInputStream stream , boolean gzip ) throws IOException {
29
+ public IGameState loadGame (FileInputStream stream , boolean gzip ) throws IOException {
49
30
if (gzip ) {
50
31
return loadGame (new GZIPInputStream (stream ));
51
32
} else {
52
33
return loadGame (stream );
53
34
}
54
35
}
55
36
56
- public T loadGame (InputStream file ) throws IOException {
37
+ public IGameState loadGame (InputStream file ) throws IOException {
57
38
client = new GameLoaderClient (file );
58
39
client .addListener (this );
59
40
client .start ();
@@ -79,15 +60,10 @@ public void onGameOver(String roomId, GameResult result) {
79
60
@ Override
80
61
public void onNewState (String roomId , IGameState state ) {
81
62
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 ();
91
67
}
92
68
}
93
69
0 commit comments