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
| // Define a struct for the payload data
type moveData struct {
GroupId string `json:"groupId"`
Move string `json:"move"`
}
// Define a struct for the match state
type matchState struct {
CreatorId string `json:"creatorId"`
GroupId string `json:"groupId"`
Players []string `json:"players"`
Moves map[string][]string `json:"moves"`
}
// This RPC takes a player's move (such as a Chess move) and applies it to the match state before broadcasting the move to all other players
if err := initializer.RegisterRpc("make_move", func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
// Get the user id
userId, ok := ctx.Value(runtime.RUNTIME_CTX_USER_ID).(string)
if !ok {
logger.Error("unable to get user id")
return "", runtime.NewError("unable to find user id", 13)
}
// Unmarshal the payload
var data moveData
if err := json.Unmarshal([]byte(payload), &data); err != nil {
logger.Error("unable to unmarshal payload: %v", err)
return "", runtime.NewError("invalid payload", 3)
}
// Get the group
groups, err := nk.GroupsGetId(ctx, []string{data.GroupId})
if err != nil {
logger.Error("unable to find group: %v", err)
return "", runtime.NewError("invalid group id", 3)
}
if len(groups) == 0 {
logger.Error("group does not exist")
return "", runtime.NewError("group does not exist", 5)
}
group := groups[0]
// Get the current match state
records, err := nk.StorageRead(ctx, []*runtime.StorageRead{
{
Collection: "matchState",
Key: data.GroupId,
UserID: group.CreatorId,
},
})
if err != nil || len(records) == 0 {
logger.Error("unable to find match state")
return "", runtime.NewError("unable to find match state", 13)
}
/*
Given a match state with a structure such as:
{
"creatorId": "<creatorId>",
"groupId": "<groupId>",
"players": [ "<userId>" ],
"moves": {
"<userId>": [ "e4", "Nf3" ],
"<userId2>": [ "e5" ]
},
}
*/
var matchState matchState
if err := json.Unmarshal([]byte(records[0].Value), &matchState); err != nil {
logger.Error("unable to unmarshal match state: %v", err)
return "", runtime.NewError("unable to get match state", 13)
}
// Add the player's move to their move list
// This example assumes the game is something similar to Chess
// Note: You would normally validate this move before applying it
matchState.Moves[userId] = append(matchState.Moves[userId], data.Move)
// Notify all players of the player's move
for _, playerUserId := range matchState.Players {
nk.NotificationSend(ctx, playerUserId, "", map[string]interface{}{
"matchId": matchState.GroupId,
"userId": matchState.CreatorId,
"move": data.Move,
}, 1, matchState.CreatorId, true)
}
// Marshal the response
matchStateJson, err := json.Marshal(matchState)
if err != nil {
logger.Error("error marshaling response: %v", err)
return "", runtime.NewError("internal error", 13)
}
// Update the match state
nk.StorageWrite(ctx, []*runtime.StorageWrite{
{
Collection: "matchState",
Key: matchState.GroupId,
UserID: matchState.CreatorId,
Value: string(matchStateJson),
},
})
return "{ \"success\": true }", nil
}); err != nil {
logger.Error("unable to register make_move rpc: %v", err)
return err
}
|