-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
349 lines (305 loc) · 9.73 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
QUORATE main file
Copyright (C) 2024 Nota
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"bufio"
"encoding/xml"
"errors"
"flag"
"fmt"
"log"
"os"
nsclient "quorate/internal/ns-client"
"sort"
"strconv"
"strings"
"time"
)
const Gpl = `QUORATE v1.0.5, Copyright (C) 2024 Nota
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. Check the LICENSE file for more info.
`
type Hit struct {
Name string
Delegate string
SecondNation string
IsDeltip bool
UpdateTime int64
TriggerRegion string
TriggerTime int64
}
type RegionsDumpList struct {
Regions []RegionDump `xml:"REGION"`
}
type RegionDump struct {
Name string `xml:"NAME"`
LastMinor int64 `xml:"LASTMINORUPDATE"`
LastMajor int64 `xml:"LASTMAJORUPDATE"`
}
func flagPassed(name string, set flag.FlagSet) bool {
found := false
set.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}
func main() {
var maxEndoCount int
var minimumTrigger int
var isMinor bool
var reDownDump bool
var proposalId string
var userAgent string
flagSet := flag.FlagSet{}
flagSet.StringVar(&userAgent, "useragent", "", "Your user agent")
flagSet.StringVar(&proposalId, "proposal", "", "The proposal ID")
flagSet.IntVar(&maxEndoCount, "endos", -1, "The maximum endorsement count for a target")
flagSet.IntVar(&minimumTrigger, "mintrig", -1, "The minimum trigger time")
flagSet.BoolVar(&isMinor, "minor", false, "Use if generating times for minor")
flagSet.BoolVar(&reDownDump, "redownload", false, "Use to redownload the daily dump if it's already present")
err := flagSet.Parse(os.Args[1:])
if errors.Is(err, flag.ErrHelp) {
return
} else if err != nil {
log.Fatal(err)
}
fmt.Println(Gpl)
scanner := bufio.NewScanner(os.Stdin)
for userAgent == "" {
fmt.Print("Enter your main nation: ")
scanner.Scan()
userAgent = scanner.Text()
time.Sleep(50 * time.Millisecond) //avoids weird behavior on ctrl C
}
nsclient.SetUserAgent(userAgent)
log.Println("User agent set to " + userAgent)
time.Sleep(500 * time.Millisecond)
for proposalId == "" {
fmt.Print("Enter a World Assembly Proposal ID (e.g. proposal_id_12312312): ")
scanner.Scan()
proposalId = scanner.Text()
time.Sleep(50 * time.Millisecond)
}
log.Println("Proposal set to " + proposalId)
time.Sleep(500 * time.Millisecond)
for endoCountString := ""; err != nil || maxEndoCount < 1; maxEndoCount, err = strconv.Atoi(endoCountString) {
fmt.Print("Enter the endo count: ")
scanner.Scan()
endoCountString = strings.ToLower(scanner.Text())
time.Sleep(50 * time.Millisecond)
}
log.Printf("Endo count set to %d!\n", maxEndoCount)
time.Sleep(500 * time.Millisecond)
for minTrigString := ""; err != nil || minimumTrigger < 1; minimumTrigger, err = strconv.Atoi(minTrigString) {
fmt.Print("Enter the minimum trigger time: ")
scanner.Scan()
minTrigString = strings.ToLower(scanner.Text())
time.Sleep(50 * time.Millisecond)
}
log.Printf("Minimum trigger set to %d!\n", minimumTrigger)
time.Sleep(500 * time.Millisecond)
if !flagPassed("minor", flagSet) {
var choice string
for choice != "major" && choice != "minor" {
fmt.Print("Which update do you want to search for? (minor/major) ")
scanner.Scan()
choice = strings.ToLower(scanner.Text())
time.Sleep(50 * time.Millisecond)
}
isMinor = choice == "minor"
}
getNewDump := true
if _, err := os.Stat("regions.xml"); err == nil {
if flagPassed("redownload", flagSet) {
getNewDump = reDownDump
} else {
choice := "qwerty"
for choice != "y" && choice != "n" && choice != "" {
fmt.Print("Daily regions dump already downloaded! Download again? (Y/n) ")
scanner.Scan()
choice = strings.ToLower(scanner.Text())
time.Sleep(50 * time.Millisecond)
}
if choice == "n" {
getNewDump = false
}
}
}
if getNewDump {
log.Println("Getting region dump...")
err := nsclient.GetRegionDump()
if err != nil {
log.Fatal(err)
}
log.Println("Region dump saved!")
}
log.Printf("Getting approvals on proposal %s...\n", proposalId)
approvals, err := nsclient.GetProposalApprovals(proposalId)
if err != nil {
log.Fatal(err)
}
log.Printf("%d approvals found!\n", len(approvals))
log.Println("Checking which regions can be hit (this may take a while)...")
var hittable []Hit
for _, approval := range approvals {
//we do this by api to see if delbumps are possible. we also get update times just to sort them here.
region, err := nsclient.GetNationRegion(approval)
if err != nil {
log.Fatal(err)
}
regionInfo, err := nsclient.GetRegionInfo(region)
if err != nil {
log.Fatal(err)
}
if regionInfo.Password {
continue
}
region = strings.Replace(strings.ToLower(region), " ", "_", -1)
var updateTime int64
if isMinor {
updateTime = regionInfo.LastMinor
} else {
updateTime = regionInfo.LastMajor
}
if regionInfo.DelEndos < maxEndoCount {
hit := Hit{Name: region, Delegate: approval, IsDeltip: false, UpdateTime: updateTime}
hittable = append(hittable, hit)
log.Printf("Region %s with delegate %s can be hit!\n", region, approval)
} else if regionInfo.DelEndos < regionInfo.SecondEndos+maxEndoCount {
hit := Hit{Name: region, Delegate: approval, SecondNation: regionInfo.SecondNation, IsDeltip: true,
UpdateTime: updateTime}
hittable = append(hittable, hit)
log.Printf("Region %s with delegate %s can be deltipped by nation %s!\n", region, approval,
regionInfo.SecondNation)
}
time.Sleep(1500 * time.Millisecond) //courtesy sleep even though we have ratelimiting
}
if len(hittable) == 0 {
log.Print("No regions are hittable! Press enter to exit...")
scanner.Scan()
}
log.Printf("Checks done! %d regions are hittable!\n", len(hittable))
//sort hittable targets by update time
log.Println("Sorting regions by update time...")
sort.Slice(hittable[:], func(i, j int) bool {
return hittable[i].UpdateTime < hittable[j].UpdateTime
})
log.Println("Regions sorted!")
log.Println("Loading regions dump...")
dumpFile, err := os.ReadFile("regions.xml")
if err != nil {
log.Fatal(err)
}
var regionsList RegionsDumpList
err = xml.Unmarshal(dumpFile, ®ionsList)
if err != nil {
log.Fatal(err)
}
regions := regionsList.Regions
dumpFile = nil //remove from memory
log.Println("Regions dump loaded!")
updateTimes := make(map[int64]string)
firstUpdateRegion := regions[0].Name
var firstUpdateTime int64
if isMinor {
firstUpdateTime = regions[0].LastMinor
} else {
firstUpdateTime = regions[0].LastMajor
}
hitIndex := 0
log.Println("Getting triggers for regions...")
for _, region := range regions {
canonName := strings.Replace(strings.ToLower(region.Name), " ", "_", -1)
if hitIndex == len(hittable) {
break
}
var regionUpdate int64
if isMinor {
regionUpdate = region.LastMinor
} else {
regionUpdate = region.LastMajor
}
if _, exists := updateTimes[regionUpdate]; !exists {
updateTimes[regionUpdate] = canonName
}
//edge case where region doesn't exist in daily dump
if hitIndex != len(hittable)-1 && canonName == hittable[hitIndex+1].Name {
hitIndex++
}
if canonName == hittable[hitIndex].Name {
hittable[hitIndex].UpdateTime = regionUpdate
for i := 0; true; i++ {
trigTime := regionUpdate - int64(minimumTrigger+i)
if trigRegion, exists := updateTimes[trigTime]; exists {
hittable[hitIndex].TriggerTime = trigTime
hittable[hitIndex].TriggerRegion = trigRegion
hitIndex++
break
} else if trigTime <= firstUpdateTime {
hittable[hitIndex].TriggerTime = firstUpdateTime
hittable[hitIndex].TriggerRegion = firstUpdateRegion
hitIndex++
break
}
}
}
}
log.Println("Triggers obtained!")
log.Println("Creating trigger_list.txt and raidFile.txt...")
updateStartTime := time.Unix(firstUpdateTime, 0)
for updateStartTime.Second() != 0 {
updateStartTime = updateStartTime.Add(-1 * time.Second)
}
var triggerFileBuilder strings.Builder
var raidFileBuilder strings.Builder
for i, hit := range hittable {
if hit.TriggerRegion == "" {
continue
}
updateStartTimeDiff := (time.Duration(hit.UpdateTime-firstUpdateTime) * time.Second).String()
triggerTimeDiff := time.Duration(hit.UpdateTime-hit.TriggerTime) * time.Second
triggerFileBuilder.WriteString(hit.TriggerRegion + "\n")
raidFileBuilder.WriteString(fmt.Sprintf("%d) https://www.nationstates.net/region=%s (%s)\n", i+1, hit.Name,
updateStartTimeDiff))
if hit.IsDeltip {
raidFileBuilder.WriteString(fmt.Sprintf("ENDORSE: https://www.nationstates.net/nation=%s\n", hit.SecondNation))
}
raidFileBuilder.WriteString(fmt.Sprintf("\ta) https://www.nationstates.net/template-overall=none/region=%s (%s)\n\n",
hit.TriggerRegion, triggerTimeDiff))
}
triggerFile, err := os.Create("trigger_list.txt")
if err != nil {
log.Fatal(err)
}
defer triggerFile.Close()
_, err = triggerFile.WriteString(triggerFileBuilder.String())
if err != nil {
log.Fatal(err)
}
raidFile, err := os.Create("raidFile.txt")
if err != nil {
log.Fatal(err)
}
defer raidFile.Close()
_, err = raidFile.WriteString(raidFileBuilder.String())
if err != nil {
log.Fatal(err)
}
log.Print("Files created! Press enter to exit...")
scanner.Scan()
}