Commit eadc9aff4df073fc1146adae03fcff79e734059f

Authored by zhangqijia
1 parent d500811b

feat: 增加上阵下阵协议,增加 IAgent接口,

删除一些无用日志
逻辑接口参数增加IAgent接口
增加IAgent接口的目的是为了在逻辑函数中可以获得和设置role数据
cmd/gameserver/action/RoleAction.go
... ... @@ -11,14 +11,14 @@ import (
11 11 "pro2d/pb"
12 12 )
13 13  
14   -func HeartRpc(msg components.IMessage) (int32, interface{}) {
  14 +func HeartRpc(agent components.IAgent, msg components.IMessage) (int32, interface{}) {
15 15 //msg.Conn.SetLastHeartCheckTime()
16 16 return 0, nil
17 17 }
18 18  
19 19 func getRandomName() string {
20 20 name := ""
21   - for {
  21 + for {
22 22 name = common.RandomName(common.DefaultName)
23 23 if name == "" {
24 24 continue
... ... @@ -35,7 +35,7 @@ func getRandomName() string {
35 35 return name
36 36 }
37 37  
38   -func CreateRpc(msg components.IMessage) (int32, interface{}) {
  38 +func CreateRpc(agent components.IAgent, msg components.IMessage) (int32, interface{}) {
39 39 req := pb.CreateReq{}
40 40 if err := proto.Unmarshal(msg.GetData(), &req); err != nil {
41 41 logger.Error("CreateRpc err: %v", err)
... ... @@ -57,7 +57,7 @@ func CreateRpc(msg components.IMessage) (int32, interface{}) {
57 57 return 0, nil
58 58 }
59 59  
60   -func LoginRpc(msg components.IMessage) (int32, interface{}) {
  60 +func LoginRpc(agent components.IAgent, msg components.IMessage) (int32, interface{}) {
61 61 //logger.Debug("11111111cmd: %v, msg: %s", msg.GetHeader().GetMsgID(), msg.GetData())
62 62 req := pb.LoginReq{}
63 63 if err := proto.Unmarshal(msg.GetData(), &req); err != nil {
... ... @@ -70,6 +70,27 @@ func LoginRpc(msg components.IMessage) (int32, interface{}) {
70 70 return 2, nil
71 71 }
72 72 role.SetProperty("Device", req.Device)
  73 + protoMsg := &pb.RoleRsp{
  74 + Role: role.Role,
  75 + Hero: role.GetAllHero(),
  76 + Team: role.GetAllTeam(),
  77 + }
  78 + //登录成功,存储agent role
  79 + agent.SetSchema(role)
  80 + return 0, protoMsg
  81 +}
73 82  
74   - return 0, role
  83 +func GoIntoBattleRpc(agent components.IAgent, msg components.IMessage) (int32, interface{}) {
  84 + req := pb.GoIntoBattleReq{}
  85 + if err := proto.Unmarshal(msg.GetData(), &req); err != nil {
  86 + logger.Error("loginRpc err: %v", err)
  87 + return 1, nil
  88 + }
  89 + role := agent.GetSchema().(*models.RoleModel)
  90 + if role == nil {
  91 + return 2, nil
  92 + }
  93 +
  94 + role.UpdateTeam(req.Team)
  95 + return 0, nil
75 96 }
... ...
cmd/gameserver/action/protocode.go
... ... @@ -11,6 +11,7 @@ func GetActionMap() map[interface{}]interface{} {
11 11 am[uint32(pb.ProtoCode_HeartReq)] = HeartRpc
12 12 am[uint32(pb.ProtoCode_LoginReq)] = LoginRpc
13 13 am[uint32(pb.ProtoCode_CreateReq)] = CreateRpc
  14 + am[uint32(pb.ProtoCode_GoIntoBattleReq)] = GoIntoBattleRpc
14 15  
15 16 return am
16   -}
17 17 \ No newline at end of file
  18 +}
... ...
cmd/gameserver/main.go
... ... @@ -5,12 +5,13 @@ import (
5 5 "net/http"
6 6 "os"
7 7 "os/signal"
  8 + "pro2d/cmd/gameserver/service"
8 9 "pro2d/common"
9 10 "pro2d/common/logger"
10 11 "syscall"
11 12 )
12 13  
13   -func main() {
  14 +func main() {
14 15 err := make(chan error)
15 16 stopChan := make(chan os.Signal)
16 17 signal.Notify(stopChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
... ... @@ -18,7 +19,7 @@ func main() {
18 19 userChan := make(chan os.Signal)
19 20 signal.Notify(userChan, syscall.SIGUSR1, syscall.SIGUSR2)
20 21  
21   - s,err1 := NewGameServer(common.GlobalConf.GameConf)
  22 + s, err1 := service.NewGameServer(common.GlobalConf.GameConf)
22 23 if err1 != nil {
23 24 logger.Error(err1)
24 25 return
... ... @@ -33,7 +34,7 @@ func main() {
33 34  
34 35 for {
35 36 select {
36   - case e := <- err:
  37 + case e := <-err:
37 38 logger.Error("game server error: %v", e)
38 39 return
39 40 case <-stopChan:
... ... @@ -41,7 +42,7 @@ func main() {
41 42 s.Stop()
42 43 return
43 44 case u := <-userChan:
44   - logger.Debug("userChan .. %v",u.String())
  45 + logger.Debug("userChan .. %v", u.String())
45 46 e := s.IServer.GetPlugin().LoadPlugin()
46 47 if e != nil {
47 48 logger.Error("err: ", e.Error())
... ...
cmd/gameserver/agent.go renamed to cmd/gameserver/service/agent.go
1   -package main
  1 +package service
2 2  
3 3 import (
4 4 "github.com/golang/protobuf/proto"
5 5 "math"
6   - "pro2d/cmd/gameserver/action"
7 6 "pro2d/common"
8 7 "pro2d/common/components"
9 8 "pro2d/common/logger"
... ... @@ -16,58 +15,49 @@ import (
16 15 type Agent struct {
17 16 components.IConnection
18 17 Server components.IServer
  18 + components.IAgent
19 19  
20   - Role *models.RoleModel
21   - nextCheckTime int64 //下一次检查的时间
22   - lastHeartCheckTime int64
23   - heartTimeoutCount int //超时次数
  20 + Role *models.RoleModel
  21 + nextCheckTime int64 //下一次检查的时间
  22 + lastHeartCheckTime int64
  23 + heartTimeoutCount int //超时次数
24 24 }
25 25  
26   -var agentPool = sync.Pool{New: func() interface{} { return new(Agent)}}
  26 +var agentPool = sync.Pool{New: func() interface{} { return new(Agent) }}
27 27  
28 28 func NewAgent(s components.IServer) *Agent {
29 29 a := agentPool.Get().(*Agent)
30 30 a.Server = s
31 31  
32   - a.nextCheckTime = 0
  32 + a.nextCheckTime = 0
33 33 a.lastHeartCheckTime = common.Timex()
34   - a.heartTimeoutCount= 0
  34 + a.heartTimeoutCount = 0
35 35 return a
36 36 }
37 37  
38   -func (c *Agent) OnConnection(conn components.IConnection) {
39   - c.IConnection = conn
  38 +func (c *Agent) SetSchema(schema components.ISchema) {
  39 + c.Role = schema.(*models.RoleModel)
40 40 }
41 41  
42   -func (c *Agent) OnLogin(msg components.IMessage) {
43   - //第一次登录
44   - errCode, irole := action.LoginRpc(msg)
45   - if errCode != 0 {
46   - c.Send(errCode, msg.GetHeader().GetMsgID(), nil)
47   - return
48   - }
  42 +func (c *Agent) GetSchema() components.ISchema {
  43 + return c.Role
  44 +}
49 45  
50   - role := irole.(*models.RoleModel)
51   - protoMsg := &pb.RoleRsp{
52   - Role: role.Role,
53   - Hero: role.GetAllHero(),
54   - Team: role.GetAllTeam(),
55   - }
56   - rsp, err := proto.Marshal(protoMsg)
57   - if err != nil {
58   - c.Send(-100, msg.GetHeader().GetMsgID(), nil)
59   - return
60   - }
61   - c.Send(errCode, msg.GetHeader().GetMsgID(), rsp)
62   - //登录成功,存储agent role
63   - c.Role = role
  46 +func (c *Agent) SetServer(server components.IServer) {
  47 + c.Server = server
64 48 }
65 49  
66   -func (c *Agent) OnMessage(msg components.IMessage) {
67   - atomic.StoreInt64(&c.lastHeartCheckTime, common.Timex())
  50 +func (c *Agent) GetServer() components.IServer {
  51 + return c.Server
  52 +}
68 53  
69   - if msg.GetHeader().GetMsgID() == uint32(pb.ProtoCode_LoginReq) {
70   - c.OnLogin(msg)
  54 +func (c *Agent) OnConnection(conn components.IConnection) {
  55 + c.IConnection = conn
  56 +}
  57 +
  58 +func (c *Agent) OnMessage(msg components.IMessage) {
  59 + atomic.StoreInt64(&c.lastHeartCheckTime, common.Timex())
  60 + if msg.GetHeader().GetMsgID() == uint32(pb.ProtoCode_HeartReq) {
71 61 return
72 62 }
73 63  
... ... @@ -79,8 +69,8 @@ func (c *Agent) OnMessage(msg components.IMessage) {
79 69 logger.Debug("protocolID: %d", msg.GetHeader().GetMsgID())
80 70 //fmt.Printf("errCode: %d, protoMsg:%v\n", errCode, protoMsg)
81 71  
82   - f := md.(func (msg components.IMessage) (int32, interface{}))
83   - errCode, protoMsg := f(msg)
  72 + f := md.(func(agent components.IAgent, msg components.IMessage) (int32, interface{}))
  73 + errCode, protoMsg := f(c, msg)
84 74  
85 75 if protoMsg == nil {
86 76 c.Send(errCode, msg.GetHeader().GetMsgID(), nil)
... ... @@ -95,7 +85,7 @@ func (c *Agent) OnMessage(msg components.IMessage) {
95 85 c.Send(errCode, msg.GetHeader().GetMsgID(), rsp)
96 86 }
97 87  
98   -func (c *Agent) OnTimer() {
  88 +func (c *Agent) OnTimer() {
99 89 nextCheckTime := atomic.LoadInt64(&c.nextCheckTime)
100 90 now := common.Timex()
101 91 if now >= nextCheckTime {
... ... @@ -111,11 +101,8 @@ func (c *Agent) OnTimer() {
111 101 }
112 102 }
113 103  
114   -func (c *Agent) OnClose() {
115   - c.Close()
116   -}
117   -
118   -func (c *Agent) Close() {
  104 +func (c *Agent) OnClose() {
  105 + c.IConnection = nil
119 106 c.Role = nil
120 107 agentPool.Put(c)
121 108  
... ... @@ -126,17 +113,17 @@ func (c *Agent) Close() {
126 113 c.Role.OnOfflineEvent()
127 114 }
128 115  
129   -func (c *Agent) checkHeartBeat(now int64) {
  116 +func (c *Agent) checkHeartBeat(now int64) {
130 117 lastHeartCheckTime := atomic.LoadInt64(&c.lastHeartCheckTime)
131 118 //logger.Debug("checkHeartBeat ID: %d, last: %d, now: %d", c.GetID(), lastHeartCheckTime, now)
132   - if math.Abs(float64(lastHeartCheckTime - now)) > common.HeartTimerInterval {
  119 + if math.Abs(float64(lastHeartCheckTime-now)) > common.HeartTimerInterval {
133 120 c.heartTimeoutCount++
134 121 if c.heartTimeoutCount >= common.HeartTimeoutCountMax {
135 122 c.Stop()
136 123 return
137 124 }
138 125 logger.Debug("timeout count: %d", c.heartTimeoutCount)
139   - }else {
  126 + } else {
140 127 c.heartTimeoutCount = 0
141 128 }
142 129 }
... ...
cmd/gameserver/game.go renamed to cmd/gameserver/service/game.go
1   -package main
  1 +package service
2 2  
3 3 import (
4 4 "fmt"
... ... @@ -88,7 +88,7 @@ func (s *GameServer) OnConnection(conn components.IConnection) {
88 88 }
89 89  
90 90 func (s *GameServer) OnMessage(msg components.IMessage) {
91   - agent := s.GetConnManage().GetConn(msg.GetSession().GetID())
  91 + agent := s.GetConnManage().GetConn(msg.GetSID())
92 92 if agent == nil {
93 93 return
94 94 }
... ...
common/components/conn.go
... ... @@ -15,7 +15,7 @@ type Connection struct {
15 15 IConnection
16 16 net.Conn
17 17 splitter ISplitter
18   - Id uint32
  18 + Id uint32
19 19  
20 20 scanner *bufio.Scanner
21 21 writer *bufio.Writer
... ... @@ -33,7 +33,7 @@ type Connection struct {
33 33 }
34 34  
35 35 var connectionPool = &sync.Pool{
36   - New: func() interface{} { return new(Connection)},
  36 + New: func() interface{} { return new(Connection) },
37 37 }
38 38  
39 39 func NewConn(id int, conn net.Conn, splitter ISplitter) IConnection {
... ... @@ -48,8 +48,8 @@ func NewConn(id int, conn net.Conn, splitter ISplitter) IConnection {
48 48 c.Conn = conn
49 49 c.splitter = splitter
50 50  
51   - c.scanner = bufio.NewScanner(conn)
52   - c.writer = bufio.NewWriter(conn)
  51 + c.scanner = bufio.NewScanner(conn)
  52 + c.writer = bufio.NewWriter(conn)
53 53  
54 54 c.reset()
55 55  
... ... @@ -57,11 +57,11 @@ func NewConn(id int, conn net.Conn, splitter ISplitter) IConnection {
57 57 }
58 58  
59 59 func (c *Connection) reset() {
60   - c.WBuffer = make(chan []byte, common.MaxMsgChan)
61   - c.Quit = make(chan *Connection)
  60 + c.WBuffer = make(chan []byte, common.MaxMsgChan)
  61 + c.Quit = make(chan *Connection)
62 62  
63 63 if c.readFunc == nil {
64   - c.readFunc = make(chan func(), 10)
  64 + c.readFunc = make(chan func(), 10)
65 65 }
66 66 if c.timerFunc == nil {
67 67 c.timerFunc = make(chan func(), 10)
... ... @@ -104,7 +104,9 @@ func (c *Connection) Start() {
104 104 }
105 105  
106 106 func (c *Connection) Stop() {
107   - if atomic.LoadUint32(&c.Status) == 0 { return }
  107 + if atomic.LoadUint32(&c.Status) == 0 {
  108 + return
  109 + }
108 110  
109 111 sendTimeout := time.NewTimer(5 * time.Millisecond)
110 112 defer sendTimeout.Stop()
... ... @@ -148,7 +150,7 @@ func (c *Connection) defaultTimerCallback(conn IConnection) {
148 150  
149 151 func (c *Connection) write() {
150 152 defer func() {
151   - logger.Debug("write close")
  153 + //logger.Debug("write close")
152 154 c.Stop()
153 155 }()
154 156  
... ... @@ -167,7 +169,7 @@ func (c *Connection) write() {
167 169  
168 170 func (c *Connection) read() {
169 171 defer func() {
170   - logger.Debug("read close")
  172 + //logger.Debug("read close")
171 173 c.Stop()
172 174 }()
173 175  
... ... @@ -179,7 +181,7 @@ func (c *Connection) read() {
179 181 return
180 182 }
181 183  
182   - req.SetSession(c)
  184 + req.SetSID(c.GetID())
183 185 c.readFunc <- func() {
184 186 c.messageCallback(req)
185 187 }
... ... @@ -194,7 +196,7 @@ func (c *Connection) read() {
194 196 //此设计目的是为了让网络数据与定时器处理都在一条协程里处理。不想加锁。。。
195 197 func (c *Connection) listen() {
196 198 defer func() {
197   - logger.Debug("listen close")
  199 + //logger.Debug("listen close")
198 200 c.quitting()
199 201 }()
200 202  
... ... @@ -211,7 +213,9 @@ func (c *Connection) listen() {
211 213 }
212 214  
213 215 func (c *Connection) handleTimeOut() {
214   - if atomic.LoadUint32(&c.Status) == 0 { return }
  216 + if atomic.LoadUint32(&c.Status) == 0 {
  217 + return
  218 + }
215 219  
216 220 c.timerFunc <- func() {
217 221 c.timerCallback(c)
... ... @@ -220,7 +224,9 @@ func (c *Connection) handleTimeOut() {
220 224 }
221 225  
222 226 func (c *Connection) quitting() {
223   - if atomic.LoadUint32(&c.Status) == 0 { return }
  227 + if atomic.LoadUint32(&c.Status) == 0 {
  228 + return
  229 + }
224 230 atomic.StoreUint32(&c.Status, 0)
225 231  
226 232 logger.Debug("ID: %d close", c.Id)
... ...
common/components/connector.go
... ... @@ -23,20 +23,20 @@ func WithCtorCount(count int) ConnectorOption {
23 23 type Connector struct {
24 24 IConnector
25 25 IServer
26   - splitter ISplitter
27   - ip string
28   - port int
29   - sum int
  26 + splitter ISplitter
  27 + ip string
  28 + port int
  29 + sum int
30 30  
31 31 Conns IConnManage
32   - ids uint32
  32 + ids uint32
33 33 }
34 34  
35 35 func NewConnector(ip string, port int, options ...ConnectorOption) IConnector {
36 36 c := &Connector{
37   - ids: 0,
38   - ip: ip,
39   - port: port,
  37 + ids: 0,
  38 + ip: ip,
  39 + port: port,
40 40 Conns: NewConnManage(),
41 41 }
42 42 for _, option := range options {
... ... @@ -50,7 +50,7 @@ func (c *Connector) Connect() error {
50 50 c.sum = 1
51 51 }
52 52 for i := 0; i < c.sum; i++ {
53   - conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d",c.ip, c.port))
  53 + conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", c.ip, c.port))
54 54 if err != nil {
55 55 return err
56 56 }
... ... @@ -67,14 +67,14 @@ func (c *Connector) Connect() error {
67 67 return nil
68 68 }
69 69  
70   -func (c *Connector) DisConnect(){
  70 +func (c *Connector) DisConnect() {
71 71 c.Conns.StopAllConns()
72 72 }
73 73  
74   -func (c *Connector) Send( cmd uint32, b []byte) {
  74 +func (c *Connector) Send(cmd uint32, b []byte) {
75 75 c.Conns.Range(func(key interface{}, value interface{}) bool {
76 76 conn := value.(IConnection)
77   - conn.Send(0, cmd ,b)
  77 + conn.Send(0, cmd, b)
78 78 return true
79 79 })
80 80 }
... ... @@ -83,18 +83,18 @@ func (c *Connector) GetSplitter() ISplitter {
83 83 return c.splitter
84 84 }
85 85  
86   -func (c *Connector) OnConnect(conn IConnection){
  86 +func (c *Connector) OnConnect(conn IConnection) {
87 87 c.Conns.AddConn(conn.GetID(), conn)
88 88 }
89 89  
90   -func (c *Connector) OnMessage(msg IMessage){
91   - logger.Debug("recv msg cmd: %d, conn: %d data: %s", msg.GetHeader().GetMsgID(), msg.GetSession().GetID(), msg.GetData())
  90 +func (c *Connector) OnMessage(msg IMessage) {
  91 + logger.Debug("recv msg cmd: %d, conn: %d data: %s", msg.GetHeader().GetMsgID(), msg.GetSID(), msg.GetData())
92 92 }
93 93  
94   -func (c *Connector) OnClose(conn IConnection){
  94 +func (c *Connector) OnClose(conn IConnection) {
95 95 logger.Debug("onclose id: %d", conn.GetID())
96 96 }
97 97  
98   -func (c *Connector) OnTimer(conn IConnection){
  98 +func (c *Connector) OnTimer(conn IConnection) {
99 99 logger.Debug("ontimer id: %d", conn.GetID())
100   -}
101 100 \ No newline at end of file
  101 +}
... ...
common/components/icompontents.go
... ... @@ -21,8 +21,8 @@ type (
21 21 GetData() []byte //获取消息内容
22 22 SetData([]byte) //设置消息内容
23 23  
24   - SetSession(IConnection) //设置连接
25   - GetSession() IConnection //获取连接
  24 + SetSID(id uint32) //设置连接
  25 + GetSID() uint32 //获取连接
26 26 }
27 27 //网络拆包解包器
28 28 ISplitter interface {
... ... @@ -77,6 +77,13 @@ type (
77 77 SetCloseCallback(CloseCallback)
78 78 SetTimerCallback(TimerCallback)
79 79 }
  80 + IAgent interface {
  81 + GetSchema() ISchema
  82 + SetSchema(schema ISchema)
  83 +
  84 + GetServer() IServer
  85 + SetServer(server IServer)
  86 + }
80 87  
81 88 //Connector
82 89 IConnector interface {
... ... @@ -125,10 +132,10 @@ type (
125 132 GetPri() interface{}
126 133 GetSchema() interface{}
127 134 GetSchemaName() string
  135 + UpdateSchema(interface{})
128 136  
129 137 Load() error
130 138 Create() error
131   - Save() error
132 139 Update()
133 140  
134 141 SetProperty(key string, val interface{})
... ...
common/components/pbsplitter.go
... ... @@ -35,7 +35,7 @@ type PBMessage struct {
35 35 Head IHead
36 36 Body []byte
37 37  
38   - conn IConnection
  38 + ID uint32
39 39 }
40 40  
41 41 func (m *PBMessage) GetHeader() IHead {
... ... @@ -53,12 +53,12 @@ func (m *PBMessage) SetData(b []byte) {
53 53 m.Body = b
54 54 }
55 55  
56   -func (m *PBMessage) SetSession(connection IConnection) {
57   - m.conn = connection
  56 +func (m *PBMessage) SetSID(id uint32) {
  57 + m.ID = id
58 58 }
59 59  
60   -func (m *PBMessage) GetSession() IConnection {
61   - return m.conn
  60 +func (m *PBMessage) GetSID() uint32 {
  61 + return m.ID
62 62 }
63 63  
64 64 type PBSplitter struct {
... ...
common/db/mongoproxy/mongo.go
... ... @@ -13,7 +13,7 @@ import (
13 13 )
14 14  
15 15 var (
16   - mongoClient *mongo.Client
  16 + mongoClient *mongo.Client
17 17 mongoDatabase *mongo.Database
18 18 )
19 19  
... ... @@ -22,7 +22,7 @@ type MgoColl struct {
22 22 Schema components.ISchema
23 23  
24 24 dbname string
25   - coll *mongo.Collection
  25 + coll *mongo.Collection
26 26 }
27 27  
28 28 func NewMongoColl(dbname string, schema components.ISchema) *MgoColl {
... ... @@ -45,19 +45,19 @@ func (m *MgoColl) CreateTable() error {
45 45 return DB().CreateCollection(context.TODO(), m.dbname)
46 46 }
47 47  
48   -func (m *MgoColl) Create() (interface{}, error){
  48 +func (m *MgoColl) Create() (interface{}, error) {
49 49 return m.coll.InsertOne(context.TODO(), m.Schema.GetSchema())
50 50 }
51 51  
52   -func (m *MgoColl) Save() error{
53   - _, err := m.coll.UpdateOne(context.TODO(), m.Schema.GetPri(), m.Schema.GetSchema())
  52 +func (m *MgoColl) Save() error {
  53 + _, err := m.coll.UpdateOne(context.TODO(), m.Schema.GetPri(), bson.D{{"$set", m.Schema.GetSchema()}})
54 54 if err != nil {
55 55 return err
56 56 }
57 57 return nil
58 58 }
59 59  
60   -func (m *MgoColl) Load() error{
  60 +func (m *MgoColl) Load() error {
61 61 r := m.coll.FindOne(context.TODO(), m.Schema.GetPri())
62 62 err := r.Decode(m.Schema.GetSchema())
63 63 if err != nil {
... ... @@ -72,7 +72,7 @@ func (m *MgoColl) FindOne() error {
72 72 return singleResult.Decode(m.Schema.GetSchema())
73 73 }
74 74  
75   -func (m *MgoColl) UpdateOne(filter interface{}, update interface{})*mongo.UpdateResult {
  75 +func (m *MgoColl) UpdateOne(filter interface{}, update interface{}) *mongo.UpdateResult {
76 76 res, err := m.coll.UpdateOne(context.TODO(), filter, bson.D{{"$set", update}})
77 77 if err != nil {
78 78 return nil
... ... @@ -86,23 +86,23 @@ func (m *MgoColl) UpdateProperty(key string, val interface{}) error {
86 86 }
87 87  
88 88 func (m *MgoColl) UpdateProperties(properties map[string]interface{}) error {
89   - _, err := m.coll.UpdateOne(context.TODO(), m.Schema.GetPri(), properties)
  89 + _, err := m.coll.UpdateOne(context.TODO(), m.Schema.GetPri(), bson.D{{"$set", properties}})
90 90 return err
91 91 }
92 92  
93 93 //索引
94   -func (m *MgoColl) SetUnique(key string) (string, error){
  94 +func (m *MgoColl) SetUnique(key string) (string, error) {
95 95 return m.coll.Indexes().CreateOne(
96 96 context.Background(),
97 97 mongo.IndexModel{
98   - Keys : bsonx.Doc{{key, bsonx.Int32(1)}},
  98 + Keys: bsonx.Doc{{key, bsonx.Int32(1)}},
99 99 Options: options.Index().SetUnique(true),
100 100 },
101 101 )
102 102 }
103 103  
104 104 func (m *MgoColl) Delete(key string, value interface{}) int64 {
105   - filter := bson.D{ {key, value}}
  105 + filter := bson.D{{key, value}}
106 106 count, err := m.coll.DeleteOne(context.TODO(), filter, nil)
107 107 if err != nil {
108 108 fmt.Println(err)
... ...
conf/conf.yaml
... ... @@ -35,7 +35,7 @@ server_game:
35 35 id: "2"
36 36 name: "game"
37 37 ip: "192.168.0.206"
38   - encipher: true
  38 + encipher: false
39 39 port: 8850
40 40 pool_size: 1
41 41 debugport: 6061
... ...
models/role.go
... ... @@ -103,7 +103,7 @@ func (m *RoleModel) InitRole() {
103 103  
104 104 func (m *RoleModel) LoadHero() {
105 105 heros := make([]*pb.Hero, 10)
106   - err := mongoproxy.FindMany("hero", "role_id", m.Role.Id, &heros)
  106 + err := mongoproxy.FindMany("hero", "roleid", m.Role.Id, &heros)
107 107 if err != nil {
108 108 logger.Error(err)
109 109 return
... ... @@ -115,7 +115,7 @@ func (m *RoleModel) LoadHero() {
115 115  
116 116 func (m *RoleModel) LoadTeams() {
117 117 teams := make([]*pb.Team, 4)
118   - err := mongoproxy.FindMany("team", "role_id", m.Role.Id, &teams)
  118 + err := mongoproxy.FindMany("team", "roleid", m.Role.Id, &teams)
119 119 if err != nil {
120 120 logger.Error(err)
121 121 return
... ... @@ -188,6 +188,16 @@ func (m *RoleModel) AddTeam(team *pb.Team) {
188 188 m.Teams[team.Id] = t
189 189 }
190 190  
  191 +func (m *RoleModel) UpdateTeam(teams []*pb.Team) {
  192 + for _, team := range teams {
  193 + team.RoleId = m.Role.Id
  194 + t := m.Teams[team.Id]
  195 + if t != nil {
  196 + t.UpdateSchema(team)
  197 + }
  198 + }
  199 +}
  200 +
191 201 func (m *RoleModel) OnRecoverTimer(now int64) {
192 202 m.saveRoleData(now)
193 203 }
... ...
models/role_test.go
... ... @@ -82,3 +82,23 @@ func TestRoleModel_ProtoReflect(t *testing.T) {
82 82 sch.UpdateProperty(nil, "Device", "123123123", false)
83 83 fmt.Println(sch.Role)
84 84 }
  85 +func TestRoleModel_UpdateTeam(t *testing.T) {
  86 + err := mongoproxy.ConnectMongo(common.GlobalConf.GameConf.MongoConf)
  87 + if err != nil {
  88 + logger.Error(err)
  89 + return
  90 + }
  91 + sch := NewRole("1")
  92 + sch.Load()
  93 + sch.LoadAll()
  94 + sch.UpdateTeam([]*pb.Team{
  95 + &pb.Team{
  96 + Id: "147262174025748480",
  97 + RoleId: "",
  98 + HeroId1: "",
  99 + HeroId2: "",
  100 + HeroId3: "",
  101 + },
  102 + })
  103 + sch.OnOfflineEvent()
  104 +}
... ...
models/schema.go
... ... @@ -43,7 +43,11 @@ func NewSchema(key string, schema interface{}) *Schema {
43 43 }
44 44  
45 45 for i := 0; i < s.Type().NumField(); i++ {
46   - sch.reflectIndex[strings.ToLower(s.Type().Field(i).Name)] = i
  46 + name := s.Type().Field(i).Name
  47 + if strings.Compare(name[0:1], strings.ToLower(name[0:1])) == 0 {
  48 + continue
  49 + }
  50 + sch.reflectIndex[strings.ToLower(strings.ToLower(name))] = i
47 51 }
48 52  
49 53 sch.db = mongoproxy.NewMongoColl(sch.GetSchemaName(), sch)
... ... @@ -95,6 +99,20 @@ func (s *Schema) GetSchemaName() string {
95 99 return strings.ToLower(s.reflectValue.Type().Name())
96 100 }
97 101  
  102 +func (s *Schema) UpdateSchema(schema interface{}) {
  103 + sch := reflect.ValueOf(schema)
  104 + if sch.Kind() == reflect.Ptr {
  105 + sch = reflect.ValueOf(schema).Elem()
  106 + }
  107 + for i := 0; i < sch.Type().NumField(); i++ {
  108 + name := sch.Type().Field(i).Name
  109 + if _, ok := s.reflectIndex[strings.ToLower(name)]; !ok {
  110 + continue
  111 + }
  112 + s.SetProperty(sch.Type().Field(i).Name, sch.Field(i).Interface())
  113 + }
  114 +}
  115 +
98 116 func (s *Schema) Load() error {
99 117 return s.db.Load()
100 118 }
... ... @@ -104,17 +122,15 @@ func (s *Schema) Create() error {
104 122 return err
105 123 }
106 124  
107   -func (s *Schema) Save() error {
108   - return s.db.Save()
109   -}
110   -
  125 +//更新缓存字段到数据库
111 126 func (s *Schema) Update() {
112   - if s.cacheFields != nil {
  127 + if len(s.cacheFields) > 0 {
113 128 s.db.UpdateProperties(s.cacheFields)
114 129 s.cacheFields = make(map[string]interface{})
115 130 }
116 131 }
117 132  
  133 +//更新内存,并把字段缓存
118 134 func (s *Schema) SetProperty(key string, val interface{}) {
119 135 idx, ok := s.reflectIndex[strings.ToLower(key)]
120 136 if !ok {
... ... @@ -124,6 +140,7 @@ func (s *Schema) SetProperty(key string, val interface{}) {
124 140 s.cacheFields[strings.ToLower(key)] = val
125 141 }
126 142  
  143 +//更新内存,并把字段缓存
127 144 func (s *Schema) SetProperties(properties map[string]interface{}) {
128 145 for key, val := range properties {
129 146 idx, ok := s.reflectIndex[strings.ToLower(key)]
... ...
pb/game.pb.go
... ... @@ -343,6 +343,53 @@ func (x *UpdateRolePropertyRsp) GetRole() *Role {
343 343 return nil
344 344 }
345 345  
  346 +type GoIntoBattleReq struct {
  347 + state protoimpl.MessageState
  348 + sizeCache protoimpl.SizeCache
  349 + unknownFields protoimpl.UnknownFields
  350 +
  351 + Team []*Team `protobuf:"bytes,1,rep,name=team,proto3" json:"team,omitempty"`
  352 +}
  353 +
  354 +func (x *GoIntoBattleReq) Reset() {
  355 + *x = GoIntoBattleReq{}
  356 + if protoimpl.UnsafeEnabled {
  357 + mi := &file_game_proto_msgTypes[6]
  358 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  359 + ms.StoreMessageInfo(mi)
  360 + }
  361 +}
  362 +
  363 +func (x *GoIntoBattleReq) String() string {
  364 + return protoimpl.X.MessageStringOf(x)
  365 +}
  366 +
  367 +func (*GoIntoBattleReq) ProtoMessage() {}
  368 +
  369 +func (x *GoIntoBattleReq) ProtoReflect() protoreflect.Message {
  370 + mi := &file_game_proto_msgTypes[6]
  371 + if protoimpl.UnsafeEnabled && x != nil {
  372 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  373 + if ms.LoadMessageInfo() == nil {
  374 + ms.StoreMessageInfo(mi)
  375 + }
  376 + return ms
  377 + }
  378 + return mi.MessageOf(x)
  379 +}
  380 +
  381 +// Deprecated: Use GoIntoBattleReq.ProtoReflect.Descriptor instead.
  382 +func (*GoIntoBattleReq) Descriptor() ([]byte, []int) {
  383 + return file_game_proto_rawDescGZIP(), []int{6}
  384 +}
  385 +
  386 +func (x *GoIntoBattleReq) GetTeam() []*Team {
  387 + if x != nil {
  388 + return x.Team
  389 + }
  390 + return nil
  391 +}
  392 +
346 393 var File_game_proto protoreflect.FileDescriptor
347 394  
348 395 var file_game_proto_rawDesc = []byte{
... ... @@ -371,8 +418,11 @@ var file_game_proto_rawDesc = []byte{
371 418 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x03, 0x28, 0x05, 0x52, 0x02, 0x69, 0x64, 0x12,
372 419 0x20, 0x0a, 0x04, 0x72, 0x6f, 0x6c, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e,
373 420 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x73, 0x2e, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x04, 0x72, 0x6f, 0x6c,
374   - 0x65, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70,
375   - 0x72, 0x6f, 0x74, 0x6f, 0x33,
  421 + 0x65, 0x22, 0x33, 0x0a, 0x0f, 0x47, 0x6f, 0x49, 0x6e, 0x74, 0x6f, 0x42, 0x61, 0x74, 0x74, 0x6c,
  422 + 0x65, 0x52, 0x65, 0x71, 0x12, 0x20, 0x0a, 0x04, 0x74, 0x65, 0x61, 0x6d, 0x18, 0x01, 0x20, 0x03,
  423 + 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x73, 0x2e, 0x54, 0x65, 0x61, 0x6d,
  424 + 0x52, 0x04, 0x74, 0x65, 0x61, 0x6d, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b,
  425 + 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
376 426 }
377 427  
378 428 var (
... ... @@ -387,7 +437,7 @@ func file_game_proto_rawDescGZIP() []byte {
387 437 return file_game_proto_rawDescData
388 438 }
389 439  
390   -var file_game_proto_msgTypes = make([]protoimpl.MessageInfo, 6)
  440 +var file_game_proto_msgTypes = make([]protoimpl.MessageInfo, 7)
391 441 var file_game_proto_goTypes = []interface{}{
392 442 (*HeartReq)(nil), // 0: game.HeartReq
393 443 (*HeartRsp)(nil), // 1: game.HeartRsp
... ... @@ -395,20 +445,22 @@ var file_game_proto_goTypes = []interface{}{
395 445 (*CreateReq)(nil), // 3: game.CreateReq
396 446 (*RoleRsp)(nil), // 4: game.RoleRsp
397 447 (*UpdateRolePropertyRsp)(nil), // 5: game.UpdateRolePropertyRsp
398   - (*Role)(nil), // 6: models.Role
399   - (*Hero)(nil), // 7: models.Hero
400   - (*Team)(nil), // 8: models.Team
  448 + (*GoIntoBattleReq)(nil), // 6: game.GoIntoBattleReq
  449 + (*Role)(nil), // 7: models.Role
  450 + (*Hero)(nil), // 8: models.Hero
  451 + (*Team)(nil), // 9: models.Team
401 452 }
402 453 var file_game_proto_depIdxs = []int32{
403   - 6, // 0: game.RoleRsp.role:type_name -> models.Role
404   - 7, // 1: game.RoleRsp.hero:type_name -> models.Hero
405   - 8, // 2: game.RoleRsp.team:type_name -> models.Team
406   - 6, // 3: game.UpdateRolePropertyRsp.role:type_name -> models.Role
407   - 4, // [4:4] is the sub-list for method output_type
408   - 4, // [4:4] is the sub-list for method input_type
409   - 4, // [4:4] is the sub-list for extension type_name
410   - 4, // [4:4] is the sub-list for extension extendee
411   - 0, // [0:4] is the sub-list for field type_name
  454 + 7, // 0: game.RoleRsp.role:type_name -> models.Role
  455 + 8, // 1: game.RoleRsp.hero:type_name -> models.Hero
  456 + 9, // 2: game.RoleRsp.team:type_name -> models.Team
  457 + 7, // 3: game.UpdateRolePropertyRsp.role:type_name -> models.Role
  458 + 9, // 4: game.GoIntoBattleReq.team:type_name -> models.Team
  459 + 5, // [5:5] is the sub-list for method output_type
  460 + 5, // [5:5] is the sub-list for method input_type
  461 + 5, // [5:5] is the sub-list for extension type_name
  462 + 5, // [5:5] is the sub-list for extension extendee
  463 + 0, // [0:5] is the sub-list for field type_name
412 464 }
413 465  
414 466 func init() { file_game_proto_init() }
... ... @@ -490,6 +542,18 @@ func file_game_proto_init() {
490 542 return nil
491 543 }
492 544 }
  545 + file_game_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} {
  546 + switch v := v.(*GoIntoBattleReq); i {
  547 + case 0:
  548 + return &v.state
  549 + case 1:
  550 + return &v.sizeCache
  551 + case 2:
  552 + return &v.unknownFields
  553 + default:
  554 + return nil
  555 + }
  556 + }
493 557 }
494 558 type x struct{}
495 559 out := protoimpl.TypeBuilder{
... ... @@ -497,7 +561,7 @@ func file_game_proto_init() {
497 561 GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
498 562 RawDescriptor: file_game_proto_rawDesc,
499 563 NumEnums: 0,
500   - NumMessages: 6,
  564 + NumMessages: 7,
501 565 NumExtensions: 0,
502 566 NumServices: 0,
503 567 },
... ...
pb/protocode.pb.go
... ... @@ -24,33 +24,39 @@ type ProtoCode int32
24 24  
25 25 const (
26 26 ProtoCode_UNKNOWN ProtoCode = 0
27   - ProtoCode_HeartReq ProtoCode = 1
28   - ProtoCode_HeartRsp ProtoCode = 2
29   - ProtoCode_LoginReq ProtoCode = 3
30   - ProtoCode_CreateReq ProtoCode = 4
31   - ProtoCode_RoleRsp ProtoCode = 5
32   - ProtoCode_UpdateRolePropertyRsp ProtoCode = 6
  27 + ProtoCode_LoginRsp ProtoCode = 1
  28 + ProtoCode_HeartReq ProtoCode = 2
  29 + ProtoCode_HeartRsp ProtoCode = 3
  30 + ProtoCode_LoginReq ProtoCode = 4
  31 + ProtoCode_CreateReq ProtoCode = 5
  32 + ProtoCode_RoleRsp ProtoCode = 6
  33 + ProtoCode_UpdateRolePropertyRsp ProtoCode = 7
  34 + ProtoCode_GoIntoBattleReq ProtoCode = 8
33 35 )
34 36  
35 37 // Enum value maps for ProtoCode.
36 38 var (
37 39 ProtoCode_name = map[int32]string{
38 40 0: "UNKNOWN",
39   - 1: "HeartReq",
40   - 2: "HeartRsp",
41   - 3: "LoginReq",
42   - 4: "CreateReq",
43   - 5: "RoleRsp",
44   - 6: "UpdateRolePropertyRsp",
  41 + 1: "LoginRsp",
  42 + 2: "HeartReq",
  43 + 3: "HeartRsp",
  44 + 4: "LoginReq",
  45 + 5: "CreateReq",
  46 + 6: "RoleRsp",
  47 + 7: "UpdateRolePropertyRsp",
  48 + 8: "GoIntoBattleReq",
45 49 }
46 50 ProtoCode_value = map[string]int32{
47 51 "UNKNOWN": 0,
48   - "HeartReq": 1,
49   - "HeartRsp": 2,
50   - "LoginReq": 3,
51   - "CreateReq": 4,
52   - "RoleRsp": 5,
53   - "UpdateRolePropertyRsp": 6,
  52 + "LoginRsp": 1,
  53 + "HeartReq": 2,
  54 + "HeartRsp": 3,
  55 + "LoginReq": 4,
  56 + "CreateReq": 5,
  57 + "RoleRsp": 6,
  58 + "UpdateRolePropertyRsp": 7,
  59 + "GoIntoBattleReq": 8,
54 60 }
55 61 )
56 62  
... ... @@ -85,16 +91,18 @@ var File_protocode_proto protoreflect.FileDescriptor
85 91  
86 92 var file_protocode_proto_rawDesc = []byte{
87 93 0x0a, 0x0f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x64, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74,
88   - 0x6f, 0x12, 0x09, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x64, 0x65, 0x2a, 0x79, 0x0a, 0x09,
89   - 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x4e, 0x4b,
90   - 0x4e, 0x4f, 0x57, 0x4e, 0x10, 0x00, 0x12, 0x0c, 0x0a, 0x08, 0x48, 0x65, 0x61, 0x72, 0x74, 0x52,
91   - 0x65, 0x71, 0x10, 0x01, 0x12, 0x0c, 0x0a, 0x08, 0x48, 0x65, 0x61, 0x72, 0x74, 0x52, 0x73, 0x70,
92   - 0x10, 0x02, 0x12, 0x0c, 0x0a, 0x08, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x10, 0x03,
93   - 0x12, 0x0d, 0x0a, 0x09, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x10, 0x04, 0x12,
94   - 0x0b, 0x0a, 0x07, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x73, 0x70, 0x10, 0x05, 0x12, 0x19, 0x0a, 0x15,
95   - 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x52, 0x6f, 0x6c, 0x65, 0x50, 0x72, 0x6f, 0x70, 0x65, 0x72,
96   - 0x74, 0x79, 0x52, 0x73, 0x70, 0x10, 0x06, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62,
97   - 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
  94 + 0x6f, 0x12, 0x09, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x64, 0x65, 0x2a, 0x9c, 0x01, 0x0a,
  95 + 0x09, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x4e,
  96 + 0x4b, 0x4e, 0x4f, 0x57, 0x4e, 0x10, 0x00, 0x12, 0x0c, 0x0a, 0x08, 0x4c, 0x6f, 0x67, 0x69, 0x6e,
  97 + 0x52, 0x73, 0x70, 0x10, 0x01, 0x12, 0x0c, 0x0a, 0x08, 0x48, 0x65, 0x61, 0x72, 0x74, 0x52, 0x65,
  98 + 0x71, 0x10, 0x02, 0x12, 0x0c, 0x0a, 0x08, 0x48, 0x65, 0x61, 0x72, 0x74, 0x52, 0x73, 0x70, 0x10,
  99 + 0x03, 0x12, 0x0c, 0x0a, 0x08, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x10, 0x04, 0x12,
  100 + 0x0d, 0x0a, 0x09, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x10, 0x05, 0x12, 0x0b,
  101 + 0x0a, 0x07, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x73, 0x70, 0x10, 0x06, 0x12, 0x19, 0x0a, 0x15, 0x55,
  102 + 0x70, 0x64, 0x61, 0x74, 0x65, 0x52, 0x6f, 0x6c, 0x65, 0x50, 0x72, 0x6f, 0x70, 0x65, 0x72, 0x74,
  103 + 0x79, 0x52, 0x73, 0x70, 0x10, 0x07, 0x12, 0x13, 0x0a, 0x0f, 0x47, 0x6f, 0x49, 0x6e, 0x74, 0x6f,
  104 + 0x42, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x52, 0x65, 0x71, 0x10, 0x08, 0x42, 0x0a, 0x5a, 0x08, 0x2e,
  105 + 0x2e, 0x2f, 0x70, 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
98 106 }
99 107  
100 108 var (
... ...
1   -Subproject commit 43bf509f59c5aa87099fff52e0eeede70b0fe7d4
  1 +Subproject commit 003912502b9da983fa9c33b1890f9830b625e21a
... ...
tools/protostostruct.go
  1 +//弃用 使用python3重写了。
1 2 package main
2 3  
3 4 import (
... ...