使用SuperSocket开发联网斗地主(三):抢地主
本次实现抢地主功能,此功能的难点在于按顺序区分自己和对家,以及出牌顺序,服务端按顺序分玩家顺序很好办,但到了客户端每个人都是坐在最下面的位置,对家要按顺序分配在左右两边。
抢地主分为几种情况:
1. 玩家1抢 玩家2抢 玩家3抢,玩家3中
2. 玩家1抢 玩家2抢 玩家3不抢,玩家2中
3. 玩家1抢 玩家2不抢 玩家3不抢,玩家1中
4. 玩家1不抢 玩家2不抢 玩家3不抢,玩家3中
然后就是谁中谁出牌,然后把底牌给地主,抢地主逻辑如下:
DouDiZhuGameConfig.GameDouble = DouDiZhuGameConfig.GameDouble * 2;//加倍 DouDiZhuGameConfig.Player_DiZhu = session.SessionID;//标记临时地主 DouDiZhuGameConfig.RequestDiZhuIndex++; //分别通知各位玩家 foreach (var item in DouDiZhuGameConfig.OnlineUserList) { var play_session = webSocketServer.GetSessionByID(item.Key); ResponseModel<DouDiZhuUserInfoModel> res_sendCard = new ResponseModel<DouDiZhuUserInfoModel>(); res_sendCard.Action = "requestdizhu_ok"; res_sendCard.Data = item.Value; res_sendCard.Data.IsMyTurn = false; res_sendCard.Data.GameDouble = DouDiZhuGameConfig.GameDouble; //标记下个出牌者 if(DouDiZhuGameConfig.RequestDiZhuIndex + 1==item.Value.PlayerIndex) { item.Value.IsMyTurn = true; } //抢地主完毕 if(DouDiZhuGameConfig.RequestDiZhuIndex==3) { if (item.Key == DouDiZhuGameConfig.Player_DiZhu) { res_sendCard.Data.IsDiZhu = true; res_sendCard.Data.IsMyTurn = true;//地主先出 res_sendCard.Data.MyCards.AddRange(item.Value.CommonCards);//把底牌给地主 } res_sendCard.Data.CommonCards = new List<DouDiZhuGameCard>();//清空每个人的底牌 item.Value.CommonCards = new List<DouDiZhuGameCard>();//清空每个人的底牌 } this.SendMessage(play_session, JsonConvert.SerializeObject(res_sendCard)); }
还有不抢地主的逻辑:
if (DouDiZhuGameConfig.RequestDiZhuIndex < 4) { DouDiZhuGameConfig.RequestDiZhuIndex++; //if(DouDiZhuGameConfig.RequestDiZhuIndex==2)// //{ // DouDiZhuGameConfig.Player_DiZhu = session.SessionID; //} //分别通知各位玩家 foreach (var item in DouDiZhuGameConfig.OnlineUserList) { var play_session = webSocketServer.GetSessionByID(item.Key); ResponseModel<DouDiZhuUserInfoModel> res_sendCard = new ResponseModel<DouDiZhuUserInfoModel>(); res_sendCard.Action = "requestdizhu_ok"; res_sendCard.Data = item.Value; res_sendCard.Data.IsMyTurn = false; //标记下个出牌者 if (DouDiZhuGameConfig.RequestDiZhuIndex + 1 == item.Value.PlayerIndex) { item.Value.IsMyTurn = true; } //1不抢,2不抢,3不抢 if (DouDiZhuGameConfig.RequestDiZhuIndex == 2&& DouDiZhuGameConfig.Player_DiZhu=="") { if (item.Value.PlayerIndex == 3) { res_sendCard.Data.IsDiZhu = true; res_sendCard.Data.IsMyTurn = true;//地主先出 res_sendCard.Data.MyCards.AddRange(item.Value.CommonCards);//把底牌给地主 } res_sendCard.Data.CommonCards = new List<DouDiZhuGameCard>();//清空每个人的底牌 item.Value.CommonCards = new List<DouDiZhuGameCard>();//清空每个人的底牌 } //1不抢,2抢,3不抢 1抢,2不抢,3不抢 else if (DouDiZhuGameConfig.RequestDiZhuIndex == 3 && DouDiZhuGameConfig.Player_DiZhu != "") { if (item.Key==DouDiZhuGameConfig.Player_DiZhu) { res_sendCard.Data.IsDiZhu = true; res_sendCard.Data.IsMyTurn = true;//地主先出 res_sendCard.Data.MyCards.AddRange(item.Value.CommonCards);//把底牌给地主 } res_sendCard.Data.CommonCards = new List<DouDiZhuGameCard>();//清空每个人的底牌 item.Value.CommonCards = new List<DouDiZhuGameCard>();//清空每个人的底牌 } this.SendMessage(play_session, JsonConvert.SerializeObject(res_sendCard)); }
然后就是,不管谁抢了,总得给大家都通知下:
//公共信息广播 if (DouDiZhuGameConfig.RequestDiZhuIndex == 3) { List<DouDiZhuUserInfoModel> pubUserInfo = new List<DouDiZhuUserInfoModel>(); foreach (var itemSession in webSocketServer.GetAllSessions()) { ResponseModel<List<DouDiZhuUserInfoModel>> res_pubInfo = new ResponseModel<List<DouDiZhuUserInfoModel>>(); pubUserInfo.Clear(); foreach (var itemUser in DouDiZhuGameConfig.OnlineUserList) { pubUserInfo.Add(new DouDiZhuUserInfoModel { UserName = itemUser.Value.UserName, UserMoney = itemUser.Value.UserMoney, IsDiZhu = itemUser.Value.IsDiZhu, GameDouble = itemUser.Value.GameDouble, PlayerIndex = itemUser.Value.PlayerIndex, IsMe = itemUser.Key == itemSession.SessionID }); } res_pubInfo.Action = "pubInfo"; res_pubInfo.Data = pubUserInfo; this.SendMessage(itemSession, JsonConvert.SerializeObject(res_pubInfo)); } }
客户端处理桌子分配有三种情况,逻辑如下:
1-3* 2-1 3-2 |
1-2 2-3* 3-1 |
1-1 2-2 3-3* |
//公布广播信息
function pubUserInfo(mydata, userData) { var myIndex = 0, myName = mydata.UserName; for (let o of userData) { if (o.UserName == myName) { myIndex = o.PlayerIndex; nowBeiShu = o.GameDouble; break; } } // 1-3* // 2-1 // 3-2 if (myIndex == 1) { for (let o of userData) { //找到我 if (o.PlayerIndex == 1) { o.PlayerIndex = 3; Player3Info = o; } else if (o.PlayerIndex == 2) { o.PlayerIndex = 1; Player1Info = o; } else if (o.PlayerIndex == 3) { o.PlayerIndex = 2; Player2Info = o; } } } // 1-2 // 2-3* // 3-1 if (myIndex == 2) { for (let o of userData) { //找到我 if (o.PlayerIndex == 1) { o.PlayerIndex = 2; Player2Info = o; } else if (o.PlayerIndex == 2) { o.PlayerIndex = 3; Player3Info = o; } else if (o.PlayerIndex == 3) { o.PlayerIndex = 1; Player1Info = o; } } } // 1-1 // 2-2 // 3-3* if (myIndex == 3) { for (let o of userData) { //找到我 if (o.PlayerIndex == 1) { Player1Info = o; } else if (o.PlayerIndex == 2) { Player2Info = o; } else if (o.PlayerIndex == 3) { Player3Info = o; } } }
最终效果如: