add solution word to client

This commit is contained in:
2021-06-24 12:10:59 +02:00
parent aca27cdae2
commit c0f5f3f785
11 changed files with 698258 additions and 640 deletions

View File

@ -1,6 +1,8 @@
import logging
import uuid
import datetime as dt
from . import json_websockets
from . import session
@ -9,12 +11,27 @@ class CrosswordConnection(json_websockets.JsonWebsocketConnection):
sessions = {}
last_cleanup = None
def clean_sessions():
now = dt.datetime.utcnow()
if CrosswordConnection.last_cleanup is None or (now - CrosswordConnection.last_cleanup).total_seconds() > 3600:
CrosswordConnection.last_cleanup = now
sessions_to_close = []
for key, session in CrosswordConnection.sessions.items():
if session.is_expired():
sessions_to_close.append(key)
for key in sessions_to_close:
del(CrosswordConnection.sessions[key])
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._session = None
async def send_crossword(self, sessionId: str, lang:str = "en"):
async def send_crossword(self, sessionId: str, lang: str = "en"):
if sessionId not in CrosswordConnection.sessions:
await self.send_error(msg="unknown session")
return
@ -83,6 +100,9 @@ class CrosswordConnection(json_websockets.JsonWebsocketConnection):
await self.send_crossword(sessionId, lang=lang)
# clean up old session
CrosswordConnection.clean_sessions()
async def send_error(self, msg: str):
await self.send({
'type': 'error',
@ -103,7 +123,7 @@ class CrosswordConnection(json_websockets.JsonWebsocketConnection):
sessionId = message['sessionId']
if "lang" in message:
lang = message['lang']
await self.register(sessionId=sessionId, lang = lang)
await self.register(sessionId=sessionId, lang=lang)
return
if self._session is None:

View File

@ -185,7 +185,7 @@ def create_word_grid(w: int, h: int, lang_code: str = "en", target_density=0.5):
# check if there is space before and after
if y - 1 >= 0 and grid[y - 1, x] != " ":
return False
if y + n < grid.shape[0] - 1 and grid[y+n, x] != " ":
if y + n < grid.shape[0] and grid[y+n, x] != " ":
return False
if grid.shape[0] - n < y or y < 0:
@ -214,7 +214,7 @@ def create_word_grid(w: int, h: int, lang_code: str = "en", target_density=0.5):
# check if there is space before and after
if x - 1 >= 0 and grid[y, x - 1] != " ":
return False
if x + n < grid.shape[1] - 1 and grid[y, x + n] != " ":
if x + n < grid.shape[1] and grid[y, x + n] != " ":
return False
if grid.shape[1] - n < x or x < 0:

View File

@ -4,10 +4,12 @@ from . import crossword
class Session(object):
def __init__(self) -> None:
def __init__(self, days_to_expire:int = 2) -> None:
self.crossword = None
self.datetime_created = dt.datetime.utcnow()
self.connected_sockets = set()
self.last_touched = self.datetime_created
self.days_to_expire = 2
def cleanup(self):
sockets_to_remove = []
@ -46,3 +48,17 @@ class Session(object):
self.create_crossword(lang = lang)
return self.crossword
def touch(self):
self.last_touched = dt.datetime.utcnow()
def is_expired(self):
self.cleanup()
if len(self.connected_sockets) > 0:
return False
now = dt.datetime.utcnow()
if (now - self.last_touched).days > self.days_to_expire:
return True
return False

697425
server/steam@cake: Normal file

File diff suppressed because it is too large Load Diff