From bfbd1cbf0afdeb7dceaef55711a9a2054fab0bfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Mon, 3 Jun 2019 22:28:32 +0200 Subject: [PATCH] gameKeys iteration --- backend/session.coffee | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/backend/session.coffee b/backend/session.coffee index e8f3590..e3bc13f 100644 --- a/backend/session.coffee +++ b/backend/session.coffee @@ -81,9 +81,39 @@ class FFTCGSESSION reject null else - # return game ID - logger.info "OK '#{@gameKey digest}' created" - resolve digest + # add game to active set + @db.sadd (@gameKey 'active'), (@gameKey digest), (err, res) => + # return game ID + logger.info "OK '#{@gameKey digest}' created" + resolve digest + + getGames: -> + # function to return all active gameKeys + activeGameKeys = (set, cursor) => + # start iteration + set ?= new Set() + cursor ?= '0' + + return new Promise (resolve, reject) => + # scan "active" gameKey + @db.sscan (@gameKey 'active'), cursor, 'COUNT', '100', (err, res) => + + # add to results set + cursor = res[0] + for key in res[1] + set.add key + + if cursor == '0' + # done on cursor = 0 + resolve set + else + # recursive call (resolve one step deeper) + allGames set, cursor + .then (set) => + resolve set + + activeGameKeys().then (set) => + logger.info "game count: #{Array.from(set).length}" joinGame: (digest, userid) -> new Promise (resolve, reject) =>