This repository has been archived by the owner on Jul 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
492 lines (462 loc) · 14.4 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math"
"math/rand"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/gizak/termui"
)
var (
client *http.Client
APIURL = "https://community.steam-api.com/%s/v0001/"
selfInfo SelfInfo
textPos int
dMap = map[int]int{1: 600, 2: 1200, 3: 2400, 4: 2400}
scoreMap = []int{0, 1200, 2400, 4800, 12000, 30000, 72000, 180000, 450000, 1200000, 2400000, 3600000, 4800000, 6000000, 7200000, 8400000, 9600000, 10800000, 12000000, 14400000, 16800000, 19200000, 21600000, 24000000, 26400000}
dName = map[int]string{1: "easy", 2: "medium", 3: "hard", 4: "boss"}
pMap map[string]PlanetDetail
nextPlanet string
nextZone int
difficulty int
cache time.Time
token string
steamID string
accountID int
)
func init() {
client = &http.Client{}
pMap = make(map[string]PlanetDetail)
res, err := ioutil.ReadFile("token.txt")
if err != nil {
fmt.Println("Couldn't read token", err)
os.Exit(1)
}
token = string(res)
token = strings.TrimSpace(token)
var t Token
json.Unmarshal(res, &t)
if t.Steamid != "" {
steamID = t.Steamid
token = t.Token
accountID, err = strconv.Atoi(steamID)
account64 := int64(accountID) & 0xFFFFFFFF
accountID = int(account64)
if err != nil {
log.Fatal("accountid", err)
}
}
}
func spost(path string, form url.Values) []byte {
form.Add("access_token", token)
req, err := http.NewRequest("POST", fmt.Sprintf(APIURL, path), strings.NewReader(form.Encode()))
if err != nil {
log.Fatalln(err)
}
req.Header.Set("Origin", "https://steamcommunity.com")
req.Header.Set("Referer", "https://steamcommunity.com/saliengame/play")
req.Header.Set("Accept", "*/*")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3464.0 Safari/537.36")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
// log.Println(string(body))
return body
}
func sget(path string, params string) []byte {
req, err := http.NewRequest("GET", fmt.Sprintf(APIURL, path)+params, nil)
req.Header.Set("Origin", "https://steamcommunity.com")
req.Header.Set("Referer", "https://steamcommunity.com/saliengame/play")
req.Header.Set("Accept", "*/*")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3464.0 Safari/537.36")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
// log.Println(string(body))
return body
}
func joinPlanet(id string) {
printStatus("joining planet " + id)
spost("ITerritoryControlMinigameService/JoinPlanet", url.Values{"id": []string{id}})
}
func leavePlanet() {
if selfInfo.Response.ActivePlanet != "" {
printStatus("leaving planet " + selfInfo.Response.ActivePlanet)
spost("IMiniGameService/LeaveGame", url.Values{"gameid": []string{selfInfo.Response.ActivePlanet}})
}
}
func leaveZone() {
if selfInfo.Response.ActiveZoneGame != "" {
printStatus("leaving zone " + selfInfo.Response.ActiveZoneGame)
spost("IMiniGameService/LeaveGame", url.Values{"gameid": []string{selfInfo.Response.ActiveZoneGame}})
}
if selfInfo.Response.ActiveBossGame != "" {
printStatus("leaving boss zone " + selfInfo.Response.ActiveZoneGame)
spost("IMiniGameService/LeaveGame", url.Values{"gameid": []string{selfInfo.Response.ActiveBossGame}})
}
}
func joinZone(pos int) error {
printStatus(fmt.Sprintf("joining zone %d", pos))
res := spost("ITerritoryControlMinigameService/JoinZone", url.Values{"zone_position": []string{strconv.Itoa(pos)}})
var zoneinfo ZoneInfo
json.Unmarshal(res, &zoneinfo)
if zoneinfo.Response.ZoneInfo.ZonePosition == 0 {
printStatus(fmt.Sprintf("ERROR: zone %d failed", pos))
return fmt.Errorf("failed")
}
printStatus(fmt.Sprintf("OK: zone %d joined", pos))
printZoneCapture(strconv.Itoa(int(math.Trunc(zoneinfo.Response.ZoneInfo.CaptureProgress*100))) + "%")
return nil
}
func joinBossZone(pos int) error {
printStatus(fmt.Sprintf("joining zone %d", pos))
res := spost("ITerritoryControlMinigameService/JoinBossZone", url.Values{"zone_position": []string{strconv.Itoa(pos)}})
var zoneinfo ZoneInfo
json.Unmarshal(res, &zoneinfo)
printStatus(fmt.Sprintf("%#v", zoneinfo.Response))
/*
if zoneinfo.Response.ZoneInfo.ZonePosition == 0 {
printStatus(fmt.Sprintf("ERROR: bosszone %d failed", pos))
return fmt.Errorf("failed")
}
*/
printStatus(fmt.Sprintf("OK: bosszone %d joined", pos))
printZoneCapture(strconv.Itoa(int(math.Trunc(zoneinfo.Response.ZoneInfo.CaptureProgress*100))) + "%")
return nil
}
func getPlanets() Planets {
res := sget("ITerritoryControlMinigameService/GetPlanets", "?active_only=1")
var planets Planets
json.Unmarshal(res, &planets)
for _, p := range planets.Response.Planets {
if p.State.Captured == false {
printStatus(p.ID + " " + p.State.Name + " --")
}
}
return planets
}
func getPlanet(ID string) PlanetInfo {
res := sget("ITerritoryControlMinigameService/GetPlanet", "?id="+ID)
var planets Planets
json.Unmarshal(res, &planets)
if len(planets.Response.Planets) == 0 {
return PlanetInfo{}
}
planet := planets.Response.Planets[0]
pMap[ID] = planet
allZones := make(map[int][]int)
for _, z := range planet.Zones {
if z.ZonePosition == 0 {
continue
}
if !z.Captured && z.CaptureProgress < 0.95 {
allZones[z.Difficulty] = append(allZones[z.Difficulty], z.ZonePosition)
}
if !z.Captured && z.Type == 4 {
allZones[z.Type] = append(allZones[z.Type], z.ZonePosition)
}
}
return PlanetInfo{allZones}
}
func getUncapturedPlanets() string {
info := make(map[string]PlanetInfo)
planets := getPlanets()
for _, planet := range planets.Response.Planets {
if planet.State.Captured == false {
planetinfo := getPlanet(planet.ID)
info[planet.ID] = planetinfo
}
}
type result struct {
ID string
Count int
}
results := make(map[int][]result)
for i := 4; i > 0; i-- {
for k, v := range info {
results[i] = append(results[i], result{k, len(v.allZones[i])})
}
}
sort := make(map[int]int)
ordered := make(map[int]string)
for i := 4; i > 0; i-- {
for _, result := range results[i] {
if result.Count > sort[i] {
sort[i] = result.Count
ordered[i] = result.ID
}
}
}
for i := 4; i > 0; i-- {
if sort[i] > 0 {
printStatus(fmt.Sprintf("search returning planet %s with %d zones of difficulty %d", ordered[i], sort[i], i))
printZonesLeft(strconv.Itoa(sort[i]))
return ordered[i]
}
}
return ""
}
func refreshPlanetInfo() PlanetInfo {
if selfInfo.Response.ActivePlanet != "" {
return getPlanet(selfInfo.Response.ActivePlanet)
}
return PlanetInfo{}
}
func getSelfInfo(p bool) {
res := spost("ITerritoryControlMinigameService/GetPlayerInfo", url.Values{})
json.Unmarshal(res, &selfInfo)
if p {
printStatus(fmt.Sprintf("Planet/Zone: %s %s Level: %d Score: %s Nextscore: %s", selfInfo.Response.ActivePlanet, selfInfo.Response.ActiveZonePosition,
selfInfo.Response.Level, selfInfo.Response.Score, selfInfo.Response.NextLevelScore))
printScore(selfInfo.Response.Score)
printLevel(strconv.Itoa(selfInfo.Response.Level))
printPlanet(selfInfo.Response.ActivePlanet)
}
}
func reportScore(addscore int) error {
res := spost("ITerritoryControlMinigameService/ReportScore", url.Values{"score": []string{strconv.Itoa(addscore)}})
var score Score
json.Unmarshal(res, &score)
if score.Response.NewScore == "" {
printStatus("ERROR: score update failed")
return fmt.Errorf("score update failed")
}
printScore(score.Response.NewScore)
if score.Response.OldLevel != score.Response.NewLevel {
printLevel(strconv.Itoa(score.Response.NewLevel))
}
return nil
}
func random(min, max int) int {
rand.Seed(time.Now().Unix())
return rand.Intn(max-min) + min
}
func fightBoss() error {
var boss Boss
waitcount := 0
res := spost("ITerritoryControlMinigameService/ReportBossDamage", url.Values{"use_heal_ability": []string{strconv.Itoa(0)}, "damage_to_boss": []string{strconv.Itoa(0)}, "damage_taken": []string{strconv.Itoa(0)}})
json.Unmarshal(res, &boss)
for boss.Response.WaitingForPlayers == true {
printStatus("OK: boss: waiting for players " + strconv.Itoa(waitcount))
time.Sleep(time.Second * 5)
res := spost("ITerritoryControlMinigameService/ReportBossDamage", url.Values{"use_heal_ability": []string{strconv.Itoa(0)}, "damage_to_boss": []string{strconv.Itoa(0)}, "damage_taken": []string{strconv.Itoa(0)}})
json.Unmarshal(res, &boss)
waitcount++
if waitcount == 5 {
printStatus("ERROR: waiting to long for players, returning")
return fmt.Errorf("waiting to long for players, returning")
}
}
waitcount = 1
for boss.Response.GameOver == false && boss.Response.BossStatus.BossMaxHp != 0 {
printStatus("OK: boss: updating score " + strconv.Itoa(waitcount))
res := spost("ITerritoryControlMinigameService/ReportBossDamage", url.Values{"use_heal_ability": []string{strconv.Itoa(0)}, "damage_to_boss": []string{strconv.Itoa(random(40, 90))}, "damage_taken": []string{strconv.Itoa(0)}})
json.Unmarshal(res, &boss)
bossMax := boss.Response.BossStatus.BossMaxHp
bossHP := boss.Response.BossStatus.BossHp
perc := int(math.Trunc(float64(bossMax-bossHP) / float64(bossMax) * 100))
for _, p := range boss.Response.BossStatus.BossPlayers {
if p.Accountid == accountID {
oldscore, err := strconv.Atoi(p.ScoreOnJoin)
if err != nil {
break
}
currentscore := oldscore + p.XpEarned
nextscore, err := strconv.Atoi(p.NextLevelScore)
if err != nil {
break
}
printScore(strconv.Itoa(currentscore))
basescore := scoreMap[selfInfo.Response.Level-1]
perc := math.Trunc(float64(currentscore-basescore) / float64(nextscore-basescore) * 100)
updateNextLevelGauge(int(perc))
todoscore := nextscore - currentscore
rate := float64(p.XpEarned) / float64(waitcount*5)
printNextLevel(strconv.Itoa(todoscore/int(rate)) + "s")
bossrate := float64(bossMax-bossHP) / float64(waitcount*5)
printNextGrind(strconv.Itoa(int(bossrate)) + "s")
break
}
}
updateBossGauge(perc)
time.Sleep(time.Second * 5)
waitcount++
}
return nil
}
func getNext() (string, int, int) {
nextPlanet := getUncapturedPlanets()
nextZone := 0
difficulty := 0
info := getPlanet(nextPlanet)
for k := 4; k > 0; k-- {
if len(info.allZones[k]) > 0 {
t := fmt.Sprintf("difficulty: %d available: %#v", k, info.allZones[k])
printStatus(t)
nextZone = info.allZones[k][0]
difficulty = k
break
}
}
cache = time.Now()
return nextPlanet, nextZone, difficulty
}
func loop() error {
var err error
planetID := ""
printZone("?")
printPlanet("?")
printDifficulty("?")
printCapture("?")
printNextGrind("?")
printNextLevel("?")
printZoneCapture("?")
// printZonesLeft("?")
getSelfInfo(true)
nextscore, _ := strconv.Atoi(selfInfo.Response.NextLevelScore)
currentscore, _ := strconv.Atoi(selfInfo.Response.Score)
todoscore := nextscore - currentscore
t := fmt.Sprintf("%.2f %% done for next level\n", (float64(currentscore)/float64(nextscore))*100)
basescore := scoreMap[selfInfo.Response.Level-1]
perc := math.Trunc(float64(currentscore-basescore) / float64(nextscore-basescore) * 100)
updateGauge(0)
updateNextLevelGauge(int(perc))
printStatus(t)
// only lookup if we're starting up, otherwise we have stuff in cache
if time.Since(cache) > time.Second*20 {
nextPlanet, nextZone, difficulty = getNext()
if nextPlanet != selfInfo.Response.ActivePlanet {
leavePlanet()
}
if strconv.Itoa(nextZone) != selfInfo.Response.ActiveZonePosition {
leaveZone()
}
}
planetID = nextPlanet
if nextPlanet != selfInfo.Response.ActivePlanet {
if planetID == "" {
printStatus("ERROR: no planetID found")
return fmt.Errorf("no planetID found")
}
joinPlanet(planetID)
}
time.Sleep(time.Millisecond * 500)
getSelfInfo(false)
if selfInfo.Response.ActivePlanet == planetID {
printStatus("OK: planet " + planetID + " joined")
printPlanet(planetID)
} else {
printStatus("ERROR: planet " + planetID + " join failed")
time.Sleep(time.Second * 5)
return fmt.Errorf("planetjoin failed")
}
retryjoincount := 0
retryjoin:
if retryjoincount == 5 {
printStatus("ERROR: retryjoin to high, returning. Sleeping 10s")
time.Sleep(time.Second * 10)
return fmt.Errorf("retryjoin failed")
}
if strconv.Itoa(nextZone) != selfInfo.Response.ActiveZonePosition {
var err error
if difficulty == 4 {
err = joinBossZone(nextZone)
} else {
err = joinZone(nextZone)
}
if err != nil {
time.Sleep(time.Second * 5)
retryjoincount++
goto retryjoin
}
} else {
printStatus(fmt.Sprintf("OK: zone already %d joined", nextZone))
refreshPlanetInfo()
}
printZone(strconv.Itoa(nextZone))
printDifficulty(dName[difficulty])
printCapture(strconv.Itoa(int(math.Trunc(pMap[planetID].State.CaptureProgress*100))) + "%")
if difficulty == 4 {
printStatus("OK: start fighting boss")
return fightBoss()
}
ticker := time.NewTicker(1 * time.Second)
go func(difficulty int) {
i := 0
for range ticker.C {
i++
perc := math.Trunc(float64(i) / float64(110) * 100)
printNextGrind(strconv.Itoa(110-i) + "s")
score := dMap[difficulty]
printNextLevel(strconv.Itoa(todoscore/(score/110)-i) + "s")
updateGauge(int(perc))
// proactive next planet
if i == 105 {
nextPlanet, nextZone, difficulty = getNext()
}
}
}(difficulty)
time.Sleep(time.Second * 110)
ticker.Stop()
score := dMap[difficulty]
printStatus(fmt.Sprintf("next level will be in %d seconds", todoscore/(score/110)))
printNextLevel(strconv.Itoa(todoscore/(score/110)) + "s")
retryscorecount := 0
retryscore:
if retryscorecount == 5 {
printStatus("ERROR: retryscore to high, returning. Sleeping 10s")
time.Sleep(time.Second * 10)
return fmt.Errorf("retryscore failed")
}
err = reportScore(score)
if err != nil {
time.Sleep(time.Second * 5)
retryscorecount++
goto retryscore
}
return nil
}
func main() {
err := termui.Init()
if err != nil {
panic(err)
}
defer termui.Close()
termui.Handle("/sys/kbd/q", func(termui.Event) {
termui.StopLoop()
os.Exit(0)
})
printText("Info", "Press q to exit", 40, 0, 15)
go func() {
for {
err := loop()
if err != nil {
time.Sleep(time.Second * 10)
}
updateGauge(0)
printStatus("OK: starting new loop")
}
}()
termui.Loop()
}