no one will ever know

This commit is contained in:
Niklas Kapelle 2025-01-11 00:03:26 +01:00
parent 8583032f83
commit 39ed008f0f
Signed by: niklas
GPG Key ID: 4EB651B36D841D16
9 changed files with 150 additions and 41 deletions

View File

@ -15,11 +15,14 @@ type Config struct {
func Start(config Config) { func Start(config Config) {
repo := repo.NewInMemoryRepo() repo,err := repo.NewSQLRepo()
controller := newController(repo) if err != nil {
panic(err)
loadFromFile(controller, "./data.json") }
//
// controller := newController(repo)
// loadFromFile(controller, "./data.json")
mux := web.CreateWebserver(repo) mux := web.CreateWebserver(repo)
@ -40,7 +43,10 @@ func loadFromFile(c Controller, path string) error {
} }
for _, game := range payload { for _, game := range payload {
c.AddGame(game) _, err = c.AddGame(game)
if err != nil{
panic(err)
}
} }
return nil return nil

View File

@ -61,7 +61,11 @@ func (c *Controller) AddGame(game model.Game) (model.GameID, error) {
parsedGame.ID = id parsedGame.ID = id
c.createGameResult(parsedGame) err = c.createGameResult(parsedGame)
if err != nil {
return "", err
}
return id, nil return id, nil
} }
@ -71,15 +75,15 @@ func (c *Controller) createGameResult(game model.Game) error {
if err != nil { if err != nil {
return err return err
} }
t0p1, err := c.repo.GetPlayer(game.Team0Player0) t0p1, err := c.repo.GetPlayer(game.Team0Player1)
if err != nil { if err != nil {
return err return err
} }
t1p0, err := c.repo.GetPlayer(game.Team0Player0) t1p0, err := c.repo.GetPlayer(game.Team1Player0)
if err != nil { if err != nil {
return err return err
} }
t1p1, err := c.repo.GetPlayer(game.Team0Player0) t1p1, err := c.repo.GetPlayer(game.Team1Player1)
if err != nil { if err != nil {
return err return err
} }

View File

@ -4,12 +4,13 @@ type PlayerID string
type Player struct { type Player struct {
ID PlayerID ID PlayerID
Name string
Elo int Elo int
} }
func NewPlayer(id PlayerID) Player { func NewPlayer(name string) Player {
return Player{ return Player{
ID: id, Name: name,
Elo: 100, Elo: 1000,
} }
} }

View File

@ -63,8 +63,12 @@ func (r *InMemoryRepo) GetOrCreatePlayerID(name string) (model.PlayerID, error)
} }
} }
newPlayer := model.NewPlayer(name)
newPlayer.ID = id
// No player found. Create one. // No player found. Create one.
r.players = append(r.players, model.NewPlayer(id)) r.players = append(r.players, newPlayer)
return id, nil return id, nil
} }

View File

@ -3,6 +3,7 @@ package repo
import ( import (
"database/sql" "database/sql"
"strconv" "strconv"
"time"
"git.kapelle.org/niklas/beerpong-elo/internal/model" "git.kapelle.org/niklas/beerpong-elo/internal/model"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
@ -13,7 +14,7 @@ type SQLRepo struct {
} }
func NewSQLRepo() (Repo, error) { func NewSQLRepo() (Repo, error) {
db, err := sql.Open("mysql", "root:hunter2@/beer") db, err := sql.Open("mysql", "root:hunter2@/beer?parseTime=true")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -24,7 +25,6 @@ func NewSQLRepo() (Repo, error) {
} }
func (s *SQLRepo) AddGame(game model.Game) (model.GameID, error) { func (s *SQLRepo) AddGame(game model.Game) (model.GameID, error) {
stmt, err := s.db.Prepare("INSERT INTO Games(added,score,overtime,author,team0player0,team0player1,team1player0,team1player1) VALUES (?,?,?,?,?,?,?,?)") stmt, err := s.db.Prepare("INSERT INTO Games(added,score,overtime,author,team0player0,team0player1,team1player0,team1player1) VALUES (?,?,?,?,?,?,?,?)")
if err != nil { if err != nil {
return "0", err return "0", err
@ -32,19 +32,36 @@ func (s *SQLRepo) AddGame(game model.Game) (model.GameID, error) {
res, err := stmt.Exec(game.Added, game.Score, game.Overtime, game.Author, game.Team0Player0, game.Team0Player1, game.Team1Player0, game.Team1Player1) res, err := stmt.Exec(game.Added, game.Score, game.Overtime, game.Author, game.Team0Player0, game.Team0Player1, game.Team1Player0, game.Team1Player1)
if err != nil { if err != nil {
return "0", err return "", err
} }
id, err := res.LastInsertId() id, err := res.LastInsertId()
if err != nil { if err != nil {
return "0", err return "", err
} }
return model.GameID(strconv.Itoa(int(id))), nil return model.GameID(int64ToString(id)), nil
} }
func (s *SQLRepo) AddGameResult(model.GameResult) (model.GameResultID, error) { func (s *SQLRepo) AddGameResult(gameResult model.GameResult) (model.GameResultID, error) {
panic("unimplemented") stms, err := s.db.Prepare("INSERT INTO GameResults(game,player,startElo,endElo) VALUES (?,?,?,?)")
if err != nil {
return "", err
}
res,err := stms.Exec(gameResult.Game,gameResult.Player,gameResult.StartElo,gameResult.EndElo)
if err != nil {
return "", err
}
id, err := res.LastInsertId()
if err != nil {
return "", err
}
return model.GameResultID(int64ToString(id)),nil
} }
func (s *SQLRepo) GetAllPlayers() ([]model.Player, error) { func (s *SQLRepo) GetAllPlayers() ([]model.Player, error) {
@ -52,10 +69,10 @@ func (s *SQLRepo) GetAllPlayers() ([]model.Player, error) {
} }
func (s *SQLRepo) GetGame(id model.GameID) (*model.Game, error) { func (s *SQLRepo) GetGame(id model.GameID) (*model.Game, error) {
rows := s.db.QueryRow("SELECT id,score,overtime,author,team0player0,team0player1,team1player0,team1player1 FROM Games WHERE id = ? ", id) rows := s.db.QueryRow("SELECT id,added, score,overtime,author,team0player0,team0player1,team1player0,team1player1 FROM Games WHERE id = ? ", id)
var foundID int64 var foundID int64
// var foundAdded time.Time var foundAdded time.Time
var foundScore int64 var foundScore int64
var foundOvertime bool var foundOvertime bool
var foundAuthor int64 var foundAuthor int64
@ -65,7 +82,7 @@ func (s *SQLRepo) GetGame(id model.GameID) (*model.Game, error) {
var foundT1P0 int64 var foundT1P0 int64
var foundT1P1 int64 var foundT1P1 int64
if err := rows.Scan(&foundID,&foundScore,&foundOvertime,&foundAuthor,&foundT0P0,&foundT0P1,&foundT1P0,&foundT1P1); err != nil { if err := rows.Scan(&foundID, &foundAdded, &foundScore, &foundOvertime, &foundAuthor, &foundT0P0, &foundT0P1, &foundT1P0, &foundT1P1); err != nil {
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return nil, nil return nil, nil
} }
@ -74,7 +91,7 @@ func (s *SQLRepo) GetGame(id model.GameID) (*model.Game, error) {
game := model.Game{ game := model.Game{
ID: model.GameID(int64ToString(foundID)), ID: model.GameID(int64ToString(foundID)),
// Added: foundAdded, Added: foundAdded,
Score: int(foundScore), Score: int(foundScore),
Overtime: foundOvertime, Overtime: foundOvertime,
Author: model.PlayerID(int64ToString(foundAuthor)), Author: model.PlayerID(int64ToString(foundAuthor)),
@ -120,12 +137,14 @@ func (s *SQLRepo) GetOrCreatePlayerID(name string) (model.PlayerID, error) {
return *foundID, nil return *foundID, nil
} }
stmt, err := s.db.Prepare("INSERT INTO Players(name) VALUES (?)") stmt, err := s.db.Prepare("INSERT INTO Players(name,elo) VALUES (?,?)")
if err != nil { if err != nil {
return "", err return "", err
} }
res, err := stmt.Exec(name) newPlayer := model.NewPlayer(name)
res, err := stmt.Exec(newPlayer.Name,newPlayer.Elo)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -139,12 +158,13 @@ func (s *SQLRepo) GetOrCreatePlayerID(name string) (model.PlayerID, error) {
} }
func (s *SQLRepo) GetPlayer(id model.PlayerID) (*model.Player, error) { func (s *SQLRepo) GetPlayer(id model.PlayerID) (*model.Player, error) {
rows := s.db.QueryRow("SELECT id,name from Players WHERE id = ?", id) rows := s.db.QueryRow("SELECT id,name,elo from Players WHERE id = ?", id)
var playerID int64 var playerID int64
var playerName string var playerName string
var playerElo int64
if err := rows.Scan(&playerID, &playerName); err != nil { if err := rows.Scan(&playerID, &playerName, &playerElo); err != nil {
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return nil, nil return nil, nil
} }
@ -153,6 +173,8 @@ func (s *SQLRepo) GetPlayer(id model.PlayerID) (*model.Player, error) {
return &model.Player{ return &model.Player{
ID: model.PlayerID(strconv.Itoa(int(playerID))), ID: model.PlayerID(strconv.Itoa(int(playerID))),
Name: playerName,
Elo: int(playerElo),
}, nil }, nil
} }

View File

@ -1,6 +1,7 @@
CREATE TABLE Players ( CREATE TABLE Players (
id INT AUTO_INCREMENT PRIMARY KEY, id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL name VARCHAR(255) NOT NULL,
elo INT
); );
CREATE TABLE Games ( CREATE TABLE Games (
@ -29,3 +30,15 @@ CREATE TABLE GameResults (
FOREIGN KEY (game) REFERENCES Games(id) on DELETE SET NULL, FOREIGN KEY (game) REFERENCES Games(id) on DELETE SET NULL,
FOREIGN KEY (player) REFERENCES Players(id) on DELETE SET NULL FOREIGN KEY (player) REFERENCES Players(id) on DELETE SET NULL
) )
DELIMITER $$
CREATE TRIGGER UpdateCurrentELO
AFTER INSERT ON GameResults
FOR EACH ROW
BEGIN
UPDATE Players
SET elo = NEW.endElo
WHERE id = NEW.player;
END$$
DELIMITER ;

View File

@ -17,6 +17,12 @@ func createShema(repo repo.Repo) graphql.Schema {
"ID": &graphql.Field{ "ID": &graphql.Field{
Type: graphql.NewNonNull(graphql.ID), Type: graphql.NewNonNull(graphql.ID),
}, },
"name": &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
},
"elo": &graphql.Field{
Type: graphql.NewNonNull(graphql.Int),
},
}, },
}) })

View File

@ -3,7 +3,7 @@
import { graphql } from "./../gql"; import { graphql } from "./../gql";
import { onMount } from "svelte"; import { onMount } from "svelte";
import type { GetGameQuery } from "../gql/graphql"; import type { GetGameQuery } from "../gql/graphql";
import { useRoute } from "@dvcol/svelte-simple-router/router"; import { useRoute, link } from "@dvcol/svelte-simple-router/router";
const doc = graphql(` const doc = graphql(`
query getGame($gameID: ID!) { query getGame($gameID: ID!) {
@ -11,17 +11,25 @@
added added
overtime overtime
score score
author {
ID
name
}
team0player0 { team0player0 {
ID ID
name
} }
team0player1 { team0player1 {
ID ID
name
} }
team1player0 { team1player0 {
ID ID
name
} }
team1player1 { team1player1 {
ID ID
name
} }
} }
} }
@ -52,7 +60,7 @@
}); });
</script> </script>
<h1 class="text-2xl">Game</h1> <h1 class="text-4xl">Game</h1>
{#if loading} {#if loading}
Loading... Loading...
@ -61,5 +69,47 @@
{:else if data == null || data.game == null} {:else if data == null || data.game == null}
Game not found. Game not found.
{:else} {:else}
{data.game.score} {#snippet team(
teamname: string,
name1: string,
id1: string,
name2: string,
id2: string,
)}
<div class="mx-3">
<h2 class="text-xl">{teamname}</h2>
<div>
<a use:link href="/player/{id1}">{name1}</a>
</div>
<div>
<a use:link href="/player/{id2}">{name2}</a>
</div>
</div>
{/snippet}
Added by: {data.game.author.name} on {data.game.added}
<div class="flex justify-between">
{@render team(
"Team 1",
data.game.team0player0.name,
data.game.team0player0.ID,
data.game.team0player1.name,
data.game.team0player1.ID,
)}
<div class="text-2xl flex items-center">
<div>
<span>{data.game.score > 0 ? data.game.score : 0}</span>
-
<span>{data.game.score < 0 ? -data.game.score : 0}</span>
</div>
</div>
{@render team(
"Team 2",
data.game.team1player0.name,
data.game.team1player0.ID,
data.game.team1player1.name,
data.game.team1player1.ID,
)}
</div>
{/if} {/if}

View File

@ -9,6 +9,8 @@
query getPlayer($playerID: ID!) { query getPlayer($playerID: ID!) {
player(id: $playerID) { player(id: $playerID) {
ID ID
name
elo
} }
} }
`); `);
@ -47,5 +49,6 @@
{:else if data == null || data.player == null} {:else if data == null || data.player == null}
Game not found. Game not found.
{:else} {:else}
{data.player.ID} Player: {data.player.name} <br>
Elo: {data.player.elo}
{/if} {/if}