Skip to content

Commit

Permalink
Merge branch 'pr-tag-cmd' into json-api
Browse files Browse the repository at this point in the history
  • Loading branch information
Istador committed Jun 21, 2024
2 parents 29878e5 + e160acc commit b4eff9e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
55 changes: 35 additions & 20 deletions Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -463,13 +463,18 @@ await c.Send(new ChangeStagePacket {
return $"Invalid time for seconds {args[3]} (range: 0-59)";
TagPacket tagPacket = new TagPacket {
UpdateType = TagPacket.TagUpdate.Time,
Minutes = minutes,
Seconds = seconds
Minutes = minutes,
Seconds = seconds,
};
if (args[1] == "*")
server.Broadcast(tagPacket);
else
client?.Send(tagPacket);
if (args[1] == "*") {
Parallel.ForEachAsync(server.Clients, async (client, _) => {
await server.Broadcast(tagPacket, client);
await client.Send(tagPacket);
});
} else if (client != null) {
server.Broadcast(tagPacket, client);
client.Send(tagPacket);
}
return $"Set time for {(args[1] == "*" ? "everyone" : args[1])} to {minutes}:{seconds}";
}
case "seeking" when args.Length == 3: {
Expand All @@ -478,12 +483,17 @@ await c.Send(new ChangeStagePacket {
if (!bool.TryParse(args[2], out bool seeking)) return $"Usage: tag seeking {args[1]} <true/false>";
TagPacket tagPacket = new TagPacket {
UpdateType = TagPacket.TagUpdate.State,
IsIt = seeking
IsIt = seeking,
};
if (args[1] == "*")
server.Broadcast(tagPacket);
else
client?.Send(tagPacket);
if (args[1] == "*") {
Parallel.ForEachAsync(server.Clients, async (client, _) => {
await server.Broadcast(tagPacket, client);
await client.Send(tagPacket);
});
} else if (client != null) {
server.Broadcast(tagPacket, client);
client.Send(tagPacket);
}
return $"Set {(args[1] == "*" ? "everyone" : args[1])} to {(seeking ? "seeker" : "hider")}";
}
case "start" when args.Length > 2: {
Expand All @@ -497,17 +507,22 @@ await c.Send(new ChangeStagePacket {
int realTime = 1000 * time;
await Task.Delay(realTime);
await Task.WhenAll(
Parallel.ForEachAsync(seekers, async (seeker, _) =>
await server.Broadcast(new TagPacket {
Parallel.ForEachAsync(seekers, async (seeker, _) => {
TagPacket packet = new TagPacket {
UpdateType = TagPacket.TagUpdate.State,
IsIt = true
}, seeker)),
Parallel.ForEachAsync(server.Clients.Except(seekers), async (hider, _) =>
await server.Broadcast(new TagPacket {
IsIt = true,
};
await server.Broadcast(packet, seeker);
await seeker.Send(packet);
}),
Parallel.ForEachAsync(server.Clients.Except(seekers), async (hider, _) => {
TagPacket packet = new TagPacket {
UpdateType = TagPacket.TagUpdate.State,
IsIt = false
}, hider)
)
IsIt = false,
};
await server.Broadcast(packet, hider);
await hider.Send(packet);
})
);
consoleLogger.Info($"Started game with seekers {string.Join(", ", seekerNames)}");
});
Expand Down
4 changes: 2 additions & 2 deletions Shared/Packet/Packets/TagPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public void Serialize(Span<byte> data) {
MemoryMarshal.Write(data, ref UpdateType);
MemoryMarshal.Write(data[1..], ref IsIt);
MemoryMarshal.Write(data[2..], ref Seconds);
MemoryMarshal.Write(data[4..], ref Minutes);
MemoryMarshal.Write(data[3..], ref Minutes);
}

public void Deserialize(ReadOnlySpan<byte> data) {
UpdateType = MemoryMarshal.Read<TagUpdate>(data);
IsIt = MemoryMarshal.Read<bool>(data[1..]);
Seconds = MemoryMarshal.Read<byte>(data[2..]);
Minutes = MemoryMarshal.Read<ushort>(data[4..]);
Minutes = MemoryMarshal.Read<ushort>(data[3..]);
}

[Flags]
Expand Down

0 comments on commit b4eff9e

Please sign in to comment.