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

@ -33,31 +33,49 @@ class FFTCGDB
@db.run 'DROP TABLE IF EXISTS users;', (err) =>
logger.error err.message if err
@db.run '''
CREATE TABLE users (
user integer PRIMARY KEY,
login text NOT NULL COLLATE NOCASE,
pwdhash text NOT NULL,
settings text,
UNIQUE(login)
);
''', (err) =>
CREATE TABLE users (
user integer PRIMARY KEY,
login text NOT NULL COLLATE NOCASE,
pwdhash text NOT NULL,
settings text,
UNIQUE(login)
);
''', (err) =>
logger.error err.message if err
@db.run 'DROP TABLE IF EXISTS decks;', (err) =>
logger.error err.message if err
@db.run 'DROP TABLE IF EXISTS decks;', (err) =>
@db.run '''
CREATE TABLE decks (
deck integer PRIMARY KEY,
user integer NOT NULL,
json text,
FOREIGN KEY (user) REFERENCES users (user)
ON DELETE CASCADE
);
''', (err) =>
logger.error err.message if err
@db.run '''
CREATE TABLE decks (
deck integer PRIMARY KEY,
user integer NOT NULL,
json text,
FOREIGN KEY (user) REFERENCES users (user)
ON DELETE CASCADE
);
''', (err) =>
logger.error err.message if err
@db.run '''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"}]}');'''
@db.run '''
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'
@ -75,7 +93,7 @@ class FFTCGDB
validate: (login, password) ->
defined = (value) -> value? and value isnt ''
new Promise (resolve, reject) =>
new Promise (resolve, reject) ->
if (defined login) and (defined password)
# both are defined
resolve null
@ -97,7 +115,10 @@ class FFTCGDB
else
# 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.finalize()
if err
@ -119,8 +140,12 @@ class FFTCGDB
@validate login, password
.then =>
# get users table row
stmt = @db.prepare 'SELECT * FROM users WHERE login = ?'
stmt.get [login], (err, row) =>
stmt = @db.prepare '''
SELECT *
FROM users
WHERE login = ?
'''
stmt.get [login], (err, row) ->
stmt.finalize()
if err
logger.warn "login: FAIL db '#{err.code}' for '#{login}'"
@ -153,8 +178,12 @@ class FFTCGDB
getUser: (userID) ->
new Promise (resolve, reject) =>
# get users table row
stmt = @db.prepare 'SELECT * FROM users WHERE user = ?'
stmt.get [userID], (err, row) =>
stmt = @db.prepare '''
SELECT *
FROM users
WHERE user = ?
'''
stmt.get [userID], (err, row) ->
stmt.finalize()
if err
logger.warn "get: FAIL db '#{err.code}' for '#{userID}'"
@ -173,7 +202,10 @@ class FFTCGDB
addDeck: (userID, deckCards) ->
new Promise (resolve, reject) =>
# 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.finalize()
if err
@ -186,7 +218,11 @@ class FFTCGDB
modDeck: (userID, deckID, deckCards) ->
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.finalize()
if err
@ -200,7 +236,12 @@ class FFTCGDB
getDecks: (userID) ->
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.finalize()
if err
@ -212,7 +253,10 @@ class FFTCGDB
delDeck: (userID, deckID) ->
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.finalize()
if err

View file

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

View file

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