Skip to content

Commit

Permalink
Changed models to match singleplayer backend
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasrberg authored and root committed Jul 2, 2024
1 parent 578f390 commit b6f3e36
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
7 changes: 4 additions & 3 deletions src/webapp/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def handle_joinGame(json_data):
data = json.loads(json_data or 'null')
try:
pair_id = data["pair_id"]
difficulty_id = data["difficulty_id"]
except (KeyError, TypeError):
pair_id = ''
app.logger.error("No pair id for " + request.sid)
Expand All @@ -118,9 +119,9 @@ def handle_joinGame(json_data):

else:
game_id = uuid.uuid4().hex
labels = models.get_n_labels(setup.NUM_GAMES)
labels = models.get_n_labels(setup.NUM_GAMES, difficulty_id)
today = datetime.datetime.today()
models.insert_into_games(game_id, json.dumps(labels), today)
models.insert_into_games(game_id, json.dumps(labels), today, difficulty_id)
models.insert_into_players(player_id, game_id, "Waiting")
models.insert_into_mulitplayer(game_id, player_id, pair_id)
player_nr = "player_1"
Expand Down Expand Up @@ -284,7 +285,7 @@ def get_label(game_id):
label = labels[game.session_num - 1]
norwegian_label = models.to_norwegian(label)
data = {"label": norwegian_label}
return json.dumps(data)
return json.dumps(data["label"])


def translate_probabilities(labels):
Expand Down
50 changes: 34 additions & 16 deletions src/webapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,24 @@ class Games(db.Model):
session_num = db.Column(db.Integer, default=1)
labels = db.Column(db.String(64))
date = db.Column(db.DateTime)

players = db.relationship("Players", uselist=False, back_populates="game")
difficulty_id = db.Column(
db.Integer, db.ForeignKey("difficulty.id"), default=1)
players = db.relationship(
"Players", uselist=False, back_populates="game", cascade="all, delete"
)
mulitplay = db.relationship(
"MulitPlayer", uselist=False, back_populates="game"
"MulitPlayer", uselist=False, back_populates="game",
cascade="all, delete"
)


class Scores(db.Model):
"""
This is the Scores model in the database. It is important that the
inserted values match the column values.
"""

score_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(32))
player_id = db.Column(db.NVARCHAR(32), db.ForeignKey("players.player_id"))
score = db.Column(db.Integer, nullable=False)
date = db.Column(db.Date)

Expand All @@ -57,7 +60,8 @@ class Players(db.Model):
"""

player_id = db.Column(db.NVARCHAR(32), primary_key=True)
game_id = db.Column(db.NVARCHAR(32), db.ForeignKey("games.game_id"), nullable=False)
game_id = db.Column(db.NVARCHAR(32), db.ForeignKey(
"games.game_id"), nullable=False)
state = db.Column(db.String(32), nullable=False)

game = db.relationship("Games", back_populates="players")
Expand Down Expand Up @@ -88,6 +92,21 @@ class Labels(db.Model):

english = db.Column(db.String(32), primary_key=True)
norwegian = db.Column(db.String(32))
difficulty_id = db.Column(
db.Integer, db.ForeignKey("difficulty.id"), default=1)

class User(db.Model):
"""
This is user model in the database to store username and psw for
administrators.
"""
username = db.Column(db.String(64), primary_key=True)
password = db.Column(db.String(256))


class Difficulty(db.Model):
id = db.Column(db.Integer, primary_key=True)
difficulty = db.Column(db.String(32), nullable=False)


# Functions to manipulate the tables above
Expand All @@ -101,7 +120,7 @@ def create_tables(app):
return True


def insert_into_games(game_id, labels, date):
def insert_into_games(game_id, labels, date, difficulty_id):
"""
Insert values into Games table.
Expand All @@ -117,7 +136,7 @@ def insert_into_games(game_id, labels, date):
):

try:
game = Games(game_id=game_id, labels=labels, date=date)
game = Games(game_id=game_id, labels=labels, date=date, difficulty_id=difficulty_id)
db.session.add(game)
db.session.commit()
return True
Expand All @@ -130,7 +149,7 @@ def insert_into_games(game_id, labels, date):
)


def insert_into_scores(name, score, date):
def insert_into_scores(player_id, score, date):
"""
Insert values into Scores table.
Expand All @@ -142,12 +161,12 @@ def insert_into_scores(name, score, date):
score_int_or_float = isinstance(score, float) or isinstance(score, int)

if (
isinstance(name, str)
isinstance(player_id, str)
and score_int_or_float
and isinstance(date, datetime.date)
):
try:
score = Scores(name=name, score=score, date=date)
score = Scores(player_id=player_id, score=score, date=date)
db.session.add(score)
db.session.commit()
return True
Expand All @@ -159,7 +178,6 @@ def insert_into_scores(name, score, date):
"float and date has to be datetime.date."
)


def insert_into_players(player_id, game_id, state):
"""
Insert values into Players table.
Expand Down Expand Up @@ -430,21 +448,21 @@ def insert_into_labels(english, norwegian):
raise UserError("English and norwegian must be strings")


def get_n_labels(n):
def get_n_labels(n, difficulty_id):
"""
Reads all rows from database and chooses n random labels in a list
Reads all rows from database and chooses n random labels in a list.
"""
try:
# read all english labels in database
labels = Labels.query.all()
labels = Labels.query.filter(
Labels.difficulty_id <= difficulty_id).all()
english_labels = [str(label.english) for label in labels]
random_list = random.sample(english_labels, n)
return random_list

except Exception as e:
raise Exception("Could not read Labels table: " + str(e))


def get_all_labels():
"""
Reads all labels from database
Expand Down

0 comments on commit b6f3e36

Please sign in to comment.