@@ -4,12 +4,12 @@ let skins = require('../config/skins'); | |||
let roles = require('../config/roles'); | |||
let profanities = require('../misc/profanities'); | |||
let fixes = require('../fixes/fixes'); | |||
let loginRewards = require('../config/loginRewards'); | |||
let mail = require('../mail/mail'); | |||
let scheduler = require('../misc/scheduler'); | |||
let spirits = require('../config/spirits'); | |||
let ga = require('../security/ga'); | |||
const checkLoginRewards = require('./auth/checkLoginRewards'); | |||
module.exports = { | |||
type: 'auth', | |||
@@ -22,15 +22,14 @@ module.exports = { | |||
customChannels: [], | |||
play: function (data) { | |||
play: async function (data) { | |||
if (!this.username) | |||
return; | |||
let character = this.characters[data.data.name]; | |||
if (!character) | |||
return; | |||
if (character.permadead) | |||
else if (character.permadead) | |||
return; | |||
character.stash = this.stash; | |||
@@ -38,50 +37,13 @@ module.exports = { | |||
this.charname = character.name; | |||
this.checkLoginReward(data, character); | |||
checkLoginRewards(this, data, character, this.onSendRewards.bind(this, data, character)); | |||
cons.modifyPlayerCount(1); | |||
}, | |||
checkLoginReward: function (data, character) { | |||
let accountInfo = this.accountInfo; | |||
let time = scheduler.getTime(); | |||
let lastLogin = accountInfo.lastLogin; | |||
if (!lastLogin || lastLogin.day !== time.day) { | |||
let daysSkipped = 1; | |||
if (lastLogin) { | |||
if (time.day > lastLogin.day) | |||
daysSkipped = time.day - lastLogin.day; | |||
else { | |||
let daysInMonth = scheduler.daysInMonth(lastLogin.month); | |||
daysSkipped = (daysInMonth - lastLogin.day) + time.day; | |||
for (let i = lastLogin.month + 1; i < time.month - 1; i++) | |||
daysSkipped += scheduler.daysInMonth(i); | |||
} | |||
} | |||
if (daysSkipped === 1) { | |||
accountInfo.loginStreak++; | |||
if (accountInfo.loginStreak > 21) | |||
accountInfo.loginStreak = 21; | |||
} else { | |||
accountInfo.loginStreak -= (daysSkipped - 1); | |||
if (accountInfo.loginStreak < 1) | |||
accountInfo.loginStreak = 1; | |||
} | |||
let rewards = loginRewards.generate(accountInfo.loginStreak); | |||
mail.sendMail(character.name, rewards, this.onSendRewards.bind(this, data, character)); | |||
} else | |||
this.onSendRewards(data, character); | |||
accountInfo.lastLogin = time; | |||
}, | |||
onSendRewards: async function (data, character) { | |||
//Bit of a hack. Rethink doesn't havve a busy list | |||
//Bit of a hack. Rethink doesn't have a busy list | |||
if (mail.busy) | |||
delete mail.busy[character.name]; | |||
@@ -0,0 +1,65 @@ | |||
const scheduler = require('../../misc/scheduler'); | |||
const loginRewards = require('../../config/loginRewards'); | |||
const mail = require('../mail/mail'); | |||
const calculateDaysSkipped = (oldTime, newTime) => { | |||
let daysSkipped = 1; | |||
if (oldTime.year === newTime.year && oldTime.month === newTime.month) { | |||
//Same year and month | |||
daysSkipped = newTime.day - oldTime.day; | |||
} else if (oldTime.year === newTime.year) { | |||
//Same month | |||
let daysInMonth = scheduler.daysInMonth(oldTime.month); | |||
daysSkipped = (daysInMonth - oldTime.day) + newTime.day; | |||
for (let i = oldTime.month + 1; i < newTime.month - 1; i++) | |||
daysSkipped += scheduler.daysInMonth(i); | |||
} else { | |||
//Different year and month | |||
const daysInMonth = scheduler.daysInMonth(oldTime.month); | |||
daysSkipped = (daysInMonth - oldTime.day) + newTime.day; | |||
for (let i = oldTime.year + 1; i < newTime.year - 1; i++) | |||
daysSkipped += 365; | |||
for (let i = oldTime.month + 1; i < 12; i++) | |||
daysSkipped += scheduler.daysInMonth(i); | |||
for (let i = 0; i < newTime.month - 1; i++) | |||
daysSkipped += scheduler.daysInMonth(i); | |||
} | |||
return daysSkipped; | |||
}; | |||
module.exports = async (cpnAuth, data, character, cbDone) => { | |||
const accountInfo = cpnAuth.accountInfo; | |||
const time = scheduler.getTime(); | |||
const lastLogin = accountInfo.lastLogin; | |||
accountInfo.lastLogin = time; | |||
if ( | |||
!lastLogin || | |||
( | |||
lastLogin.day === time.day && | |||
lastLogin.month === time.month && | |||
lastLogin.year === time.year | |||
) | |||
) { | |||
cbDone(); | |||
return; | |||
} | |||
const daysSkipped = calculateDaysSkipped(lastLogin, time); | |||
if (daysSkipped === 1) | |||
accountInfo.loginStreak++; | |||
else | |||
accountInfo.loginStreak -= (daysSkipped - 1); | |||
accountInfo.loginStreak = Math.min(1, Math.max(21, accountInfo.loginStreak)); | |||
const rewards = loginRewards.generate(accountInfo.loginStreak); | |||
mail.sendMail(character.name, rewards, cbDone); | |||
}; |
@@ -0,0 +1,8 @@ | |||
let serverConfig = require('../config/serverConfig'); | |||
const mappings = { | |||
sqlite: './ioSqlite', | |||
rethink: './ioRethink' | |||
}; | |||
module.exports = require(mappings[serverConfig.db]); |
@@ -1,4 +1,5 @@ | |||
const serverConfig = require('../config/serverConfig'); | |||
const tableNames = require('./tables'); | |||
const r = require('rethinkdbdash')({ | |||
host: serverConfig.dbHost, | |||
@@ -6,21 +7,6 @@ const r = require('rethinkdbdash')({ | |||
db: serverConfig.dbName | |||
}); | |||
const tables = [ | |||
'character', | |||
'characterList', | |||
'stash', | |||
'skins', | |||
'login', | |||
'leaderboard', | |||
'customMap', | |||
'mail', | |||
'customChannels', | |||
'error', | |||
'modLog', | |||
'accountInfo' | |||
]; | |||
module.exports = { | |||
staticCon: null, | |||
@@ -37,7 +23,7 @@ module.exports = { | |||
} | |||
for (const table of tables) { | |||
for (const table of tableNames) { | |||
try { | |||
await r.tableCreate(table).run(); | |||
} catch (e) { |
@@ -1,10 +1,5 @@ | |||
let util = require('util'); | |||
let serverConfig = require('../config/serverConfig'); | |||
if (serverConfig.db === 'rethink') { | |||
module.exports = require('./ioRethink'); | |||
return; | |||
} | |||
const tableNames = require('./tables'); | |||
module.exports = { | |||
db: null, | |||
@@ -13,20 +8,7 @@ module.exports = { | |||
buffer: [], | |||
processing: [], | |||
tables: { | |||
character: null, | |||
characterList: null, | |||
stash: null, | |||
skins: null, | |||
login: null, | |||
leaderboard: null, | |||
customMap: null, | |||
mail: null, | |||
customChannels: null, | |||
error: null, | |||
modLog: null, | |||
accountInfo: null | |||
}, | |||
tables: {}, | |||
init: async function (cbReady) { | |||
let sqlite = require('sqlite3').verbose(); | |||
@@ -34,10 +16,10 @@ module.exports = { | |||
}, | |||
onDbCreated: function (cbReady) { | |||
let db = this.db; | |||
let tables = this.tables; | |||
let scope = this; | |||
db.serialize(function () { | |||
for (let t in tables) { | |||
for (let t in tableNames) { | |||
db.run(` | |||
CREATE TABLE ${t} (key VARCHAR(50), value TEXT) | |||
`, scope.onTableCreated.bind(scope, t)); | |||
@@ -46,6 +28,7 @@ module.exports = { | |||
cbReady(); | |||
}, this); | |||
}, | |||
onTableCreated: async function (table) { | |||
}, |
@@ -0,0 +1,15 @@ | |||
module.exports = [ | |||
'character', | |||
'characterList', | |||
'stash', | |||
'skins', | |||
'login', | |||
'leaderboard', | |||
'customMap', | |||
'mail', | |||
'customChannels', | |||
'error', | |||
'modLog', | |||
'accountInfo', | |||
'mtxStash' | |||
]; |
@@ -100,6 +100,7 @@ module.exports = { | |||
hour: time.getHours(), | |||
day: time.getDate(), | |||
month: time.getMonth() + 1, | |||
year: time.getUTCFullYear(), | |||
weekday: time.getDay() | |||
}; | |||
}, | |||