-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
199 lines (180 loc) · 5.88 KB
/
main.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// +build pro free debug
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"reflect"
"strconv"
)
// TournamentConfig is the master configuration
type TournamentConfig struct {
CompetitionName string `json:"competitionName"`
EnableWebserver bool `json:"enableWebserver"`
FileReadSpeed int `json:"fileReadSpeed"`
MatchDataDir string `json:"matchDataDir"`
MatchConfig MatchConfig `json:"matchConfig"`
TwitchChannel string `json:"twitchChannel"`
WebserverPort int `json:"webserverPort"`
WebsiteURL string `json:"websiteUrl"`
}
// MatchConfig holds match specific configuration data.
type MatchConfig struct {
LogfileDirectory string `json:"logfileDirectory"`
PlayoffSchedule string `json:"playoffSchedule"`
PlayoffsEnabled bool `json:"playoffsEnabled"`
QualSchedule string `json:"qualSchedule"`
QualificationsEnabled bool `json:"qualificationsEnabled"`
}
var (
// DefaultConfig is the default config.
DefaultConfig = TournamentConfig{
CompetitionName: "xRC Tournament",
EnableWebserver: true,
FileReadSpeed: 5,
MatchDataDir: "./",
WebsiteURL: "localhost",
MatchConfig: MatchConfig{
LogfileDirectory: "./",
PlayoffsEnabled: true,
QualificationsEnabled: true,
QualSchedule: "schedule.csv",
PlayoffSchedule: "elimschedule.csv",
},
TwitchChannel: "SinnDevelopment",
WebserverPort: 8080,
}
// MATCHES holds the current master list of matches played.
MATCHES []XRCMatchData
// PLAYERS holds the current master list of players seen.
PLAYERS []XRCPlayer
// PLAYERSET holds the player master list.
PLAYERSET = make(map[string]XRCPlayer)
// Config is the currently active configuration
Config TournamentConfig
// QualSchedule is the imported qual schedule
QualSchedule Schedule
// PlayoffSchedule is the imported playoff scheule
PlayoffSchedule Schedule
// MasterSchedule is the currently active event schedule
MasterSchedule *Schedule
// Compile time variables.
Version string
CommitHash string
)
func main() {
fmt.Println("Starting xRC Tournament v" + Version + "@" + CommitHash + " by Sinn Development - https://sinndevelopment.com")
_, err := os.Open("config.json")
if err != nil {
fmt.Println("Could not open config.json. Using default values.")
fmt.Println(err)
// Write config.json out from default values.
Config = DefaultConfig
defaultConfigJSON, _ := json.Marshal(DefaultConfig)
err = ioutil.WriteFile("config.json", defaultConfigJSON, 0775)
if err != nil {
fmt.Println("Could not write default config.json.")
fmt.Println(err)
}
return
}
configJSON, err := ioutil.ReadFile("config.json")
if err != nil {
fmt.Println("Could not read config.json.")
fmt.Println(err)
return
}
err = json.Unmarshal(configJSON, &Config)
if err != nil {
fmt.Println("Could not parse config.json. Please correct linting errors.")
fmt.Println(err)
return
}
quit := make(chan struct{})
if Config.EnableWebserver {
// Qualifications and Playoffs are only usable when in webserver mode.
if Config.MatchConfig.QualificationsEnabled {
QualSchedule = ImportSchedule(Config.MatchConfig.QualSchedule)
QualSchedule.Type = "Qualification"
MasterSchedule = &QualSchedule
}
if Config.MatchConfig.PlayoffsEnabled {
PlayoffSchedule = ImportSchedule(Config.MatchConfig.PlayoffSchedule)
PlayoffSchedule.Type = "Playoff"
MasterSchedule = &PlayoffSchedule
}
usePlayers := true
useMatches := true
matchesJSON, err := ioutil.ReadFile("matches.json")
if err != nil {
fmt.Println("Could not read matches.json. Starting with no matches run.")
useMatches = false
}
playerJSON, err := ioutil.ReadFile("players.json")
if err != nil {
fmt.Println("Could not read players.json. Starting with no players.")
usePlayers = false
}
if useMatches {
err = json.Unmarshal(matchesJSON, &MATCHES)
if err != nil {
fmt.Println(err)
}
if Config.MatchConfig.PlayoffsEnabled || Config.MatchConfig.QualificationsEnabled {
debug("Read in matches successfully. Parsing to find scheduled matches.")
expected := 0
for i, m := range MATCHES {
matchFound, schedule := IsScheduledMatch(&m, MasterSchedule.Matches)
if matchFound && !MasterSchedule.Matches[schedule].Completed {
expected++
MasterSchedule.Matches[schedule].Completed = true
MasterSchedule.Matches[schedule].MatchData = &m
MasterSchedule.Matches[schedule].MasterIndex = i
UpdateMatchWLT(&m, PLAYERSET)
}
}
imported := 0
for _, m := range MasterSchedule.Matches {
if m.MatchData != nil {
imported++
}
}
debug(MasterSchedule)
debug("Real Scheduled matches found that were completed: " + strconv.Itoa(imported))
debug("Potentially Matching Scheduled matches found that were completed: " + strconv.Itoa(expected))
}
}
if usePlayers {
err = json.Unmarshal(playerJSON, &PLAYERS)
if err != nil {
fmt.Println(err)
}
debug("Reading in master player lists.")
for _, p := range PLAYERS {
if p.Name == "" {
debug("Found player with empty string name. Likely a non-full match.")
continue
}
if reflect.DeepEqual(PLAYERSET[p.Name], XRCPlayer{}) {
debug("Found new player.")
PLAYERSET[p.Name] = p
continue
}
debug("Found player that already exists. " + p.Name)
player := PLAYERSET[p.Name]
player.Update(p)
PLAYERSET[p.Name] = player
}
}
setVersion()
go XRCDataHandler(Config.FileReadSpeed, quit)
fmt.Println("Successfully loaded webserver, view at http://" + Config.WebsiteURL + ":" + strconv.Itoa(Config.WebserverPort))
startWebserver(strconv.Itoa(Config.WebserverPort))
} else {
// If the webserver is not enabled, we must block the main thread from exiting with the datahandler.
setVersion()
fmt.Println("Successfully loaded file archiver.")
XRCDataHandler(Config.FileReadSpeed, quit)
}
}