LobbyDatabase.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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::clearOldData()
  74. {
  75. static const std::string removeActiveAccounts = R"(
  76. UPDATE accounts
  77. SET online = 0
  78. WHERE online <> 0
  79. )";
  80. static const std::string removeActiveRooms = R"(
  81. UPDATE gameRooms
  82. SET status = 5
  83. WHERE status <> 5
  84. )";
  85. database->prepare(removeActiveAccounts)->execute();
  86. database->prepare(removeActiveRooms)->execute();
  87. }
  88. void LobbyDatabase::prepareStatements()
  89. {
  90. // INSERT INTO
  91. static const std::string insertChatMessageText = R"(
  92. INSERT INTO chatMessages(senderName, messageText) VALUES( ?, ?);
  93. )";
  94. static const std::string insertAccountText = R"(
  95. INSERT INTO accounts(accountID, displayName, online) VALUES(?,?,0);
  96. )";
  97. static const std::string insertAccessCookieText = R"(
  98. INSERT INTO accountCookies(accountID, cookieUUID) VALUES(?,?);
  99. )";
  100. static const std::string insertGameRoomText = R"(
  101. INSERT INTO gameRooms(roomID, hostAccountID, status, playerLimit) VALUES(?, ?, 0, 8);
  102. )";
  103. static const std::string insertGameRoomPlayersText = R"(
  104. INSERT INTO gameRoomPlayers(roomID, accountID) VALUES(?,?);
  105. )";
  106. static const std::string insertGameRoomInvitesText = R"(
  107. INSERT INTO gameRoomInvites(roomID, accountID) VALUES(?,?);
  108. )";
  109. // DELETE FROM
  110. static const std::string deleteGameRoomPlayersText = R"(
  111. DELETE FROM gameRoomPlayers WHERE roomID = ? AND accountID = ?
  112. )";
  113. static const std::string deleteGameRoomInvitesText = R"(
  114. DELETE FROM gameRoomInvites WHERE roomID = ? AND accountID = ?
  115. )";
  116. // UPDATE
  117. static const std::string setAccountOnlineText = R"(
  118. UPDATE accounts
  119. SET online = ?
  120. WHERE accountID = ?
  121. )";
  122. static const std::string setGameRoomStatusText = R"(
  123. UPDATE gameRooms
  124. SET status = ?
  125. WHERE roomID = ?
  126. )";
  127. static const std::string updateAccountLoginTimeText = R"(
  128. UPDATE accounts
  129. SET lastLoginTime = CURRENT_TIMESTAMP
  130. WHERE accountID = ?
  131. )";
  132. // SELECT FROM
  133. static const std::string getRecentMessageHistoryText = R"(
  134. SELECT senderName, displayName, messageText, strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',cm.creationTime) AS secondsElapsed
  135. FROM chatMessages cm
  136. LEFT JOIN accounts on accountID = senderName
  137. WHERE secondsElapsed < 60*60*18
  138. ORDER BY cm.creationTime DESC
  139. LIMIT 100
  140. )";
  141. static const std::string getIdleGameRoomText = R"(
  142. SELECT roomID
  143. FROM gameRooms
  144. WHERE hostAccountID = ? AND status = 0
  145. LIMIT 1
  146. )";
  147. static const std::string getGameRoomStatusText = R"(
  148. SELECT status
  149. FROM gameRooms
  150. WHERE roomID = ?
  151. )";
  152. static const std::string getAccountGameRoomText = R"(
  153. SELECT grp.roomID
  154. FROM gameRoomPlayers grp
  155. LEFT JOIN gameRooms gr ON gr.roomID = grp.roomID
  156. WHERE accountID = ? AND status IN (1, 2)
  157. LIMIT 1
  158. )";
  159. static const std::string getActiveAccountsText = R"(
  160. SELECT accountID, displayName
  161. FROM accounts
  162. WHERE online = 1
  163. )";
  164. static const std::string getActiveGameRoomsText = R"(
  165. SELECT roomID, hostAccountID, displayName, status, playerLimit
  166. FROM gameRooms
  167. LEFT JOIN accounts ON hostAccountID = accountID
  168. WHERE status = 1
  169. )";
  170. static const std::string countRoomUsedSlotsText = R"(
  171. SELECT COUNT(accountID)
  172. FROM gameRoomPlayers
  173. WHERE roomID = ?
  174. )";
  175. static const std::string countRoomTotalSlotsText = R"(
  176. SELECT playerLimit
  177. FROM gameRooms
  178. WHERE roomID = ?
  179. )";
  180. static const std::string getAccountDisplayNameText = R"(
  181. SELECT displayName
  182. FROM accounts
  183. WHERE accountID = ?
  184. )";
  185. static const std::string isAccountCookieValidText = R"(
  186. SELECT COUNT(accountID)
  187. FROM accountCookies
  188. WHERE accountID = ? AND cookieUUID = ?
  189. )";
  190. static const std::string isGameRoomCookieValidText = R"(
  191. SELECT COUNT(roomID)
  192. FROM gameRooms
  193. LEFT JOIN accountCookies ON accountCookies.accountID = gameRooms.hostAccountID
  194. WHERE roomID = ? AND cookieUUID = ? AND strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',creationTime) < ?
  195. )";
  196. static const std::string isPlayerInGameRoomText = R"(
  197. SELECT COUNT(accountID)
  198. FROM gameRoomPlayers grp
  199. LEFT JOIN gameRooms gr ON gr.roomID = grp.roomID
  200. WHERE accountID = ? AND grp.roomID = ? AND status IN (1, 2)
  201. )";
  202. static const std::string isPlayerInAnyGameRoomText = R"(
  203. SELECT COUNT(accountID)
  204. FROM gameRoomPlayers grp
  205. LEFT JOIN gameRooms gr ON gr.roomID = grp.roomID
  206. WHERE accountID = ? AND status IN (1, 2)
  207. )";
  208. static const std::string isAccountIDExistsText = R"(
  209. SELECT COUNT(accountID)
  210. FROM accounts
  211. WHERE accountID = ?
  212. )";
  213. static const std::string isAccountNameExistsText = R"(
  214. SELECT COUNT(displayName)
  215. FROM accounts
  216. WHERE displayName = ?
  217. )";
  218. insertChatMessageStatement = database->prepare(insertChatMessageText);
  219. insertAccountStatement = database->prepare(insertAccountText);
  220. insertAccessCookieStatement = database->prepare(insertAccessCookieText);
  221. insertGameRoomStatement = database->prepare(insertGameRoomText);
  222. insertGameRoomPlayersStatement = database->prepare(insertGameRoomPlayersText);
  223. insertGameRoomInvitesStatement = database->prepare(insertGameRoomInvitesText);
  224. deleteGameRoomPlayersStatement = database->prepare(deleteGameRoomPlayersText);
  225. deleteGameRoomInvitesStatement = database->prepare(deleteGameRoomInvitesText);
  226. setAccountOnlineStatement = database->prepare(setAccountOnlineText);
  227. setGameRoomStatusStatement = database->prepare(setGameRoomStatusText);
  228. updateAccountLoginTimeStatement = database->prepare(updateAccountLoginTimeText);
  229. getRecentMessageHistoryStatement = database->prepare(getRecentMessageHistoryText);
  230. getIdleGameRoomStatement = database->prepare(getIdleGameRoomText);
  231. getGameRoomStatusStatement = database->prepare(getGameRoomStatusText);
  232. getAccountGameRoomStatement = database->prepare(getAccountGameRoomText);
  233. getActiveAccountsStatement = database->prepare(getActiveAccountsText);
  234. getActiveGameRoomsStatement = database->prepare(getActiveGameRoomsText);
  235. getAccountDisplayNameStatement = database->prepare(getAccountDisplayNameText);
  236. countRoomUsedSlotsStatement = database->prepare(countRoomUsedSlotsText);
  237. countRoomTotalSlotsStatement = database->prepare(countRoomTotalSlotsText);
  238. isAccountCookieValidStatement = database->prepare(isAccountCookieValidText);
  239. isPlayerInGameRoomStatement = database->prepare(isPlayerInGameRoomText);
  240. isPlayerInAnyGameRoomStatement = database->prepare(isPlayerInAnyGameRoomText);
  241. isAccountIDExistsStatement = database->prepare(isAccountIDExistsText);
  242. isAccountNameExistsStatement = database->prepare(isAccountNameExistsText);
  243. }
  244. LobbyDatabase::~LobbyDatabase() = default;
  245. LobbyDatabase::LobbyDatabase(const boost::filesystem::path & databasePath)
  246. {
  247. database = SQLiteInstance::open(databasePath, true);
  248. createTables();
  249. clearOldData();
  250. prepareStatements();
  251. }
  252. void LobbyDatabase::insertChatMessage(const std::string & sender, const std::string & roomType, const std::string & roomName, const std::string & messageText)
  253. {
  254. insertChatMessageStatement->executeOnce(sender, messageText);
  255. }
  256. bool LobbyDatabase::isPlayerInGameRoom(const std::string & accountID)
  257. {
  258. bool result = false;
  259. isPlayerInAnyGameRoomStatement->setBinds(accountID);
  260. if(isPlayerInAnyGameRoomStatement->execute())
  261. isPlayerInAnyGameRoomStatement->getColumns(result);
  262. isPlayerInAnyGameRoomStatement->reset();
  263. return result;
  264. }
  265. bool LobbyDatabase::isPlayerInGameRoom(const std::string & accountID, const std::string & roomID)
  266. {
  267. bool result = false;
  268. isPlayerInGameRoomStatement->setBinds(accountID, roomID);
  269. if(isPlayerInGameRoomStatement->execute())
  270. isPlayerInGameRoomStatement->getColumns(result);
  271. isPlayerInGameRoomStatement->reset();
  272. return result;
  273. }
  274. std::vector<LobbyChatMessage> LobbyDatabase::getRecentMessageHistory()
  275. {
  276. std::vector<LobbyChatMessage> result;
  277. while(getRecentMessageHistoryStatement->execute())
  278. {
  279. LobbyChatMessage message;
  280. getRecentMessageHistoryStatement->getColumns(message.accountID, message.displayName, message.messageText, message.age);
  281. result.push_back(message);
  282. }
  283. getRecentMessageHistoryStatement->reset();
  284. return result;
  285. }
  286. void LobbyDatabase::setAccountOnline(const std::string & accountID, bool isOnline)
  287. {
  288. setAccountOnlineStatement->executeOnce(isOnline ? 1 : 0, accountID);
  289. }
  290. void LobbyDatabase::setGameRoomStatus(const std::string & roomID, LobbyRoomState roomStatus)
  291. {
  292. setGameRoomStatusStatement->executeOnce(vstd::to_underlying(roomStatus), roomID);
  293. }
  294. void LobbyDatabase::insertPlayerIntoGameRoom(const std::string & accountID, const std::string & roomID)
  295. {
  296. insertGameRoomPlayersStatement->executeOnce(roomID, accountID);
  297. }
  298. void LobbyDatabase::deletePlayerFromGameRoom(const std::string & accountID, const std::string & roomID)
  299. {
  300. deleteGameRoomPlayersStatement->executeOnce(roomID, accountID);
  301. }
  302. void LobbyDatabase::deleteGameRoomInvite(const std::string & targetAccountID, const std::string & roomID)
  303. {
  304. deleteGameRoomInvitesStatement->executeOnce(roomID, targetAccountID);
  305. }
  306. void LobbyDatabase::insertGameRoomInvite(const std::string & targetAccountID, const std::string & roomID)
  307. {
  308. insertGameRoomInvitesStatement->executeOnce(roomID, targetAccountID);
  309. }
  310. void LobbyDatabase::insertGameRoom(const std::string & roomID, const std::string & hostAccountID)
  311. {
  312. insertGameRoomStatement->executeOnce(roomID, hostAccountID);
  313. }
  314. void LobbyDatabase::insertAccount(const std::string & accountID, const std::string & displayName)
  315. {
  316. insertAccountStatement->executeOnce(accountID, displayName);
  317. }
  318. void LobbyDatabase::insertAccessCookie(const std::string & accountID, const std::string & accessCookieUUID)
  319. {
  320. insertAccessCookieStatement->executeOnce(accountID, accessCookieUUID);
  321. }
  322. void LobbyDatabase::updateAccountLoginTime(const std::string & accountID)
  323. {
  324. updateAccountLoginTimeStatement->executeOnce(accountID);
  325. }
  326. std::string LobbyDatabase::getAccountDisplayName(const std::string & accountID)
  327. {
  328. std::string result;
  329. getAccountDisplayNameStatement->setBinds(accountID);
  330. if(getAccountDisplayNameStatement->execute())
  331. getAccountDisplayNameStatement->getColumns(result);
  332. getAccountDisplayNameStatement->reset();
  333. return result;
  334. }
  335. LobbyCookieStatus LobbyDatabase::getAccountCookieStatus(const std::string & accountID, const std::string & accessCookieUUID)
  336. {
  337. bool result = false;
  338. isAccountCookieValidStatement->setBinds(accountID, accessCookieUUID);
  339. if(isAccountCookieValidStatement->execute())
  340. isAccountCookieValidStatement->getColumns(result);
  341. isAccountCookieValidStatement->reset();
  342. return result ? LobbyCookieStatus::VALID : LobbyCookieStatus::INVALID;
  343. }
  344. LobbyInviteStatus LobbyDatabase::getAccountInviteStatus(const std::string & accountID, const std::string & roomID)
  345. {
  346. assert(0);
  347. return {};
  348. }
  349. LobbyRoomState LobbyDatabase::getGameRoomStatus(const std::string & roomID)
  350. {
  351. int result = -1;
  352. getGameRoomStatusStatement->setBinds(roomID);
  353. if(getGameRoomStatusStatement->execute())
  354. getGameRoomStatusStatement->getColumns(result);
  355. getGameRoomStatusStatement->reset();
  356. if (result != -1)
  357. return static_cast<LobbyRoomState>(result);
  358. return LobbyRoomState::CLOSED;
  359. }
  360. uint32_t LobbyDatabase::getGameRoomFreeSlots(const std::string & roomID)
  361. {
  362. uint32_t usedSlots = 0;
  363. uint32_t totalSlots = 0;
  364. countRoomUsedSlotsStatement->setBinds(roomID);
  365. if(countRoomUsedSlotsStatement->execute())
  366. countRoomUsedSlotsStatement->getColumns(usedSlots);
  367. countRoomUsedSlotsStatement->reset();
  368. countRoomTotalSlotsStatement->setBinds(roomID);
  369. if(countRoomTotalSlotsStatement->execute())
  370. countRoomTotalSlotsStatement->getColumns(totalSlots);
  371. countRoomTotalSlotsStatement->reset();
  372. if (totalSlots > usedSlots)
  373. return totalSlots - usedSlots;
  374. return 0;
  375. }
  376. bool LobbyDatabase::isAccountNameExists(const std::string & displayName)
  377. {
  378. bool result = false;
  379. isAccountNameExistsStatement->setBinds(displayName);
  380. if(isAccountNameExistsStatement->execute())
  381. isAccountNameExistsStatement->getColumns(result);
  382. isAccountNameExistsStatement->reset();
  383. return result;
  384. }
  385. bool LobbyDatabase::isAccountIDExists(const std::string & accountID)
  386. {
  387. bool result = false;
  388. isAccountIDExistsStatement->setBinds(accountID);
  389. if(isAccountIDExistsStatement->execute())
  390. isAccountIDExistsStatement->getColumns(result);
  391. isAccountIDExistsStatement->reset();
  392. return result;
  393. }
  394. std::vector<LobbyGameRoom> LobbyDatabase::getActiveGameRooms()
  395. {
  396. std::vector<LobbyGameRoom> result;
  397. while(getActiveGameRoomsStatement->execute())
  398. {
  399. LobbyGameRoom entry;
  400. getActiveGameRoomsStatement->getColumns(entry.roomID, entry.hostAccountID, entry.hostAccountDisplayName, entry.roomStatus, entry.playersLimit);
  401. result.push_back(entry);
  402. }
  403. getActiveGameRoomsStatement->reset();
  404. for (auto & room : result)
  405. {
  406. countRoomUsedSlotsStatement->setBinds(room.roomID);
  407. if(countRoomUsedSlotsStatement->execute())
  408. countRoomUsedSlotsStatement->getColumns(room.playersCount);
  409. countRoomUsedSlotsStatement->reset();
  410. }
  411. return result;
  412. }
  413. std::vector<LobbyAccount> LobbyDatabase::getActiveAccounts()
  414. {
  415. std::vector<LobbyAccount> result;
  416. while(getActiveAccountsStatement->execute())
  417. {
  418. LobbyAccount entry;
  419. getActiveAccountsStatement->getColumns(entry.accountID, entry.displayName);
  420. result.push_back(entry);
  421. }
  422. getActiveAccountsStatement->reset();
  423. return result;
  424. }
  425. std::string LobbyDatabase::getIdleGameRoom(const std::string & hostAccountID)
  426. {
  427. std::string result;
  428. getIdleGameRoomStatement->setBinds(hostAccountID);
  429. if(getIdleGameRoomStatement->execute())
  430. getIdleGameRoomStatement->getColumns(result);
  431. getIdleGameRoomStatement->reset();
  432. return result;
  433. }
  434. std::string LobbyDatabase::getAccountGameRoom(const std::string & accountID)
  435. {
  436. std::string result;
  437. getAccountGameRoomStatement->setBinds(accountID);
  438. if(getAccountGameRoomStatement->execute())
  439. getAccountGameRoomStatement->getColumns(result);
  440. getAccountGameRoomStatement->reset();
  441. return result;
  442. }