-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_system_store.go
110 lines (82 loc) · 2.05 KB
/
file_system_store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package poker
import (
"encoding/json"
"fmt"
"os"
"sort"
)
type FileSystemPlayerStore struct {
Database *json.Encoder
league League
}
func initializePlayerDBFile(file *os.File) error {
file.Seek(0, 0)
info, err := file.Stat()
if err != nil {
return fmt.Errorf("Problem getting file info from file %s, %v", file.Name(), err)
}
if info.Size() == 0 {
file.Write([]byte("[]"))
file.Seek(0, 0)
}
return nil
}
func NewFileSystemPlayerStore(file *os.File) (*FileSystemPlayerStore, error) {
err := initializePlayerDBFile(file)
if err != nil {
return nil, fmt.Errorf("Problem initialising player db file, %v", err)
}
league, err := NewLeague(file)
if err != nil {
return nil, fmt.Errorf("Problem loading player store from file %s, %v", file.Name(), err)
}
return &FileSystemPlayerStore{
Database: json.NewEncoder(&Tape{file}),
league: league,
}, nil
}
func (f *FileSystemPlayerStore) GetLeague() League {
sort.Slice(f.league, func(i, j int) bool {
return f.league[i].Wins > f.league[j].Wins
})
return f.league
}
func (f *FileSystemPlayerStore) RecordWin(name string) {
player := f.league.Find(name)
if player != nil {
player.Wins++
} else {
f.league = append(f.league, Player{name, 1})
}
f.Database.Encode(f.league)
}
func (f *FileSystemPlayerStore) GetPLayerScore(name string) int {
player := f.league.Find(name)
if player != nil {
return player.Wins
}
return 0
}
func (f *FileSystemPlayerStore) GetPlayerScore(name string) (wins int) {
for _, player := range f.GetLeague() {
if player.Name == name {
wins = player.Wins
break
}
}
return
}
func FileSystemPlayerStoreFromFile(path string) (*FileSystemPlayerStore, func(), error) {
db, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return nil, nil, fmt.Errorf("Problem opening %s %v", path, err)
}
closeFunc := func() {
db.Close()
}
store, err := NewFileSystemPlayerStore(db)
if err != nil {
return nil, nil, fmt.Errorf("problem creating file system player store, %v", err)
}
return store, closeFunc, nil
}