-
Notifications
You must be signed in to change notification settings - Fork 7
/
MyPetBattle.lua
1274 lines (1102 loc) · 54.2 KB
/
MyPetBattle.lua
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
-----------------
-- MyPetBattle --
-----------------
MyPetBattle = {}
---------------
---- SETUP ----
---------------
-- INITIALIZATION -
mypetbattle_enabled = false
mypetbattle_join_pvp = false
mypetbattle_auto_forfeit = false
mypetbattle_wintrade_enabled = false
MPB_STATS_TABLE = {}
--[
MyPetBattleState = {}
--[[
Notes:
The _AGAIN states are repeated by the one second loop. They are started from the immediate one-time state of the same name.
The flags cover a range of states.
--]]
MyPetBattleState.SET_TEAM_START = 200 -- begin setting team
MyPetBattleState.FLAG_SET_TEAM_IN_PROGRESS = false
MyPetBattleState.TRANSFER_PET_LIST = 201 -- send/receive pet list : send our pet list first time
MyPetBattleState.TRANSFER_PET_LIST_AGAIN = 202 -- send/receive pet list : send our pet list every second
MyPetBattleState.RECEIVED_PET_LIST_QUEUED = 203 -- received pet list while queued : send pet list with acknowledge first time
MyPetBattleState.RECEIVED_PET_LIST = 204 -- received pet list : send pet list with acknowledge first time
MyPetBattleState.RECEIVED_PET_LIST_AGAIN = 205 -- received pet list : send pet list with acknowledge every second
MyPetBattleState.RECEIVED_PET_LIST_ACK = 206 -- received pet list with ack : time to set the team
MyPetBattleState.SET_TEAM = 207 -- finish setting team : set the team
MyPetBattleState.SET_TEAM_IN_PROGRESS = 208
MyPetBattleState.SET_TEAM_AGAIN = 209 -- retry setting team : this may occur once during login if it's too early to set the team
MyPetBattleState.SET_TEAM_DONE = 210
MyPetBattleState.BATTLE_OPENED = 400 -- battle opened : set at PET_BATTLE_OPENING_DONE
MyPetBattleState.FLAG_BATTLE_IN_PROGRESS = false
MyPetBattleState.BATTLE_IN_PROGRESS = 500 -- check forfeit : during this time, forfeit may occur - go to battle close state
MyPetBattleState.FLAG_ALLOW_FORFEIT = false
MyPetBattleState.BATTLE_FORFEIT_DONE = 501 -- forfeit timer gone : do nothing
MyPetBattleState.BATTLE_CLOSED = 600 -- battle closed : set at PET_BATTLE_CLOSE
MyPetBattleState.BATTLE_CLOSED_IN_PROGRESS = 601 -- : wait 5 seconds after closing
MyPetBattleState.BATTLE_CLOSED_DONE = 602 -- battle closed 2 : do nothing
MyPetBattleVars = {}
MyPetBattleVars.state = 0
local MPB_onUpdate
function MyPetBattle.setState(state, immediate)
if mypetbattle_debug then print("|cffff8000MPB|r: MyPetBattle.setState", state, immediate, self) end
if not state then
print("|cffff8000MPB|r: invalid state")
else
MyPetBattleVars.state = state
if immediate then
MPB_onUpdate(self,0)
end
end
end
--]
--[
MyPetBattleMatchPos = {}
-- match message character position constants:
MyPetBattleMatchPos.MATCH = 1 -- m=match
MyPetBattleMatchPos.ACK = 2 -- a=acknowledge
MyPetBattleMatchPos.FORFEIT = 3 -- f=forfeit
MyPetBattleMatchPos.LEADER = 4 -- L=Leader (leader of the party UnitIsGroupLeader("player"))
MyPetBattleMatchPos.SORT_METHOD = 5 -- 1=MPB.SORT_METHOD
MyPetBattleMatchPos.USE_NON_RARE = 6 -- 1=MPB.USE_NON_RARE
MyPetBattleMatchPos.SLOT_1_LOCK = 8 -- ##=slot 1 locked level
MyPetBattleMatchPos.SLOT_2_LOCK = 10 -- ##=slot 2 locked level
MyPetBattleMatchPos.SLOT_3_LOCK = 12 -- ##=slot 3 locked level
MyPetBattleMatchPos.START_LEVEL = 15 -- ##=levelRange.start
MyPetBattleMatchPos.LEVEL_STEP = 17 -- ?=level_step +/-
MyPetBattleMatchPos.MIN_KEEP = 18 -- #=number to keep of each level
MyPetBattleMatchPos.LEVELS = 20 -- ###=count (0-9) of pets for each level (1-25)
-- #=total count for a level
-- #=useable non_rare count
-- #=useable rare count
MyPetBattleMatchPos.JOIN_PVP = 95 -- p=join PvP
--
-- mafL10 121200 24-1 111222333444555666777888999000111222333444555666777888999000111222333444555p
-- m...01 000025 01+3 111222333444555666777888999000111222333444555666777888999000111222333444555.
--]
RegisterAddonMessagePrefix("MPB")
--------------------
---- SETUP DONE ----
--------------------
name, speciesName = C_PetBattles.GetName(LE_BATTLE_PET_ALLY, currentPetID)
---------------------
-- EVENTS HANDLING --
---------------------
local mypetbattle_frame, events = CreateFrame("Frame"), {};
--------------------------------------------------------
-- LOAD A LOT OF STUFF WHEN THE ADDON HAS BEEN LOADED --
function events:ADDON_LOADED(...)
-- print("ADDON_LOADED", ...)
local addonName = ...
if addonName == "MyPetBattle" then
---------------------------------------------------------------
-- SET SAVEDVARIABLES TO DEFAULT VALUES IF THEY DO NOT EXIST --
-- TEAM SETUP OPTIONS
if MPB_CONFIG_TEAMSETUP_RANDOM_TEAM_PET_HEALTH_THRESHOLD == nil then
MPB_CONFIG_TEAMSETUP_RANDOM_TEAM_PET_HEALTH_THRESHOLD = 0.80
end
if MPB_CONFIG_TEAMSETUP_PET1_LEVEL_ADJUSTMENT == nil then
MPB_CONFIG_TEAMSETUP_PET1_LEVEL_ADJUSTMENT = -2
end
if MPB_CONFIG_TEAMSETUP_PET2_LEVEL_ADJUSTMENT == nil then
MPB_CONFIG_TEAMSETUP_PET2_LEVEL_ADJUSTMENT = -2
end
if MPB_CONFIG_TEAMSETUP_PET3_LEVEL_ADJUSTMENT == nil then
MPB_CONFIG_TEAMSETUP_PET3_LEVEL_ADJUSTMENT = 2
end
-- PRE-COMBAT OPTIONS
if MPB_CONFIG_PRE_COMBAT_ATTEMPT_STRONGEST_RANDOM_TEAM == nil then
MPB_CONFIG_PRE_COMBAT_ATTEMPT_STRONGEST_RANDOM_TEAM = false
end
-- COMBAT OPTIONS
if MPB_CONFIG_COMBAT_SWAP_PET_HEALTH_THRESHOLD == nil then
MPB_CONFIG_COMBAT_SWAP_PET_HEALTH_THRESHOLD = 0.35
end
-- POST-COMBAT OPTIONS
if MPB_CONFIG_POST_COMBAT_USE_REVIVE_BATTLE_PETS_AFTER_COMBAT == nil then
MPB_CONFIG_POST_COMBAT_USE_REVIVE_BATTLE_PETS_AFTER_COMBAT = false
end
if not MPB_CONFIG_POST_COMBAT_USE_BATTLE_PET_BANDAGE_AFTER_COMBAT == nil then
MPB_CONFIG_POST_COMBAT_USE_BATTLE_PET_BANDAGE_AFTER_COMBAT = false
end
if not MPB_CONFIG_POST_COMBAT_AUTOMATIC_NEW_RANDOM_TEAM_AFTER_COMBAT == nil then
MPB_CONFIG_POST_COMBAT_AUTOMATIC_NEW_RANDOM_TEAM_AFTER_COMBAT = false
end
-- MISC OPTIONS
if MPB_CONFIG_MISC_AUTOMATIC_RELEASE_NON_RARES == nil then
MPB_CONFIG_MISC_AUTOMATIC_RELEASE_NON_RARES = false
end
-- CAPTURE PETS OPTIONS
if MPB_CAPTURE_RARES == nil then
MPB_CAPTURE_RARES = false
end
if MPB_CAPTURE_COMMON_UNCOMMON == nil then
MPB_CAPTURE_COMMON_UNCOMMON = false
end
-- TEAM PET LEVEL
if MPB_EDITBOX_DESIRED_PET_LEVEL == nil then
MPB_EDITBOX_DESIRED_PET_LEVEL = 1
end
-- FORFEIT TIMER
if MPB_FORFEIT_TIMER == nil then
MPB_FORFEIT_TIMER = 55
end
if MPB == nil then
MPB = {}
end
if MPB.USE_NON_RARE == nil then
-- Set this to true if you want to allow using non-rare pets. Higher quality pets are still given priority for the same level.
MPB.USE_NON_RARE = false
end
if MPB.SORT_METHOD == nil then
MPB.SORT_METHOD = MPB.PRIORITIZE_LEVEL and 1 or 0
-- 0: pets chosen first by rarity and second by level (within the selected allowable level range).
-- 1: pets chosen first by level difference and second by rarity.
-- 2: pets chosen first by level and second by level difference and third by rarity.
end
MPB.PRIORITIZE_LEVEL = nil -- replaced by MPB.SORT_METHOD
if MPB.RANDOMIZE == nil then
-- Set this to true if you want pets to be chosen randomly instead of name.
MPB.RANDOMIZE = false
end
if MPB.KEEP_MINIMUM_NUM_OF_A_LEVEL == nil then
-- This is a number between 0 and 9. The addon won't choose a pet of a level if you plan to win (are not forfeiting) if you only have this number of pets of that level.
-- 0 means you don't care about keeping pets at each level. However, if you don't have pets at a level, then you can't win-trade with a player that only has pets at that level.
MPB.KEEP_MINIMUM_NUM_OF_A_LEVEL = 0
end
if MPB.EQUIP_SAFARI_HAT == nil then
-- If this is enabled, then the Safari Hat is equipped when the battle starts and is unequipped when the battle ends so you get bonus XP for your pets.
-- If you have an heirloom helmet then you also get bonus XP for your character since the player XP is awarded after the Safari Hat is switched to the heirloom helmet.
MPB.EQUIP_SAFARI_HAT = true
end
if MPB.MATCH_PET_LEVELS_DURING_WT == nil then
-- If this is enabled, and win-trading is also enabled, then this will cause pet levels to be matched with the other player's pet levels
MPB.MATCH_PET_LEVELS_DURING_WT = false
end
--[
-- Note that the player that is not auto-forfeiting actually controls the following settings. If both players are not auto-forfeiting then the leader controls the settings.
if MPB.MATCH_PET_LEVELS_START_LEVEL == nil then
-- The level that you want to start leveling pets from.
MPB.MATCH_PET_LEVELS_START_LEVEL = 24
end
if MPB.MATCH_PET_LEVELS_LEVEL_STEP == nil then
-- This is either -1 or +1. Use -1 if you want to work down the levels starting from the start level. Use +1 if you want to work up the levels instead.
MPB.MATCH_PET_LEVELS_LEVEL_STEP = -1
end
--]
---------------------------------------
-- SET UI ELEMENTS TO SAVEDVARIABLES --
-- TEAM SETUP
EditBox_min_team_pet_health:SetText(tostring(MPB_CONFIG_TEAMSETUP_RANDOM_TEAM_PET_HEALTH_THRESHOLD * 100))
Slider_pet1_level:SetValue(MPB_CONFIG_TEAMSETUP_PET1_LEVEL_ADJUSTMENT)
Slider_pet2_level:SetValue(MPB_CONFIG_TEAMSETUP_PET2_LEVEL_ADJUSTMENT)
Slider_pet3_level:SetValue(MPB_CONFIG_TEAMSETUP_PET3_LEVEL_ADJUSTMENT)
CheckButtonIncludeOnlyRare:SetChecked(not MPB.USE_NON_RARE)
DropDownSortMethod_Init(DropDownSortMethod)
CheckButtonRandomize:SetChecked(MPB.RANDOMIZE)
EditBoxLevelKeepMinimum:SetText(MPB.KEEP_MINIMUM_NUM_OF_A_LEVEL)
-- PRE-COMBAT
CheckButton15:SetChecked(MPB_CONFIG_PRE_COMBAT_ATTEMPT_STRONGEST_RANDOM_TEAM)
-- COMBAT
EditBox_swap_pet_health_threshold:SetText(tostring(MPB_CONFIG_COMBAT_SWAP_PET_HEALTH_THRESHOLD * 100))
CheckButtonEquipSafariHat:SetChecked(MPB.EQUIP_SAFARI_HAT)
-- POST-COMBAT
CheckButton12:SetChecked(MPB_CONFIG_POST_COMBAT_USE_REVIVE_BATTLE_PETS_AFTER_COMBAT)
CheckButton14:SetChecked(MPB_CONFIG_POST_COMBAT_USE_BATTLE_PET_BANDAGE_AFTER_COMBAT)
CheckButton11:SetChecked(MPB_CONFIG_POST_COMBAT_AUTOMATIC_NEW_RANDOM_TEAM_AFTER_COMBAT)
-- MISC
CheckButton13:SetChecked(MPB_CONFIG_MISC_AUTOMATIC_RELEASE_NON_RARES)
-- LOAD LAST USED DESIRED_PET_LEVEL FOR THE RANDOM TEAM GENERATION
local loadLastUsedDesiredPetLevel = MPB_EDITBOX_DESIRED_PET_LEVEL
EditBox_PetLevel:SetText(loadLastUsedDesiredPetLevel)
-- LOCK PETS FOR RANDOM TEAM GENERATION
Check_lock_pet_1:SetChecked(MPB_LOCK_PET1)
Check_lock_pet_2:SetChecked(MPB_LOCK_PET2)
Check_lock_pet_3:SetChecked(MPB_LOCK_PET3)
-- SET TEXTURE FOR CONFIG BUTTON (GEAR) MANUALLY AS THE XML FILE DO NOT WANT DO WHAT I WANT!
MPB_Config_Button:SetNormalTexture("Interface\\Addons\\MyPetBattle\\Images\\icon-config")
MPB_Config_Button:SetHighlightTexture("Interface\\Buttons\\UI-Panel-MinimizeButton-Highlight.blp")
-- SET CAPTURE RARE/UNCOMMON/COMMON CHECKBOXES
CheckButton3:SetChecked(MPB_CAPTURE_RARES)
CheckButton4:SetChecked(MPB_CAPTURE_COMMON_UNCOMMON)
-- SET FORFEIT TIMER
EditBox_ForfeitTimer:SetText(MPB_FORFEIT_TIMER)
-- LOAD WT MATCH LEVELS OPTIONS
CheckButton_wt_match_levels:SetChecked(MPB.MATCH_PET_LEVELS_DURING_WT)
EditBox_MatchLevel:SetText(MPB.MATCH_PET_LEVELS_START_LEVEL)
EditBox_Direction:SetText((MPB.MATCH_PET_LEVELS_LEVEL_STEP > 0) and "+1" or "-1")
-- CALL FUNCTION IN Frame.lua TO UPDATE THE POSITION OF THE MINIMAP BUTTON
MPB_MMButton_UpdatePosition()
-- SHOW/HIDE UI AT LOGIN ACCORDING TO CHARACTER SPECIFIC SAVEDVARIABLE
if MPB_SHOW_UI == nil then MPB_SHOW_UI = true end -- Set variable to true the first time
if MPB_SHOW_UI then MyPetBattleForm:Show() else MyPetBattleForm:Hide() end
-- PRINT THAT WE ARE DONE LOADING
print("Loaded |cffff8000MPB")
end
end
function events:PLAYER_LOGIN(...) --
-- print("PLAYER_LOGIN")
print("\124cFFFF9933My Pet Battle ready for action")
-- Set default text of textfield "PetLevel" for random team generation
end
function events:PLAYER_ENTERING_WORLD(...) --
-- mypetbattle_debug = true
if mypetbattle_debug then print("PLAYER_ENTERING_WORLD", ...) end
end
function events:PET_BATTLE_ABILITY_CHANGED(...) --
-- print("PET_BATTLE_ABILITY_CHANGED")
end
function events:PET_BATTLE_ACTION_SELECTED(...) -- Player selected a pet action
-- print("PET_BATTLE_ACTION_SELECTED")
end
function events:PET_BATTLE_AURA_APPLIED(...) -- Aura applied e.g. Adrenaline for 3 turn (3 stacks)
-- print("PET_BATTLE_AURA_APPLIED")
end
function events:PET_BATTLE_AURA_CANCELED(...) --
-- print("PET_BATTLE_AURA_CANCELED")
end
function events:PET_BATTLE_AURA_CHANGED(...) -- Aura changed e.g. Adrenaline changed to 2 stacks
-- print("PET_BATTLE_AURA_CHANGED")
end
function events:PET_BATTLE_CAPTURED(...) --
-- print("PET_BATTLE_CAPTURED")
-- print(...)
local petOwner, petIndex = ...
if (petOwner == LE_BATTLE_PET_ENEMY) then
local name, speciesName = C_PetBattles.GetName(petOwner, petIndex)
-- local speciesId, petGUID = C_PetJournal.FindPetIDByName(speciesName)
-- local link = C_PetJournal.GetBattlePetLink(petGUID)
-- local petName = C_PetBattles.GetName(petOwner, petIndex);
-- local petIcon = C_PetBattles.GetIcon(petOwner, petIndex);
-- local quality = C_PetBattles.GetBreedQuality(petOwner, petIndex);
-- ADD STATS TO STATS TABLE
MyPetBattle.addStats(speciesName,1)
end
end
function events:PET_BATTLE_CLOSE(...) --
if mypetbattle_debug then print("PET_BATTLE_CLOSE", ...) end
if MyPetBattleState.FLAG_BATTLE_IN_PROGRESS then -- don't want to do this twice so check this flag
MyPetBattle.setState(MyPetBattleState.BATTLE_CLOSED, true)
end
end
function events:PET_BATTLE_FINAL_ROUND(...) --
if mypetbattle_debug then print("PET_BATTLE_FINAL_ROUND", ...) end
if mypetbattle_debug then
-- SHOWS THE SAME RESULT ALL THE TIME FOR PVP BATTLES REGARDLESS OF WHO WINS?!?!
-- This only works if there was no forfeit. I think the parameter only indicates who performed the final action?
if ... == LE_BATTLE_PET_ALLY then
print("We won!")
-- ADD STATS TO STATS TABLE
MyPetBattle.addStats("Wins",1)
elseif ... == LE_BATTLE_PET_ENEMY then
print("We lost!")
-- ADD STATS TO STATS TABLE
MyPetBattle.addStats("Losses",1)
else
print("Not LE_BATTLE_PET_ALLY or LE_BATTLE_PET_ENEMY won?!")
end
end
end
function events:PET_BATTLE_HEALTH_CHANGED(...) --
-- petOwner, petIndex, healthChange
-- local myOutput1, myOutput2, myOutput3 = ...;
-- print("OUTPUT: " .. myOutput1 .. ", " .. myOutput2 .. ", " .. myOutput3)
-- print("PET_BATTLE_HEALTH_CHANGED")
local petOwner, petIndex, healthChange = ...;
local pet_name, pet_speciesName = C_PetBattles.GetName(petOwner, petIndex)
local pet_icon = C_PetBattles.GetIcon(petOwner, petIndex)
local pet_currentHealth = C_PetBattles.GetHealth(petOwner, petIndex)
local pet_maxHealth = C_PetBattles.GetMaxHealth(petOwner, petIndex)
local pet_healthPercentage = pet_currentHealth / pet_maxHealth * 100
local textColor
if pet_healthPercentage < 30 then
textColor = "\124cFFFF0000" -- red
elseif pet_healthPercentage < 75 then
textColor = "\124cFFFFFF00" -- yellow
elseif pet_healthPercentage <= 100 then
textColor = "\124cFF00FF00" -- green
end
local textPetOwner
if petOwner == 1 then
textPetOwner = "Player: "
else
textPetOwner = "Enemy: "
end
if mypetbattle_debug then
if healthChange > 0 then
-- print("gained " .. healthChange .. " health")
print(textPetOwner.."\124T"..pet_icon..":0\124t |cFF0066FF[" .. pet_name .. "]|r gained \124cFF00FF00" .. healthChange .. "\124r health. Current health: " .. textColor .. pet_currentHealth .. "/" .. pet_maxHealth .. string.format(" (%2.0f%%)", pet_healthPercentage))
elseif healthChange < 0 then
-- print("lost " .. abs(healthChange) .. " health")
print(textPetOwner.."\124T"..pet_icon..":0\124t |cFF0066FF[" .. pet_name .. "]|r lost \124cFFFF0000" .. abs(healthChange) .. "\124r health. Current health: " .. textColor .. pet_currentHealth .. "/" .. pet_maxHealth .. string.format(" (%2.0f%%)", pet_healthPercentage))
end
end
-- print(textPetOwner.."\124T"..pet_icon..":0\124t |cFF0066FF[" .. pet_name .. "]|r current health: " .. textColor .. pet_currentHealth .. "/" .. pet_maxHealth .. string.format(" (%2.0f%%)", pet_healthPercentage) .. " point")
-- print("Pet number "..petIndex .. " health is: " .. C_PetBattles.GetHealth(petOwner, petIndex))
currentPetID = C_PetBattles.GetActivePet(LE_BATTLE_PET_ALLY)
currentPet_Health = C_PetBattles.GetHealth(LE_BATTLE_PET_ALLY, currentPetID)
currentPet_MAX_Health = C_PetBattles.GetMaxHealth(LE_BATTLE_PET_ALLY, currentPetID)
currentPet_Health_percentage = currentPet_Health / currentPet_MAX_Health * 100
-- currentPetID = C_PetBattles.GetActivePet(LE_BATTLE_PET_ALLY)
-- currentPet_Health = C_PetBattles.GetHealth(LE_BATTLE_PET_ALLY, currentPetID)
-- currentPet_MAX_Health = C_PetBattles.GetMaxHealth(LE_BATTLE_PET_ALLY, currentPetID)
-- currentPet_Health_percentage = currentPet_Health / currentPet_MAX_Health * 100
-- print("|cff00ff00 Current pets health: " .. currentPet_Health .. "/" .. currentPet_MAX_Health .. string.format(" (%2.0f%%)", currentPet_Health_percentage) .. " points")
end
function events:PET_BATTLE_LEVEL_CHANGED(...) --
if mypetbattle_debug then print("PET_BATTLE_LEVEL_CHANGED", ...) end
local petOwner, petIndex, newLevel = ...
if petOwner == LE_BATTLE_PET_ALLY then
local pet_name, pet_speciesName = C_PetBattles.GetName(petOwner, petIndex)
local pet_icon = C_PetBattles.GetIcon(petOwner, petIndex)
local pet_level = C_PetBattles.GetLevel(petOwner, petIndex)
MyPetBattle.setPetLevel(petIndex, newLevel, pet_level)
print("|cffff8000MPB|r: |cFF0066FF\124T"..pet_icon..":16\124t [" .. pet_name .. "]\124r is now level: " .. pet_level .."!")
end
end
function events:PET_BATTLE_LOOT_RECEIVED(...) --
-- print("PET_BATTLE_LOOT_RECEIVED")
local typeIdentifier, itemLink, quantity = ...;
print("|cffff8000MPB|r: \124cFF00FF00Item won:\124r " .. quantity .. " x " .. itemLink)
if ( typeIdentifier == "item" ) then
local itemName, itemLink, _, _, _, _, _, _, _, itemIcon, _ = GetItemInfo(itemLink);
-- local itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, itemSubType, itemStackCount, itemEquipLoc, itemTexture, itemSellPrice = GetItemInfo(itemLink);
elseif ( typeIdentifier == "currency" ) then
local itemName, _, itemIcon, _, _, _, _, _ = GetCurrencyInfo(itemLink);
-- local currencyName, currencyQuantity, currencyIcon, earnedThisWeek, weeklyMax, maxQuantity, discovered, rarity = GetCurrencyInfo(itemLink);
end
-- ADD STATS TO STATS TABLE
MyPetBattle.addStats(itemLink,quantity)
end
function MyPetBattle.addStats(key,value)
-- Loot received
-- items
-- currency
-- Pets caught: common, uncommon, rare, type etc.
-- Pets fought
-- PvP win/loose, streak
-- XP from WT and other
if MPB_STATS_TABLE[key] ~= nil then
MPB_STATS_TABLE[key] = MPB_STATS_TABLE[key] + value
else
MPB_STATS_TABLE[key] = value
end
end
function MyPetBattle.printStats()
-- PRINT STATISTICS OF OUR BATTLES
print("|cffff8000MPB|r: |cffff8000MPB stats for current session:|r")
for key,value in pairs(MPB_STATS_TABLE) do
print(" - "..value.."x",key)
end
end
function events:PET_BATTLE_MAX_HEALTH_CHANGED(...) --
-- print("PET_BATTLE_MAX_HEALTH_CHANGED")
end
function events:PET_BATTLE_OPENING_DONE(...) -- OPENING DONE AND READY TO BATTLE
if mypetbattle_debug then print("PET_BATTLE_OPENING_DONE", ...) end
print("|cffff8000MPB|r: Opening done")
MyPetBattle.setState(MyPetBattleState.BATTLE_OPENED, true)
end
function events:PET_BATTLE_OPENING_START(...) --
if mypetbattle_debug then print("PET_BATTLE_OPENING_START", ...) end
print("|cffff8000MPB|r: |cFF00FFFFGame Starting!")
player_level_start = UnitLevel("player")
player_xp_max_start = UnitXPMax("player")
-- Get pets level
pet1_level_start = C_PetBattles.GetLevel(LE_BATTLE_PET_ALLY,1)
pet2_level_start = C_PetBattles.GetLevel(LE_BATTLE_PET_ALLY,2)
pet3_level_start = C_PetBattles.GetLevel(LE_BATTLE_PET_ALLY,3)
pet1_xp_gained = 0
pet2_xp_gained = 0
pet3_xp_gained = 0
--[[
-- Get pets current xp
pet1_xp_start, pet1_maxXp_start = C_PetBattles.GetXP(1, 1)
pet2_xp_start, pet2_maxXp_start = C_PetBattles.GetXP(1, 2)
pet3_xp_start, pet3_maxXp_start = C_PetBattles.GetXP(1, 3)
--]]
end
function events:PET_BATTLE_OVER(...) -- Pet battle over (someone won)
if mypetbattle_debug then print("PET_BATTLE_OVER", ...) end
-- Get pet names
pet1_name, pet1_speciesName = C_PetBattles.GetName(1, 1)
pet2_name, pet2_speciesName = C_PetBattles.GetName(1, 2)
pet3_name, pet3_speciesName = C_PetBattles.GetName(1, 3)
-- Get pet icons
pet1_icon = C_PetBattles.GetIcon(1, 1)
pet2_icon = C_PetBattles.GetIcon(1, 2)
pet3_icon = C_PetBattles.GetIcon(1, 3)
--[[
-- Get xp after combat has ended
pet1_xp_end, pet1_maxXp_end = C_PetBattles.GetXP(1, 1)
pet2_xp_end, pet2_maxXp_end = C_PetBattles.GetXP(1, 2)
pet3_xp_end, pet3_maxXp_end = C_PetBattles.GetXP(1, 3)
-- Calculate xp gain -- This calculation is wrong if the pet gained a level!
pet1_xp_gained = pet1_xp_end - pet1_xp_start
pet2_xp_gained = pet2_xp_end - pet2_xp_start
pet3_xp_gained = pet3_xp_end - pet3_xp_start
--]]
-- Print xp change
if pet1_xp_gained > 0 then print("|cffff8000MPB|r: |cFF0066FF\124T"..pet1_icon..":0\124t [" .. pet1_name .. "] |cFFFFFFFFgained " .. pet1_xp_gained .. " xp") end
if pet2_xp_gained > 0 then print("|cffff8000MPB|r: |cFF0066FF\124T"..pet2_icon..":0\124t [" .. pet2_name .. "] |cFFFFFFFFgained " .. pet2_xp_gained .. " xp") end
if pet3_xp_gained > 0 then print("|cffff8000MPB|r: |cFF0066FF\124T"..pet3_icon..":0\124t [" .. pet3_name .. "] |cFFFFFFFFgained " .. pet3_xp_gained .. " xp") end
print("|cffff8000MPB|r: |cFF00FFFFGame Over!")
end
local needFirstSetTeam = true
function events:UPDATE_SUMMONPETS_ACTION(...) --
if mypetbattle_debug then print("UPDATE_SUMMONPETS_ACTION", ...) end
-- After battle, only one team gets this event so it's not useful if you want for each battle a random team or a team that matches the levels of the enemy team.
-- The event occurs at startup after the PetJournal is properly loaded.
if needFirstSetTeam then
MyPetBattle.setState(MyPetBattleState.SET_TEAM_START, false)
needFirstSetTeam = nil
end
end
function events:COMPANION_UPDATE(...) --
-- print("COMPANION_UPDATE")
end
function events:PET_JOURNAL_LIST_UPDATE(...)
-- print(PET_JOURNAL_LIST_UPDATE)
end
function events:PET_BATTLE_PET_CHANGED(...) --
-- print("PET_BATTLE_PET_CHANGED")
end
function events:PET_BATTLE_PET_ROUND_PLAYBACK_COMPLETE(...) --
if mypetbattle_debug then print("PET_BATTLE_PET_ROUND_PLAYBACK_COMPLETE", ...) end
local round = ...
local roundstring = "|cffff8000MPB|r: Round " .. (round + 1) .. " - "
local doMyPetBattle = mypetbattle_enabled -- or (mypetbattle_wintrade_enabled and round <= math.random(0,2)) -- ATTACK A RANDOM NUMBER OF TIMES BETWEEN 1-3 IF WE ARE DOING WT (0 AND 2 SINCE ROUND 0 EXISTS)
if doMyPetBattle then
-------------------------------------
-- AUTO-SELECT FIRST AVAILABLE PET --
if C_PetBattles.ShouldShowPetSelect() == true then
for i=1,3 do
if MyPetBattle.hp(i) > 0 then
C_PetBattles.ChangePet(i)
print(roundstring .. "|cFFFF3300Changing pet!")
return
end
end
end
end
-------------------------------
-- GET PET OWNER AND PET INFO --
local petOwner = LE_BATTLE_PET_ALLY
local petIndex = C_PetBattles.GetActivePet(petOwner)
local petType = C_PetBattles.GetPetType(petOwner, petIndex)
local petLevel = C_PetBattles.GetLevel(petOwner, petIndex)
local petHealth = C_PetBattles.GetHealth(petOwner, petIndex)
local petMaxHealth = C_PetBattles.GetMaxHealth(petOwner, petIndex)
-------------------------------
-- GET ENEMY PET OWNER AND PET INFO --
local petOwnerEnemy = LE_BATTLE_PET_ENEMY
local petIndexEnemy = C_PetBattles.GetActivePet(petOwnerEnemy)
local petTypeEnemy = C_PetBattles.GetPetType(petOwnerEnemy, petIndexEnemy)
local petLevelEnemy = C_PetBattles.GetLevel(petOwnerEnemy, petIndexEnemy)
local numAlivePetsEnemy = 0
for j=1,3 do
if (C_PetBattles.GetHealth(petOwnerEnemy, j) or 0) > 0 then
numAlivePetsEnemy = numAlivePetsEnemy + 1
end
end
if doMyPetBattle then
-----------------------------------------------------------------------------------------
-- SKIP TURN IF POLYMORPHED, STUNNED ETC. BY ENEMY (MAYBE CHANGE PET IF LOW ON HEALTH) --
if MyPetBattle.buff("Polymorphed") or MyPetBattle.buff("Asleep") or MyPetBattle.buff("Crystal Prison") or MyPetBattle.buff("Stunned") or MyPetBattle.buff("Drowsy") then
C_PetBattles.SkipTurn()
return
end
--------------------------------------------------------------------------------
-- SWITCH PET AT HEALTH THRESHOLD BEFORE IT DIES. CAN BE SET FROM CONFIG MENU --
if MyPetBattle.hp(petIndex) < MPB_CONFIG_COMBAT_SWAP_PET_HEALTH_THRESHOLD then
for j=1,3 do
if MyPetBattle.hp(j) >= MPB_CONFIG_COMBAT_SWAP_PET_HEALTH_THRESHOLD then
C_PetBattles.ChangePet(j)
print(roundstring .. "|cFFFF3300Changing pet due to low health!")
return
end
end
end
------------------------------------------------------------------
-- CHECK IF WE SHOULD AND CAN CAPTURE RARE PETS WE ARE FIGHTING --
-- THE FUNCTION canCaptureRare() IN MyPetBattle_data.lua WILL CHECK IF WE CAN CAPTURE THE RARE WE ARE FIGHTING
if MPB_CAPTURE_RARES and MyPetBattle.canCaptureRare() and mypetbattle_enabled then
print(roundstring .. "|cFF8A2BE2We are trying to capture a rare!")
C_PetBattles.UseTrap() -- USE THE TRAP
return
end
-------------------------------------------------------------------------
-- CHECK IF WE SHOULD CAPTURE COMMON/UNCOMMON IF WE DO NOT OWN THE PET --
if MPB_CAPTURE_COMMON_UNCOMMON and MyPetBattle.canCaptureCommon() and mypetbattle_enabled then
print(roundstring .. "|cFF00FF00We do not have this pet (0/3), let us capture it (common/uncommon)!")
C_PetBattles.UseTrap() -- USE THE TRAP
return
end
-------------------------------------------
-- CHECK IF WE SHOULD LET THE ENEMY LIVE --
-- Forfeiter has levels 25,8,8. The winner has levels 8,8,25. If we are the forfeiter then we don't want our level 25 to kill the other's low level pets.
if mypetbattle_wintrade_enabled and mypetbattle_auto_forfeit then
if petLevel > petLevelEnemy + 3 or numAlivePetsEnemy <= 1 then
if petHealth < petMaxHealth then
for j=1,3 do
if j ~= petIndex and MyPetBattle.hp(j) >= MPB_CONFIG_COMBAT_SWAP_PET_HEALTH_THRESHOLD then
C_PetBattles.ChangePet(j)
print(roundstring .. "|cFFFF3300Changing pet due to enemy low level!")
return
end
end
end
print(roundstring .. "|cFFFF3300Skipping turn due to enemy low level!")
C_PetBattles.SkipTurn()
return
end
end
end
-----------------------------
-- GETTING READY TO ATTACK --
local spell = nil
if petType == 1 then -- HUMANOID
spell = humanoid()
elseif petType == 2 then -- DRAGONKIN
spell = dragonkin()
elseif petType == 3 then -- FLYING
spell = flying()
elseif petType == 4 then -- UNDEAD
spell = undead()
elseif petType == 5 then -- CRITTER
spell = critter()
elseif petType == 6 then -- MAGIC
spell = magic()
elseif petType == 7 then -- ELEMENTAL
spell = elemental()
elseif petType == 8 then -- BEAST
spell = beast()
elseif petType == 9 then -- WATER / AQUATIC
spell = aquatic()
elseif petType == 10 then -- MECHANICAL
spell = mechanical()
end
local actionIndex = MyPetBattle.getSpellSlotIndex(spell)
local spellID, spellName, spellIcon, _, _, _, _, _ = C_PetBattles.GetAbilityInfo(petOwner, petIndex, actionIndex)
if doMyPetBattle then
----------------------------------------------
-- IF WE HAVE SPELL WE CAN CAST, THEN CAST! --
if spell ~= nil and spell ~= "UNKNOWN" then
print(roundstring .. "|cffFF4500Casting\124r \124T"..spellIcon..":0\124t \124cff4e96f7\124HbattlePetAbil:"..spellID..":0:0:0\124h["..spell.."]\124h\124r");
-- print("actionIndex: ", actionIndex)
C_PetBattles.UseAbility(actionIndex) -- USE PET ABILITY
-- IF THE PET IS UNKNOWN, THEN CAST THE FIRST AVAILABLE SPELL SO WE WILL AT LEAST ATTACK
elseif spell == "UNKNOWN" then
print(roundstring .. "|cffFF4500Pet is unknown, casting first spell available")
C_PetBattles.UseAbility(1) -- USE PET ABILITY 1
end
elseif mypetbattle_debug then
------------------------------------------------------------------------------
-- IF WE'RE NOT ENABLED BUT IN DEBUG MODE THEN SHOW WHAT WE WOULD HAVE CAST --
print(roundstring .. "|cffFF4500Would be casting\124r \124T"..spellIcon..":0\124t \124cff4e96f7\124HbattlePetAbil:"..spellID..":0:0:0\124h["..(spell or "null").."]\124h\124r");
end
end
function events:PET_BATTLE_PET_ROUND_RESULTS(...) --
-- print("PET_BATTLE_PET_ROUND_RESULTS")
local roundNumber = ...
if roundNumber ~= 0 and mypetbattle_debug then
print("Round "..roundNumber)
end
end
function events:PET_BATTLE_PVP_DUEL_REQUESTED(...) --
print("PET_BATTLE_PVP_DUEL_REQUESTED")
end
function events:PET_BATTLE_PVP_DUEL_REQUEST_CANCEL(...) --
print("PET_BATTLE_PVP_DUEL_REQUEST_CANCEL")
end
function events:PET_BATTLE_QUEUE_PROPOSAL_ACCEPTED(...) --
if mypetbattle_debug then print("PET_BATTLE_QUEUE_PROPOSAL_ACCEPTED", ...) end
print("|cffff8000MPB|r: Pet Battle PvP match accepted")
end
function events:PET_BATTLE_QUEUE_PROPOSAL_DECLINED(...) --
if mypetbattle_debug then print("PET_BATTLE_QUEUE_PROPOSAL_DECLINED", ...) end
print("|cffff8000MPB|r: Removed from pet battle PvP queue")
end
function events:PET_BATTLE_QUEUE_PROPOSE_MATCH(...) --
if mypetbattle_debug then print("PET_BATTLE_QUEUE_PROPOSE_MATCH", ...) end
-- Sync setup for wt
if mypetbattle_wintrade_enabled then
MPB_SyncTimeSent = GetTime()
if mypetbattle_debug then print("|cffff8000MPB|r: Queue popped, sending sync message! " .. MPB_SyncTimeSent) end
SendAddonMessage("MPB", "s"..MPB_SyncTimeSent, "PARTY")
end
-- Automatic accept PvP queue popup if enabled
if (mypetbattle_join_pvp and not mypetbattle_wintrade_enabled) then
print("|cffff8000MPB|r: Auto-accepting pet PvP match!")
C_PetBattles.AcceptQueuedPVPMatch()
end
end
function events:PET_BATTLE_QUEUE_STATUS(...) --
-- print("PET_BATTLE_QUEUE_STATUS")
end
function events:PET_BATTLE_TURN_STARTED(...) --
print("PET_BATTLE_TURN_STARTED")
end
function events:PET_BATTLE_XP_CHANGED(...) --
if mypetbattle_debug then print("PET_BATTLE_XP_CHANGED", ...) end
local petOwner, petIndex, xpGained = ...
if petOwner == LE_BATTLE_PET_ALLY then
_G["pet"..petIndex.."_xp_gained"] = xpGained
end
end
MyPetBattleVars.xpGains = {}
local function pairsByKeys (t, f)
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a, f)
local i = 0 -- iterator variable
local iter = function () -- iterator function
i = i + 1
if a[i] == nil then return nil
else return a[i], t[a[i]]
end
end
return iter
end
function MyPetBattle:dumpXPGains(chatframenumber)
for level,leveltable in pairsByKeys(MyPetBattleVars.xpGains) do
--table.sort(leveltable,sortlevel)
--print(level,leveltable)
for xp, xptable in pairs(leveltable) do
--print(xp,xptable)
if xp ~= "xpmax" then
local message = string.format("-- %2d %6d %7d %s", level, xp, leveltable.xpmax, xptable.percent)
for team, count in pairs(xptable) do
-- print(team,count)
if team ~= "count" and team ~= "percent" then
message = message .. string.format(" %sx%d", team, count)
end
end
_G["ChatFrame" .. (chatframenumber or 1)]:AddMessage(message)
end
end
end
end
function events:PLAYER_XP_UPDATE(...)
if mypetbattle_debug then print("PLAYER_XP_UPDATE", ...) end
end
function events:CHAT_MSG_COMBAT_XP_GAIN(...)
if mypetbattle_debug then print("CHAT_MSG_COMBAT_XP_GAIN", ...) end
if player_level_start then
if not MyPetBattleVars.xpGains[player_level_start] then
MyPetBattleVars.xpGains[player_level_start] = {xpmax=player_xp_max_start}
end
local pattern = string.gsub(COMBATLOG_XPGAIN_FIRSTPERSON_UNNAMED, "%%d", "(%%d+)")
message = ...
local xpGained = tonumber(string.match(message, pattern))
if not MyPetBattleVars.xpGains[player_level_start][xpGained] then
MyPetBattleVars.xpGains[player_level_start][xpGained] = {percent=string.format("(%2.2f%%)", xpGained * 100 / player_xp_max_start), count=0}
end
MyPetBattleVars.xpGains[player_level_start][xpGained].count = MyPetBattleVars.xpGains[player_level_start][xpGained].count + 1
local levelstring = (pet1_level_start or "") .. "," ..(pet2_level_start or "") .. "," ..(pet3_level_start or "")
MyPetBattleVars.xpGains[player_level_start][xpGained][levelstring] = (MyPetBattleVars.xpGains[player_level_start][xpGained][levelstring] or 0) + 1
end
end
function events:CHAT_MSG_ADDON(...) --
-- print("CHAT_MSG_ADDON", ...)
local prefix, message, channel, sender = ...
-- CHECK IF MESSAGE COMES FROM MPB ADDON
if prefix == "MPB" and channel == "PARTY" and sender ~= UnitName("player") then
if (string.sub(message,1,1) == "s") then
MPB_SyncTimeReceived = tonumber(string.sub(message,2))
if mypetbattle_debug then print("We received sync message: "..MPB_SyncTimeReceived.." from "..prefix) end
elseif (string.sub(message,1,1) == "m") then
if MyPetBattleVars.state >= MyPetBattleState.TRANSFER_PET_LIST and MyPetBattleVars.state <= MyPetBattleState.RECEIVED_PET_LIST_AGAIN then
MyPetBattleVars.petListEnemy = message
MyPetBattle.setState(MyPetBattleState.RECEIVED_PET_LIST, true)
else
-- We're getting messages from our WT buddy. Maybe we should ensure that we're in a state to accept them.
if mypetbattle_wintrade_enabled and MPB.MATCH_PET_LEVELS_DURING_WT then
if not MyPetBattleState.FLAG_SET_TEAM_IN_PROGRESS and not MyPetBattleState.FLAG_BATTLE_IN_PROGRESS then
if string.sub(message,2,2) ~= "a" then
if not C_PetBattles.IsInBattle() then
MyPetBattleVars.petListEnemy = message
MyPetBattle.setState(MyPetBattleState.RECEIVED_PET_LIST_QUEUED, true)
end
end
end
end
end
elseif (string.sub(message,1,1) == "t") then
MyPetBattleVars.loadOutEnemy = message
end
end
end
function events:PLAYER_EQUIPMENT_CHANGED(...)
if mypetbattle_debug then print("PLAYER_EQUIPMENT_CHANGED", ...) end
end
mypetbattle_frame:SetScript("OnEvent", function(self, event, ...)
events[event](self, ...); -- call one of the functions above
end);
-- REGISTER ALL EVENTS FOR WHICH HANDLERS HAVE BEEN DEFINED
for k, v in pairs(events) do
mypetbattle_frame:RegisterEvent(k);
end
--------------------
--- TIMER FRAME ----
--------------------
MPB_timerTotal = 0 -- TIMER INIT FOR AUTOMATIC FORFEIT
MPB_timerOneSec = 0 -- 1 SEC TIMER INIT FOR DIFFERENT MECHANICS E.G. AUTO RE-QUEUE PVP
MPB_timerSetTeamCountdown = 0
MPB_timerSetTeamInterval = 1 -- 1 second timer for setting team
MPB_timerStartPVPMatchingCountdown = 0
MPB_timerStartPVPMatchingAfterSetTeamInterval = 2
MPB_timerStartPVPMatchingAfterStopPVPMatching = 2
MPB_timerStartPVPMatchingAfterDeclineQueuedPVPMatch = 2
MPB_timerStartPVPMatchingInterval = 5
MPB_timerAcceptPVPMatchmakingCountdown = 0
MPB_timerAcceptPVPMatchmakingInterval = 5
MPB_timerBattleCloseCountdown = 0
MPB_timerBattleCloseInterval = 5
if MPB_SyncTimeReceived == nil then MPB_SyncTimeReceived = 0 end
if MPB_SyncTimeSent == nil then MPB_SyncTimeSent = 0 end
MPB_syncCounter = 0
MPB_petguids = {0,0,0}
MPB_onUpdate = function (self,elapsed)
--print("MPB_onUpdate", self, elapsed)
-- update timers
MPB_timerOneSec = MPB_timerOneSec + elapsed
if MPB_timerStartPVPMatchingCountdown > 0 then
MPB_timerStartPVPMatchingCountdown = MPB_timerStartPVPMatchingCountdown - elapsed
end
if MPB_timerAcceptPVPMatchmakingCountdown > 0 then
MPB_timerAcceptPVPMatchmakingCountdown = MPB_timerAcceptPVPMatchmakingCountdown - elapsed
end
if MPB_timerSetTeamCountdown > 0 then
MPB_timerSetTeamCountdown = MPB_timerSetTeamCountdown - elapsed
end
-- check states
if MyPetBattleVars.state == MyPetBattleState.SET_TEAM_START then
if mypetbattle_debug then print("MyPetBattleState.SET_TEAM_START") end
MyPetBattleState.FLAG_SET_TEAM_IN_PROGRESS = true
if mypetbattle_wintrade_enabled and MPB.MATCH_PET_LEVELS_DURING_WT then
MyPetBattle.setState(MyPetBattleState.TRANSFER_PET_LIST, false)
else
MyPetBattle.setState(MyPetBattleState.SET_TEAM, false)
end
end
if MyPetBattleVars.state == MyPetBattleState.TRANSFER_PET_LIST then
if mypetbattle_debug then print("MyPetBattleState.TRANSFER_PET_LIST") end
MPB_timerSetTeamCountdown = MPB_timerSetTeamInterval
MyPetBattle.sendPetList() -- send our pet list
print("|cffff8000MPB|r: |cFF4169E1Sent your pet list for the first time and waiting for the enemy's pet list.")
MyPetBattle.setState(MyPetBattleState.TRANSFER_PET_LIST_AGAIN, false)
end
if MyPetBattleVars.state == MyPetBattleState.RECEIVED_PET_LIST_QUEUED then
if mypetbattle_debug then print("MyPetBattleState.RECEIVED_PET_LIST_QUEUED") end
print("|cffff8000MPB|r: |cFF4169E1Received the enemy's pet list while queued.")
MyPetBattleState.FLAG_SET_TEAM_IN_PROGRESS = true
if C_PetBattles.GetPVPMatchmakingInfo() == "queued" then
print("|cffff8000MPB|r: |cFF4169E1Stopping existing queue.")
MPB_timerStartPVPMatchingCountdown = MPB_timerStartPVPMatchingAfterStopPVPMatching
C_PetBattles.StopPVPMatchmaking()
end
MyPetBattle.setState(MyPetBattleState.RECEIVED_PET_LIST, false)
end
if MyPetBattleVars.state == MyPetBattleState.RECEIVED_PET_LIST then
if mypetbattle_debug then print("MyPetBattleState.RECEIVED_PET_LIST") end
MPB_timerSetTeamCountdown = MPB_timerSetTeamInterval
if not mypetbattle_join_pvp then
if string.sub(MyPetBattleVars.petListEnemy,MyPetBattleMatchPos.JOIN_PVP,MyPetBattleMatchPos.JOIN_PVP) == "p" then
mypetbattle_join_pvp = true
CheckButtonJoinPvP:SetChecked(mypetbattle_join_pvp)
print("|cffff8000MPB|r: |cFF00FF00PvP enabled by other player")
end
end
MyPetBattle.sendPetList() -- send our pet list again but with acknowledge this time
if string.sub(MyPetBattleVars.petListEnemy,2,2) == "a" then
print("|cffff8000MPB|r: |cFF4169E1Received the enemy's pet list with acknowledgement of your pet list.")
MyPetBattle.setState(MyPetBattleState.RECEIVED_PET_LIST_ACK, false)
else
print("|cffff8000MPB|r: |cFF4169E1Received the enemy's pet list and waiting for acknowledgement of your pet list.")
MyPetBattle.setState(MyPetBattleState.RECEIVED_PET_LIST_AGAIN, false)
end
end
if MyPetBattleVars.state == MyPetBattleState.RECEIVED_PET_LIST_ACK then
if mypetbattle_debug then print("MyPetBattleState.RECEIVED_PET_LIST_ACK") end
MyPetBattle.setState(MyPetBattleState.SET_TEAM, false)
end
if MyPetBattleVars.state == MyPetBattleState.SET_TEAM then
if mypetbattle_debug then print("MyPetBattleState.SET_TEAM") end
MyPetBattle.setState(MyPetBattleState.SET_TEAM_IN_PROGRESS, false)
local desiredPetLevel = EditBox_PetLevel:GetText() -- Get user input for desired pet level
MyPetBattle.setTeam(desiredPetLevel) -- Setup our team
-- Save desired pet level for next time we log in
MPB_EDITBOX_DESIRED_PET_LEVEL = desiredPetLevel
-- Clear focus from the editbox
EditBox_PetLevel:ClearFocus()
local petGUID = C_PetJournal.GetPetLoadOutInfo(1)
if not petGUID then
MyPetBattle.setState(MyPetBattleState.SET_TEAM_AGAIN, false)
else
MyPetBattle.setState(MyPetBattleState.SET_TEAM_DONE, false)
MyPetBattleState.FLAG_SET_TEAM_IN_PROGRESS = false
MPB_timerStartPVPMatchingCountdown = MPB_timerStartPVPMatchingAfterSetTeamInterval -- wait a few seconds before queuing to make sure the team was loaded completely
end
end
if MyPetBattleVars.state == MyPetBattleState.BATTLE_OPENED then
if mypetbattle_debug then print("MyPetBattleState.BATTLE_OPENED") end
MyPetBattleState.FLAG_BATTLE_IN_PROGRESS = true
-- STOPWATCH FOR WT
if mypetbattle_wintrade_enabled or mypetbattle_auto_forfeit then
if Stopwatch_IsPlaying() then Stopwatch_Clear() end
Stopwatch_StartCountdown(0, 0, MPB_FORFEIT_TIMER+5) -- SET STOP WATCH TO COUNT DOWN FROM MPB_FORFEIT_TIMER + 5 SEC. +5 FOR MINOR ADJUSTMENT BECAUSE FORFEIT ANIMATION TAKES ABOUT 5 SEC
Stopwatch_Play() -- STARTS THE STOP WATCH
if mypetbattle_debug then print("Started Stopwatch") end
end