151 lines
4.3 KiB
Go
151 lines
4.3 KiB
Go
package transact
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
|
|
"mongo.games.com/goserver/core/logger"
|
|
"mongo.games.com/goserver/core/netlib"
|
|
"mongo.games.com/goserver/core/transact"
|
|
|
|
"mongo.games.com/game/common"
|
|
"mongo.games.com/game/gamesrv/base"
|
|
"mongo.games.com/game/gamesrv/tienlen"
|
|
"mongo.games.com/game/proto"
|
|
webapiproto "mongo.games.com/game/protocol/webapi"
|
|
)
|
|
|
|
const __REQIP__ = "__REQIP__"
|
|
|
|
var (
|
|
WebAPIErrParam = errors.New("param err")
|
|
WebAPIErrNoPlayer = errors.New("player no find")
|
|
)
|
|
|
|
func init() {
|
|
transact.RegisteHandler(common.TransTypeGameSrvWebApi, &WebAPITranscateHandler{})
|
|
}
|
|
|
|
var WebAPIHandlerMgrSingleton = &WebAPIHandlerMgr{wshMap: make(map[string]WebAPIHandler)}
|
|
|
|
type WebAPITranscateHandler struct {
|
|
}
|
|
|
|
func (this *WebAPITranscateHandler) OnExcute(tNode *transact.TransNode, ud interface{}) transact.TransExeResult {
|
|
logger.Logger.Trace("WebAPITranscateHandler.OnExcute ")
|
|
req := &common.M2GWebApiRequest{}
|
|
err := netlib.UnmarshalPacketNoPackId(ud.([]byte), req)
|
|
if err == nil {
|
|
wsh := WebAPIHandlerMgrSingleton.GetWebAPIHandler(req.Path)
|
|
if wsh == nil {
|
|
logger.Logger.Error("WebAPITranscateHandler no registe WebAPIHandler ", req.Path)
|
|
return transact.TransExeResult_Failed
|
|
}
|
|
tag, msg := wsh.Handler(tNode, req.Body)
|
|
tNode.TransRep.RetFiels = msg
|
|
switch tag {
|
|
case common.ResponseTag_Ok:
|
|
return transact.TransExeResult_Success
|
|
case common.ResponseTag_TransactYield:
|
|
return transact.TransExeResult_Yield
|
|
}
|
|
}
|
|
logger.Logger.Error("WebAPITranscateHandler.OnExcute err:", err.Error())
|
|
return transact.TransExeResult_Failed
|
|
}
|
|
|
|
func (this *WebAPITranscateHandler) OnCommit(tNode *transact.TransNode) transact.TransExeResult {
|
|
logger.Logger.Trace("WebAPITranscateHandler.OnCommit ")
|
|
return transact.TransExeResult_Success
|
|
}
|
|
|
|
func (this *WebAPITranscateHandler) OnRollBack(tNode *transact.TransNode) transact.TransExeResult {
|
|
logger.Logger.Trace("WebAPITranscateHandler.OnRollBack ")
|
|
return transact.TransExeResult_Success
|
|
}
|
|
|
|
func (this *WebAPITranscateHandler) OnChildTransRep(tNode *transact.TransNode, hChild transact.TransNodeID, retCode int,
|
|
ud interface{}) transact.TransExeResult {
|
|
logger.Logger.Trace("WebAPITranscateHandler.OnChildTransRep ")
|
|
return transact.TransExeResult_Success
|
|
}
|
|
|
|
type WebAPIHandler interface {
|
|
Handler(*transact.TransNode, []byte) (int, proto.Message)
|
|
}
|
|
|
|
type WebAPIHandlerWrapper func(*transact.TransNode, []byte) (int, proto.Message)
|
|
|
|
func (wshw WebAPIHandlerWrapper) Handler(tNode *transact.TransNode, params []byte) (int, proto.Message) {
|
|
return wshw(tNode, params)
|
|
}
|
|
|
|
type WebAPIHandlerMgr struct {
|
|
wshMap map[string]WebAPIHandler
|
|
DataWaitList sync.Map
|
|
}
|
|
|
|
func (this *WebAPIHandlerMgr) RegisteWebAPIHandler(name string, wsh WebAPIHandler) {
|
|
this.wshMap[name] = wsh
|
|
}
|
|
|
|
func (this *WebAPIHandlerMgr) GetWebAPIHandler(name string) WebAPIHandler {
|
|
if wsh, exist := this.wshMap[name]; exist {
|
|
return wsh
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func init() {
|
|
// 对局详情
|
|
WebAPIHandlerMgrSingleton.RegisteWebAPIHandler("/api/game/room_info", WebAPIHandlerWrapper(
|
|
func(tNode *transact.TransNode, params []byte) (int, proto.Message) {
|
|
pack := &webapiproto.SARoomInfo{}
|
|
msg := &webapiproto.ASRoomInfo{}
|
|
err := proto.Unmarshal(params, msg)
|
|
if err != nil {
|
|
pack.Tag = webapiproto.TagCode_FAILED
|
|
pack.Msg = "数据序列化失败"
|
|
return common.ResponseTag_Ok, pack
|
|
}
|
|
pack.Tag = webapiproto.TagCode_SUCCESS
|
|
|
|
scene := base.SceneMgrSington.GetScene(int(msg.GetRoomId()))
|
|
if scene == nil || scene.ExtraData == nil {
|
|
pack.Tag = webapiproto.TagCode_NotFound
|
|
pack.Msg = "房间没找到"
|
|
return common.ResponseTag_Ok, pack
|
|
}
|
|
|
|
switch d := scene.ExtraData.(type) {
|
|
case *tienlen.TienLenSceneData:
|
|
for k := range d.BilledList {
|
|
pack.SnId = append(pack.SnId, k)
|
|
}
|
|
for k, v := range d.RoundLogId {
|
|
var score []int64
|
|
for _, vv := range pack.SnId {
|
|
list := d.BilledList[vv]
|
|
if list == nil || len(*list) <= k {
|
|
score = append(score, 0)
|
|
continue
|
|
}
|
|
score = append(score, (*list)[k].ChangeScore)
|
|
}
|
|
item := &webapiproto.RoundInfo{
|
|
Round: int32(k + 1),
|
|
Ts: d.RoundEndTime[k],
|
|
Score: score,
|
|
LogId: v,
|
|
}
|
|
pack.List = append(pack.List, item)
|
|
}
|
|
return common.ResponseTag_Ok, pack
|
|
default:
|
|
pack.Tag = webapiproto.TagCode_FAILED
|
|
pack.Msg = "未实现"
|
|
}
|
|
return common.ResponseTag_Ok, pack
|
|
}))
|
|
}
|