Skip to content

Commit

Permalink
Use atomics
Browse files Browse the repository at this point in the history
  • Loading branch information
Garanas committed May 11, 2024
1 parent 7d4ef66 commit 0f0aca2
Showing 1 changed file with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

public class ReplaySemantics {

Expand All @@ -22,21 +23,21 @@ public static Duration tickToDuration(int tick) {
* @return
*/
public static List<RegisteredEvent> registerEvents(List<Source> sources, List<Event> events) {
final int[] tick = {-1};
final int[] clientId = {-1};
final AtomicInteger tick = new AtomicInteger(0);
final AtomicInteger clientId = new AtomicInteger(-1);

return events.stream().map((event) -> switch (event) {
case Event.Advance e -> {
tick[0] = tick[0] + e.ticksToAdvance();
tick.addAndGet(e.ticksToAdvance());
yield null;
}

case Event.SetCommandSource e -> {
clientId[0] = e.playerIndex();
clientId.set(e.playerIndex());
yield null;
}

default -> new RegisteredEvent(tick[0], sources.get(clientId[0]), event);
default -> new RegisteredEvent(tick.intValue(), sources.get(clientId.intValue()), event);
}).filter(Objects::nonNull).toList();
}

Expand Down

0 comments on commit 0f0aca2

Please sign in to comment.