Example #1
0
        public void AddPlayer(GameSession player)
        {
            //			if(IsJoin(player)){
            ////				/玩家已经在游戏
            //				player.LobbyError(Messages.MSG_PLAYER_INGAME);
            //				return;
            //			}
            Logger.Debug("add " + player.Name + " to " + Name);
            if (State != GameState.Lobby)
            {
                if (State == GameState.End)
                    return;
                player.Type = (int)PlayerType.Observer;
                SendJoinGame(player);
                player.SendTypeChange();
                player.Send(GameServerPacket.EmtryMessage(StocMessage.DuelStart));
                lock (Observers)
                    Observers.Add(player);
                if (State == GameState.Duel)
                {
                    //中途观战
                    InitNewSpectator(player);
                }
                else if (State == GameState.Side)
                {
                    player.ServerMessage(Messages.MSG_WATCH_SIDE);
                }
                ServerApi.OnPlayerEnter(player, this);
                return;
            }
            if (HostPlayer == null)
                HostPlayer = player;
            int pos = GetAvailablePlayerPos();
            if (pos != -1)
            {
                Players[pos] = player;
                IsReady[pos] = false;

                player.Type = pos;
                using (GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter))
                {
                    enter.WriteUnicode(player.Name, 20);
                    enter.Write((byte)pos);
                    SendToAll(enter);
                }
                //	Server.OnPlayEvent(PlayerStatu.PlayerReady, player);
            }
            else
            {
                using (GameServerPacket watch = new GameServerPacket(StocMessage.HsWatchChange))
                {
                    watch.Write((short)(Observers.Count + 1));
                    SendToAll(watch);
                }
                player.Type = (int)PlayerType.Observer;
                lock (Observers)
                    Observers.Add(player);
                //				if(player.IsAuthentified){
                //					ServerMessage("[Server] "+player.Name+" watch game.", PlayerType.White);
                //				}
            }
            SendJoinGame(player);
            player.SendTypeChange();

            for (int i = 0; i < Players.Length; i++)
            {
                if (Players[i] != null)
                {
                    using (GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter))
                    {
                        enter.WriteUnicode(Players[i].Name, 20);
                        enter.Write((byte)i);
                        player.Send(enter, false);
                    }
                    if (IsReady[i])
                    {
                        using (GameServerPacket change = new GameServerPacket(StocMessage.HsPlayerChange))
                        {
                            change.Write((byte)((i << 4) + (int)PlayerChange.Ready));
                            player.Send(change, false);
                        }
                    }
                }
            }
            bool _watch = false;
            lock (Observers)
            {
                _watch = Observers.Count > 0;
            }
            if (_watch)
            {
                using (GameServerPacket nwatch = new GameServerPacket(StocMessage.HsWatchChange))
                {
                    nwatch.Write((short)Observers.Count);
                    player.Send(nwatch, false);
                }
            }
            player.PeekSend();
            ServerApi.OnPlayerEnter(player, this);
        }
Example #2
0
        public void MoveToObserver(GameSession player)
        {
            if (State != GameState.Lobby)
                return;
            if (player.Type == (int)PlayerType.Observer)
                return;
            if (IsReady[player.Type])
                return;
            Players[player.Type] = null;
            IsReady[player.Type] = false;
            lock (Observers)
                Observers.Add(player);

            using (GameServerPacket change = new GameServerPacket(StocMessage.HsPlayerChange))
            {
                change.Write((byte)((player.Type << 4) + (int)PlayerChange.Observe));
                SendToAll(change);
            }

            player.Type = (int)PlayerType.Observer;
            player.SendTypeChange();
        }
Example #3
0
        public void MoveToDuelist(GameSession player)
        {
            if (State != GameState.Lobby)
                return;
            int pos = GetAvailablePlayerPos();
            if (pos == -1)
                return;
            if (player.Type != (int)PlayerType.Observer)
            {
                if (!IsTag || IsReady[player.Type])
                    return;

                pos = (player.Type + 1) % 4;
                while (Players[pos] != null)
                    pos = (pos + 1) % 4;

                using (GameServerPacket change = new GameServerPacket(StocMessage.HsPlayerChange))
                {
                    change.Write((byte)((player.Type << 4) + pos));
                    SendToAll(change);
                }
                Players[player.Type] = null;
                Players[pos] = player;
                player.Type = pos;
                player.SendTypeChange();
            }
            else
            {
                lock (Observers)
                    Observers.Remove(player);
                Players[pos] = player;
                player.Type = pos;

                using (GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter))
                {
                    enter.WriteUnicode(player.Name, 20);
                    enter.Write((byte)pos);
                    SendToAll(enter);
                }

                using (GameServerPacket nwatch = new GameServerPacket(StocMessage.HsWatchChange))
                {
                    nwatch.Write((short)Observers.Count);
                    SendToAll(nwatch);
                }

                player.SendTypeChange();
            }
        }