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