moved repo and inMemoryRepo in 2 files
This commit is contained in:
parent
9997ed4fd2
commit
e7b1241cfe
84
internal/repo/inMemoryRepo.go
Normal file
84
internal/repo/inMemoryRepo.go
Normal file
@ -0,0 +1,84 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
model "git.kapelle.org/niklas/beerpong-elo/internal/model"
|
||||
)
|
||||
|
||||
type InMemoryRepo struct {
|
||||
games []model.Game
|
||||
players []model.Player
|
||||
}
|
||||
|
||||
func NewInMemoryRepo() Repo {
|
||||
return &InMemoryRepo{
|
||||
games: []model.Game{},
|
||||
players: []model.Player{},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *InMemoryRepo) AddGame(game model.InputGame) model.GameID {
|
||||
id := len(r.games)
|
||||
|
||||
authID := r.GetOrCreatePlayerID(game.Author)
|
||||
t0p0ID := r.GetOrCreatePlayerID(game.Team0Player0)
|
||||
t0p1ID := r.GetOrCreatePlayerID(game.Team0Player1)
|
||||
t1p0ID := r.GetOrCreatePlayerID(game.Team1Player0)
|
||||
t1p1ID := r.GetOrCreatePlayerID(game.Team1Player1)
|
||||
|
||||
parsedGame := model.Game{
|
||||
ID: model.GameID(rune(id)),
|
||||
Added: game.Added,
|
||||
Author: authID,
|
||||
Team0Player0: t0p0ID,
|
||||
Team0Player1: t0p1ID,
|
||||
Team1Player0: t1p1ID,
|
||||
Team1Player1: t1p0ID,
|
||||
Score: game.Score,
|
||||
Overtime: game.Overtime,
|
||||
}
|
||||
|
||||
r.games = append(r.games, parsedGame)
|
||||
|
||||
return model.GameID((rune(id)))
|
||||
}
|
||||
|
||||
func (r *InMemoryRepo) GetGame(id model.GameID) model.Game {
|
||||
i, err := strconv.Atoi(string(id))
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return r.games[i]
|
||||
}
|
||||
|
||||
func (r *InMemoryRepo) GetAllGames() []model.Game {
|
||||
return r.games
|
||||
}
|
||||
|
||||
func (r *InMemoryRepo) GetOrCreatePlayerID(name string) model.PlayerID {
|
||||
id := model.PlayerID(name)
|
||||
|
||||
for _, player := range r.players {
|
||||
if player.ID == id {
|
||||
return id
|
||||
}
|
||||
}
|
||||
|
||||
// No player found. Create one.
|
||||
r.players = append(r.players, model.NewPlayer(id))
|
||||
|
||||
return id
|
||||
}
|
||||
|
||||
func (r *InMemoryRepo) GetPlayer(id model.PlayerID) *model.Player {
|
||||
for _, player := range r.players {
|
||||
if player.ID == id {
|
||||
return &player
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -1,8 +1,6 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
model "git.kapelle.org/niklas/beerpong-elo/internal/model"
|
||||
)
|
||||
|
||||
@ -14,80 +12,3 @@ type Repo interface {
|
||||
GetOrCreatePlayerID(name string) model.PlayerID
|
||||
GetPlayer(id model.PlayerID) *model.Player
|
||||
}
|
||||
|
||||
type InMemoryRepo struct {
|
||||
games []model.Game
|
||||
players []model.Player
|
||||
}
|
||||
|
||||
func NewInMemoryRepo() Repo {
|
||||
return &InMemoryRepo{
|
||||
games: []model.Game{},
|
||||
players: []model.Player{},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *InMemoryRepo) AddGame(game model.InputGame) model.GameID {
|
||||
id := len(r.games)
|
||||
|
||||
authID := r.GetOrCreatePlayerID(game.Author)
|
||||
t0p0ID := r.GetOrCreatePlayerID(game.Team0Player0)
|
||||
t0p1ID := r.GetOrCreatePlayerID(game.Team0Player1)
|
||||
t1p0ID := r.GetOrCreatePlayerID(game.Team1Player0)
|
||||
t1p1ID := r.GetOrCreatePlayerID(game.Team1Player1)
|
||||
|
||||
parsedGame := model.Game{
|
||||
ID: model.GameID(rune(id)),
|
||||
Added: game.Added,
|
||||
Author: authID,
|
||||
Team0Player0: t0p0ID,
|
||||
Team0Player1: t0p1ID,
|
||||
Team1Player0: t1p1ID,
|
||||
Team1Player1: t1p0ID,
|
||||
Score: game.Score,
|
||||
Overtime: game.Overtime,
|
||||
}
|
||||
|
||||
r.games = append(r.games, parsedGame)
|
||||
|
||||
return model.GameID((rune(id)))
|
||||
}
|
||||
|
||||
func (r *InMemoryRepo) GetGame(id model.GameID) model.Game {
|
||||
i, err := strconv.Atoi(string(id))
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return r.games[i]
|
||||
}
|
||||
|
||||
func (r *InMemoryRepo) GetAllGames() []model.Game {
|
||||
return r.games
|
||||
}
|
||||
|
||||
func (r *InMemoryRepo) GetOrCreatePlayerID(name string) model.PlayerID {
|
||||
id := model.PlayerID(name)
|
||||
|
||||
for _, player := range r.players {
|
||||
if player.ID == id {
|
||||
return id
|
||||
}
|
||||
}
|
||||
|
||||
// No player found. Create one.
|
||||
r.players = append(r.players, model.NewPlayer(id))
|
||||
|
||||
return id
|
||||
}
|
||||
|
||||
func (r *InMemoryRepo) GetPlayer(id model.PlayerID) *model.Player {
|
||||
for _, player := range r.players {
|
||||
if player.ID == id {
|
||||
return &player
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user