-
Notifications
You must be signed in to change notification settings - Fork 0
/
match_state.go
194 lines (176 loc) · 5.76 KB
/
match_state.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
package main
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"time"
nakamaCommands "github.com/challenge-league/nakama-go/commands"
nakamaContext "github.com/challenge-league/nakama-go/context"
"github.com/heroiclabs/nakama-common/runtime"
log "github.com/micro/go-micro/v2/logger"
)
func writeMatchStateInLoop(ctx context.Context, nk runtime.NakamaModule, matchState *nakamaCommands.MatchState) *nakamaCommands.MatchState {
matchState, err := writeMatchState(ctx, nk, matchState)
if err != nil {
log.Errorf("Error %+v", err)
return nil
}
return matchState
}
func archiveMatchState(ctx context.Context, nk runtime.NakamaModule, s *nakamaCommands.MatchState) error {
matchState := *s
matchState.StorageCollection = nakamaCommands.MATCH_ARCHIVE_COLLECTION
matchState.Active = false
matchState.ActualDateTimeEnd = time.Now()
matchState.ActualDuration = matchState.ActualDateTimeEnd.Sub(matchState.DateTimeStart)
matchState.Version = "*"
if _, err := writeMatchState(ctx, nk, &matchState); err != nil {
log.Error(err)
return err
}
return nil
}
func getDummyMatchState(matchID string, collection string) *nakamaCommands.MatchState {
return &nakamaCommands.MatchState{
StorageCollection: collection,
MatchID: matchID,
StorageUserID: nakamaContext.NakamaSystemUserID,
}
}
func MatchStateGetRPC(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
var request *nakamaCommands.MatchStateGetRequest
if err := json.Unmarshal([]byte(payload), &request); err != nil {
log.Error(err)
return "", err
}
matchState, err := readMatchState(ctx, nk, getDummyMatchState(request.ID, request.StorageCollection))
if err != nil {
log.Error(err)
return "", err
}
return MarshalIndent(matchState), nil
}
func matchStateListGet(ctx context.Context, nk runtime.NakamaModule, userID string, collection string) ([]*nakamaCommands.MatchState, error) {
storageCollection := nakamaCommands.MATCH_COLLECTION
if collection != "" {
storageCollection = collection
}
storageObjects, _, err := nk.StorageList(ctx,
userID,
storageCollection,
nakamaCommands.MAX_LIST_LIMIT,
"",
)
if err != nil {
log.Error(err)
return nil, err
}
if collection == "" && len(storageObjects) == 0 {
storageObjects, _, err = nk.StorageList(ctx,
userID,
nakamaCommands.MATCH_ARCHIVE_COLLECTION,
nakamaCommands.MAX_LIST_LIMIT,
"",
)
}
if len(storageObjects) == 0 {
log.Info("No match found")
return nil, nil
}
var matchStateList []*nakamaCommands.MatchState
for _, object := range storageObjects {
var state *nakamaCommands.MatchState
if err := json.Unmarshal([]byte(object.Value), &state); err != nil {
log.Error(err)
return nil, err
}
state.Version = object.Version
matchStateList = append(matchStateList, state)
}
return matchStateList, nil
}
func MatchStateListGetRPC(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
var request *nakamaCommands.MatchStateListGetRequest
if err := json.Unmarshal([]byte(payload), &request); err != nil {
log.Error(err)
return "", err
}
matchStateList, err := matchStateListGet(ctx, nk, request.UserID, request.StorageCollection)
if err != nil {
log.Error(err)
return "", err
}
return string(Marshal(matchStateList)), nil
}
func deleteMatchState(ctx context.Context, nk runtime.NakamaModule, matchState *nakamaCommands.MatchState) error {
err := nk.StorageDelete(ctx, []*runtime.StorageDelete{
&runtime.StorageDelete{
Collection: matchState.StorageCollection,
Key: matchState.MatchID,
UserID: matchState.StorageUserID,
},
})
if err != nil {
log.Error(err)
return err
}
return nil
}
func readMatchState(ctx context.Context, nk runtime.NakamaModule, matchState *nakamaCommands.MatchState) (*nakamaCommands.MatchState, error) {
log.Infof("%+v", matchState.StorageCollection)
storageCollection := nakamaCommands.MATCH_COLLECTION
if matchState.StorageCollection != "" {
storageCollection = matchState.StorageCollection
}
storageObjects, err := nk.StorageRead(ctx, []*runtime.StorageRead{&runtime.StorageRead{
Collection: storageCollection,
Key: matchState.MatchID,
UserID: matchState.StorageUserID,
}})
if err != nil {
log.Error(err)
return nil, err
}
if matchState.StorageCollection == "" && len(storageObjects) == 0 {
storageObjects, err = nk.StorageRead(ctx, []*runtime.StorageRead{&runtime.StorageRead{
Collection: nakamaCommands.MATCH_ARCHIVE_COLLECTION,
Key: matchState.MatchID,
UserID: matchState.StorageUserID,
}})
}
if len(storageObjects) == 0 {
return nil, runtime.NewError(fmt.Sprintf("No match found with ID: %v", matchState.MatchID), 404)
}
var state *nakamaCommands.MatchState
if err := json.Unmarshal([]byte(storageObjects[0].Value), &state); err != nil {
log.Error(err)
return nil, err
}
state.Version = storageObjects[0].Version
return state, nil
}
func writeMatchState(ctx context.Context, nk runtime.NakamaModule, matchState *nakamaCommands.MatchState) (*nakamaCommands.MatchState, error) {
log.Infof("Writing match state: %+v", matchState)
acks, err := nk.StorageWrite(ctx, []*runtime.StorageWrite{
&runtime.StorageWrite{
Collection: matchState.StorageCollection,
Key: matchState.MatchID,
Value: string(Marshal(matchState)),
UserID: matchState.StorageUserID,
PermissionWrite: runtime.STORAGE_PERMISSION_NO_WRITE,
PermissionRead: runtime.STORAGE_PERMISSION_PUBLIC_READ,
Version: matchState.Version,
},
})
if err != nil {
log.Error(err)
return nil, err
}
if len(acks) != 1 {
log.Errorf("Invocation failed. Return result not expected: ", len(acks))
return nil, err
}
matchState.Version = acks[0].Version
return matchState, err
}