Esempio n. 1
0
		/// <summary>
		/// 是否允许断线重连
		/// </summary>
	//	private bool CanPause = false;
		
		public Game(GameRoom room, GameConfig config)
		{
			Config = config;
			State = GameState.Lobby;
			IsMatch = config.Mode == 1;
			IsTag = config.Mode == 2;
			CurrentPlayer = 0;
			LifePoints = new int[2];
			Players = new Player[IsTag ? 4 : 2];
			PlayerNames = new string[IsTag ? 4 : 2];
			CurPlayers = new Player[2];
			IsReady = new bool[IsTag ? 4 : 2];
			m_handResult = new int[2];
			m_timelimit = new int[2];
			m_bonustime = new int[2];
			m_matchResult = new int[3];
			AutoEndTrun = Program.Config.AutoEndTurn;
			Observers = new List<Player>();
			if (config.LfList >= 0 && config.LfList < BanlistManager.Banlists.Count)
				Banlist = BanlistManager.Banlists[config.LfList];
			else if(BanlistManager.Banlists.Count>0){
				Banlist = BanlistManager.Banlists[0];
			}
			m_room = room;
			m_analyser = new GameAnalyser(this);
			yrpName=DateTime.Now.ToString("yyyyMMddHHmmss");
		}
Esempio n. 2
0
 public GameClient(TcpClient client)
 {
     IsConnected = true;
     Player = new Player(this);
     m_client = client;
     m_reader = new BinaryReader(m_client.GetStream());
     m_recvQueue = new Queue<GameClientPacket>();
     m_sendQueue = new Queue<byte[]>();
     m_receivedLen = -1;
 }
Esempio n. 3
0
        public void ReloadGameConfig(string gameinfo)
        {
            Config.Load(gameinfo);
            IsMatch = Config.Mode == 1;
            IsTag = Config.Mode == 2;
            Players = new Player[IsTag ? 4 : 2];
            IsReady = new bool[IsTag ? 4 : 2];
            if (Config.LfList >= 0 && Config.LfList < BanlistManager.Banlists.Count)
                Banlist = BanlistManager.Banlists[Config.LfList];
            LifePoints[0] = Config.StartLp;
            LifePoints[1] = Config.StartLp;

        }
Esempio n. 4
0
		public static bool onChat(GameConfig config,Player player,string msg)
		{
			if(!string.IsNullOrEmpty(msg)){
				msg = msg.Trim();
				if(msg=="/ai"){
					if(Program.Config.MaxAICount==0){
						player.ServerMessage(Messages.MSG_NO_AI);
					}
					else if(config!=null && AddAI(config.Name)){
						player.ServerMessage(Messages.MSG_ADD_AI);
					}else{
						player.ServerMessage(Messages.MSG_NO_FREE_AI);
					}
					return false;
				}
//				if(msg.StartsWith("@") && !Program.Config.PrivateChat){
//					player.ServerMessage(Messages.MSG_BAN_PCHAT);
//					return false;
//				}
//				if(msg.StartsWith("@server ")){
//					Logger.WriteLineWithColor(player.Name+":"+msg.Replace("@server ",""), ConsoleColor.Yellow);
//					return false;
//				}else if(msg.StartsWith("@system ")){
//					Logger.WriteLineWithColor(player.Name+":"+msg.Replace("@system ",""), ConsoleColor.Yellow);
//					return false;
//				}else if(msg.StartsWith("@@ ")){
//					Logger.WriteLineWithColor(player.Name+":"+msg.Replace("@@ ",""), ConsoleColor.Yellow);
//					return false;
//				}else if(msg.StartsWith("@")){
//					//私聊
//					int i=msg.IndexOf(' ');
//					if(i>0){
//						try{
//							string name=msg.Substring(1, i-1);
//							string cxt=msg.Substring(i+1);
//							if(GameManager.SendErrorMessage(player.Name+": "+cxt, name).Count==0){
//								player.ServerMessage(Messages.MSG_SEND_FAIL);
//							}else{
//								return false;
//							}
//						}catch(Exception){
//
//						}
//					}
//				}
			}
			return true;
		}
Esempio n. 5
0
 public Game(GameRoom room, GameConfig config)
 {
     Config = config;
     State = GameState.Lobby;
     IsMatch = config.Mode == 1;
     IsTag = config.Mode == 2;
     CurrentPlayer = 0;
     LifePoints = new int[2];
     Players = new Player[IsTag ? 4 : 2];
     CurPlayers = new Player[2];
     IsReady = new bool[IsTag ? 4 : 2];
     m_handResult = new int[2];
     m_timelimit = new int[2];
     m_bonustime = new int[2];
     m_matchResult = new int[3];
     Observers = new List<Player>();
     if (config.LfList >= 0 && config.LfList < BanlistManager.Banlists.Count)
         Banlist = BanlistManager.Banlists[config.LfList];
     m_room = room;
     m_analyser = new GameAnalyser(this);
 }
Esempio n. 6
0
 public void Chat(Player player, string msg)
 {
     GameServerPacket packet = new GameServerPacket(StocMessage.Chat);
     packet.Write((short)player.Type);
     packet.Write(msg, msg.Length + 1);
     SendToAllBut(packet, player);
     if (Program.Config.STDOUT == true) 
         Console.WriteLine("::::chat|{0}|{1}", player.Name, msg);
 }
Esempio n. 7
0
        public void MoveToObserver(Player player)
        {
            if (State != GameState.Lobby)
                return;
            if (player.Type == (int)PlayerType.Observer)
                return;
            if (IsReady[player.Type])
                return;
            if (Program.Config.STDOUT == true)
                Console.WriteLine("::::left-slot|{1}|{0}", player.Name, player.Type);
            Players[player.Type] = null;
            IsReady[player.Type] = false;
            Observers.Add(player);

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

            player.Type = (int)PlayerType.Observer;
            player.SendTypeChange();
            if (Program.Config.STDOUT == true)
                Console.WriteLine("::::spectator|{0}", Observers.Count);
        }
Esempio n. 8
0
        public void MoveToDuelist(Player 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;

                GameServerPacket change = new GameServerPacket(StocMessage.HsPlayerChange);
                if (Program.Config.STDOUT == true)
                    Console.WriteLine("::::left-slot|{1}|{0}", player.Name, player.Type);
                change.Write((byte)((player.Type << 4) + pos));
                SendToAll(change);

                Players[player.Type] = null;
                Players[pos] = player;
                player.Type = pos;
                player.SendTypeChange();
            }
            else
            {
                Observers.Remove(player);
                Players[pos] = player;
                player.Type = pos;

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

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

                player.SendTypeChange();
            }
            if (Program.Config.STDOUT == true)
                Console.WriteLine("::::join-slot|{1}|{0}", player.Name, pos);
            if (Program.Config.STDOUT == true)
                Console.WriteLine("::::spectator|{0}", Observers.Count);
        }
Esempio n. 9
0
		public void MoveToObserver(Player 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;
			Observers.Add(player);

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

			player.Type = (int)PlayerType.Observer;
			player.SendTypeChange();
		}
Esempio n. 10
0
        public void Surrender(Player player, int reason, bool force = false)
        {
            if (!force)
                if (State != GameState.Duel)
                    return;
            if (player.Type == (int)PlayerType.Observer)
                return;
            GameServerPacket win = new GameServerPacket(GameMessage.Win);
         //   if (Program.Config.STDOUT == true)
         //       Console.WriteLine("Surrender - {0}", player.Name);  //Need API

            int team = player.Type;
            if (IsTag)
                team = player.Type >= 2 ? 1 : 0;
            win.Write((byte)(1 - team));
            win.Write((byte)reason);
            SendToAll(win);

            MatchSaveResult(1 - team);

            RecordWin(1 - team, reason, force);

            EndDuel(reason == 4);
        }
Esempio n. 11
0
        public void TpResult(Player player, bool result)
        {
            if (State != GameState.Starting)
                return;
            if (player.Type != m_startplayer)
                return;

            m_swapped = false;
            if (result && player.Type == (IsTag ? 2 : 1) || !result && player.Type == 0)
            {
                m_swapped = true;
                if (IsTag)
                {
                    Player temp = Players[0];
                    Players[0] = Players[2];
                    Players[2] = temp;

                    temp = Players[1];
                    Players[1] = Players[3];
                    Players[3] = temp;

                    Players[0].Type = 0;
                    Players[1].Type = 1;
                    Players[2].Type = 2;
                    Players[3].Type = 3;
                }
                else
                {
                    Player temp = Players[0];
                    Players[0] = Players[1];
                    Players[1] = temp;
                    Players[0].Type = 0;
                    Players[1].Type = 1;
                }
            }
            CurPlayers[0] = Players[0];
            CurPlayers[1] = Players[IsTag ? 3 : 1];

            State = GameState.Duel;
            int seed = Environment.TickCount;
            m_duel = Duel.Create((uint)seed);
            Random rand = new Random(seed);

            m_duel.SetAnalyzer(m_analyser.Analyse);
            m_duel.SetErrorHandler(HandleError);

            m_duel.InitPlayers(Config.StartLp, Config.StartHand, Config.DrawCount);

            int opt = 0;
            if (Config.EnablePriority)
                opt += 0x08;
            if (Config.NoShuffleDeck)
                opt += 0x10;
            if (IsTag)
                opt += 0x20;

            Replay = new Replay((uint)seed, IsTag);
            Replay.Writer.WriteUnicode(Players[0].Name, 20);
            Replay.Writer.WriteUnicode(Players[1].Name, 20);
            if (IsTag)
            {
                Replay.Writer.WriteUnicode(Players[2].Name, 20);
                Replay.Writer.WriteUnicode(Players[3].Name, 20);
            }
            Replay.Writer.Write(Config.StartLp);
            Replay.Writer.Write(Config.StartHand);
            Replay.Writer.Write(Config.DrawCount);
            Replay.Writer.Write(opt);

            for (int i = 0; i < Players.Length; i++)
            {
                Player dplayer = Players[i == 2 ? 3 : (i == 3 ? 2 : i)];
                int pid = i;
                if (IsTag)
                    pid = i >= 2 ? 1 : 0;
                if (!Config.NoShuffleDeck)
                {
                    IList<int> cards = ShuffleCards(rand, dplayer.Deck.Main);
                    Replay.Writer.Write(cards.Count);
                    foreach (int id in cards)
                    {
                        if (IsTag && (i == 1 || i == 3))
                            m_duel.AddTagCard(id, pid, CardLocation.Deck);
                        else
                            m_duel.AddCard(id, pid, CardLocation.Deck);
                        Replay.Writer.Write(id);
                    }
                }
                else
                {
                    Replay.Writer.Write(dplayer.Deck.Main.Count);
                    for (int j = dplayer.Deck.Main.Count - 1; j >= 0; j--)
                    {
                        int id = dplayer.Deck.Main[j];
                        if (IsTag && (i == 1 || i == 3))
                            m_duel.AddTagCard(id, pid, CardLocation.Deck);
                        else
                            m_duel.AddCard(id, pid, CardLocation.Deck);
                        Replay.Writer.Write(id);
                    }
                }
                Replay.Writer.Write(dplayer.Deck.Extra.Count);
                foreach (int id in dplayer.Deck.Extra)
                {
                    if (IsTag && (i == 1 || i == 3))
                        m_duel.AddTagCard(id, pid, CardLocation.Extra);
                    else
                        m_duel.AddCard(id, pid, CardLocation.Extra);
                    Replay.Writer.Write(id);
                }
            }

            GameServerPacket packet = new GameServerPacket(GameMessage.Start);
            packet.Write((byte)0);
            packet.Write(Config.StartLp);
            packet.Write(Config.StartLp);
            packet.Write((short)m_duel.QueryFieldCount(0, CardLocation.Deck));
            packet.Write((short)m_duel.QueryFieldCount(0, CardLocation.Extra));
            packet.Write((short)m_duel.QueryFieldCount(1, CardLocation.Deck));
            packet.Write((short)m_duel.QueryFieldCount(1, CardLocation.Extra));
            SendToTeam(packet, 0);

            packet.SetPosition(2);
            packet.Write((byte)1);
            SendToTeam(packet, 1);

            packet.SetPosition(2);
            if (m_swapped)
                packet.Write((byte)0x11);
            else
                packet.Write((byte)0x10);
            SendToObservers(packet);

            RefreshExtra(0);
            RefreshExtra(1);

            m_duel.Start(opt);

            TurnCount = 0;
            LifePoints[0] = Config.StartLp;
            LifePoints[1] = Config.StartLp;
            Process();
        }
Esempio n. 12
0
        public void StartDuel(Player player)
        {
            if (State != GameState.Lobby)
                return;
            if (!player.Equals(HostPlayer))
                return;
            for (int i = 0; i < Players.Length; i++)
            {
                if (!IsReady[i])
                    return;
                if (Players[i] == null)
                    return;
            }

            State = GameState.Hand;
            if (Program.Config.STDOUT == true) 
                Console.WriteLine("::::startduel");

            SendToAll(new GameServerPacket(StocMessage.DuelStart));

            SendHand();
        }
Esempio n. 13
0
 private void SendDuelingPlayers(Player player)
 {
     for (int i = 0; i < Players.Length; i++)
     {
         GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter);
         int id = i;
         if (m_swapped)
         {
             if (IsTag)
             {
                 if (i == 0 || id == 1)
                     id = i + 2;
                 else
                     id = i - 2;
             }
             else
                 id = 1 - i;
         }
         enter.Write(Players[id].Name, 20);
         enter.Write((byte)i);
         player.Send(enter);
     }
 }
Esempio n. 14
0
        private void SendJoinGame(Player player)
        {
            GameServerPacket join = new GameServerPacket(StocMessage.JoinGame);
            join.Write(Banlist == null ? 0U : Banlist.Hash);
            join.Write((byte)Config.Rule);
            join.Write((byte)Config.Mode);
            join.Write(Config.EnablePriority);
            join.Write(Config.NoCheckDeck);
            join.Write(Config.NoShuffleDeck);
            // C++ padding: 5 bytes + 3 bytes = 8 bytes
            for (int i = 0; i < 3; i++)
                join.Write((byte)0);
            join.Write(Config.StartLp);
            join.Write((byte)Config.StartHand);
            join.Write((byte)Config.DrawCount);
            join.Write((short)Config.GameTimer);
            player.Send(join);

            if (State != GameState.Lobby)
                SendDuelingPlayers(player);
        }
Esempio n. 15
0
		public void Surrender(Player player, int reason, bool force = false)
		{
			if(!force)
				if (State != GameState.Duel)
					return;
			if (player.Type == (int)PlayerType.Observer)
				return;
			GameServerPacket win = new GameServerPacket(GameMessage.Win);
			int team = player.Type;
			if (IsTag)
				team = player.Type >= 2 ? 1 : 0;
			win.Write((byte)(1 - team));
			win.Write((byte)reason);
			SendToAll(win);

			MatchSaveResult(1 - team);

			RecordWin(1 - team, reason, force);

			EndDuel(reason == 4);
		}
Esempio n. 16
0
		public void RemovePlayer(Player player)
		{
			if(player==null){
				return;
			}
			if (player.Equals(HostPlayer) && State == GameState.Lobby){
				//Logger.WriteLine("HostPlayer is leave", false);
				m_room.Close(true);
			}
			else if (player.Type == (int)PlayerType.Observer)
			{
				Observers.Remove(player);
				if (State == GameState.Lobby)
				{
					GameServerPacket nwatch = new GameServerPacket(StocMessage.HsWatchChange);
					nwatch.Write((short) Observers.Count);
					SendToAll(nwatch);
				}
				player.Disconnect();
			}
			else if (State == GameState.Lobby)
			{
				Players[player.Type] = null;
				IsReady[player.Type] = false;
				GameServerPacket change = new GameServerPacket(StocMessage.HsPlayerChange);
				change.Write((byte)((player.Type << 4) + (int) PlayerChange.Leave));
				SendToAll(change);
				player.Disconnect();
			}
			else{
//				if(CanPause){
//					if(State == GameState.Duel){
//						//断线重连
//						string name = player.Name;
//						int pos = player.Type;
//						if(pos != (int)PlayerType.Observer){
//							PlayerNames[pos] = name;
//							Players[pos] = null;
//							IsPause = true;
//							AutoEndTrun = false;
//							SendToAll(GameManager.getMessage(string.Format(Messages.MSG_DISCONECT
//							                                               , name, Config.GameTimer),PlayerType.Red));
//							return;
//						}
//					}else if(State == GameState.Side){
//						//断线重连
//						IsPause = true;
//					}
//				}
				if(IsEnd){
					return;
				}
				Surrender(player, 4, true);
			}
		}
Esempio n. 17
0
		public void StartDuel(Player player)
		{
			if (State != GameState.Lobby)
				return;
			if (!player.Equals(HostPlayer))
				return;
			for (int i = 0; i < Players.Length; i++)
			{
				if (!IsReady[i]){
					return;
				}
				if (Players[i] == null){
					return;
				}
			}

			State = GameState.Hand;
			SendToAll(new GameServerPacket(StocMessage.DuelStart));

			SendHand();
		}
Esempio n. 18
0
		public void KickPlayer(Player player, int pos)
		{
			if (State != GameState.Lobby)
				return;
			if (pos >= Players.Length || !player.Equals(HostPlayer) || player.Equals(Players[pos]) || Players[pos] == null)
				return;
			RemovePlayer(Players[pos]);
		}
Esempio n. 19
0
		public void Chat(Player player, string msg)
		{
			GameServerPacket packet = new GameServerPacket(StocMessage.Chat);
			packet.Write((short)player.Type);
			packet.WriteUnicode(msg, msg.Length + 1);
			SendToAllBut(packet, player);
		}
Esempio n. 20
0
        public void SetReady(Player player, bool ready)
        {
            if (State != GameState.Lobby)
                return;
            if (player.Type == (int)PlayerType.Observer)
                return;
            if (IsReady[player.Type] == ready)
                return;

            if (ready)
            {
                bool ocg = Config.Rule == 0 || Config.Rule == 2;
                bool tcg = Config.Rule == 1 || Config.Rule == 2;
                int result = 1;
                if (Config.NoCheckDeck)
                    result = 0;
                else if (player.Deck != null)
                    result = player.Deck.Check(Banlist, ocg, tcg);
                if (result != 0)
                {
                    GameServerPacket rechange = new GameServerPacket(StocMessage.HsPlayerChange);
                    rechange.Write((byte)((player.Type << 4) + (int)(PlayerChange.NotReady)));
                    player.Send(rechange);
                    GameServerPacket error = new GameServerPacket(StocMessage.ErrorMsg);
                    error.Write((byte)2); // ErrorMsg.DeckError
                    // C++ padding: 1 byte + 3 bytes = 4 bytes
                    for (int i = 0; i < 3; i++)
                        error.Write((byte)0);
                    error.Write(result);
                    player.Send(error);
                    return;
                }
            }

            IsReady[player.Type] = ready;

            GameServerPacket change = new GameServerPacket(StocMessage.HsPlayerChange);
            change.Write((byte)((player.Type << 4) + (int)(ready ? PlayerChange.Ready : PlayerChange.NotReady)));
            if (Program.Config.STDOUT == true)
            Console.WriteLine("::::lock-slot|{1}|{0}", ready, player.Type);
            SendToAll(change);
        }
Esempio n. 21
0
 public void KickPlayer(Player player, int pos)
 {
     if (State != GameState.Lobby)
         return;
     if (pos >= Players.Length || !player.Equals(HostPlayer) || player.Equals(Players[pos]) || Players[pos] == null)
         return;
     RemovePlayer(Players[pos]);
     if (Program.Config.STDOUT == true) 
         Console.WriteLine("::::left-slot|{1}|{0}", player.Name, pos);
 }
Esempio n. 22
0
        private void InitNewSpectator(Player player)
        {
            int deck1 = m_duel.QueryFieldCount(0, CardLocation.Deck);
            int deck2 = m_duel.QueryFieldCount(1, CardLocation.Deck);

            int hand1 = m_duel.QueryFieldCount(0, CardLocation.Hand);
            int hand2 = m_duel.QueryFieldCount(1, CardLocation.Hand);

            GameServerPacket packet = new GameServerPacket(GameMessage.Start);
            packet.Write((byte)(m_swapped ? 0x11 : 0x10));
            packet.Write(LifePoints[0]);
            packet.Write(LifePoints[1]);
            packet.Write((short)(deck1 + hand1));
            packet.Write((short)m_duel.QueryFieldCount(0, CardLocation.Extra));
            packet.Write((short)(deck2 + hand2));
            packet.Write((short)m_duel.QueryFieldCount(1, CardLocation.Extra));
            player.Send(packet);

            GameServerPacket draw = new GameServerPacket(GameMessage.Draw);
            draw.Write((byte)0);
            draw.Write((byte)hand1);
            for (int i = 0; i < hand1; i++)
                draw.Write(0);
            player.Send(draw);

            draw = new GameServerPacket(GameMessage.Draw);
            draw.Write((byte)1);
            draw.Write((byte)hand2);
            for (int i = 0; i < hand2; i++)
                draw.Write(0);
            player.Send(draw);

            GameServerPacket turn = new GameServerPacket(GameMessage.NewTurn);
            turn.Write((byte)0);
            player.Send(turn);
            if (CurrentPlayer == 1)
            {
                turn = new GameServerPacket(GameMessage.NewTurn);
                turn.Write((byte)0);
                player.Send(turn);
            }

            InitSpectatorLocation(player, CardLocation.MonsterZone);
            InitSpectatorLocation(player, CardLocation.SpellZone);
            InitSpectatorLocation(player, CardLocation.Grave);
            InitSpectatorLocation(player, CardLocation.Removed);
        }
Esempio n. 23
0
        public void HandResult(Player player, int result)
        {
            if (State != GameState.Hand)
                return;
            if (player.Type == (int)PlayerType.Observer)
                return;
            if (result < 1 || result > 3)
                return;
            if (IsTag && player.Type != 0 && player.Type != 2)
                return;
            int type = player.Type;
            if (IsTag && player.Type == 2)
                type = 1;
            if (m_handResult[type] != 0)
                return;
            m_handResult[type] = result;
            if (m_handResult[0] != 0 && m_handResult[1] != 0)
            {
                GameServerPacket packet = new GameServerPacket(StocMessage.HandResult);
                packet.Write((byte)m_handResult[0]);
                packet.Write((byte)m_handResult[1]);
                SendToTeam(packet, 0);
                SendToObservers(packet);

                packet = new GameServerPacket(StocMessage.HandResult);
                packet.Write((byte)m_handResult[1]);
                packet.Write((byte)m_handResult[0]);
                SendToTeam(packet, 1);

                if (m_handResult[0] == m_handResult[1])
                {
                    m_handResult[0] = 0;
                    m_handResult[1] = 0;
                    SendHand();
                    return;
                }
                if ((m_handResult[0] == 1 && m_handResult[1] == 2) ||
                    (m_handResult[0] == 2 && m_handResult[1] == 3) ||
                    (m_handResult[0] == 3 && m_handResult[1] == 1))
                    m_startplayer = IsTag ? 2 : 1;
                else
                    m_startplayer = 0;
                State = GameState.Starting;
                Players[m_startplayer].Send(new GameServerPacket(StocMessage.SelectTp));
            }
        }
Esempio n. 24
0
 public void SendToAllBut(GameServerPacket packet, Player except)
 {
     foreach (Player player in Players)
         if (player != null && !player.Equals(except))
             player.Send(packet);
     foreach (Player player in Observers)
         if (!player.Equals(except))
             player.Send(packet);
 }
Esempio n. 25
0
		public void AddPlayer(Player player)
		{
			if (State != GameState.Lobby)
			{
				if (State == GameState.End)
					return;
				//断线重连
//				if(CanPause && IsPause){
//					for(int i=0;i<PlayerNames.Length;i++){
//						if(PlayerNames[i] == player.Name){
//							if(Players[i] == null){
//								//重新加入游戏
//								player.Type = i;
//								Players[i] = player;
//								AutoEndTrun = Program.Config.AutoEndTurn;
//								GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter);
//								enter.Write(player.Name, 20);
//								enter.Write((byte)i);
//								SendToAll(enter);
//								SendJoinGame(player);
//								player.SendTypeChange();
//								//player.Send(new GameServerPacket(StocMessage.DuelStart));
//								InitNewSpectator(player, i);
//								IsPause = false;
//								return;
//							}
//							break;
//						}
//					}
//				}
				player.Type = (int)PlayerType.Observer;
				SendJoinGame(player);
				player.SendTypeChange();
				player.Send(new GameServerPacket(StocMessage.DuelStart));
				Observers.Add(player);
				if (State == GameState.Duel){
					//中途观战
					InitNewSpectator(player);
				}else if(State == GameState.Side){
					player.ServerMessage(Messages.MSG_WATCH_SIDE);
				}
				return;
			}

			if (HostPlayer == null)
				HostPlayer = player;

			int pos = GetAvailablePlayerPos();
			if (pos != -1)
			{
				GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter);
				enter.WriteUnicode(player.Name, 20);
				enter.Write((byte)pos);
				SendToAll(enter);
				PlayerNames[pos] = player.Name;
				Players[pos] = player;
				IsReady[pos] = false;
				player.Type = pos;
			}
			else
			{
				GameServerPacket watch = new GameServerPacket(StocMessage.HsWatchChange);
				watch.Write((short)(Observers.Count + 1));
				SendToAll(watch);

				player.Type = (int)PlayerType.Observer;
				Observers.Add(player);
				if(player.IsAuthentified){
					SendToAll(GameManager.getMessage("[Server] "+player.Name+" watch game.", PlayerType.White));
				}
			}

			SendJoinGame(player);
			player.SendTypeChange();

			for (int i = 0; i < Players.Length; i++)
			{
				if (Players[i] != null)
				{
					GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter);
					enter.WriteUnicode(Players[i].Name, 20);
					enter.Write((byte)i);
					player.Send(enter);
					if (IsReady[i])
					{
						GameServerPacket change = new GameServerPacket(StocMessage.HsPlayerChange);
						change.Write((byte)((i << 4) + (int)PlayerChange.Ready));
						player.Send(change);
					}
				}
			}

			if (Observers.Count > 0)
			{
				GameServerPacket nwatch = new GameServerPacket(StocMessage.HsWatchChange);
				nwatch.Write((short)Observers.Count);
				player.Send(nwatch);
			}
		}
Esempio n. 26
0
        public void RemovePlayer(Player player)
        {
            if (player.Equals(HostPlayer) && State == GameState.Lobby)
                m_room.Close();
            else if (player.Type == (int)PlayerType.Observer)
            {
                Observers.Remove(player);
                if (State == GameState.Lobby)
                {
                    GameServerPacket nwatch = new GameServerPacket(StocMessage.HsWatchChange);
                    nwatch.Write((short)Observers.Count);
                    SendToAll(nwatch);
                    if (Program.Config.STDOUT == true)
                    Console.WriteLine("::::spectator|{0}", Observers.Count);
                }
             //   if (Program.Config.STDOUT == true)        
             //       Console.WriteLine("{0} - disconnected", player.Name); //Need API
                player.Disconnect();
            }
            else if (State == GameState.Lobby)
            {
                Players[player.Type] = null;
                IsReady[player.Type] = false;

                GameServerPacket change = new GameServerPacket(StocMessage.HsPlayerChange);
                change.Write((byte)((player.Type << 4) + (int)PlayerChange.Leave));
                if (Program.Config.STDOUT == true)
                    Console.WriteLine("::::left-slot|{1}|{0}", player.Name, player.Type);

                SendToAll(change);
                player.Disconnect();
            }
            else
                Surrender(player, 4, true);
        }
Esempio n. 27
0
        private void InitSpectatorLocation(Player player, CardLocation loc)
        {
            for (int index = 0; index < 2; index++)
            {
                int flag = loc == CardLocation.MonsterZone ? 0x91fff : 0x81fff;
                byte[] result = m_duel.QueryFieldCard(index, loc, flag, false);

                MemoryStream ms = new MemoryStream(result);
                BinaryReader reader = new BinaryReader(ms);
                BinaryWriter writer = new BinaryWriter(ms);
                while (ms.Position < ms.Length)
                {
                    int len = reader.ReadInt32();
                    if (len == 4)
                        continue;
                    long pos = ms.Position;
                    reader.ReadBytes(len - 4);
                    long endPos = ms.Position;

                    ms.Position = pos;
                    ClientCard card = new ClientCard();
                    card.Update(reader);
                    ms.Position = endPos;

                    bool facedown = ((card.Position & (int)CardPosition.FaceDown) != 0);

                    GameServerPacket move = new GameServerPacket(GameMessage.Move);
                    move.Write(facedown ? 0 : card.Code);
                    move.Write(0);
                    move.Write((byte)card.Controler);
                    move.Write((byte)card.Location);
                    move.Write((byte)card.Sequence);
                    move.Write((byte)card.Position);
                    move.Write(0);
                    player.Send(move);

                    foreach (ClientCard material in card.Overlay)
                    {
                        GameServerPacket xyzcreate = new GameServerPacket(GameMessage.Move);
                        xyzcreate.Write(material.Code);
                        xyzcreate.Write(0);
                        xyzcreate.Write((byte)index);
                        xyzcreate.Write((byte)CardLocation.Grave);
                        xyzcreate.Write((byte)0);
                        xyzcreate.Write((byte)0);
                        xyzcreate.Write(0);
                        player.Send(xyzcreate);

                        GameServerPacket xyzmove = new GameServerPacket(GameMessage.Move);
                        xyzmove.Write(material.Code);
                        xyzmove.Write((byte)index);
                        xyzmove.Write((byte)CardLocation.Grave);
                        xyzmove.Write((byte)0);
                        xyzmove.Write((byte)0);
                        xyzmove.Write((byte)material.Controler);
                        xyzmove.Write((byte)material.Location);
                        xyzmove.Write((byte)material.Sequence);
                        xyzmove.Write((byte)material.Position);
                        xyzmove.Write(0);
                        player.Send(xyzmove);
                    }

                    if (facedown)
                    {
                        ms.Position = pos;
                        writer.Write(new byte[len - 4]);
                    }
                }

                if (loc == CardLocation.MonsterZone)
                {
                    result = m_duel.QueryFieldCard(index, loc, 0x81fff, false);
                    ms = new MemoryStream(result);
                    reader = new BinaryReader(ms);
                    writer = new BinaryWriter(ms);
                    while (ms.Position < ms.Length)
                    {
                        int len = reader.ReadInt32();
                        if (len == 4)
                            continue;
                        long pos = ms.Position;
                        byte[] raw = reader.ReadBytes(len - 4);

                        bool facedown = ((raw[11] & (int)CardPosition.FaceDown) != 0);
                        if (facedown)
                        {
                            ms.Position = pos;
                            writer.Write(new byte[len - 4]);
                        }
                    }
                }

                GameServerPacket update = new GameServerPacket(GameMessage.UpdateData);
                update.Write((byte)index);
                update.Write((byte)loc);
                update.Write(result);
                player.Send(update);
            }
        }
Esempio n. 28
0
		public void MoveToDuelist(Player 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;

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

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

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

				player.SendTypeChange();
			}
		}
Esempio n. 29
0
 public bool Equals(Player player)
 {
     return ReferenceEquals(this, player);
 }
Esempio n. 30
0
        public void AddPlayer(Player player)
        {
            int pos = GetAvailablePlayerPos();
            if (Program.Config.STDOUT == true)
                Console.WriteLine("::::join-slot|{1}|{0}", player.Name, pos);


            if (State != GameState.Lobby)
            {
                player.Type = (int)PlayerType.Observer;
                if (State == GameState.End)
                    return;
                SendJoinGame(player);
                player.SendTypeChange();
                player.Send(new GameServerPacket(StocMessage.DuelStart));
                Observers.Add(player);


                if (State == GameState.Duel)
                    InitNewSpectator(player);
                return;
            }

            if (HostPlayer == null)
                HostPlayer = player;

           
            if (pos != -1)
            {
                GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter);
                enter.Write(player.Name, 20);
                enter.Write((byte)pos);
                SendToAll(enter);

                Players[pos] = player;
                IsReady[pos] = false;
                player.Type = pos;

                
            }
            else
            {
                GameServerPacket watch = new GameServerPacket(StocMessage.HsWatchChange);
                watch.Write((short)(Observers.Count + 1));
                if (Program.Config.STDOUT == true)
                Console.WriteLine("::::spectator|{0}", Observers.Count);
                SendToAll(watch);

                player.Type = (int)PlayerType.Observer;
                Observers.Add(player);
               
            }

            SendJoinGame(player);
            player.SendTypeChange();

            for (int i = 0; i < Players.Length; i++)
            {
                if (Players[i] != null)
                {
                    GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter);
                    enter.Write(Players[i].Name, 20);
                    enter.Write((byte)i);
                    player.Send(enter);

                    if (IsReady[i])
                    {
                        GameServerPacket change = new GameServerPacket(StocMessage.HsPlayerChange);
                        change.Write((byte)((i << 4) + (int)PlayerChange.Ready));
                        player.Send(change);
                    }
                }
            }

            if (Observers.Count > 0)
            {
                GameServerPacket nwatch = new GameServerPacket(StocMessage.HsWatchChange);
                nwatch.Write((short)Observers.Count);
                player.Send(nwatch);
                if (Program.Config.STDOUT == true)
                Console.WriteLine("::::spectator|{0}", Observers.Count);
            }
        }