From 6992284cabe6185c680ad6166e663e42811328f3 Mon Sep 17 00:00:00 2001 From: iamBadgers Date: Thu, 23 May 2024 00:00:34 -0700 Subject: [PATCH] working on the loader. --- .gitignore | 2 + frontend/src/vues/ServerStats.vue | 48 +++++++++---- loader/databasesync.py | 113 ++++++++++++++++++++++++++++++ loader/sheetloader.py | 0 4 files changed, 148 insertions(+), 15 deletions(-) create mode 100644 loader/databasesync.py create mode 100644 loader/sheetloader.py diff --git a/.gitignore b/.gitignore index 4786822..a38ed89 100644 --- a/.gitignore +++ b/.gitignore @@ -2,9 +2,11 @@ node_modules frontent/dist backend/dist +loader/venv # sqlite file *.db *.sqbpro +*.csv # local env files .env.local diff --git a/frontend/src/vues/ServerStats.vue b/frontend/src/vues/ServerStats.vue index 1d16182..4c14692 100644 --- a/frontend/src/vues/ServerStats.vue +++ b/frontend/src/vues/ServerStats.vue @@ -47,11 +47,11 @@ -
+ + +
- -
-
+ @@ -79,8 +79,9 @@ margin-right: 15px; } .chart { + margin-left: 20px; width: 25%; - min-width: 500px; + width: 450px; } @@ -93,6 +94,14 @@ const dateSelect = ref(-1) const dateItems = buildDateItems() +const chartSelect = ref("active") + +const chartItems = [ + {title: "Active Characters", value: "active"}, + {title: "Role Picks", value: "picks"}, + {title: "Role Applications", value: "apps"} +] + const gameStats = ref({}) const roleStats = ref({}) @@ -136,21 +145,30 @@ function dateToMonthId(date: Date) { return (date.getUTCFullYear() - 2023) * 12 + date.getUTCMonth() } +function updateChart(stats, tag) { + chart.data = { + labels: Object.keys(stats), + datasets: [ + { + label: tag, + data: Object.values(stats).map((p) => p[tag]) + } + ] + } + chart.update() +} + watch(dateSelect, async (newValue, oldValue) => { loadData() }) watch(roleStats, async (newValue, oldValue) => { - chart.data = { - labels: Object.keys(roleStats.value), - datasets: [ - { - label: 'Active', - data: Object.values(roleStats.value).map((p) => p.active) - } - ] - } - chart.update() + updateChart(newValue, chartSelect.value) +}) + +watch(chartSelect, async (newValue, oldValue) => { + + updateChart(roleStats.value, newValue) }) onMounted(async () => { diff --git a/loader/databasesync.py b/loader/databasesync.py new file mode 100644 index 0000000..1e1b3b8 --- /dev/null +++ b/loader/databasesync.py @@ -0,0 +1,113 @@ +import csv +import sqlite3 +from datetime import datetime +from collections import namedtuple + + +Character = namedtuple('Character', + ['id', 'characterName', 'playerName', 'role', 'creationDate', 'status']) + +Game = namedtuple('Game', + ['id', 'title', 'status', 'fix', 'event', 'postdate', 'gamemaster', 'payouteb', 'payoutip', 'payoutloot']) + +Link = namedtuple('Link', ['gameId', 'gameTitle', 'characterId', 'characterName']) + +def extractCharacterFromRow(counter, row): + return Character( + counter, + row['Character'], + row['Player'], + row['Role'], + int(datetime.strptime(row['Approval Date'],'%m/%d/%Y').timestamp()) if row['Approval Date'] else 0, + row['Status']) + +def extractGameFromRow(counter, row): + return Game( + counter, + row['Game Title'], + row['Game Status'], + row['Fix'], + 0, + int(datetime.strptime(row['Game Date'],'%d-%b-%Y').timestamp()) if row['Game Date'] else 0, + row['GM'], + row['Payout (EB)'], + row['Payout (IP)'], + row['Payout (Loot)']) + +def loadCharacters(characterFileName): + characters = [] + characterNameToId = {} + + with open(characterFileName) as csvFile: + reader = csv.DictReader(csvFile) + counter = 1 + for row in reader: + character = extractCharacterFromRow(counter, row) + if character.characterName: + characters.append(character) + characterNameToId[character.characterName] = counter + counter += 1 + + return characters, characterNameToId + +def loadGames(gameFileName): + games = [] + gameTitleToId = {} + + with open(gameFileName) as csvFile: + reader = csv.DictReader(csvFile) + counter = 1 + for row in reader: + game = extractGameFromRow(counter, row) + if game.title: + games.append(game) + gameTitleToId[game.title] = counter + counter += 1 + + return games, gameTitleToId + +def loadAppsAndPicks(characterNameToId, gameTitleToId, gameFileName): + apps = [] + picks = [] + + with open(gameFileName) as csvFile: + reader = csv.DictReader(csvFile) + for row in reader: + gameTitle = row['Game Title'] + if (gameTitle): + gameId = gameTitleToId[gameTitle] + for i in range(1,7): + characterName = row['Character {} (Player)'.format(i)] + if characterName: + characterId = characterNameToId[characterName] + picks.append(Link(gameId, gameTitle, characterId, characterName)) + for i in range(7,11): + characterName = row['Character {}'.format(i)] + if (characterName): + characterId = characterNameToId[characterName] + picks.append(Link(gameId, gameTitle, characterId, characterName)) + for i in range(1,51): + characterName = row['Applicant {}'.format(i)] + if characterName: + characterId = characterNameToId[characterName] + apps.append(Link(gameId, gameTitle, characterId, characterName)) + + return apps, picks + +def main(): + characters, characterNameToId = loadCharacters("Character Data.csv") + games, gameTitleToId = loadGames("Game Data.csv") + apps, picks = loadAppsAndPicks(characterNameToId, gameTitleToId, "Game Data.csv") + + with sqlite3.connect('testdb.db') as connection: + cursor = connection.cursor() + for character in characters: + cursor.execute(""" + REPLACE INTO Characters (characterName, id, playerName, role, creationDate, status) + VALUES (?, ?, ?, ?, ?, ?) + """, [character.characterName, character.id, character.playerName, character.role, character.creationDate, character.status]) + + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/loader/sheetloader.py b/loader/sheetloader.py new file mode 100644 index 0000000..e69de29