golang制作一个斗地主游戏服务器[15]:再写打牌和跟牌(下)

本来两个文章想叠加写在一起的,发现内容有点多, 拆分一下

前面讲了客户端发送协议的, 现在就要讲服务器如何接收到协议

 // 大部分还是抄袭23协议的内容
		case 26:
			// 26 是打掉具体手牌的
			req := pack.GetOutCardReq()
			log.Println("玩家要求打牌", req)

			nTableIndex := req.GetTableIndex()
			// 找一下有没有这个桌子
			pTable := GetTableByIndex(int(nTableIndex))
			if pTable == nil {
				log.Println("找不到桌子 ID = [", nTableIndex, "]")
				return
			}


			if !pTable.checkPlayerValid(pData) {
				log.Println("发现一个非法的用户发包")
				return
			}

			// 拿到手牌的所有内容, 并且通过 chan传入
			outCards := req.GetOutCards()
			pTable.ddz.chPlayDZ <- outCards.GetCardValue() // 把打牌的具体内容通过chan
		}

通过协议和 chan 送进来的手牌列表,  会在ddz的逻辑里面处理


// 回合开始
func (self *TDDZ) roundStart() {
	log.Println("回合开始, 准备打牌")
	//1. 新人打牌, 打第一张
	ch := make(chan []int32, 1)
	self.chPlayDZ = ch

	var pOutCards c.TCards  // 打出的牌的列表
	var pLeftCards c.TCards // 剩余的牌的列表
	select {
	case n := <-ch:
		log.Println("模拟打牌1", n)
		// 把协议的数组转换成打出的手牌类
		pOutCards = c.NewCardsByArray(n)
		// 记得排序
		pOutCards.Sort()
		log.Println("本轮要打的牌是:", self.nTurnPosition)
		pOutCards.Print()
		// 得到剩余的牌的列表
		pLeftCards = self.pHandCardList[self.nTurnPosition-1].Out(pOutCards)
		log.Println("本轮剩余的牌是:")
		pLeftCards.Print()

	case <-time.After(time.Second * 10000): // 假设5秒超时
		log.Println("roundStart 超时")

		// 默认当做单牌
		// 要找到一个能匹配上的牌组
		log.Println("打出前")
		self.pHandCardList[self.nTurnPosition-1].Print()

		// 超时默认AI托管打牌
		pOutCards, pLeftCards = self.pHandCardList[self.nTurnPosition-1].AI(c.TypeSINGLE, c.PointNIL)

		//
		log.Println("位置", self.nTurnPosition, "打出牌:")
		pOutCards.Print()
	}

	// 选完以后重新弄上手牌
	self.pHandCardList[self.nTurnPosition-1] = c.NewCards(len(pLeftCards))
	// 把剩余的牌直接覆盖掉手牌的列表里
	copy(self.pHandCardList[self.nTurnPosition-1], pLeftCards)
	log.Println("新手牌是")
	self.pHandCardList[self.nTurnPosition-1].Print()

	// 凡是有人打过牌, 那么最大的牌, 不就是这个当前的位置?
	self.nLargePosition = self.nTurnPosition
	self.nTurnPosition++ // 当前轮到的人的
	if self.nTurnPosition > 3 {
		self.nTurnPosition -= 3
	}
	self.nHand++ // 手次

	self.nCardType, self.nCardPoint = pOutCards.GetType() // 当前出牌牌型 // 当前出牌点数

	// 这里准备发牌
	self.pushHandCards()
}


// 广播手牌
func (self *TDDZ) pushHandCards() {
	for i := 0; i < 3; i++ {
		pPlayer := self.pTable.pPlayerList[i]
		pack := &ddzpb.TDDZ{}
		pack.DealCardBc = &ddzpb.TDealCardBc{}
		pack.DealCardBc.Position = proto.Int(i + 1)

		pack.DealCardBc.YourCards = pbTCards(&self.pHandCardList[i]) // 手牌发给3家
		pack.Command = proto.Int(21)
		buff, _ := proto.Marshal(pack)
		pPlayer.Conn.WritePack(buff)
	}
}

猜你喜欢

转载自blog.csdn.net/warrially/article/details/89142000