forked from jromang/picochess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgn.py
79 lines (75 loc) · 3.65 KB
/
pgn.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
# Copyright (C) 2013-2014 Jean-Francois Romang ([email protected])
# Shivkumar Shivaji ()
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import threading
import base64
import chess
import chess.pgn
import datetime
import logging
import requests
from utilities import *
class PgnDisplay(Display, threading.Thread):
def __init__(self, pgn_file_name, email=None, key=None):
super(PgnDisplay, self).__init__()
self.file_name = pgn_file_name
self.email = email
if email and key:
self.key = base64.b64decode(str.encode(key)).decode("utf-8")
def run(self):
while True:
#Check if we have something to display
try:
message = self.message_queue.get()
if message == Message.GAME_ENDS and message.moves:
logging.debug('Saving game to [' + self.file_name+']')
game = node = chess.pgn.Game()
for move in message.moves:
node = node.add_main_variation(move)
# Headers
game.headers["Event"] = 'PicoChess game'
game.headers["Site"] = get_location()
game.headers["Date"] = datetime.date.today().strftime('%Y.%m.%d')
if message.result == GameResult.ABORT:
game.headers["Result"] = "*"
elif message.result == GameResult.STALEMATE:
game.headers["Result"] = "1/2-1/2"
elif message.result in (GameResult.MATE, GameResult.TIME_CONTROL):
try:
game.headers["Result"] = "0-1" if message.turn == chess.WHITE else "1-0"
except AttributeError:
game.headers["Result"] = "1-0"
if message.mode == Mode.PLAY_WHITE:
game.headers["White"] = self.email.split('@')[0] if self.email else 'Player'
game.headers["Black"] = "PicoChess"
if message.mode == Mode.PLAY_BLACK:
game.headers["White"] = "PicoChess"
game.headers["Black"] = self.email.split('@')[0] if self.email else 'Player'
# Save to file
file = open(self.file_name, "a")
exporter = chess.pgn.FileExporter(file)
game.export(exporter)
file.close()
# Send email
if self.email:
out = requests.post("https://api.mailgun.net/v2/picochess.org/messages",
auth=("api", self.key),
data={"from": "Your PicoChess computer <[email protected]>",
"to": self.email,
"subject": "Game PGN",
"text": str(game)})
logging.debug(out)
except queue.Empty:
pass