This repository was archived by the owner on Nov 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite.js
134 lines (120 loc) · 4.88 KB
/
sqlite.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
'use strict';
const sqlite = require("sqlite3").verbose();
const SELECT_PLAYER = "SELECT name, pass, class as clazz, online, level, next, idled, penalties, lastLogin, items FROM players";
var db;
module.exports = function (callback) {
if (!db) {
db = new sqlite.Database(require('path').resolve(__dirname, '.database'), function (error) {
setupDatabase(error, callback);
});
}
return db;
};
// TODO: Call this when the bot starts
module.exports.getOnlinePlayers = function (callback) {
if (typeof callback !== "function") return; // This isn't valid, don't process
db.all(`${SELECT_PLAYER} WHERE online = 1`, function (error, rows) {
// Wrap into a single object
callback({rows: rows, error: error});
});
};
module.exports.getTotalPlayers = function (callback) {
if (typeof callback !== "function") return 0;
db.get("SELECT COUNT(*) as value FROM players", function (error, row) {
callback({value: row ? row.value : 0, error: error});
});
};
module.exports.getTopPlayers = function (limit, callback) {
if (typeof limit === "function") {
callback = limit;
limit = 3;
} else if (!limit || limit < 1) {
limit = 3;
}
if (typeof callback !== "function") return; // This isn't valid, don't process
// Highest 'level' first, followed by lowest 'next' (means they have more "exp")
db.all(`${SELECT_PLAYER} ORDER BY level DESC, next ASC LIMIT ${limit}`, function (error, rows) {
// Wrap into an object
callback({rows: rows, error: error});
});
};
module.exports.getPlayer = function (name, callback) {
if (typeof callback !== "function") return; // This isn't valid, don't process
db.get(`${SELECT_PLAYER} WHERE name = ?`, name, function (error, row) {
if (row) row.fromDB = true;
callback({data: row, error: error});
});
};
// Pass player to provide more information without needing to change the signature
module.exports.deletePlayer = function (player, callback) {
// Callback isn't required here
db.run("DELETE FROM players WHERE name = ?", player.name, function (error) {
if (typeof callback !== "function") return; // This isn't valid, don't process
callback({error: error, deletions: this.changes});
});
};
module.exports.savePlayer = function (player, callback) {
// Callback isn't required here
if (player.isStored()) db.run("UPDATE players SET pass = $pass, class = $class, online = $online, level = $level, next = $next, idled = $idled, penalties = $penalties, lastLogin = $lastLogin, itemSum = $itemSum, items = $items WHERE name = $name", function(error) {
if (typeof callback !== "function") return; // This isn't valid, don't process
callback({error: error, saved: error ? false : this.changes});
});
else db.run("INSERT INTO players VALUES ($name, $pass, $class, $online, $level, $next, $idled, $penalties, $lastLogin, $itemSum, $items)", player.save(), function (error) {
if (typeof callback !== "function") return; // This isn't valid, don't process
callback({error: error, saved: error ? false : this.lastID});
});
};
module.exports.updatePlayer = function (player, what, callback) {
// Callback isn't required here
// TODO
};
module.exports.loadChannels = function (callback) {
if (typeof callback !== "function") return; // This isn't valid, don't process
db.all("SELECT channel, options FROM channels", function (error, rows) {
if (rows) rows.forEach(function (row) {
row.options = JSON.parse(row.options);
});
callback({rows: rows, error: error});
});
};
module.exports.saveChannels = function (channels) {
var statement = db.prepare("INSERT OR REPLACE INTO channels VALUES ($channel, $options)");
Object.keys(channels).forEach(function (key) {
var options = JSON.stringify(channels[key]);
if (!options) return;
statement.run({
$channel: key,
$options: options
}, function (error) {
if (error) global.logger.error(`IdleRPG: Error saving ${key}: ${error}`);
});
});
};
// *** Setup goes down here
var player_columns = [
"name TEXT UNIQUE COLLATE NOCASE",
"pass TEXT",
"class TEXT",
"online INT",
"level INT",
"next INT",
"idled INT",
"penalties INT",
"lastLogin INT",
"itemSum INT",
"items TEXT", // Store all items in a giant blob
];
function setupDatabase(error, callback) {
if (error) return global.logger.error(error);
global.logger.debug("Setting up IdleRPG database");
var player_def = player_columns.join(", ");
// Sadly CREATE TABLE doesn't return any way to varify if it created or not
db.run(`CREATE TABLE IF NOT EXISTS players (${player_def})`, function (error) {
if (error) global.logger.error(error);
if (typeof callback === "function") callback("players", error);
db.run("CREATE TABLE IF NOT EXISTS channels (channel TEXT UNIQUE, options TEXT)", function (error) {
if (error) global.logger.error(error);
if (typeof callback === "function") callback("channels", error);
});
});
}