-
Notifications
You must be signed in to change notification settings - Fork 3
/
pokemon.py
2144 lines (1917 loc) · 108 KB
/
pokemon.py
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
"""
Allows to configure custom shell commands to turn a value for a sensor.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.command_sensor/
"""
import logging
import subprocess
from datetime import timedelta
import random
import os
import shutil
import os.path
import requests
import sqlite3 as lite
from math import floor
from homeassistant.helpers.entity import Entity
from homeassistant.components import input_boolean, input_select
from homeassistant.util import Throttle
from homeassistant.helpers.event import track_utc_time_change
from homeassistant.helpers.event import track_state_change
from homeassistant.helpers.entity import generate_entity_id
import homeassistant.loader as loader
from homeassistant.const import (
STATE_ON, STATE_OFF)
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = "Ash"
DEFAULT_ENEMY = "Gary"
DEFAULT_HIDE_ENEMY = True
DEFAULT_TIMEOUT = 15
DEFAULT_INTERVAL = 60
DOMAIN = "pokemon"
ENTITY_ID_FORMAT = DOMAIN + '.{}'
TOTALPOKEDEX = 721
NUMBEROFVICTORIESPERBADGE = 1
POKEDEX = []
IV = 30
EV = 85
STAB = 1.5
POKEMONDICTIONARY = {}
POKEMONDICTIONARYGEN1 = {}
POKEMONDICTIONARYGEN2 = {}
POKEMONDICTIONARYGEN3 = {}
POKEMONDICTIONARYGEN4 = {}
POKEMONDICTIONARYGEN5 = {}
POKEMONDICTIONARYGEN6 = {}
TYPEDICTIONARY = {}
MOVES_DICTIONARY = {}
MOVES_PER_LEVELDICTIONARY = {}
POKEMON_DIR = None
# input boolean to decide if a user is playing automatic or manual
# input select to select the next move the pokemon must do
# if hideenemy = true => enemy is always automatic
# if hideenemy = false => enemy can be manual as well
# configurable timeout to set an input boolean back to automatic if no manual moves are done
def setup(hass, config):
""" Set up the iCloud Scanner. """
if config.get(DOMAIN) is None:
return False
POKEMON_DIR = hass.config.path(DOMAIN)
if not os.path.exists(POKEMON_DIR):
os.makedirs(POKEMON_DIR)
picture_dir = os.path.join(hass.config.path('www'), DOMAIN)
if not os.path.exists(picture_dir):
os.makedirs(picture_dir)
full_filename = "pokemon.csv"
file_path = os.path.join(POKEMON_DIR, full_filename)
if os.path.isfile(file_path):
os.remove(file_path)
if not os.path.isfile(file_path):
url = "https://raw.githubusercontent.com/Bart274/pokemonHA/master/pokemon.csv"
pokemoncsv = requests.get(url, stream=True)
if pokemoncsv.status_code == 200:
with open(file_path, 'wb') as opened_file:
pokemoncsv.raw.decode_content = True
shutil.copyfileobj(pokemoncsv.raw, opened_file)
else:
err = "An error happened when requesting the pokemon.csv!"
_LOGGER.error(err)
return False
full_filename = "pokemontypeadvantages.csv"
file_path = os.path.join(POKEMON_DIR, full_filename)
if os.path.isfile(file_path):
os.remove(file_path)
if not os.path.isfile(file_path):
url = "https://raw.githubusercontent.com/Bart274/pokemonHA/master/pokemontypeadvantages.csv"
pokemontypeadvantagescsv = requests.get(url, stream=True)
if pokemontypeadvantagescsv.status_code == 200:
with open(file_path, 'wb') as opened_file:
pokemontypeadvantagescsv.raw.decode_content = True
shutil.copyfileobj(pokemontypeadvantagescsv.raw, opened_file)
else:
err = "An error happened when requesting the pokemontypeadvantages.csv!"
_LOGGER.error(err)
return False
full_filename = "pokemonmoves.csv"
file_path = os.path.join(POKEMON_DIR, full_filename)
if os.path.isfile(file_path):
os.remove(file_path)
if not os.path.isfile(file_path):
url = "https://raw.githubusercontent.com/Bart274/pokemonHA/master/pokemonmoves.csv"
pokemonmovescsv = requests.get(url, stream=True)
if pokemonmovescsv.status_code == 200:
with open(file_path, 'wb') as opened_file:
pokemonmovescsv.raw.decode_content = True
shutil.copyfileobj(pokemonmovescsv.raw, opened_file)
else:
err = "An error happened when requesting the pokemonmoves.csv!"
_LOGGER.error(err)
return False
full_filename = "pokemonmovesperlevel.csv"
file_path = os.path.join(POKEMON_DIR, full_filename)
if os.path.isfile(file_path):
os.remove(file_path)
if not os.path.isfile(file_path):
url = "https://raw.githubusercontent.com/Bart274/pokemonHA/master/pokemonmovesperlevel.csv"
pokemoncsv = requests.get(url, stream=True)
if pokemoncsv.status_code == 200:
with open(file_path, 'wb') as opened_file:
pokemoncsv.raw.decode_content = True
shutil.copyfileobj(pokemoncsv.raw, opened_file)
else:
err = "An error happened when requesting the pokemon.csv!"
_LOGGER.error(err)
return False
fin = open(os.path.join(POKEMON_DIR,"pokemon.csv"), 'r')
# Creating a dictionary with each Pokemon's name as a key
for line in fin:
line = line.strip()
pokeList = line.split(",")
if pokeList[17] == 'yes' and pokeList[0].strip(' \t\n\r') != '' and pokeList[0].lower().strip(' \t\n\r') != 'nat':
POKEMONDICTIONARY[pokeList[0]] = pokeList
if int(pokeList[14]) <= 1:
POKEMONDICTIONARYGEN1[pokeList[0]] = pokeList
if int(pokeList[14]) <= 2:
POKEMONDICTIONARYGEN2[pokeList[0]] = pokeList
if int(pokeList[14]) <= 3:
POKEMONDICTIONARYGEN3[pokeList[0]] = pokeList
if int(pokeList[14]) <= 4:
POKEMONDICTIONARYGEN4[pokeList[0]] = pokeList
if int(pokeList[14]) <= 5:
POKEMONDICTIONARYGEN5[pokeList[0]] = pokeList
if int(pokeList[14]) <= 6:
POKEMONDICTIONARYGEN6[pokeList[0]] = pokeList
fin.close()
# Taking the keys from above, turning them into a list, and sorting them
for key in POKEMONDICTIONARY:
POKEDEX.append(POKEMONDICTIONARY[key][0].lower())
POKEDEX.sort()
# Reading "Type Advantages.csv" file to determine type advantages and the damage modifier
# Stores the line number in the csv as the key and a list giving information about type advantage for the value
fin = open(os.path.join(POKEMON_DIR,"pokemontypeadvantages.csv"), 'r')
for line in fin:
line = line.strip()
typeList = line.split(",")
TYPEDICTIONARY[typeList[0]] = typeList
# This list contains a number in the first position, the attack type in the second, the defending type in the third,
# and the appropriate damage multiplier in the fourth
fin.close()
fin = open(os.path.join(POKEMON_DIR,"pokemonmoves.csv"), 'r')
for line in fin:
line = line.strip()
movelist = line.split(",")
MOVES_DICTIONARY[movelist[0]] = movelist # The name of the move is the key while the rest of the
# list is the value
fin.close()
fin = open(os.path.join(POKEMON_DIR,"pokemonmovesperlevel.csv"), 'r')
for line in fin:
line = line.strip()
movelist = line.split(",")
MOVES_PER_LEVELDICTIONARY[int(movelist[0]) * 1000 + int(movelist[1])] = movelist # The name of the move is the key while the rest of the
# list is the value
fin.close()
pokemon_config = config[DOMAIN]
def readdata(pokemonentity):
POKEMON_DIR = hass.config.path(DOMAIN)
if not os.path.exists(POKEMON_DIR):
os.makedirs(POKEMON_DIR)
full_filename = "pokemon.db"
file_path = os.path.join(POKEMON_DIR, full_filename)
try:
con = lite.connect(file_path)
with con:
con.row_factory = lite.Row
cur = con.cursor()
cur.execute("SELECT * FROM Pokemon WHERE Id = '" + pokemonentity.entity_id +"'")
rows = cur.fetchall()
if len(rows) == 1:
for row in rows:
if pokemonentity.type == 'player' or pokemonentity.type == 'enemy':
pokemonentity.caughtpokemon = row['PokemonCaught'].split(",")
pokemonentity.seenpokemon = row['PokemonSeen'].split(",")
pokemonentity.pokedexcaught = len(pokemonentity.caughtpokemon)
pokemonentity.pokedex = str(round(pokemonentity.pokedexcaught / TOTALPOKEDEX * 100, 2)) + '%'
pokemonentity.pokedexseen = len(pokemonentity.seenpokemon)
pokemonentity.victories = row['Victories']
pokemonentity.badges = row['Badges']
elif pokemonentity.type == 'pokemon':
pokemonentity.chosenpokemon = str(row['PokemonID'])
pokemonentity.level = row['PokemonLevel']
except lite.Error as e:
if con:
con.rollback()
finally:
if con:
con.close()
# Get the username and password from the configuration
playername = pokemon_config.get('playername', DEFAULT_NAME)
enemyname = pokemon_config.get('enemyname', DEFAULT_ENEMY)
hideenemy = pokemon_config.get('hide_enemy', DEFAULT_HIDE_ENEMY)
timeout = pokemon_config.get('timeout', DEFAULT_TIMEOUT)
interval = pokemon_config.get('interval', DEFAULT_INTERVAL)
player = Pokemon(hass, 'player', playername, hideenemy, picture_dir)
player.update_ha_state()
enemy = Pokemon(hass, 'enemy', enemyname, hideenemy, picture_dir)
enemy.update_ha_state()
pokemonplayer1 = Pokemon(hass, 'pokemon', '1', hideenemy, picture_dir, player, enemy)
readdata(pokemonplayer1)
pokemonplayer1.update_ha_state()
pokemonplayer2 = Pokemon(hass, 'pokemon', '2', hideenemy, picture_dir, player, enemy)
readdata(pokemonplayer2)
pokemonplayer2.update_ha_state()
pokemonplayer3 = Pokemon(hass, 'pokemon', '3', hideenemy, picture_dir, player, enemy)
readdata(pokemonplayer3)
pokemonplayer3.update_ha_state()
pokemonplayer4 = Pokemon(hass, 'pokemon', '4', hideenemy, picture_dir, player, enemy)
readdata(pokemonplayer4)
pokemonplayer4.update_ha_state()
pokemonplayer5 = Pokemon(hass, 'pokemon', '5', hideenemy, picture_dir, player, enemy)
readdata(pokemonplayer5)
pokemonplayer5.update_ha_state()
pokemonplayer6 = Pokemon(hass, 'pokemon', '6', hideenemy, picture_dir, player, enemy)
readdata(pokemonplayer6)
pokemonplayer6.update_ha_state()
pokemonenemy1 = Pokemon(hass, 'pokemon', '1', hideenemy, picture_dir, enemy, player)
readdata(pokemonenemy1)
pokemonenemy1.update_ha_state()
pokemonenemy2 = Pokemon(hass, 'pokemon', '2', hideenemy, picture_dir, enemy, player)
readdata(pokemonenemy2)
pokemonenemy2.update_ha_state()
pokemonenemy3 = Pokemon(hass, 'pokemon', '3', hideenemy, picture_dir, enemy, player)
readdata(pokemonenemy3)
pokemonenemy3.update_ha_state()
pokemonenemy4 = Pokemon(hass, 'pokemon', '4', hideenemy, picture_dir, enemy, player)
readdata(pokemonenemy4)
pokemonenemy4.update_ha_state()
pokemonenemy5 = Pokemon(hass, 'pokemon', '5', hideenemy, picture_dir, enemy, player)
readdata(pokemonenemy5)
pokemonenemy5.update_ha_state()
pokemonenemy6 = Pokemon(hass, 'pokemon', '6', hideenemy, picture_dir, enemy, player)
readdata(pokemonenemy6)
pokemonenemy6.update_ha_state()
readdata(player)
player.update_ha_state()
readdata(enemy)
enemy.update_ha_state()
pokemonbattleenemy = Pokemon(hass, 'battleenemy', 'battle', hideenemy, picture_dir, player, enemy,
pokemonplayer1, pokemonplayer2, pokemonplayer3,
pokemonplayer4, pokemonplayer5, pokemonplayer6,
pokemonenemy1, pokemonenemy2, pokemonenemy3,
pokemonenemy4, pokemonenemy5, pokemonenemy6)
pokemonbattleenemy.update_ha_state()
pokemonbattle = Pokemon(hass, 'battle', 'battle', hideenemy, picture_dir, player, enemy,
pokemonplayer1, pokemonplayer2, pokemonplayer3,
pokemonplayer4, pokemonplayer5, pokemonplayer6,
pokemonenemy1, pokemonenemy2, pokemonenemy3,
pokemonenemy4, pokemonenemy5, pokemonenemy6, pokemonbattleenemy)
pokemonbattle.update_ha_state()
input_boolean.setup(hass, {
'input_boolean': {
'pokemonplayer': {
'name': player.name,
'initial': True,
},
},
})
if not hideenemy:
input_boolean.setup(hass, {
'input_boolean': {
'pokemonenemy': {
'name': enemy.name,
'initial': True,
},
},
})
if hideenemy:
group = loader.get_component('group')
group.Group(hass, 'Pokemon', [
pokemonbattle.entity_id, player.entity_id, 'input_boolean.pokemonplayer', 'input_select.pokemonplayer', pokemonplayer1.entity_id,
pokemonplayer2.entity_id, pokemonplayer3.entity_id,
pokemonplayer4.entity_id, pokemonplayer5.entity_id,
pokemonplayer6.entity_id, enemy.entity_id, pokemonenemy1.entity_id,
pokemonenemy2.entity_id, pokemonenemy3.entity_id,
pokemonenemy4.entity_id, pokemonenemy5.entity_id,
pokemonenemy6.entity_id])
else:
group = loader.get_component('group')
group.Group(hass, 'PokemonPlayer', [
pokemonbattle.entity_id, player.entity_id, 'input_boolean.pokemonplayer', 'input_select.pokemonplayer', enemy.entity_id, 'input_boolean.pokemonenemy', pokemonplayer1.entity_id,
pokemonplayer2.entity_id, pokemonplayer3.entity_id,
pokemonplayer4.entity_id, pokemonplayer5.entity_id,
pokemonplayer6.entity_id])
group.Group(hass, 'PokemonEnemy', [
pokemonbattleenemy.entity_id, player.entity_id, 'input_boolean.pokemonplayer', enemy.entity_id, 'input_boolean.pokemonenemy', 'input_select.pokemonenemy', pokemonenemy1.entity_id,
pokemonenemy2.entity_id, pokemonenemy3.entity_id,
pokemonenemy4.entity_id, pokemonenemy5.entity_id,
pokemonenemy6.entity_id])
def writedata():
""" writes the data to the db file """
POKEMON_DIR = hass.config.path(DOMAIN)
if not os.path.exists(POKEMON_DIR):
os.makedirs(POKEMON_DIR)
full_filename = "pokemon.db"
file_path = os.path.join(POKEMON_DIR, full_filename)
playercaughtpokemon = ''
for caught in player.caughtpokemon:
if playercaughtpokemon != '':
playercaughtpokemon += ','
playercaughtpokemon += caught
playerseenpokemon = ''
for seen in player.seenpokemon:
if playerseenpokemon != '':
playerseenpokemon += ','
playerseenpokemon += seen
enemycaughtpokemon = ''
for caught in enemy.caughtpokemon:
if enemycaughtpokemon != '':
enemycaughtpokemon += ','
enemycaughtpokemon += caught
enemyseenpokemon = ''
for seen in enemy.seenpokemon:
if enemyseenpokemon != '':
enemyseenpokemon += ','
enemyseenpokemon += seen
pokemondata = (
(player.entity_id, player.victories, player.badges, playercaughtpokemon, playerseenpokemon, 0, 0),
(enemy.entity_id, enemy.victories, enemy.badges, enemycaughtpokemon, playerseenpokemon, 0, 0),
(pokemonplayer1.entity_id, 0, 0, '', '', pokemonplayer1.chosenpokemon, pokemonplayer1.level),
(pokemonplayer2.entity_id, 0, 0, '', '', pokemonplayer2.chosenpokemon, pokemonplayer2.level),
(pokemonplayer3.entity_id, 0, 0, '', '', pokemonplayer3.chosenpokemon, pokemonplayer3.level),
(pokemonplayer4.entity_id, 0, 0, '', '', pokemonplayer4.chosenpokemon, pokemonplayer4.level),
(pokemonplayer5.entity_id, 0, 0, '', '', pokemonplayer5.chosenpokemon, pokemonplayer5.level),
(pokemonplayer6.entity_id, 0, 0, '', '', pokemonplayer6.chosenpokemon, pokemonplayer6.level),
(pokemonenemy1.entity_id, 0, 0, '', '', pokemonenemy1.chosenpokemon, pokemonenemy1.level),
(pokemonenemy2.entity_id, 0, 0, '', '', pokemonenemy2.chosenpokemon, pokemonenemy2.level),
(pokemonenemy3.entity_id, 0, 0, '', '', pokemonenemy3.chosenpokemon, pokemonenemy3.level),
(pokemonenemy4.entity_id, 0, 0, '', '', pokemonenemy4.chosenpokemon, pokemonenemy4.level),
(pokemonenemy5.entity_id, 0, 0, '', '', pokemonenemy5.chosenpokemon, pokemonenemy5.level),
(pokemonenemy6.entity_id, 0, 0, '', '', pokemonenemy6.chosenpokemon, pokemonenemy6.level)
)
try:
con = lite.connect(file_path)
with con:
cur = con.cursor()
cur.execute("DROP TABLE IF EXISTS Pokemon")
cur.execute("CREATE TABLE Pokemon(Id TEXT, Victories INT, Badges INT, PokemonCaught TEXT, PokemonSeen TEXT, PokemonID INT, PokemonLevel INT)")
cur.executemany("INSERT INTO Pokemon VALUES(?, ?, ?, ?, ?, ?, ?)", pokemondata)
except lite.Error as e:
if con:
con.rollback()
_LOGGER.error("POKEMON: Error %s:" % e.args[0])
finally:
if con:
con.close()
def updateextraentities():
if pokemonbattle.waitinginput and pokemonbattle.timer < timeout:
pokemonbattle.timer += 1
elif pokemonbattle.waitinginput and pokemonbattle.timer == timeout:
if pokemonbattle.attacker.person1.type == 'player':
hass.services.call('input_boolean', 'turn_on', {
'entity_id': 'input_boolean.pokemonplayer',
})
elif pokemonbattle.attacker.person1.type == 'enemy':
hass.services.call('input_boolean', 'turn_on', {
'entity_id': 'input_boolean.pokemonenemy',
})
pokemonbattle.timer = 0
pokemonbattle.waitinginput = False
else:
pokemonbattle.timer = 0
pokemonbattle.waitinginput = False
def update(now):
""" Keeps the api logged in of all account """
pokemonbattle.update()
pokemonbattleenemy.update()
player.update()
enemy.update()
pokemonplayer1.update()
pokemonplayer2.update()
pokemonplayer3.update()
pokemonplayer4.update()
pokemonplayer5.update()
pokemonplayer6.update()
pokemonenemy1.update()
pokemonenemy2.update()
pokemonenemy3.update()
pokemonenemy4.update()
pokemonenemy5.update()
pokemonenemy6.update()
writedata()
updateextraentities()
track_utc_time_change(
hass, update,
second=range(0, 60, interval)
)
def check_state_change(entity, old_state, new_state):
if new_state is not None and new_state != '':
update(None)
if hideenemy:
track_state_change(hass, 'input_select.pokemonplayer',
check_state_change)
else:
track_state_change(hass, 'input_select.pokemonplayer',
check_state_change)
track_state_change(hass, 'input_select.pokemonenemy',
check_state_change)
# Tells the bootstrapper that the component was successfully initialized
return True
class Pokemon(Entity):
def __init__(self, hass, type, name, hideenemy, picture_dir, person1=None,
person2=None, pokemonplayer1=None, pokemonplayer2=None,
pokemonplayer3=None, pokemonplayer4=None, pokemonplayer5=None,
pokemonplayer6=None, pokemonenemy1=None, pokemonenemy2=None,
pokemonenemy3=None, pokemonenemy4=None, pokemonenemy5=None,
pokemonenemy6=None, pokemonbattleenemy=None):
self.hass = hass
self.type = type
if type == 'pokemon':
self.pname = type + name
else:
self.pname = name
self.person1 = person1
self.person2 = person2
self.hideenemy = hideenemy
self.picture_dir = picture_dir
self.health = 100
self._state = None
self.lastmove = None
self.attackedwith = None
self.damage = 0
self.resetting = 1
self.victories = 0
self.badges = 0
self.pokedex = 0
self.pokedexcaught = 0
self.pokedexseen = 0
self.caughtpokemon = []
self.seenpokemon = []
self.level = 5
self.timer = 0
self.won = False
self.attacker = None
self.victim = None
self.activepokemonplayer = None
self.activepokemonenemy = None
self.battlestate = "Battle beginning"
self.pokemonplayer1 = pokemonplayer1
self.pokemonleft = 0
if self.pokemonplayer1 is not None:
self.pokemonplayer1.choosepokemon(self.pokemonplayer1.chosenpokemon)
self.pokemonplayer1.won = True
self.pokemonplayer2 = pokemonplayer2
if self.pokemonplayer2 is not None:
self.pokemonplayer2.choosepokemon(self.pokemonplayer2.chosenpokemon)
self.pokemonplayer2.won = True
self.pokemonplayer3 = pokemonplayer3
if self.pokemonplayer3 is not None:
self.pokemonplayer3.choosepokemon(self.pokemonplayer3.chosenpokemon)
self.pokemonplayer3.won = True
self.pokemonplayer4 = pokemonplayer4
if self.pokemonplayer4 is not None:
self.pokemonplayer4.choosepokemon(self.pokemonplayer4.chosenpokemon)
self.pokemonplayer4.won = True
self.pokemonplayer5 = pokemonplayer5
if self.pokemonplayer5 is not None:
self.pokemonplayer5.choosepokemon(self.pokemonplayer5.chosenpokemon)
self.pokemonplayer5.won = True
self.pokemonplayer6 = pokemonplayer6
if self.pokemonplayer6 is not None:
self.pokemonplayer6.choosepokemon(self.pokemonplayer6.chosenpokemon)
self.pokemonplayer6.won = True
self.pokemonenemy1 = pokemonenemy1
if self.pokemonenemy1 is not None:
self.pokemonenemy1.choosepokemon(self.pokemonenemy1.chosenpokemon)
self.pokemonenemy1.won = True
self.pokemonenemy2 = pokemonenemy2
if self.pokemonenemy2 is not None:
self.pokemonenemy2.choosepokemon(self.pokemonenemy2.chosenpokemon)
self.pokemonenemy2.won = True
self.pokemonenemy3 = pokemonenemy3
if self.pokemonenemy3 is not None:
self.pokemonenemy3.choosepokemon(self.pokemonenemy3.chosenpokemon)
self.pokemonenemy3.won = True
self.pokemonenemy4 = pokemonenemy4
if self.pokemonenemy4 is not None:
self.pokemonenemy4.choosepokemon(self.pokemonenemy4.chosenpokemon)
self.pokemonenemy4.won = True
self.pokemonenemy5 = pokemonenemy5
if self.pokemonenemy5 is not None:
self.pokemonenemy5.choosepokemon(self.pokemonenemy5.chosenpokemon)
self.pokemonenemy5.won = True
self.pokemonenemy6 = pokemonenemy6
if self.pokemonenemy6 is not None:
self.pokemonenemy6.choosepokemon(self.pokemonenemy6.chosenpokemon)
self.pokemonenemy6.won = True
self.pokemonbattleenemy = pokemonbattleenemy
self.firstbattle = True
self.fainted = True
self.active = False
self.chosenpokemon = None
self.__id = None
self.pokemonname = None
self.type1 = None
self.type2 = None
self.__hp = None
self.__atk = None
self.__defense = None
self.__spAtk = None
self.__spDef = None
self.__speed = None
self.battleHP = None
self.battleATK = None
self.battleDEF = None
self.battleSpATK = None
self.battleSpDEF = None
self.battleSpeed = None
self.originalATK = None
self.originalDEF = None
self.originalSpATK = None
self.originalSpDEF = None
self.originalSpeed = None
self.move1 = None
self.move2 = None
self.move3 = None
self.move4 = None
self.atkStage = None
self.defStage = None
self.spAtkStage = None
self.spDefStage = None
self.speedStage = None
self.generation = None
self.height = None
self.weight = None
self.movedictionary = {}
self.waitinginput = False
if self.type == 'pokemon':
self.entity_id = generate_entity_id(
ENTITY_ID_FORMAT, self.pname + self.person1.type,
hass=self.hass)
else:
self.entity_id = generate_entity_id(
ENTITY_ID_FORMAT, self.type,
hass=self.hass)
@property
def state_attributes(self):
""" returns the friendlyname of the icloud tracker """
if self.type == 'player' or self.type == 'enemy':
return {
"Name": self.pname,
"Victories": self.victories,
"Badges": self.badges,
"Pokedex completed": self.pokedex,
"Pokemon caught": self.pokedexcaught,
"Pokemon seen": self.pokedexseen
}
elif self.type == 'pokemon':
if self.person1.type == 'enemy':
if not self.active and not self.fainted and self.hideenemy:
return {
"Health": '?',
"Level": '?',
"Owner": self.person1.pname,
"Generation": '?',
"Type": '?',
"Attack": '?',
"Defense": '?',
"Special Attack": '?',
"Special Defense": '?',
"Speed": '?',
"Height": '?',
"Weight": '?',
"Moves": '?'
}
moves = ''
for key in self.movedictionary:
if moves != '':
moves += ', '
moves += self.movedictionary[key].name + ' PP ' + str(self.movedictionary[key].pp)
temptype = self.type1
if self.type2 is not None and self.type2.strip(' \t\n\r') != '':
temptype += '/' + self.type2
if self.chosenpokemon is None or self.active is False:
return {
"Health": self.health,
"Level": self.level,
"Owner": self.person1.pname,
"Generation": self.generation,
"Type": temptype,
"Attack": self.battleATK,
"Defense": self.battleDEF,
"Special Attack": self.battleSpATK,
"Special Defense": self.battleSpDEF,
"Speed": self.battleSpeed,
"Height": self.height,
"Weight": self.weight,
"Moves": moves
}
else:
tempentitypicture = "/local/" + DOMAIN + "/"
full_filename = self.__id.zfill(3) +".gif"
file_path = os.path.join(self.picture_dir, full_filename)
if os.path.isfile(file_path):
tempentitypicture += full_filename
else:
tempentitypicture += "missingpicture.jpg"
return {
"Health": self.health,
"Level": self.level,
"Owner": self.person1.pname,
"Generation": self.generation,
"Type": temptype,
"Attack": self.battleATK,
"Defense": self.battleDEF,
"Special Attack": self.battleSpATK,
"Special Defense": self.battleSpDEF,
"Speed": self.battleSpeed,
"Height": self.height,
"Weight": self.weight,
"entity_picture": tempentitypicture,
"Moves": moves
}
else:
if self.attacker is None:
tempattacker = None
else:
tempattacker = self.attacker.person1.pname
if self.victim is None:
tempvictim = None
else:
tempvictim = self.victim.person1.pname
if self.activepokemonplayer is None:
tempapp = None
else:
tempapp = self.activepokemonplayer.pokemonname
if self.activepokemonenemy is None:
tempape = None
else:
tempape = self.activepokemonenemy.pokemonname
return {
"Attacker": tempattacker,
"Victim": tempvictim,
"Active pokemon player": tempapp,
"Active pokemon enemy": tempape,
"Action": self.battlestate
}
@property
def hidden(self):
"""If device should be hidden."""
""" returns the friendlyname of the icloud tracker """
if self.type == 'pokemon':
if self.chosenpokemon is None:
return True
else:
return self.fainted
elif self.type == 'battleenemy':
return self.hideenemy
else:
return False
@property
def icon(self):
"""Return the icon to use for device if any."""
if self.type == 'player' or self.type == 'enemy':
if self.pokemonleft == 6:
return 'mdi:dice-6'
elif self.pokemonleft == 5:
return 'mdi:dice-5'
elif self.pokemonleft == 4:
return 'mdi:dice-4'
elif self.pokemonleft == 3:
return 'mdi:dice-3'
elif self.pokemonleft == 2:
return 'mdi:dice-2'
elif self.pokemonleft == 1:
return 'mdi:dice-1'
else:
return 'mdi:account-card-details'
elif self.type == 'battle':
if self.resetting > 0:
return 'mdi:cached'
elif self.battlestate == self.person1.pname + " defeated " + self.person2.pname:
return 'mdi:trophy'
elif self.battlestate == self.person2.pname + " defeated " + self.person1.pname:
return 'mdi:emoticon-sad'
else:
return 'mdi:sword'
elif self.type == 'battleenemy':
if self.resetting > 0:
return 'mdi:cached'
elif self.battlestate == self.person1.pname + " defeated " + self.person2.pname:
return 'mdi:emoticon-sad'
elif self.battlestate == self.person2.pname + " defeated " + self.person1.pname:
return 'mdi:trophy'
else:
return 'mdi:sword'
else:
if not self.active:
if self.chosenpokemon is None or self.fainted:
return 'mdi:ghost'
elif not self.fainted:
return 'mdi:pokeball'
@property
def state(self):
"""Return the state of the device."""
if self.type == 'player' or self.type == 'enemy':
self._state = self.victories
elif self.type == 'pokemon':
tempname = self.health
if self.person1.type == 'enemy':
if not self.active and not self.fainted and self.hideenemy:
tempname = '?'
self._state = tempname
else:
self._state = self.battlestate
return self._state
@property
def unit_of_measurement(self):
""" Unit of measurement of this entity """
if self.type == 'player' or self.type == 'enemy':
return "WINS"
elif self.type == 'pokemon':
return "HP"
@property
def name(self):
"""Return the name of the sensor."""
if self.type == 'pokemon':
return self.pokemonname
else:
return self.pname
def levelup(self):
tempstring = ""
tempname = self.pokemonname
if self.person1.type == 'enemy':
tempname = "Foe's " + tempname
prevlevel = self.level
if self.level < 100:
self.level += 1
tempstring += "\n" + tempname + " grew to level " + str(self.level)
tempHP1 = round((self.__hp * 2 + IV + (EV / 4)) * prevlevel / 100 + 10 + prevlevel,0)
tempHP2 = round((self.__hp * 2 + IV + (EV / 4)) * self.level / 100 + 10 + self.level,0)
self.battleHP += tempHP2 - tempHP1
tempstring += "\nHP +" + str(tempHP2 - tempHP1)
tempATK1 = round(((self.__atk * 2 + IV + (EV / 4)) * prevlevel / 100 + 5),0)
tempATK2 = round(((self.__atk * 2 + IV + (EV / 4)) * self.level / 100 + 5),0)
self.battleATK += tempATK2 - tempATK1
tempstring += "\nATK +" + str(tempATK2 - tempATK1)
tempDEF1 = round(((self.__defense * 2 + IV + (EV / 4)) * prevlevel / 100 + 5),0)
tempDEF2 = round(((self.__defense * 2 + IV + (EV / 4)) * self.level / 100 + 5),0)
self.battleDEF += tempDEF2 - tempDEF1
tempstring += "\nDEF +" + str(tempDEF2 - tempDEF1)
tempSPATK1 = round(((self.__spAtk * 2 + IV + (EV / 4)) * prevlevel / 100 + 5),0)
tempSPATK2 = round(((self.__spAtk * 2 + IV + (EV / 4)) * self.level / 100 + 5),0)
self.battleSpATK += tempSPATK2 - tempSPATK1
tempstring += "\nSpATK +" + str(tempSPATK2 - tempSPATK1)
tempSPDEF1 = round(((self.__spDef * 2 + IV + (EV / 4)) * prevlevel / 100 + 5),0)
tempSPDEF2 = round(((self.__spDef * 2 + IV + (EV / 4)) * self.level / 100 + 5),0)
self.battleSpDEF += tempSPDEF2 - tempSPDEF1
tempstring += "\nSpDEF +" + str(tempSPDEF2 - tempSPDEF1)
tempSPEED1 = round(((self.__speed * 2 + IV + (EV / 4)) * prevlevel / 100 + 5),0)
tempSPEED2 = round(((self.__speed * 2 + IV + (EV / 4)) * self.level / 100 + 5),0)
self.battleSpeed += tempSPEED2 - tempSPEED1
tempstring += "\nSpeed +" + str(tempSPEED2 - tempSPEED1)
self.originalATK = self.battleATK
self.originalDEF = self.battleDEF
self.originalSpATK = self.battleSpATK
self.originalSpDEF = self.battleSpDEF
self.originalSpeed = self.battleSpeed
self.health = self.battleHP
self.atkStage = 0
self.defStage = 0
self.spAtkStage = 0
self.spDefStage = 0
self.speedStage = 0
x = 1
while x <= self.level:
key = int(self.__id) * 1000 + x
if key in MOVES_PER_LEVELDICTIONARY:
moveInfo = MOVES_PER_LEVELDICTIONARY[key]
if moveInfo[2] != '' and moveInfo[2] not in self.movedictionary:
self.movedictionary[moveInfo[2]] = Move(moveInfo[2])
if moveInfo[3] != '' and moveInfo[3] not in self.movedictionary:
self.movedictionary[moveInfo[3]] = Move(moveInfo[3])
if moveInfo[4] != '' and moveInfo[4] not in self.movedictionary:
self.movedictionary[moveInfo[4]] = Move(moveInfo[4])
if moveInfo[5] != '' and moveInfo[5] not in self.movedictionary:
self.movedictionary[moveInfo[5]] = Move(moveInfo[5])
if moveInfo[6] != '' and moveInfo[6] not in self.movedictionary:
self.movedictionary[moveInfo[6]] = Move(moveInfo[6])
if moveInfo[7] != '' and moveInfo[7] not in self.movedictionary:
self.movedictionary[moveInfo[7]] = Move(moveInfo[7])
if moveInfo[8] != '' and moveInfo[8] not in self.movedictionary:
self.movedictionary[moveInfo[8]] = Move(moveInfo[8])
if moveInfo[9] != '' and moveInfo[9] not in self.movedictionary:
self.movedictionary[moveInfo[9]] = Move(moveInfo[9])
if moveInfo[10] != '' and moveInfo[10] not in self.movedictionary:
self.movedictionary[moveInfo[10]] = Move(moveInfo[10])
if moveInfo[11] != '' and moveInfo[11] not in self.movedictionary:
self.movedictionary[moveInfo[11]] = Move(moveInfo[11])
if moveInfo[12] != '' and moveInfo[12] not in self.movedictionary:
self.movedictionary[moveInfo[12]] = Move(moveInfo[12])
if moveInfo[13] != '' and moveInfo[13] not in self.movedictionary:
self.movedictionary[moveInfo[13]] = Move(moveInfo[13])
if moveInfo[14] != '' and moveInfo[14] not in self.movedictionary:
self.movedictionary[moveInfo[14]] = Move(moveInfo[14])
if moveInfo[15] != '' and moveInfo[15] not in self.movedictionary:
self.movedictionary[moveInfo[15]] = Move(moveInfo[15])
x += 1
if '165' in self.movedictionary:
del self.movedictionary['165']
if not self.movedictionary:
self.movedictionary['165'] = Move('165')
return tempstring
def choosepokemon(self, chosenpokemon=None):
if chosenpokemon is None:
if floor(self.person1.badges / 8) == 0:
chosenpokemon = random.choice(list(POKEMONDICTIONARYGEN1))
elif floor(self.person1.badges / 8) == 1:
chosenpokemon = random.choice(list(POKEMONDICTIONARYGEN2))
elif floor(self.person1.badges / 8) == 2:
chosenpokemon = random.choice(list(POKEMONDICTIONARYGEN3))
elif floor(self.person1.badges / 8) == 3:
chosenpokemon = random.choice(list(POKEMONDICTIONARYGEN4))
elif floor(self.person1.badges / 8) == 4:
chosenpokemon = random.choice(list(POKEMONDICTIONARYGEN5))
elif floor(self.person1.badges / 8) == 5:
chosenpokemon = random.choice(list(POKEMONDICTIONARYGEN6))
else:
chosenpokemon = random.choice(list(POKEMONDICTIONARY))
self.level = 5
self.chosenpokemon = chosenpokemon
if self.chosenpokemon in POKEMONDICTIONARY:
pokemonInfo = POKEMONDICTIONARY[self.chosenpokemon]
if self.chosenpokemon not in self.person1.caughtpokemon:
self.person1.caughtpokemon.append(self.chosenpokemon)
self.person1.pokedexcaught += 1
self.person1.pokedex = str(round(self.person1.pokedexcaught / TOTALPOKEDEX * 100, 2)) + '%'
if self.chosenpokemon not in self.person1.seenpokemon:
self.person1.seenpokemon.append(self.chosenpokemon)
self.person1.pokedexseen += 1
self.fainted = False
self.active = False
self.won = False
# ATTRIBUTES
# Referring to the pokemonInfo list to fill in the rest of the attributes
# ID Info
self.__id = pokemonInfo[0]
self.pokemonname = pokemonInfo[1]
full_filename = self.__id.zfill(3) +".gif"
file_path = os.path.join(self.picture_dir, full_filename)
if not os.path.isfile(file_path):
url = "https://raw.githubusercontent.com/Bart274/pokemonHA/master/Sprites/" + full_filename
picture = requests.get(url, stream=True)
if picture.status_code == 200:
with open(file_path, 'wb') as opened_file:
picture.raw.decode_content = True
shutil.copyfileobj(picture.raw, opened_file)
# Type
self.type1 = pokemonInfo[2]
self.type2 = pokemonInfo[3]
# BASE STATS
self.__hp = int(pokemonInfo[4])
self.__atk = int(pokemonInfo[5])
self.__defense = int(pokemonInfo[6])
self.__spAtk = int(pokemonInfo[7])
self.__spDef = int(pokemonInfo[8])
self.__speed = int(pokemonInfo[9])
# In Battle Stats
# The base stat is different from the in battle stat. The base stat is just used for calculating the in-battle stat
# The in battle stats are calculated based on a formula from the games
#self.battleHP = int(self.__hp + (0.5*IV) + (0.125*EV) + 60)
#self.battleATK = self.__atk + (0.5*IV) + (0.125*EV) + 5
#self.battleDEF = self.__defense + (0.5*IV) + (0.125*EV) + 5
#self.battleSpATK = self.__spAtk + (0.5*IV) + (0.125*EV) + 5
#self.battleSpDEF = self.__spDef + (0.5*IV) + (0.125*EV) + 5
#self.battleSpeed = self.__speed + (0.5*IV) + (0.125*EV) + 5
self.battleHP = round((self.__hp * 2 + IV + (EV / 4)) * self.level / 100 + 10 + self.level,0)
self.battleATK = round(((self.__atk * 2 + IV + (EV / 4)) * self.level / 100 + 5),0)