no one will ever know
This commit is contained in:
parent
8583032f83
commit
39ed008f0f
@ -15,11 +15,14 @@ type Config struct {
|
||||
|
||||
func Start(config Config) {
|
||||
|
||||
repo := repo.NewInMemoryRepo()
|
||||
repo,err := repo.NewSQLRepo()
|
||||
|
||||
controller := newController(repo)
|
||||
|
||||
loadFromFile(controller, "./data.json")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
//
|
||||
// controller := newController(repo)
|
||||
// loadFromFile(controller, "./data.json")
|
||||
|
||||
mux := web.CreateWebserver(repo)
|
||||
|
||||
@ -40,7 +43,10 @@ func loadFromFile(c Controller, path string) error {
|
||||
}
|
||||
|
||||
for _, game := range payload {
|
||||
c.AddGame(game)
|
||||
_, err = c.AddGame(game)
|
||||
if err != nil{
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -61,7 +61,11 @@ func (c *Controller) AddGame(game model.Game) (model.GameID, error) {
|
||||
|
||||
parsedGame.ID = id
|
||||
|
||||
c.createGameResult(parsedGame)
|
||||
err = c.createGameResult(parsedGame)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
||||
@ -71,15 +75,15 @@ func (c *Controller) createGameResult(game model.Game) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
t0p1, err := c.repo.GetPlayer(game.Team0Player0)
|
||||
t0p1, err := c.repo.GetPlayer(game.Team0Player1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
t1p0, err := c.repo.GetPlayer(game.Team0Player0)
|
||||
t1p0, err := c.repo.GetPlayer(game.Team1Player0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
t1p1, err := c.repo.GetPlayer(game.Team0Player0)
|
||||
t1p1, err := c.repo.GetPlayer(game.Team1Player1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -4,12 +4,13 @@ type PlayerID string
|
||||
|
||||
type Player struct {
|
||||
ID PlayerID
|
||||
Name string
|
||||
Elo int
|
||||
}
|
||||
|
||||
func NewPlayer(id PlayerID) Player {
|
||||
func NewPlayer(name string) Player {
|
||||
return Player{
|
||||
ID: id,
|
||||
Elo: 100,
|
||||
Name: name,
|
||||
Elo: 1000,
|
||||
}
|
||||
}
|
||||
|
@ -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.
|
||||
r.players = append(r.players, model.NewPlayer(id))
|
||||
r.players = append(r.players, newPlayer)
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package repo
|
||||
import (
|
||||
"database/sql"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.kapelle.org/niklas/beerpong-elo/internal/model"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
@ -13,7 +14,7 @@ type SQLRepo struct {
|
||||
}
|
||||
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
@ -24,7 +25,6 @@ func NewSQLRepo() (Repo, 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 (?,?,?,?,?,?,?,?)")
|
||||
if err != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
return "0", err
|
||||
return "", err
|
||||
}
|
||||
|
||||
id, err := res.LastInsertId()
|
||||
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) {
|
||||
panic("unimplemented")
|
||||
func (s *SQLRepo) AddGameResult(gameResult model.GameResult) (model.GameResultID, error) {
|
||||
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) {
|
||||
@ -52,10 +69,10 @@ func (s *SQLRepo) GetAllPlayers() ([]model.Player, 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 foundAdded time.Time
|
||||
var foundAdded time.Time
|
||||
var foundScore int64
|
||||
var foundOvertime bool
|
||||
var foundAuthor int64
|
||||
@ -65,7 +82,7 @@ func (s *SQLRepo) GetGame(id model.GameID) (*model.Game, error) {
|
||||
var foundT1P0 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 {
|
||||
return nil, nil
|
||||
}
|
||||
@ -73,16 +90,16 @@ func (s *SQLRepo) GetGame(id model.GameID) (*model.Game, error) {
|
||||
}
|
||||
|
||||
game := model.Game{
|
||||
ID: model.GameID(int64ToString(foundID)),
|
||||
// Added: foundAdded,
|
||||
Score: int(foundScore),
|
||||
Overtime: foundOvertime,
|
||||
Author: model.PlayerID(int64ToString(foundAuthor)),
|
||||
ID: model.GameID(int64ToString(foundID)),
|
||||
Added: foundAdded,
|
||||
Score: int(foundScore),
|
||||
Overtime: foundOvertime,
|
||||
Author: model.PlayerID(int64ToString(foundAuthor)),
|
||||
|
||||
Team0Player0: model.PlayerID(int64ToString(foundT0P0)),
|
||||
Team0Player1: model.PlayerID(int64ToString(foundT0P1)),
|
||||
Team1Player0: model.PlayerID(int64ToString(foundT1P0)),
|
||||
Team1Player1: model.PlayerID(int64ToString(foundT1P1)),
|
||||
Team0Player0: model.PlayerID(int64ToString(foundT0P0)),
|
||||
Team0Player1: model.PlayerID(int64ToString(foundT0P1)),
|
||||
Team1Player0: model.PlayerID(int64ToString(foundT1P0)),
|
||||
Team1Player1: model.PlayerID(int64ToString(foundT1P1)),
|
||||
}
|
||||
|
||||
return &game, nil
|
||||
@ -120,12 +137,14 @@ func (s *SQLRepo) GetOrCreatePlayerID(name string) (model.PlayerID, error) {
|
||||
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 {
|
||||
return "", err
|
||||
}
|
||||
|
||||
res, err := stmt.Exec(name)
|
||||
newPlayer := model.NewPlayer(name)
|
||||
|
||||
res, err := stmt.Exec(newPlayer.Name,newPlayer.Elo)
|
||||
if err != nil {
|
||||
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) {
|
||||
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 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 {
|
||||
return nil, nil
|
||||
}
|
||||
@ -153,6 +173,8 @@ func (s *SQLRepo) GetPlayer(id model.PlayerID) (*model.Player, error) {
|
||||
|
||||
return &model.Player{
|
||||
ID: model.PlayerID(strconv.Itoa(int(playerID))),
|
||||
Name: playerName,
|
||||
Elo: int(playerElo),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
CREATE TABLE Players (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL
|
||||
name VARCHAR(255) NOT NULL,
|
||||
elo INT
|
||||
);
|
||||
|
||||
CREATE TABLE Games (
|
||||
@ -29,3 +30,15 @@ CREATE TABLE GameResults (
|
||||
FOREIGN KEY (game) REFERENCES Games(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 ;
|
||||
|
@ -17,6 +17,12 @@ func createShema(repo repo.Repo) graphql.Schema {
|
||||
"ID": &graphql.Field{
|
||||
Type: graphql.NewNonNull(graphql.ID),
|
||||
},
|
||||
"name": &graphql.Field{
|
||||
Type: graphql.NewNonNull(graphql.String),
|
||||
},
|
||||
"elo": &graphql.Field{
|
||||
Type: graphql.NewNonNull(graphql.Int),
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
import { graphql } from "./../gql";
|
||||
import { onMount } from "svelte";
|
||||
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(`
|
||||
query getGame($gameID: ID!) {
|
||||
@ -11,17 +11,25 @@
|
||||
added
|
||||
overtime
|
||||
score
|
||||
author {
|
||||
ID
|
||||
name
|
||||
}
|
||||
team0player0 {
|
||||
ID
|
||||
name
|
||||
}
|
||||
team0player1 {
|
||||
ID
|
||||
name
|
||||
}
|
||||
team1player0 {
|
||||
ID
|
||||
name
|
||||
}
|
||||
team1player1 {
|
||||
ID
|
||||
name
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -52,7 +60,7 @@
|
||||
});
|
||||
</script>
|
||||
|
||||
<h1 class="text-2xl">Game</h1>
|
||||
<h1 class="text-4xl">Game</h1>
|
||||
|
||||
{#if loading}
|
||||
Loading...
|
||||
@ -61,5 +69,47 @@
|
||||
{:else if data == null || data.game == null}
|
||||
Game not found.
|
||||
{: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}
|
||||
|
@ -9,6 +9,8 @@
|
||||
query getPlayer($playerID: ID!) {
|
||||
player(id: $playerID) {
|
||||
ID
|
||||
name
|
||||
elo
|
||||
}
|
||||
}
|
||||
`);
|
||||
@ -47,5 +49,6 @@
|
||||
{:else if data == null || data.player == null}
|
||||
Game not found.
|
||||
{:else}
|
||||
{data.player.ID}
|
||||
Player: {data.player.name} <br>
|
||||
Elo: {data.player.elo}
|
||||
{/if}
|
||||
|
Loading…
x
Reference in New Issue
Block a user