279 lines
6.8 KiB
Go
279 lines
6.8 KiB
Go
package baseroom
|
|
|
|
import (
|
|
"fmt"
|
|
"game/common/proto/pb"
|
|
"game/common/utils"
|
|
"github.com/fox/fox/ipb"
|
|
"github.com/fox/fox/log"
|
|
"github.com/fox/fox/processor"
|
|
"github.com/fox/fox/service"
|
|
"github.com/fox/fox/timer"
|
|
"github.com/golang/protobuf/proto"
|
|
"time"
|
|
)
|
|
|
|
type BaseRoom[Seat ISeat] struct {
|
|
id int
|
|
roomType int // 房间配置id 初级,中级,高级
|
|
playType int // 玩法配置id color玩法id
|
|
gameNo string
|
|
Seats []Seat
|
|
processor *processor.Processor
|
|
timeProcessor *processor.TimerProcessor
|
|
|
|
timeTypes map[timer.ITimeType]uint32
|
|
srv service.IService
|
|
}
|
|
|
|
func NewBaseRoom[Seat ISeat](id, roomType, playType int, srv service.IService) (*BaseRoom[Seat], pb.ErrCode) {
|
|
room := &BaseRoom[Seat]{
|
|
id: id,
|
|
roomType: roomType,
|
|
playType: playType,
|
|
gameNo: "",
|
|
timeTypes: make(map[timer.ITimeType]uint32),
|
|
srv: srv,
|
|
processor: processor.NewProcessor(),
|
|
timeProcessor: processor.NewTimerProcessor(),
|
|
}
|
|
|
|
return room, pb.ErrCode_OK
|
|
}
|
|
|
|
func (r *BaseRoom[Seat]) Id() int {
|
|
return r.id
|
|
}
|
|
|
|
func (r *BaseRoom[Seat]) RoomType() int {
|
|
return r.roomType
|
|
}
|
|
|
|
func (r *BaseRoom[Seat]) PlayType() int {
|
|
return r.playType
|
|
}
|
|
|
|
// 入座玩家数量(包含机器人)
|
|
func (r *BaseRoom[Seat]) SeatPlayerNum() int {
|
|
num := 0
|
|
for _, seat := range r.Seats {
|
|
if !seat.Empty() {
|
|
num++
|
|
}
|
|
}
|
|
return num
|
|
}
|
|
|
|
// 真实玩家数量
|
|
func (r *BaseRoom[Seat]) RealPlayerNum() int {
|
|
num := 0
|
|
for _, seat := range r.Seats {
|
|
if !seat.Empty() && seat.Player().Robot() == nil {
|
|
num++
|
|
}
|
|
}
|
|
return num
|
|
}
|
|
|
|
// 机器人数量
|
|
func (r *BaseRoom[Seat]) RobotPlayerNum() int {
|
|
num := 0
|
|
for _, seat := range r.Seats {
|
|
if !seat.Empty() && seat.Player().Robot() != nil {
|
|
num++
|
|
}
|
|
}
|
|
return num
|
|
}
|
|
|
|
// 返回是否有空座位及空座位编号
|
|
func (r *BaseRoom[Seat]) HasEmptySeat() (bool, int) {
|
|
for _, seat := range r.Seats {
|
|
if seat.Empty() {
|
|
return true, seat.No()
|
|
}
|
|
}
|
|
return false, -1
|
|
}
|
|
|
|
// 查找玩家及玩家所在的座位
|
|
func (r *BaseRoom[Seat]) FindPlayer(uid int64) (IPlayer, Seat) {
|
|
for _, seat := range r.Seats {
|
|
if !seat.Empty() && seat.Player().Id() == uid {
|
|
return seat.Player(), seat
|
|
}
|
|
}
|
|
return nil, utils.TValue[Seat]{}.V
|
|
}
|
|
|
|
func (r *BaseRoom[Seat]) AddPlayer(player IPlayer, seat int) {
|
|
if seat < 0 || seat >= len(r.Seats) {
|
|
log.Error(r.SeatLog(r.Seats[seat], "out of range"))
|
|
return
|
|
}
|
|
r.Seats[seat].SetPlayer(player)
|
|
return
|
|
}
|
|
|
|
func (r *BaseRoom[Seat]) RemovePlayer(player IPlayer) {
|
|
for _, seat := range r.Seats {
|
|
if !seat.Empty() && seat.Player().Id() == player.Id() {
|
|
seat.SetPlayer(nil)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
func (r *BaseRoom[Seat]) RangePlayer(proc func(IPlayer) bool) {
|
|
for _, seat := range r.Seats {
|
|
if !seat.Empty() && !proc(seat.Player()) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (r *BaseRoom[Seat]) FilterPlayer(proc func(IPlayer) bool) []IPlayer {
|
|
players := make([]IPlayer, 0)
|
|
for _, seat := range r.Seats {
|
|
if !seat.Empty() && proc(seat.Player()) {
|
|
players = append(players, seat.Player())
|
|
}
|
|
}
|
|
return players
|
|
}
|
|
|
|
func (r *BaseRoom[Seat]) DebugSendMsg(user IPlayer, msgId pb.MsgId, msg proto.Message) {
|
|
log.Debug(r.UserLog(user.Id(), "send msg:%v %v", msgId, msg.String()))
|
|
}
|
|
|
|
func (r *BaseRoom[Seat]) SendMsg(user IPlayer, msgId pb.MsgId, msg proto.Message) {
|
|
r.DebugSendMsg(user, msgId, msg)
|
|
if user.Robot() != nil {
|
|
user.Robot().OnMessage(msgId, msg)
|
|
} else {
|
|
for _, seat := range r.Seats {
|
|
if !seat.Empty() && seat.Player().Id() == user.Id() {
|
|
iMsg := ipb.MakeMsgEx(r.srv.Name(), 0, user.Id(), int32(msgId), msg)
|
|
_ = r.srv.Send(user.GateTopicName(), iMsg)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func (r *BaseRoom[Seat]) Broadcast(msgId pb.MsgId, msg proto.Message, exclude ...IPlayer) {
|
|
for _, seat := range r.Seats {
|
|
if !seat.Empty() {
|
|
exist := false
|
|
for _, excludePlayer := range exclude {
|
|
if excludePlayer.Id() == seat.Player().Id() {
|
|
exist = true
|
|
break
|
|
}
|
|
}
|
|
if !exist {
|
|
r.SendMsg(seat.Player(), msgId, msg)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (r *BaseRoom[Seat]) NewTimer(timerType timer.ITimeType, duration time.Duration, args ...any) {
|
|
if _, ok := r.timeTypes[timerType]; ok {
|
|
log.Error(r.Log("timer type:%v is exist.can not new timer", timerType.String()))
|
|
// if timerType == TtPlayerAct {
|
|
// log.Debug(r.Log(log.StackTrace()))
|
|
// }
|
|
return
|
|
}
|
|
tid := r.srv.NewTimer(duration+time.Duration(10)*time.Millisecond, func() {
|
|
if err := r.timeProcessor.Dispatch(timerType, args...); err != nil {
|
|
log.ErrorF(r.Log("timer dispatch err:%v", err))
|
|
}
|
|
}, true, r.Log("start type:%v timer", timerType.String()))
|
|
r.timeTypes[timerType] = tid
|
|
// log.Debug(r.Log("start type:%v timer", timerType.String()))
|
|
}
|
|
|
|
func (r *BaseRoom[Seat]) CancelTimer(timerType timer.ITimeType) {
|
|
if tid, ok := r.timeTypes[timerType]; ok {
|
|
r.srv.CancelTimer(tid)
|
|
delete(r.timeTypes, timerType)
|
|
// log.Debug(r.Log("stop type:%v timer, rest timer:%+v", timerType.String(), r.timeTypes))
|
|
}
|
|
}
|
|
|
|
func (r *BaseRoom[Seat]) CancelAllTimer() {
|
|
for _, tid := range r.timeTypes {
|
|
r.srv.CancelTimer(tid)
|
|
// log.Debug(r.Log("start type:%v timer", timerType.String()))
|
|
}
|
|
r.timeTypes = make(map[timer.ITimeType]uint32)
|
|
}
|
|
|
|
func (r *BaseRoom[Seat]) Log(format string, a ...any) string {
|
|
head := fmt.Sprintf("room:%v type:%v ", r.id, r.roomType)
|
|
return head + fmt.Sprintf(format, a...)
|
|
}
|
|
|
|
func (r *BaseRoom[Seat]) SeatLog(seat Seat, format string, a ...any) string {
|
|
head := ""
|
|
if seat.Player() != nil {
|
|
head = fmt.Sprintf("room:%v type:%v seat:%v user:%v robot:%v ", r.id, r.roomType, seat.No(), seat.Player().Id(), seat.Player().Robot() != nil)
|
|
} else {
|
|
head = fmt.Sprintf("room:%v type:%v seat:%v ", r.id, r.roomType, seat.No())
|
|
}
|
|
|
|
return head + fmt.Sprintf(format, a...)
|
|
}
|
|
|
|
func (r *BaseRoom[Seat]) UserLog(uid int64, format string, a ...any) string {
|
|
var seat Seat
|
|
exist := false
|
|
for _, st := range r.Seats {
|
|
if !st.Empty() && st.Player().Id() == uid {
|
|
seat = st
|
|
exist = true
|
|
break
|
|
}
|
|
}
|
|
if exist {
|
|
return r.SeatLog(seat, format, a...)
|
|
} else {
|
|
return r.Log(format, a...)
|
|
}
|
|
}
|
|
|
|
func (r *BaseRoom[Seat]) Unmarshal(cmd int32, data []byte) (any, error) {
|
|
return r.processor.Unmarshal(cmd, data)
|
|
}
|
|
|
|
func (r *BaseRoom[Seat]) Dispatch(user IPlayer, cmd int32, params ...any) error {
|
|
inp := make([]any, len(params)+1)
|
|
inp = append(inp, user)
|
|
inp = append(inp, params...)
|
|
return r.processor.Dispatch(cmd, inp...)
|
|
}
|
|
|
|
func (r *BaseRoom[Seat]) RegisterMessages(metas processor.RegisterMetas) {
|
|
r.processor.RegisterMessages(metas)
|
|
}
|
|
|
|
func (r *BaseRoom[Seat]) timerDispatch(user IPlayer, cmd timer.ITimeType, params ...any) error {
|
|
inp := make([]any, len(params)+1)
|
|
inp = append(inp, user)
|
|
inp = append(inp, params...)
|
|
return r.timeProcessor.Dispatch(cmd, inp...)
|
|
}
|
|
|
|
// 注册时间事件及处理
|
|
func (r *BaseRoom[Seat]) RegisterTimerMessages(metas processor.RegisterTimerMetas) {
|
|
r.timeProcessor.RegisterMessages(metas)
|
|
}
|
|
|
|
// 初始化房间
|
|
func (r *BaseRoom[Seat]) OnInit() {
|
|
|
|
}
|