-
Notifications
You must be signed in to change notification settings - Fork 5
/
game.go
95 lines (81 loc) · 2.1 KB
/
game.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
package main
import (
"context"
"errors"
"github.com/jkomoros/sudoku"
"github.com/samsarahq/thunder/sqlgen"
)
type Game struct {
Id int64 `sql:",primary" graphql:",key"`
State string
Data string
Name string
Solved bool
}
func checkPuzzle(puzzle string) bool {
grid := sudoku.LoadSDK(puzzle)
return grid.Solved()
}
func (s *Server) GetGameById(ctx context.Context, args struct{ Id int64 }) (*Game, error) {
var result *Game
if err := s.db.QueryRow(ctx, &result, sqlgen.Filter{"id": args.Id}, nil); err != nil {
return nil, err
}
return result, nil
}
func (s *Server) GetAllGames(ctx context.Context) ([]*Game, error) {
var result []*Game
if err := s.db.Query(ctx, &result, nil, nil); err != nil {
return nil, err
}
return result, nil
}
func (s *Server) GetPlayersForGame(ctx context.Context, g *Game) ([]*Player, error) {
var result []*Player
if err := s.db.Query(ctx, &result, nil, nil); err != nil {
return nil, err
}
return result, nil
}
func (s *Server) CreateGame(ctx context.Context, args struct{ Name string }) (*Game, error) {
grid := sudoku.GenerateGrid(sudoku.DefaultGenerationOptions())
gridString := grid.DataString()
game := Game{Name: args.Name, Data: gridString, State: gridString}
res, err := s.db.InsertRow(ctx, &game)
if err != nil {
return nil, err
}
lastInsertId, err := res.LastInsertId()
if err != nil {
return nil, err
}
game.Id = lastInsertId
return &game, nil
}
type updateGameArgs struct {
Id int64
Row int16
Col int16
Val int16
}
func (s *Server) UpdateGame(ctx context.Context, args updateGameArgs) error {
var game *Game
if err := s.db.QueryRow(ctx, &game, sqlgen.Filter{"id": args.Id}, nil); err != nil {
return err
}
if game.Solved {
return nil
}
var r, c = int(args.Row), int(args.Col)
if origState := sudoku.LoadSDK(game.Data).Cell(r, c).Number(); origState != 0 {
return errors.New("Can't change original cell")
}
grid := sudoku.MutableLoadSDK(game.State)
grid.MutableCell(r, c).SetNumber(int(args.Val))
if grid.Solved() {
game.Solved = true
}
game.State = grid.DataString()
err := s.db.UpdateRow(ctx, game)
return err
}