gm.go
1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package service
import (
"github.com/gin-gonic/gin"
"net/http"
"pro2d/cmd/gameserver/action"
"pro2d/common/components"
"pro2d/models"
"reflect"
)
type GmServer struct {
components.IHttp
Server components.IServer
}
func NewGmServer(server components.IServer, port ...string) *GmServer {
return &GmServer{
IHttp: components.NewHttpServer("", port...),
Server: server,
}
}
func (s *GmServer) HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc {
return func(c *gin.Context) {
c.Request.ParseForm()
roleId, ok := c.GetPostForm("role_id")
if !ok {
c.JSON(http.StatusOK, gin.H{"code": -101, "message": "role not exist"})
return
}
conn := s.Server.GetConnManage().GetConnByRID(roleId)
var role *models.RoleModel
if conn != nil {
//在线
role = conn.(*Agent).Role
} else {
//离线
role = models.NewRole(roleId)
if err := role.Load(); err != nil {
c.JSON(http.StatusOK, gin.H{"code": -102, "message": "role not exist"})
return
}
role.LoadAll()
}
properties := make(map[string]interface{})
for k, v := range c.Request.PostForm {
properties[k] = v[0]
}
properties["_conn"] = conn
properties["_role"] = role
v := tvl.Call([]reflect.Value{obj, reflect.ValueOf(properties)})
role.SaveRoleData(0)
if len(v) != 1 {
c.JSON(http.StatusNotFound, gin.H{"code": -100, "message": "request param len is error"})
return
}
c.JSON(http.StatusOK, gin.H{"code": v[0].Interface()})
}
}
func (s *GmServer) Start() error {
s.SetHandlerFuncCallback(s.HandlerFuncObj)
s.BindHandler(&action.GmAction{})
//gin.SetMode(gin.ReleaseMode)
return s.IHttp.Start()
}