From 344f53f4784a79c49361ba2133a3ac6154a9e954 Mon Sep 17 00:00:00 2001 From: VirxEC Date: Wed, 17 Jul 2024 21:43:23 -0400 Subject: [PATCH] Basic implementation of match comms --- RLBotCS/Server/FlatBuffersSession.cs | 13 +++++++++- .../FlatbuffersMessage/SendMatchComm.cs | 25 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 RLBotCS/Server/FlatbuffersMessage/SendMatchComm.cs diff --git a/RLBotCS/Server/FlatBuffersSession.cs b/RLBotCS/Server/FlatBuffersSession.cs index c8162a4..487420c 100644 --- a/RLBotCS/Server/FlatBuffersSession.cs +++ b/RLBotCS/Server/FlatBuffersSession.cs @@ -23,6 +23,8 @@ public record RendersAllowed(bool Allowed) : SessionMessage; public record StateSettingAllowed(bool Allowed) : SessionMessage; + public record MatchComm(MatchCommT matchComm) : SessionMessage; + public record StopMatch : SessionMessage; } @@ -120,7 +122,7 @@ private async Task ParseClientMessage(TypedPayload message) break; var matchComms = MatchComm.GetRootAsMatchComm(byteBuffer).UnPack(); - // todo: send to server to send to other clients + await _rlbotServer.WriteAsync(new SendMatchComm(_clientId, matchComms)); break; @@ -209,6 +211,15 @@ await SendPayloadToClientAsync( break; case SessionMessage.StateSettingAllowed m: _stateSettingIsEnabled = m.Allowed; + break; + case SessionMessage.MatchComm m when _isReady && _wantsComms: + _messageBuilder.Clear(); + _messageBuilder.Finish(MatchComm.Pack(_messageBuilder, m.matchComm).Value); + + await SendPayloadToClientAsync( + TypedPayload.FromFlatBufferBuilder(DataType.MatchComms, _messageBuilder) + ); + break; case SessionMessage.StopMatch when _isReady && _closeAfterMatch: Console.WriteLine("Core got stop match message from server."); diff --git a/RLBotCS/Server/FlatbuffersMessage/SendMatchComm.cs b/RLBotCS/Server/FlatbuffersMessage/SendMatchComm.cs new file mode 100644 index 0000000..02842aa --- /dev/null +++ b/RLBotCS/Server/FlatbuffersMessage/SendMatchComm.cs @@ -0,0 +1,25 @@ +using rlbot.flat; + +namespace RLBotCS.Server.FlatbuffersMessage; + +internal record SendMatchComm(int ClientId, MatchCommT MatchComm) : IServerMessage +{ + public ServerAction Execute(ServerContext context) + { + var message = new SessionMessage.MatchComm(MatchComm); + + foreach (var session in context.Sessions) + { + var id = session.Key; + var writer = session.Value; + + // Don't send the message back to the client that sent it. + if (id != ClientId) + { + writer.writer.TryWrite(message); + } + } + + return ServerAction.Continue; + } +}