forked from TappedOutReborn/GameServer-Reborn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.js
More file actions
131 lines (117 loc) · 4.26 KB
/
setup.js
File metadata and controls
131 lines (117 loc) · 4.26 KB
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
import fs from "fs";
import { randomBytes } from "crypto";
import sqlite3 from "sqlite3";
const CONFIG_PATH = "./config.json";
const DEFAULT_CONFIG = {
verbose: true,
ip: "0.0.0.0",
listenPort: 4242,
proxyPort: 8080,
dataDirectory: "data",
startingDonuts: 0,
startingUID: 1000000000000,
startingMID: "3042000000000000",
adminKey: "",
useTSTO_API: false,
TSTO_APIkey: "",
TSTO_APIteam: "",
useSMTP: false,
SMTPhost: "mail.example.com",
SMTPport: 465,
SMTPsecure: true,
SMTPuser: "user",
SMTPpass: "pass",
serveDlcsLocally: true,
localDlcFolder: "/dlc",
backupDirectory: "backups",
backupInterval: 21600000,
maxBackups: 10
};
if (!fs.existsSync(CONFIG_PATH)) {
DEFAULT_CONFIG.adminKey = randomBytes(32).toString("hex");
fs.writeFileSync(CONFIG_PATH, JSON.stringify(DEFAULT_CONFIG, null, 2), "utf8");
console.log("Created default config.json — please review and update it before running the server.");
} else {
// Merge in any missing keys without overwriting existing values
const existing = JSON.parse(fs.readFileSync(CONFIG_PATH, "utf8"));
let changed = false;
for (const [key, value] of Object.entries(DEFAULT_CONFIG)) {
if (!(key in existing)) {
existing[key] = value;
changed = true;
}
}
// Generate adminKey if it's missing or empty
if (!existing.adminKey) {
existing.adminKey = randomBytes(32).toString("hex");
changed = true;
}
if (changed) {
fs.writeFileSync(CONFIG_PATH, JSON.stringify(existing, null, 2), "utf8");
console.log("config.json updated with missing default keys.");
}
}
// --- Database initialization ---
const config = JSON.parse(fs.readFileSync(CONFIG_PATH, "utf8"));
// Ensure data directory exists
if (!fs.existsSync(config.dataDirectory)) {
fs.mkdirSync(config.dataDirectory, { recursive: true });
console.log(`Created data directory: ${config.dataDirectory}`);
}
const dbPath = `${config.dataDirectory}/users.db`;
await new Promise((resolve, reject) => {
const db = new sqlite3.Database(dbPath, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => {
if (err) return reject(err);
db.serialize(() => {
// UserData table
db.run(`
CREATE TABLE IF NOT EXISTS UserData (
MayhemId TEXT UNIQUE,
UserId INT UNIQUE,
UserName TEXT UNIQUE,
UserEmail TEXT UNIQUE,
UserCred INT,
UserAccessToken TEXT UNIQUE,
UserAccessCode TEXT UNIQUE,
UserRefreshToken TEXT UNIQUE,
SessionId TEXT UNIQUE,
SessionKey TEXT UNIQUE,
WholeLandToken TEXT,
LandSavePath TEXT,
CurrencySavePath TEXT,
AdvertisingId TEXT,
LastPlayedTime INTEGER
);
`, (err) => { if (err) console.error("Could not create UserData table:", err.message); });
// Add LastPlayedTime column for existing DBs (ignore error if column already exists)
db.run("ALTER TABLE UserData ADD COLUMN LastPlayedTime INTEGER;", () => {});
// Initialize LastPlayedTime for existing rows
db.run("UPDATE UserData SET LastPlayedTime = ? WHERE LastPlayedTime IS NULL;", [Date.now()]);
// UserData indexes
db.run("CREATE INDEX IF NOT EXISTS idx_userid ON UserData(UserId);");
db.run("CREATE INDEX IF NOT EXISTS idx_mayhemid ON UserData(MayhemId);");
db.run("CREATE INDEX IF NOT EXISTS idx_last_played_time ON UserData(LastPlayedTime);");
// Friends table
db.run(`
CREATE TABLE IF NOT EXISTS Friends (
ownerMayhemId TEXT,
friendMayhemId TEXT,
status TEXT,
createdAt INTEGER
);
`, (err) => { if (err) console.error("Could not create Friends table:", err.message); });
// Friends indexes
db.run("CREATE INDEX IF NOT EXISTS idx_friends_owner_status ON Friends(ownerMayhemId, status);");
db.run("CREATE INDEX IF NOT EXISTS idx_friends_friend_status ON Friends(friendMayhemId, status);");
db.run("CREATE INDEX IF NOT EXISTS idx_friends_created ON Friends(createdAt);");
db.run("SELECT 1;", (err) => {
// Final no-op to flush serialize queue, then close
db.close((closeErr) => {
if (closeErr) return reject(closeErr);
console.log("Database initialized.");
resolve();
});
});
});
});
});