/// <summary> /// 创建房间数据处理 /// </summary> /// <param name="roomSeatSockets">要操作的socket字典</param> /// <param name="roomNum">要操作的房间号</param> /// <param name="socket">对应的客户端socket</param> /// <param name="roomStr">对应的接收到的房间数据</param> /// <returns>成功返回true,否则返回false</returns> public static string RoomDataDeal(GameDataCenter gameDataCenter, Socket socket, string roomStr) { string[] strTemp = DataCtrl.SegmentData(roomStr); switch (strTemp[0]) { //创建房间的信息,房间号++,建立socket的list,加入当前socket,将当前list加入dic,如果对应的房间号的list包含此socket,加入对应的playerdata,返回true,否则,返回false。 case "0": int roomNum = 1; while(gameDataCenter.RoomDataDic.Keys.Contains(roomNum)) { roomNum++; } gameDataCenter.RoomNum= roomNum; List<Socket> l = new List<Socket>(); l.Add(socket); gameDataCenter.RoomSeatSockets.Add(gameDataCenter.RoomNum, l); if (gameDataCenter.RoomSeatSockets[gameDataCenter.RoomNum].Contains(socket)) { GameRoomData gameRoomData = new GameRoomData(); PlayerData playerData = new PlayerData(1, gameDataCenter.MailNickDic[strTemp[1]],strTemp[1],socket); playerData.IsKing = true; gameRoomData.PlayerDataDic.Add(1,playerData); gameDataCenter.RoomDataDic.Add(gameDataCenter.RoomNum, gameRoomData); //更新所有的在大厅中的人的信息,同时更新特定房间的人的信息 RoomDataPushAll(gameDataCenter); RoomDataPush(gameDataCenter, roomNum); return "1|0|1|" + gameDataCenter.RoomNum.ToString() + "|" + gameDataCenter.RoomSeatSockets[gameDataCenter.RoomNum].Count.ToString() + "|"; } else { gameDataCenter.RoomNum--; return "1|0|0|创建失败|"; } //加入房间的信息,先取得房间号i,判断房间号i存在否,存在则将此socket加入list,如果此list含此socket,返回true。 case "1": int i; int.TryParse(strTemp[1], out i); if (!gameDataCenter.RoomSeatSockets.Keys.Contains(i)) { return "1|1|0|加入失败|"; } gameDataCenter.RoomSeatSockets[i].Add(socket); if (gameDataCenter.RoomSeatSockets[i].Contains(socket)) { PlayerData playerData = new PlayerData(gameDataCenter.RoomSeatSockets[i].Count, gameDataCenter.MailNickDic[strTemp[1]], strTemp[1],socket); gameDataCenter.RoomDataDic[i].PlayerDataDic.Add(gameDataCenter.RoomDataDic[i].PlayerDataDic.Count+1, playerData); //更新所有的在大厅中的人的信息,同时更新特定房间的人的信息 RoomDataPushAll(gameDataCenter); RoomDataPush(gameDataCenter, i); return "1|1|1|" + i + "|" + gameDataCenter.RoomSeatSockets[i].Count.ToString() + "|"; } else { return "1|1|0|加入失败|"; } //更新房间信息 case "2": string strRoomDataUpdate = RoomDataUpdate(gameDataCenter, socket, strTemp); return strRoomDataUpdate; default: return "未知错误"; } }
//第几个房间群发 public static void GroupSend(GameDataCenter gameDataCenter, int roomNum, string str) { foreach (var item in gameDataCenter.RoomSeatSockets[roomNum]) { NetCtrl.Send(item, str); } }
public ViewModel() { NetCtrl = new NetCtrl(); MySQLCtrl = new MySQLCtrl(); GameDataCenter = new GameDataCenter(); Ips = new ObservableCollection<ServerIp>(); foreach (var item in NetCtrl.GetLocalIP()) { if (!item.ToString().Contains("%")) { Ips.Add(new ServerIp(item)); } } }
//处理客户端传过来的所有信息 public static void DealData(GameDataCenter gameDataCenter, Socket socket, string str) { string[] strTemp = SegmentData(str); switch (strTemp[0]) { //处理登陆注册信息 case "0": GameUser gameuser = InfoDataCtrl.InfoDataDeal(App.viewModel.MySQLCtrl, socket, str.Substring(2)); if (gameuser.Status == "0|1|1|") { if (gameDataCenter.MailNickDic.Keys.Contains(gameuser.Mail)) { gameuser.Status = "0|1|0|您的账户已经登陆|"; } else { gameDataCenter.MailNickDic.Add(gameuser.Mail, gameuser.NickName); } } NetCtrl.Send(socket, gameuser.Status); break; //处理房间座位信息 case "1": string roomStatus = InfoDataCtrl.RoomDataDeal(gameDataCenter, socket, str.Substring(2)); NetCtrl.Send(socket, roomStatus); int roomToStart = 0; if (roomStatus.Substring(2)[0] == '1') { if (IsStartEnable(gameDataCenter, socket, roomStatus, ref roomToStart)) { NetCtrl.Send(gameDataCenter.RoomSeatSockets[roomToStart][0], "1|2|"); } } break; //处理游戏中的数据 case "2": DealGameData(gameDataCenter, str.Substring(2)); break; default: Console.WriteLine("接收到错误信息"); break; } }
//对开始游戏的处理,房间号|座号|开始游戏 private static void DealStartGame(GameDataCenter gameDataCenter, string[] gameStrs) { int roomNum = Convert.ToInt32(gameStrs[0]); //游戏正式开始 GroupSend(gameDataCenter, roomNum, "1|3|"); foreach (var item in gameDataCenter.RoomDataDic[roomNum].PlayerDataDic) { item.Value.PocketBuildings.Add(gameDataCenter.RoomDataDic[roomNum].BackBuildings[0]); gameDataCenter.RoomDataDic[roomNum].BackBuildings.RemoveAt(0); item.Value.PocketBuildings.Add(gameDataCenter.RoomDataDic[roomNum].BackBuildings[0]); gameDataCenter.RoomDataDic[roomNum].BackBuildings.RemoveAt(0); item.Value.Money = 2; //发给特定的人告诉他的手牌和钱 NetCtrl.Send(item.Value.Socket, "2|0|" + item.Value.PocketBuildings[0].ID + "|" + item.Value.PocketBuildings[1].ID + "|" + "2|"); //群发消息更改特定人的钱 GroupSend(gameDataCenter, roomNum, "2|1|" + item.Value.Seat + "|2|"); } foreach (var item in gameDataCenter.RoomDataDic[roomNum].PlayerDataDic) { if (item.Value.IsKing) { gameDataCenter.RoomDataDic[roomNum].TableHeros = gameDataCenter.RoomDataDic[roomNum].BackHeros; gameDataCenter.RoomDataDic[roomNum].BackHeros.Clear(); gameDataCenter.RoomDataDic[roomNum].BackHeros.Add(gameDataCenter.RoomDataDic[roomNum].TableHeros[0]); gameDataCenter.RoomDataDic[roomNum].TableHeros.RemoveAt(0); string s = "2|4|0|"; foreach (var item1 in gameDataCenter.RoomDataDic[roomNum].TableHeros) { s += (item1.ID + "|"); } NetCtrl.Send(item.Value.Socket, s); } } }
//得到桌子上的角色牌的string private static string GetTableHeroString(GameDataCenter gameDataCenter, int roomNum) { string s = ""; foreach (var item in gameDataCenter.RoomDataDic[roomNum].TableHeros) { s += (item.ID + "|"); } return s; }
//选择某个英雄被偷窃 private static void DealSelectHeroToStole(GameDataCenter gameDataCenter, string[] gameStrs) { int roomNum = Convert.ToInt32(gameStrs[0]); int seatNum = Convert.ToInt32(gameStrs[1]); int ID = Convert.ToInt32(gameStrs[4]); foreach (var item in gameDataCenter.RoomDataDic[roomNum].PlayerDataDic) { foreach (var role in item.Value.Role) { if (role.ID == ID) { item.Value.IsStole = true; } } } }
//对选择玩家的处理 private static void DealSelectPlayer(GameDataCenter gameDataCenter, string[] gameStrs) { int roomNum = Convert.ToInt32(gameStrs[0]); int targetSeatNum = Convert.ToInt32(gameStrs[4]); int mySeatNum = Convert.ToInt32(gameStrs[1]); List<Building> listTemp = new List<Building>(); listTemp.AddRange(gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[targetSeatNum].PocketBuildings); gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[targetSeatNum].PocketBuildings.Clear(); gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[targetSeatNum].PocketBuildings.AddRange(gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[mySeatNum].PocketBuildings); gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[mySeatNum].PocketBuildings.Clear(); gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[mySeatNum].PocketBuildings.AddRange(listTemp); }
//选择英雄到背面 private static void DealSelectHeroToBack(GameDataCenter gameDataCenter, string[] gameStrs) { int roomNum = Convert.ToInt32(gameStrs[0]); int seatNum = Convert.ToInt32(gameStrs[1]); int ID = Convert.ToInt32(gameStrs[4]); foreach (var item in gameDataCenter.RoomDataDic[roomNum].TableHeros) { if (item.ID == ID) { gameDataCenter.RoomDataDic[roomNum].BackHeros.Add(item); gameDataCenter.RoomDataDic[roomNum].TableHeros.Remove(item); } } SendHerosToNext(gameDataCenter, roomNum, seatNum); }
//选择英雄到自己的名下 private static void DealSelectHeroToMe(GameDataCenter gameDataCenter, string[] gameStrs) { int roomNum = Convert.ToInt32(gameStrs[0]); int seatNum = Convert.ToInt32(gameStrs[1]); int ID = Convert.ToInt32(gameStrs[4]); int nums = gameDataCenter.RoomDataDic[roomNum].PlayerDataDic.Count; foreach (var item in gameDataCenter.RoomDataDic[roomNum].TableHeros) { if (item.ID == ID) { gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[seatNum].Role.Add(item); gameDataCenter.RoomDataDic[roomNum].HeroToPlayer.Add(item.ID, seatNum); gameDataCenter.RoomDataDic[roomNum].TableHeros.Remove(item); } } //如果不是king还要盖住一个牌 if (!gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[seatNum].IsKing) { NetCtrl.Send(gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[seatNum].Socket, "2|4|1|" + GetTableHeroString(gameDataCenter, roomNum)); } else { SendHerosToNext(gameDataCenter, roomNum, seatNum); } }
//向所有在房间的玩家推送粗略的房间信息 static string RoomDataPushAll(GameDataCenter gameDataCenter) { string s = "1|4|0|"; foreach (var item in gameDataCenter.RoomDataDic) { s += (item.Key + "|"); s += (item.Value.PlayerDataDic.Count + "|"); s += (item.Value.PlayerDataDic[1].NickName + "|"); } s += "*"; foreach (var item in gameDataCenter.LobbySocketList) { NetCtrl.Send(item, s); } return "1"; }
//向特定房间推送特定的房间详细信息 static string RoomDataPush(GameDataCenter gameDataCenter,int roomNumTemp ) { string s = "1|4|1|"+roomNumTemp+"|"; foreach (var item in gameDataCenter.RoomDataDic[roomNumTemp].PlayerDataDic) { s += (item.Value.Seat + "|"); s += (item.Value.NickName + "|"); } s += "*"; foreach (var item in gameDataCenter.RoomSeatSockets[roomNumTemp]) { NetCtrl.Send(item, s); } return "1"; }
//判断是否可以开始游戏,如果可以开始,返回给对应的房间的创始人 private static bool IsStartEnable(GameDataCenter gameDataCenter, Socket socket, string roomStatus, ref int roomToStart) { string[] status = SegmentData(roomStatus); if (status[2] == "0") return false; if (status[4] == "2") { int.TryParse(status[3], out roomToStart); return true; } return false; }
//选择建筑牌进行建筑 private static void DealSelectBuildingToBuild(GameDataCenter gameDataCenter, string[] gameStrs) { int roomNum = Convert.ToInt32(gameStrs[0]); int seatNum = Convert.ToInt32(gameStrs[1]); switch (gameStrs[4]) { //建筑1张牌的时候 case "0": int id = Convert.ToInt32(gameStrs[5]); foreach (var item in gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[seatNum].PocketBuildings) { if (item.ID == id) { gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[seatNum].TableBuilding.Add(item); gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[seatNum].PocketBuildings.Remove(item); } } break; //建筑多张牌的时候 case "1": int bNum = gameStrs.Length - 5; List<int> idList = new List<int>(); for (int i = 0; i < bNum; i++) { idList.Add(Convert.ToInt32(gameStrs[i + 5])); } foreach (var item in gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[seatNum].PocketBuildings) { if (idList.Contains(item.ID)) { gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[seatNum].TableBuilding.Add(item); gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[seatNum].PocketBuildings.Remove(item); } } break; default: return; } }
//将某个对象的某张牌拆掉 private static void DealSelectBuildingToBreak(GameDataCenter gameDataCenter, string[] gameStrs) { int roomNum = Convert.ToInt32(gameStrs[0]); int seatNum = Convert.ToInt32(gameStrs[1]); int targetSeatNum = Convert.ToInt32(gameStrs[4]); int id = Convert.ToInt32(gameStrs[5]); foreach (var item in gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[targetSeatNum].TableBuilding) { if (item.ID == id) { gameDataCenter.RoomDataDic[roomNum].BackBuildings.Add(item); gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[targetSeatNum].TableBuilding.Remove(item); } } }
//对选择建筑的处理 private static void DealSelectBuilding(GameDataCenter gameDataCenter, string[] gameStrs) { switch (gameStrs[3]) { //对纳入囊中的处理 case "0": DealSelectBuildingToMe(gameDataCenter, gameStrs); break; //对进行建筑的处理 case "1": DealSelectBuildingToBuild(gameDataCenter, gameStrs); break; //对拆牌建筑的处理 case "2": DealSelectBuildingToBreak(gameDataCenter, gameStrs); break; default: return; } }
//处理更新房间信息的请求 private static string RoomDataUpdate(GameDataCenter gameDataCenter, Socket socket, string[] strTemp) { switch(strTemp[1]) { //初次请求更新房间信息 case "0": gameDataCenter.LobbySocketList.Add(socket); string str = "1|4|0|"; foreach (var item in gameDataCenter.RoomDataDic) { str += (item.Key + "|"); str += (item.Value.PlayerDataDic.Count + "|"); str += (item.Value.PlayerDataDic[1].NickName + "|"); } return str; //返回错误信息 default:return "1|4|-1|"; } }
//将Heros发给下一家进行选牌 private static void SendHerosToNext(GameDataCenter gameDataCenter, int roomNum, int seatNum) { int playerNum = gameDataCenter.RoomDataDic[roomNum].PlayerDataDic.Count; int nextPlayerNum; if (seatNum != playerNum) { nextPlayerNum = seatNum + 1; } else { nextPlayerNum = 1; } //如果下家有皇冠,游戏开始;否则,下家继续选择角色牌 if (gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[nextPlayerNum].IsKing) { int i = 0; while (!gameDataCenter.RoomDataDic[roomNum].HeroToPlayer.Keys.Contains(i)) { i++; } int startSeat = gameDataCenter.RoomDataDic[roomNum].HeroToPlayer[i]; NetCtrl.Send(gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[startSeat].Socket, "2|5|"); } else { NetCtrl.Send(gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[nextPlayerNum].Socket, "2|4|0|" + GetTableHeroString(gameDataCenter, roomNum)); } }
//选择建筑牌拿到自己的手中 private static void DealSelectBuildingToMe(GameDataCenter gameDataCenter, string[] gameStrs) { int roomNum = Convert.ToInt32(gameStrs[0]); int seatNum = Convert.ToInt32(gameStrs[1]); switch (gameStrs[4]) { case "0": int id = Convert.ToInt32(gameStrs[5]); foreach (var item in gameDataCenter.RoomDataDic[roomNum].TableBuildings) { if (item.ID == id) { gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[seatNum].PocketBuildings.Add(item); gameDataCenter.RoomDataDic[roomNum].TableBuildings.Remove(item); gameDataCenter.RoomDataDic[roomNum].BackBuildings.AddRange(gameDataCenter.RoomDataDic[roomNum].TableBuildings); gameDataCenter.RoomDataDic[roomNum].TableBuildings.Clear(); } } break; case "1": int id1 = Convert.ToInt32(gameStrs[5]); int id2 = Convert.ToInt32(gameStrs[6]); foreach (var item in gameDataCenter.RoomDataDic[roomNum].TableBuildings) { if (item.ID == id1) { gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[seatNum].PocketBuildings.Add(item); gameDataCenter.RoomDataDic[roomNum].TableBuildings.Remove(item); } if (item.ID == id2) { gameDataCenter.RoomDataDic[roomNum].PlayerDataDic[seatNum].PocketBuildings.Add(item); gameDataCenter.RoomDataDic[roomNum].TableBuildings.Remove(item); } } gameDataCenter.RoomDataDic[roomNum].BackBuildings.AddRange(gameDataCenter.RoomDataDic[roomNum].TableBuildings); gameDataCenter.RoomDataDic[roomNum].TableBuildings.Clear(); break; default: return; } }
//所有的游戏数据的处理 private static void DealGameData(GameDataCenter gameDataCenter, string str) { string[] gameStrs = SegmentData(str); switch (gameStrs[2]) { //对开始游戏的处理 case "0": DealStartGame(gameDataCenter, gameStrs); break; //对选择角色的处理 case "1": DealSelectHero(gameDataCenter, gameStrs); break; //对选择玩家的处理 case "2": DealSelectPlayer(gameDataCenter, gameStrs); break; //对选择建筑的处理 case "3": DealSelectBuilding(gameDataCenter, gameStrs); break; default: return; } }
//对选择角色的请求 private static void DealSelectHero(GameDataCenter gameDataCenter, string[] gameStrs) { switch (gameStrs[3]) { case "0": DealSelectHeroToMe(gameDataCenter, gameStrs); break; case "1": DealSelectHeroToBack(gameDataCenter, gameStrs); break; case "2": DealSelectHeroToKill(gameDataCenter, gameStrs); break; case "3": DealSelectHeroToStole(gameDataCenter, gameStrs); break; default: return; } }