This commit is contained in:
Jörn-Michael Miehe 2019-06-04 16:58:44 +02:00
parent 7182121423
commit b56aedbca6
3 changed files with 81 additions and 38 deletions

View file

@ -56,8 +56,26 @@ class FFTCGDB
''', (err) => ''', (err) =>
logger.error err.message if err logger.error err.message if err
@db.run '''INSERT INTO users VALUES(1,'jmm','$2b$13$jgDdHHDWqq1RV6PXxf7aOO6AbxqY6tbxIADyIO0FeXt2BlKQCCMzS',NULL);''' @db.run '''
@db.run '''INSERT INTO decks VALUES(1,1,'{"name":"Antipode Bomb Version 6.0","note":"As Seen In Tournament: The North American Water Cup","cards":[{"count":1,"serial":"1-192"},{"count":2,"serial":"7-132"},{"count":2,"serial":"8-037"},{"count":2,"serial":"8-139"},{"count":1,"serial":"5-036"},{"count":3,"serial":"4-048"},{"count":1,"serial":"2-026"},{"count":3,"serial":"8-043"},{"count":3,"serial":"4-021"},{"count":3,"serial":"3-033"},{"count":1,"serial":"8-014"},{"count":2,"serial":"8-006"},{"count":1,"serial":"8-042"},{"count":1,"serial":"6-027"},{"count":3,"serial":"5-019"},{"count":2,"serial":"2-019"},{"count":2,"serial":"5-032"},{"count":3,"serial":"4-026"},{"count":3,"serial":"1-057"},{"count":1,"serial":"1-048"},{"count":2,"serial":"8-036"},{"count":3,"serial":"8-005"},{"count":3,"serial":"2-005"},{"count":1,"serial":"7-017"},{"count":1,"serial":"8-007"}]}');''' INSERT INTO users VALUES (1,'jmm','$2b$13$jgDdHHDWqq1RV6PXxf7aOO6AbxqY6tbxIADyIO0FeXt2BlKQCCMzS',NULL);
'''
@db.run '''
INSERT INTO decks VALUES (1,1,'{
"name":"Antipode Bomb Version 6.0",
"note":"As Seen In Tournament: The North American Water Cup",
"cards":[
{"count":1,"serial":"1-192"},{"count":2,"serial":"7-132"},{"count":2,"serial":"8-037"},
{"count":2,"serial":"8-139"},{"count":1,"serial":"5-036"},{"count":3,"serial":"4-048"},
{"count":1,"serial":"2-026"},{"count":3,"serial":"8-043"},{"count":3,"serial":"4-021"},
{"count":3,"serial":"3-033"},{"count":1,"serial":"8-014"},{"count":2,"serial":"8-006"},
{"count":1,"serial":"8-042"},{"count":1,"serial":"6-027"},{"count":3,"serial":"5-019"},
{"count":2,"serial":"2-019"},{"count":2,"serial":"5-032"},{"count":3,"serial":"4-026"},
{"count":3,"serial":"1-057"},{"count":1,"serial":"1-048"},{"count":2,"serial":"8-036"},
{"count":3,"serial":"8-005"},{"count":3,"serial":"2-005"},{"count":1,"serial":"7-017"},
{"count":1,"serial":"8-007"}
]
}');
'''
logger.info 'OK clear' logger.info 'OK clear'
@ -75,7 +93,7 @@ class FFTCGDB
validate: (login, password) -> validate: (login, password) ->
defined = (value) -> value? and value isnt '' defined = (value) -> value? and value isnt ''
new Promise (resolve, reject) => new Promise (resolve, reject) ->
if (defined login) and (defined password) if (defined login) and (defined password)
# both are defined # both are defined
resolve null resolve null
@ -97,7 +115,10 @@ class FFTCGDB
else else
# try creating row in users table # try creating row in users table
stmt = @db.prepare 'INSERT INTO users (login, pwdhash) VALUES (?, ?)' stmt = @db.prepare '''
INSERT INTO users (login, pwdhash)
VALUES (?, ?)
'''
stmt.run [login, hash], (err) -> stmt.run [login, hash], (err) ->
stmt.finalize() stmt.finalize()
if err if err
@ -119,8 +140,12 @@ class FFTCGDB
@validate login, password @validate login, password
.then => .then =>
# get users table row # get users table row
stmt = @db.prepare 'SELECT * FROM users WHERE login = ?' stmt = @db.prepare '''
stmt.get [login], (err, row) => SELECT *
FROM users
WHERE login = ?
'''
stmt.get [login], (err, row) ->
stmt.finalize() stmt.finalize()
if err if err
logger.warn "login: FAIL db '#{err.code}' for '#{login}'" logger.warn "login: FAIL db '#{err.code}' for '#{login}'"
@ -153,8 +178,12 @@ class FFTCGDB
getUser: (userID) -> getUser: (userID) ->
new Promise (resolve, reject) => new Promise (resolve, reject) =>
# get users table row # get users table row
stmt = @db.prepare 'SELECT * FROM users WHERE user = ?' stmt = @db.prepare '''
stmt.get [userID], (err, row) => SELECT *
FROM users
WHERE user = ?
'''
stmt.get [userID], (err, row) ->
stmt.finalize() stmt.finalize()
if err if err
logger.warn "get: FAIL db '#{err.code}' for '#{userID}'" logger.warn "get: FAIL db '#{err.code}' for '#{userID}'"
@ -173,7 +202,10 @@ class FFTCGDB
addDeck: (userID, deckCards) -> addDeck: (userID, deckCards) ->
new Promise (resolve, reject) => new Promise (resolve, reject) =>
# try creating row in decks table # try creating row in decks table
stmt = @db.prepare 'INSERT INTO decks (user, json) VALUES (?, ?)' stmt = @db.prepare '''
INSERT INTO decks (user, json)
VALUES (?, ?)
'''
stmt.run [userID, JSON.stringify deckCards], (err) -> stmt.run [userID, JSON.stringify deckCards], (err) ->
stmt.finalize() stmt.finalize()
if err if err
@ -186,7 +218,11 @@ class FFTCGDB
modDeck: (userID, deckID, deckCards) -> modDeck: (userID, deckID, deckCards) ->
new Promise (resolve, reject) => new Promise (resolve, reject) =>
stmt = @db.prepare 'UPDATE decks SET json = ? WHERE deck = ? AND user = ?' stmt = @db.prepare '''
UPDATE decks
SET json = ?
WHERE deck = ? AND user = ?
'''
stmt.run [(JSON.stringify deckCards), deckID, userID], (err) -> stmt.run [(JSON.stringify deckCards), deckID, userID], (err) ->
stmt.finalize() stmt.finalize()
if err if err
@ -200,7 +236,12 @@ class FFTCGDB
getDecks: (userID) -> getDecks: (userID) ->
new Promise (resolve, reject) => new Promise (resolve, reject) =>
stmt = @db.prepare 'SELECT decks.deck, decks.json FROM decks INNER JOIN users ON decks.user = users.user WHERE users.user = ?' stmt = @db.prepare '''
SELECT decks.deck, decks.json
FROM decks
INNER JOIN users ON decks.user = users.user
WHERE users.user = ?
'''
stmt.all [userID], (err, rows) -> stmt.all [userID], (err, rows) ->
stmt.finalize() stmt.finalize()
if err if err
@ -212,7 +253,10 @@ class FFTCGDB
delDeck: (userID, deckID) -> delDeck: (userID, deckID) ->
new Promise (resolve, reject) => new Promise (resolve, reject) =>
stmt = @db.prepare 'DELETE FROM decks WHERE deck = ? AND user = ?' stmt = @db.prepare '''
DELETE FROM decks
WHERE deck = ? AND user = ?
'''
stmt.run [deckID, userID], (err) -> stmt.run [deckID, userID], (err) ->
stmt.finalize() stmt.finalize()
if err if err

View file

@ -53,7 +53,6 @@ fastify.listen 3001, '0.0.0.0'
logger.error err logger.error err
# Handle termination # Handle termination
process.on 'SIGINT', -> process.on 'SIGUSR2', ->
socket.close()
logger.info 'shutting down after SIGINT' logger.info 'shutting down after SIGINT'
process.exit() process.exit()

View file

@ -17,7 +17,7 @@ class FFTCGSESSION
host: 'redis' host: 'redis'
port: 6379 port: 6379
@db.on 'error', (err) => @db.on 'error', (err) ->
logger.error err.message logger.error err.message
sessionKey: (digest) -> "session.#{digest}" sessionKey: (digest) -> "session.#{digest}"
@ -96,7 +96,7 @@ class FFTCGSESSION
return new Promise (resolve, reject) => return new Promise (resolve, reject) =>
# scan "active" gameKey # scan "active" gameKey
@db.sscan (@gameKey 'active'), cursor, 'COUNT', '100', (err, res) => @db.sscan (@gameKey 'active'), cursor, 'COUNT', '100', (err, res) ->
# add to results set # add to results set
cursor = res[0] cursor = res[0]
@ -109,10 +109,10 @@ class FFTCGSESSION
else else
# recursive call (resolve one step deeper) # recursive call (resolve one step deeper)
allGames set, cursor allGames set, cursor
.then (set) => .then (set) ->
resolve set resolve set
activeGameKeys().then (set) => activeGameKeys().then (set) ->
logger.info "game count: #{Array.from(set).length}" logger.info "game count: #{Array.from(set).length}"
joinGame: (digest, userid) -> joinGame: (digest, userid) ->