-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
249 lines (217 loc) · 7.58 KB
/
main.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
import (
"bytes"
"context"
"fmt"
"image"
"image/color"
"image/draw"
"image/gif"
"image/png"
"io/ioutil"
"net/http"
"os"
pb "github.com/domino14/liwords/rpc/api/proto/game_service"
macondopb "github.com/domino14/macondo/gen/api/proto/macondo"
)
var boardConfig = []string{
"= ' = ' =",
" - \" \" - ",
" - ' ' - ",
"' - ' - '",
" - - ",
" \" \" \" \" ",
" ' ' ' ' ",
"= ' * ' =",
" ' ' ' ' ",
" \" \" \" \" ",
" - - ",
"' - ' - '",
" - ' ' - ",
" - \" \" - ",
"= ' = ' =",
}
var tileSrc = map[byte][2]int{
'A': {0, 0}, 'B': {0, 1}, 'C': {0, 2}, 'D': {0, 3}, 'E': {0, 4},
'F': {0, 5}, 'G': {0, 6}, 'H': {0, 7}, 'I': {0, 8}, 'J': {0, 9},
'K': {1, 0}, 'L': {1, 1}, 'M': {1, 2}, 'N': {1, 3}, 'O': {1, 4},
'P': {1, 5}, 'Q': {1, 6}, 'R': {1, 7}, 'S': {1, 8}, 'T': {1, 9},
'U': {2, 0}, 'V': {2, 1}, 'W': {2, 2}, 'X': {2, 3}, 'Y': {2, 4},
'Z': {2, 5}, 'a': {2, 6}, 'b': {2, 7}, 'c': {2, 8}, 'd': {2, 9},
'e': {3, 0}, 'f': {3, 1}, 'g': {3, 2}, 'h': {3, 3}, 'i': {3, 4},
'j': {3, 5}, 'k': {3, 6}, 'l': {3, 7}, 'm': {3, 8}, 'n': {3, 9},
'o': {4, 0}, 'p': {4, 1}, 'q': {4, 2}, 'r': {4, 3}, 's': {4, 4},
't': {4, 5}, 'u': {4, 6}, 'v': {4, 7}, 'w': {4, 8}, 'x': {4, 9},
'y': {5, 0}, 'z': {5, 1}, '?': {5, 2},
}
var boardSrc = map[byte][2]int{
'-': {5, 3}, '=': {5, 4},
'\'': {5, 5}, '"': {5, 6}, '*': {5, 7}, ' ': {5, 8},
}
// From liwords/liwords-ui/src/color_modes.scss
// color-board-dls: #b9e7f5,
// color-board-dws: #f6c0c0,
// color-board-tws: #a92e2e,
// color-board-tls: #3b88ca,
// color-board-empty: #ffffff,
// color-tile-background: #6b268b,
// color-tile-background-secondary: #cfb7d1,
// color-tile-background-tertiary: #955f9a,
// color-tile-background-quaternary: #dec5e4,
// color-tile-blank-text: #6b268b,
// color-tile-text: #ffffff,
// color-tile-last-background: #f4b000,
// color-tile-last-text: #414141,
// color-tile-last-blank: #414141,
var colorPalette = []color.Color{
color.RGBA{0xb9, 0xe7, 0xf5, 0xff},
color.RGBA{0xf6, 0xc0, 0x0c, 0xff},
color.RGBA{0xa9, 0x2e, 0x2e, 0xff},
color.RGBA{0x3b, 0x88, 0xca, 0xff},
color.RGBA{0xff, 0xff, 0xff, 0xff},
color.RGBA{0x6b, 0x26, 0x8b, 0xff},
color.RGBA{0xcf, 0xb7, 0xd1, 0xff},
color.RGBA{0x95, 0xf5, 0x9a, 0xff},
color.RGBA{0xde, 0xc5, 0xe4, 0xff},
color.RGBA{0x6b, 0x26, 0x8b, 0xff},
color.RGBA{0xf4, 0xb0, 0x00, 0xff},
color.RGBA{0x41, 0x41, 0x41, 0xff} }
// Doubled because of retina screen.
const squareDim = 2 * 34
func loadTilesImg() (image.Image, error) {
tilesBytes, err := ioutil.ReadFile("data/tiles.png")
if err != nil {
return nil, err
}
img, err := png.Decode(bytes.NewReader(tilesBytes))
if err != nil {
return nil, err
}
bounds := img.Bounds()
expectedX := 10 * squareDim
expectedY := 6 * squareDim
if bounds.Min.X != 0 || bounds.Min.Y != 0 || bounds.Dx() != expectedX || bounds.Dy() != expectedY {
return nil, fmt.Errorf("unexpected size: %s vs %s", bounds.String(), image.Pt(expectedX, expectedY))
}
return img, nil
}
func AnimateGame(tilesImg image.Image, boardConfig []string, hist *macondopb.GameHistory) (*gif.GIF, error) {
img := image.NewPaletted(image.Rect(0, 0, 15*squareDim, 15*squareDim), colorPalette)
gameGif := &gif.GIF{}
// Draw the board.
for r := 0; r < 15; r++ {
y := r * squareDim
for c := 0; c < 15; c++ {
x := c * squareDim
b := boardConfig[r][c]
srcPt, ok := boardSrc[b]
if !ok {
srcPt = boardSrc[' ']
}
draw.Draw(img, image.Rect(x, y, x+squareDim, y+squareDim), tilesImg,
image.Pt(srcPt[1]*squareDim, srcPt[0]*squareDim), draw.Over)
}
}
gameGif.Image = append(gameGif.Image, img)
gameGif.Delay = append(gameGif.Delay, 100)
prevImg := img
for i := range hist.Events {
evtImg := image.NewPaletted(prevImg.Bounds(), colorPalette)
draw.Draw(evtImg, evtImg.Bounds(), prevImg, image.Pt(0, 0), draw.Over)
removePhony, err := drawEvent(*hist.Events[i], evtImg, tilesImg)
if err != nil {
return &gif.GIF{}, fmt.Errorf("Error drawing event: %v", err)
}
if removePhony {
draw.Draw(evtImg, evtImg.Bounds(), gameGif.Image[i-1], image.Pt(0, 0), draw.Over)
}
gameGif.Image = append(gameGif.Image, evtImg)
gameGif.Delay = append(gameGif.Delay, 100)
prevImg = evtImg
}
return gameGif, nil
}
func drawPlay(evt macondopb.GameEvent, boardImg *image.Paletted, tilesImg image.Image) {
var right int
var down int
if evt.Direction == macondopb.GameEvent_HORIZONTAL {
right = len(evt.PlayedTiles)
down = 1
} else {
right = 1
down = len(evt.PlayedTiles)
}
row := int(evt.GetRow())
column := int(evt.GetColumn())
nRows := row + down
nCols := column + right
idx := 0
fmt.Printf("%s %s\n", evt.Position, evt.PlayedTiles)
for i := row; i < nRows; i++ {
y := i * squareDim
for j := column; j < nCols; j++ {
x := j * squareDim
letter := evt.PlayedTiles[idx]
if letter != '.' {
srcPt := tileSrc[letter]
draw.Draw(boardImg, image.Rect(x, y, x+squareDim, y+squareDim), tilesImg,
image.Pt(srcPt[1]*squareDim, srcPt[0]*squareDim), draw.Over)
}
idx++
}
}
}
func drawEvent(evt macondopb.GameEvent, boardImg *image.Paletted, tilesImg image.Image) (bool, error) {
evtType := evt.GetType()
removePhony := false
switch evtType {
case macondopb.GameEvent_TILE_PLACEMENT_MOVE:
fmt.Printf("Tile Placement Play ")
drawPlay(evt, boardImg, tilesImg)
case macondopb.GameEvent_PHONY_TILES_RETURNED:
fmt.Printf("Phony tiles returned!\n")
removePhony = true
case macondopb.GameEvent_PASS:
fmt.Printf("Pass.\n")
case macondopb.GameEvent_CHALLENGE_BONUS:
fmt.Printf("Challenge bonus.\n")
case macondopb.GameEvent_END_RACK_PTS:
fmt.Printf("End rack points.\n")
case macondopb.GameEvent_EXCHANGE:
fmt.Printf("Exchange %v\n", evt.Exchanged)
case macondopb.GameEvent_END_RACK_PENALTY:
fmt.Printf("End rack penalty.\n")
case macondopb.GameEvent_TIME_PENALTY:
fmt.Printf("Time penalty.\n")
case macondopb.GameEvent_UNSUCCESSFUL_CHALLENGE_TURN_LOSS:
fmt.Printf("Unsuccessful challenge turn loss.\n")
default:
return removePhony, fmt.Errorf("event type %v not supported", evtType)
}
return removePhony, nil
}
func GetGameHistory(id string) (*macondopb.GameHistory, error) {
client := pb.NewGameMetadataServiceProtobufClient("https://woogles.io", &http.Client{})
history, err := client.GetGameHistory(context.Background(), &pb.GameHistoryRequest{GameId: id})
if err != nil {
return &macondopb.GameHistory{}, err
}
return history.History, nil
}
func main() {
// Cache this.
tilesImg, err := loadTilesImg()
if err != nil {
panic(err)
}
gameHistory, err := GetGameHistory(string(os.Args[1]))
if err != nil {
fmt.Println("Caught Error", err)
}
gameGif, err := AnimateGame(tilesImg, boardConfig, gameHistory)
f, err := os.OpenFile(string(os.Args[1]) + ".gif", os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
panic(err)
}
gif.EncodeAll(f, gameGif)
}