/// <summary> /// 处理下注的业务请求 /// </summary> /// <param name="clientPeer">要进行下注的玩家</param> /// <param name="roomId">在哪个房间进行的下注</param> /// <param name="money">下了多少注</param> private void ProcessBottomPourRequest(ClientPeer clientPeer, int roomId, int money) { Console.WriteLine("接收到客户端发送过来的下注请求~"); bool isSuccess = this.userCache.SubMoney(clientPeer, money); //使用用户数据缓存对象减少玩家的钱数 if (isSuccess) //减少钱数成功了 { RoomInfo roomInfo = this.roomCache.GetRoomInfoByRoomId(roomId); //根据指定房间编号获取房间信息数据 //记录最后一个下注的玩家 if (!this.passeServiceCache.roomLastBottompourUserDict.ContainsKey(roomInfo)) { this.passeServiceCache.roomLastBottompourUserDict.Add(roomInfo, clientPeer); } else { this.passeServiceCache.roomLastBottompourUserDict[roomInfo] = clientPeer; } //Todo:给客户端发送消息 //通知客户端减少的钱数 UserInfo userInfo = this.roomCache.GetUserInfoByClientPeer(clientPeer);//根据客户端连接对象获取用户信息 //构造要发送给下注客户端的一条网络消息 //业务模块/帕斯响应码/下注响应码|下的注数,下注的玩家座位索引 SocketMessage message = new SocketMessage() { OperationCode = OperationCode.Service, SubOperationCode = (int)ServiceCode.Passe_BottomPourResponse, DataValue = money + "," + userInfo.ClientIndex.ToString()//发送的消息:下注响应码|减少的钱数,减少钱数的客户端座位号 }; Thread.Sleep(1000); //广播消息给每一个房间内的玩家 this.roomCache.BroadcastMessageByRoomId(roomId, message); //通知给房间内的每一个客户端对象,告知他们有玩家下注了. List <ClientPeer> clientPeers = this.roomCache.GetClientPeersByRoomId(roomId); //根据指定房间编号获取房间内的客户端连接对象列表 int tmpIndex = (clientPeers.FindIndex(client => client == clientPeer) + 1); //找出当前玩家的下一个玩家 通知他该跟注了 if (tmpIndex > clientPeers.Count - 1) { tmpIndex = clientPeers.Count - 1; } ClientPeer nextClientPeer = clientPeers[tmpIndex]; //判断当前玩家是否是最后一个玩家 如果是的话 需要给玩家列表中的第一个玩家发跟注释响应消息 if (nextClientPeer == clientPeers[clientPeers.Count - 1] && nextClientPeer == clientPeer) { nextClientPeer = clientPeers[0]; } else//不做任何处理 直接正常发送即可(这里表示当前玩家不是玩家列表中最后一个玩家) { } Thread.Sleep(1000); //将消息更改至跟注释的一条网络消息 //业务模块/帕斯响应码/跟牌响应码 message.ChangeMessage(OperationCode.Service, (int)ServiceCode.Passe_Response, (int)PasseGameCode.Follow_Response); nextClientPeer.OnSendMessage(message);//给下一个玩家发送跟注消息,提示他需要进行跟注 } else//减少钱数失败了 { //Todo:给客户端发送消息,告诉你钱不够了,不能进行下注了.... } }
}//接收消息 #region 获取房间列表 /// <summary> /// 处理客户端对象获取房间列表的请求 /// </summary> /// <param name="clientPeer"></param> private void ProcessGetRoomListRequest(ClientPeer clientPeer) { if (clientPeer == null) { return; } List <RoomInfo> rooms = this.roomCache.Rooms; //取出所有房间 string roomData = string.Empty; //用于存储所有房间的数据 StringBuilder sb = new StringBuilder(); //用于房间数据之间的拼接工作 foreach (RoomInfo room in rooms) //遍历所有房间 { sb.Append(room.Id + "," + room.Name + "|"); //进行房间数据之间的拼接 } if (sb.Length > 0) { roomData = sb.ToString().Remove(sb.Length - 1, 1); } //拼接完成后,将数据发送给请求获取房间列表的客户端对象 message.ChangeMessage(OperationCode.Room, (int)RoomCode.GetRoomList_Response, roomData); clientPeer.OnSendMessage(message); }
/// <summary> /// 通过房间编号更改存储用户信息和用户是否跟牌的数据字典 /// </summary> /// <param name="roomId"></param> /// <param name="clients"></param> public void ChangeRoomFollowcardUserDictionaryByRoomId(int roomId, params ClientPeer[] clients) { RoomInfo tmpRoomInfo = this.GetRoomInfoByRoomId(roomId); //根据房间编号获取房间信息数据 if (tmpRoomInfo != null) //如果获取到了房间 { for (int clientIndex = 0; clientIndex < clients.Length; clientIndex++) //使用循环遍历新进房间来的用户 { if (!this.roomFollowcardUserDict[tmpRoomInfo].ContainsKey(clientPeers[clientIndex])) { this.roomFollowcardUserDict[tmpRoomInfo].Remove(clientPeers[clientIndex]); this.roomFollowcardUserDict[tmpRoomInfo].Add(clients[clientIndex], true);//将他们添加到数据字典中 } else { this.roomFollowcardUserDict[tmpRoomInfo][clientPeers[clientIndex]] = true; } } foreach (KeyValuePair <RoomInfo, Dictionary <ClientPeer, bool> > roomItem in this.roomFollowcardUserDict) { foreach (KeyValuePair <ClientPeer, bool> clientItem in roomItem.Value) { if (clientItem.Value == true) //如果等于true 就表示跟牌 { List <ClientPeer> clientPeers = this.roomCache.GetClientPeersByRoomId(roomId); //根据指定房间编号获取房间内的客户端连接对象列表 int tmpIndex = (clientPeers.FindIndex(client => client == clients[0]) + 1); //找出当前玩家的下一个玩家 通知他该跟注了 if (tmpIndex > clientPeers.Count - 1) { tmpIndex = 0; } ClientPeer nextClientPeer = clientPeers[tmpIndex]; ClientPeer peer = this.roomLastBottompourUserDict[tmpRoomInfo]; if (nextClientPeer != peer) { //业务模块/帕斯响应码/跟牌响应码 message.ChangeMessage(OperationCode.Service, (int)ServiceCode.Passe_Response, (int)PasseGameCode.Follow_Response); nextClientPeer.OnSendMessage(message);//给下一个玩家发送跟注消息,提示他需要进行跟注 } else { LogMessage.Instance.SetLogMessage("跟牌的玩家个数和玩家出牌的索引值一致,开始进行下一轮发牌的工作~"); //Todo:再发一轮明牌 //Todo:下完注之后给这个玩家发牌 ClientPeer clientPeer = null; foreach (KeyValuePair <UserInfo, int> user in this.roomUserMaxScoreDict[tmpRoomInfo]) { clientPeer = this.roomCache.GetClientPeerByUserInfo(user.Key);//根据用户信息获取连接对象[这个对象就是本轮获胜者] break; } if (clientPeer != null) { this.GetClearCardByRoomId(tmpRoomInfo.Id); break; } } } else//如果不等于true 就表示不跟牌 { continue;//直接使用continue 关键字 终止本次循环 开始下次循环 } } break; } } }