Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions draw.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import hanabi
from PIL import Image, ImageDraw, ImageFont
import io

def rounded_rectangle(image: ImageDraw, xy, corner_radius, fill=None, outline=None):
upper_left_point = xy[0]
Expand Down Expand Up @@ -77,7 +78,7 @@ def render_card_friend(image, x, y, color, value):
# if color == 'black': text_fill = (255, 255, 255)
image.text((x + width/2.5, y + height/8), value, font=text_font, fill=text_fill)

def draw_board_state(game, player_viewing, filename):
def draw_board_state(game, player_viewing):
width = int(400 / size)
height = (width * 16) // 9
if len(game.players) == 4:
Expand Down Expand Up @@ -196,8 +197,10 @@ def draw_board_state(game, player_viewing, filename):
if len(game.players) < 4: y -= 50/size
else: y -= 40/size
draw.text((x, y), game.last_action_description, font=text_font, fill=text_fill)

image.save(filename)
image_file = io.BytesIO()
image.save(image_file, "PNG")
image_file.seek(0)
return image_file

if __name__ == '__main__':
game = hanabi.Game(['Giacomo', 'Gabriele', 'Fabrizio'])
Expand All @@ -213,4 +216,6 @@ def draw_board_state(game, player_viewing, filename):
# game.hands['Giacomo'][0].is_value_known = True
# game.hands['Giacomo'][0].not_values = [1, 2, 3]
# game.hands['Giacomo'][0].not_colors = ['red', 'blue', 'green']
draw_board_state(game, 'Giacomo', 'image.png')
image = draw_board_state(game, 'Giacomo')
with open('image.png', 'wb') as f:
f.write(image.read())
4 changes: 3 additions & 1 deletion hanabi.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,9 @@ def main():

while True:
# print_board_state(game, game.players[game.active_player])
draw.draw_board_state(game, game.players[game.active_player], 'image.png')
image = draw.draw_board_state(game, game.players[game.active_player])
with open('image.png', 'wb') as f:
f.write(image.read())

result = check_state(game)
if result > 0:
Expand Down
13 changes: 4 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,9 @@ def add_player(server, chat_id, user_id, name, allow_repeated_players=False):

def send_game_views(bot, chat_game, last_player=''):
for name, user_id in chat_game.player_to_user.items():
# TODO: Send directly generated image, without write to disk.
filename = str(user_id) + '.png'
draw.draw_board_state(chat_game.game, name, filename)
image = draw.draw_board_state(chat_game.game, name)
try:
with open(filename, 'rb') as image:
bot.sendPhoto(user_id, image)
bot.sendPhoto(user_id, image)
except Exception as ex:
print(ex)
pass
Expand Down Expand Up @@ -173,11 +170,9 @@ def handle_game_ending(bot, chat_game):
send_game_views(bot, chat_game)
chat_id = chat_game.chat_id
game = chat_game.game
filename = str(chat_id) + '.png'
draw.draw_board_state(chat_game.game, '', filename)
image = draw.draw_board_state(chat_game.game)
try:
with open(filename, 'rb') as image:
bot.sendPhoto(chat_id, image)
bot.sendPhoto(chat_id, image)
except Exception as ex:
print(ex)

Expand Down
1 change: 0 additions & 1 deletion todo.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Todo

## Chatbot
- Send image without writing to disk.
- Dump memory to disk in order to resurrect games after crashes or disconnections

## Rendering