forked from CodeSpartan/MMOKitPersistenceServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseSqlite.cs
More file actions
251 lines (226 loc) · 11.2 KB
/
Copy pathDatabaseSqlite.cs
File metadata and controls
251 lines (226 loc) · 11.2 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using Microsoft.Data.Sqlite;
using System.Data;
using System.Data.Common;
namespace PersistenceServer
{
public class DatabaseSqlite : Database
{
public DatabaseSqlite(SettingsReader settings) : base(settings)
{
ConnectionParams = $"Data Source={settings.SqliteFilename};";
GetIdentitySqlCommand = "SELECT last_insert_rowid();";
}
protected override async Task<DbConnection> GetConnection(string parameters)
{
var connection = new SqliteConnection(parameters);
await connection.OpenAsync();
return connection;
}
protected override DbCommand GetCommand(string parameters, DbConnection? connection) => new SqliteCommand(parameters, (SqliteConnection?)connection);
public override async Task CheckCreateDatabase(SettingsReader settings)
{
// get the number of tables in the database (from a special table called sqlite_master)
var getTablesQuery = await RunQuery($"SELECT count(*) FROM sqlite_master WHERE type = 'table';");
// if there's 0 tables, it means the database is new, so we create them
if (getTablesQuery.GetBigInt(0, "count(*)") == 0) // count returns BigInt
{
Console.Write("Database not found or is empty: creating... ");
// create tables
await RunNonQuery(
@"CREATE TABLE ""accounts"" (
""id"" INTEGER NOT NULL UNIQUE,
""name"" TEXT UNIQUE,
""steamid"" TEXT UNIQUE,
""password"" TEXT,
""salt"" TEXT,
""email"" TEXT,
""status"" INTEGER,
PRIMARY KEY(""id"" AUTOINCREMENT),
UNIQUE(""name"")
);
CREATE UNIQUE INDEX accname
ON accounts(name);
CREATE UNIQUE INDEX accsteamid
ON accounts(steamid);
CREATE TABLE ""guilds"" (
""id"" INTEGER NOT NULL UNIQUE,
""name"" TEXT UNIQUE,
""serialized"" TEXT,
PRIMARY KEY(""id"" AUTOINCREMENT),
UNIQUE(""name"")
);
CREATE UNIQUE INDEX guildname
ON guilds(name);
CREATE TABLE ""characters"" (
""id"" INTEGER NOT NULL UNIQUE,
""name"" TEXT UNIQUE,
""owner"" INTEGER,
""guild"" INTEGER,
""guildrank"" INTEGER,
""permissions"" INTEGER NOT NULL DEFAULT 0,
""serialized"" TEXT,
PRIMARY KEY(""id"" AUTOINCREMENT),
UNIQUE(""name""),
FOREIGN KEY(""owner"") REFERENCES ""accounts""(""id"") ON UPDATE CASCADE ON DELETE SET NULL,
FOREIGN KEY(""guild"") REFERENCES ""guilds""(""id"") ON UPDATE CASCADE ON DELETE SET NULL
);
CREATE UNIQUE INDEX charname
ON characters(name);
CREATE INDEX charowner
ON characters(owner);
CREATE INDEX charguild
ON characters(guild);
CREATE TABLE ""servers"" (
""id"" INTEGER NOT NULL UNIQUE,
""port"" INTEGER NOT NULL,
""level"" TEXT NOT NULL,
""serialized"" TEXT,
PRIMARY KEY(""id"" AUTOINCREMENT)
);
CREATE UNIQUE INDEX ServerPortLevel ON servers (port, level);
CREATE TABLE ""persistentobjs"" (
""id"" INTEGER NOT NULL UNIQUE,
""level"" TEXT NOT NULL,
""port"" INTEGER NOT NULL,
""objectId"" INTEGER NOT NULL,
""serialized"" TEXT NOT NULL,
PRIMARY KEY(""id"" AUTOINCREMENT)
);
CREATE UNIQUE INDEX PersistentObjIndex ON persistentobjs (objectId, port, level);
");
// ~create tables
Console.WriteLine("done.");
}
else
{
Console.WriteLine($"Database found: {settings.SqliteFilename}");
}
}
public override async Task<int> LoginUser(string accountName, string password)
{
var cmd = GetCommand("SELECT id, password, salt, status FROM accounts WHERE name = @accountName");
cmd.AddParam("@accountName", accountName);
var dt = await RunQuery(cmd);
// if not account with this name is found
if (!dt.HasRows())
{
return -1;
}
var id = (int)dt.GetBigInt(0, "id")!;
var status = dt.GetBigInt(0, "status");
var passwordInDb = dt.GetString(0, "password");
var salt = dt.GetString(0, "salt");
// if status is banned
if (status == -1)
{
return -1;
}
// if wrong password
if (passwordInDb != BCrypt.Net.BCrypt.HashPassword(password, salt + Pepper))
{
return -1;
}
// if everything checks out, allow login by returning user's id
return id;
}
public override async Task<Guild?> CreateGuild(string guildName, int charId)
{
// check if guild with this name exists
var checkCmd = GetCommand("SELECT * FROM guilds WHERE name = @guildName");
checkCmd.AddParam("@guildName", guildName);
var dt = await RunQuery(checkCmd);
if (dt.HasRows()) {
return null;
}
var createGuildCmd = GetCommand("INSERT INTO `guilds` (`id`, `name`) VALUES (NULL, @guildName);");
createGuildCmd.AddParam("@guildName", guildName);
int lastInsertedId = await RunInsert(createGuildCmd);
var updateCharCmd = GetCommand("UPDATE `characters` SET `guild` = @guildId, `guildRank` = '0' WHERE `characters`.`id` = @charId; ");
updateCharCmd.AddParam("@guildId", lastInsertedId);
updateCharCmd.AddParam("@charId", charId);
await RunNonQuery(updateCharCmd);
return new Guild(lastInsertedId, guildName);
}
/*
* Due to a bug in Microsoft.Data.Sqlite, DataTable.Load preserves UNIQUE constraints from JOIN queries
* This makes it impossible to have two rows with the same guild name and it throws an error
* I submitted a bug report https://github.com/dotnet/efcore/issues/30765
* In the meantime, we manually create DataTable columns for this query specifically, which allows us to bypass the bug
*/
public async override Task<Dictionary<int, Guild>> GetGuilds()
{
var result = new Dictionary<int, Guild>();
/*
* An example of what we can expect in return:
*
* guildId guildName charId charName
* 1 Diamond Dogs 1 Arthur Pendragon
* 1 Diamond Dogs 2 Raven
* 2 No Dogs NULL NULL
*
* In this example "Diamond Dogs" has two members: Arthur Pendragon and Raven
* The guild "No Dogs" is memberless. It shouldn't happen, but if it does, we'll print a warning.
*/
await using var conn = await GetConnection(ConnectionParams);
var cmd = GetCommand(@"
SELECT guilds.id as guildId, guilds.name as guildName, characters.id as charId, characters.name as charName, characters.guildRank as guildRank FROM guilds
LEFT JOIN characters
ON guilds.id = characters.guild
", conn);
await using var reader = await cmd.ExecuteReaderAsync();
DataTable dt = new();
dt.Columns.Add("guildId", typeof(int));
dt.Columns.Add("guildName", typeof(string));
dt.Columns.Add("charId", typeof(int));
dt.Columns.Add("charName", typeof(string));
dt.Columns.Add("guildRank", typeof(int));
dt.Load(reader);
await cmd.DisposeAsync();
if (!dt.HasRows()) return result;
//Console.WriteLine("GuildId, GuildName, CharId, CharName, GuildRank");
foreach (var row in dt.Rows.OfType<DataRow>())
{
var guildId = (int)row.GetInt("guildId")!;
var guildName = (string)row.GetString("guildName")!;
var charId = row.GetInt("charId");
var charName = row.GetString("charName");
var guildRank = row.GetInt("guildRank");
//Console.WriteLine($"{guildId}, {guildName}, {charId}, {charName}, {guildRank}");
// if the guild hasn't been initialized yet, do so now
if (!result.ContainsKey(guildId))
{
result.Add(guildId, new Guild(guildId, guildName));
}
if (charId == null || charName == null)
{
Console.WriteLine($"Info: guild \"{guildName}\" (id: {guildId}) is parentless and memberless");
continue;
}
result[guildId].PopulateMember((int)charId, charName, (int)guildRank!);
}
return result;
}
// Because sqlite and mysql diverge when handling upsert operations (insert or update), we got two different functions
// The identifier here is level+port
public override async Task SaveServerInfo(string serializedServerInfo, int port, string level)
{
var cmd = GetCommand("INSERT OR REPLACE INTO `servers` (`id`, `port`, `level`, `serialized`) VALUES (NULL, @port, @level, @serialized)");
cmd.AddParam("@port", port);
cmd.AddParam("@level", level);
cmd.AddParam("@serialized", serializedServerInfo);
await RunNonQuery(cmd);
Console.WriteLine($"{DateTime.Now:HH:mm} Server Info ({port}-{level}) was saved to DB.");
}
// Because sqlite and mysql diverge when handling upsert operations (insert or update), we got two different functions
// The identifier in the DB is a combination of level+port+objectId
public override async Task SavePersistentObject(string level, int port, int objectId, string jsonString)
{
var cmd = GetCommand("INSERT OR REPLACE INTO `persistentobjs` (`id`, `level`, `port`, `objectId`, `serialized`) VALUES (NULL, @level, @port, @objectId, @serialized)");
cmd.AddParam("@level", level);
cmd.AddParam("@port", port);
cmd.AddParam("@objectId", objectId);
cmd.AddParam("@serialized", jsonString);
await RunNonQuery(cmd);
}
}
}