Ejemplo n.º 1
0
    void DarkRift_MessageReceived(object sender, MessageReceivedEventArgs e)
    {
        using (Message message = e.GetMessage())
            using (DarkRiftReader reader = message.GetReader()) {
                if (OverallStateManager.Instance.OverallState == OverallState.IN_GAME)
                {
                    if (ClientGameManager.Instance.ClientState == GameStates.GAME_IN_PROGRESS ||
                        ClientGameManager.Instance.ClientState == GameStates.WAITING_FOR_PLAYERS)
                    {
                        //game is in progress
                        if (message.Tag == NetworkTags.EntityUpdate)
                        {
                            ClientEntityManager entityManager = ClientEntityManager.Instance;
                            while (reader.Position < reader.Length)
                            {
                                string entityId     = reader.ReadString();
                                string entityTypeId = reader.ReadString();
                                Entity entity       = entityManager.GetEntity(entityId);
                                if (entity == null)
                                {
                                    entity = entityManager.CreateEntity(entityTypeId);
                                    reader.ReadSerializableInto(ref entity);                             //must populate entity before registering since registration depends on entity data
                                    entityManager.RegisterEntity(entity);
                                }
                                else
                                {
                                    reader.ReadSerializableInto(ref entity);
                                }
                            }
                        }
                        else if (message.Tag == NetworkTags.PlayerEventUpdate)
                        {
                            ClientEntityManager entityManager = ClientEntityManager.Instance;
                            while (reader.Position < reader.Length)
                            {
                                string      playerEventId     = reader.ReadString();
                                string      playerEventTypeId = reader.ReadString();
                                PlayerEvent playerEvent       = entityManager.GetPlayerEvent(playerEventId);
                                if (playerEvent == null)
                                {
                                    playerEvent = entityManager.CreatePlayerEvent(playerEventTypeId);
                                    reader.ReadSerializableInto(ref playerEvent);                             //must populate event before registering since registration depends on event data
                                    entityManager.RegisterPlayerEvent(playerEvent);
                                }
                                else
                                {
                                    reader.ReadSerializableInto(ref playerEvent);
                                }
                            }
                        }
                        else if (message.Tag == NetworkTags.EntityDeath)
                        {
                            ClientEntityManager.Instance.HandleEntityDeath(reader.ReadString());
                        }
                        else if (message.Tag == NetworkTags.EntityDespawn)
                        {
                            ClientEntityManager.Instance.HandleEntityDespawn(reader.ReadString());
                        }
                        else if (message.Tag == NetworkTags.PlayerEventEnd)
                        {
                            ClientEntityManager.Instance.HandlePlayerEventEnd(reader.ReadString());
                        }
                        else if (message.Tag == NetworkTags.CapturePoint)
                        {
                            while (reader.Position < reader.Length)
                            {
                                CapturePoint capturePoint = ClientGameManager.Instance.CapturePoints[reader.ReadUInt16()];
                                reader.ReadSerializableInto(ref capturePoint);
                            }
                        }
                        else if (message.Tag == NetworkTags.GameState)
                        {
                            ClientGameManager.Instance.UpdateGameState(reader.ReadSerializable <GameState>(), reader.ReadBoolean());
                        }
                    }

                    if (message.Tag == NetworkTags.ChatMessage)
                    {
                        UIManager.Instance.AddChatMessage(ClientGameManager.Instance.GameState.GetPlayer(reader.ReadString()), reader.ReadString());
                    }
                    else if (message.Tag == NetworkTags.UnitList)
                    {
                        List <PlayerUnit> playerUnits = new List <PlayerUnit>();
                        while (reader.Position < reader.Length)
                        {
                            playerUnits.Add(reader.ReadSerializable <PlayerUnit>());
                        }
                        UIManager.Instance.OnUnitListReceived(playerUnits);
                    }
                    else if (message.Tag == NetworkTags.PlayerJoined)
                    {
                        ClientGameManager.Instance.OnPlayerJoined(reader.ReadSerializable <Player>());
                    }
                    else if (message.Tag == NetworkTags.PlayerLeft)
                    {
                        ClientGameManager.Instance.OnPlayerLeft(reader.ReadSerializable <Player>());
                    }
                }
                else if (OverallStateManager.Instance.OverallState != OverallState.IN_GAME)
                {
                    if (waitingToJoin)
                    {
                        if (message.Tag == NetworkTags.GameState)
                        {
                            waitingToJoin = false;
                            OverallStateManager.Instance.LoadGame(reader.ReadSerializable <GameState>(), reader.ReadBoolean());
                        }
                    }
                    else
                    {
                        if (message.Tag == NetworkTags.FullUnitList)
                        {
                            List <PlayerUnit> playerUnits = new List <PlayerUnit>();
                            while (reader.Position < reader.Length)
                            {
                                playerUnits.Add(reader.ReadSerializable <PlayerUnit>());
                            }
                            MainMenuManager.Instance.OnUnitListReceived(playerUnits);
                        }
                    }
                }

                if (message.Tag == NetworkTags.Connection)
                {
                    //Response to join request indicating if our matchmaking ticket was valid
                    if (reader.ReadBoolean())
                    {
                        IsConnectedToServer = true;
                        //Notify anyone interested that we've completed joining a server
                        Debug.Log("PlayFab: Ticket accepted by server, join completed.");
                        ServerJoinSuccess?.Invoke("PlayFab: Ticket accepted by server, join completed.");
                    }
                    else
                    {
                        Debug.LogError("PlayFab: Ticket rejected by server, disconnecting.");
                        ServerJoinFailure?.Invoke("PlayFab: Ticket rejected by server, disconnecting.");
                        client.Disconnect();
                    }
                }
            }
    }