コード例 #1
0
        public void ReceivePacket(tcp.TCPToolkit.Packet packet)
        {
            ServerStartsMessage ready = common.serialization.IConvertible.CreateFromBytes <ServerStartsMessage>(packet.Data.ArraySegment());

            if (ready != null)
            {
#if DEBUG_LOG
                Debug.Log("Received server start message.");
#endif // DEBUG_LOG
                m_serverSentSignal = true;
                return;
            }

            // loads other players in lobby, receives message from server indicating a new player joined
            CharacterListMessage clientList = common.serialization.IConvertible.CreateFromBytes <CharacterListMessage>(packet.Data.ArraySegment());
            if (clientList != null)
            {
                Debug.Log("Received " + clientList.PlayerCharacters.Value.Count + " characters from server");
                foreach (common.serialization.types.String id in clientList.PlayerCharacters.Value.Values)
                {
                    Debug.Log("Fetching character " + id.Value + " from microservice");
                    // fetch character data from microservice
                    string strID = id.Value;
                    m_characterService.GetCharacter(strID, (CharacterData character) =>
                    {
                        Debug.Log("Got character from " + character.PlayerID + " : " + character.Name);
                        m_clientCharacters[character.PlayerID.GetHashCode()] = character;
                        ClientListUpdate.Invoke(new List <CharacterData>(m_clientCharacters.Values));
                    });
                }
                return;
            }

            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();
            ServerInitMessage start = common.serialization.IConvertible.CreateFromBytes <ServerInitMessage>(packet.Data.ArraySegment());
            watch.Stop();
            if (start != null)
            {
#if DEBUG_LOG
                Debug.Log("Time elapsed for world deserialization : " + watch.ElapsedMilliseconds + " ms");
#endif // DEBUG_LOG
                m_awaitedInitMessage = start;
                return;
            }
        }
コード例 #2
0
        protected override void StateUpdate()
        {
            lock (m_lock)
            {
                if (EveryoneIsReady() && !m_awaitingClientLoadWorld)
                {
                    m_UDPServer.Unsubscribe(this);

                    common.world.cellType.CellInfo[,] cellInfoArray = m_worldGenerator.GetCellInfoArray();

                    ServerInitMessage message = new ServerInitMessage(m_simulationBuffer, m_clientCharacters, cellInfoArray);

                    foreach (int id in m_clientCharacters.Keys)
                    {
                        m_TCPServer.Send(message.GetBytes(), id);
                    }

                    Debug.Log("Waiting for clients to load their worlds");
                    m_awaitingClientLoadWorld = true;
                }

                if (EveryoneIsWorldLoaded())
                {
                    m_readyToStartGame        = true;
                    m_awaitingClientLoadWorld = false;
                }
            }

            if (m_readyToStartGame)
            {
                m_readyToStartGame = false;
                ServerStartsMessage message = new ServerStartsMessage();

                Debug.Log("Starting game.");
                foreach (int id in m_clientCharacters.Keys)
                {
                    m_TCPServer.Send(message.GetBytes(), id);
                }

                m_TCPServer.Unsubscribe(this);

                m_gameplayState.Init(m_clientCharacters, m_simulationBuffer);
                m_currentState = m_gameplayState;
            }
        }