123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627 |
- 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)
- // }
|