Skip to content

Commit ca82f1a

Browse files
committed
refactor(framework): make ScoreDefinition immutable
1 parent 3f083bc commit ca82f1a

File tree

6 files changed

+23
-36
lines changed

6 files changed

+23
-36
lines changed

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,11 @@ public class GamePlugin implements IGamePlugin {
1818
public static final String PLUGIN_AUTHOR = "";
1919
public static final String PLUGIN_UUID = "swc_2020_hive";
2020

21-
public static final ScoreDefinition SCORE_DEFINITION;
22-
23-
static {
24-
SCORE_DEFINITION = new ScoreDefinition();
25-
SCORE_DEFINITION.add("Gewinner");
26-
// NOTE: Always write the XML representation of unicode characters, not the character directly, as it confuses the
27-
// parsers which consume the server messages!
28-
SCORE_DEFINITION.add(new ScoreFragment("\u2205 freie Felder", ScoreAggregation.AVERAGE));
29-
}
21+
// NOTE: Always write the XML representation of unicode characters, not the character directly, as it confuses the
22+
// parsers which consume the server messages!
23+
public static final ScoreDefinition SCORE_DEFINITION = new ScoreDefinition(new ScoreFragment[]{
24+
new ScoreFragment("Gewinner"),
25+
new ScoreFragment("\u2205 freie Felder", ScoreAggregation.AVERAGE)});
3026

3127
@Override
3228
public IGameInstance createGame() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public void addResultToScore(GameResult result, String name1, String name2) thro
284284
final List<PlayerScore> playerScores = result.getScores();
285285
firstScore.setNumberOfTests(firstScore.getNumberOfTests() + 1);
286286
secondScore.setNumberOfTests(secondScore.getNumberOfTests() + 1);
287-
for (int i = 0; i < scoreDefinition.size(); i++) {
287+
for (int i = 0; i < scoreDefinition.getSize(); i++) {
288288
ScoreFragment fragment = scoreDefinition.get(i);
289289
ScoreValue firstValue = firstScore.getScoreValues().get(i);
290290
ScoreValue secondValue = secondScore.getScoreValues().get(i);

server/test/sc/server/plugins/TestPlugin.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,7 @@
1111
public class TestPlugin implements IGamePlugin {
1212
public static final String TEST_PLUGIN_UUID = "012345-norris";
1313

14-
public static final ScoreDefinition SCORE_DEFINITION;
15-
16-
static {
17-
SCORE_DEFINITION = new ScoreDefinition();
18-
SCORE_DEFINITION.add("winner");
19-
}
20-
21-
public TestPlugin() {
22-
23-
}
14+
public static final ScoreDefinition SCORE_DEFINITION = new ScoreDefinition("winner");
2415

2516
@Override
2617
public IGameInstance createGame() {
@@ -37,8 +28,6 @@ public void initialize(IGamePluginHost host) {
3728

3829
@Override
3930
public void unload() {
40-
// TODO Auto-generated method stub
41-
4231
}
4332

4433
@Override

socha-sdk/src/framework/sc/shared/Score.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ public int getNumberOfTests() {
4343
}
4444

4545
public ScoreDefinition getScoreDefinition() {
46-
ScoreDefinition scoreDefinition = new ScoreDefinition();
47-
for (ScoreValue scoreValue : this) {
48-
scoreDefinition.add(scoreValue.getFragment());
49-
}
50-
return scoreDefinition;
46+
return new ScoreDefinition((ScoreFragment[]) scoreValues.stream().map(ScoreValue::getFragment).toArray());
5147
}
5248

5349
public List<ScoreValue> getScoreValues() {

socha-sdk/src/framework/sc/shared/ScoreDefinition.kt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,35 @@ package sc.shared
22

33
import com.thoughtworks.xstream.annotations.XStreamAlias
44
import com.thoughtworks.xstream.annotations.XStreamImplicit
5+
import com.thoughtworks.xstream.annotations.XStreamOmitField
56

67
@XStreamAlias(value = "scoreDefinition")
78
class ScoreDefinition(
89
@XStreamImplicit(itemFieldName = "fragment")
9-
private val fragments: MutableList<ScoreFragment> = ArrayList()
10-
): List<ScoreFragment> by fragments {
10+
private val fragments: Array<ScoreFragment>
11+
): Iterable<ScoreFragment>, RandomAccess {
1112

12-
fun add(string: String) =
13-
add(ScoreFragment(string))
13+
constructor(vararg fragments: String): this(fragments.map { ScoreFragment(it) }.toTypedArray())
1414

15-
fun add(fragment: ScoreFragment) =
16-
fragments.add(fragment)
15+
@XStreamOmitField
16+
val size = fragments.size
1717

1818
val isValid: Boolean
1919
get() = size > 0
2020

21+
operator fun get(index: Int) =
22+
fragments[index]
23+
24+
override fun iterator() =
25+
fragments.iterator()
26+
2127
override fun toString() =
2228
"ScoreDefinition[${fragments.joinToString()}]"
2329

2430
override fun equals(other: Any?): Boolean =
25-
other is ScoreDefinition && fragments == other.fragments
31+
other is ScoreDefinition && fragments.contentEquals(other.fragments)
2632

2733
override fun hashCode() =
28-
fragments.hashCode()
34+
fragments.contentHashCode()
2935

3036
}

socha-sdk/src/test/sc/shared/GameResultTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class GameResultTest: StringSpec({
1010
val xstream = XStream().apply {
1111
setMode(XStream.NO_REFERENCES)
1212
}
13-
val definition = ScoreDefinition().apply { add("winner") }
13+
val definition = ScoreDefinition("winner")
1414
val scores: List<PlayerScore> = listOf(
1515
PlayerScore(ScoreCause.REGULAR, "test", 1),
1616
PlayerScore(ScoreCause.LEFT, "second test", 0)

0 commit comments

Comments
 (0)