-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
87 lines (70 loc) · 1.89 KB
/
cache.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"fmt"
"strconv"
)
// want to be able to cache enemy information relating to the environment
// cache things in CR order?
var monsterCache map[string]Monster
var environmentCache map[Environment][]Monster
func initialize() {
loadCache()
loadEnvironmentCache()
}
func loadCache() {
monsterCache = make(map[string]Monster)
monsters := loadAllMonstersFromFile() // TODO load from database instead
for i := 0; i < len(monsters); i++ {
monster := monsters[i]
monsterCache[monster.Name] = monster
}
fmt.Printf("%v\n", monsterCache)
}
func _loadEnvironmentCacheDb() {
environmentCache = make(map[Environment][]Monster)
for _, env := range Environments {
monsters := databaseSelectMonstersByEnvironment(env);
environmentCache[env] = monsters
}
}
func loadEnvironmentCache() {
environmentCache = make(map[Environment][]Monster)
for _, env := range Environments {
monsters := cacheGetMonsterByEnvironment(env);
environmentCache[env] = monsters
}
}
// yes, this is quadratic...
func cacheGetMonsterByEnvironment(env Environment) []Monster {
monsters := make([]Monster, 30)
for _, monster := range monsterCache {
inEnv := false
for _, mEnv := range monster.env {
if mEnv == env {
inEnv = true
}
}
if inEnv {
monsters = append(monsters, monster)
}
}
return monsters
}
// TODO: refactor to imporve extensibility
func getMonsters(env Environment, cr int) []Monster {
candidates := make([]Monster,10)
for name, mon := range monsterCache {
crInt, _ := strconv.Atoi(mon.Challenge_Rating)
if crInt == cr {
candidates = append(candidates, mon)
fmt.Println(name)
}
}
return candidates
}
func getMonstersForEnvironment(env Environment) []Monster {
// make sure the environment ID is related to the database's
// TODO: programmatically enforce ID relation
// want to index it different ways anyway, like order by CR
return environmentCache[env]
}