Skip to content

Commit 0be3987

Browse files
authored
Merge pull request #11 from Zwazel-Teaching-Projects/fix/add-unique-messages
no more duplicate messages (if unique) on java side!
2 parents 346d756 + 269ebab commit 0be3987

File tree

6 files changed

+59
-2
lines changed

6 files changed

+59
-2
lines changed

src/main/java/dev/zwazel/internal/GameSimulationThread.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
import java.io.DataOutputStream;
1313
import java.io.IOException;
1414
import java.nio.charset.StandardCharsets;
15-
import java.util.ArrayList;
16-
import java.util.List;
15+
import java.util.*;
1716
import java.util.concurrent.BlockingQueue;
1817

1918
@RequiredArgsConstructor
@@ -79,6 +78,31 @@ private void sendTickMessages() throws IOException {
7978
return;
8079
}
8180

81+
// Use a set to track which unique message types have been encountered (from the end)
82+
Set<Class<?>> seenUniqueTypes = new HashSet<>();
83+
// Iterate backward to keep the most recent message and remove older duplicates
84+
ListIterator<MessageContainer> iterator = messageList.listIterator(messageList.size());
85+
while (iterator.hasPrevious()) {
86+
MessageContainer current = iterator.previous();
87+
if (current.getMessage().isUnique()) {
88+
if (seenUniqueTypes.contains(current.getMessage().getClass())) {
89+
// Remove this outdated message of the same unique type
90+
iterator.remove();
91+
92+
if (internalWorld.isInternalDebug()) {
93+
System.out.println("Duplicate message type removed: " + current.getMessage().getClass().getSimpleName());
94+
}
95+
} else {
96+
// Mark this unique type as encountered
97+
seenUniqueTypes.add(current.getMessage().getClass());
98+
99+
if (internalWorld.isInternalDebug()) {
100+
System.out.println("Unique message type: " + current.getMessage().getClass().getSimpleName());
101+
}
102+
}
103+
}
104+
}
105+
82106
messageList.forEach(message -> message.applyBeforeSend(internalWorld));
83107

84108
String json = objectMapper.writeValueAsString(messageList);

src/main/java/dev/zwazel/internal/message/MessageData.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.zwazel.internal.message;
22

3+
import com.fasterxml.jackson.annotation.JsonIgnore;
34
import com.fasterxml.jackson.annotation.JsonSubTypes;
45
import com.fasterxml.jackson.annotation.JsonTypeInfo;
56
import dev.zwazel.internal.InternalGameWorld;
@@ -57,4 +58,16 @@ default void applyBeforeSend(InternalGameWorld world) {
5758
*/
5859
default void applyOnAddingToQueue(InternalGameWorld world) {
5960
}
61+
62+
/**
63+
* Determines if the message is unique.
64+
* If it is unique, before sending all messages to the server at the end of the tick, we remove all other messages of the same type.
65+
* So only the latest message of this type is sent.
66+
*
67+
* @return if the message is unique
68+
*/
69+
@JsonIgnore
70+
default boolean isUnique() {
71+
return false;
72+
}
6073
}

src/main/java/dev/zwazel/internal/message/data/tank/MoveTankCommand.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ public void applyOnAddingToQueue(InternalGameWorld world) {
2424
predictedState.transformTurret(), predictedState.state(), predictedState.shootCooldown(), predictedState.currentHealth());
2525
world.updatePredictedState(predictedState);
2626
}
27+
28+
@Override
29+
public boolean isUnique() {
30+
return true;
31+
}
2732
}

src/main/java/dev/zwazel/internal/message/data/tank/RotateTankBodyCommand.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ public void applyOnAddingToQueue(InternalGameWorld world) {
2323
predictedState.transformTurret(), predictedState.state(), predictedState.shootCooldown(), predictedState.currentHealth());
2424
world.updatePredictedState(predictedState);
2525
}
26+
27+
@Override
28+
public boolean isUnique() {
29+
return true;
30+
}
2631
}

src/main/java/dev/zwazel/internal/message/data/tank/RotateTankTurretCommand.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ public void applyOnAddingToQueue(InternalGameWorld world) {
2828
predictedState = new ClientState(predictedState.id(), currentBodyTransform, newTurretTransform, predictedState.state(), predictedState.shootCooldown(), predictedState.currentHealth());
2929
world.updatePredictedState(predictedState);
3030
}
31+
32+
@Override
33+
public boolean isUnique() {
34+
return true;
35+
}
3136
}

src/main/java/dev/zwazel/internal/message/data/tank/ShootCommand.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ public void applyOnAddingToQueue(InternalGameWorld world) {
2424
world.updatePredictedState(new ClientState(predictedState.id(), predictedState.transformBody(),
2525
predictedState.transformTurret(), predictedState.state(), tankConfig.shootCooldown(), predictedState.currentHealth()));
2626
}
27+
28+
@Override
29+
public boolean isUnique() {
30+
return true;
31+
}
2732
}

0 commit comments

Comments
 (0)