samba/server/truco/handler/matchHandler.go
2025-06-04 09:51:39 +08:00

209 lines
6.7 KiB
Go

package handler
import (
"fmt"
"github.com/rabbitmq/amqp091-go"
"samba/pkg/log"
"samba/proto"
. "samba/server/game/player"
. "samba/server/truco/room"
. "samba/server/truco/service"
"samba/stub"
"samba/util/model"
"samba/util/util"
)
// 玩家匹配
func onMatchClubRoom(_ *amqp091.Delivery, msg map[string]interface{}) {
_, _, uid, data := ParseMsg(msg)
req, err := util.MapToStructT[proto.ReqMatchClubRoom](data)
if err != nil {
log.Error(err.Error())
return
}
rsp := &proto.RspMatchClubRoom{Code: proto.Ok, RoomType: req.RoomType, ClubId: req.ClubId}
if rmIds := model.GetUserPlayingRoom(uid); len(rmIds) != 0 {
log.Error(fmt.Sprintf("player:%v is already in truco matchRoom:%v", uid, rmIds))
rsp.Code = proto.InRoom
SendMsgToGate(uid, proto.RspMatchClubRoomId, &rsp)
return
}
var player *Player
if player, err = 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()))
}
rsp.Code = proto.Internal
SendMsgToGate(uid, proto.RspMatchClubRoomId, &rsp)
return
}
// 封禁玩家不能玩
if player.MStatus == 1 {
rsp.Code = proto.AccountLocked
SendMsgToGate(uid, proto.RspMatchClubRoomId, &rsp)
return
}
roomCnf, code := stub.FindRoomCnf(req.RoomType)
if code != proto.Ok || roomCnf == nil || roomCnf.Mode != int(stub.RmClub) {
rsp.Code = proto.BadParam
SendMsgToGate(uid, proto.RspMatchClubRoomId, &rsp)
return
}
req.ClubId = 10000
rsp.Code = GMatchClubQueue.EnterMatchQueue(player, req.RoomType, req.ClubId)
if rsp.Code != proto.Ok {
log.Debug(fmt.Sprintf("user:%v enter matchRoom type:%v err:%v", uid, req.RoomType, rsp.Code.Error()))
SendMsgToGate(player.UID, proto.RspMatchClubRoomId, rsp)
return
}
//rsp.Player = MakePlayerToProto(player, req.ClubId)
SendMsgToGate(player.UID, proto.RspMatchClubRoomId, rsp)
}
// 取消玩家匹配
func onCancelMatchClubRoom(_ *amqp091.Delivery, msg map[string]interface{}) {
_, _, uid, data := ParseMsg(msg)
req, err := util.MapToStructT[proto.ReqCancelMatchRoom](data)
if err != nil {
log.Error(err.Error())
return
}
var player *Player
if player, err = 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()))
}
SendMsgToGate(uid, proto.RspCancelMatchClubRoomId, &proto.RspCancelMatchClubRoom{Code: proto.Internal, RoomType: req.RoomType})
return
}
cancelRet := proto.BadParam
for {
// 取消该玩家所有的匹配
matchRoom := GMatchClubQueue.FindRoomByPlayer(player.UID)
if matchRoom != nil {
// 房间只有他一个真人,则回收机器人及移除房间
players := matchRoom.RemovePlayer(player.UID)
realPlayerNum := len(matchRoom.RealPlayers())
if realPlayerNum == 0 {
if len(players) > 0 {
for _, p := range players {
_ = matchRoom.RemovePlayer(p.UID)
if p.IsRobot() {
RobotMgr.Push(p.Player)
}
}
}
GMatchClubQueue.RemoveRoom(matchRoom.Id(), matchRoom.Type())
}
cancelRet = proto.Ok
log.Debug(fmt.Sprintf("user:%v leave matching queue. cancel matching", player.UID))
} else {
break
}
}
SendMsgToGate(player.UID, proto.RspCancelMatchClubRoomId, &proto.RspCancelMatchClubRoom{Code: cancelRet, RoomType: req.RoomType})
}
// 玩家匹配
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
}
if rmIds := model.GetUserPlayingRoom(uid); len(rmIds) != 0 {
log.Error(fmt.Sprintf("player:%v is already in truco matchRoom:%v", uid, rmIds))
SendMsgToGate(uid, proto.RspMatchRoomId, &proto.RspMatchRoom{Code: proto.InRoom, RoomType: req.RoomType})
return
}
var player *Player
if player, err = 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()))
}
SendMsgToGate(uid, proto.RspMatchRoomId, &proto.RspMatchRoom{Code: proto.Internal, RoomType: req.RoomType})
return
}
// 封禁玩家不能玩
if player.MStatus == 1 {
rsp := &proto.RspMatchRoom{Code: proto.Ok, RoomType: req.RoomType}
rsp.Code = proto.AccountLocked
SendMsgToGate(uid, proto.RspMatchRoomId, &rsp)
return
}
code := GMatchQueue.EnterMatchQueue(player, req.RoomType, 0)
if code != proto.Ok {
log.Debug(fmt.Sprintf("user:%v enter matchRoom type:%v err:%v", uid, req.RoomType, code.Error()))
SendMsgToGate(player.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)})
}
// 取消玩家匹配
func onCancelMatchRoom(_ *amqp091.Delivery, msg map[string]interface{}) {
_, _, uid, data := ParseMsg(msg)
req, err := util.MapToStructT[proto.ReqCancelMatchRoom](data)
if err != nil {
log.Error(err.Error())
return
}
var player *Player
if player, err = 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()))
}
SendMsgToGate(uid, proto.RspCancelMatchRoomId, &proto.RspCancelMatchRoom{Code: proto.Internal, RoomType: req.RoomType})
return
}
cancelRet := proto.BadParam
for {
// 取消该玩家所有的匹配
matchRoom := GMatchQueue.FindRoomByPlayer(player.UID)
if matchRoom != nil {
// 房间只有他一个真人,则回收机器人及移除房间
players := matchRoom.RemovePlayer(player.UID)
realPlayerNum := len(matchRoom.RealPlayers())
if realPlayerNum == 0 {
if len(players) > 0 {
for _, p := range players {
_ = matchRoom.RemovePlayer(p.UID)
if p.IsRobot() {
RobotMgr.Push(p.Player)
}
}
}
GMatchQueue.RemoveRoom(matchRoom.Id(), matchRoom.Type())
}
cancelRet = proto.Ok
log.Debug(fmt.Sprintf("user:%v leave matching queue. cancel matching", player.UID))
} else {
break
}
}
SendMsgToGate(player.UID, proto.RspCancelMatchRoomId, &proto.RspCancelMatchRoom{Code: cancelRet, RoomType: req.RoomType})
}