Skip to content

Commit

Permalink
Merge branch 'main' into paco/game-logs-2
Browse files Browse the repository at this point in the history
  • Loading branch information
PacoSchatz authored Mar 10, 2024
2 parents 367cb58 + b849ebb commit 18da4a1
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 15 deletions.
8 changes: 4 additions & 4 deletions comprl/scripts/dummy_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

def insert_users():
"""inserts four dummy user to the user database"""
user_data.add(user_name="test1", user_token="token1")
user_data.add(user_name="test2", user_token="token2")
user_data.add(user_name="test3", user_token="token3")
user_data.add(user_name="test4", user_token="token4")
user_data.add(user_name="test1", user_password="password1", user_token="token1")
user_data.add(user_name="test2", user_password="password2", user_token="token2")
user_data.add(user_name="test3", user_password="password3", user_token="token3")
user_data.add(user_name="test4", user_password="password4", user_token="token4")

logging.info("Four dummy users have been added.")

Expand Down
12 changes: 11 additions & 1 deletion comprl/scripts/new_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

from comprl.server.data import UserData
from comprl.server.data import ConnectionInfo
from comprl.server.data.interfaces import UserRole
import logging
import argparse
import uuid
import getpass

try:
import tomllib # type: ignore[import-not-found]
Expand All @@ -24,7 +26,15 @@ def insert_user(name: str):
name (str): name of the new user
"""
token = str(uuid.uuid4())
user_data.add(user_name=name, user_token=token)
password = getpass.getpass("Please enter a password for the user: ")
isAdmin = input("Do you want the new user to be an admin? (Y/N): ")
if isAdmin.lower() == "y":
role = UserRole.ADMIN
else:
role = UserRole.USER
user_data.add(
user_name=name, user_password=password, user_token=token, user_role=role
)


if __name__ == "__main__":
Expand Down
15 changes: 14 additions & 1 deletion comprl/server/data/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

from datetime import datetime
from enum import IntEnum
from enum import IntEnum, Enum

from comprl.shared.types import GameID

Expand Down Expand Up @@ -73,3 +73,16 @@ def __init__(
self.disconnected_id = None
if end_state == GameEndState.DISCONNECTED:
self.disconnected_id = user1_id if is_user1_disconnected else user2_id


class UserRole(Enum):
"""
Represents the possible user roles.
Attributes:
USER: Normal user without administrative rights.
ADMIN = User with administrative rights.
"""

USER = "user"
ADMIN = "admin"
25 changes: 20 additions & 5 deletions comprl/server/data/sql_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import dataclasses
import sqlite3

from comprl.server.data.interfaces import GameResult
from comprl.server.data.interfaces import GameResult, UserRole
from comprl.shared.types import GameID


Expand Down Expand Up @@ -120,31 +120,46 @@ def __init__(self, connection: SQLiteConnectionInfo) -> None:
f"""
CREATE TABLE IF NOT EXISTS {connection.table} (
user_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
username TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
role TEXT NOT NULL DEFAULT 'user',
token TEXT NOT NULL,
mu FLOAT NOT NULL,
sigma FLOAT NOT NULL
)"""
)

def add(
self, user_name: str, user_token: str, user_mu=25.000, user_sigma=8.333
self,
user_name: str,
user_password: str,
user_token: str,
user_role=UserRole.USER,
user_mu=25.000,
user_sigma=8.333,
) -> int:
"""
Adds a new user to the database.
Args:
user_name (str): The name of the user.
user_password (str): The password of the user.
user_token (str): The token of the user.
user_role (UserRole, optional): The role of the user.
Defaults to UserRole.USER.
user_mu (float, optional): The mu value of the user. Defaults to 25.
user_sigma (float, optional): The sigma value of the user. Defaults to 8.33.
Returns:
int: The ID of the newly added user.
"""

user_role = user_role.value

self.cursor.execute(
f"""INSERT INTO {self.table}(name, token, mu, sigma) VALUES (?,?,?,?)""",
(user_name, user_token, user_mu, user_sigma),
f"""INSERT INTO {self.table}(username, password, role, token, mu, sigma)
VALUES (?,?,?,?,?,?)""",
(user_name, user_password, user_role, user_token, user_mu, user_sigma),
)
self.cursor.execute(
f"""SELECT user_id FROM {self.table} WHERE token=?""",
Expand Down
24 changes: 20 additions & 4 deletions tests/reset_database_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,26 @@

def reset_tests():
user_data = UserData(ConfigProvider.get("user_data"))
userID1 = user_data.add(user_name="user_1", user_token=str(uuid.uuid4()))
userID2 = user_data.add(user_name="user_2", user_token=str(uuid.uuid4()))
userID3 = user_data.add(user_name="user_3", user_token=str(uuid.uuid4()))
userID4 = user_data.add(user_name="user_4", user_token=str(uuid.uuid4()))
userID1 = user_data.add(
user_name="user_1",
user_password=str(uuid.uuid4()),
user_token=str(uuid.uuid4()),
)
userID2 = user_data.add(
user_name="user_2",
user_password=str(uuid.uuid4()),
user_token=str(uuid.uuid4()),
)
userID3 = user_data.add(
user_name="user_3",
user_password=str(uuid.uuid4()),
user_token=str(uuid.uuid4()),
)
userID4 = user_data.add(
user_name="user_4",
user_password=str(uuid.uuid4()),
user_token=str(uuid.uuid4()),
)

user_data.set_matchmaking_parameters(user_id=userID1, mu=24.000, sigma=9.333)
user_data.set_matchmaking_parameters(user_id=userID2, mu=23.000, sigma=9.000)
Expand Down

0 comments on commit 18da4a1

Please sign in to comment.