diff --git a/backend/session.coffee b/backend/session.coffee index f315f27..e8f3590 100644 --- a/backend/session.coffee +++ b/backend/session.coffee @@ -20,17 +20,19 @@ class FFTCGSESSION @db.on 'error', (err) => logger.error err.message - start: (data) -> - new Promise (resolve) => - # hash data - hmac = crypto.createHmac 'sha256', Math.random().toString() - hmac.update (JSON.stringify data) - digest = hmac.digest 'base64' - logger.debug 'digest', digest + sessionKey: (digest) -> "session.#{digest}" + gameKey: (digest) -> "game.#{digest}" - # push (hash, data) into DB for the configured timespan - @db.setex digest, EXPIRY.login * 86400, (JSON.stringify data), (err) -> - logger.info "OK '#{digest}' created" + start: (userid) -> + new Promise (resolve) => + # hash userid + hmac = crypto.createHmac 'sha256', Math.random().toString() + hmac.update userid.toString() + digest = hmac.digest 'base64' + + # push (hash, userid) into DB for the configured timespan + @db.setex (@sessionKey digest), EXPIRY.login * 86400, userid, => + logger.info "OK '#{@sessionKey digest}' created" # return cookie data resolve value: digest @@ -40,23 +42,83 @@ class FFTCGSESSION destroy: (digest) -> new Promise (resolve, reject) => # delete hash immediately - @db.del digest, (err, res) -> + @db.del (@sessionKey digest), (err, res) => if res == 0 reject null else - logger.info "OK '#{digest}' deleted" + logger.info "OK '#{@sessionKey digest}' deleted" resolve null check: (digest) -> new Promise (resolve, reject) => # refresh expiry timer on digest - @db.expire digest, EXPIRY.login * 86400, (err, res) => + @db.expire (@sessionKey digest), EXPIRY.login * 86400, (err, res) => if res == 0 reject null else - @db.get digest, (err, res) -> - logger.debug "OK '#{digest}' resumed" - resolve (JSON.parse res) + @db.get (@sessionKey digest), (err, res) => + logger.debug "OK '#{@sessionKey digest}' resumed" + resolve res + + newGame: (userid) -> + new Promise (resolve, reject) => + # generate hash + hmac = crypto.createHmac 'sha256', Math.random().toString() + hmac.update userid.toString() + digest = hmac.digest 'base64' + + # insert game key + @db.hsetnx (@gameKey digest), 'owner', userid, (err, res) => + if res == 0 + @db.del (@gameKey digest) + reject null + + else + @db.expire (@gameKey digest), EXPIRY.game * 86400, (err, res) => + if res == 0 + @db.del (@gameKey digest) + reject null + + else + # return game ID + logger.info "OK '#{@gameKey digest}' created" + resolve digest + + joinGame: (digest, userid) -> + new Promise (resolve, reject) => + # refresh expiry timer on digest + @db.expire (@gameKey digest), EXPIRY.game * 86400, (err, res) => + if res == 0 + reject null + + else + # insert opponent value + @db.hsetnx (@gameKey digest), 'opponent', userid, (err, res) => + if res == 0 + reject null + + else + # return game ID + logger.info "OK '#{@gameKey digest}' joined" + resolve digest + + updateGame: (digest, state) -> + new Promise (resolve, reject) => + # refresh expiry timer on digest + @db.expire (@gameKey digest), EXPIRY.game * 86400, (err, res) => + if res == 0 + reject null + + else + # update state value + @db.hset (@gameKey digest), 'state', (JSON.stringify state), (err, res) => + if res == 0 + reject null + + else + # return game ID + logger.info "OK '#{@gameKey digest}' updated" + resolve digest module.exports = new FFTCGSESSION