Initial commit
This commit is contained in:
55
core/cardService.js
Normal file
55
core/cardService.js
Normal file
@@ -0,0 +1,55 @@
|
||||
const fs = require('fs');
|
||||
var path = require('path');
|
||||
const zlib = require('zlib');
|
||||
|
||||
String.prototype.splice = function (idx, rem, str) {
|
||||
return this.slice(0, idx) + str + this.slice(idx + Math.abs(rem));
|
||||
};
|
||||
|
||||
const color = ['grey', 'red', 'green', 'blue']
|
||||
|
||||
exports.BoardCard = (card, next) => {
|
||||
filepath = path.join(process.cwd(), 'Cards', card.nme + '.svg')
|
||||
fs.readFile(filepath, 'utf8', function (err, source) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
if (card.sym > 0) {
|
||||
const cir = '<circle cx="0" cy="0" r="70" stroke="clrX" stroke-width="12" fill="clrX" fill-opacity="0.5" />'
|
||||
let coin = cir.replace(/clrX/g, color[card.sym])
|
||||
let x = String(source).indexOf('</svg>')
|
||||
source = source.splice(x, 0, coin);
|
||||
}
|
||||
|
||||
if (card.cls > 0) {
|
||||
let crs = '<path d="M -70,-60 70,60 z m 1,120 -1,1 140,-120 -1,1" style="stroke:clrX;stroke-width:32;stroke-linejoin:round" />'
|
||||
let chk = crs.replace(/clrX/g, color[card.sym])
|
||||
let x = String(source).indexOf('</svg>')
|
||||
source = source.splice(x, 0, chk);
|
||||
}
|
||||
|
||||
var buff = Buffer.from(JSON.stringify({ inx: card.inx, crd: card.crd, img: source }))
|
||||
zlib.gzip(buff, function (_, squished) {
|
||||
next(null, squished);
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
exports.DealtCard = (card, next) => {
|
||||
filepath = path.join(process.cwd(), 'Cards', card.nme + '.svg')
|
||||
fs.readFile(filepath, 'utf8', function (err, source) {
|
||||
var buff = Buffer.from(JSON.stringify({ inx: card.inx, dlr: card.dlr, img: source }))
|
||||
zlib.gzip(buff, function (_, squished) {
|
||||
next(null, squished);
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
exports.getSvg = (card, res) => {
|
||||
filepath = path.join(process.cwd(), 'Cards', card + '.svg')
|
||||
fs.readFile(filepath, 'utf8', function (err, source) {
|
||||
res.setHeader('Content-type', 'image/svg+xml');
|
||||
res.end(source);
|
||||
})
|
||||
}
|
||||
123
core/dbService.js
Normal file
123
core/dbService.js
Normal file
@@ -0,0 +1,123 @@
|
||||
const sql = require('mssql')
|
||||
const settings = require("../settings")
|
||||
// async/await style:
|
||||
const pool = new sql.ConnectionPool(settings.dbConfig);
|
||||
const poolConn = pool.connect();
|
||||
const Log = require("./logMessage");
|
||||
|
||||
pool.on('error', err => {
|
||||
Log.logStuff('Connect err: ' + err);
|
||||
throw err;
|
||||
})
|
||||
|
||||
function getIP(req) {
|
||||
var ip = (req.headers["X-Forwarded-For"] ||
|
||||
req.headers["x-forwarded-for"] || '').split(',')[0] ||
|
||||
req.connection.remoteAddress;
|
||||
return ip;
|
||||
}
|
||||
|
||||
function parseCookies(req) {
|
||||
var list = {}, rc = req.headers.cookie;
|
||||
rc && rc.split(';').forEach(function (cookie) {
|
||||
var parts = cookie.split('=');
|
||||
list[parts.shift().trim()] = decodeURI(parts.join('='))
|
||||
});
|
||||
return list;
|
||||
}
|
||||
|
||||
exports.systemAccess = function (req, res) {
|
||||
var data = "";
|
||||
req.on('data', function (chunk) {
|
||||
data += chunk;
|
||||
});
|
||||
req.on('end', function () {
|
||||
poolConn.then((pc) => {
|
||||
pc.request()
|
||||
.input('ipaddr', sql.VarChar(50), getIP(req))
|
||||
.input('rqst', sql.VarChar(sql.MAX), data)
|
||||
.output('resp', sql.VarChar(sql.MAX))
|
||||
.output('auth', sql.VarChar(50))
|
||||
.execute('spLogin', (err, result) => {
|
||||
if (err) {
|
||||
res.writeHead(503, { "Content-Type": "plain/text" });
|
||||
res.write(err);
|
||||
res.end();
|
||||
} else {
|
||||
res.writeHead(200, {
|
||||
"Set-Cookie": "sessid=" + result.output.auth,
|
||||
"Content-Type": "application/json"
|
||||
});
|
||||
res.write(result.output.resp);
|
||||
res.end();
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
exports.systemService = function (req, res) {
|
||||
var data = "";
|
||||
req.on('data', function (chunk) {
|
||||
data += chunk;
|
||||
});
|
||||
var cookies = parseCookies(req);
|
||||
req.on('end', function () {
|
||||
poolConn.then((pc) => {
|
||||
pc.request()
|
||||
.input('auth', sql.VarChar(50), cookies.sessid)
|
||||
.input('rqst', sql.VarChar(sql.MAX), data)
|
||||
.output('resp', sql.VarChar(sql.MAX))
|
||||
.execute('spSysRoute', (err, result) => {
|
||||
if (err) {
|
||||
res.writeHead(503, { "Content-Type": "plain/text" });
|
||||
res.write(err);
|
||||
res.end();
|
||||
} else {
|
||||
res.writeHead(200, { "Content-Type": "application/json" });
|
||||
res.write(result.output.resp);
|
||||
res.end();
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
exports.nextPlayer = (game, data, callback) => {
|
||||
let obj = JSON.stringify(data);
|
||||
poolConn.then((pc) => {
|
||||
pc.request()
|
||||
.input('game', sql.Char(5), game)
|
||||
.input('rqst', sql.VarChar(sql.MAX), obj)
|
||||
.output('resp', sql.VarChar(sql.MAX))
|
||||
.execute('spPlayerNext', (err, result) => {
|
||||
if (err) {
|
||||
callback(err)
|
||||
} else {
|
||||
callback(null, result.output.resp)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
exports.systemAction = function (token, game, data, callback) {
|
||||
let obj = JSON.stringify(data);
|
||||
poolConn.then((pc) => {
|
||||
pc.request()
|
||||
.input('tokn', sql.VarChar(50), token)
|
||||
.input('game', sql.Char(5), game)
|
||||
.input('rqst', sql.VarChar(sql.MAX), obj)
|
||||
.output('resp', sql.VarChar(sql.MAX))
|
||||
.execute('spSysAction', (err, result) => {
|
||||
if (err) {
|
||||
callback(err)
|
||||
} else {
|
||||
callback(null, result.output.resp)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
exports.systemExit = () => {
|
||||
console.log('good bye')
|
||||
}
|
||||
7
core/logMessage.js
Normal file
7
core/logMessage.js
Normal file
@@ -0,0 +1,7 @@
|
||||
var fs = require('fs');
|
||||
exports.logStuff = function (msg) {
|
||||
fs.appendFile('message.txt', msg, function (err) {
|
||||
if (err) throw err;
|
||||
console.log('Saved!');
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user