package user_ev import ( "hilo-user/_const" "hilo-user/_const/enum/user_e" "hilo-user/domain" "hilo-user/domain/event" "hilo-user/resource/mysql" ) //注册监听 var reportGameInfoEvent = new(event.Base) /** 注册事件 */ type ReportGameInfoEvent struct { UserId mysql.ID `json:"uid"` // 用户id,请求get_user_info 接口返回uid参数 ReportType user_e.ReportType `json:"report_type"` // 上报类型 user_start|user_settle GameStartObject *GameStartObject `json:"user_start_object"` // user_start对应结构体 GameSettleObject *GameSettleObject `json:"user_settle_object"` // user_settle对应结构体 } type GameStartObject struct { MgId uint64 `json:"mg_id"` // 游戏id MgIdStr string `json:"mg_id_str"` // 小游戏id数值型兼容字段(nodejs服务请使用当前字段) RoomId string `json:"room_id"` // 接入方房间id GameMode int32 `json:"user_mode"` // 游戏模式,设定游戏的一些功能(参与游戏的人数,出手时间,特定的玩法) GameRoundId string `json:"user_round_id"` // 本局游戏的id (重复上报,使用该字段去重) BattleStartAt int32 `json:"battle_start_at"` // 战斗开始时间(秒) Players []PlayerObject `json:"players"` // player_object 数组 ReportGameInfoExtras string `json:"report_user_info_extras"` // 游戏上报信息扩展参数(透传),取值范围:长度不超过1024字节,超过则截断 ReportGameInfoKey string `json:"report_user_info_key"` // 游戏上报信息扩展参数(透传),取值范围:长度不超过64字节,超过则截断。接入方服务端可以根据这个字段来查询一局游戏的数据 } type PlayerObject struct { Uid string `json:"uid"` // 接入方uid,机器人为空字符 IsAi int32 `json:"is_ai"` // 0:普通用户,1:机器人 } type GameSettleObject struct { MgId uint64 `json:"mg_id"` // 游戏id MgIdStr string `json:"mg_id_str"` // 小游戏id数值型兼容字段(nodejs服务请使用当前字段) RoomId string `json:"room_id"` // 接入方房间id GameMode int32 `json:"user_mode"` // 游戏模式 GameRoundId string `json:"user_round_id"` // 本局游戏的id (重复上报,使用该字段去重) BattleStartAt uint32 `json:"battle_start_at"` // 战斗开始时间(秒) BattleEndAt uint32 `json:"battle_end_at"` // 战斗结束时间(秒) BattleDuration int32 `json:"battle_duration"` // 战斗总时间(秒) Results []PlayerResultObject `json:"results"` // player_result_object 数组 ReportGameInfoExtras string `json:"report_user_info_extras"` // 游戏上报信息扩展参数(透传),取值范围:长度不超过1024字节,超过则截断 ReportGameInfoKey string `json:"report_user_info_key"` // 游戏上报信息扩展参数(透传),取值范围:长度不超过64字节,超过则截断。接入方服务端可以根据这个字段来查询一局游戏的数据 } type PlayerResultObject struct { Uid string `json:"uid"` // 接入方uid,机器人为空字符 Rank int32 `json:"rank"` // 排名从1开始,平局排名相同 IsEscaped int32 `json:"is_escaped"` // 0:正常,1:逃跑 IsAi int32 `json:"is_ai"` // 0:普通用户,1:机器人 Role int32 `json:"role"` // 0:表示没有角色信息,玩家在游戏中的角色 游戏role 说明 Score int32 `json:"score"` // 玩家当前局得到的分数 IsWin int32 `json:"is_win"` // 结果 0:表示没有信息,1:输,2:赢,3:平局 Award int32 `json:"award"` // 奖励 Extras string `json:"extras"` // 扩展参数扩展说明 IsManaged int32 `json:"is_managed"` // 是否托管 0:未托管 1:托管 Diamond int64 `json:"diamond"` // 钻石收益,有可能负数,后期计算 LudoExtras *LudoExtras } type LudoExtras struct { Color int `json:"color"` Steps int `json:"steps"` } //添加领域事件,在每个领域模型中init中添加,因为这是静态业务,非动态的。 func AddReportGameInfoEventSync(callback func(model *domain.Model, event interface{}) error) { reportGameInfoEvent.SyncList = append(reportGameInfoEvent.SyncList, callback) } //加入到异步操作中 func AddReportGameInfoEventAsync(callback func(model *domain.Model, event interface{}) error) { reportGameInfoEvent.AsyncList = append(reportGameInfoEvent.AsyncList, callback) } //领域事件发布 func PublishReportGameInfoEvent(model *domain.Model, event interface{}) error { //执行同步的领域事件 for _, callback := range reportGameInfoEvent.SyncList { if err := callback(model, event); err != nil { return err } } // 执行异步的领域事件 if len(reportGameInfoEvent.AsyncList) > 0 { go func() { defer _const.CheckGoPanic() for _, callback := range reportGameInfoEvent.AsyncList { // 异步事件需要用新model,主要是db var newModel = domain.CreateModelContext(model.MyContext) if err := callback(newModel, event); err != nil { model.Log.Errorf("ReportGameInfoEvent aysnc fail:%v", err) } } }() } return nil }