328 lines
9.4 KiB
Go
328 lines
9.4 KiB
Go
![]() |
package handler
|
|||
|
|
|||
|
import (
|
|||
|
"fmt"
|
|||
|
"github.com/rabbitmq/amqp091-go"
|
|||
|
"samba/pkg/log"
|
|||
|
"samba/proto"
|
|||
|
. "samba/server/game/baseroom"
|
|||
|
. "samba/server/game/player"
|
|||
|
. "samba/server/truco/room"
|
|||
|
. "samba/server/truco/service"
|
|||
|
"samba/util/config"
|
|||
|
"samba/util/model"
|
|||
|
"samba/util/state"
|
|||
|
"samba/util/util"
|
|||
|
)
|
|||
|
|
|||
|
func getUserClubAllScore(userClubInfo *model.UserClubInfo) int64 {
|
|||
|
return userClubInfo.Score + userClubInfo.FreeScore + userClubInfo.InOutFreeScore
|
|||
|
}
|
|||
|
|
|||
|
func checkEnterClubRoom(userClubInfo *model.UserClubInfo, req *proto.ReqEnterClubRoom) (proto.ErrorCode, IRoom) {
|
|||
|
if req.ClubId == 0 {
|
|||
|
log.Debug(fmt.Sprintf("club:%v not exist", req.ClubId))
|
|||
|
return proto.BadParam, nil
|
|||
|
}
|
|||
|
if userClubInfo == nil {
|
|||
|
log.Debug(fmt.Sprintf("userClubInfo is nil"))
|
|||
|
return proto.NotInClub, nil
|
|||
|
}
|
|||
|
if getUserClubAllScore(userClubInfo) < 1 {
|
|||
|
log.Debug(fmt.Sprintf("user:%v club:%v socre:%v freeScore:%v inoutScore:%v not enough", userClubInfo.UID, req.ClubId, userClubInfo.Score,
|
|||
|
userClubInfo.FreeScore, userClubInfo.InOutFreeScore))
|
|||
|
return proto.UserClubScoreNotEnough, nil
|
|||
|
}
|
|||
|
|
|||
|
clubInfo, _ := model.NewClubInfoOp().Load(req.ClubId)
|
|||
|
clubCoin := int64(0)
|
|||
|
if clubInfo != nil {
|
|||
|
clubCoin, _ = model.NewUserResourceOp().Get(clubInfo.Creator, model.ResClubCoins)
|
|||
|
if clubInfo.Open != 1 {
|
|||
|
return proto.ClubNotOpen, nil
|
|||
|
}
|
|||
|
}
|
|||
|
// 部长身上俱乐部币不足
|
|||
|
if clubCoin < 1 {
|
|||
|
return proto.ClubCoinNotEnough, nil
|
|||
|
}
|
|||
|
|
|||
|
waitRooms, err := model.NewClubPlayingRoomOp().LoadAll(req.ClubId)
|
|||
|
if err != nil {
|
|||
|
log.Debug(fmt.Sprintf("club:%v not exist waiting rooms", req.ClubId))
|
|||
|
return proto.Internal, nil
|
|||
|
}
|
|||
|
var waitRoom *model.ClubPlayingRoom
|
|||
|
for _, wtRoom := range waitRooms {
|
|||
|
if req.RoomID == wtRoom.RoomId {
|
|||
|
waitRoom = wtRoom
|
|||
|
break
|
|||
|
} else {
|
|||
|
if wtRoom.RoomType == req.RoomType {
|
|||
|
waitRoom = wtRoom
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
if waitRoom != nil {
|
|||
|
if waitRoom.Blind > getUserClubAllScore(userClubInfo) {
|
|||
|
return proto.UserClubScoreNotEnough, nil
|
|||
|
}
|
|||
|
if rm := RoomMgr.Find(waitRoom.RoomId); rm != nil && rm.ClubId() == req.ClubId {
|
|||
|
return proto.Ok, rm
|
|||
|
}
|
|||
|
}
|
|||
|
return proto.NotClubPlayGame, nil
|
|||
|
}
|
|||
|
|
|||
|
// 玩家进俱乐部房间
|
|||
|
func onEnterClubRoom(_ *amqp091.Delivery, msg map[string]interface{}) {
|
|||
|
_, _, uid, data := ParseMsg(msg)
|
|||
|
req, err := util.MapToStructT[proto.ReqEnterClubRoom](data)
|
|||
|
if err != nil {
|
|||
|
log.Error(err.Error())
|
|||
|
return
|
|||
|
}
|
|||
|
log.Debug(fmt.Sprintf("recv user:%v ReqEnterClubRoom msg:%v", uid, req))
|
|||
|
rsp := &proto.RspEnterClubRoom{Code: proto.Ok, ClubId: req.ClubId, RoomID: req.RoomID, RoomType: req.RoomType}
|
|||
|
var user *Player
|
|||
|
if user, err = NewPlayer(uid); err != nil || user == nil || user.UserInfo == nil {
|
|||
|
if err == nil {
|
|||
|
log.Error(fmt.Sprintf("user:%v new user error", uid))
|
|||
|
} else {
|
|||
|
log.Error(fmt.Sprintf("user:%v new user error:%v", uid, err.Error()))
|
|||
|
}
|
|||
|
rsp.Code = proto.Internal
|
|||
|
SendMsgToGate(uid, proto.RspEnterClubRoomId, rsp)
|
|||
|
return
|
|||
|
}
|
|||
|
userClubInfo, _ := model.NewUserClubInfoOp().Load(uid, req.ClubId)
|
|||
|
code, iRm := checkEnterClubRoom(userClubInfo, req)
|
|||
|
rsp.Code = code
|
|||
|
SendMsgToGate(uid, proto.RspEnterClubRoomId, rsp)
|
|||
|
if iRm != nil {
|
|||
|
trucoRoom := iRm.(*TrucoRoom)
|
|||
|
trucoRoom.OnEnterRoom(user)
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 1. 房间匹配规则:走以下判断
|
|||
|
// 1.1. 判断1:携带的积分是否>0
|
|||
|
// 1.1. 是——走判断2
|
|||
|
// 1.2. 否——提示:您的积分不足,请联系管理员!
|
|||
|
// 2. 判断2:是否为俱乐部内首次对局
|
|||
|
// 2.1. 是——将用户放入到可进入且能尽快开始的房间中
|
|||
|
// 2.2. 否——走判断3
|
|||
|
// 3. 判断3:携带积分是否满足进入上次对局的场次
|
|||
|
// 3.1. 是——根据用户上次的进入场次匹配房间
|
|||
|
// 3.2. 否——将用户放入到可进入且能尽快开始的房间中
|
|||
|
func checkQuickEnterClubRoom(clubInfo *model.ClubInfo, userClubInfo *model.UserClubInfo, req *proto.ReqQuickEnterClubRoom) (proto.ErrorCode, IRoom) {
|
|||
|
if req.ClubId == 0 {
|
|||
|
log.Debug(fmt.Sprintf("club:%v can not 0 ", req.ClubId))
|
|||
|
return proto.BadParam, nil
|
|||
|
}
|
|||
|
if userClubInfo == nil {
|
|||
|
log.Debug(fmt.Sprintf("club:%v user club info can not nil", req.ClubId))
|
|||
|
return proto.BadParam, nil
|
|||
|
}
|
|||
|
if clubInfo == nil {
|
|||
|
log.Debug(fmt.Sprintf("club:%v not exist", req.ClubId))
|
|||
|
return proto.NotInClub, nil
|
|||
|
}
|
|||
|
if clubInfo.Open != 1 {
|
|||
|
return proto.ClubNotOpen, nil
|
|||
|
}
|
|||
|
if getUserClubAllScore(userClubInfo) < 1 {
|
|||
|
log.Debug(fmt.Sprintf("user:%v club:%v socre:%v freeScore:%v inoutScore:%v not enough", userClubInfo.UID, req.ClubId, userClubInfo.Score,
|
|||
|
userClubInfo.FreeScore, userClubInfo.InOutFreeScore))
|
|||
|
return proto.UserClubScoreNotEnough, nil
|
|||
|
}
|
|||
|
clubCoin, _ := model.NewUserResourceOp().Get(clubInfo.Creator, model.ResClubCoins)
|
|||
|
// 部长身上俱乐部币不足
|
|||
|
if clubCoin < 1 {
|
|||
|
return proto.ClubCoinNotEnough, nil
|
|||
|
}
|
|||
|
var waitRoom *model.ClubPlayingRoom
|
|||
|
// 最近玩的房间配置id
|
|||
|
roomType := model.NewUserQuickMatchOp().GetRoomType(userClubInfo.UID, req.PlayType)
|
|||
|
// 等待开始房间
|
|||
|
waitAndPlayingRooms, err := model.NewClubPlayingRoomOp().LoadAll(req.ClubId)
|
|||
|
if err != nil {
|
|||
|
log.Debug(fmt.Sprintf("club:%v not exist waiting rooms", req.ClubId))
|
|||
|
return proto.Internal, nil
|
|||
|
}
|
|||
|
if roomType != 0 {
|
|||
|
for _, info := range waitAndPlayingRooms {
|
|||
|
if info.RoomType == roomType && info.Status <= int(state.RsReadyStart) {
|
|||
|
if userClubInfo.Score < info.Blind {
|
|||
|
continue
|
|||
|
}
|
|||
|
if info.MaxPlayerNum-info.CurrPlayerNum < 1 {
|
|||
|
continue
|
|||
|
}
|
|||
|
waitRoom = info
|
|||
|
break
|
|||
|
}
|
|||
|
}
|
|||
|
// 没有该房间,则查找玩法一致的可进入房间
|
|||
|
if waitRoom == nil {
|
|||
|
for _, info := range waitAndPlayingRooms {
|
|||
|
if info.PlayType == req.PlayType {
|
|||
|
if userClubInfo.Score < info.Blind {
|
|||
|
continue
|
|||
|
}
|
|||
|
if info.MaxPlayerNum-info.CurrPlayerNum < 1 {
|
|||
|
continue
|
|||
|
}
|
|||
|
waitRoom = info
|
|||
|
break
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
if waitRoom == nil {
|
|||
|
return proto.NotClubPlayGame, nil
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 查看能立即凑满人数开局的房间
|
|||
|
if waitRoom == nil {
|
|||
|
needPlayerNum := 100
|
|||
|
for _, info := range waitAndPlayingRooms {
|
|||
|
if info.PlayType == req.PlayType && info.Status <= int(state.RsReadyStart) {
|
|||
|
if userClubInfo.Score < info.Blind {
|
|||
|
continue
|
|||
|
}
|
|||
|
if needPlayerNum <= info.MaxPlayerNum-info.CurrPlayerNum {
|
|||
|
continue
|
|||
|
}
|
|||
|
needPlayerNum = info.MaxPlayerNum - info.CurrPlayerNum
|
|||
|
waitRoom = info
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
if waitRoom == nil {
|
|||
|
return proto.UserClubScoreNotEnough, nil
|
|||
|
} else {
|
|||
|
if rm := RoomMgr.Find(waitRoom.RoomId); rm != nil && rm.ClubId() == req.ClubId {
|
|||
|
return proto.Ok, rm
|
|||
|
} else {
|
|||
|
return proto.NotClubPlayGame, nil
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 俱乐部玩法快速进房间
|
|||
|
func onQuickEnterClubRoom(_ *amqp091.Delivery, msg map[string]interface{}) {
|
|||
|
_, _, uid, data := ParseMsg(msg)
|
|||
|
req, err := util.MapToStructT[proto.ReqQuickEnterClubRoom](data)
|
|||
|
if err != nil {
|
|||
|
log.Error(err.Error())
|
|||
|
return
|
|||
|
}
|
|||
|
log.Debug(fmt.Sprintf("recv user:%v ReqQuickEnterClubRoom msg:%v", uid, req))
|
|||
|
userClubInfo, _ := model.NewUserClubInfoOp().Load(uid, req.ClubId)
|
|||
|
clubInfo, _ := model.NewClubInfoOp().Load(req.ClubId)
|
|||
|
code, iRm := checkQuickEnterClubRoom(clubInfo, userClubInfo, req)
|
|||
|
rsp := &proto.RspQuickEnterClubRoom{Code: code, ClubId: req.ClubId, PlayType: req.PlayType}
|
|||
|
if code != proto.Ok {
|
|||
|
SendMsgToGate(uid, proto.RspQuickEnterClubRoomId, rsp)
|
|||
|
return
|
|||
|
}
|
|||
|
|
|||
|
var user *Player
|
|||
|
if user, err = NewPlayer(uid); err != nil || user == nil || user.UserInfo == nil {
|
|||
|
if err == nil {
|
|||
|
log.Error(fmt.Sprintf("user:%v new user error", uid))
|
|||
|
} else {
|
|||
|
log.Error(fmt.Sprintf("user:%v new user error:%v", uid, err.Error()))
|
|||
|
}
|
|||
|
rsp.Code = proto.Internal
|
|||
|
SendMsgToGate(uid, proto.RspQuickEnterClubRoomId, rsp)
|
|||
|
return
|
|||
|
}
|
|||
|
rsp.Code = code
|
|||
|
SendMsgToGate(uid, proto.RspQuickEnterClubRoomId, rsp)
|
|||
|
if iRm != nil {
|
|||
|
trucoRoom := iRm.(*TrucoRoom)
|
|||
|
trucoRoom.OnEnterRoom(user)
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 俱乐部创建玩法
|
|||
|
func onCreatePlayGame(_ *amqp091.Delivery, msg map[string]interface{}) {
|
|||
|
if TrucoService.AutoWaitStop {
|
|||
|
return
|
|||
|
}
|
|||
|
// 不是俱乐部服务
|
|||
|
if config.Cmd.IsClub == 0 {
|
|||
|
return
|
|||
|
}
|
|||
|
_, _, _, data := ParseMsg(msg)
|
|||
|
req, err := util.MapToStructT[proto.RspCreatePlayGame](data)
|
|||
|
if err != nil {
|
|||
|
log.Error(err.Error())
|
|||
|
return
|
|||
|
}
|
|||
|
RoomMgr.CreateClubRoom(req.ClubId, req.GameInfo.Id, NewTrucoRoom)
|
|||
|
}
|
|||
|
|
|||
|
// 俱乐部删除玩法
|
|||
|
func onDelPlayGame(_ *amqp091.Delivery, msg map[string]interface{}) {
|
|||
|
// 不是俱乐部服务
|
|||
|
if config.Cmd.IsClub == 0 {
|
|||
|
return
|
|||
|
}
|
|||
|
_, _, _, data := ParseMsg(msg)
|
|||
|
req, err := util.MapToStructT[proto.RspDeletePlayGame](data)
|
|||
|
if err != nil {
|
|||
|
log.Error(err.Error())
|
|||
|
return
|
|||
|
}
|
|||
|
RoomMgr.DeleteClubRoom(req.ClubId, req.PlayGameId)
|
|||
|
if TrucoService.AutoWaitStop {
|
|||
|
return
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
// 俱乐部修改玩法
|
|||
|
func onUpdatePlayGame(_ *amqp091.Delivery, msg map[string]interface{}) {
|
|||
|
// 不是俱乐部服务
|
|||
|
if config.Cmd.IsClub == 0 {
|
|||
|
return
|
|||
|
}
|
|||
|
_, _, _, data := ParseMsg(msg)
|
|||
|
req, err := util.MapToStructT[proto.RspUpdatePlayGame](data)
|
|||
|
if err != nil {
|
|||
|
log.Error(err.Error())
|
|||
|
return
|
|||
|
}
|
|||
|
RoomMgr.DeleteClubRoom(req.ClubId, req.SrcGameId)
|
|||
|
if TrucoService.AutoWaitStop {
|
|||
|
return
|
|||
|
}
|
|||
|
RoomMgr.CreateClubRoom(req.ClubId, req.GameInfo.Id, NewTrucoRoom)
|
|||
|
}
|
|||
|
|
|||
|
// 俱乐部修改玩法
|
|||
|
func onSetPlayGameValid(_ *amqp091.Delivery, msg map[string]interface{}) {
|
|||
|
// 不是俱乐部服务
|
|||
|
if config.Cmd.IsClub == 0 {
|
|||
|
return
|
|||
|
}
|
|||
|
_, _, _, data := ParseMsg(msg)
|
|||
|
req, err := util.MapToStructT[proto.RspSetPlayGameValid](data)
|
|||
|
if err != nil {
|
|||
|
log.Error(err.Error())
|
|||
|
return
|
|||
|
}
|
|||
|
if req.Valid == 0 {
|
|||
|
RoomMgr.DeleteClubRoom(req.ClubId, req.PlayGameId)
|
|||
|
return
|
|||
|
}
|
|||
|
if TrucoService.AutoWaitStop {
|
|||
|
return
|
|||
|
}
|
|||
|
RoomMgr.CreateClubRoom(req.ClubId, req.PlayGameId, NewTrucoRoom)
|
|||
|
}
|