Skip to content

Commit 85290f9

Browse files
committed
rework: centralize XStream instantiation & remove plugin api
1 parent 7b6d116 commit 85290f9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+169
-315
lines changed

helpers/test-client/src/sc/TestClient.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import ch.qos.logback.classic.Level;
44
import ch.qos.logback.classic.Logger;
5-
import com.thoughtworks.xstream.XStream;
65
import jargs.gnu.CmdLineParser;
76
import jargs.gnu.CmdLineParser.Option;
87
import org.slf4j.LoggerFactory;
@@ -11,7 +10,6 @@
1110
import sc.networking.TcpNetwork;
1211
import sc.networking.clients.XStreamClient;
1312
import sc.plugin2021.util.Constants;
14-
import sc.protocol.helpers.LobbyProtocol;
1513
import sc.protocol.requests.*;
1614
import sc.protocol.responses.*;
1715
import sc.server.Configuration;
@@ -21,7 +19,6 @@
2119
import java.io.IOException;
2220
import java.net.Socket;
2321
import java.util.Arrays;
24-
import java.util.Collection;
2522
import java.util.List;
2623
import java.util.concurrent.ExecutorService;
2724
import java.util.concurrent.Executors;
@@ -132,7 +129,7 @@ public static void main(String[] args) {
132129
Runtime.getRuntime().addShutdownHook(new Thread(server::destroyForcibly));
133130
Thread.sleep(1000);
134131
}
135-
testclient = new TestClient(Configuration.getXStream(), sc.plugin2020.util.Configuration.getClassesToRegister(), host, port, numberOfTests);
132+
testclient = new TestClient(host, port, numberOfTests);
136133
Runtime.getRuntime().addShutdownHook(new Thread(testclient::printScores));
137134
} catch (Exception e) {
138135
logger.error("Error while initializing: " + e.toString());
@@ -155,11 +152,9 @@ public static void main(String[] args) {
155152

156153
private ExecutorService waiter = Executors.newSingleThreadExecutor();
157154

158-
public TestClient(XStream xstream, Collection<Class<?>> protocolClasses,
159-
String host, int port, int totalTests) throws IOException {
160-
super(xstream, createTcpNetwork(host, port));
161-
LobbyProtocol.registerMessages(xstream);
162-
LobbyProtocol.registerAdditionalMessages(xstream, protocolClasses);
155+
public TestClient(String host, int port, int totalTests) throws IOException {
156+
super(createTcpNetwork(host, port));
157+
163158
this.host = host;
164159
this.port = port;
165160
this.totalTests = totalTests;

plugin/src/client/sc/plugin2020/AbstractClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public abstract class AbstractClient implements ILobbyClientListener {
4848
public AbstractClient(String host, int port, PlayerType id) throws IOException {
4949
this.gameType = GamePlugin.PLUGIN_UUID;
5050
try {
51-
this.client = new LobbyClient(Configuration.getXStream(), Configuration.getClassesToRegister(), host, port);
51+
this.client = new LobbyClient(host, port);
5252
} catch(ConnectException e) {
5353
logger.error("Could not connect to Server: " + e.getMessage());
5454
System.exit(1);

plugin/src/client/sc/plugin2021/AbstractClient.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import sc.framework.plugins.protocol.MoveRequest
77
import sc.networking.clients.IControllableGame
88
import sc.networking.clients.ILobbyClientListener
99
import sc.networking.clients.LobbyClient
10-
import sc.plugin2021.util.Configuration
1110
import sc.protocol.responses.PrepareGameProtocolMessage
1211
import sc.protocol.responses.ProtocolErrorMessage
1312
import sc.protocol.responses.ProtocolMessage
@@ -39,8 +38,8 @@ abstract class AbstractClient @Throws(IOException::class) constructor(
3938
protected var handler: IGameHandler? = null
4039

4140
/** The lobby client that connects to the room. Stops on connection failure. */
42-
private val client = try {
43-
LobbyClient(Configuration.xStream, Configuration.classesToRegister, host, port)
41+
private val client: LobbyClient = try {
42+
LobbyClient(host, port)
4443
} catch (e: ConnectException) {
4544
logger.error("Could not connect to Server: ${e.message}")
4645
exitProcess(1)

plugin/src/server/sc/plugin2020/Game.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public WinCondition checkWinCondition() {
201201
public void loadFromFile(String file) {
202202
logger.info("Loading game from: " + file);
203203
GameLoader gl = new GameLoader(GameState.class);
204-
Object gameInfo = gl.loadGame(Configuration.getXStream(), file);
204+
Object gameInfo = gl.loadGame(file);
205205
if(gameInfo != null) {
206206
loadGameInfo(gameInfo);
207207
}
@@ -239,7 +239,7 @@ public void loadFromFile(String file, int turn) {
239239
e.printStackTrace();
240240
}
241241
GameLoader gl = new GameLoader(GameState.class);
242-
Object gameInfo = gl.loadGame(Configuration.getXStream(), "./tmp_replay.xml");
242+
Object gameInfo = gl.loadGame("./tmp_replay.xml");
243243
if(gameInfo != null) {
244244
loadGameInfo(gameInfo);
245245
}

plugin/src/server/sc/plugin2020/GamePlugin.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import sc.api.plugins.IGameInstance;
44
import sc.api.plugins.IGamePlugin;
5-
import sc.api.plugins.host.IGamePluginHost;
5+
import sc.helpers.XStreamKt;
66
import sc.plugin2020.util.Configuration;
77
import sc.plugins.PluginDescriptor;
8+
import sc.protocol.helpers.LobbyProtocol;
89
import sc.shared.ScoreAggregation;
910
import sc.shared.ScoreDefinition;
1011
import sc.shared.ScoreFragment;
@@ -30,8 +31,8 @@ public IGameInstance createGame() {
3031
}
3132

3233
@Override
33-
public void initialize(IGamePluginHost host) {
34-
host.registerProtocolClasses(Configuration.getClassesToRegister());
34+
public void initialize() {
35+
LobbyProtocol.registerAdditionalMessages(XStreamKt.getXStream(), Configuration.getClassesToRegister());
3536
}
3637

3738
@Override

plugin/src/server/sc/plugin2021/GamePlugin.kt

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package sc.plugin2021
22

33
import sc.api.plugins.IGameInstance
44
import sc.api.plugins.IGamePlugin
5-
import sc.api.plugins.host.IGamePluginHost
6-
import sc.plugin2021.util.Configuration.classesToRegister
5+
import sc.plugin2021.xstream.BoardConverter
76
import sc.plugins.PluginDescriptor
7+
import sc.protocol.helpers.LobbyProtocol
88
import sc.shared.ScoreAggregation
99
import sc.shared.ScoreDefinition
1010
import sc.shared.ScoreFragment
11+
import sc.helpers.xStream as xstream
1112

1213
@PluginDescriptor(name = "Blokus", uuid = "swc_2021_blokus", author = "")
1314
class GamePlugin: IGamePlugin {
@@ -20,19 +21,34 @@ class GamePlugin: IGamePlugin {
2021
ScoreFragment("Gewinner"),
2122
ScoreFragment("\u2205 Punkte", ScoreAggregation.AVERAGE)
2223
))
24+
25+
private val classesToRegister: List<Class<*>>
26+
get() = listOf(Board::class.java, Coordinates::class.java,
27+
Field::class.java, GameState::class.java,
28+
Move::class.java, Piece::class.java,
29+
Color::class.java, Team::class.java)
30+
31+
fun registerXStream() {
32+
LobbyProtocol.registerAdditionalMessages(xstream, classesToRegister)
33+
34+
xstream.registerConverter(BoardConverter())
35+
}
36+
37+
@JvmStatic
38+
val xStream by lazy {
39+
registerXStream()
40+
xstream
41+
}
2342
}
2443

2544
override fun createGame(): IGameInstance {
2645
return Game()
2746
}
2847

29-
override fun initialize(host: IGamePluginHost) {
30-
host.registerProtocolClasses(classesToRegister)
31-
}
32-
33-
override fun unload() {
34-
TODO("Not yet implemented")
48+
override fun initialize() {
49+
registerXStream()
3550
}
3651

3752
override fun getScoreDefinition(): ScoreDefinition = SCORE_DEFINITION
53+
3854
}

plugin/src/shared/sc/plugin2021/util/Configuration.kt

Lines changed: 0 additions & 29 deletions
This file was deleted.

plugin/src/test/sc/plugin2021/GameStateTest.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import io.kotest.matchers.shouldBe
44
import io.kotest.core.spec.style.StringSpec
55
import org.junit.jupiter.api.assertDoesNotThrow
66
import org.junit.jupiter.api.assertThrows
7-
import sc.plugin2021.util.Configuration
7+
import sc.plugin2021.GamePlugin.Companion.xStream
88
import sc.plugin2021.util.GameRuleLogic
99
import sc.shared.InvalidMoveException
1010

@@ -68,13 +68,12 @@ class GameStateTest: StringSpec({
6868

6969
}
7070
"XML conversion works" {
71-
val xstream = Configuration.xStream
7271
val state = GameState()
7372

74-
xstream.fromXML(xstream.toXML(state)).toString() shouldBe state.toString()
75-
xstream.fromXML(xstream.toXML(state)) shouldBe state
73+
xStream.fromXML(xStream.toXML(state)).toString() shouldBe state.toString()
74+
xStream.fromXML(xStream.toXML(state)) shouldBe state
7675

77-
val transformed = xstream.fromXML(xstream.toXML(GameState())) as GameState
76+
val transformed = xStream.fromXML(xStream.toXML(GameState())) as GameState
7877
transformed.deployedPieces shouldBe null
7978
GameRuleLogic.isFirstMove(transformed) shouldBe true
8079
transformed.getPointsForPlayer(Team.ONE)

plugin/src/test/sc/plugin2021/GameTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import io.kotest.matchers.shouldBe
44
import io.kotest.matchers.shouldNotBe
55
import io.kotest.core.spec.style.StringSpec
66
import io.kotest.matchers.collections.shouldContainExactly
7-
import sc.plugin2020.util.Constants
7+
import sc.plugin2021.util.Constants
88
import sc.plugin2021.util.GameRuleLogic
99
import sc.shared.PlayerScore
1010
import sc.shared.ScoreCause

plugin/src/test/sc/plugin2021/PieceTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.kotest.matchers.maps.shouldContain
77
import io.kotest.matchers.maps.shouldContainExactly
88
import io.kotest.matchers.shouldBe
99
import org.opentest4j.AssertionFailedError
10+
import sc.plugin2021.GamePlugin.Companion.xStream
1011
import sc.plugin2021.util.*
1112

1213

@@ -186,9 +187,9 @@ class PieceTest: StringSpec({
186187
</piece>
187188
""".trimIndent())
188189
) {piece, xml ->
189-
Configuration.xStream.toXML(piece) shouldBe xml
190+
xStream.toXML(piece) shouldBe xml
190191

191-
val converted = Configuration.xStream.fromXML(Configuration.xStream.toXML(piece)) as Piece
192+
val converted = xStream.fromXML(xStream.toXML(piece)) as Piece
192193
converted.toString() shouldBe piece.toString()
193194
converted shouldBe piece
194195
}

0 commit comments

Comments
 (0)