Пример #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);
        }
        public static void HandleJoinBattle(object packet, Session session)
        {
            // TODO : handle better AI avatar
            Battle battle;
            if (BattleManager.Battles.TryGetValue(session.Player.Id, out battle) && WorldManager.AddPlayerSession(session))
            {
                BattleSide playerSide = battle.FindSideByUsername(session.Player.Username);
                Avatar playerAvatar = session.Player.Avatar;
                Avatar opponentAvatar;
                Session opponentSession = WorldManager.GetPlayerSession(playerSide.OpponentSide.PlayerName);
                if (opponentSession == null)
                    opponentAvatar = new Avatar();
                else
                    opponentAvatar = opponentSession.Player.Avatar;

                Avatar whiteAvatar;
                Avatar blackAvatar;
                if (playerSide.Color == PlayerColor.white)
                {
                    whiteAvatar = playerAvatar;
                    blackAvatar = opponentAvatar;
                }
                else
                {
                    whiteAvatar = opponentAvatar;
                    blackAvatar = playerAvatar;
                }

                var gameInfo = new PacketGameInfo()
                {
                    White = battle.WhiteSide.PlayerName,
                    Black = battle.BlackSide.PlayerName,
                    GameType = battle.Type,
                    GameId = 1,
                    Color = playerSide.Color,
                    roundTimerSeconds = battle.RoundTimeSeconds,
                    Phase = battle.Phase,
                    WhiteAvatar = whiteAvatar,
                    BlackAvatar = blackAvatar,
                    WhiteIdolTypes = new PacketIdolTypes()
                    {
                        ProfileId = battle.WhiteSide.PlayerId,
                        Type = "DEFAULT",
                        Idol1 = 0,
                        Idol2 = 0,
                        Idol3 = 0,
                        Idol4 = 0,
                        Idol5 = 0,
                    },
                    BlackIdolTypes = new PacketIdolTypes()
                    {
                        ProfileId = battle.BlackSide.PlayerId,
                        Type = "DEFAULT",
                        Idol1 = 0,
                        Idol2 = 0,
                        Idol3 = 0,
                        Idol4 = 0,
                        Idol5 = 0,
                    },
                    CustomSettings = new List<string>(),
                    RewardForIdolKill = battle.RewardForIdolKill,
                    NodeId = ((IPEndPoint)session.Socket.LocalEndPoint).Address.ToString(),
                    Port = (uint)ConfigManager.Config.Network.BattlePort,
                    MaxTierRewardMultiplier = 0.5f,
                    TierRewardMultiplierDelta = new List<float>(),
                    WhiteIdols = battle.WhiteSide.Idols,
                    BlackIdols = battle.BlackSide.Idols
                };

                session.Send(gameInfo);
            }
            else
            {
                LogManager.Write("JoinBattle", "player {0} failed to join battle ! Either battle not created or could not add player session!", session.Player.Username);
            }
        }