@@ -38,7 +38,7 @@ type game struct {
38
38
Player common.Disk
39
39
}
40
40
41
- func getGameAndOpponentAndConnectionIDs (ctx context.Context , args Args , host string ) (game , string , [ ]string , error ) {
41
+ func getGame (ctx context.Context , args Args , host string ) (game , string , map [ string ]string , error ) {
42
42
// Get the whole item from DynamoDB.
43
43
output , err := args .DB .GetItemWithContext (ctx , & dynamodb.GetItemInput {
44
44
TableName : aws .String (args .TableName ),
@@ -64,39 +64,39 @@ func getGameAndOpponentAndConnectionIDs(ctx context.Context, args Args, host str
64
64
return game , "" , nil , err
65
65
}
66
66
67
- // Get just the connection ID values.
68
- var connectionIDs []string
69
- for _ , v := range item .Connections {
70
- connectionIDs = append (connectionIDs , v )
71
- }
72
-
73
- return game , item .Opponent , connectionIDs , err
67
+ return game , item .Opponent , item .Connections , err
74
68
}
75
69
76
- func updateGame (ctx context.Context , args Args , host string , game game ) error {
70
+ func updateGame (ctx context.Context , args Args , host string , game game , connName , connID string ) error {
77
71
gameBytes , err := json .Marshal (& game )
78
72
if err != nil {
79
73
return err
80
74
}
81
75
82
76
update := expression .Set (expression .Name (attribGame ), expression .Value (gameBytes ))
77
+ condition := expression .Name (attribConnections + "." + connName ).Equal (expression .Value (connID ))
83
78
84
- _ , err = updateItem (ctx , args , host , update , false )
79
+ _ , err = updateItem (ctx , args , host , update , condition , false )
85
80
return err
86
81
}
87
82
88
- func updateGameOpponentSetConnection (ctx context.Context , args Args , host string , game game , opponent , connName , connID string ) error {
83
+ func createGame (ctx context.Context , args Args , host string , game game , opponent , connName , connID string ) error {
89
84
gameBytes , err := json .Marshal (& game )
90
85
if err != nil {
91
86
return err
92
87
}
93
88
94
89
update := expression .
95
90
Set (expression .Name (attribGame ), expression .Value (gameBytes )).
96
- Set (expression .Name (attribOpponent ), expression .Value (opponent )).
97
91
Set (expression .Name (attribConnections ), expression .Value (map [string ]string {connName : connID }))
98
92
99
- _ , err = updateItem (ctx , args , host , update , false )
93
+ if opponent != "" {
94
+ update = update .Set (expression .Name (attribOpponent ), expression .Value (opponent ))
95
+ }
96
+
97
+ condition := expression .Name (attribHost ).AttributeNotExists ()
98
+
99
+ _ , err = updateItem (ctx , args , host , update , condition , false )
100
100
return err
101
101
}
102
102
@@ -106,7 +106,7 @@ func updateOpponentConnectionGetGameConnectionIDs(ctx context.Context, args Args
106
106
Set (expression .Name (attribConnections + "." + connName ), expression .Value (connID ))
107
107
condition := expression .In (expression .Name (attribOpponent ), expression .Value (expectedOpponents [0 ]), expression .Value (expectedOpponents [1 ]))
108
108
109
- output , err := updateItemWithCondition (ctx , args , host , update , condition , true )
109
+ output , err := updateItem (ctx , args , host , update , condition , true )
110
110
if err != nil {
111
111
return game {}, nil , err
112
112
}
@@ -158,11 +158,24 @@ func getHostsByOpponent(ctx context.Context, args Args, opponent string) ([]stri
158
158
return hosts , nil
159
159
}
160
160
161
- func deleteGameGetConnectionIDs (ctx context.Context , args Args , host string ) ([]string , error ) {
161
+ func deleteGameGetConnectionIDs (ctx context.Context , args Args , host , connName , connID string ) ([]string , error ) {
162
+ exp , err := expression .NewBuilder ().
163
+ WithCondition (expression .Or (
164
+ expression .Name (attribConnections + "." + connName ).Equal (expression .Value (connID )),
165
+ expression .Name (attribHost ).AttributeNotExists (),
166
+ )).
167
+ Build ()
168
+ if err != nil {
169
+ return nil , err
170
+ }
171
+
162
172
output , err := args .DB .DeleteItemWithContext (ctx , & dynamodb.DeleteItemInput {
163
- TableName : aws .String (args .TableName ),
164
- Key : hostKey (host ),
165
- ReturnValues : aws .String (dynamodb .ReturnValueAllOld ),
173
+ TableName : aws .String (args .TableName ),
174
+ Key : hostKey (host ),
175
+ ConditionExpression : exp .Condition (),
176
+ ExpressionAttributeNames : exp .Names (),
177
+ ExpressionAttributeValues : exp .Values (),
178
+ ReturnValues : aws .String (dynamodb .ReturnValueAllOld ),
166
179
})
167
180
if err != nil {
168
181
return nil , err
@@ -184,21 +197,9 @@ func deleteGameGetConnectionIDs(ctx context.Context, args Args, host string) ([]
184
197
}
185
198
186
199
// updateItem wraps dynamodb.UpdateItemWithContext.
187
- func updateItem (ctx context.Context , args Args , host string , update expression.UpdateBuilder , returnOldValues bool ) (* dynamodb.UpdateItemOutput , error ) {
188
- update = update .Set (expression .Name (attribTTL ), expression .Value (time .Now ().Add (time .Hour ).Unix ()))
189
- builder := expression .NewBuilder ().WithUpdate (update )
190
- return updateItemWithBuilder (ctx , args , host , builder , returnOldValues )
191
- }
192
-
193
- // updateItemWithCondition wraps dynamodb.UpdateItemWithContext.
194
- func updateItemWithCondition (ctx context.Context , args Args , host string , update expression.UpdateBuilder , condition expression.ConditionBuilder , returnOldValues bool ) (* dynamodb.UpdateItemOutput , error ) {
200
+ func updateItem (ctx context.Context , args Args , host string , update expression.UpdateBuilder , condition expression.ConditionBuilder , returnOldValues bool ) (* dynamodb.UpdateItemOutput , error ) {
195
201
update = update .Set (expression .Name (attribTTL ), expression .Value (time .Now ().Add (time .Hour ).Unix ()))
196
202
builder := expression .NewBuilder ().WithUpdate (update ).WithCondition (condition )
197
- return updateItemWithBuilder (ctx , args , host , builder , returnOldValues )
198
- }
199
-
200
- // updateItemWithBuilder wraps dynamodb.UpdateItemWithContext.
201
- func updateItemWithBuilder (ctx context.Context , args Args , host string , builder expression.Builder , returnOldValues bool ) (* dynamodb.UpdateItemOutput , error ) {
202
203
exp , err := builder .Build ()
203
204
if err != nil {
204
205
return nil , err
0 commit comments