-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpokemon.py
160 lines (127 loc) · 4.88 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
'''
Okay this class allows you to call !wtp from chat
A random pokemon will appear as a silloutted bitmap and then users
will get some time to guess the pkmn before the time is up.
Upon a correct guess or when the time runs out, the correct pokemon
will display with the corresponding win or loss message.
Features might include something that lets you guess from a
specific gen of pokemon rather than all of them. For nows we will
only include gen one
'''
from os import listdir
from os.path import isfile, join, dirname, abspath, basename, splitext
import re
from random import randint
from PokeAPI import PokeAPI
from PIL import Image
import zlib
MESSAGE = "You are now playing Who's That Pokemon" \
"\n Guess the pokemon in the shadow to win."
class pkmn(object):
'''This class contains the pokemon to be guessed.
It also contains functions to output a sillouetted image
of a given pokemon (filename)
'''
papi = PokeAPI() # ayeee papiii
LOCK = False
DEX = "sugimori"
COMPRESS='compress_cache'
KURO = "kuro_cache"
TIME_TO_START = 5
TIME_TO_GUESS = 30
MAX_PKMN = 721
def __init__(self):
super(pkmn, self).__init__()
self.pkmn_data = None
self.pkmn_id = 0
self.pkmn_name = ''
self.initialize() #initialize random id and derived name
def initialize(self):
id = pkmn.generate_id()
data = self.papi.get_pokemon(id)
name = data['name']
self.pkmn_data = data
self.pkmn_id = id
self.pkmn_name = name
LOCK = True #sets lock as long as game is still being played
return
def display_message(self):
return MESSAGE
def display_img(self, silhouette = False, compress = False):
# #if sihouette edit the image to mask the pokemon in question
# if silhouette:
# return self.generate_silhouette(self.get_img_path(self.pkmn_id))
#
# #from folder return the appropriate image path to the pokemon
# return self.get_img_path(self.pkmn_id)
img = self.get_img_path(self.pkmn_id)
# if compress:
# img = self.compress_image(img)
if silhouette:
img = self.generate_silhouette(img)
return img
def check_guess(self, guess):
if guess == self.pkmn_name:
return True
else: return False
@staticmethod
def get_img_path(id, folder=DEX):
path = join(dirname(abspath(__file__)), folder)
files = []
#files = [f for f in listdir(path) if ''.join(str(id), splitext(f)) in f]
try:
files = [f for f in listdir(path) if ''.join((str(id),splitext(f)[1])) == f]
#files = [f for f in listdir(path) if str(id) in f]
except FileNotFoundError:
print("Could not find {} in {}".format(id, path))
except TypeError:
print("Type error {} in {}".format(id, path))
finally:
files.sort(key=len)
return join(path, files[0])
@staticmethod
def generate_id():
return randint(1, pkmn.MAX_PKMN)
@staticmethod
def generate_silhouette(image_path, folder=KURO):
filename = basename(image_path)
new_path = join(dirname(abspath(__file__)), folder, filename)
# don't do any work if there is already an image cached
if isfile(new_path):
return new_path
#converts image to rgba pixel data
image = Image.open(image_path).convert('RGBA')
pixel_data = list(image.getdata())
#changes each solid pixel to a black one
for i,pixel in enumerate(pixel_data):
if pixel[:3] >= (0,0,0) and pkmn.almost_equals(pixel[3], 255): #pixel[3] >= 10
pixel_data[i] = (0,0,0,255)
image.putdata(pixel_data)
image.save(new_path)
#returns the path at the end to be referenced
return new_path
@staticmethod
def almost_equals(a, b, thres=50):
return abs(a - b) < thres
@staticmethod
def compress_image(image_path, input=DEX, output=COMPRESS, compression_rate=9):
filename = basename(image_path)
old_path = join(dirname(abspath(__file__)), input, filename)
new_path = join(dirname(abspath(__file__)), output, filename)
if isfile(new_path):
return new_path
with open(old_path, "rb") as in_file:
compressed = zlib.compress(in_file.read(), compression_rate)
with open(new_path, "wb") as out_file:
out_file.write(compressed)
return new_path
'''
Design flow:
1. user launches command
2. bot generates a random number (pokemon)
3. bot displays the silloetted image of the pokemon to client
4. user attempts to guess pokemon name
5. bot compares the name to the database
6. if correct or out of time move on - else repeat 4 & 5
7. display correct image and appropriate win/lose text
'''