/// <summary> /// 计算牌型大小 /// </summary> private void CalculCardBigSmall() { var p = playerInfo.GetEnumerator(); JsonData js = new JsonData(); while (p.MoveNext()) { JsonData j = new JsonData(); p.Current.Value.info.cowType = MostMaxCard(p.Current.Value.info.card); j["chairId"] = p.Current.Value.info.chairId; j["nick"] = p.Current.Value.nick; j["cowType"] = new JsonData(); j["card"] = new JsonData(); for (int i = 0; i < p.Current.Value.info.cowType.Length; i++) { j["cowType"].Add(p.Current.Value.info.cowType[i]); } for (int i = 0; i < p.Current.Value.info.card.Count; i++) { j["card"].Add(p.Current.Value.info.card[i]); } js.Add(j); } ClientDataItem item = new ClientDataItem(); item.main_CMD = 100; item.sub_CMD = 2; item.data = ToolManager.GetBytes(JsonMapper.ToJson(js)); GroupSendMessage(item); }
/// <summary> /// 发送所有玩家消息 /// </summary> public void SendAllPlayerInfo() { JsonData js = new JsonData(); js["player"] = new JsonData(); var player = playerInfo.GetEnumerator(); while (player.MoveNext()) { JsonData j = new JsonData(); j["chairId"] = player.Current.Value.info.chairId; j["nick"] = player.Current.Value.nick; j["userId"] = player.Current.Value.userId; j["status"] = player.Current.Value.status; j["coin"] = player.Current.Value.coin; j["sex"] = player.Current.Value.sex; js["player"].Add(j); } ClientDataItem c = new ClientDataItem(); c.main_CMD = 100; c.sub_CMD = 1; c.data = ToolManager.GetBytes(JsonMapper.ToJson(js)); GroupSendMessage(c); }
/// <summary> /// 群发消息 /// </summary> internal void GroupSendMessage(ClientDataItem item) { byte[] bytes = ToolManager.ObjectToBytes(item.main_CMD, item.sub_CMD, item.data); var player = playerInfo.GetEnumerator(); while (player.MoveNext()) { player.Current.Value.socket.Send(bytes); } }
private Dictionary <string, UserInfoManager> playerInfo = new Dictionary <string, UserInfoManager>(); //所有在这个房间玩家的信息 public NiuNiu(ClientDataItem item) { JsonData js = JsonMapper.ToObject(ToolManager.GetString(item.data)); peopleQuantity = byte.Parse(js["people"].ToString()); isChair = new bool[peopleQuantity]; for (int i = 0; i < isChair.Length; i++) { isChair[i] = false; } }
/// <summary> /// 接收消息 /// </summary> public void ReceiveGameData(ClientDataItem item) { JsonData js = JsonMapper.ToObject(ToolManager.GetString(item.data)); switch (item.sub_CMD) { case 1: //接收玩家状态信息,并判断所有玩家准备后,发牌 playerInfo[item.ip].status = ushort.Parse(js["status"].ToString()); byte count = 0; var e = playerInfo.GetEnumerator(); while (e.MoveNext()) { if (e.Current.Value.status == (ushort)GameState.Ready) { count++; } } if (count == peopleQuantity) //所有人准备 发牌 { cardTypeCount = 0; SendCard(count, item); } break; case 2: //接收客户端发过来的牌型 cardTypeCount++; playerInfo[item.ip].info.card.Clear(); JsonData cardType = js["cardType"]; for (int i = 0; i < cardType.Count; i++) { playerInfo[item.ip].info.card.Add(byte.Parse(cardType[i].ToString())); } if (cardTypeCount == peopleQuantity) //所有人牌型已经摆好,计算牌型大小得分并群发返回 { CalculCardBigSmall(); } break; case 11: break; } }
/// <summary> /// 发牌,随机发,每人直接发五张 /// </summary> /// 54张扑克随机抽五张 private void SendCard(byte people, ClientDataItem item) { JsonData js = new JsonData(); List <List <byte> > player = new List <List <byte> >(); List <byte> isExist = new List <byte>(); for (int i = 0; i < people; i++) { player.Add(Allocation(isExist)); } var p = playerInfo.GetEnumerator(); while (p.MoveNext()) { js["chairId"] = p.Current.Value.info.chairId; js["card"] = new JsonData(); js["card"].Add(player[p.Current.Value.info.chairId]); item.ip = p.Current.Value.ip; item.sub_CMD = 2; item.data = ToolManager.GetBytes(JsonMapper.ToJson(js)); SendMessage(item); } }
/// <summary> /// 单发消息 /// </summary> internal void SendMessage(ClientDataItem item) { playerInfo[item.ip].socket.Send(ToolManager.ObjectToBytes(item.main_CMD, item.sub_CMD, item.data)); }