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 }