diff --git a/internal/beerpong-elo.go b/internal/beerpong-elo.go index 0f8c70b..b9fd9be 100644 --- a/internal/beerpong-elo.go +++ b/internal/beerpong-elo.go @@ -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 diff --git a/internal/controller.go b/internal/controller.go index ea4e09c..d6d4d6b 100644 --- a/internal/controller.go +++ b/internal/controller.go @@ -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 } diff --git a/internal/model/player.go b/internal/model/player.go index fa8152e..0838caf 100644 --- a/internal/model/player.go +++ b/internal/model/player.go @@ -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, } } diff --git a/internal/repo/inMemoryRepo.go b/internal/repo/inMemoryRepo.go index 2cbeff7..40a5727 100644 --- a/internal/repo/inMemoryRepo.go +++ b/internal/repo/inMemoryRepo.go @@ -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 } diff --git a/internal/repo/sqlRepo.go b/internal/repo/sqlRepo.go index 22501ab..b2c29b1 100644 --- a/internal/repo/sqlRepo.go +++ b/internal/repo/sqlRepo.go @@ -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 } diff --git a/internal/repo/sqlSchema.sql b/internal/repo/sqlSchema.sql index 5caae19..ee048cb 100644 --- a/internal/repo/sqlSchema.sql +++ b/internal/repo/sqlSchema.sql @@ -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 ; diff --git a/internal/web/gqlSchema.go b/internal/web/gqlSchema.go index 29bcc44..0640348 100644 --- a/internal/web/gqlSchema.go +++ b/internal/web/gqlSchema.go @@ -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), + }, }, }) diff --git a/web/src/pages/Game.svelte b/web/src/pages/Game.svelte index 14f7a9b..ad5ada6 100644 --- a/web/src/pages/Game.svelte +++ b/web/src/pages/Game.svelte @@ -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 @@ }); -