forked from jekirl/poketrainer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventory.py
More file actions
165 lines (146 loc) · 6.68 KB
/
inventory.py
File metadata and controls
165 lines (146 loc) · 6.68 KB
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
from __future__ import absolute_import
import json
from collections import defaultdict
from pgoapi.protos.POGOProtos.Inventory import Item_pb2 as Inventory_Enum
class Inventory:
def __init__(self, percentages, inventory_items):
self.inventory_items = inventory_items
self.ultra_balls = 0
self.great_balls = 0
self.poke_balls = 0
self.master_balls = 0
self.potion = 0
self.hyper_potion = 0
self.super_potion = 0
self.max_potion = 0
self.lucky_eggs = 0
self.razz_berries = 0
self.pokeball_percent = (percentages[0] / 100)
self.greatball_percent = (percentages[1] / 100)
self.ultraball_percent = (percentages[2] / 100)
self.use_masterball = (percentages[3])
self.pokemon_candy = defaultdict()
self.eggs_available = []
self.incubators_available = []
self.incubators_busy = []
self.setup_inventory()
def setup_inventory(self):
for inventory_item in self.inventory_items:
item = inventory_item['inventory_item_data'].get('item', {})
item_id = item.get('item_id', -1)
item_count = item.get('count', 0)
if item_id == Inventory_Enum.ITEM_POTION:
self.potion = item_count
elif item_id == Inventory_Enum.ITEM_SUPER_POTION:
self.super_potion = item_count
elif item_id == Inventory_Enum.ITEM_MAX_POTION:
self.max_potion = item_count
elif item_id == Inventory_Enum.ITEM_HYPER_POTION:
self.hyper_potion = item_count
elif item_id == Inventory_Enum.ITEM_POKE_BALL:
self.poke_balls = item_count
elif item_id == Inventory_Enum.ITEM_GREAT_BALL:
self.great_balls = item_count
elif item_id == Inventory_Enum.ITEM_MASTER_BALL:
self.master_balls = item_count
elif item_id == Inventory_Enum.ITEM_ULTRA_BALL:
self.ultra_balls = item_count
elif item_id == Inventory_Enum.ITEM_LUCKY_EGG:
self.lucky_eggs = item_count
elif item_id == Inventory_Enum.ITEM_RAZZ_BERRY:
self.razz_berries = item_count
pokemon_family = inventory_item['inventory_item_data'].get('pokemon_family', {})
self.pokemon_candy[pokemon_family.get('family_id', -1)] = pokemon_family.get('candy', -1)
pokemon_data = inventory_item['inventory_item_data'].get('pokemon_data', {})
if pokemon_data.get('is_egg', False) and not pokemon_data.get('egg_incubator_id', False):
self.eggs_available.append(pokemon_data)
egg_incubators = inventory_item['inventory_item_data'].get('egg_incubators', {}).get('egg_incubator', [])
for incubator in egg_incubators:
if "pokemon_id" in incubator:
self.incubators_busy.append(incubator)
else:
self.incubators_available.append(incubator)
def can_attempt_catch(self):
return self.poke_balls + self.great_balls + self.ultra_balls + self.master_balls > 0
def take_pokeball(self):
self.poke_balls -= 1
def take_greatball(self):
self.great_balls -= 1
def take_masterball(self):
self.master_balls -= 1
def take_ultraball(self):
self.ultra_balls -= 1
def best_ball(self):
if self.use_masterball and self.master_balls:
return Inventory_Enum.ITEM_MASTER_BALL
elif self.ultra_balls:
return Inventory_Enum.ITEM_ULTRA_BALL
elif self.great_balls:
return Inventory_Enum.ITEM_GREAT_BALL
else:
return Inventory_Enum.ITEM_POKE_BALL
# FIXME make not bad, this should be configurable
def take_next_ball(self, capture_probability):
if self.can_attempt_catch():
if capture_probability.get(Inventory_Enum.ITEM_POKE_BALL, 0) > self.pokeball_percent and self.poke_balls:
self.take_pokeball()
return Inventory_Enum.ITEM_POKE_BALL
elif capture_probability.get(Inventory_Enum.ITEM_GREAT_BALL, 0) > self.greatball_percent and self.great_balls:
self.take_greatball()
return Inventory_Enum.ITEM_GREAT_BALL
elif capture_probability.get(Inventory_Enum.ITEM_ULTRA_BALL, 0) > self.ultraball_percent and self.ultra_balls:
self.take_ultraball()
return Inventory_Enum.ITEM_ULTRA_BALL
else:
best_ball = self.best_ball()
self.take_ball(self.best_ball())
return best_ball
else:
return -1
def take_ball(self, ball_id):
if ball_id == Inventory_Enum.ITEM_POKE_BALL:
self.poke_balls -= 1
elif ball_id == Inventory_Enum.ITEM_GREAT_BALL:
self.great_balls -= 1
elif ball_id == Inventory_Enum.ITEM_ULTRA_BALL:
self.ultra_balls -= 1
elif ball_id == Inventory_Enum.ITEM_MASTER_BALL:
self.master_balls -= 1
def has_lucky_egg(self):
for inventory_item in self.inventory_items:
item = inventory_item['inventory_item_data'].get('item', {})
item_id = item.get('item_id', -1)
if item_id == Inventory_Enum.ITEM_LUCKY_EGG:
return True
return False
def take_lucky_egg(self):
self.lucky_eggs -= 1
return Inventory_Enum.ITEM_LUCKY_EGG
def has_berry(self):
# Only Razz berries are in the game at the moment
for inventory_item in self.inventory_items:
item = inventory_item['inventory_item_data'].get('item', {})
item_id = item.get('item_id', -1)
if item_id == Inventory_Enum.ITEM_RAZZ_BERRY:
return True
return False
def take_berry(self):
self.razz_berries -= 1
return Inventory_Enum.ITEM_RAZZ_BERRY
def __str__(self):
str_ = "PokeBalls: {0}, GreatBalls: {1}, MasterBalls: {2}, UltraBalls: {3} \n " \
"Potion: {4}, Super Potion: {5}, Max Potion {6}, Hyper Potion {7}, Lucky Eggs {8}, Razz Berries {9}"
return str_.format(self.poke_balls,
self.great_balls,
self.master_balls,
self.ultra_balls,
self.potion,
self.super_potion,
self.max_potion,
self.hyper_potion,
self.lucky_eggs,
self.razz_berries)
def __repr__(self):
return self.__str__()
def to_json(self):
return json.dumps(self, default=lambda o: o.__dict__)