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

fix: minutes in tag packets and tag server commands #56

Merged
merged 5 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
55 changes: 35 additions & 20 deletions Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,13 +456,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 @@ -471,12 +476,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 @@ -490,17 +500,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
Loading