Commit 0ce6c418858831a69e66346b24cd4b46af83f580

Authored by zhangqijia
1 parent 70bbc959

fix: 修复 bug RoleModels.saveRoleData

common/etcd/etcd.go
... ... @@ -87,7 +87,6 @@ func (e *EtcdClient) PutWithLeasePrefix(prefix, key, val string, ttl int64) erro
87 87 //每秒会续租一次,所以就会受到一次应答
88 88 //fmt.Println("收到自动续租应答:", keepResp.ID)
89 89 }
90   - logger.Debug(keepChan)
91 90 }
92 91 }
93 92  
... ...
models/role.go
... ... @@ -13,11 +13,11 @@ import (
13 13 type RoleModel struct {
14 14 components.ISchema
15 15 Role *pb.Role
16   - Heros HeroMap
17   - Teams TeamMap
  16 + Heros SchemaMap
  17 + Teams SchemaMap
18 18 Prop *PropModel
19 19  
20   - lastSaveTs int64
  20 + lastSaveTs int64
21 21 }
22 22  
23 23 func RoleExistByUid(uid string) *RoleModel {
... ... @@ -28,13 +28,12 @@ func RoleExistByUid(uid string) *RoleModel {
28 28 return nil
29 29 }
30 30  
31   -
32 31 r := &RoleModel{
33 32 ISchema: NewSchema(data.Id, data),
34   - Role: data,
35   - Heros: make(HeroMap),
36   - Teams: make(TeamMap),
37   - Prop: new(PropModel),
  33 + Role: data,
  34 + Heros: make(SchemaMap),
  35 + Teams: make(SchemaMap),
  36 + Prop: new(PropModel),
38 37 }
39 38 r.LoadAll()
40 39 return r
... ... @@ -44,14 +43,14 @@ func NewRole(id string) *RoleModel {
44 43 data := &pb.Role{Id: id}
45 44 m := &RoleModel{
46 45 ISchema: NewSchema(id, data),
47   - Role: data,
48   - Heros: make(HeroMap),
49   - Teams: make(TeamMap),
  46 + Role: data,
  47 + Heros: make(SchemaMap),
  48 + Teams: make(SchemaMap),
50 49 }
51 50 return m
52 51 }
53 52  
54   -func (m *RoleModel) InitRole() {
  53 +func (m *RoleModel) InitRole() {
55 54 //init hero
56 55 h1 := pb.Hero{
57 56 Id: common.SnowFlack.NextValStr(),
... ... @@ -121,12 +120,12 @@ func (m *RoleModel) LoadTeams() {
121 120 logger.Error(err)
122 121 return
123 122 }
124   - for _, team:= range teams {
  123 + for _, team := range teams {
125 124 m.Teams[team.Id] = NewTeam(team)
126 125 }
127 126 }
128 127  
129   -func (m *RoleModel) LoadAll() {
  128 +func (m *RoleModel) LoadAll() {
130 129 m.LoadHero()
131 130 m.LoadTeams()
132 131 }
... ... @@ -142,18 +141,17 @@ func (m *RoleModel) GetAllHero() []*pb.Hero {
142 141 return h
143 142 }
144 143  
145   -func (m *RoleModel) GetAllTeam() []*pb.Team{
  144 +func (m *RoleModel) GetAllTeam() []*pb.Team {
146 145 var t []*pb.Team
147   - for _, team := range m.Teams{
148   - t = append(t, team.Team)
  146 + for _, team := range m.Teams {
  147 + t = append(t, team.(*TeamModel).Team)
149 148 }
150 149 return t
151 150 }
152 151  
153   -
154 152 func (m *RoleModel) AddHero(hero *pb.Hero) {
155 153 h := NewHero(hero)
156   - h .Create()
  154 + h.Create()
157 155 m.Heros[hero.Id] = h
158 156 }
159 157  
... ... @@ -163,7 +161,6 @@ func (m *RoleModel) AddTeam(team *pb.Team) {
163 161 m.Teams[team.Id] = t
164 162 }
165 163  
166   -
167 164 func (m *RoleModel) OnRecoverTimer(now int64) {
168 165 m.saveRoleData(now)
169 166 }
... ... @@ -174,7 +171,7 @@ func (m *RoleModel) OnOfflineEvent() {
174 171 }
175 172  
176 173 func (m *RoleModel) saveRoleData(now int64) {
177   - if now - m.lastSaveTs < common.SaveDataInterval {
  174 + if now-m.lastSaveTs < common.SaveDataInterval {
178 175 return
179 176 }
180 177 atomic.StoreInt64(&m.lastSaveTs, now)
... ... @@ -187,12 +184,12 @@ func (m *RoleModel) saveRoleData(now int64) {
187 184 }
188 185 }
189 186  
190   - //mpObjs := []interface{}{m.Heros, m.Teams}
191   - //for _, mpObj := range mpObjs {
192   - // for _, v := range mpObj.(map[string]components.ISchema) {
193   - // if v != nil {
194   - // v.Update()
195   - // }
196   - // }
197   - //}
198   -}
199 187 \ No newline at end of file
  188 + mpObjs := []SchemaMap{m.Heros, m.Teams}
  189 + for _, mpObj := range mpObjs {
  190 + for _, v := range mpObj {
  191 + if v != nil {
  192 + v.Update()
  193 + }
  194 + }
  195 + }
  196 +}
... ...
models/schema.go
... ... @@ -16,6 +16,8 @@ func WithSchemaDB(idb components.IDB) SchemaOption {
16 16 }
17 17 }
18 18  
  19 +type SchemaMap map[string]components.ISchema
  20 +
19 21 type Schema struct {
20 22 db components.IDB
21 23 reflectValue *reflect.Value
... ... @@ -23,7 +25,7 @@ type Schema struct {
23 25  
24 26 cacheFields map[string]interface{}
25 27  
26   - pri interface{}
  28 + pri interface{}
27 29 schema interface{}
28 30 }
29 31  
... ... @@ -34,13 +36,13 @@ func NewSchema(key string, schema interface{}) *Schema {
34 36 }
35 37 sch := &Schema{
36 38 reflectValue: &s,
37   - reflectType: s.Type(),
38   - cacheFields: make(map[string]interface{}),
39   - schema: schema,
  39 + reflectType: s.Type(),
  40 + cacheFields: make(map[string]interface{}),
  41 + schema: schema,
40 42 }
41 43  
42 44 sch.db = mongoproxy.NewMongoColl(sch.GetSchemaName(), sch)
43   - sch.pri = mongoproxy.GetBsonD(sch.getPriTag(), key)
  45 + sch.pri = mongoproxy.GetBsonD(sch.getPriTag(), key)
44 46 return sch
45 47 }
46 48  
... ... @@ -58,9 +60,9 @@ func (s *Schema) FindIndex() (string, []string) {
58 60 return strings.ToLower(s.reflectType.Name()), index
59 61 }
60 62  
61   -func (s *Schema) Init() {
  63 +func (s *Schema) Init() {
62 64 coll, keys := s.FindIndex()
63   - for _, index := range keys {
  65 + for _, index := range keys {
64 66 s.db.CreateTable()
65 67  
66 68 logger.Debug("InitDoc collect: %v, createIndex: %s", coll, index)
... ...
models/team.go
1 1 package models
2 2  
3 3 import (
  4 + "pro2d/common/components"
4 5 "pro2d/pb"
5 6 )
6 7  
7 8 type TeamMap map[string]*TeamModel
8 9 type TeamModel struct {
9   - *Schema
  10 + components.ISchema
10 11 Team *pb.Team
11 12 }
12 13  
13 14 func NewTeam(data *pb.Team) *TeamModel {
14 15 m := &TeamModel{
15   - Schema: NewSchema(data.Id, data),
16   - Team: data,
  16 + ISchema: NewSchema(data.Id, data),
  17 + Team: data,
17 18 }
18 19  
19 20 return m
20   -}
21 21 \ No newline at end of file
  22 +}
... ...