Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add game packets #126

Open
wants to merge 4 commits into
base: 1.16
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions doc/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,54 @@ Size: 0 if non-present, else size of `X`.

## Packets

### Game-related Packets

#### `plasmid:game/player_add`

Packet sent to all players of a game space when a player is joined to a game space.
The player that was joined to the game space also receives this packet.

Direction: `C<-S`

| Fields | Type | Description |
|:--------------:|:----------:|:----------------------------------------------------------------|
| game_type_id | Identifier | The identifier of the game type. |
| game_type_name | Text | The name of the game type. |
| game_id | Identifier | The identifier of the game. |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, I'm not sure if this should be included in the packet. Relying on game ids is never a good idea.

Copy link
Contributor Author

@haykam821 haykam821 Feb 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any specific reason why packet consumers should not be able to have access to the game ID?

To be more specific, providing game type IDs (which I am not sure whether you are against as well) would allow packet consumers to distinguish between game types for icons, and providing game IDs would allow packet consumers to differentiate by individual game as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They can’t and shouldn’t be relied on by anything. Games don’t have an ID- hence the rename to source. It’s just where the game was loaded from, and that might not even be present in the case of an anonymously created game.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packet consumers would be required to handle anonymous games, then. If the identifier was removed, packet consumers would just end up relying on text, which is worse. After all, games can have no text as well (though, this just falls back rather than being null).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They should probably rather be passed all the game custom values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would that custom value not just be the same as the game ID? In addition, would all anonymous games have that custom value as well? It would duplicate the existing game ID for not much benefit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No- it would provide whatever values are needed directly. Anonymous games are an example of where things would not have a game id, but really the motivation in this case is they can't be considered to be stable. A new game config can be added, or a config can be moved around- but then you have to update the map within the tracker to consider that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The game ID is the thing that this map would need, and for the problem the tracker aims to solve, there is no way around it; any attempts at providing a custom value replacement would just duplicate the ID.

I think packet consumers can handle the task of recognizing that the given game IDs are not stable and may not exist. The tracker would ignore games without IDs and treat games with changed IDs as entirely separate games, unless the user chooses to merge two game IDs.

Copy link
Member

@Gegy Gegy Feb 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point is to not have the map in the first place, but rather the values within the map provided within the game. What do you imagine this map would be used to store? I suspect I am misunderstanding something and causing us to not get anywhere 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The map would store play counts for each game.

| game_name | Text | The name of the game. |
| player_count | int | The new player count of the game space after the player joined. |
| player_uuid | UUID | The UUID of the player that joined the game space. |

#### `plasmid:game/player_removed`

Packet sent to all players of a game space when a player is removed from a game space.
The player that was removed from the game space also receives this packet.

Direction: `C<-S`

| Fields | Type | Description |
|:--------------:|:----------:|:--------------------------------------------------------------------|
| game_type_id | Identifier | The identifier of the game type. |
| game_type_name | Text | The name of the game type. |
| game_id | Identifier | The identifier of the game. |
| game_name | Text | The name of the game. |
| player_count | int | The new player count of the game space after the player is removed. |
| player_uuid | UUID | The UUID of the player that was removed from the game space. |

#### `plasmid:game/game_close`

Packet sent to all players in a game space when it closes.

Direction: `C<-S`

| Fields | Type | Description |
|:--------------:|:---------------:|:-----------------------------------|
| game_type_id | Identifier | The identifier of the game type. |
| game_type_name | Text | The name of the game type. |
| game_id | Identifier | The identifier of the game. |
| game_name | Text | The name of the game. |
| reason | GameCloseReason | The reason for the game's closure. |

### Workspace-related Packets

The server controls most of the requests sent by a client for workspace-related packets as it requires some permissions.
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/xyz/nucleoid/plasmid/event/GamePackets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package xyz.nucleoid.plasmid.event;

import java.util.function.Consumer;

import io.netty.buffer.Unpooled;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import xyz.nucleoid.plasmid.Plasmid;
import xyz.nucleoid.plasmid.game.ConfiguredGame;
import xyz.nucleoid.plasmid.game.GameCloseReason;
import xyz.nucleoid.plasmid.game.GameSpace;
import xyz.nucleoid.plasmid.game.GameType;

/**
* A helper class for creating {@linkplain net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket custom payload packets} to send to clients.
*/
public final class GamePackets {
private GamePackets() { }

public static CustomPayloadS2CPacket playerAdd(GameSpace gameSpace, ServerPlayerEntity player) {
return createPacket("player_add", buf -> writeGameAndPlayerInfo(buf, gameSpace, player));
}

public static CustomPayloadS2CPacket playerRemove(GameSpace gameSpace, ServerPlayerEntity player) {
return createPacket("player_remove", buf -> writeGameAndPlayerInfo(buf, gameSpace, player));
}

public static CustomPayloadS2CPacket gameClose(GameSpace gameSpace, GameCloseReason reason) {
return createPacket("game_close", buf -> {
writeGameInfo(buf, gameSpace);
buf.writeString(reason == GameCloseReason.FINISHED ? "finished" : "canceled");
});
}

private static void writeGameAndPlayerInfo(PacketByteBuf buf, GameSpace gameSpace, ServerPlayerEntity player) {
writeGameInfo(buf, gameSpace);
writePlayerInfo(buf, gameSpace, player);
}

private static void writeGameInfo(PacketByteBuf buf, GameSpace gameSpace) {
ConfiguredGame<?> game = gameSpace.getGameConfig();
GameType<?> gameType = game.getType();

// Game type ID and name
buf.writeIdentifier(gameType.getIdentifier());
buf.writeText(gameType.getName());

// Game ID and name
buf.writeIdentifier(game.getSource());
buf.writeText(game.getNameText());
}

private static void writePlayerInfo(PacketByteBuf buf, GameSpace gameSpace, ServerPlayerEntity player) {
buf.writeInt(gameSpace.getPlayerCount());
buf.writeUuid(player.getUuid());
}

private static CustomPayloadS2CPacket createPacket(String type, Consumer<PacketByteBuf> writer) {
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
writer.accept(buf);

return new CustomPayloadS2CPacket(createChannel(type), buf);
}

private static Identifier createChannel(String type) {
return new Identifier(Plasmid.ID, "game/" + type);
}
}
12 changes: 12 additions & 0 deletions src/main/java/xyz/nucleoid/plasmid/game/ManagedGameSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.collect.Lists;
import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
import net.minecraft.network.Packet;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
Expand All @@ -24,6 +25,7 @@
import xyz.nucleoid.leukocyte.shape.ProtectionShape;
import xyz.nucleoid.plasmid.error.ErrorReporter;
import xyz.nucleoid.plasmid.event.GameEvents;
import xyz.nucleoid.plasmid.event.GamePackets;
import xyz.nucleoid.plasmid.game.event.*;
import xyz.nucleoid.plasmid.game.player.JoinResult;
import xyz.nucleoid.plasmid.game.player.MutablePlayerSet;
Expand Down Expand Up @@ -238,6 +240,7 @@ public boolean addPlayer(ServerPlayerEntity player) {
}

this.lifecycle.addPlayer(this, player);
this.players.sendPacket(GamePackets.playerAdd(this, player));

return true;
}
Expand Down Expand Up @@ -265,6 +268,10 @@ private void onRemovePlayer(ServerPlayerEntity player) {
this.players.remove(player);
this.lifecycle.removePlayer(this, player);

Packet<?> packet = GamePackets.playerRemove(this, player);
this.players.sendPacket(packet);
player.networkHandler.sendPacket(packet);

if (this.getPlayerCount() <= 0) {
this.close(GameCloseReason.CANCELED);
}
Expand Down Expand Up @@ -362,6 +369,11 @@ public void close(GameCloseReason reason) {

this.lifecycle.onClosed(this, players, reason);

Packet<?> packet = GamePackets.gameClose(this, reason);
for (ServerPlayerEntity player : players) {
player.networkHandler.sendPacket(packet);
}

Leukocyte leukocyte = Leukocyte.get(this.getServer());
leukocyte.removeAuthority(this.ruleAuthority);

Expand Down