game and session keys
This commit is contained in:
parent
e55e659cd0
commit
7b5bfa4fe1
1 changed files with 78 additions and 16 deletions
|
@ -20,17 +20,19 @@ class FFTCGSESSION
|
||||||
@db.on 'error', (err) =>
|
@db.on 'error', (err) =>
|
||||||
logger.error err.message
|
logger.error err.message
|
||||||
|
|
||||||
start: (data) ->
|
sessionKey: (digest) -> "session.#{digest}"
|
||||||
new Promise (resolve) =>
|
gameKey: (digest) -> "game.#{digest}"
|
||||||
# hash data
|
|
||||||
hmac = crypto.createHmac 'sha256', Math.random().toString()
|
|
||||||
hmac.update (JSON.stringify data)
|
|
||||||
digest = hmac.digest 'base64'
|
|
||||||
logger.debug 'digest', digest
|
|
||||||
|
|
||||||
# push (hash, data) into DB for the configured timespan
|
start: (userid) ->
|
||||||
@db.setex digest, EXPIRY.login * 86400, (JSON.stringify data), (err) ->
|
new Promise (resolve) =>
|
||||||
logger.info "OK '#{digest}' created"
|
# 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
|
# return cookie data
|
||||||
resolve
|
resolve
|
||||||
value: digest
|
value: digest
|
||||||
|
@ -40,23 +42,83 @@ class FFTCGSESSION
|
||||||
destroy: (digest) ->
|
destroy: (digest) ->
|
||||||
new Promise (resolve, reject) =>
|
new Promise (resolve, reject) =>
|
||||||
# delete hash immediately
|
# delete hash immediately
|
||||||
@db.del digest, (err, res) ->
|
@db.del (@sessionKey digest), (err, res) =>
|
||||||
if res == 0
|
if res == 0
|
||||||
reject null
|
reject null
|
||||||
else
|
else
|
||||||
logger.info "OK '#{digest}' deleted"
|
logger.info "OK '#{@sessionKey digest}' deleted"
|
||||||
resolve null
|
resolve null
|
||||||
|
|
||||||
check: (digest) ->
|
check: (digest) ->
|
||||||
new Promise (resolve, reject) =>
|
new Promise (resolve, reject) =>
|
||||||
# refresh expiry timer on digest
|
# 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
|
if res == 0
|
||||||
reject null
|
reject null
|
||||||
|
|
||||||
else
|
else
|
||||||
@db.get digest, (err, res) ->
|
@db.get (@sessionKey digest), (err, res) =>
|
||||||
logger.debug "OK '#{digest}' resumed"
|
logger.debug "OK '#{@sessionKey digest}' resumed"
|
||||||
resolve (JSON.parse res)
|
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
|
module.exports = new FFTCGSESSION
|
||||||
|
|
Reference in a new issue