Skip to content

Commit

Permalink
Fix random games not showing correct player count
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Nov 22, 2023
1 parent 7949ff9 commit 3a590d3
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 171 deletions.
16 changes: 15 additions & 1 deletion src/main/java/xyz/nucleoid/plasmid/game/GameSpaceMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@
public record GameSpaceMetadata(
UUID id,
Identifier userId,
GameConfig<?> sourceConfig
GameConfig<?> sourceConfig,
GameConfig<?> originalSourceConfig
) {


public GameSpaceMetadata(
UUID id,
Identifier userId,
GameConfig<?> sourceConfig
) {
this(id, userId, sourceConfig, sourceConfig);
}
/**
* @return the globally unique ID for this {@link GameSpace}
*/
Expand All @@ -40,4 +50,8 @@ public Identifier userId() {
public GameConfig<?> sourceConfig() {
return this.sourceConfig;
}

public boolean isSourceConfig(GameConfig<?> gameConfig) {
return this.sourceConfig == gameConfig || this.originalSourceConfig == gameConfig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ public CompletableFuture<ManagedGameSpace> open(GameConfig<?> config) {
() -> config.openProcedure(this.server),
Util.getMainWorkerExecutor()
).thenApplyAsync(
procedure -> this.addGameSpace(procedure.configOverride() != null ? procedure.configOverride() : config, procedure),
procedure -> this.addGameSpace(procedure.configOverride() != null ? procedure.configOverride() : config, config, procedure),
this.server
);
}

private ManagedGameSpace addGameSpace(GameConfig<?> config, GameOpenProcedure procedure) {
private ManagedGameSpace addGameSpace(GameConfig<?> config, GameConfig<?> sourceConfig, GameOpenProcedure procedure) {
if (this.server == null) {
throw new RuntimeException("Not initialized yet!");
}
Expand All @@ -102,7 +102,7 @@ private ManagedGameSpace addGameSpace(GameConfig<?> config, GameOpenProcedure pr
var userId = this.userIds.acquire(config);
Preconditions.checkState(!this.userIdToGameSpace.containsKey(userId), "duplicate GameSpace user id acquired");

var metadata = new GameSpaceMetadata(id, userId, config);
var metadata = new GameSpaceMetadata(id, userId, config, sourceConfig);

var gameSpace = new ManagedGameSpace(this.server, this, metadata);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.function.Consumer;
import java.util.function.Function;

public final class ConcurrentGamePortalBackend implements GamePortalBackend {
public final class ConcurrentGamePortalBackend implements GameConfigGamePortalBackend {
private final Identifier gameId;
private CompletableFuture<ManagedGameSpace> gameFuture;

Expand All @@ -31,50 +31,8 @@ public ConcurrentGamePortalBackend(Identifier gameId) {
}

@Override
public void provideGameSpaces(Consumer<GameSpace> consumer) {
var gameConfig = GameConfigs.get(this.gameId);
for (var gameSpace : GameSpaceManager.get().getOpenGameSpaces()) {
if (gameSpace.getMetadata().sourceConfig() == gameConfig) {
consumer.accept(gameSpace);
}
}
}

@Override
public int getPlayerCount() {
int count = 0;
var gameConfig = GameConfigs.get(this.gameId);
for (var gameSpace : GameSpaceManager.get().getOpenGameSpaces()) {
if (gameSpace.getMetadata().sourceConfig() == gameConfig) {
count += gameSpace.getPlayers().size();
}
}
return count;
}

@Override
public List<Text> getDescription() {
var config = GameConfigs.get(this.gameId);
if (config != null) {
return config.description();
}

return Collections.emptyList();
}

@Override
public ItemStack getIcon() {
var config = GameConfigs.get(this.gameId);
if (config != null) {
return config.icon();
}

return Items.BARRIER.getDefaultStack();
}

@Override
public ActionType getActionType() {
return ActionType.PLAY;
public Identifier gameId() {
return this.gameId;
}

@Override
Expand Down Expand Up @@ -107,15 +65,7 @@ public void applyTo(ServerPlayerEntity player) {
}, player.server);
}

@Override
public Text getName() {
var config = GameConfigs.get(this.gameId);
if (config != null) {
return config.name();
} else {
return Text.literal(this.gameId.toString()).formatted(Formatting.RED);
}
}


public CompletableFuture<ManagedGameSpace> getOrOpenNew(MinecraftServer server) {
var future = this.gameFuture;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package xyz.nucleoid.plasmid.game.portal.game;

import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import xyz.nucleoid.plasmid.game.GameSpace;
import xyz.nucleoid.plasmid.game.config.GameConfigs;
import xyz.nucleoid.plasmid.game.manager.GameSpaceManager;
import xyz.nucleoid.plasmid.game.portal.GamePortalBackend;

import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

public interface GameConfigGamePortalBackend extends GamePortalBackend {
Identifier gameId();

@Override
default void provideGameSpaces(Consumer<GameSpace> consumer) {
var gameConfig = GameConfigs.get(this.gameId());
for (var gameSpace : GameSpaceManager.get().getOpenGameSpaces()) {
if (gameSpace.getMetadata().isSourceConfig(gameConfig)) {
consumer.accept(gameSpace);
}
}
}

@Override
default int getPlayerCount() {
int count = 0;
var gameConfig = GameConfigs.get(this.gameId());
for (var gameSpace : GameSpaceManager.get().getOpenGameSpaces()) {
if (gameSpace.getMetadata().isSourceConfig(gameConfig)) {
count += gameSpace.getPlayers().size();
}
}
return count;
}

@Override
default List<Text> getDescription() {
var config = GameConfigs.get(this.gameId());
if (config != null) {
return config.description();
}

return Collections.emptyList();
}

@Override
default ItemStack getIcon() {
var config = GameConfigs.get(this.gameId());
if (config != null) {
return config.icon();
}

return Items.BARRIER.getDefaultStack();
}

@Override
default Text getName() {
var config = GameConfigs.get(this.gameId());
if (config != null) {
return config.name();
} else {
return Text.literal(this.gameId().toString()).formatted(Formatting.RED);
}
}

@Override
default ActionType getActionType() {
return ActionType.PLAY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,7 @@
import java.util.function.Consumer;
import java.util.function.Function;

public record NewGamePortalBackend(Identifier gameId) implements GamePortalBackend {

@Override
public void provideGameSpaces(Consumer<GameSpace> consumer) {
var gameConfig = GameConfigs.get(this.gameId);
for (var gameSpace : GameSpaceManager.get().getOpenGameSpaces()) {
if (gameSpace.getMetadata().sourceConfig() == gameConfig) {
consumer.accept(gameSpace);
}
}
}

@Override
public int getPlayerCount() {
int count = 0;
var gameConfig = GameConfigs.get(this.gameId);
for (var gameSpace : GameSpaceManager.get().getOpenGameSpaces()) {
if (gameSpace.getMetadata().sourceConfig() == gameConfig) {
count += gameSpace.getPlayers().size();
}
}
return count;
}

@Override
public ActionType getActionType() {
return ActionType.PLAY;
}

public record NewGamePortalBackend(Identifier gameId) implements GameConfigGamePortalBackend {
@Override
public void applyTo(ServerPlayerEntity player) {
CompletableFuture.supplyAsync(() -> this.openGame(player.server))
Expand All @@ -69,36 +41,6 @@ public void applyTo(ServerPlayerEntity player) {
}, player.server);
}

@Override
public Text getName() {
var config = GameConfigs.get(this.gameId);
if (config != null) {
return config.name();
} else {
return Text.literal(this.gameId.toString()).formatted(Formatting.RED);
}
}

@Override
public List<Text> getDescription() {
var config = GameConfigs.get(this.gameId);
if (config != null) {
return config.description();
}

return Collections.emptyList();
}

@Override
public ItemStack getIcon() {
var config = GameConfigs.get(this.gameId);
if (config != null) {
return config.icon();
}

return Items.BARRIER.getDefaultStack();
}

private CompletableFuture<ManagedGameSpace> openGame(MinecraftServer server) {
var config = GameConfigs.get(this.gameId);
if (config == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,14 @@
import java.util.function.Consumer;
import java.util.function.Function;

public final class SingleGamePortalBackend implements GamePortalBackend {
public final class SingleGamePortalBackend implements GameConfigGamePortalBackend {
private final Identifier gameId;
private CompletableFuture<ManagedGameSpace> gameFuture;

public SingleGamePortalBackend(Identifier gameId) {
this.gameId = gameId;
}

@Override
public int getPlayerCount() {
var future = this.gameFuture;
if (future != null && future.isDone() && !future.isCompletedExceptionally()) {
var game = future.join();
return game.getPlayers().size();
}
return 0;
}

@Override
public void provideGameSpaces(Consumer<GameSpace> consumer) {
var future = this.gameFuture;
if (future != null && future.isDone() && !future.isCompletedExceptionally()) {
consumer.accept(future.join());
}
}

@Override
public void applyTo(ServerPlayerEntity player) {
CompletableFuture.supplyAsync(() -> this.getOrOpen(player.server))
Expand All @@ -68,41 +50,6 @@ public void applyTo(ServerPlayerEntity player) {
}, player.server);
}

@Override
public Text getName() {
var config = GameConfigs.get(this.gameId);
if (config != null) {
return config.name();
} else {
return Text.literal(this.gameId.toString()).formatted(Formatting.RED);
}
}

@Override
public ActionType getActionType() {
return ActionType.PLAY;
}

@Override
public List<Text> getDescription() {
var config = GameConfigs.get(this.gameId);
if (config != null) {
return config.description();
}

return Collections.emptyList();
}

@Override
public ItemStack getIcon() {
var config = GameConfigs.get(this.gameId);
if (config != null) {
return config.icon();
}

return Items.BARRIER.getDefaultStack();
}

public CompletableFuture<ManagedGameSpace> getOrOpen(MinecraftServer server) {
var future = this.gameFuture;
if (future == null) {
Expand Down Expand Up @@ -135,6 +82,11 @@ private CompletableFuture<ManagedGameSpace> openGame(MinecraftServer server) {
}, server);
}

@Override
public Identifier gameId() {
return this.gameId;
}

private class LifecycleListeners implements GameLifecycle.Listeners {
@Override
public void onClosing(GameSpace gameSpace, GameCloseReason reason) {
Expand Down

0 comments on commit 3a590d3

Please sign in to comment.