-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.go
114 lines (96 loc) · 3.57 KB
/
data.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"discord-esports/models"
"encoding/json"
"fmt"
"os"
"sync"
"time"
)
type EsportsData struct {
LastPostTimestamp time.Time
LastUpdateTimestamp time.Time
LOLSchedule map[string][]LOLEsportsLeagueSchedule `json:"lolSchedule"`
VALSchedule map[string][]VALEsportsTournamentSchedule `json:"valSchedule"`
m sync.Mutex
}
// Loads or create json file
func loadOrCreateFile(filePath string, target interface{}) error {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
err := os.WriteFile(filePath, []byte("{}"), 0644)
if err != nil {
return fmt.Errorf("error creating '%v' file: %v", filePath, err)
}
}
err := loadFile(filePath, &target)
if err != nil {
return fmt.Errorf("error loading file '%v': %v", filePath, err)
}
return nil
}
func loadFile(filePath string, target interface{}) error {
file, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("error reading file '%v': %v", filePath, err)
}
err = json.Unmarshal(file, &target)
if err != nil {
return fmt.Errorf("error parsing json '%v': %v", filePath, err)
}
return nil
}
func saveEsportsFile() {
client.logger.Info("Saving esports data file.")
esports.m.Lock()
defer esports.m.Unlock()
file, err := json.MarshalIndent(&esports, "", " ")
if err != nil {
client.logger.Error(fmt.Sprintf("error parsing esports data file: %v", err))
}
err = os.WriteFile(ESPORTS_FILE_PATH, file, 0644)
if err != nil {
client.logger.Error(fmt.Sprintf("Error writing esports data file: %v", err))
}
}
func loadWikiData() error {
files, err := os.ReadDir(CHAMPIONS_FOLDER_PATH)
if err != nil {
return fmt.Errorf("error reading '%v' dir: %v", CHAMPIONS_FOLDER_PATH, err)
}
championsNames = make(map[string]string)
for _, f := range files {
if f.IsDir() {
continue
}
champion := models.Champion{}
err = loadFile(fmt.Sprintf("%v/%v", NORMALIZED_CHAMPIONS_FOLDER_PATH, f.Name()), &champion)
if err != nil {
return fmt.Errorf("error loading champion file '%v': %v", f.Name(), err)
}
spellsInfo[champion.Key] = make([]SpellInfo, 0)
spellsEmbeds[champion.Key] = make(map[string][]SpellEmbeds)
for i, spell := range champion.Spells.Passive {
spellsInfo[champion.Key] = append(spellsInfo[champion.Key], createSpellInfo(&spell, "P", i))
spellsEmbeds[champion.Key]["P"] = append(spellsEmbeds[champion.Key]["P"], createChampionSpellEmbed(&champion, &spell))
}
for i, spell := range champion.Spells.Q {
spellsInfo[champion.Key] = append(spellsInfo[champion.Key], createSpellInfo(&spell, "Q", i))
spellsEmbeds[champion.Key]["Q"] = append(spellsEmbeds[champion.Key]["Q"], createChampionSpellEmbed(&champion, &spell))
}
for i, spell := range champion.Spells.W {
spellsInfo[champion.Key] = append(spellsInfo[champion.Key], createSpellInfo(&spell, "W", i))
spellsEmbeds[champion.Key]["W"] = append(spellsEmbeds[champion.Key]["W"], createChampionSpellEmbed(&champion, &spell))
}
for i, spell := range champion.Spells.E {
spellsInfo[champion.Key] = append(spellsInfo[champion.Key], createSpellInfo(&spell, "E", i))
spellsEmbeds[champion.Key]["E"] = append(spellsEmbeds[champion.Key]["E"], createChampionSpellEmbed(&champion, &spell))
}
for i, spell := range champion.Spells.R {
spellsInfo[champion.Key] = append(spellsInfo[champion.Key], createSpellInfo(&spell, "R", i))
spellsEmbeds[champion.Key]["R"] = append(spellsEmbeds[champion.Key]["R"], createChampionSpellEmbed(&champion, &spell))
}
championsEmbeds[champion.Key] = createChampionEmbed(&champion)
championsNames[champion.Name] = champion.Key
}
return nil
}