package game //这里处理房间状态的变化 type QZNNStatus int type QznnRoundUserStatus int const ( QZNNStatusInit QZNNStatus = 1 //等待有2位准备好 QZNNStatusWait QZNNStatus = 2 //等待加入,此时不能退出 QZNNStatusChooseMaster QZNNStatus = 3 //选择坐庄完成 QZNNStatusChooseMul QZNNStatus = 4 //选择倍数完成 QZNNStatusCal QZNNStatus = 5 //结算完,进入初始化状态 QznnRoundUserStatusWait QznnRoundUserStatus = 1 //等待选庄 QznnRoundUserStatusWaitMul QznnRoundUserStatus = 2 //等待选倍数 QznnRoundUserStatusWaitOpen QznnRoundUserStatus = 3 //等待开牌 QznnRoundUserStatusWaitCal QznnRoundUserStatus = 4 //等待结算 ) // var globalIndex uint64 = 800000 // var qznnRoundMap = map[uint32]*QznnRound{} // var qznnRoundMapLocker = sync.Mutex{} // type ReadyReq struct { // RoomID uint32 // UserID uint32 // } // type ChooseMasterReq struct { // RoomID uint32 // UserID uint32 // Mul int //0不抢,1,2,3 // } // type ChooseMulReq struct { // RoomID uint32 // UserID uint32 // Mul int //1-15 // } // type OpenReq struct { // RoomID uint32 // UserID uint32 // } // type QznnRoundUser struct { // UserID uint32 // MasterMul int //抢庄倍数 // ChooseMul int //倍数 // IsMaster bool //是否为庄家 // Status QznnRoundUserStatus //状态 // } // type QznnRound struct { // RoomID uint32 // Issue string // Status QZNNStatus // UserMap map[uint32]*QznnRoundUser // StartTime time.Time // ChooseMasterCh chan bool // ChooseMulCh chan bool // OpenCh chan bool // } // //按时间处理状态变化 // func (p *QznnRound) Start() { // p.StartTime = time.Now() // //发送开始消息 // { // data, err := proto.Marshal(&gameproto.Start{ // RoomID: p.RoomID, // MulList: []uint32{0, 1, 2, 3}, // }) // if err != nil { // logrus.Error(err) // return // } // nxd.SendMsgToRoomUser(p.RoomID, online.Msg{ // RoomID: p.RoomID, // NotifyType: uint32(gameproto.NotifyTypeEnum_NotifyTypeStart), // Data: string(data), // }) // } // logrus.Info("QznnReady start ==>", p.StartTime) // //等待完毕 // onStatusChange(p) // //选择坐庄 // select { // case <-p.ChooseMasterCh: // // // case <-time.After(5 * time.Second): // func() { // //等超时,则默认处理 // qznnRoundMapLocker.Lock() // defer qznnRoundMapLocker.Unlock() // for _, item := range p.UserMap { // if item.MasterMul == 0 { // item.MasterMul = 1 // data, err := proto.Marshal(&gameproto.ChooseMaster{ // UserID: item.UserID, // Mul: uint32(item.MasterMul), // }) // if err != nil { // logrus.Error(err) // return // } // nxd.SendMsgToRoomUser(p.RoomID, online.Msg{ // RoomID: p.RoomID, // NotifyType: uint32(gameproto.NotifyTypeEnum_NotifyTypeChooseMaster), // Data: string(data), // }) // } // } // }() // } // p.Status = QZNNStatusChooseMaster // //选完 // func() { // qznnRoundMapLocker.Lock() // defer qznnRoundMapLocker.Unlock() // userID := uint32(0) // for _, item := range p.UserMap { // userID = item.UserID // break // } // data, err := proto.Marshal(&gameproto.ChooseMasterFinish{ // MasterUserID: userID, // MulList: []uint32{1, 2, 3, 4, 5, 6, 7, 8}, // RoomID: p.RoomID, // }) // if err != nil { // logrus.Error(err) // return // } // nxd.SendMsgToRoomUser(p.RoomID, online.Msg{ // RoomID: p.RoomID, // NotifyType: uint32(gameproto.NotifyTypeEnum_NotifyTypeChooseMasterFinish), // Data: string(data), // }) // }() // onStatusChange(p) // //选择倍数 // select { // case <-p.ChooseMulCh: // // // case <-time.After(5 * time.Second): // func() { // //等超时,则默认处理 // qznnRoundMapLocker.Lock() // defer qznnRoundMapLocker.Unlock() // for _, item := range p.UserMap { // if item.MasterMul == 0 { // item.MasterMul = 1 // data, err := proto.Marshal(&gameproto.ChooseMaster{ // UserID: item.UserID, // Mul: uint32(item.ChooseMul), // RoomID: p.RoomID, // }) // if err != nil { // logrus.Error(err) // return // } // nxd.SendMsgToRoomUser(p.RoomID, online.Msg{ // RoomID: p.RoomID, // NotifyType: uint32(gameproto.NotifyTypeEnum_NotifyTypeChooseMaster), // Data: string(data), // }) // } // } // }() // } // p.Status = QZNNStatusChooseMul // //选完 // func() { // qznnRoundMapLocker.Lock() // defer qznnRoundMapLocker.Unlock() // data, err := proto.Marshal(&gameproto.ChooseMulinish{ // RoomID: p.RoomID, // }) // if err != nil { // logrus.Error(err) // return // } // nxd.SendMsgToRoomUser(p.RoomID, online.Msg{ // RoomID: p.RoomID, // NotifyType: uint32(gameproto.NotifyTypeEnum_NotifyTypeChooseMulinish), // Data: string(data), // }) // }() // //开牌 // select { // case <-p.OpenCh: // // // case <-time.After(15 * time.Second): // func() { // //等超时,则默认处理 // qznnRoundMapLocker.Lock() // defer qznnRoundMapLocker.Unlock() // for _, item := range p.UserMap { // if item.Status == QznnRoundUserStatusWaitOpen { // item.Status = QznnRoundUserStatusWaitCal // data, err := proto.Marshal(&gameproto.Open{ // CardList: []uint32{1, 2, 3, 4, 5}, // Winloss: 100, // UserID: item.UserID, // RoomID: p.RoomID, // }) // if err != nil { // logrus.Error(err) // return // } // nxd.SendMsgToRoomUser(p.RoomID, online.Msg{ // RoomID: p.RoomID, // NotifyType: uint32(gameproto.NotifyTypeEnum_NotifyTypeOpen), // Data: string(data), // }) // } // } // }() // } // <-time.After(3 * time.Second) // p.Status = QZNNStatusInit // data, err := proto.Marshal(&gameproto.GameInit{}) // if err != nil { // logrus.Error(err) // return // } // nxd.SendMsgToRoomUser(p.RoomID, online.Msg{ // RoomID: p.RoomID, // NotifyType: uint32(gameproto.NotifyTypeEnum_NotifyTypeGameInit), // Data: string(data), // }) // onStatusChange(p) // } // func QznnRoomStatus(roomID uint32) (QZNNStatus, bool) { // qznnRoundMapLocker.Lock() // defer qznnRoundMapLocker.Unlock() // roomRound, exists := qznnRoundMap[roomID] // if !exists { // return QZNNStatusInit, false // } // return roomRound.Status, true // } // func QznnReady(ctx *gin.Context) { // var err error // readyReq := ReadyReq{} // if err := ctx.ShouldBindJSON(&readyReq); err != nil { // logrus.Error(err) // webutils.FailedResponseErr(ctx, err) // return // } // userModel := model.User{} // err = userModel.GetUserByID(readyReq.UserID) // if err != nil { // logrus.Error(err) // webutils.FailedResponse(ctx, "内部错误") // return // } // qznnRoundMapLocker.Lock() // defer qznnRoundMapLocker.Unlock() // roomRound, exists := qznnRoundMap[readyReq.RoomID] // if !exists { // roomRound = &QznnRound{ // RoomID: readyReq.RoomID, // Issue: GetIssue(readyReq.RoomID), // Status: QZNNStatusInit, // UserMap: map[uint32]*QznnRoundUser{}, // StartTime: time.Time{}, // ChooseMasterCh: make(chan bool), // ChooseMulCh: make(chan bool), // OpenCh: make(chan bool), // } // qznnRoundMap[readyReq.RoomID] = roomRound // } // if roomRound.Status != QZNNStatusWait && roomRound.Status != QZNNStatusInit { // webutils.FailedResponse(ctx, "游戏已经开始,无法加入") // return // } // _, exists = roomRound.UserMap[uint32(userModel.ID)] // if !exists { // roomRound.UserMap[uint32(userModel.ID)] = &QznnRoundUser{ // UserID: uint32(userModel.ID), // MasterMul: 1, // ChooseMul: 1, // IsMaster: false, // Status: QznnRoundUserStatusWait, // } // } else { // webutils.SuccessResponse(ctx, gin.H{ // "status": "exists", // }) // } // if len(roomRound.UserMap) >= 2 && roomRound.Status == QZNNStatusInit { // //开始 // roomRound.Status = QZNNStatusWait // go roomRound.Start() // } // webutils.SuccessResponse(ctx, nil) // } // func QznnUnReady(ctx *gin.Context) { // readyReq := ReadyReq{} // if err := ctx.ShouldBindJSON(&readyReq); err != nil { // logrus.Error(err) // webutils.FailedResponseErr(ctx, err) // return // } // qznnRoundMapLocker.Lock() // defer qznnRoundMapLocker.Unlock() // roomRound, exists := qznnRoundMap[readyReq.RoomID] // if !exists { // roomRound = &QznnRound{ // RoomID: readyReq.RoomID, // Issue: GetIssue(readyReq.RoomID), // Status: QZNNStatusInit, // UserMap: map[uint32]*QznnRoundUser{}, // StartTime: time.Time{}, // ChooseMasterCh: make(chan bool), // ChooseMulCh: make(chan bool), // OpenCh: make(chan bool), // } // qznnRoundMap[readyReq.RoomID] = roomRound // } // if roomRound.Status != QZNNStatusInit { // webutils.FailedResponse(ctx, "游戏已经开始,无法取消") // return // } // delete(roomRound.UserMap, readyReq.UserID) // webutils.SuccessResponse(ctx, nil) // } // func QznnChooseMaster(ctx *gin.Context) { // req := ChooseMasterReq{} // if err := ctx.ShouldBindJSON(&req); err != nil { // logrus.Error(err) // webutils.FailedResponseErr(ctx, err) // return // } // qznnRoundMapLocker.Lock() // defer qznnRoundMapLocker.Unlock() // roomRound, exists := qznnRoundMap[req.RoomID] // if !exists { // logrus.Error("用户不存在") // webutils.FailedResponse(ctx, "用户错误") // return // } // if roomRound.Status != QZNNStatusWait { // logrus.Error("状态错误") // webutils.FailedResponse(ctx, "状态错误") // return // } // roomRound.UserMap[req.UserID].Status = QznnRoundUserStatusWaitMul // roomRound.UserMap[req.UserID].MasterMul = req.Mul // allIsWaitMul := true // for _, item := range roomRound.UserMap { // if item.Status != QznnRoundUserStatusWaitMul { // allIsWaitMul = false // break // } // } // //发消息 // data, err := proto.Marshal(&gameproto.ChooseMaster{ // UserID: req.UserID, // Mul: uint32(roomRound.UserMap[req.UserID].MasterMul), // }) // if err != nil { // logrus.Error(err) // return // } // nxd.SendMsgToRoomUser(roomRound.RoomID, online.Msg{ // RoomID: roomRound.RoomID, // NotifyType: uint32(gameproto.NotifyTypeEnum_NotifyTypeChooseMaster), // Data: string(data), // }) // if allIsWaitMul { // func() { // roomRound.ChooseMasterCh <- true // }() // } // webutils.SuccessResponse(ctx, nil) // } // func QznnChooseMul(ctx *gin.Context) { // req := ChooseMulReq{} // if err := ctx.ShouldBindJSON(&req); err != nil { // logrus.Error(err) // webutils.FailedResponseErr(ctx, err) // return // } // qznnRoundMapLocker.Lock() // defer qznnRoundMapLocker.Unlock() // roomRound, exists := qznnRoundMap[req.RoomID] // if !exists { // logrus.Error("用户不存在") // webutils.FailedResponse(ctx, "用户错误") // return // } // if roomRound.Status != QZNNStatusChooseMaster { // logrus.Error("状态错误") // webutils.FailedResponse(ctx, "状态错误") // return // } // roomRound.UserMap[req.UserID].Status = QznnRoundUserStatusWaitOpen // roomRound.UserMap[req.UserID].ChooseMul = req.Mul // allIsWaitCal := true // for _, item := range roomRound.UserMap { // if item.Status != QznnRoundUserStatusWaitOpen { // allIsWaitCal = false // break // } // } // data, err := proto.Marshal(&gameproto.ChooseMul{ // UserID: req.UserID, // Mul: uint32(roomRound.UserMap[req.UserID].ChooseMul), // }) // if err != nil { // logrus.Error(err) // return // } // nxd.SendMsgToRoomUser(roomRound.RoomID, online.Msg{ // RoomID: roomRound.RoomID, // NotifyType: uint32(gameproto.NotifyTypeEnum_NotifyTypeChooseMul), // Data: string(data), // }) // if allIsWaitCal { // func() { // roomRound.ChooseMulCh <- true // }() // } // webutils.SuccessResponse(ctx, nil) // } // func QznnOpen(ctx *gin.Context) { // req := OpenReq{} // if err := ctx.ShouldBindJSON(&req); err != nil { // logrus.Error(err) // webutils.FailedResponseErr(ctx, err) // return // } // qznnRoundMapLocker.Lock() // defer qznnRoundMapLocker.Unlock() // roomRound, exists := qznnRoundMap[req.RoomID] // if !exists { // logrus.Error("用户不存在") // webutils.FailedResponse(ctx, "用户错误") // return // } // if roomRound.Status != QZNNStatusChooseMul { // logrus.Error("状态错误") // webutils.FailedResponse(ctx, "状态错误") // return // } // roomRound.UserMap[req.UserID].Status = QznnRoundUserStatusWaitCal // allIsWaitCal := true // for _, item := range roomRound.UserMap { // if item.Status != QznnRoundUserStatusWaitMul { // allIsWaitCal = false // break // } // } // data, err := proto.Marshal(&gameproto.Open{ // CardList: []uint32{1, 2, 3, 4, 5}, // Winloss: 100, // UserID: req.UserID, // RoomID: req.RoomID, // }) // if err != nil { // logrus.Error(err) // return // } // nxd.SendMsgToRoomUser(req.RoomID, online.Msg{ // RoomID: req.RoomID, // NotifyType: uint32(gameproto.NotifyTypeEnum_NotifyTypeOpen), // Data: string(data), // }) // if allIsWaitCal { // func() { // roomRound.OpenCh <- true // }() // } // webutils.SuccessResponse(ctx, nil) // } // func onStatusChange(p *QznnRound) { // qznnRoundMapLocker.Lock() // defer qznnRoundMapLocker.Unlock() // if p.Status == QZNNStatusWait { // //等3秒,用于新进来的人准备 // <-time.After(3 * time.Second) // return // } // if p.Status == QZNNStatusChooseMaster { // fmt.Println("master finish") // return // } // if p.Status == QZNNStatusChooseMul { // fmt.Println("mul finish") // return // } // if p.Status == QZNNStatusCal { // fmt.Println("open finish") // return // } // if p.Status == QZNNStatusInit { // //清理,然后发通知 // for k := range p.UserMap { // delete(p.UserMap, k) // } // fmt.Println("重新进入初始化,等下一轮开始") // return // } // //这里是异常了 // logrus.Fatal("异常:", p) // } // func GetIssue(roomID uint32) string { // index := atomic.AddUint64(&globalIndex, 1) // return fmt.Sprintf("%d-%d-%d", roomID, time.Now().Unix(), index) // }