-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
1752 lines (1627 loc) · 56.1 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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"crypto/tls"
"encoding/json"
"log"
"net"
"strings"
"github.com/line/line-bot-sdk-go/linebot"
"github.com/mong0520/ChainChronicleGo/clients"
"github.com/mong0520/ChainChronicleGo/clients/item"
"github.com/mong0520/ChainChronicleGo/clients/quest"
"github.com/mong0520/ChainChronicleGo/clients/session"
"github.com/mong0520/ChainChronicleGo/clients/tower"
"github.com/mong0520/ChainChronicleGo/clients/user"
"github.com/mong0520/ChainChronicleGo/clients/uzu"
"github.com/mong0520/ChainChronicleGo/models"
"github.com/mong0520/ChainChronicleGo/utils"
"fmt"
"os"
"path"
"strconv"
"time"
mapset "github.com/deckarep/golang-set"
"github.com/icza/dyno"
"github.com/jessevdk/go-flags"
"github.com/mong0520/ChainChronicleGo/clients/card"
"github.com/mong0520/ChainChronicleGo/clients/explorer"
"github.com/mong0520/ChainChronicleGo/clients/gacha"
"github.com/mong0520/ChainChronicleGo/clients/general"
"github.com/mong0520/ChainChronicleGo/clients/present"
"github.com/mong0520/ChainChronicleGo/clients/raid"
"github.com/mong0520/ChainChronicleGo/clients/teacher"
"github.com/mong0520/ChainChronicleGo/clients/tutorial"
"github.com/mong0520/ChainChronicleGo/clients/weapon"
"github.com/mong0520/ChainChronicleGo/controllers"
"github.com/op/go-logging"
"github.com/robfig/config"
uuid "github.com/satori/go.uuid"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
const (
ModeHttp string = "http"
ModeHttps string = "https"
)
type Options struct {
ConfigPath string `short:"c" long:"config" description:"Config path" required:"true"`
GlobalConfigPath string `short:"g" long:"global" description:"global config path" required:"false"`
Action string `short:"a" long:"action" description:"Action to run" required:"false"`
Repeat int `short:"r" long:"repeat" description:"Repeat action for r times" required:"false"`
Timeout int `short:"t" long:"timeout" description:"Timeout in seconds between repeat" required:"false"`
Mode string `short:"m" long:"mode" description:"Chatbot mode or cli mode" required:"false" default:"cli"`
}
var bot *linebot.Client
var lineReplyMessage string
var options Options
var parser = flags.NewParser(&options, flags.Default)
var metadata = &clients.Metadata{}
//var logger *log.Logger
var logger *logging.Logger
var LogROOT = "logs"
var actionMapping = map[string]interface{}{
"QUEST": doQuest,
"STATUS": doStatus,
"PASSWORD": doPassword,
"TAKEOVER": doTakeOver,
"BUY": doBuy,
"GACHA": doGacha,
"TUTORIAL": doTutorial,
"DRAMA": doDrama,
"DEBUG": doDebug,
"RESET_DISCIPLE": doResetDisciple,
"CHARS": doShowChars,
"ALLDATA": doShowAllData,
"COMPOSE": doCompose,
"TEACHER": doTeacher,
"DISCIPLE": doDisciple,
"UPDATE": doUpdateDB,
"EXPLORER": doExplorer,
"TOWER": doTower,
"EXTOWER": doExTower,
"WASTE": doWasteMoney,
"SHOWUZU": doShowUZU,
"UZU": doUzu,
"SORTIE": doSortie,
"BOOK": doShowBook,
}
func doAction(sectionName string) {
for action, actionFunction := range actionMapping {
//logger.Info(action, actionFunction)
if strings.HasPrefix(sectionName, action) {
logger.Infof("### Current Flow = [%s] ###", sectionName)
actionFunction.(func(*clients.Metadata, string))(metadata, sectionName)
}
}
}
func initLogFile() (logFile *os.File, err error) {
logFileName := path.Base(options.ConfigPath)
logFilePath := path.Join("logs", logFileName)
if _, err := os.Stat(LogROOT); os.IsNotExist(err) {
os.Mkdir(LogROOT, 0755)
}
return os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
}
func start() {
logFile, err := initLogFile()
defer logFile.Close()
if err != nil {
logger.Error("open file error !")
}
logger = utils.GetLoggerEx(logFile)
globalConfig, err := config.ReadDefault("conf/global.conf")
if err != nil {
logger.Error("Unable to read config, ", err)
return
}
config, err := config.ReadDefault(options.ConfigPath)
if err != nil {
logger.Error("Unable to read config, ", err)
return
}
metadata.Config = config
dialInfo := &mgo.DialInfo{}
if globalConfig.HasSection("DB") {
logger.Debug("MongoDB is connected by global config")
dbURI, err := globalConfig.String("DB", "uri")
if err != nil {
logger.Error(err)
}
dialInfo, _ = mgo.ParseURL(dbURI)
tlsConfig := &tls.Config{}
dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
conn, err := tls.Dial("tcp", addr.String(), tlsConfig)
return conn, err
}
if db, err := mgo.DialWithInfo(dialInfo); err != nil {
logger.Error("Unable to connect DB", err)
logger.Debug(dialInfo)
} else {
metadata.DB = db
}
} else {
logger.Debug("MongoDB is connected to localhost")
//connect to localhost without username/password
if db, err := mgo.Dial("localhost:27017"); err != nil {
logger.Error("Unable to connect DB", err)
} else {
metadata.DB = db
}
}
// c, err := redis.Dial("tcp", ":6379")
// if err != nil {
// logger.Fatal(err)
// }
// metadata.RedisConn = c
// defer c.Close()
//utils.DumpConfig(metadata.Config)
uid, _ := metadata.Config.String("GENERAL", "Uid")
metadata.Uid = uid
//logger.Info(uid)
token, _ := metadata.Config.String("GENERAL", "Token")
metadata.Token = token
if options.Action == "" {
flowString, _ := metadata.Config.String("GENERAL", "Flow")
flowString = strings.Replace(flowString, " ", "", -1)
metadata.Flow = strings.Split(flowString, ",")
} else {
flowString := options.Action
flowString = strings.Replace(flowString, " ", "", -1)
metadata.Flow = strings.Split(flowString, ",")
}
sid, err := session.Login(uid, token, false)
if err != nil {
logger.Infof("%s\n", err)
return
}
fmt.Println("sid = ", sid)
alldata, _ := user.GetAllData(sid)
metadata.AllData = alldata
metadata.Sid = sid
metadata.AllDataS = &models.AllData{}
err = json.Unmarshal([]byte(utils.Map2JsonString(metadata.AllData)), metadata.AllDataS)
if err != nil {
log.Println(err)
os.Exit(-1)
}
//dumpUser(metadata)
if options.Mode == "d" {
fmt.Println("Start daemon mode...")
// InitLineBot(metadata)
}
flowLoop, _ := metadata.Config.Int("GENERAL", "FlowLoop")
sleepDuration, err := metadata.Config.Int("GENERAL", "FlowLoopDelay")
if options.Repeat > 0 {
flowLoop = options.Repeat
}
if options.Timeout > 0 {
sleepDuration = options.Timeout
}
for i := 1; i <= flowLoop; i++ {
logger.Infof("Start #%d Flow\n", i)
for _, flow := range metadata.Flow {
logger.Infof("Current action = [%s]\n", flow)
doAction(strings.ToUpper(flow))
}
if sleepDuration > 0 {
logger.Info("Waiting", sleepDuration, "seconds")
time.Sleep(time.Second * time.Duration(sleepDuration))
}
}
}
func doDrama(metadata *clients.Metadata, section string) {
questInfo := quest.NewQuest()
//questList, _ := dyno.GetSlice(metadata.AllData, "body", 29, "data")
//logger.Info(questList)
logger.Infof("開始通過主線任務...\n")
maxRetryCount := 10
currentRetryCount := 0
flag := 0
lastQid := 331043
counter := 0
dramaLevel := 1
gradudateThreshold := 38
hcid := 9420
lvThreshold := 50
for {
//logger.Info(v, reflect.TypeOf(v))
//logger.Info(dyno.Get(metadata.AllData, "body", 29, "data", flag, "id"))
if qType, err := dyno.GetFloat64(metadata.AllData, "body", 29, "data", flag, "type"); err != nil {
logger.Info(qType, err)
} else {
questInfo.Type = int(qType)
}
if qId, err := dyno.GetFloat64(metadata.AllData, "body", 29, "data", flag, "id"); err != nil {
logger.Info(qId, err)
} else {
questInfo.QuestId = int(qId)
}
logger.Info(questInfo.QuestId)
counter += 1
if counter >= gradudateThreshold {
// check if current LV >= 50
break
}
break
questInfo.Fid = 1965350
questInfo.Lv = dramaLevel
questInfo.Hcid = hcid
questInfo.Pt = 0
questInfo.Version = 3
errSet := mapset.NewSet()
errSet.Clear()
//logger.Infof("%+v\n", questInfo)
resp, err := questInfo.StartQeust(metadata)
errSet.Add(err)
switch err {
case 0:
_, err = questInfo.EndQeust(metadata)
logger.Infof("%d/%d 完成關卡\n", counter, gradudateThreshold)
errSet.Add(err)
case 103:
logger.Infof("體力不足\n")
if _, err := user.RecoveryAp(1, 1, metadata.Sid); err != 0 {
logger.Info("無法恢復體力")
panic(err)
}
default:
logger.Info("未知的錯誤")
errSet.Add(err)
logger.Info(utils.Map2JsonString(resp))
if resp, err := questInfo.GetTreasure(metadata); err != 0 {
logger.Info(resp)
}
}
if errSet.Contains(0) == false {
logger.Infof("Unknown error in drama: %s", errSet)
currentRetryCount++
if currentRetryCount >= maxRetryCount {
uid, _ := metadata.Config.String("GENERAL", "Uid")
logger.Infof("UID %s is is uable to complete Drama", uid)
} else {
logger.Info("Retry...")
continue
}
} else {
currentRetryCount = 0
if questInfo.QuestId == lastQid {
continue
} else {
if flag >= 4 {
dramaLevel = 2
}
flag++
}
}
}
alldata, _ := user.GetAllData(metadata.Sid)
metadata.AllData = alldata
if currentLv, err := dyno.GetFloat64(metadata.AllData, "body", 4, "data", "lv"); err != nil {
logger.Info(err)
} else {
logger.Infof("Current LV %0.f", currentLv)
if int(currentLv) >= lvThreshold {
teacher.IS_GRADUATED = true
}
}
}
func doTutorial(metadata *clients.Metadata, section string) {
tutorialInfo := []map[string]int{
{"tid": 0, "qid": -1},
{"tid": 1, "qid": -1},
{"tid": 2, "qid": -1},
{"tid": 3, "qid": 210001},
{"tid": 4, "qid": 210001},
{"tid": 5, "qid": -1},
{"tid": 6, "qid": 210002},
{"tid": 7, "qid": -1},
{"tid": 8, "qid": 210101},
{"tid": 9, "qid": -1},
{"tid": 10, "qid": 210101},
{"tid": 11, "qid": -1},
{"tid": 12, "qid": -1},
{"tid": 13, "qid": 210102},
{"tid": 14, "qid": -1},
{"tid": 15, "qid": 210102},
{"tid": 16, "qid": -1},
{"tid": 17, "qid": 215000},
{"tid": 18, "qid": 215000},
{"tid": 19, "qid": -1},
{"tid": 20, "qid": -1},
}
newUid := fmt.Sprintf("ANDO%s", uuid.Must(uuid.NewV4(), nil).String())
logger.Infof("New UUID = %s", newUid)
// set tor proxy
sid, err := session.Login(newUid, "", false)
if err != nil {
log.Printf("無法建立新帳號,嘗試使用 TOR...\n")
if sid, err = session.Login(newUid, "", true); err != nil {
log.Printf("建立帳號失敗 %s\n", err)
os.Exit(1)
}
}
metadata.Uid = newUid
//logger.Info(uid)
token, _ := metadata.Config.String("GENERAL", "Token")
metadata.Token = token
metadata.Sid = sid
resp, _ := user.GetAllData(sid)
openId, _ := dyno.Get(resp, "body", 4, "data", "uid")
logger.Infof("新帳號創立成功, UID = %s, OpenID = %.0f\n", newUid, openId)
//
for _, t := range tutorialInfo {
if t["qid"] != -1 {
param := map[string]interface{}{
"pt": 0,
"tid": t["tid"],
}
tutorial.Tutorial(sid, true, param)
questInfo := quest.NewQuest()
questInfo.QuestId = t["qid"]
questInfo.Fid = 1965350
questInfo.Pt = 0
if resp, err := questInfo.EndQeust(metadata); err != 0 {
logger.Info(utils.Map2JsonString(resp), err)
break
}
} else {
if t["tid"] == 1 {
param := map[string]interface{}{
"name": "Allen",
"hero": "Allen",
"tid": t["tid"],
}
if resp, err := tutorial.Tutorial(sid, false, param); err != 0 {
logger.Info(utils.Map2JsonString(resp), err)
break
}
} else {
param := map[string]interface{}{
"tid": t["tid"],
}
if resp, err := tutorial.Tutorial(sid, false, param); err != 0 {
logger.Info(utils.Map2JsonString(resp), err)
break
}
}
}
}
logger.Infof("新帳號完成新手教學, UID = %s, OpenID = %.0f\n", newUid, openId)
getPresents(metadata, nil)
}
func doGacha(metadata *clients.Metadata, section string) {
var result strings.Builder
gachaInfo := gacha.NewGachaInfo()
utils.ParseConfig2Struct(metadata.Config, section, gachaInfo)
logger.Info("開始轉蛋")
if resp, ret := gachaInfo.Gacha(metadata); ret == 0 {
gachaResult := ProcessGachaResult(resp)
for _, card := range gachaResult["char"].([]models.GachaResultChara) {
myCard := models.Charainfo{}
query := bson.M{"cid": card.ID}
controllers.GeneralQuery(metadata.DB, "charainfo", query, &myCard)
msg := fmt.Sprintf("得到 %d星卡: %s-%s", myCard.Rarity, myCard.Title, myCard.Name)
result.WriteString(msg)
//logger.Infof("得到 %d星卡: %s-%s", myCard.Rarity, myCard.Title, myCard.Name)
if gachaInfo.AutoSell && myCard.Rarity <= gachaInfo.AutoSellRarityThreshold {
logger.Info("賣出卡片...")
doSellItem(metadata, card.Idx, "")
}
}
} else {
result.WriteString("轉蛋失敗,請查看訊息記錄")
logger.Info(utils.Map2JsonString(resp))
}
lineReplyMessage = result.String()
}
func doSellItem(metadata *clients.Metadata, cid int, section string) {
if ret, err := card.Sell(metadata, cid); err != 0 {
logger.Info("\t-> 賣出卡片失敗", utils.Map2JsonString(ret))
} else {
logger.Info("\t-> 賣出卡片成功")
}
}
func doWasteMoney(metadata *clients.Metadata, section string) {
// 一次少 9000
targetMoney := 110000000
targetCount := targetMoney / 9000
logger.Info("Target Count =", targetCount)
for i := 0; i < targetCount; i++ {
logger.Infof("Loop # %d", i)
doExplorer(metadata, "EXPLORER")
for eid := 1; eid <= 3; eid++ {
explorer.CancelExplorer(metadata.Sid, eid)
}
// if i%10 == 0 {
// doStatus(metadata, section)
// }
}
}
func doExplorer(metadata *clients.Metadata, section string) {
getPresents(metadata, []string{"card"})
setCharaData()
metadata.ExplorerExcludeCids = []int{2007}
explorerList, _ := explorer.GetExplorerList(metadata.Sid)
pickup, _ := dyno.GetSlice(explorerList, "pickup")
//log.Printf("%s\n", utils.Map2JsonString(explorerList))
//log.Printf("%v\n", pickup)
pickupList := []explorer.Pickup{}
for _, p := range pickup {
pickupItem := &explorer.Pickup{}
utils.Map2Struct(p.(map[string]interface{}), pickupItem)
pickupList = append(pickupList, *pickupItem)
}
//for _, pickupIteam := range pickupList{
// log.Printf("%+v\n", pickupIteam)
//}
useStone, _ := metadata.Config.Bool(section, "StoneFinish")
explorerAreaStr, _ := metadata.Config.String(section, "area")
explorerAreas := strings.Split(explorerAreaStr, ",")
mock, _ := metadata.Config.Bool(section, "Mock")
for i, e := range explorerAreas {
area, _ := strconv.Atoi(e)
resp, err := explorer.GetExplorerResult(metadata.Sid, i+1)
switch err {
case 0, 2308:
logger.Info("可以開始探索")
case 2302:
logger.Info("探索尚未結束")
logger.Infof("Use stone to finish? %t\n", useStone)
if useStone {
explorer.FinishExplorer(metadata.Sid, i+1)
//logger.Info(utils.Map2JsonString(resp))
resp, _ = explorer.GetExplorerResult(metadata.Sid, i+1)
//logger.Info(utils.Map2JsonString(resp))
rewards, _ := dyno.GetSlice(resp, "explorer_reward")
for _, reward := range rewards {
//logger.Info(reward)
rewardItem := reward.(map[string]interface{})["item_id"].(float64)
rewardType := reward.(map[string]interface{})["item_type"].(string)
//logger.Info("Reward Type ", rewardType)
if rewardType == "card" {
tmpCardInfo := &models.Charainfo{} // for mongodb result
cid := int(rewardItem)
//logger.Info("cid =", cid)
query := bson.M{"cid": cid}
if err := controllers.GeneralQuery(metadata.DB, "charainfo", query, &tmpCardInfo); err != nil {
logger.Info("得到角色", rewardItem, err)
} else {
logger.Info("得到", tmpCardInfo.Rarity, "星角色", tmpCardInfo.Name)
if tmpCardInfo.Cid == 6248 {
os.Exit(0)
}
}
} else {
//logger.Info("得到ID", rewardItem)
}
}
}
case 1:
logger.Info("已被登出")
default:
logger.Info("未知的結果")
logger.Info(resp)
}
for _, pickupItem := range pickupList {
if pickupItem.LocationID == area {
//logger.Infof("Start to find best card to explorer area %d\n", pickupItem.LocationID)
result := map[string]int{}
if mock {
result = findBestCardToExplorerMocked(i)
} else {
result = findBestCardToExplorer(&pickupItem)
}
param := map[string]int{
"explorer_idx": i + 1,
"location_id": area,
"card_idx": result["idx"],
"pickup": 1,
"interval": 1,
}
resp, err := explorer.StartExplorer(metadata.Sid, param)
switch err {
case 0:
break
case 2311:
param["pickup"] = 0
explorer.StartExplorer(metadata.Sid, param)
default:
logger.Infof("%s\n", utils.Map2JsonString(resp))
}
}
}
}
}
func setCharaData() {
chars, _ := dyno.GetSlice(metadata.AllData, "body", 6, "data")
charaInfo := []models.Charainfo{} // for mongodb result
charaData := []models.CharaData{} // for alldata structure
for _, c := range chars {
tmpCardInfo := &models.Charainfo{} // for mongodb result
tmpCharData := &models.CharaData{} // for alldata structure
utils.Map2Struct(c.(map[string]interface{}), tmpCharData)
if tmpCharData.Type != 0 {
continue
}
query := bson.M{"cid": tmpCharData.ID}
if err := controllers.GeneralQuery(metadata.DB, "charainfo", query, &tmpCardInfo); err != nil {
logger.Info(tmpCharData.ID, err)
} else {
charaInfo = append(charaInfo, *tmpCardInfo)
charaData = append(charaData, *tmpCharData)
}
}
if metadata.CharInfo == nil {
metadata.CharInfo = charaInfo
}
if metadata.CharData == nil {
metadata.CharData = charaData
}
}
func findBestCardToExplorerMocked(idx int) (result map[string]int) {
result = map[string]int{
"cid": 0,
"idx": 433731138,
}
results := []map[string]int{
{"cid": 0, "idx": 402682419},
{"cid": 0, "idx": 421163524},
{"cid": 0, "idx": 433731138},
}
return results[idx]
}
func findBestCardToExplorer(pickupItem *explorer.Pickup) (result map[string]int) {
result = map[string]int{
"cid": 0,
"idx": 0,
}
for idx, charInfo := range metadata.CharInfo {
charData := metadata.CharData[idx]
if charInfo.Rarity >= 5 {
// 不使用五星卡探索
continue
} else if ((pickupItem.Home == charInfo.Home) || (pickupItem.Jobtype == charInfo.Jobtype)) && !utils.InArray(charInfo.Cid, metadata.ExplorerExcludeCids) {
// 適合的
logger.Infof("Pick %s to explorer, cid = %d, idx = %d, rank = %d\n", charInfo.Name, charInfo.Cid, charData.Idx, charInfo.Rarity)
metadata.ExplorerExcludeCids = append(metadata.ExplorerExcludeCids, charInfo.Cid)
result["cid"] = charInfo.Cid
result["idx"] = charData.Idx
break
} else {
// 找不到適合的
result["cid"] = charInfo.Cid
result["idx"] = charData.Idx
}
}
return result
}
func ProcessGachaResult(resp map[string]interface{}) (gachaResult map[string]interface{}) {
gachaData, _ := dyno.GetSlice(resp, "body")
//logger.Info(utils.Map2JsonString(resp))
gachaResult = map[string]interface{}{
"char": []models.GachaResultChara{},
"item": []models.GachaResultItem{},
"weapon": []models.GachaResultWeapon{},
}
gachaResult["char"] = []models.GachaResultChara{}
gachaResult["item"] = []models.GachaResultItem{}
gachaResult["weapon"] = []models.GachaResultWeapon{}
charList := make([]models.GachaResultChara, 0)
itemList := make([]models.GachaResultItem, 0)
weaponList := make([]models.GachaResultWeapon, 0)
for i, data := range gachaData {
if i == 0 {
continue
}
dataType, _ := dyno.GetFloat64(data, "type")
switch dataType {
case 15:
logger.Info(i, "Type 15", data)
case 1:
//logger.Info(i, "得到角色")
list := data.(map[string]interface{})["data"].([]interface{})
for _, item := range list {
tmpItem := &models.GachaResultChara{}
tmpDBItem := &models.Charainfo{}
if err := utils.Map2Struct(item.(map[string]interface{}), tmpItem); err != nil {
logger.Info("Unable to convert to struct", err)
} else {
query := bson.M{"cid": tmpItem.ID}
if err := controllers.GeneralQuery(metadata.DB, "charainfo", query, tmpDBItem); err != nil {
logger.Info(i, "得到", tmpItem.ID)
} else {
logger.Infof("得到 %d星卡: %s-%s", tmpDBItem.Rarity, tmpDBItem.Title, tmpDBItem.Name)
}
charList = append(charList, *tmpItem)
}
}
case 2:
//logger.Info(i, "得到成長卡/冶鍊卡", data)
list := data.(map[string]interface{})["data"].([]interface{})
for _, item := range list {
tmpItem := &models.GachaResultItem{}
tmpDBItem := &models.Chararein{}
if err := utils.Map2Struct(item.(map[string]interface{}), tmpItem); err != nil {
logger.Info("Unable to convert to struct", err)
} else {
query := bson.M{"id": tmpItem.ItemID}
controllers.GeneralQuery(metadata.DB, "chararein", query, tmpDBItem)
logger.Info(i, "得到", tmpDBItem.Name)
itemList = append(itemList, *tmpItem)
}
}
case 14:
continue
case 105:
list := data.(map[string]interface{})["data"].([]interface{})
for _, item := range list {
tmpItem := &models.GachaResultWeapon{}
tmpDBItem := &models.Weapon{}
if err := utils.Map2Struct(item.(map[string]interface{}), tmpItem); err != nil {
logger.Info("Unable to convert to struct", err)
} else {
query := bson.M{"id": tmpItem.ItemID}
if err := controllers.GeneralQuery(metadata.DB, "evolve", query, tmpDBItem); err != nil {
logger.Info(i, "得到", tmpItem.ItemID)
} else {
logger.Info(i, "得到", tmpDBItem.Name)
}
weaponList = append(weaponList, *tmpItem)
}
}
default:
logger.Info(dataType)
}
}
gachaResult["char"] = charList
gachaResult["item"] = itemList
return gachaResult
}
func doShowUZU(metadata *clients.Metadata, section string) {
lineReplyMessage = ""
uzuData, _ := uzu.GetUzuInfo(metadata.Sid)
uzuHistoryStr, _ := json.Marshal(metadata.AllData["body"].([]interface{})[27].(map[string]interface{})["data"])
uzuHistories := uzu.UzuHistoryStruct{}
json.Unmarshal([]byte(uzuHistoryStr), &uzuHistories)
logger.Debug("UZUID \tName\t\tScheduleID\tLastScheduleID\tFinishedStage(idx start from 1)")
for idx, uzu := range uzuData.Uzu {
currentScheduleID := uzuData.GetCurrentScheduleID(uzu.UzuID)
clearList := uzuHistories[idx].ClearList
lastScheduleID := uzuHistories[idx].LastScheduleID
msgShort := fmt.Sprintf("%s=%d\n", uzu.Name, uzu.UzuID)
msg := fmt.Sprintf("%d\t%s\t%d\t\t%d\t\t%v", uzu.UzuID, uzu.Name, currentScheduleID, lastScheduleID, clearList)
lineReplyMessage = lineReplyMessage + msgShort
logger.Debugf(msg)
}
}
func doUzu(metadata *clients.Metadata, section string) {
// stage id from 1 to 12, NOT form 0
// Entry qeust
apiEntry := "uzu/entry"
apiRecover := "user/recover_uzu"
apiResult := "uzu/result"
param := map[string]interface{}{}
autoRecover, err := metadata.Config.Bool(section, "AutoRecover")
if err != nil {
autoRecover = false
}
metadata.Config.RemoveOption(section, "AutoRecover")
paramsRaw, _ := metadata.Config.SectionOptions(section)
for _, p := range paramsRaw {
param[p], _ = metadata.Config.String(section, p)
}
param["fid"] = 1965350
param["htype"] = 0
stStart, err := metadata.Config.Int(section, "st_start")
if err != nil {
logger.Error(err)
}
stEnd, err := metadata.Config.Int(section, "st_end")
if err != nil {
logger.Error(err)
}
for i := stStart; i <= stEnd; i++ {
// logger.Debugf("Start UZU with Options %+v", param)
param["st"] = strconv.Itoa(i)
resp, ret := general.GeneralAction(apiEntry, metadata.Sid, param)
switch ret {
case 0:
logger.Debugf("開始挑戰天魔ID: %s, 第 %s 層", param["uzid"], param["st"])
case 2803:
logger.Debugf("無挑戰天魔ID: %s, 第 %s 層,挑戰權不足", param["uzid"], param["st"])
if autoRecover {
param := map[string]interface{}{}
param["type"] = 1
param["item_id"] = 35
_, ret := general.GeneralAction(apiRecover, metadata.Sid, param)
if ret != 0 {
logger.Error("回復挑戰權失敗")
return
}
logger.Debug("挑戰權回復成功,重新挑戰")
// doUzu(metadata, section)
i--
continue
}
case 2809:
logger.Debugf("無法挑戰天魔ID: %s, 第 %s 層", param["uzid"], param["st"])
return
default:
logger.Debugf("未知的錯誤")
logger.Info(resp)
logger.Info(ret)
return
}
// End Quest
paramResult := map[string]interface{}{}
paramResult["res"] = 1
paramResult["uzid"] = param["uzid"]
// logger.Debugf("End UZU with Options %+v", paramResult)
requestUrl := fmt.Sprintf("%s/%s", clients.HOST, apiResult)
resp, _ = utils.PostV2(
requestUrl,
"wvt=%5b%7b%22wave_num%22%3a1%2c%22time%22%3a1383%7d%2c%7b%22wave_num%22%3a2%2c%22time%22%3a2849%7d%2c%7b%22wave_num%22%3a3%2c%22time%22%3a4316%7d%2c%7b%22wave_num%22%3a4%2c%22time%22%3a6856%7d%5d&mission=%7b%22cid%22%3a%5b2282%2c295%2c7634%2c5275%2c1245%2c8194%5d%2c%22sid%22%3a%5b0%2c0%2c296%2c5014%2c8131%2c8192%5d%2c%22fid%22%3a%5b8900%5d%2c%22hrid%22%3a%5b7206%5d%2c%22ms%22%3a0%2c%22md%22%3a3057%2c%22sc%22%3a%7b%220%22%3a0%2c%221%22%3a0%2c%222%22%3a0%2c%223%22%3a0%2c%224%22%3a0%7d%2c%22es%22%3a0%2c%22at%22%3a0%2c%22he%22%3a0%2c%22da%22%3a0%2c%22ba%22%3a0%2c%22bu%22%3a0%2c%22job%22%3a%7b%220%22%3a2%2c%221%22%3a4%2c%222%22%3a0%2c%223%22%3a2%2c%224%22%3a0%7d%2c%22weapon%22%3a%7b%220%22%3a1%2c%221%22%3a0%2c%222%22%3a0%2c%223%22%3a2%2c%224%22%3a0%2c%225%22%3a3%2c%228%22%3a0%2c%229%22%3a1%2c%2210%22%3a1%7d%2c%22box%22%3a0%2c%22um%22%3a%7b%221%22%3a0%2c%222%22%3a0%2c%223%22%3a0%7d%2c%22fj%22%3a0%2c%22fw%22%3a0%2c%22fo%22%3a0%2c%22mlv%22%3a100%2c%22mbl%22%3a445%2c%22udj%22%3a0%2c%22sdmg%22%3a98973%2c%22tp%22%3a0%2c%22gma%22%3a8%2c%22gmr%22%3a4%2c%22gmp%22%3a0%2c%22stp%22%3a0%2c%22auto%22%3a1%2c%22uh%22%3a%7b%226%22%3a1%2c%224%22%3a1%2c%222%22%3a1%2c%229%22%3a1%2c%223%22%3a2%2c%225%22%3a1%2c%221%22%3a1%7d%2c%22cc%22%3a1%2c%22bf_atk%22%3a0%2c%22bf_hp%22%3a0%2c%22bf_spd%22%3a0%7d&nature=cnt%3d16b68ab30b7%26mission%3d%257b%2522cid%2522%253a%255b2282%252c295%252c7634%252c5275%252c1245%252c8194%255d%252c%2522sid%2522%253a%255b0%252c0%252c296%252c5014%252c8131%252c8192%255d%252c%2522fid%2522%253a%255b8900%255d%252c%2522hrid%2522%253a%255b7206%255d%252c%2522ms%2522%253a0%252c%2522md%2522%253a3057%252c%2522sc%2522%253a%257b%25220%2522%253a0%252c%25221%2522%253a0%252c%25222%2522%253a0%252c%25223%2522%253a0%252c%25224%2522%253a0%257d%252c%2522es%2522%253a0%252c%2522at%2522%253a0%252c%2522he%2522%253a0%252c%2522da%2522%253a0%252c%2522ba%2522%253a0%252c%2522bu%2522%253a0%252c%2522job%2522%253a%257b%25220%2522%253a2%252c%25221%2522%253a4%252c%25222%2522%253a0%252c%25223%2522%253a2%252c%25224%2522%253a0%257d%252c%2522weapon%2522%253a%257b%25220%2522%253a1%252c%25221%2522%253a0%252c%25222%2522%253a0%252c%25223%2522%253a2%252c%25224%2522%253a0%252c%25225%2522%253a3%252c%25228%2522%253a0%252c%25229%2522%253a1%252c%252210%2522%253a1%257d%252c%2522box%2522%253a0%252c%2522um%2522%253a%257b%25221%2522%253a0%252c%25222%2522%253a0%252c%25223%2522%253a0%257d%252c%2522fj%2522%253a0%252c%2522fw%2522%253a0%252c%2522fo%2522%253a0%252c%2522mlv%2522%253a100%252c%2522mbl%2522%253a445%252c%2522udj%2522%253a0%252c%2522sdmg%2522%253a98973%252c%2522tp%2522%253a0%252c%2522gma%2522%253a8%252c%2522gmr%2522%253a4%252c%2522gmp%2522%253a0%252c%2522stp%2522%253a0%252c%2522auto%2522%253a1%252c%2522uh%2522%253a%257b%25226%2522%253a1%252c%25224%2522%253a1%252c%25222%2522%253a1%252c%25229%2522%253a1%252c%25223%2522%253a2%252c%25225%2522%253a1%252c%25221%2522%253a1%257d%252c%2522cc%2522%253a1%252c%2522bf_atk%2522%253a0%252c%2522bf_hp%2522%253a0%252c%2522bf_spd%2522%253a0%257d%26res%3d1%26uzid%3d5%26wvt%3d%255b%257b%2522wave_num%2522%253a1%252c%2522time%2522%253a1383%257d%252c%257b%2522wave_num%2522%253a2%252c%2522time%2522%253a2849%257d%252c%257b%2522wave_num%2522%253a3%252c%2522time%2522%253a4316%257d%252c%257b%2522wave_num%2522%253a4%252c%2522time%2522%253a6856%257d%255d",
paramResult,
metadata.Sid)
// logger.Info(utils.Map2JsonString(resp))
res := int(resp["res"].(float64))
if res != 0 {
logger.Debug("魔神戰挑戰失敗")
logger.Debug(resp)
logger.Debug(ret)
lineReplyMessage = "魔神戰結果錯誤"
} else {
logger.Debug("魔神戰完成")
lineReplyMessage = "魔神戰完成"
}
}
}
func doSortie(metadata *clients.Metadata, section string) {
api := "sortie/entry"
param := map[string]interface{}{}
paramsRaw, _ := metadata.Config.SectionOptions(section)
for _, p := range paramsRaw {
param[p], _ = metadata.Config.String(section, p)
}
resp, ret := general.GeneralAction(api, metadata.Sid, param)
switch ret {
case 3405:
logger.Debug(resp["msg"])
case 0:
logger.Debug("完成")
default:
logger.Info(utils.Map2JsonString(resp))
}
}
func doDebug(metadata *clients.Metadata, section string) {
api, _ := metadata.Config.String(section, "API")
param := map[string]interface{}{}
paramsRaw, _ := metadata.Config.SectionOptions(section)
for _, p := range paramsRaw {
if p == "API" {
continue
}
param[p], _ = metadata.Config.String(section, p)
}
ret, _ := general.GeneralAction(api, metadata.Sid, param)
logger.Info(utils.Map2JsonString(ret))
}
func doUpdateDB(metadata *clients.Metadata, section string) {
controllers.UpdateDB(metadata.DB)
}
func getPresents(metadata *clients.Metadata, excludeTypes []string) {
if presents, res := present.GetPresnetList(metadata.Sid); res == 0 {
for _, p := range presents.Data {
if utils.InArray(p.Data.Type, excludeTypes) {
continue
}
if _, err := present.ReceievePresent(p.Idx, metadata.Sid); err == 0 {
logger.Infof("-> 接收禮物 {%+v}\n", p)
} else {
logger.Infof("-> 接收禮物失敗 {%s}, %s\n", p.Text, err)
}
}
} else {
logger.Info(res)
}
}
func doExTower(metadata *clients.Metadata, section string) {
towerInfo, err := tower.GetCurrentTowerInfo(metadata)
if err != nil {
logger.Error("Unable to get Tower Info")
logger.Error(err)
}
twid := towerInfo.Data.TowerID
logger.Debugf("Tower ID = %d", twid)
maxFloor, err := metadata.Config.Int(section, "MaxFloor")
if err != nil {
maxFloor = 10
}
maxQuest := 3
// breakFloor, _ := metadata.Config.Int(section, "Floor")
// breakQuest, _ := metadata.Config.Int(section, "Quest")
// tower.AddTicket(metadata, twid, 0, 1)
// seems start from 2
for floorIndex := 2; floorIndex <= maxFloor; floorIndex++ {
for questIndex := 1; questIndex <= maxQuest; questIndex++ {
// if floorIndex == breakFloor && questIndex > breakQuest {
// return
// }
logger.Infof("開始年代記之塔-黃昏之間 %d-%d\n", floorIndex, questIndex)
// set team 8, 9, 10 to fit requirement on your own
pt := questIndex + 7
// tower.AddTicket(metadata, twid, 0, 1)
resp, res := tower.EnterExTower(metadata, twid, floorIndex-1, questIndex-1, pt-1)
switch res {
case 0:
case 504:
logger.Info("成員不符規定")
return
case 3305:
logger.Info("無法進行的關卡")
logger.Info(resp)
continue
case 3313:
logger.Info("重覆卡牌")
logger.Info(resp)
return
case 3317:
logger.Info("未知的關樓層,trying next..")
continue
case 3303:
logger.Infof("已完成年代的記之塔-黃昏之間: %d-%d", floorIndex, questIndex)
return
default:
logger.Info(resp)
return
}
resp, res = tower.ExitExTower(metadata, twid, 4)
switch res {
case 0:
logger.Infof("完成年代記之塔-黃昏之間 %d-%d\n", floorIndex, questIndex)
default:
logger.Debug(res)
logger.Info(resp)
return
}
}
}
}
func doTower(metadata *clients.Metadata, section string) {
towerInfo, err := tower.GetCurrentTowerInfo(metadata)
if err != nil {
logger.Error("Unable to get Tower Info")
logger.Error(err)
}
twid := towerInfo.Data.TowerID
logger.Debugf("Tower ID = %d", twid)
autoRecover, err := metadata.Config.Bool(section, "AutoRecover")
if err != nil {
autoRecover = false
}
maxFloor, err := metadata.Config.Int(section, "MaxFloor")
if err != nil {
maxFloor = 10
}
logger.Debug(maxFloor)
maxQuest := 3
// breakFloor, _ := metadata.Config.Int(section, "Floor")
// breakQuest, _ := metadata.Config.Int(section, "Quest")
// tower.AddTicket(metadata, twid, 0, 1)
for floorIndex := 1; floorIndex <= maxFloor; floorIndex++ {
for questIndex := 1; questIndex <= maxQuest; questIndex++ {
// if floorIndex == breakFloor && questIndex > breakQuest {
// return
// }
logger.Infof("開始年代記之塔 %d-%d\n", floorIndex, questIndex)
pt := questIndex
// tower.AddTicket(metadata, twid, 0, 1)
resp, res := tower.EnterTower(metadata, twid, floorIndex-1, questIndex-1, pt-1)
switch res {
case 0:
// logger.Info("Enter tower success")
case 3312:
// no key
// logger.Info(resp)
logger.Info("年代記挑戰權不足")
if autoRecover {
if resp, err := tower.AddTicket(metadata, twid, 1, 40); err != 0 {
logger.Info("回復失敗, 離開")
logger.Info(resp)
return
}