game_sync/worldsrv/scenepolicydata.go

214 lines
5.3 KiB
Go

package main
import (
"math"
"mongo.games.com/goserver/core/logger"
"mongo.games.com/game/common"
"mongo.games.com/game/model"
hallproto "mongo.games.com/game/protocol/gamehall"
"mongo.games.com/game/protocol/webapi"
)
type ScenePolicyData struct {
GameName string
GameId int32
GameMode []int32
DefaultPlayerCnt int32
EnterAfterStart bool
BetState int32
BaseScore int
}
func (spd *ScenePolicyData) Init() bool {
return true
}
func (spd *ScenePolicyData) OnStart(s *Scene) {
s.NotifyPrivateRoom(common.ListAdd)
}
// 场景关闭事件
func (spd *ScenePolicyData) OnStop(s *Scene) {
s.NotifyPrivateRoom(common.ListDel)
// 房主付费,房间没有玩就解散了,返还房主建房费用
if s.IsCustom() && s.GetCostType() == 2 && s.currRound == 0 {
spd.GiveCostPayment(s, s.creator)
}
}
// 场景心跳事件
func (spd *ScenePolicyData) OnTick(s *Scene) {
}
// 玩家进入事件
func (spd *ScenePolicyData) OnPlayerEnter(s *Scene, p *Player) {
s.NotifyPrivateRoom(common.ListModify)
}
// 玩家离开事件
func (spd *ScenePolicyData) OnPlayerLeave(s *Scene, p *Player) {
s.NotifyPrivateRoom(common.ListModify)
}
// 系统维护关闭事件
func (spd *ScenePolicyData) OnShutdown(s *Scene) {
}
func (spd *ScenePolicyData) OnSceneState(s *Scene, state int) {
s.SceneState = int32(state)
switch state {
case common.SceneStateWaite:
s.NotifyPrivateRoom(common.ListModify)
case common.SceneStateStart:
s.NotifyPrivateRoom(common.ListModify)
if s.IsCustom() {
if s.GetCostType() == 1 {
for _, v := range s.players {
spd.CostPayment(s, v)
}
}
}
case common.SceneStateEnd:
s.NotifyPrivateRoom(common.ListModify)
}
}
func (spd *ScenePolicyData) OnGameState(s *Scene, state int) {
}
// 能否进入
func (spd *ScenePolicyData) CanEnter(s *Scene, p *Player) int {
if !spd.EnterAfterStart {
if s.starting {
return int(hallproto.OpResultCode_Game_OPRC_GameStarting_Game)
}
}
return 0
}
func (spd *ScenePolicyData) costEnough(costType, playerNum int, roomConfig *webapi.RoomConfig, snid int32, f func(items []*model.Item)) bool {
isEnough := true
var items []*model.Item
if costType == 2 {
// 房主
for _, v := range roomConfig.GetCost() {
if item := BagMgrSingleton.GetItem(snid, v.GetItemId()); item == nil || item.ItemNum < v.GetItemNum() {
isEnough = false
break
} else {
items = append(items, &model.Item{
ItemId: v.GetItemId(),
ItemNum: v.GetItemNum(),
})
}
}
} else {
// AA
for _, v := range roomConfig.GetCost() {
n := int64(math.Ceil(float64(v.GetItemNum()) / float64(playerNum)))
if item := BagMgrSingleton.GetItem(snid, v.GetItemId()); item == nil || item.ItemNum < n {
isEnough = false
break
} else {
items = append(items, &model.Item{
ItemId: v.GetItemId(),
ItemNum: n,
})
}
}
}
if isEnough {
f(items)
}
return isEnough
}
func (spd *ScenePolicyData) CostEnough(costType, playerNum int, roomConfig *webapi.RoomConfig, p *Player) bool {
if roomConfig == nil {
return false
}
return spd.costEnough(costType, playerNum, roomConfig, p.SnId, func(items []*model.Item) {})
}
func (spd *ScenePolicyData) CostPayment(s *Scene, p *Player) bool {
roomConfig := PlatformMgrSingleton.GetConfig(p.Platform).RoomConfig[s.GetRoomConfigId()]
if roomConfig == nil {
return false
}
return spd.costEnough(int(s.GetCostType()), s.playerNum, roomConfig, p.SnId, func(items []*model.Item) {
for _, v := range items {
v.ItemNum = -v.ItemNum
}
BagMgrSingleton.AddItemsV2(&model.AddItemParam{
P: p.PlayerData,
Change: items,
GainWay: common.GainWayRoomCost,
Operator: "system",
Remark: "竞技馆进房费用",
GameId: int64(s.gameId),
GameFreeId: int64(s.dbGameFree.GetId()),
RoomConfigId: roomConfig.GetId(),
})
})
}
// GiveCostPayment 退房费
func (spd *ScenePolicyData) GiveCostPayment(s *Scene, snid int32) bool {
roomConfig := PlatformMgrSingleton.GetConfig(s.limitPlatform.IdStr).RoomConfig[s.GetRoomConfigId()]
if roomConfig == nil {
return false
}
if s.GetCostType() != 2 { // 只有房主付费才有返还
return false
}
var items []*model.Item
for _, v := range roomConfig.GetCost() {
items = append(items, &model.Item{
ItemId: v.GetItemId(),
ItemNum: v.GetItemNum(),
})
}
p := PlayerMgrSington.GetPlayerBySnId(snid)
if p != nil {
BagMgrSingleton.AddItemsV2(&model.AddItemParam{
P: p.PlayerData,
Change: items,
GainWay: common.GainWayRoomCost,
Operator: "system",
Remark: "竞技场房间费用返还",
GameId: int64(s.gameId),
GameFreeId: int64(s.dbGameFree.GetId()),
NoLog: false,
RoomConfigId: roomConfig.GetId(),
})
} else {
BagMgrSingleton.AddItemsOffline(s.limitPlatform.IdStr, snid, items, common.GainWayRoomCost, "system",
"竞技场费用返还", int64(s.gameId), int64(s.dbGameFree.GetId()), false, func(err error) {
logger.Logger.Errorf("竞技场房间费用返还失败, err: %v", err)
})
}
return false
}
func (spd *ScenePolicyData) GetBetState() int32 {
return spd.BetState
}
func (spd *ScenePolicyData) GetPlayerNum() int {
return int(spd.DefaultPlayerCnt)
}
func (spd *ScenePolicyData) GetBaseScore() int {
return int(spd.BaseScore)
}