diff --git a/README.md b/README.md index 81aa6c3..a6c1986 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,6 @@ ChirpNestBot - [ ] Items parsing/identification - [ ] Withdrawal bot - [x] Foray interception -- [ ] Tribute interception \ No newline at end of file +- [ ] Tribute interception +- [ ] Experience graphs & forecast +- [ ] Redis/eliminate map race conditions \ No newline at end of file diff --git a/client.go b/client.go index 9dc8bd9..1deed4c 100644 --- a/client.go +++ b/client.go @@ -21,7 +21,7 @@ func clientMsgMeAck(m *ChatWarsMessageMeAck) { c.GuildID64 = m.GuildID64 c.State = m.State c.LastUpdate = m.Msg.Date - if cacheObjGuild[``].ObjID64 != m.GuildID64 && strings.Compare(c.Role, ``) == 0 { + if getObjGuildID(``) != m.GuildID64 && strings.Compare(c.Role, ``) == 0 { clientSendCWMsg(m.Msg.UserID64, "/g_roles") } } @@ -34,7 +34,7 @@ func clientMsgMeAck(m *ChatWarsMessageMeAck) { LastUpdate: m.Msg.Date, } clientsCW[m.Msg.UserID64] = &c - if cacheObjGuild[``].ObjID64 != m.GuildID64 { + if getObjGuildID(``) != m.GuildID64 { clientSendCWMsg(m.Msg.UserID64, "/g_roles") } } diff --git a/obj.go b/obj.go index be489bc..ee12f51 100644 --- a/obj.go +++ b/obj.go @@ -2,13 +2,14 @@ package main import ( "strconv" + "sync" "time" ) var ( - cacheObjCastle map[string]ChatWarsCastle - cacheObjGuild map[string]ChatWarsGuild - cacheObjUser map[string]ChatWarsUser + cacheObjCastle sync.Map + cacheObjGuild sync.Map + cacheObjUser sync.Map ) func getObjTypeId(objId int64) (int64, error) { @@ -157,7 +158,8 @@ func addObjCastle(logo string, name string) (int64, error) { } func getObjCastleID(s string) int64 { - return cacheObjCastle[s].ObjID64 + c, _ := cacheObjCastle.Load(s) + return c.ObjID64 } func loadObjCastle() error { @@ -167,7 +169,7 @@ func loadObjCastle() error { name string ) - cacheObjCastle = make(map[string]ChatWarsCastle) + cacheObjCastle = new(sync.Map) castles, err := db.Query(`SELECT oc.obj_id, oc.logo, oc.name FROM obj_castle oc;`) if err != nil { @@ -184,8 +186,8 @@ func loadObjCastle() error { c.ObjID64 = id c.Logo = logo c.Name = name - cacheObjCastle[logo] = *c - cacheObjCastle[name] = *c + cacheObjCastle.Store(logo, *c) + cacheObjCastle.Store(name, *c) } return nil @@ -241,7 +243,7 @@ func addObjGuild(tag string, name string) (int64, error) { } func getObjGuildID(s string) int64 { - if g, ok := cacheObjGuild[s]; ok { + if g, ok := cacheObjGuild.Load(s); ok { return g.ObjID64 } else { objID64, err := addObjGuild(s, ``) @@ -250,9 +252,9 @@ func getObjGuildID(s string) int64 { g.ObjID64 = objID64 g.Tag = s g.Name = `` - cacheObjGuild[s] = *g + cacheObjGuild.Store(s, *g) + return objID64 } - return cacheObjGuild[s].ObjID64 } func loadObjGuild() error { @@ -262,7 +264,7 @@ func loadObjGuild() error { name string ) - cacheObjGuild = make(map[string]ChatWarsGuild) + cacheObjGuild = new(sync.Map) guilds, err := db.Query(`SELECT og.obj_id, og.tag, og.name FROM obj_guild og;`) if err != nil { @@ -279,7 +281,7 @@ func loadObjGuild() error { g.ObjID64 = id g.Tag = tag g.Name = name - cacheObjGuild[tag] = *g + cacheObjGuild.Store(tag, *g) } return nil @@ -335,7 +337,7 @@ func addObjUser(name string) (int64, error) { } func getObjUserID(s string) int64 { - if u, ok := cacheObjUser[s]; ok { + if u, ok := cacheObjUser.Load(s); ok { return u.ObjID64 } else { objID64, err := addObjUser(s) @@ -343,9 +345,9 @@ func getObjUserID(s string) int64 { u := new(ChatWarsUser) u.ObjID64 = objID64 u.Name = s - cacheObjUser[s] = *u + cacheObjUser.Store(s, *u) + return objID64 } - return cacheObjUser[s].ObjID64 } func loadObjUser() error { @@ -354,7 +356,7 @@ func loadObjUser() error { name string ) - cacheObjUser = make(map[string]ChatWarsUser) + cacheObjUser = new(sync.Map) users, err := db.Query(`SELECT ou.obj_id, ou.name FROM obj_user ou;`) if err != nil { @@ -370,7 +372,7 @@ func loadObjUser() error { u := new(ChatWarsUser) u.ObjID64 = id u.Name = name - cacheObjUser[name] = *u + cacheObjUser.Store(name, *u) } return nil