|
@@ -0,0 +1,165 @@
|
|
|
|
+package teen
|
|
|
|
+
|
|
|
|
+import (
|
|
|
|
+ "encoding/json"
|
|
|
|
+ "fmt"
|
|
|
|
+ "log"
|
|
|
|
+ "math/rand"
|
|
|
|
+ "os"
|
|
|
|
+ "server/game/room"
|
|
|
|
+ "server/msg"
|
|
|
|
+ "server/user"
|
|
|
|
+
|
|
|
|
+ "github.com/name5566/leaf/gate"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+type TeenPattiRoom struct {
|
|
|
|
+ Boot string `json:"boot"`
|
|
|
|
+ MinBuyin string `json:"minBuyin"`
|
|
|
|
+ ChaalLimmit string `json:"chaalLimmit"`
|
|
|
|
+ PotLimit string `json:"potLimit"`
|
|
|
|
+ TotalPlayers string `json:"totalPlayers"`
|
|
|
|
+ RoomLevel string `json:"roomLevel"`
|
|
|
|
+ RoomId string `json:"roomId"`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+var GameConfig struct {
|
|
|
|
+ GameId int `json:"game_id"`
|
|
|
|
+ Name string `json:"name"`
|
|
|
|
+ Status int `json:"status"`
|
|
|
|
+ RoomList []TeenPattiRoom `json:"room_list"`
|
|
|
|
+ ThinkTime int `json:"think_time"`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// var Teen_Patti_Room = &room.Room{
|
|
|
|
+// Id: "teen_patti",
|
|
|
|
+// Players: make([]*player.Player, 0),
|
|
|
|
+// Status: 1,
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+func init() {
|
|
|
|
+ data, err := os.ReadFile("../../../bin/gamedata/teen_patti_conf.json")
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Fatal("%v", err)
|
|
|
|
+ }
|
|
|
|
+ err = json.Unmarshal(data, &GameConfig)
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Fatal("%v", err)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ go handleEvents()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func createRoom(userId string, roomId string, agent gate.Agent) {
|
|
|
|
+ //随即创建 1-3 个人机陪这个userId
|
|
|
|
+ userData := agent.UserData().(*user.UserData)
|
|
|
|
+ random := rand.Intn(3) + 1
|
|
|
|
+ //先移除掉除自己之外的机器人
|
|
|
|
+ userData.Teen_Patti_Room.RemoveAllRobot()
|
|
|
|
+ sitPos := randomSitPos(userData.Teen_Patti_Room, random)
|
|
|
|
+ for i := 0; i < random; i++ {
|
|
|
|
+ userData.Teen_Patti_Room.Players = append(userData.Teen_Patti_Room.Players, &room.Player{
|
|
|
|
+ Id: fmt.Sprintf("%d", i),
|
|
|
|
+ Agent: nil,
|
|
|
|
+ IsRobot: true,
|
|
|
|
+ UserData: createRobotUserData(),
|
|
|
|
+ SitPos: sitPos[i],
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ randomDealer(userData.Teen_Patti_Room)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 创建人机的UserData
|
|
|
|
+func createRobotUserData() *user.UserData {
|
|
|
|
+ return &user.UserData{
|
|
|
|
+ Id: "robot",
|
|
|
|
+ Status: 1,
|
|
|
|
+ Teen_Patti_Room: nil,
|
|
|
|
+ Head_Image: randomIndianHeadImage(),
|
|
|
|
+ Nickname: randomIndianName(),
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func startGame(userId string, roomId string, agent gate.Agent) {
|
|
|
|
+ // agent := Teen_Patti_Room.GetPlayer(userId).Agent
|
|
|
|
+ createRoom(userId, roomId, agent)
|
|
|
|
+ agent.WriteMsg(&msg.ReqJoinRoom{
|
|
|
|
+ UserId: userId,
|
|
|
|
+ RoomId: roomId,
|
|
|
|
+ GameId: "teen_patti",
|
|
|
|
+ })
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func convertToMsgRoomList(rooms []TeenPattiRoom) *msg.TeenPattiRoomList {
|
|
|
|
+ msgRooms := make([]*msg.TeenPattiRoom, len(rooms))
|
|
|
|
+ for i, room := range rooms {
|
|
|
|
+ msgRooms[i] = &msg.TeenPattiRoom{
|
|
|
|
+ Boot: room.Boot,
|
|
|
|
+ MinBuyin: room.MinBuyin,
|
|
|
|
+ ChaalLimmit: room.ChaalLimmit,
|
|
|
|
+ PotLimit: room.PotLimit,
|
|
|
|
+ TotalPlayers: room.TotalPlayers,
|
|
|
|
+ RoomLevel: room.RoomLevel,
|
|
|
|
+ RoomId: room.RoomId,
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return &msg.TeenPattiRoomList{
|
|
|
|
+ TeenPattiRoom: msgRooms,
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* 完整的52张牌映射:
|
|
|
|
+黑桃(0):
|
|
|
|
+ i=0: ♠A (Color=0, Point=1)
|
|
|
|
+ i=1: ♠2 (Color=0, Point=2)
|
|
|
|
+ i=2: ♠3 (Color=0, Point=3)
|
|
|
|
+ ...
|
|
|
|
+ i=10: ♠J (Color=0, Point=11)
|
|
|
|
+ i=11: ♠Q (Color=0, Point=12)
|
|
|
|
+ i=12: ♠K (Color=0, Point=13)
|
|
|
|
+
|
|
|
|
+红桃(1):
|
|
|
|
+ i=13: ♥A (Color=1, Point=1)
|
|
|
|
+ i=14: ♥2 (Color=1, Point=2)
|
|
|
|
+ i=15: ♥3 (Color=1, Point=3)
|
|
|
|
+ ...
|
|
|
|
+ i=23: ♥J (Color=1, Point=11)
|
|
|
|
+ i=24: ♥Q (Color=1, Point=12)
|
|
|
|
+ i=25: ♥K (Color=1, Point=13)
|
|
|
|
+
|
|
|
|
+梅花(2):
|
|
|
|
+ i=26: ♣A (Color=2, Point=1)
|
|
|
|
+ i=27: ♣2 (Color=2, Point=2)
|
|
|
|
+ i=28: ♣3 (Color=2, Point=3)
|
|
|
|
+ ...
|
|
|
|
+ i=36: ♣J (Color=2, Point=11)
|
|
|
|
+ i=37: ♣Q (Color=2, Point=12)
|
|
|
|
+ i=38: ♣K (Color=2, Point=13)
|
|
|
|
+
|
|
|
|
+方块(3):
|
|
|
|
+ i=39: ♦A (Color=3, Point=1)
|
|
|
|
+ i=40: ♦2 (Color=3, Point=2)
|
|
|
|
+ i=41: ♦3 (Color=3, Point=3)
|
|
|
|
+ ...
|
|
|
|
+ i=49: ♦J (Color=3, Point=11)
|
|
|
|
+ i=50: ♦Q (Color=3, Point=12)
|
|
|
|
+ i=51: ♦K (Color=3, Point=13)
|
|
|
|
+*/
|
|
|
|
+// 初始化房间的卡牌
|
|
|
|
+func initCardDeck(c_room *room.Room) {
|
|
|
|
+ // 按照花色和点数初始化卡牌, 52张, 4种花色, 13种点数
|
|
|
|
+ c_room.CardDeck = make([]room.Card, 52)
|
|
|
|
+ for i := 0; i < 52; i++ {
|
|
|
|
+ c_room.CardDeck[i] = room.Card{
|
|
|
|
+ Color: i / 13,
|
|
|
|
+ Point: i%13 + 1,
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 洗牌
|
|
|
|
+func shuffleCardDeck(deck []room.Card) {
|
|
|
|
+ rand.Shuffle(len(deck), func(i, j int) {
|
|
|
|
+ deck[i], deck[j] = deck[j], deck[i]
|
|
|
|
+ })
|
|
|
|
+}
|