コード例 #1
0
    private void Dispatch(Message completeMessage)
    {
        if (completeMessage.type != MessageType.Ping && completeMessage.type != MessageType.Pong)
        {
            Debug.Log($"Recieved message of type {completeMessage.type}");
        }

        Action action = completeMessage.type switch {
            MessageType.Connect when !isHost => () => {
                string hostName = Encoding.UTF8.GetString(completeMessage.data);

                host = new Player(string.IsNullOrEmpty(hostName) ? "Host" : hostName, Team.White, true);
                string localName = PlayerPrefs.GetString("PlayerName", "GUEST");
                player = new Player(localName, Team.Black, false);

                mainThreadActions.Enqueue(() =>
                {
                    LoadLobby(Lobby.Type.Client);
                    lobby?.UpdatePlayerName(host);
                });

                SendMessage(new Message(MessageType.UpdateName, System.Text.Encoding.UTF8.GetBytes(localName)));
            },
            MessageType.Disconnect when isHost => PlayerDisconnected,
            MessageType.Disconnect when !isHost => lobby == null ? (Action)Disconnect : (Action)Shutdown,
            MessageType.Ping => () => SendMessage(new Message(MessageType.Pong)),
            MessageType.Pong => ReceivePong,
            MessageType.ProposeTeamChange => ReceiveTeamChangeProposal,
            MessageType.ApproveTeamChange => () => mainThreadActions.Enqueue(SwapTeams),
            MessageType.Ready => Ready,
            MessageType.Unready => Unready,
            MessageType.HandicapOverlayOn when lobby => () => lobby?.ToggleHandicapOverlay(true),
            MessageType.HandicapOverlayOff when lobby => () => lobby?.ToggleHandicapOverlay(false),
            MessageType.StartMatch when !isHost => () => StartMatch(GameParams.Deserialize(completeMessage.data)),
            MessageType.Surrender when multiplayer => () => multiplayer.Surrender(
                surrenderingTeam: isHost?player.Value.team: host.team,
                timestamp: JsonConvert.DeserializeObject <float>(Encoding.ASCII.GetString(completeMessage.data))
                ),
            MessageType.BoardState when multiplayer => () => multiplayer.ReceiveBoard(BoardState.Deserialize(completeMessage.data)),
            MessageType.Promotion when multiplayer => () => multiplayer.ReceivePromotion(Promotion.Deserialize(completeMessage.data)),
            MessageType.OfferDraw when multiplayer => () => mainThreadActions.Enqueue(() => GameObject.FindObjectOfType <OfferDrawPanel>()?.Open()),
            MessageType.AcceptDraw when multiplayer => () => multiplayer.Draw(JsonConvert.DeserializeObject <float>(Encoding.ASCII.GetString(completeMessage.data))),
            MessageType.UpdateName when isHost => () => UpdateClientName(completeMessage),
            MessageType.UpdateName when !isHost => () => UpdateHostName(completeMessage),
            MessageType.FlagFall when multiplayer => () => multiplayer.ReceiveFlagfall(Flagfall.Deserialize(completeMessage.data)),
            MessageType.Checkmate when multiplayer => () => multiplayer.ReceiveCheckmate(BitConverter.ToSingle(completeMessage.data, 0)),
            MessageType.Stalemate when multiplayer => () => multiplayer.ReceiveStalemate(BitConverter.ToSingle(completeMessage.data, 0)),
            MessageType.OpponentSearching when lobby&& !isHost => lobby.OpponentSearching,
            MessageType.OpponentFound when lobby&& !isHost => () => mainThreadActions.Enqueue(() => lobby.OpponentFound(host)),
            _ => () => Debug.LogWarning($"Ignoring unhandled message {completeMessage.type}"),
        };

        action?.Invoke();
    }