-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdatabase.cpp
More file actions
183 lines (169 loc) · 5.05 KB
/
database.cpp
File metadata and controls
183 lines (169 loc) · 5.05 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
/*
Copyright (C) 2025 Flyinghead
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "database.h"
#include "common.h"
#include <cstdio>
#include <stdexcept>
#include <algorithm>
static std::string databasePath = "iwango.db";
void setDatabasePath(const std::string& databasePath)
{
if (!databasePath.empty()) {
::databasePath = databasePath;
Database db(databasePath);
}
}
//
// Gate server
//
bool createHandle(GameId gameId, const std::string& user, int index, const std::string& handle)
{
try {
Database db(databasePath);
Statement stmt(db, "INSERT INTO USER_HANDLE (USER_NAME, GAME, HANDLE_INDEX, HANDLE) VALUES (?, ?, ?, ?)");
stmt.bind(1, user);
stmt.bind(2, (int)gameId);
stmt.bind(3, index);
stmt.bind(4, handle);
stmt.step();
return true;
} catch (const UniqueConstraintViolation& e) {
throw e;
} catch (const std::runtime_error& e) {
ERROR_LOG(gameId, "createHandle: %s", e.what());
return false;
}
}
bool replaceHandle(GameId gameId, const std::string& user, int index, const std::string& handle)
{
try {
Database db(databasePath);
Statement stmt(db, "UPDATE USER_HANDLE SET HANDLE = ? WHERE USER_NAME = ? AND GAME = ? AND HANDLE_INDEX = ?");
stmt.bind(1, handle);
stmt.bind(2, user);
stmt.bind(3, (int)gameId);
stmt.bind(4, index);
stmt.step();
return true;
} catch (const UniqueConstraintViolation& e) {
throw e;
} catch (const std::runtime_error& e) {
ERROR_LOG(gameId, "replaceHandle: %s", e.what());
return false;
}
}
bool deleteHandle(GameId gameId, const std::string& user, int index)
{
try {
Database db(databasePath);
db.exec("BEGIN TRANSACTION");
{
Statement stmt(db, "DELETE FROM USER_HANDLE WHERE USER_NAME = ? AND GAME = ? AND HANDLE_INDEX = ?");
stmt.bind(1, user);
stmt.bind(2, (int)gameId);
stmt.bind(3, index);
stmt.step();
}
for (int i = index + 1; i < 8; i++)
{
Statement stmt(db, "UPDATE USER_HANDLE SET HANDLE_INDEX = HANDLE_INDEX - 1 WHERE USER_NAME = ? AND GAME = ? AND HANDLE_INDEX = ?");
stmt.bind(1, user);
stmt.bind(2, (int)gameId);
stmt.bind(3, i);
stmt.step();
if (stmt.changedRows() == 0)
break;
}
db.exec("COMMIT");
return true;
} catch (const std::runtime_error& e) {
ERROR_LOG(gameId, "deleteHandle: %s", e.what());
return false;
}
}
std::vector<std::string> getHandles(GameId gameId, const std::string& user, const std::string& defaultHandle)
{
std::vector<std::string> handles;
try {
Database db(databasePath);
Statement stmt(db, "SELECT HANDLE FROM USER_HANDLE WHERE USER_NAME = ? AND GAME = ? ORDER BY HANDLE_INDEX");
stmt.bind(1, user);
stmt.bind(2, (int)gameId);
while (stmt.step())
handles.push_back(stmt.getStringColumn(0));
if (handles.empty() && !defaultHandle.empty()) {
try {
if (createHandle(gameId, user, 0, defaultHandle))
handles.push_back(defaultHandle);
} catch (const UniqueConstraintViolation&) {
}
}
} catch (const std::runtime_error& e) {
ERROR_LOG(gameId, "getHandles: %s", e.what());
}
return handles;
}
//
// Lobby server
//
void updateExtraUserMem(GameId gameId, const std::string& user, const uint8_t *data, int offset, int size)
{
try {
Database db(databasePath);
std::vector<uint8_t> blob;
bool newBlob = false;
Statement stmt(db, "SELECT EXTRAMEM FROM USER_EXTRAMEM WHERE USER_NAME = ? AND GAME = ?");
stmt.bind(1, user);
stmt.bind(2, (int)gameId);
if (stmt.step())
blob = stmt.getBlobColumn(0);
else
newBlob = true;
if ((int)blob.size() < offset + size)
blob.resize(offset + size);
memcpy(blob.data() + offset, data, size);
if (newBlob)
{
Statement stmt(db, "INSERT INTO USER_EXTRAMEM (USER_NAME, GAME, EXTRAMEM) VALUES (?, ?, ?)");
stmt.bind(1, user);
stmt.bind(2, (int)gameId);
stmt.bind(3, data, size);
stmt.step();
}
else
{
Statement stmt(db, "UPDATE USER_EXTRAMEM SET EXTRAMEM = ? WHERE USER_NAME = ? AND GAME = ?");
stmt.bind(1, data, size);
stmt.bind(2, user);
stmt.bind(3, (int)gameId);
stmt.step();
}
} catch (const std::runtime_error& e) {
ERROR_LOG(gameId, "updateExtraUserMem: %s", e.what());
}
}
std::vector<uint8_t> getExtraUserMem(GameId gameId, const std::string& user)
{
try {
Database db(databasePath);
Statement stmt(db, "SELECT EXTRAMEM FROM USER_EXTRAMEM WHERE USER_NAME = ? AND GAME = ?");
stmt.bind(1, user);
stmt.bind(2, (int)gameId);
if (stmt.step())
return stmt.getBlobColumn(0);
} catch (const std::runtime_error& e) {
ERROR_LOG(gameId, "getExtraUserMem: %s", e.what());
}
return {};
}