LobbyDatabase.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * LobbyServer.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "LobbyDatabase.h"
  12. #include "SQLiteConnection.h"
  13. void LobbyDatabase::createTables()
  14. {
  15. static const std::string createChatMessages = R"(
  16. CREATE TABLE IF NOT EXISTS chatMessages (
  17. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  18. senderName TEXT,
  19. roomType TEXT,
  20. messageText TEXT,
  21. creationTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
  22. );
  23. )";
  24. static const std::string createTableGameRooms = R"(
  25. CREATE TABLE IF NOT EXISTS gameRooms (
  26. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  27. roomID TEXT,
  28. hostAccountID TEXT,
  29. status INTEGER NOT NULL DEFAULT 0,
  30. playerLimit INTEGER NOT NULL,
  31. creationTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
  32. );
  33. )";
  34. static const std::string createTableGameRoomPlayers = R"(
  35. CREATE TABLE IF NOT EXISTS gameRoomPlayers (
  36. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  37. roomID TEXT,
  38. accountID TEXT
  39. );
  40. )";
  41. static const std::string createTableAccounts = R"(
  42. CREATE TABLE IF NOT EXISTS accounts (
  43. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  44. accountID TEXT,
  45. displayName TEXT,
  46. online INTEGER NOT NULL,
  47. lastLoginTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
  48. creationTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
  49. );
  50. )";
  51. static const std::string createTableAccountCookies = R"(
  52. CREATE TABLE IF NOT EXISTS accountCookies (
  53. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  54. accountID TEXT,
  55. cookieUUID TEXT,
  56. creationTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
  57. );
  58. )";
  59. static const std::string createTableGameRoomInvites = R"(
  60. CREATE TABLE IF NOT EXISTS gameRoomInvites (
  61. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  62. roomID TEXT,
  63. accountID TEXT
  64. );
  65. )";
  66. database->prepare(createChatMessages)->execute();
  67. database->prepare(createTableGameRoomPlayers)->execute();
  68. database->prepare(createTableGameRooms)->execute();
  69. database->prepare(createTableAccounts)->execute();
  70. database->prepare(createTableAccountCookies)->execute();
  71. database->prepare(createTableGameRoomInvites)->execute();
  72. }
  73. void LobbyDatabase::prepareStatements()
  74. {
  75. // INSERT INTO
  76. static const std::string insertChatMessageText = R"(
  77. INSERT INTO chatMessages(senderName, messageText) VALUES( ?, ?);
  78. )";
  79. static const std::string insertAccountText = R"(
  80. INSERT INTO accounts(accountID, displayName, online) VALUES(?,?,0);
  81. )";
  82. static const std::string insertAccessCookieText = R"(
  83. INSERT INTO accountCookies(accountID, cookieUUID) VALUES(?,?);
  84. )";
  85. static const std::string insertGameRoomText = R"(
  86. INSERT INTO gameRooms(roomID, hostAccountID, status, playerLimit) VALUES(?, ?, 'empty', 8);
  87. )";
  88. static const std::string insertGameRoomPlayersText = R"(
  89. INSERT INTO gameRoomPlayers(roomID, accountID) VALUES(?,?);
  90. )";
  91. static const std::string insertGameRoomInvitesText = R"(
  92. INSERT INTO gameRoomInvites(roomID, accountID) VALUES(?,?);
  93. )";
  94. // DELETE FROM
  95. static const std::string deleteGameRoomPlayersText = R"(
  96. DELETE FROM gameRoomPlayers WHERE roomID = ? AND accountID = ?
  97. )";
  98. static const std::string deleteGameRoomInvitesText = R"(
  99. DELETE FROM gameRoomInvites WHERE roomID = ? AND accountID = ?
  100. )";
  101. // UPDATE
  102. static const std::string setGameRoomStatusText = R"(
  103. UPDATE gameRooms
  104. SET status = ?
  105. WHERE roomID = ?
  106. )";
  107. static const std::string setGameRoomPlayerLimitText = R"(
  108. UPDATE gameRooms
  109. SET playerLimit = ?
  110. WHERE roomID = ?
  111. )";
  112. // SELECT FROM
  113. static const std::string getRecentMessageHistoryText = R"(
  114. SELECT senderName, displayName, messageText, strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',cm.creationTime) AS secondsElapsed
  115. FROM chatMessages cm
  116. LEFT JOIN accounts on accountID = senderName
  117. WHERE secondsElapsed < 60*60*18
  118. ORDER BY cm.creationTime DESC
  119. LIMIT 100
  120. )";
  121. static const std::string getIdleGameRoomText = R"(
  122. SELECT roomID
  123. FROM gameRooms
  124. WHERE hostAccountID = ? AND status = 'idle'
  125. LIMIT 1
  126. )";
  127. static const std::string getAccountGameRoomText = R"(
  128. SELECT grp.roomID
  129. FROM gameRoomPlayers grp
  130. LEFT JOIN gameRooms gr ON gr.roomID = grp.roomID
  131. WHERE accountID = ? AND status IN ('public', 'private', 'busy')
  132. LIMIT 1
  133. )";
  134. static const std::string getActiveAccountsText = R"(
  135. SELECT accountID, displayName
  136. FROM accounts
  137. WHERE online = 1
  138. )";
  139. static const std::string getAccountDisplayNameText = R"(
  140. SELECT displayName
  141. FROM accounts
  142. WHERE accountID = ?
  143. )";
  144. static const std::string isAccountCookieValidText = R"(
  145. SELECT COUNT(accountID)
  146. FROM accountCookies
  147. WHERE accountID = ? AND cookieUUID = ? AND strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',creationTime) < ?
  148. )";
  149. static const std::string isGameRoomCookieValidText = R"(
  150. SELECT COUNT(roomID)
  151. FROM gameRooms
  152. LEFT JOIN accountCookies ON accountCookies.accountID = gameRooms.hostAccountID
  153. WHERE roomID = ? AND cookieUUID = ? AND strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',creationTime) < ?
  154. )";
  155. static const std::string isPlayerInGameRoomText = R"(
  156. SELECT COUNT(accountID)
  157. FROM gameRoomPlayers
  158. WHERE accountID = ? AND roomID = ?
  159. )";
  160. static const std::string isPlayerInAnyGameRoomText = R"(
  161. SELECT COUNT(accountID)
  162. FROM gameRoomPlayers
  163. WHERE accountID = ?
  164. )";
  165. static const std::string isAccountIDExistsText = R"(
  166. SELECT COUNT(accountID)
  167. FROM accounts
  168. WHERE accountID = ?
  169. )";
  170. static const std::string isAccountNameExistsText = R"(
  171. SELECT COUNT(displayName)
  172. FROM accounts
  173. WHERE displayName = ?
  174. )";
  175. insertChatMessageStatement = database->prepare(insertChatMessageText);
  176. insertAccountStatement = database->prepare(insertAccountText);
  177. insertAccessCookieStatement = database->prepare(insertAccessCookieText);
  178. insertGameRoomStatement = database->prepare(insertGameRoomText);
  179. insertGameRoomPlayersStatement = database->prepare(insertGameRoomPlayersText);
  180. insertGameRoomInvitesStatement = database->prepare(insertGameRoomInvitesText);
  181. deleteGameRoomPlayersStatement = database->prepare(deleteGameRoomPlayersText);
  182. deleteGameRoomInvitesStatement = database->prepare(deleteGameRoomInvitesText);
  183. setGameRoomStatusStatement = database->prepare(setGameRoomStatusText);
  184. setGameRoomPlayerLimitStatement = database->prepare(setGameRoomPlayerLimitText);
  185. getRecentMessageHistoryStatement = database->prepare(getRecentMessageHistoryText);
  186. getIdleGameRoomStatement = database->prepare(getIdleGameRoomText);
  187. getAccountGameRoomStatement = database->prepare(getAccountGameRoomText);
  188. getActiveAccountsStatement = database->prepare(getActiveAccountsText);
  189. getAccountDisplayNameStatement = database->prepare(getAccountDisplayNameText);
  190. isAccountCookieValidStatement = database->prepare(isAccountCookieValidText);
  191. isPlayerInGameRoomStatement = database->prepare(isPlayerInGameRoomText);
  192. isPlayerInAnyGameRoomStatement = database->prepare(isPlayerInAnyGameRoomText);
  193. isAccountIDExistsStatement = database->prepare(isAccountIDExistsText);
  194. isAccountNameExistsStatement = database->prepare(isAccountNameExistsText);
  195. }
  196. LobbyDatabase::~LobbyDatabase() = default;
  197. LobbyDatabase::LobbyDatabase(const boost::filesystem::path & databasePath)
  198. {
  199. database = SQLiteInstance::open(databasePath, true);
  200. createTables();
  201. prepareStatements();
  202. }
  203. void LobbyDatabase::insertChatMessage(const std::string & sender, const std::string & roomType, const std::string & roomName, const std::string & messageText)
  204. {
  205. insertChatMessageStatement->executeOnce(sender, messageText);
  206. }
  207. bool LobbyDatabase::isPlayerInGameRoom(const std::string & accountID)
  208. {
  209. bool result = false;
  210. isPlayerInAnyGameRoomStatement->setBinds(accountID);
  211. if(isPlayerInAnyGameRoomStatement->execute())
  212. isPlayerInAnyGameRoomStatement->getColumns(result);
  213. isPlayerInAnyGameRoomStatement->reset();
  214. return result;
  215. }
  216. bool LobbyDatabase::isPlayerInGameRoom(const std::string & accountID, const std::string & roomID)
  217. {
  218. bool result = false;
  219. isPlayerInGameRoomStatement->setBinds(accountID, roomID);
  220. if(isPlayerInGameRoomStatement->execute())
  221. isPlayerInGameRoomStatement->getColumns(result);
  222. isPlayerInGameRoomStatement->reset();
  223. return result;
  224. }
  225. std::vector<LobbyChatMessage> LobbyDatabase::getRecentMessageHistory()
  226. {
  227. std::vector<LobbyChatMessage> result;
  228. while(getRecentMessageHistoryStatement->execute())
  229. {
  230. LobbyChatMessage message;
  231. getRecentMessageHistoryStatement->getColumns(message.accountID, message.displayName, message.messageText, message.age);
  232. result.push_back(message);
  233. }
  234. getRecentMessageHistoryStatement->reset();
  235. return result;
  236. }
  237. void LobbyDatabase::setGameRoomStatus(const std::string & roomID, LobbyRoomState roomStatus)
  238. {
  239. setGameRoomStatusStatement->executeOnce(vstd::to_underlying(roomStatus), roomID);
  240. }
  241. void LobbyDatabase::setGameRoomPlayerLimit(const std::string & roomID, uint32_t playerLimit)
  242. {
  243. setGameRoomPlayerLimitStatement->executeOnce(playerLimit, roomID);
  244. }
  245. void LobbyDatabase::insertPlayerIntoGameRoom(const std::string & accountID, const std::string & roomID)
  246. {
  247. insertGameRoomPlayersStatement->executeOnce(roomID, accountID);
  248. }
  249. void LobbyDatabase::deletePlayerFromGameRoom(const std::string & accountID, const std::string & roomID)
  250. {
  251. deleteGameRoomPlayersStatement->executeOnce(roomID, accountID);
  252. }
  253. void LobbyDatabase::deleteGameRoomInvite(const std::string & targetAccountID, const std::string & roomID)
  254. {
  255. deleteGameRoomInvitesStatement->executeOnce(roomID, targetAccountID);
  256. }
  257. void LobbyDatabase::insertGameRoomInvite(const std::string & targetAccountID, const std::string & roomID)
  258. {
  259. insertGameRoomInvitesStatement->executeOnce(roomID, targetAccountID);
  260. }
  261. void LobbyDatabase::insertGameRoom(const std::string & roomID, const std::string & hostAccountID)
  262. {
  263. insertGameRoomStatement->executeOnce(roomID, hostAccountID);
  264. }
  265. void LobbyDatabase::insertAccount(const std::string & accountID, const std::string & displayName)
  266. {
  267. insertAccountStatement->executeOnce(accountID, displayName);
  268. }
  269. void LobbyDatabase::insertAccessCookie(const std::string & accountID, const std::string & accessCookieUUID)
  270. {
  271. insertAccessCookieStatement->executeOnce(accountID, accessCookieUUID);
  272. }
  273. void LobbyDatabase::updateAccessCookie(const std::string & accountID, const std::string & accessCookieUUID) {}
  274. void LobbyDatabase::updateAccountLoginTime(const std::string & accountID) {}
  275. void LobbyDatabase::updateActiveAccount(const std::string & accountID, bool isActive) {}
  276. std::string LobbyDatabase::getAccountDisplayName(const std::string & accountID)
  277. {
  278. std::string result;
  279. getAccountDisplayNameStatement->setBinds(accountID);
  280. if(getAccountDisplayNameStatement->execute())
  281. getAccountDisplayNameStatement->getColumns(result);
  282. getAccountDisplayNameStatement->reset();
  283. return result;
  284. }
  285. LobbyCookieStatus LobbyDatabase::getGameRoomCookieStatus(const std::string & accountID, const std::string & accessCookieUUID, std::chrono::seconds cookieLifetime)
  286. {
  287. return {};
  288. }
  289. LobbyCookieStatus LobbyDatabase::getAccountCookieStatus(const std::string & accountID, const std::string & accessCookieUUID, std::chrono::seconds cookieLifetime)
  290. {
  291. bool result = false;
  292. isAccountCookieValidStatement->setBinds(accountID, accessCookieUUID, cookieLifetime.count());
  293. if(isAccountCookieValidStatement->execute())
  294. isAccountCookieValidStatement->getColumns(result);
  295. isAccountCookieValidStatement->reset();
  296. return result ? LobbyCookieStatus::VALID : LobbyCookieStatus::INVALID;
  297. }
  298. LobbyInviteStatus LobbyDatabase::getAccountInviteStatus(const std::string & accountID, const std::string & roomID)
  299. {
  300. return {};
  301. }
  302. LobbyRoomState LobbyDatabase::getGameRoomStatus(const std::string & roomID)
  303. {
  304. return {};
  305. }
  306. uint32_t LobbyDatabase::getGameRoomFreeSlots(const std::string & roomID)
  307. {
  308. return 0;
  309. }
  310. bool LobbyDatabase::isAccountNameExists(const std::string & displayName)
  311. {
  312. bool result = false;
  313. isAccountNameExistsStatement->setBinds(displayName);
  314. if(isAccountNameExistsStatement->execute())
  315. isAccountNameExistsStatement->getColumns(result);
  316. isAccountNameExistsStatement->reset();
  317. return result;
  318. }
  319. bool LobbyDatabase::isAccountIDExists(const std::string & accountID)
  320. {
  321. bool result = false;
  322. isAccountIDExistsStatement->setBinds(accountID);
  323. if(isAccountIDExistsStatement->execute())
  324. isAccountIDExistsStatement->getColumns(result);
  325. isAccountIDExistsStatement->reset();
  326. return result;
  327. }
  328. std::vector<LobbyGameRoom> LobbyDatabase::getActiveGameRooms()
  329. {
  330. return {};
  331. }
  332. std::vector<LobbyAccount> LobbyDatabase::getActiveAccounts()
  333. {
  334. std::vector<LobbyAccount> result;
  335. while(getActiveAccountsStatement->execute())
  336. {
  337. LobbyAccount entry;
  338. getActiveAccountsStatement->getColumns(entry.accountID, entry.displayName);
  339. result.push_back(entry);
  340. }
  341. getActiveAccountsStatement->reset();
  342. return result;
  343. }
  344. std::string LobbyDatabase::getIdleGameRoom(const std::string & hostAccountID)
  345. {
  346. std::string result;
  347. if(getIdleGameRoomStatement->execute())
  348. getIdleGameRoomStatement->getColumns(result);
  349. getIdleGameRoomStatement->reset();
  350. return result;
  351. }
  352. std::string LobbyDatabase::getAccountGameRoom(const std::string & accountID)
  353. {
  354. std::string result;
  355. if(getAccountGameRoomStatement->execute())
  356. getAccountGameRoomStatement->getColumns(result);
  357. getAccountGameRoomStatement->reset();
  358. return result;
  359. }