chore: migrate from utcnow to now(utc)

This commit is contained in:
s3lph 2024-11-17 04:44:59 +01:00
parent 0346cdb8fd
commit 2bc089d988
Signed by: s3lph
GPG key ID: 0AA29A52FB33CFB5
3 changed files with 17 additions and 10 deletions

View file

@ -1,7 +1,7 @@
import sys import sys
import json import json
from uuid import UUID from uuid import UUID
from datetime import datetime from datetime import datetime, UTC
import jinja2 import jinja2
from bottle import Bottle, abort, request, redirect, static_file from bottle import Bottle, abort, request, redirect, static_file
@ -14,7 +14,12 @@ from webgames.game import GameState
app = Bottle() 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('/') @app.get('/')
@ -129,7 +134,7 @@ def get_player_game_play(player_id: str, game_id: str):
game = api.get_game(gameid) game = api.get_game(gameid)
if game.state == GameState.LOBBY: if game.state == GameState.LOBBY:
game.begin({}, baseurl=f'{urlbase}/') 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) 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}/') game.begin(options=request.forms, urlbase=f'{urlbase}/')
else: else:
game.process_action(playerid, request.forms) 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) return game.render(app.jinja_env, player, duration)
#@app.error(404) #@app.error(404)

View file

@ -2,7 +2,7 @@ from typing import Any, Dict, List
from enum import Enum from enum import Enum
from uuid import uuid4, UUID from uuid import uuid4, UUID
from datetime import datetime from datetime import datetime, UTC
from webgames.puzzle import Puzzle from webgames.puzzle import Puzzle
@ -10,6 +10,7 @@ from webgames.sudoku_puzzle import Sudoku
from webgames.set_puzzle import SetPuzzle from webgames.set_puzzle import SetPuzzle
from webgames.carcassonne import Carcassonne from webgames.carcassonne import Carcassonne
from webgames.go import GoPuzzle from webgames.go import GoPuzzle
from webgames.mastermind import Mastermind
from webgames.player import Player from webgames.player import Player
from webgames.human import HumanID from webgames.human import HumanID
@ -47,7 +48,7 @@ class Game:
def begin(self, options, urlbase) -> None: def begin(self, options, urlbase) -> None:
if self._state != GameState.LOBBY: if self._state != GameState.LOBBY:
raise RuntimeError(f'Can\'t start a game in {self._state} state') 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._state = GameState.RUNNING
self._puzzle.begin(options, urlbase) self._puzzle.begin(options, urlbase)
@ -55,8 +56,8 @@ class Game:
if self._state == GameState.FINISHED: if self._state == GameState.FINISHED:
raise RuntimeError(f'Can\'t start a game in {self._state} state') raise RuntimeError(f'Can\'t start a game in {self._state} state')
if not self._start: if not self._start:
self._start = datetime.utcnow() self._start = datetime.now(UTC)
self._end = datetime.utcnow() self._end = datetime.now(UTC)
self._state = GameState.FINISHED self._state = GameState.FINISHED
def process_action(self, player: UUID, action) -> None: def process_action(self, player: UUID, action) -> None:

View file

@ -1,6 +1,7 @@
from typing import Any, Dict from typing import Any, Dict
from uuid import UUID from uuid import UUID
from datetime import datetime, UTC
from sudokugen.solver import solve, NoSolution from sudokugen.solver import solve, NoSolution
from sudoku_manager import Sudoku as SudokuManager from sudoku_manager import Sudoku as SudokuManager
@ -120,13 +121,13 @@ class SudokuPuzzle(Puzzle):
value = None value = None
else: else:
value = int(v) value = int(v)
puzzle = self._puzzles[player] puzzle = self._puzzles[player]
if puzzle.solved: if puzzle.solved:
raise RuntimeError(f'Can\'t process a game action for a solved puzzle') raise RuntimeError(f'Can\'t process a game action for a solved puzzle')
puzzle.process_action(action) puzzle.process_action(action)
if puzzle.solved: if puzzle.solved:
duration = datetime.utcnow() - self._start duration = datetime.now(UTC) - self._start
self._scores.append({ self._scores.append({
'player': player, 'player': player,
'duration': duration 'duration': duration