package entertainment import ( "go_dreamfactory/comm" "go_dreamfactory/modules" "go_dreamfactory/pb" "go.mongodb.org/mongo-driver/bson/primitive" "google.golang.org/protobuf/proto" ) const ( MaxPs = 2 // 最大体力 ) //游戏房间 type Room struct { modules.ModuleBase Id string // 房间id s1 comm.IUserSession s2 comm.IUserSession player1 *pb.PlayerData // 玩家1 player2 *pb.PlayerData // 玩家2 chessboard *MapData module *Entertainment power string // 谁的权限 round int32 // 轮数 } func (this *Room) InitRoom(module *Entertainment, s1 comm.IUserSession, s2 comm.IUserSession) *Room { this.chessboard = new(MapData) this.chessboard.InitMap() // 初始化棋盘 defer this.StartGame() return &Room{ ModuleBase: modules.ModuleBase{}, Id: primitive.NewObjectID().Hex(), s1: s1, s2: s2, player1: &pb.PlayerData{ Uid: s1.GetUserId(), Name: "", Score: 0, Ps: MaxPs, }, player2: &pb.PlayerData{ Uid: s2.GetUserId(), Name: "", Score: 0, Ps: MaxPs, }, chessboard: this.chessboard, module: module, power: s1.GetUserId(), // 默认1号玩家先 round: 1, } } func (this *Room) ReceiveMessage(session comm.IUserSession, stype string, msg proto.Message) (errdata *pb.ErrorData) { switch stype { case "opertor": // 操作消息 var ( curScore int32 ) var szMap []*pb.MapData req := msg.(*pb.EntertainOperatorReq) //权限校验 if this.power == this.player1.Uid { this.player1.Ps-- if this.player1.Ps <= 0 { // 权限给下一个人 this.power = this.player2.Uid } this.player2.Ps = MaxPs } else if this.power == this.player2.Uid { this.player2.Ps-- if this.player2.Ps <= 0 { // 权限给下一个人 this.power = this.player1.Uid this.round++ } this.player1.Ps = MaxPs } else { // err 未知权限 return } // 交换元素 this.chessboard.SwapGirde(req.Curid, req.Targetid) // 交换格子 for { if b, score := this.chessboard.CheckMap(); b { break } else { curScore += score // 统计积分 } this.chessboard.DropGirde() szMap = append(szMap, &pb.MapData{ Data: this.chessboard.Data, }) } // 操作消息返回 this.s1.SendMsg(string(this.module.GetType()), "operator", &pb.EntertainOperatorResp{ Success: true, }) // 广播消息 if err := this.SendMsgToSession(string(this.module.GetType()), "operatorrst", &pb.EntertainOperatorRstPush{ Mpadata: szMap, Power: this.power, Score: curScore, Round: this.round, }, []comm.IUserSession{this.s1, this.s2}...); err != nil { this.Errorln(err) } } return } // 玩家操作 func (this *Room) Opertor(uid string, iType int32, old int32, new int32) (errdata *pb.ErrorData) { if iType == 1 { this.chessboard.SwapGirde(old, new) // 交换格子 } return } func (this *Room) StartGame() (errdata *pb.ErrorData) { if err := this.SendMsgToSession(string(this.module.GetType()), "startgame", &pb.EntertainStartGamePush{ User1: this.player1, User2: this.player2, Mpadata: &pb.MapData{ Data: this.chessboard.Data, }, Power: this.power, Round: this.round, }, []comm.IUserSession{this.s1, this.s2}...); err != nil { this.Errorln(err) } return }