Skip to content

Commit

Permalink
Merge pull request #47 from martius-lab/carina/database-layout_game-s…
Browse files Browse the repository at this point in the history
…tatistic

Database layout for the game statistics
  • Loading branch information
Cari1111 authored Jan 8, 2024
2 parents a15134b + 08db5e5 commit fe5df23
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
86 changes: 86 additions & 0 deletions teamprojekt_competition_server/server/databases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"""Databases"""

import sqlite3

GAME_DB_NAME = "game"


class GameDatabase:
"""Database to store the games"""

def __init__(self) -> None:
self.connection = sqlite3.connect(
"teamprojekt_competition_server/server/COMP_database.db"
)
self.cursor = self.connection.cursor()
self.cursor.execute(
f"""SELECT name FROM sqlite_master
WHERE type='table' AND name='{GAME_DB_NAME}'"""
)
if self.cursor.fetchone() is None:
self.cursor.execute(f"CREATE TABLE {GAME_DB_NAME}(winnerID, loserID)")

def insert_game(self, winnerID: int, loserID: int) -> int | None:
"""insert a new game into the database
Args:
winnerID (int): playerID of the winner
loserID (int): playerID of the loser
Returns:
int: ID of the game
"""
# TODO separate record file to store game steps
self.cursor.execute(f"INSERT INTO game VALUES ({winnerID}, {loserID})")
self.connection.commit()
return self.cursor.lastrowid

def get_playerIDs(self, gameID: int) -> tuple[int, int]:
"""get the IDs of the players that participated in a game
Args:
gameID (int): ID of the game
Returns:
(int, int): IDs of the winner and the loser
"""
res = self.cursor.execute(
f"SELECT winnerID, loserID FROM {GAME_DB_NAME} WHERE rowid={gameID}"
)
players = res.fetchone()
return players

def get_gameIDs(self, playerID: int) -> list[int]:
"""get all games that a player participated in
Args:
playerID (int): ID of the player
Returns:
list[int]: IDs of the games
"""
res = self.cursor.execute(
f"""SELECT rowid FROM {GAME_DB_NAME}
WHERE winnerID={playerID} OR loserID={playerID}"""
)
games = []
for (result,) in res.fetchall():
games.append(result)
return games

def get_won_gameIDs(self, playerID: int) -> list[int]:
"""get all games that a player has won
Args:
playerID (int): ID of the player
Returns:
list[int]: IDs of the games
"""
res = self.cursor.execute(
f"SELECT rowid FROM {GAME_DB_NAME} WHERE winnerID={playerID}"
)
games = []
for (result,) in res.fetchall():
games.append(result)
return games
13 changes: 13 additions & 0 deletions teamprojekt_competition_server/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .server import COMPServer
from .interfaces import IGame, IPlayer
from .game_manager import game_manager
from .databases import GameDatabase


# from .gymgame import GymGame
Expand Down Expand Up @@ -50,5 +51,17 @@ def main():
server.start()


def test_database():
"""function to test database"""
game_db = GameDatabase()
gameID = game_db.insert_game(12, 23)
gameID = game_db.insert_game(52, 12)
print(gameID)
print(game_db.get_playerIDs(gameID=gameID))
print(game_db.get_gameIDs(playerID=12))
print(game_db.get_won_gameIDs(playerID=12))


if __name__ == "__main__":
main()
# test_database()

0 comments on commit fe5df23

Please sign in to comment.