103 lines
3.0 KiB
Go
103 lines
3.0 KiB
Go
package handler
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/rabbitmq/amqp091-go"
|
|
"samba/pkg/log"
|
|
"samba/proto"
|
|
"samba/server/cacheta/room"
|
|
. "samba/server/cacheta/service"
|
|
. "samba/server/game/baseroom"
|
|
. "samba/server/game/player"
|
|
"samba/stub"
|
|
"samba/util/model"
|
|
"samba/util/util"
|
|
)
|
|
|
|
// 检查金币是否符合要求
|
|
func checkCoin(user *Player, roomCnf *stub.Room) proto.ErrorCode {
|
|
var coins, takeCoins int64
|
|
var err, err1 error
|
|
op := model.NewUserResourceOp()
|
|
coins, err = op.Get(user.UID, model.ResCoins)
|
|
takeCoins, err1 = op.GetTakeCoin(user.UID)
|
|
if err1 != nil || err != nil {
|
|
return proto.Internal
|
|
}
|
|
if coins-takeCoins < roomCnf.MinCoin {
|
|
return proto.NotEnough
|
|
}
|
|
if coins-takeCoins > roomCnf.MaxCoin {
|
|
return proto.RoomCoinLimit
|
|
}
|
|
return proto.Ok
|
|
}
|
|
|
|
func checkMatchRoom(uid int64, req *proto.ReqMatchRoom) (proto.ErrorCode, *Player, IRoom) {
|
|
var err error
|
|
if rmIds := model.GetUserPlayingRoom(uid); len(rmIds) != 0 {
|
|
log.Error(fmt.Sprintf("player:%v is already in truco matchRoom:%v", uid, rmIds))
|
|
return proto.InRoom, nil, nil
|
|
}
|
|
var player *Player
|
|
if player, err = room.NewPlayer(uid); err != nil || player == nil || player.UserInfo == nil {
|
|
if err == nil {
|
|
log.Error(fmt.Sprintf("user:%v new player error", uid))
|
|
} else {
|
|
log.Error(fmt.Sprintf("user:%v new player error:%v", uid, err.Error()))
|
|
}
|
|
return proto.Internal, nil, nil
|
|
}
|
|
// 封禁玩家不能玩
|
|
if player.MStatus == 1 {
|
|
return proto.AccountLocked, nil, nil
|
|
}
|
|
roomCnf, code := stub.FindRoomCnf(req.RoomType)
|
|
if code != proto.Ok {
|
|
return code, nil, nil
|
|
}
|
|
if code = checkCoin(player, roomCnf); code != proto.Ok {
|
|
return code, nil, nil
|
|
}
|
|
rooms := RoomMgr.FindWaitAndReadyRooms(req.RoomType)
|
|
for _, rm := range rooms {
|
|
if rm.HasEmptySeat() {
|
|
return proto.Ok, player, rm
|
|
}
|
|
}
|
|
var rm IRoom
|
|
rm, code = RoomMgr.CreateRoom(req.RoomType, 0, room.NewGameRoom)
|
|
if code != proto.Ok {
|
|
return code, nil, nil
|
|
}
|
|
return code, player, rm
|
|
}
|
|
|
|
// 玩家匹配
|
|
func onMatchRoom(_ *amqp091.Delivery, msg map[string]interface{}) {
|
|
_, _, uid, data := ParseMsg(msg)
|
|
req, err := util.MapToStructT[proto.ReqMatchRoom](data)
|
|
if err != nil {
|
|
log.Error(err.Error())
|
|
return
|
|
}
|
|
code, player, rm := checkMatchRoom(uid, req)
|
|
if code != proto.Ok {
|
|
log.Debug(fmt.Sprintf("user:%v enter matchRoom type:%v err:%v", uid, req.RoomType, code.Error()))
|
|
SendMsgToGate(uid, proto.RspMatchRoomId, &proto.RspMatchRoom{Code: code, RoomType: req.RoomType})
|
|
if code == proto.NotEnough && model.UserIsBankrupt(player.UID) {
|
|
// 提示破产
|
|
ntf, err := model.NewBankruptNtfMsg(uid)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("生成破产通知失败:%v", err))
|
|
} else {
|
|
SendMsgToGate(player.UID, proto.NtfBankruptId, ntf)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
SendMsgToGate(player.UID, proto.RspMatchRoomId, &proto.RspMatchRoom{Code: proto.Ok, RoomType: req.RoomType, Player: MakePlayerToProto(player, 0)})
|
|
enterRoomMsg := util.MakeMessage(proto.ReqEnterRoomId, &proto.ReqEnterRoom{RoomId: rm.Id()}, player.UID, rm.Id())
|
|
rm.OnMessage(proto.ReqEnterRoomId, enterRoomMsg)
|
|
}
|