@@ -242,54 +242,78 @@ function utils.indexOf(t, value)
242
242
return - 1
243
243
end
244
244
245
- function utils .updateUserAssociations (antId , state , user )
246
- -- remove previous associations
247
- if ADDRESSES [user ] and user ~= state .Owner and not utils .includes (user , state .Controllers ) then
248
- local antIndex = utils .indexOf (ADDRESSES [user ], antId )
249
- if antIndex then
250
- table.remove (ADDRESSES [user ], antIndex )
251
- end
252
- end
245
+ --- func description
246
+ -- @param {table} params
247
+ -- @param {string} params.id
248
+ -- @param {number} params.timestamp
249
+ -- @param {string | nil} params.owner
250
+ -- @param {table | nil} params.controllers
251
+ function utils .register (params )
252
+ assert (type (params ) == " table" , " Register Params must be a table" )
253
+ local id , timestamp , owner , controllers = params .id , params .timestamp , params .owner , params .controllers
254
+ assert (type (id ) == " string" , " ANT ID must be a string" )
255
+ assert (type (timestamp ) == " number" , " Timestamp must be a number" )
256
+ assert (type (owner ) == " string" or owner == nil , " Owner must be a string" )
257
+ assert (type (controllers ) == " table" or controllers == nil , " Controllers must be a table" )
258
+ ANTS [id ] = {
259
+ Owner = owner ,
260
+ Controllers = controllers or {},
261
+ RegisteredAt = timestamp ,
262
+ }
263
+ end
253
264
254
- -- add new associations
255
- if user == state .Owner or utils .includes (user , state .Controllers ) then
256
- if not ADDRESSES [user ] then
257
- ADDRESSES [user ] = {}
258
- end
259
- if not utils .includes (antId , ADDRESSES [user ]) then
260
- table.insert (ADDRESSES [user ], antId )
261
- end
265
+ function utils .controllerTableFromArray (t )
266
+ assert (type (t ) == " table" , " argument needs to be a table" )
267
+ local map = {}
268
+ for _ , v in ipairs (t ) do
269
+ map [v ] = true
262
270
end
271
+ return map
263
272
end
264
273
265
274
function utils .updateAssociations (antId , state )
266
275
-- Remove previous associations for old owner and controllers
267
- local previousOwner = ANTS [antId ].Owner
268
- local previousControllers = ANTS [antId ].Controllers
276
+ local oldAnt = ANTS [antId ]
277
+
278
+ local newOwner = state .Owner or " nilOwner"
279
+ local newControllers = utils .controllerTableFromArray (state .Controllers )
269
280
270
- if previousOwner and previousOwner ~= state .Owner and type (ANTS [antId ]) == " table" then
271
- utils .updateUserAssociations (antId , ANTS [antId ], previousOwner )
281
+ local newAffliates = {}
282
+ newAffliates [newOwner ] = true
283
+ for user , _ in pairs (newControllers ) do
284
+ newAffliates [user ] = true
272
285
end
273
286
274
- for _ , user in ipairs (previousControllers ) do
275
- if not utils .includes (user , state .Controllers ) then
276
- utils .updateUserAssociations (antId , ANTS [antId ], user )
287
+ if oldAnt ~= nil then
288
+ local previousOwner = ANTS [antId ].Owner or " nilOwner"
289
+ local previousControllers = ANTS [antId ].Controllers
290
+
291
+ local oldAffliates = {}
292
+ oldAffliates [previousOwner ] = true
293
+ for user , _ in pairs (previousControllers ) do
294
+ oldAffliates [user ] = true
277
295
end
278
- end
279
296
280
- -- Add new associations for new owner and controllers
281
- local users = { state .Owner }
282
- for _ , user in ipairs (state .Controllers ) do
283
- table.insert (users , user )
297
+ for oldAffliate , _ in pairs (oldAffliates ) do
298
+ if not newAffliates [oldAffliate ] and ADDRESSES [oldAffliate ] then
299
+ ADDRESSES [oldAffliate ][antId ] = nil
300
+ end
301
+ end
284
302
end
285
303
286
- for _ , user in ipairs (users ) do
287
- utils .updateUserAssociations (antId , state , user )
304
+ for user , _ in pairs (newAffliates ) do
305
+ ADDRESSES [user ] = ADDRESSES [user ] or {}
306
+ ADDRESSES [user ][antId ] = true
288
307
end
289
-
290
308
-- Update the ANTS table
291
- ANTS [antId ].Owner = state .Owner
292
- ANTS [antId ].Controllers = state .Controllers
309
+ if not newOwner and # newControllers == 0 then
310
+ ANTS [antId ] = nil
311
+ else -- remove ant from registry if it has no owner or controllers
312
+ ANTS [antId ] = {
313
+ Owner = newOwner ,
314
+ Controllers = newControllers ,
315
+ }
316
+ end
293
317
end
294
318
295
319
-- it is possible to register an ANT that does not respond to the state request, so we need to clean up the ANTS table
0 commit comments