Skip to content

Commit ddf15b1

Browse files
committed
[PoC] gogame based on flowstate versoin with data in state ctx
Based on makasim/flowstate#97
1 parent d3ec587 commit ddf15b1

File tree

17 files changed

+344
-143
lines changed

17 files changed

+344
-143
lines changed

go.mod

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ toolchain go1.24.3
66

77
require (
88
connectrpc.com/connect v1.18.1
9+
github.com/VictoriaMetrics/easyproto v0.1.4
910
github.com/makasim/flowstate v0.0.0-20250726182313-372320ea0677
11+
github.com/oklog/ulid/v2 v2.1.1
1012
github.com/otrego/clamshell v0.0.0-20220814024334-043dd78cf746
1113
github.com/rs/cors v1.11.1
1214
golang.org/x/net v0.38.0
1315
google.golang.org/protobuf v1.36.6
1416
)
1517

1618
require (
17-
github.com/VictoriaMetrics/easyproto v0.1.4 // indirect
19+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
1820
github.com/google/go-cmp v0.7.0 // indirect
19-
github.com/oklog/ulid/v2 v2.1.1 // indirect
2021
golang.org/x/text v0.23.0 // indirect
2122
golang.org/x/time v0.6.0 // indirect
2223
)
2324

24-
//replace github.com/makasim/flowstate => /Users/makasim/projects/Makasim/flowstate
25+
replace github.com/makasim/flowstate => /Users/makasim/projects/Makasim/flowstate

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ connectrpc.com/connect v1.18.1 h1:PAg7CjSAGvscaf6YZKUefjoih5Z/qYkyaTrBW8xvYPw=
22
connectrpc.com/connect v1.18.1/go.mod h1:0292hj1rnx8oFrStN7cB4jjVBeqs+Yx5yDIC2prWDO8=
33
github.com/VictoriaMetrics/easyproto v0.1.4 h1:r8cNvo8o6sR4QShBXQd1bKw/VVLSQma/V2KhTBPf+Sc=
44
github.com/VictoriaMetrics/easyproto v0.1.4/go.mod h1:QlGlzaJnDfFd8Lk6Ci/fuLxfTo3/GThPs2KH23mv710=
5+
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
6+
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
57
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
68
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
79
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
810
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
911
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75 h1:f0n1xnMSmBLzVfsMMvriDyA75NB/oBgILX2GcHXIQzY=
1012
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75/go.mod h1:g2644b03hfBX9Ov0ZBDgXXens4rxSxmqFBbhvKv2yVA=
11-
github.com/makasim/flowstate v0.0.0-20250726182313-372320ea0677 h1:Xl59T1WcqG2/sRl3mNinfJgpqTnOgj+iyQrKs5A3Et0=
12-
github.com/makasim/flowstate v0.0.0-20250726182313-372320ea0677/go.mod h1:576Odv1ZCb4I12pDq72WbZrwLegxzh5xpc1Fe0KDdIw=
1313
github.com/oklog/ulid/v2 v2.1.1 h1:suPZ4ARWLOJLegGFiZZ1dFAkqzhMjL3J1TzI+5wHz8s=
1414
github.com/oklog/ulid/v2 v2.1.1/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
1515
github.com/otrego/clamshell v0.0.0-20220814024334-043dd78cf746 h1:lRPeFeuZAdclG9NZudMoF5T+sImSK/YqGI7spKR7q/U=

internal/api/convertor/data.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,31 @@ func GameToData(g *v1.Game, d *flowstate.Data) error {
1212
return err
1313
}
1414

15-
d.ID = flowstate.DataID(g.Id)
16-
d.B = b
15+
d.Blob = b
1716
return nil
1817
}
1918

2019
func DataToGame(d *flowstate.Data) (*v1.Game, error) {
2120
g := &v1.Game{}
22-
if err := protojson.Unmarshal(d.B, g); err != nil {
21+
if err := protojson.Unmarshal(d.Blob, g); err != nil {
2322
return nil, err
2423
}
2524

2625
return g, nil
2726
}
2827

2928
func FindGame(e flowstate.Engine, gID string, gRev int32) (*v1.Game, *flowstate.StateCtx, *flowstate.Data, error) {
30-
d := &flowstate.Data{}
3129
stateCtx := &flowstate.StateCtx{}
3230

3331
if err := e.Do(
3432
flowstate.GetStateByID(stateCtx, flowstate.StateID(gID), int64(gRev)),
35-
flowstate.GetData(stateCtx, d, `game`),
33+
flowstate.GetData(stateCtx, `game`),
3634
); err != nil {
3735
return nil, nil, nil, err
3836
}
3937

38+
d := stateCtx.MustData(`game`)
39+
4040
g, err := DataToGame(d)
4141
if err != nil {
4242
return nil, nil, nil, err

internal/api/convertor/undo.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package convertor
22

33
import (
4-
"fmt"
5-
64
"github.com/makasim/flowstate"
75
v1 "github.com/makasim/gogame/protogen/gogame/v1"
86
"google.golang.org/protobuf/encoding/protojson"
@@ -14,14 +12,14 @@ func UndoToData(u *v1.Undo, d *flowstate.Data) error {
1412
return err
1513
}
1614

17-
d.ID = flowstate.DataID(fmt.Sprintf("%s-%d", u.GameId, u.GameRev))
18-
d.B = b
15+
//d.ID = flowstate.DataID(fmt.Sprintf("%s-%d", u.GameId, u.GameRev))
16+
d.Blob = b
1917
return nil
2018
}
2119

2220
func DataToUndo(d *flowstate.Data) (*v1.Undo, error) {
2321
u := &v1.Undo{}
24-
if err := protojson.Unmarshal(d.B, u); err != nil {
22+
if err := protojson.Unmarshal(d.Blob, u); err != nil {
2523
return nil, err
2624
}
2725

internal/api/corsmiddleware/cors.go

Lines changed: 0 additions & 37 deletions
This file was deleted.

internal/api/gameservicev1/creategamehandler/handler.go

Lines changed: 7 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,73 +2,22 @@ package creategamehandler
22

33
import (
44
"context"
5-
"fmt"
6-
"strconv"
7-
"time"
5+
"errors"
86

97
"connectrpc.com/connect"
10-
"github.com/makasim/flowstate"
11-
"github.com/makasim/gogame/internal/api/convertor"
12-
"github.com/makasim/gogame/internal/staleflow"
138
v1 "github.com/makasim/gogame/protogen/gogame/v1"
149
)
1510

11+
// deprecated
1612
type Handler struct {
17-
e flowstate.Engine
1813
}
1914

20-
func New(e flowstate.Engine) *Handler {
21-
return &Handler{
22-
e: e,
23-
}
15+
// deprecated
16+
func New() *Handler {
17+
return &Handler{}
2418
}
2519

20+
// deprecated
2621
func (h *Handler) CreateGame(_ context.Context, req *connect.Request[v1.CreateGameRequest]) (*connect.Response[v1.CreateGameResponse], error) {
27-
if req.Msg.Name == `` {
28-
return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("game name is required"))
29-
}
30-
if req.Msg.Player1 != nil && req.Msg.Player1.Name == `` {
31-
return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("player1 name is required"))
32-
}
33-
34-
g := &v1.Game{
35-
Id: strconv.FormatInt(time.Now().UnixNano(), 10),
36-
Name: req.Msg.Name,
37-
Player1: req.Msg.Player1,
38-
State: v1.State_STATE_CREATED,
39-
MoveDurationSec: req.Msg.MoveDurationSec,
40-
}
41-
if g.MoveDurationSec == 0 {
42-
g.MoveDurationSec = 60
43-
}
44-
45-
d := &flowstate.Data{}
46-
if err := convertor.GameToData(g, d); err != nil {
47-
return nil, connect.NewError(connect.CodeInternal, err)
48-
}
49-
50-
stateCtx := &flowstate.StateCtx{
51-
Current: flowstate.State{
52-
ID: flowstate.StateID(g.Id),
53-
Labels: map[string]string{
54-
`game`: `true`,
55-
`game.id`: g.Id,
56-
`game.state`: `created`,
57-
},
58-
},
59-
}
60-
61-
if err := h.e.Do(flowstate.Commit(
62-
flowstate.AttachData(stateCtx, d, `game`),
63-
flowstate.Park(stateCtx),
64-
flowstate.Delay(stateCtx, staleflow.ID, time.Minute),
65-
)); err != nil {
66-
return nil, connect.NewError(connect.CodeInternal, err)
67-
}
68-
69-
g.Rev = int32(stateCtx.Current.Rev)
70-
71-
return connect.NewResponse(&v1.CreateGameResponse{
72-
Game: g,
73-
}), nil
22+
return nil, connect.NewError(connect.CodeUnimplemented, errors.New("gogame.v1.GameService.CreateGame is not implemented"))
7423
}

internal/api/gameservicev1/joingamehandler/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (h *Handler) JoinGame(_ context.Context, req *connect.Request[v1.JoinGameRe
5757
}
5858

5959
if err := h.e.Do(flowstate.Commit(
60-
flowstate.AttachData(stateCtx, d, `game`),
60+
flowstate.StoreData(stateCtx, `game`),
6161
flowstate.Park(stateCtx),
6262
flowstate.Delay(stateCtx, movetimeoutflow.ID, time.Duration(g.MoveDurationSec)*time.Second),
6363
)); err != nil {

internal/api/gameservicev1/makemovehandler/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (h *Handler) MakeMove(_ context.Context, req *connect.Request[v1.MakeMoveRe
9292
}
9393

9494
if err := h.e.Do(flowstate.Commit(
95-
flowstate.AttachData(stateCtx, d, `game`),
95+
flowstate.StoreData(stateCtx, `game`),
9696
flowstate.Park(stateCtx),
9797
flowstate.Delay(stateCtx, movetimeoutflow.ID, time.Duration(g.MoveDurationSec)*time.Second),
9898
)); err != nil {

internal/api/gameservicev1/passhandler/handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (h *Handler) Pass(_ context.Context, req *connect.Request[v1.PassRequest])
6464
}
6565

6666
if err := h.e.Do(flowstate.Commit(
67-
flowstate.AttachData(stateCtx, d, `game`),
67+
flowstate.StoreData(stateCtx, `game`),
6868
flowstate.Park(stateCtx),
6969
)); err != nil {
7070
return nil, connect.NewError(connect.CodeInternal, err)
@@ -90,7 +90,7 @@ func (h *Handler) Pass(_ context.Context, req *connect.Request[v1.PassRequest])
9090
}
9191

9292
if err := h.e.Do(flowstate.Commit(
93-
flowstate.AttachData(stateCtx, d, `game`),
93+
flowstate.StoreData(stateCtx, `game`),
9494
flowstate.Park(stateCtx),
9595
flowstate.Delay(stateCtx, movetimeoutflow.ID, time.Duration(g.MoveDurationSec)*time.Second),
9696
)); err != nil {

internal/api/gameservicev1/resignhandler/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (h *Handler) Resign(_ context.Context, req *connect.Request[v1.ResignReques
4747
}
4848

4949
if err := h.e.Do(flowstate.Commit(
50-
flowstate.AttachData(stateCtx, d, `game`),
50+
flowstate.StoreData(stateCtx, `game`),
5151
flowstate.Park(stateCtx),
5252
)); err != nil {
5353
return nil, connect.NewError(connect.CodeInternal, err)

0 commit comments

Comments
 (0)