diff --git a/webgames/__main__.py b/webgames/__main__.py index 3ba2cf6..31a100e 100644 --- a/webgames/__main__.py +++ b/webgames/__main__.py @@ -1,7 +1,7 @@ import sys import json from uuid import UUID -from datetime import datetime +from datetime import datetime, UTC import jinja2 from bottle import Bottle, abort, request, redirect, static_file @@ -14,7 +14,12 @@ from webgames.game import GameState app = Bottle() -app.jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader('templates')) +app.jinja_env = jinja2.Environment( + loader=jinja2.FileSystemLoader('templates'), + extensions=[ + 'jinja2.ext.loopcontrols' + ] +) @app.get('/') @@ -129,7 +134,7 @@ def get_player_game_play(player_id: str, game_id: str): game = api.get_game(gameid) if game.state == GameState.LOBBY: game.begin({}, baseurl=f'{urlbase}/') - duration = str(datetime.utcnow() - game.start) + duration = str(datetime.now(UTC) - game.start) return game.render(app.jinja_env, player, duration) @@ -144,7 +149,7 @@ def post_player_game_play(player_id: str, game_id: str): game.begin(options=request.forms, urlbase=f'{urlbase}/') else: game.process_action(playerid, request.forms) - duration = str(datetime.utcnow() - game.start) + duration = str(datetime.now(UTC) - game.start) return game.render(app.jinja_env, player, duration) #@app.error(404) diff --git a/webgames/game.py b/webgames/game.py index a7502d1..2b1dd05 100644 --- a/webgames/game.py +++ b/webgames/game.py @@ -2,7 +2,7 @@ from typing import Any, Dict, List from enum import Enum from uuid import uuid4, UUID -from datetime import datetime +from datetime import datetime, UTC from webgames.puzzle import Puzzle @@ -10,6 +10,7 @@ from webgames.sudoku_puzzle import Sudoku from webgames.set_puzzle import SetPuzzle from webgames.carcassonne import Carcassonne from webgames.go import GoPuzzle +from webgames.mastermind import Mastermind from webgames.player import Player from webgames.human import HumanID @@ -47,7 +48,7 @@ class Game: def begin(self, options, urlbase) -> None: if self._state != GameState.LOBBY: raise RuntimeError(f'Can\'t start a game in {self._state} state') - self._start = datetime.utcnow() + self._start = datetime.now(UTC) self._state = GameState.RUNNING self._puzzle.begin(options, urlbase) @@ -55,8 +56,8 @@ class Game: if self._state == GameState.FINISHED: raise RuntimeError(f'Can\'t start a game in {self._state} state') if not self._start: - self._start = datetime.utcnow() - self._end = datetime.utcnow() + self._start = datetime.now(UTC) + self._end = datetime.now(UTC) self._state = GameState.FINISHED def process_action(self, player: UUID, action) -> None: diff --git a/webgames/sudoku_puzzle.py b/webgames/sudoku_puzzle.py index 6351ec4..7b805d1 100644 --- a/webgames/sudoku_puzzle.py +++ b/webgames/sudoku_puzzle.py @@ -1,6 +1,7 @@ from typing import Any, Dict from uuid import UUID +from datetime import datetime, UTC from sudokugen.solver import solve, NoSolution from sudoku_manager import Sudoku as SudokuManager @@ -120,13 +121,13 @@ class SudokuPuzzle(Puzzle): value = None else: value = int(v) - + puzzle = self._puzzles[player] if puzzle.solved: raise RuntimeError(f'Can\'t process a game action for a solved puzzle') puzzle.process_action(action) if puzzle.solved: - duration = datetime.utcnow() - self._start + duration = datetime.now(UTC) - self._start self._scores.append({ 'player': player, 'duration': duration