2020-01-17 22:43:05 +00:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
2020-01-27 20:30:40 +00:00
|
|
|
from amanuensis.errors import (
|
2020-01-29 09:12:53 +00:00
|
|
|
ArgumentError, IndexMismatchError, MissingConfigError)
|
2020-01-23 23:13:34 +00:00
|
|
|
from amanuensis.config import prepend, json_ro, json_rw
|
2020-01-17 22:43:05 +00:00
|
|
|
|
|
|
|
class LexiconModel():
|
2020-01-27 20:30:40 +00:00
|
|
|
@staticmethod
|
2020-01-19 01:14:51 +00:00
|
|
|
def by(lid=None, name=None):
|
|
|
|
"""
|
|
|
|
Gets the LexiconModel with the given lid or username
|
|
|
|
|
|
|
|
If the lid or name simply does not match an existing lexicon, returns
|
|
|
|
None. If the lid matches the index but there is something wrong with
|
|
|
|
the lexicon's config, raises an error.
|
|
|
|
"""
|
|
|
|
if lid and name:
|
2020-01-29 09:12:53 +00:00
|
|
|
raise ArgumentError("lid and name both specified to Lexicon"
|
2020-01-27 20:30:40 +00:00
|
|
|
"Model.by()")
|
2020-01-19 01:14:51 +00:00
|
|
|
if not lid and not name:
|
2020-01-29 09:12:53 +00:00
|
|
|
raise ArgumentError("One of lid or name must be not None")
|
2020-01-19 01:14:51 +00:00
|
|
|
if not lid:
|
2020-01-23 23:13:34 +00:00
|
|
|
with json_ro('lexicon', 'index.json') as index:
|
2020-01-19 01:14:51 +00:00
|
|
|
lid = index.get(name)
|
|
|
|
if not lid:
|
|
|
|
return None
|
2020-01-23 23:13:34 +00:00
|
|
|
if not os.path.isdir(prepend('lexicon', lid)):
|
2020-01-19 01:14:51 +00:00
|
|
|
raise IndexMismatchError("lexicon={} lid={}".format(name, lid))
|
2020-01-23 23:13:34 +00:00
|
|
|
if not os.path.isfile(prepend('lexicon', lid, 'config.json')):
|
2020-01-19 01:14:51 +00:00
|
|
|
raise MissingConfigError("lid={}".format(lid))
|
|
|
|
return LexiconModel(lid)
|
|
|
|
|
2020-01-17 22:43:05 +00:00
|
|
|
def __init__(self, lid):
|
2020-01-23 23:13:34 +00:00
|
|
|
if not os.path.isdir(prepend('lexicon', lid)):
|
2020-01-17 22:43:05 +00:00
|
|
|
raise ValueError("No lexicon with lid {}".format(lid))
|
2020-01-23 23:13:34 +00:00
|
|
|
if not os.path.isfile(prepend('lexicon', lid, 'config.json')):
|
2020-01-17 22:43:05 +00:00
|
|
|
raise FileNotFoundError("Lexicon {} missing config.json".format(lid))
|
|
|
|
self.id = str(lid)
|
2020-01-23 23:13:34 +00:00
|
|
|
self.config_path = prepend('lexicon', lid, 'config.json')
|
|
|
|
with json_ro(self.config_path) as j:
|
2020-01-17 22:43:05 +00:00
|
|
|
self.config = j
|
|
|
|
|
|
|
|
def __getattr__(self, key):
|
|
|
|
if key not in self.config:
|
|
|
|
raise AttributeError(key)
|
|
|
|
return self.config.get(key)
|
|
|
|
|
2020-01-29 22:35:19 +00:00
|
|
|
def __str__(self):
|
|
|
|
return '<Lexicon {0.name}>'.format(self)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<LexiconModel lid={0.id} name={0.name}>'.format(self)
|
|
|
|
|
2020-01-31 06:06:22 +00:00
|
|
|
def edit(self):
|
|
|
|
return json_rw(self.config_path)
|
|
|
|
|
2020-01-30 21:31:39 +00:00
|
|
|
def add_log(self, message):
|
2020-01-17 22:43:05 +00:00
|
|
|
now = int(time.time())
|
2020-01-23 23:13:34 +00:00
|
|
|
with json_rw(self.config_path) as j:
|
2020-01-20 15:52:41 +00:00
|
|
|
j['log'].append([now, message])
|
|
|
|
|
|
|
|
def status(self):
|
|
|
|
if self.turn.current is None:
|
|
|
|
return "unstarted"
|
2020-01-27 20:30:40 +00:00
|
|
|
if self.turn.current > self.turn.max:
|
2020-01-20 15:52:41 +00:00
|
|
|
return "completed"
|
2020-01-27 20:30:40 +00:00
|
|
|
return "ongoing"
|
2020-01-31 19:56:06 +00:00
|
|
|
|
|
|
|
def can_add_character(self, uid):
|
|
|
|
return (
|
|
|
|
# Players can't add more characters than chars_per_player
|
|
|
|
(len(self.get_characters_for_player(uid))
|
|
|
|
< self.join.chars_per_player)
|
|
|
|
# Characters can only be added before the game starts
|
|
|
|
and not self.turn.current)
|
|
|
|
|
|
|
|
def get_characters_for_player(self, uid=None):
|
|
|
|
return [
|
|
|
|
char for char in self.character.values()
|
|
|
|
if uid is None or char.player == uid]
|