Add decor
This commit is contained in:
parent
3c852bcbd5
commit
21416bdc66
33
model.py
33
model.py
@ -12,7 +12,17 @@ def generate_random_id(_s=set()):
|
|||||||
return new_id
|
return new_id
|
||||||
|
|
||||||
|
|
||||||
class Model:
|
class Model(object):
|
||||||
|
|
||||||
|
global api_methods
|
||||||
|
api_methods = {}
|
||||||
|
|
||||||
|
def api_method():
|
||||||
|
global api_methods
|
||||||
|
def wrapper(fun):
|
||||||
|
api_methods[fun.__name__] = fun
|
||||||
|
return wrapper
|
||||||
|
|
||||||
def __init__(self, filename = "tehmodel.json"):
|
def __init__(self, filename = "tehmodel.json"):
|
||||||
self.sockets = {} # mapping: client -> socket
|
self.sockets = {} # mapping: client -> socket
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
@ -33,11 +43,16 @@ class Model:
|
|||||||
if not "sessions" in self.model:
|
if not "sessions" in self.model:
|
||||||
self.model["sessions"] = {}
|
self.model["sessions"] = {}
|
||||||
|
|
||||||
async def handle_post(self, clientid, data):
|
@api_method()
|
||||||
print(f"I have received P O S T data: {data} from {clientid}")
|
async def create_session(self, clientid) -> str:
|
||||||
print("let me tell everyone")
|
sessionname = generate_random_id()
|
||||||
await self.send_state(clientid)
|
newsession = {"id": sessionname, "owner": clientid, "clients": []}
|
||||||
return True
|
self.model["sessions"][sessionname] = newsession
|
||||||
|
return sessionname
|
||||||
|
|
||||||
|
@api_method()
|
||||||
|
async def join_session(self, clientid, sessionid):
|
||||||
|
self.model["sessions"][sessionid]
|
||||||
|
|
||||||
async def send_state(self, clientid):
|
async def send_state(self, clientid):
|
||||||
# TODO: compute state, send to client
|
# TODO: compute state, send to client
|
||||||
@ -66,12 +81,6 @@ class Model:
|
|||||||
self.model["clients"][clientname] = newclient
|
self.model["clients"][clientname] = newclient
|
||||||
return clientname
|
return clientname
|
||||||
|
|
||||||
def create_session(self, clientid) -> str:
|
|
||||||
sessionname = generate_random_id()
|
|
||||||
newsession = {"id": sessionname, "owner": clientid, "clients": []}
|
|
||||||
self.model["sessions"][sessionname] = newsession
|
|
||||||
return sessionname
|
|
||||||
|
|
||||||
def subscribe(self, clientid, socket):
|
def subscribe(self, clientid, socket):
|
||||||
if not clientid in self.sockets:
|
if not clientid in self.sockets:
|
||||||
self.sockets[clientid] = []
|
self.sockets[clientid] = []
|
||||||
|
Loading…
Reference in New Issue
Block a user