From 6a1e9148d6c408353ba622f43d2fca0769a34298 Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Mon, 6 Jan 2025 15:06:47 +0100 Subject: [PATCH] i don't care anymore --- internal/controller.go | 140 ++++++++++++++++++++++++++++++++++ internal/model/gameResult.go | 8 +- internal/model/player.go | 6 +- internal/repo/inMemoryRepo.go | 6 +- 4 files changed, 152 insertions(+), 8 deletions(-) create mode 100644 internal/controller.go diff --git a/internal/controller.go b/internal/controller.go new file mode 100644 index 0000000..ea4e09c --- /dev/null +++ b/internal/controller.go @@ -0,0 +1,140 @@ +package beerpongelo + +import ( + "math" + + "git.kapelle.org/niklas/beerpong-elo/internal/model" + "git.kapelle.org/niklas/beerpong-elo/internal/repo" +) + +type Controller struct { + repo repo.Repo +} + +func newController(repo repo.Repo) Controller { + return Controller{ + repo: repo, + } +} + +func (c *Controller) AddGame(game model.Game) (model.GameID, error) { + parsedGame := model.Game{ + Added: game.Added, + Score: game.Score, + Overtime: game.Overtime, + } + + author, err := c.repo.GetOrCreatePlayerID(string(game.Author)) + if err != nil { + return "", err + } + parsedGame.Author = author + + t0p0, err := c.repo.GetOrCreatePlayerID(string(game.Team0Player0)) + if err != nil { + return "", err + } + parsedGame.Team0Player0 = t0p0 + + t0p1, err := c.repo.GetOrCreatePlayerID(string(game.Team0Player1)) + if err != nil { + return "", err + } + parsedGame.Team0Player1 = t0p1 + + t1p0, err := c.repo.GetOrCreatePlayerID(string(game.Team1Player0)) + if err != nil { + return "", err + } + parsedGame.Team1Player0 = t1p0 + + t1p1, err := c.repo.GetOrCreatePlayerID(string(game.Team1Player1)) + if err != nil { + return "", err + } + parsedGame.Team1Player1 = t1p1 + + id, err := c.repo.AddGame(parsedGame) + if err != nil { + return "", err + } + + parsedGame.ID = id + + c.createGameResult(parsedGame) + + return id, nil +} + +func (c *Controller) createGameResult(game model.Game) error { + t0p0, err := c.repo.GetPlayer(game.Team0Player0) + if err != nil { + return err + } + t0p1, err := c.repo.GetPlayer(game.Team0Player0) + if err != nil { + return err + } + t1p0, err := c.repo.GetPlayer(game.Team0Player0) + if err != nil { + return err + } + t1p1, err := c.repo.GetPlayer(game.Team0Player0) + if err != nil { + return err + } + + newT0p0, newT0p1, newT1p0, newT1p1 := NewRating(float64(t0p0.Elo), float64(t0p1.Elo), float64(t1p0.Elo), float64(t1p1.Elo), game.Score) + + t0p0Result := model.GameResult{ + Game: game.ID, + Player: t0p0.ID, + StartElo: t0p0.Elo, + EndElo: int(math.Round(newT0p0)), + } + + t0p1Result := model.GameResult{ + Game: game.ID, + Player: t0p1.ID, + StartElo: t0p1.Elo, + EndElo: int(math.Round(newT0p1)), + } + + t1p0Result := model.GameResult{ + Game: game.ID, + Player: t1p0.ID, + StartElo: t1p0.Elo, + EndElo: int(math.Round(newT1p0)), + } + + t1p1Result := model.GameResult{ + Game: game.ID, + Player: t1p1.ID, + StartElo: t1p1.Elo, + EndElo: int(math.Round(newT1p1)), + } + + _,err = c.repo.AddGameResult(t0p0Result) + if err != nil{ + return err + } + + _,err = c.repo.AddGameResult(t0p1Result) + if err != nil{ + return err + } + + _,err = c.repo.AddGameResult(t1p0Result) + if err != nil{ + return err + } + + _,err = c.repo.AddGameResult(t1p1Result) + if err != nil{ + return err + } + + // TODO: Update player ELO + + return nil +} diff --git a/internal/model/gameResult.go b/internal/model/gameResult.go index 59f384e..c2cd325 100644 --- a/internal/model/gameResult.go +++ b/internal/model/gameResult.go @@ -3,7 +3,9 @@ package model type GameResultID string type GameResult struct { - ID GameResultID - Game GameID - Player PlayerID + ID GameResultID + Game GameID + Player PlayerID + StartElo int + EndElo int } diff --git a/internal/model/player.go b/internal/model/player.go index ee80ee5..fa8152e 100644 --- a/internal/model/player.go +++ b/internal/model/player.go @@ -3,11 +3,13 @@ package model type PlayerID string type Player struct { - ID PlayerID + ID PlayerID + Elo int } func NewPlayer(id PlayerID) Player { return Player{ - ID: id, + ID: id, + Elo: 100, } } diff --git a/internal/repo/inMemoryRepo.go b/internal/repo/inMemoryRepo.go index 70daa29..d108358 100644 --- a/internal/repo/inMemoryRepo.go +++ b/internal/repo/inMemoryRepo.go @@ -89,12 +89,12 @@ func (r *InMemoryRepo) AddGameResult(result model.GameResult) (model.GameResultI return id, nil } -func (r *InMemoryRepo) GetGameResult(id model.GameResultID) (*model.GameResult,error) { +func (r *InMemoryRepo) GetGameResult(id model.GameResultID) (*model.GameResult, error) { i, err := strconv.Atoi(string(id)) if err != nil { - return nil,nil + return nil, nil } - return &r.results[i],nil + return &r.results[i], nil }