Beispiel #1
0
		/// <summary>
		/// 房间列表
		/// </summary>
		/// <returns></returns>
		public static List<RoomInfo> getRoomInfos(bool Lock=true,bool Start=true){
			List<RoomInfo> rooms=new List<RoomInfo>();
			GameRoom[] grooms;
			//MutexRooms.WaitOne();
			grooms = new GameRoom[m_rooms.Count];
			m_rooms.Values.CopyTo(grooms, 0);
			//MutexRooms.ReleaseMutex();
			foreach(GameRoom room in grooms){
				if(room!=null&&room.Game!=null){
					if(!Start){
						//仅等待的房间
						if(room.Game.State !=GameState.Lobby){
							continue;
						}
					}
					if(!Lock){
						if(room.Game.Config.HasPassword()){
							continue;
						}
					}
					RoomInfo info=room.Game.GetRoomInfo();
					if(info!=null){
						rooms.Add(info);
					}
				}
			}
			return rooms;
		}
Beispiel #2
0
 private static GameRoom CreateRoom(GameConfig config)
 {
     GameRoom room = new GameRoom(config);
     m_rooms.Add(config.Name, room);
     Logger.WriteLine("Game++");
     return room;
 }
Beispiel #3
0
		public static void OnRoomClose(GameRoom room){
            using (PacketWriter writer=new PacketWriter(2)){
				writer.Write((byte)RoomMessage.RoomClose);
				writer.WriteUnicode(room.Config.Name, 20);
				Send(writer.Content);
			}
		}
Beispiel #4
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");
		}
Beispiel #5
0
		public void SetRoom(GameRoom room)
		{
			if (m_room == null)
			{
				m_room = room;
				Game = m_room.Game;
			}
		}
Beispiel #6
0
 public void JoinGame(GameRoom room)
 {
     if (m_room == null)
     {
         m_room = room;
         Game = m_room.Game;
     }
 }
Beispiel #7
0
		public static void OnPlayerLeave(GameSession player, GameRoom room){
            if (player.Name == null || player.Name.StartsWith("[AI]")) return;
            using (PacketWriter writer=new PacketWriter(2)){
				writer.Write((byte)RoomMessage.PlayerLeave);
				writer.WriteUnicode(player.Name, 20);
				writer.WriteUnicode(room.Config.Name, 20);
				Send(writer.Content);
			}
		}
Beispiel #8
0
		public static void OnPlayerEnter(GameSession player, GameRoom room){
            if (player.Name==null||player.Name.StartsWith("[AI]")) return;
			using(PacketWriter writer=new PacketWriter(2)){
				writer.Write((byte)RoomMessage.PlayerEnter);
				writer.WriteUnicode(player.Name, 20);
				writer.WriteUnicode(room.Config.Name, 20);
				Send(writer.Content);
			}
			string tip = Messages.RandomMessage();
			if(!string.IsNullOrEmpty(tip))
				player.ServerMessage(Messages.RandomMessage());
		}
Beispiel #9
0
		public static void OnRoomCreate(GameRoom room){
			if(room==null||room.Config==null){
				return;
			}
			using(PacketWriter writer=new PacketWriter(2)){
				writer.Write((byte)RoomMessage.RoomCreate);
				writer.WriteUnicode(room.Name, 20);
				writer.WriteUnicode(room.Config.Name, 20);
				writer.WriteUnicode(room.Config.BanList);
				Send(writer.Content);
			}
		}
 public static void Remove(GameRoom room)
 {
     if (room == null)
     {
         return;
     }
     lock (Games)
     {
         if (room.Name != null)
         {
             Games.Remove(room.Name);
             ServerApi.OnRoomClose(room);
         }
     }
 }
        public GameTimer(GameRoom room)
        {
            this.room           = room;
            SideTimer           = new MyTimer(30 * 1000, SIDE_TIMEOUT);
            SideTimer.AutoReset = true;
            SideTimer.Elapsed  += new ElapsedEventHandler(SideTimer_Elapsed);

            StartingTimer           = new MyTimer(10 * 1000, STARTING_TIMEOUT);
            StartingTimer.AutoReset = true;
            StartingTimer.Elapsed  += new ElapsedEventHandler(StartingTimer_Elapsed);

            HandTimer           = new MyTimer(15 * 1000, Hand_TIMEOUT);
            HandTimer.AutoReset = true;
            HandTimer.Elapsed  += new ElapsedEventHandler(HandTimer_Elapsed);
        }
Beispiel #12
0
		public GameTimer(GameRoom room)
		{
			this.room = room;
			SideTimer = new MyTimer(30 * 1000, SIDE_TIMEOUT);
			SideTimer.AutoReset = true;
			SideTimer.Elapsed += new ElapsedEventHandler(SideTimer_Elapsed);
			
			StartingTimer = new MyTimer(10 * 1000, STARTING_TIMEOUT);
			StartingTimer.AutoReset = true;
			StartingTimer.Elapsed+=new ElapsedEventHandler(StartingTimer_Elapsed);
			
			HandTimer = new MyTimer(15 * 1000, Hand_TIMEOUT);
			HandTimer.AutoReset = true;
			HandTimer.Elapsed +=new ElapsedEventHandler(HandTimer_Elapsed);
		}
Beispiel #13
0
        private void onJoinMyCardStyleGame(GameClientPacket packet)
        {
            if (string.IsNullOrEmpty(Name) || Type != (int)PlayerType.Undefined)
            {
                return;
            }
            int version = packet.ReadInt16();

            if (version < Program.Config.ClientVersion)
            {
                LobbyError("Version too low");
                return;
            }
            else if (version > Program.Config.ClientVersion)
            {
                ServerMessage("Warning: client version is higher than servers.");
            }
            packet.ReadInt32();            //gameid
            packet.ReadInt16();

            string joinCommand = packet.ReadUnicode(60);

            GameRoom room = null;

            if (GameManager.GameExists(joinCommand))
            {
                room = GameManager.GetGame(joinCommand);
            }
            else
            {
                room = GameManager.CreateOrGetGame(new MyCardStyleGameConfig(joinCommand));
            }

            if (room == null)
            {
                LobbyError("Server Full");
                return;
            }
            if (!room.IsOpen)
            {
                LobbyError("Game Finished");
                return;
            }

            Game = room.Game;
            Game.AddPlayer(this);
            IsAuthentified = true;
        }
        public static bool CheckRoomPassword(string namepwd)
        {
            string name = Password.OnlyName(namepwd);

            lock (Games)
            {
                if (Games.ContainsKey(name))
                {
                    //This room
                    GameRoom room = Games[name];
                    if (room != null && room.Config != null)
                    {
                        return(namepwd == room.Config.Name);
                    }
                }
            }
            return(true);
        }
Beispiel #15
0
        public static GameRoom SpectateRandomGame()
        {
            List<GameRoom> filteredRooms = new List<GameRoom>();
            GameRoom[] rooms;
            rooms = new GameRoom[m_rooms.Count];
            m_rooms.Values.CopyTo(rooms, 0);

            foreach (GameRoom room in rooms)
            {
                if (room.Game.State != Game.Enums.GameState.Lobby)
                    filteredRooms.Add(room);
            }

            if (filteredRooms.Count == 0)
                return null;

            return filteredRooms[Program.Random.Next(0, filteredRooms.Count)];
        }
Beispiel #16
0
        public static GameRoom GetRandomGame(int filter = -1)
        {
            List<GameRoom> filteredRooms = new List<GameRoom>();
            GameRoom[] rooms;
            rooms = new GameRoom[m_rooms.Count];
            m_rooms.Values.CopyTo(rooms, 0);

            foreach (GameRoom room in rooms)
            {
                if (room.Game.State == Game.Enums.GameState.Lobby
                    && (filter == -1 ? true : room.Game.Config.Rule == filter))
                    filteredRooms.Add(room);
            }

            if (filteredRooms.Count == 0)
                return null;

            return filteredRooms[Program.Random.Next(0, filteredRooms.Count)];
        }
Beispiel #17
0
        private void OnCreateGame(GameClientPacket packet)
        {
            if (string.IsNullOrEmpty(Name) || Type != (int)PlayerType.Undefined)
            {
                return;
            }

            GameRoom room = null;

            room = GameManager.CreateOrGetGame(new GameConfig(packet));

            if (room == null)
            {
                LobbyError("Server Full");
                return;
            }

            Game = room.Game;
            Game.AddPlayer(this);
            IsAuthentified = true;
        }
        /// <summary>
        /// Create a room
        /// </summary>
        /// <param name="server"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public static GameRoom CreateOrGetGame(GameConfig config)
        {
            string roomname = Password.OnlyName(config.Name);

            lock (Games)
            {
                if (Games.ContainsKey(roomname))
                {
                    return(Games[roomname]);
                }
                if (Games.Count >= Program.Config.MaxRoomCount)
                {
                    return(null);
                }
                GameRoom room = new GameRoom(config);
                Logger.Info("create room:" + room.Name);
                Games.Add(roomname, room);
                ServerApi.OnRoomCreate(room);
                return(room);
            }
        }
Beispiel #19
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);
 }
Beispiel #20
0
		public static GameRoom GetRandomGame(int filter = -1)
		{
			List<GameRoom> filteredRooms = new List<GameRoom>();
			GameRoom[] rooms;
			//MutexRooms.WaitOne();
			rooms = new GameRoom[m_rooms.Count];
			m_rooms.Values.CopyTo(rooms, 0);
			//MutexRooms.ReleaseMutex();
			foreach (GameRoom room in rooms)
			{
				//随机房间不能带密码
				if (!room.Game.Config.HasPassword()&& room.Game.State == GameState.Lobby
				    && (filter == -1 ? true : room.Game.Config.Rule == filter)){
					filteredRooms.Add(room);
				}
			}

			if (filteredRooms.Count == 0)
				return null;

			return filteredRooms[Program.Random.Next(0, filteredRooms.Count)];
		}
Beispiel #21
0
        private void OnCreateGame(GameClientPacket packet)
        {
            if (string.IsNullOrEmpty(Name) || Type != (int)PlayerType.Undefined)
            {
                return;
            }
            GameRoom room = null;

            room = GameManager.CreateOrGetGame(new GameConfig(packet));

            if (room == null)
            {
                LobbyError(Messages.MSG_FULL);
                return;
            }

            Game = room.Game;
            Game.AddPlayer(this);
            //IsAuthentified = CheckAuth();
            if (!IsAuthentified)
            {
                LobbyError(Messages.ERR_AUTH_FAIL);
            }
        }
Beispiel #22
0
 private static string GetYrpName(GameRoom room)
 {
     string yrpName = room.StartTime.Value.ToString("yyyyMMdd HHmmss");
     if (room.IsMatch)
         yrpName = yrpName + " " + GetGameTagName(room) + (room.m_duelCount + 1) + ".yrp";
     else
         yrpName = yrpName + " " + GetGameTagName(room) + ".yrp";
     return yrpName;
 }
Beispiel #23
0
        private void OnJoinGame(GameClientPacket packet)
        {
            if (string.IsNullOrEmpty(Name) || Type != (int)PlayerType.Undefined)
            {
                return;
            }
            int version = packet.ReadInt16();

            if (version < Program.Config.ClientVersion)
            {
                LobbyError(Messages.ERR_LOW_VERSION);
                return;
            }
            else if (version > Program.Config.ClientVersion)
            {
                ServerMessage(Messages.MSG_HIGH_VERSION);
            }


            packet.ReadInt32();            //gameid
            packet.ReadInt16();

            string joinCommand = packet.ReadUnicode(60);

            GameRoom room = null;

            //IsAuthentified = CheckAuth();
            if (!IsAuthentified)
            {
                LobbyError(Messages.ERR_AUTH_FAIL);
                return;
            }
            if (!GameManager.CheckPassword(joinCommand))
            {
                LobbyError(Messages.ERR_PASSWORD);
                return;
            }
            if (string.IsNullOrEmpty(joinCommand) || joinCommand.ToLower() == "random")
            {
                room = GameManager.GetRandomGame();
                if (room == null)
                {
                    room = GameManager.CreateOrGetGame(new GameConfig(joinCommand));
                }
            }
            #region old
//			else if (GameManager.GameExists(joinCommand)){
//				room = GameManager.GetGame(joinCommand);
//			}
//			else if (joinCommand.ToLower() == "random")
//			{
//				room = GameManager.GetRandomGame();
//
//				if (room == null)
//				{
//					LobbyError("No Games");
//					return;
//				}
//			}
//			else if (joinCommand.ToLower() == "spectate")
//			{
//				room = GameManager.SpectateRandomGame();
//
//				if (room == null)
//				{
//					LobbyError("No Games");
//					return;
//				}
//			}
//			else if (string.IsNullOrEmpty(joinCommand) || joinCommand.ToLower() == "tcg" || joinCommand.ToLower() == "ocg"
//			         || joinCommand.ToLower() == "ocg/tcg" || joinCommand.ToLower() == "tcg/ocg")
//			{
//				int filter = string.IsNullOrEmpty(joinCommand) ? -1 :
//					(joinCommand.ToLower() == "ocg/tcg" || joinCommand.ToLower() == "tcg/ocg") ? 2 :
//					joinCommand.ToLower() == "tcg" ? 1 : 0;
//
//				room = GameManager.GetRandomGame(filter);
//				if (room == null) //if no games just make a new one!!
//					room = GameManager.CreateOrGetGame(new GameConfig(joinCommand));
//			}
            #endregion
            else
            {
                room = GameManager.CreateOrGetGame(new GameConfig(joinCommand));
                //Logger.WriteLine("join "+room.Game.Config.Name);
            }

            if (room == null)
            {
                LobbyError(Messages.MSG_FULL);
                return;
            }
            if (!room.IsOpen)
            {
                LobbyError(Messages.MSG_GAMEOVER);
                return;
            }
            if (room.Game != null && room.Game.Config != null)
            {
                //TODO: tips
                if (room.Game.Config.NoCheckDeck)
                {
                    ServerMessage(Messages.MSG_NOCHECKDECK);
                }

                if (room.Game.Config.NoShuffleDeck)
                {
                    ServerMessage(Messages.MSG_NOSHUFFLEDECK);
                }

                if (room.Game.Config.EnablePriority)
                {
                    ServerMessage(Messages.MSG_ENABLE_PROIORITY);
                }
            }
            Game = room.Game;
            Game.AddPlayer(this);
        }
Beispiel #24
0
		public static List<string> SendMessage(GameServerPacket msg, string name=null){
			List<string> playernames=new List<string>();
			GameRoom[] rooms;
			//MutexRooms.WaitOne();
			rooms = new GameRoom[m_rooms.Count];
			m_rooms.Values.CopyTo(rooms, 0);
			//MutexRooms.ReleaseMutex();
			foreach (GameRoom room in rooms)
			{
				if (room.IsOpen){
					try{
						if(string.IsNullOrEmpty(name)){
							playernames.AddRange(room.Game.SendToAll(msg).ToArray());
						}else{
							foreach(Player pl in room.Game.Players){
								if(pl!=null && pl.Name==name){
									playernames.Add(pl.Name);
									pl.Send(msg);
								}
							}
							foreach(Player pl in room.Game.Observers){
								if(pl!=null && pl.Name==name){
									playernames.Add(pl.Name);
									pl.Send(msg);
								}
							}
						}
					}catch(Exception){
						
					}
				}
			}
			return playernames;
		}
Beispiel #25
0
//
//		public static GameRoom SpectateRandomGame()
//		{
//			List<GameRoom> filteredRooms = new List<GameRoom>();
//			GameRoom[] rooms;
//			//MutexRooms.WaitOne();
//			rooms = new GameRoom[m_rooms.Count];
//			m_rooms.Values.CopyTo(rooms, 0);
//			//MutexRooms.ReleaseMutex();
//
//			foreach (GameRoom room in rooms)
//			{
//				if (room.Game.State != GameState.Lobby)
//					filteredRooms.Add(room);
//			}
//
//			if (filteredRooms.Count == 0)
//				return null;
//
//			return filteredRooms[Program.Random.Next(0, filteredRooms.Count)];
//		}

		private static GameRoom CreateRoom(GameConfig config)
		{
			GameRoom room = new GameRoom(config);
			//MutexRooms.WaitOne();
			m_rooms.Add(config.Name, room);
			//Logger.WriteLine(String.Format("++Game:{0}",config.Name));
			//MutexRooms.ReleaseMutex();
			return room;
		}
Beispiel #26
0
		public GameAnalyser(GameRoom game)
		{
			Game = game;
		}
Beispiel #27
0
 public static void Remove(GameRoom room)
 {
     if (room == null) return;
     lock (Games)
     {
         if (room.Name != null)
         {
             Games.Remove(room.Name);
             ServerApi.OnRoomClose(room);
         }
     }
 }
Beispiel #28
0
 /// <summary>
 /// 创建房间
 /// </summary>
 /// <param name="server"></param>
 /// <param name="config"></param>
 /// <returns></returns>
 public static GameRoom CreateOrGetGame(GameConfig config)
 {
     string roomname = Password.OnlyName(config.Name);
     lock (Games)
     {
         if (Games.ContainsKey(roomname))
         {
             return Games[roomname];
         }
         if (Games.Count >= Program.Config.MaxRoomCount)
         {
             return null;
         }
         GameRoom room = new GameRoom(config);
         Logger.Info("create room:" + room.Name);
         Games.Add(roomname, room);
         ServerApi.OnRoomCreate(room);
         return room;
     }
 }
Beispiel #29
0
 private static string GetYrpFileName(GameRoom room)
 {
     return Tool.Combine(Program.Config.Path, "replay/" + GetYrpName(room));
 }
Beispiel #30
0
        private static string GetGameTagName(GameRoom room)
        {
            string filename = "";
            try
            {
                filename = " {" + Tool.RemoveInvalid(room.Config.Name) + "} ";
                if (room.IsTag)
                {
                    filename += Tool.RemoveInvalid(room.Players[0].Name) + "+"
                        + Tool.RemoveInvalid(room.Players[1].Name) + " vs "
                        + Tool.RemoveInvalid(room.Players[2].Name) + "+"
                        + Tool.RemoveInvalid(room.Players[3].Name);
                }
                else {
                    filename += Tool.RemoveInvalid(room.Players[0].Name) + " vs " + Tool.RemoveInvalid(room.Players[1].Name);
                }
            }
            catch (Exception)
            {

            }
            return filename;
        }
Beispiel #31
0
        private void OnJoinGame(GameClientPacket packet)
        {
            if (string.IsNullOrEmpty(Name) || Type != (int)PlayerType.Undefined)
            {
                return;
            }

            int version = packet.ReadInt16();

            if (version < Program.Config.ClientVersion)
            {
                LobbyError("Version too low");
                return;
            }
            else if (version > Program.Config.ClientVersion)
            {
                ServerMessage("Warning: client version is higher than servers.");
            }


            packet.ReadInt32();//gameid
            packet.ReadInt16();

            string joinCommand = packet.ReadUnicode(60);

            GameRoom room = null;

            if (GameManager.GameExists(joinCommand))
            {
                room = GameManager.GetGame(joinCommand);
            }
            else if (joinCommand.ToLower() == "random")
            {
                room = GameManager.GetRandomGame();

                if (room == null)
                {
                    LobbyError("No Games");
                    return;
                }
            }
            else if (joinCommand.ToLower() == "spectate")
            {
                room = GameManager.SpectateRandomGame();

                if (room == null)
                {
                    LobbyError("No Games");
                    return;
                }
            }
            else if (string.IsNullOrEmpty(joinCommand) || joinCommand.ToLower() == "tcg" || joinCommand.ToLower() == "ocg" ||
                     joinCommand.ToLower() == "ocg/tcg" || joinCommand.ToLower() == "tcg/ocg")
            {
                int filter = string.IsNullOrEmpty(joinCommand) ? -1 :
                             (joinCommand.ToLower() == "ocg/tcg" || joinCommand.ToLower() == "tcg/ocg") ? 2 :
                             joinCommand.ToLower() == "tcg" ? 1 : 0;

                room = GameManager.GetRandomGame(filter);
                if (room == null) //if no games just make a new one!!
                {
                    room = GameManager.CreateOrGetGame(new GameConfig(joinCommand));
                }
            }
            else
            {
                room = GameManager.CreateOrGetGame(new GameConfig(joinCommand));
            }

            if (room == null)
            {
                LobbyError("Server Full");
                return;
            }
            if (!room.IsOpen)
            {
                LobbyError("Game Finished");
                return;
            }

            Game = room.Game;
            Game.AddPlayer(this);
            IsAuthentified = true;
        }
 public GameAnalyser(GameRoom game)
 {
     Game = game;
 }