示例#1
0
        /// <summary>
        /// Handler for authentication requests
        /// </summary>
        /// <param name="client">client the requested to be authenticated</param>
        /// <param name="args">arguments containing raw data</param>
        private void OnClientAuthRequest(NetPeer client, NetEventArgs args)
        {
            PacketAuth packet = PacketBase.Read <PacketAuth>((NetDataReader)(args.Data));

            //refuse if ID not 0
            if (packet.Sender != 0)
            {
                UnityEngine.Debug.Log("[SERVER] Client already has an ID");
                server.Disconnect(client, "Authentication failed.");
                return;
            }
            PlayerBuffer players = server.Players;

            //refuse if there's no available slot
            if (players.IsFull())
            {
                UnityEngine.Debug.Log("[SERVER] players list full");
                server.Disconnect(client, "Server full.");
                return;
            }

            //refuse if the ID getter fails (it shouldn't though)
            int id;

            if (!players.FirstEmptyID(out id))
            {
                UnityEngine.Debug.Log("[SERVER] no available id");
                server.Disconnect(client, "Authentication failed.");
                return;
            }
            //otherwise welcome new player
            Player     newPlayer = new Player(id, packet.Message);
            PacketAuth response  = new PacketAuth(server.ID, "WLCM", newPlayer.ID);

            GameServer.Send(response, client);

            //send already connected players to the new client
            foreach (Player p in players)
            {
                PacketBase playerPacket = new PacketPlayerEnter(server.ID, p);
                GameServer.Send(playerPacket, client);
            }

            //send game info
            PacketGameInfo gameInfo = new PacketGameInfo(server.ID, 0);

            GameServer.Send(gameInfo, client);

            //send new player to everybody, add him to the buffer
            GameServer.Send(new PacketPlayerEnter(server.ID, newPlayer), null);
            server.AddPlayer(client, newPlayer);
        }
        /// <summary>
        /// Handler for "player enter" packets.
        /// </summary>
        /// <param name="peer">server</param>
        /// <param name="args">raw data containing the player packet</param>
        private void OnPlayerEnter(NetPeer peer, NetEventArgs args)
        {
            if (GameClient.CurrentState == ClientState.Game)
            {
                return;
            }

            PacketPlayerEnter message = PacketBase.Read <PacketPlayerEnter>((NetDataReader)(args.Data));

            if (message.Player.ID == identity.ID)
            {
                SetIdentity(message.Player);
            }
            Add(message.Player);
            NetEventManager.Trigger(NetEventType.PlayerEnter, args);
        }
        /// <summary>
        /// Handler for 'player leaving' packets.
        /// Removes the player if the game is not started yet, otherwise mark him as 'disconnected'.
        /// </summary>
        /// <param name="peer">server</param>
        /// <param name="args">wrapper around raw data containing the packet</param>
        private void OnPlayerLeave(NetPeer peer, NetEventArgs args)
        {
            PacketPlayerEnter message = PacketBase.Read <PacketPlayerEnter>((NetDataReader)(args.Data));

            if (message.Player.ID == Identity.ID)
            {
                UnityEngine.Debug.LogWarning("Dafuq, I can't leave myself!");
                return;
            }
            if (GameClient.CurrentState != ClientState.Game)
            {
                Remove(message.Player);
            }
            else
            {
                SetDisconnected(message.Player.ID);
                LockstepLogic.Instance.UpdatePlayersCount(activePlayers, message.Player.ID);
            }
            NetEventManager.Trigger(NetEventType.PlayerLeave, args);
        }