45 lines
808 B
Go
45 lines
808 B
Go
package store
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"git.andreafazzi.eu/andrea/testhub/models"
|
|
)
|
|
|
|
type MemoryPlayerStore struct {
|
|
store map[string]int
|
|
|
|
// A mutex is used to synchronize read/write access to the map
|
|
lock sync.RWMutex
|
|
}
|
|
|
|
func NewMemoryPlayerStore() *MemoryPlayerStore {
|
|
s := new(MemoryPlayerStore)
|
|
s.store = make(map[string]int, 0)
|
|
|
|
return s
|
|
}
|
|
|
|
func (s *MemoryPlayerStore) GetPlayerScore(player string) int {
|
|
s.lock.RLock()
|
|
defer s.lock.RUnlock()
|
|
|
|
return s.store[player]
|
|
}
|
|
|
|
func (s *MemoryPlayerStore) RecordWin(player string) {
|
|
s.lock.Lock()
|
|
defer s.lock.Unlock()
|
|
|
|
s.store[player]++
|
|
}
|
|
|
|
func (s *MemoryPlayerStore) GetLeague() []models.Player {
|
|
var result []models.Player
|
|
|
|
for player, wins := range s.store {
|
|
result = append(result, models.Player{Name: player, Wins: wins})
|
|
}
|
|
|
|
return result
|
|
}
|