forked from rnixik/durak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_file_logger.go
184 lines (153 loc) · 4.52 KB
/
game_file_logger.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"fmt"
"os"
"strings"
"time"
)
// GameFileLogger is implementation of GameLogger which stores logs in files
type GameFileLogger struct {
dir string
errCallback func(err error)
bufferChans map[string]chan string
stopChans map[string]chan bool
}
// NewGameFileLogger constructor for GameFileLogger
func NewGameFileLogger(dir string, errCallback func(err error)) *GameFileLogger {
_ = os.MkdirAll(dir, 0777)
return &GameFileLogger{
dir: dir,
errCallback: errCallback,
bufferChans: make(map[string]chan string, 0),
stopChans: make(map[string]chan bool, 0),
}
}
// LogGameBegins starts recording log and adds entry about beginning the game
func (l *GameFileLogger) LogGameBegins(game *Game) {
l.bufferChans[game.id] = make(chan string)
l.stopChans[game.id] = make(chan bool)
go l.writeLoop(game.id)
lines := fmt.Sprintf("ENTRY Game begins. ID=%s\n", game.id)
lines += getPlayersNames(game.players) + "\n"
lines += getCurrentStateAsLines(game)
l.bufferChans[game.id] <- lines
}
// LogPlayerActionAttack adds entry about attack
func (l *GameFileLogger) LogPlayerActionAttack(game *Game, data AttackActionData) {
lines := fmt.Sprintf("ENTRY Attack. card=%s%s;\n", data.Card.Value, data.Card.Suit)
lines += getCurrentStateAsLines(game)
l.bufferChans[game.id] <- lines
}
// LogPlayerActionDefend adds entry about defense
func (l *GameFileLogger) LogPlayerActionDefend(game *Game, data DefendActionData) {
lines := fmt.Sprintf(
"Defend. attackingCard=%s%s; defendingCard=%s%s\n",
data.AttackingCard.Value,
data.AttackingCard.Suit,
data.DefendingCard.Value,
data.DefendingCard.Suit,
)
lines += getCurrentStateAsLines(game)
l.bufferChans[game.id] <- lines
}
// LogPlayerActionPickUp adds entry about pick up
func (l *GameFileLogger) LogPlayerActionPickUp(game *Game) {
lines := fmt.Sprintf("ENTRY PickUp.\n")
lines += getCurrentStateAsLines(game)
l.bufferChans[game.id] <- lines
}
// LogPlayerActionComplete adds entry about completing a round
func (l *GameFileLogger) LogPlayerActionComplete(game *Game) {
lines := fmt.Sprintf("ENTRY Complete.\n")
lines += getCurrentStateAsLines(game)
l.bufferChans[game.id] <- lines
}
// LogGameEnds adds entry about ending game and writes file with entries
func (l *GameFileLogger) LogGameEnds(game *Game, hasLoser bool, loserIndex int) {
lines := fmt.Sprintf("ENTRY Game ends. hasLoser=%t;loserIndex=%d\n", hasLoser, loserIndex)
lines += getCurrentStateAsLines(game)
l.bufferChans[game.id] <- lines
l.stopChans[game.id] <- true
}
func (l *GameFileLogger) write(gameId string, contents string) error {
currentTime := time.Now()
dir := l.dir + "/" + fmt.Sprintf("%d%02d", currentTime.Year(), currentTime.Month())
err := os.MkdirAll(dir, 0777)
if err != nil {
return err
}
f, err := os.Create(dir + "/" + gameId + ".log")
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(contents)
if err != nil {
return err
}
return nil
}
func (l *GameFileLogger) writeLoop(gameId string) {
contents := ""
defer func() {
close(l.stopChans[gameId])
err := l.write(gameId, contents)
if err != nil {
l.errCallback(err)
}
}()
for {
select {
case lines, ok := <-l.bufferChans[gameId]:
if !ok {
return
}
contents += lines
case <-l.stopChans[gameId]:
close(l.bufferChans[gameId])
}
}
}
func getCurrentStateAsLines(game *Game) string {
line := "State:\n"
line += fmt.Sprintf("players=%d %s", len(game.players), getPlayersCards(game.players))
line += "\n"
line += fmt.Sprintf("deck=%d:%s;", len(game.deck.cards), cardsToString(game.deck.cards))
line += fmt.Sprintf("battleground=%d:%s;", len(game.battleground), cardsToString(game.battleground))
line += fmt.Sprintf("attacker=%d;", game.attackerIndex)
line += fmt.Sprintf("defender=%d;", game.defenderIndex)
line += fmt.Sprintf("trump=%s%s;", game.trumpCard.Value, game.trumpCard.Suit)
line += "\n"
return line
}
func getPlayersCards(players []*Player) string {
str := ""
for index, player := range players {
if index != 0 {
str += " "
}
isBot := "human"
if strings.HasPrefix(player.Name, "bot-") {
isBot = "bot"
}
str += fmt.Sprintf("P=%d(%s):%s;", index, isBot, cardsToString(player.cards))
}
return str
}
func getPlayersNames(players []*Player) string {
str := ""
for index, player := range players {
if index != 0 {
str += " "
}
str += fmt.Sprintf("P%d=%s;", index, player.Name)
}
return str
}
func cardsToString(cards []*Card) string {
str := ""
for _, card := range cards {
str += card.Value + card.Suit
}
return str
}