LobbyDatabase.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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(?, ?, 0, 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 setAccountOnlineText = R"(
  103. UPDATE accounts
  104. SET online = ?
  105. WHERE accountID = ?
  106. )";
  107. static const std::string setGameRoomStatusText = R"(
  108. UPDATE gameRooms
  109. SET status = ?
  110. WHERE roomID = ?
  111. )";
  112. static const std::string setGameRoomPlayerLimitText = R"(
  113. UPDATE gameRooms
  114. SET playerLimit = ?
  115. WHERE roomID = ?
  116. )";
  117. // SELECT FROM
  118. static const std::string getRecentMessageHistoryText = R"(
  119. SELECT senderName, displayName, messageText, strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',cm.creationTime) AS secondsElapsed
  120. FROM chatMessages cm
  121. LEFT JOIN accounts on accountID = senderName
  122. WHERE secondsElapsed < 60*60*18
  123. ORDER BY cm.creationTime DESC
  124. LIMIT 100
  125. )";
  126. static const std::string getIdleGameRoomText = R"(
  127. SELECT roomID
  128. FROM gameRooms
  129. WHERE hostAccountID = ? AND status = 0
  130. LIMIT 1
  131. )";
  132. static const std::string getAccountGameRoomText = R"(
  133. SELECT grp.roomID
  134. FROM gameRoomPlayers grp
  135. LEFT JOIN gameRooms gr ON gr.roomID = grp.roomID
  136. WHERE accountID = ? AND status IN (1, 2)
  137. LIMIT 1
  138. )";
  139. static const std::string getActiveAccountsText = R"(
  140. SELECT accountID, displayName
  141. FROM accounts
  142. WHERE online = 1
  143. )";
  144. static const std::string getActiveGameRoomsText = R"(
  145. SELECT roomID, hostAccountID, displayName, status, 0, playerLimit
  146. FROM gameRooms
  147. LEFT JOIN accounts ON hostAccountID = accountID
  148. WHERE status = 1
  149. )";
  150. static const std::string getAccountDisplayNameText = R"(
  151. SELECT displayName
  152. FROM accounts
  153. WHERE accountID = ?
  154. )";
  155. static const std::string isAccountCookieValidText = R"(
  156. SELECT COUNT(accountID)
  157. FROM accountCookies
  158. WHERE accountID = ? AND cookieUUID = ? AND strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',creationTime) < ?
  159. )";
  160. static const std::string isGameRoomCookieValidText = R"(
  161. SELECT COUNT(roomID)
  162. FROM gameRooms
  163. LEFT JOIN accountCookies ON accountCookies.accountID = gameRooms.hostAccountID
  164. WHERE roomID = ? AND cookieUUID = ? AND strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',creationTime) < ?
  165. )";
  166. static const std::string isPlayerInGameRoomText = R"(
  167. SELECT COUNT(accountID)
  168. FROM gameRoomPlayers
  169. WHERE accountID = ? AND roomID = ?
  170. )";
  171. static const std::string isPlayerInAnyGameRoomText = R"(
  172. SELECT COUNT(accountID)
  173. FROM gameRoomPlayers
  174. WHERE accountID = ?
  175. )";
  176. static const std::string isAccountIDExistsText = R"(
  177. SELECT COUNT(accountID)
  178. FROM accounts
  179. WHERE accountID = ?
  180. )";
  181. static const std::string isAccountNameExistsText = R"(
  182. SELECT COUNT(displayName)
  183. FROM accounts
  184. WHERE displayName = ?
  185. )";
  186. insertChatMessageStatement = database->prepare(insertChatMessageText);
  187. insertAccountStatement = database->prepare(insertAccountText);
  188. insertAccessCookieStatement = database->prepare(insertAccessCookieText);
  189. insertGameRoomStatement = database->prepare(insertGameRoomText);
  190. insertGameRoomPlayersStatement = database->prepare(insertGameRoomPlayersText);
  191. insertGameRoomInvitesStatement = database->prepare(insertGameRoomInvitesText);
  192. deleteGameRoomPlayersStatement = database->prepare(deleteGameRoomPlayersText);
  193. deleteGameRoomInvitesStatement = database->prepare(deleteGameRoomInvitesText);
  194. setAccountOnlineStatement = database->prepare(setAccountOnlineText);
  195. setGameRoomStatusStatement = database->prepare(setGameRoomStatusText);
  196. setGameRoomPlayerLimitStatement = database->prepare(setGameRoomPlayerLimitText);
  197. getRecentMessageHistoryStatement = database->prepare(getRecentMessageHistoryText);
  198. getIdleGameRoomStatement = database->prepare(getIdleGameRoomText);
  199. getAccountGameRoomStatement = database->prepare(getAccountGameRoomText);
  200. getActiveAccountsStatement = database->prepare(getActiveAccountsText);
  201. getActiveGameRoomsStatement = database->prepare(getActiveGameRoomsText);
  202. getAccountDisplayNameStatement = database->prepare(getAccountDisplayNameText);
  203. isAccountCookieValidStatement = database->prepare(isAccountCookieValidText);
  204. isPlayerInGameRoomStatement = database->prepare(isPlayerInGameRoomText);
  205. isPlayerInAnyGameRoomStatement = database->prepare(isPlayerInAnyGameRoomText);
  206. isAccountIDExistsStatement = database->prepare(isAccountIDExistsText);
  207. isAccountNameExistsStatement = database->prepare(isAccountNameExistsText);
  208. }
  209. LobbyDatabase::~LobbyDatabase() = default;
  210. LobbyDatabase::LobbyDatabase(const boost::filesystem::path & databasePath)
  211. {
  212. database = SQLiteInstance::open(databasePath, true);
  213. createTables();
  214. prepareStatements();
  215. }
  216. void LobbyDatabase::insertChatMessage(const std::string & sender, const std::string & roomType, const std::string & roomName, const std::string & messageText)
  217. {
  218. insertChatMessageStatement->executeOnce(sender, messageText);
  219. }
  220. bool LobbyDatabase::isPlayerInGameRoom(const std::string & accountID)
  221. {
  222. bool result = false;
  223. isPlayerInAnyGameRoomStatement->setBinds(accountID);
  224. if(isPlayerInAnyGameRoomStatement->execute())
  225. isPlayerInAnyGameRoomStatement->getColumns(result);
  226. isPlayerInAnyGameRoomStatement->reset();
  227. return result;
  228. }
  229. bool LobbyDatabase::isPlayerInGameRoom(const std::string & accountID, const std::string & roomID)
  230. {
  231. bool result = false;
  232. isPlayerInGameRoomStatement->setBinds(accountID, roomID);
  233. if(isPlayerInGameRoomStatement->execute())
  234. isPlayerInGameRoomStatement->getColumns(result);
  235. isPlayerInGameRoomStatement->reset();
  236. return result;
  237. }
  238. std::vector<LobbyChatMessage> LobbyDatabase::getRecentMessageHistory()
  239. {
  240. std::vector<LobbyChatMessage> result;
  241. while(getRecentMessageHistoryStatement->execute())
  242. {
  243. LobbyChatMessage message;
  244. getRecentMessageHistoryStatement->getColumns(message.accountID, message.displayName, message.messageText, message.age);
  245. result.push_back(message);
  246. }
  247. getRecentMessageHistoryStatement->reset();
  248. return result;
  249. }
  250. void LobbyDatabase::setAccountOnline(const std::string & accountID, bool isOnline)
  251. {
  252. setAccountOnlineStatement->executeOnce(isOnline ? 1 : 0, accountID);
  253. }
  254. void LobbyDatabase::setGameRoomStatus(const std::string & roomID, LobbyRoomState roomStatus)
  255. {
  256. setGameRoomStatusStatement->executeOnce(vstd::to_underlying(roomStatus), roomID);
  257. }
  258. void LobbyDatabase::setGameRoomPlayerLimit(const std::string & roomID, uint32_t playerLimit)
  259. {
  260. setGameRoomPlayerLimitStatement->executeOnce(playerLimit, roomID);
  261. }
  262. void LobbyDatabase::insertPlayerIntoGameRoom(const std::string & accountID, const std::string & roomID)
  263. {
  264. insertGameRoomPlayersStatement->executeOnce(roomID, accountID);
  265. }
  266. void LobbyDatabase::deletePlayerFromGameRoom(const std::string & accountID, const std::string & roomID)
  267. {
  268. deleteGameRoomPlayersStatement->executeOnce(roomID, accountID);
  269. }
  270. void LobbyDatabase::deleteGameRoomInvite(const std::string & targetAccountID, const std::string & roomID)
  271. {
  272. deleteGameRoomInvitesStatement->executeOnce(roomID, targetAccountID);
  273. }
  274. void LobbyDatabase::insertGameRoomInvite(const std::string & targetAccountID, const std::string & roomID)
  275. {
  276. insertGameRoomInvitesStatement->executeOnce(roomID, targetAccountID);
  277. }
  278. void LobbyDatabase::insertGameRoom(const std::string & roomID, const std::string & hostAccountID)
  279. {
  280. insertGameRoomStatement->executeOnce(roomID, hostAccountID);
  281. }
  282. void LobbyDatabase::insertAccount(const std::string & accountID, const std::string & displayName)
  283. {
  284. insertAccountStatement->executeOnce(accountID, displayName);
  285. }
  286. void LobbyDatabase::insertAccessCookie(const std::string & accountID, const std::string & accessCookieUUID)
  287. {
  288. insertAccessCookieStatement->executeOnce(accountID, accessCookieUUID);
  289. }
  290. void LobbyDatabase::updateAccessCookie(const std::string & accountID, const std::string & accessCookieUUID) {}
  291. void LobbyDatabase::updateAccountLoginTime(const std::string & accountID) {}
  292. void LobbyDatabase::updateActiveAccount(const std::string & accountID, bool isActive) {}
  293. std::string LobbyDatabase::getAccountDisplayName(const std::string & accountID)
  294. {
  295. std::string result;
  296. getAccountDisplayNameStatement->setBinds(accountID);
  297. if(getAccountDisplayNameStatement->execute())
  298. getAccountDisplayNameStatement->getColumns(result);
  299. getAccountDisplayNameStatement->reset();
  300. return result;
  301. }
  302. LobbyCookieStatus LobbyDatabase::getGameRoomCookieStatus(const std::string & accountID, const std::string & accessCookieUUID, std::chrono::seconds cookieLifetime)
  303. {
  304. return {};
  305. }
  306. LobbyCookieStatus LobbyDatabase::getAccountCookieStatus(const std::string & accountID, const std::string & accessCookieUUID, std::chrono::seconds cookieLifetime)
  307. {
  308. bool result = false;
  309. isAccountCookieValidStatement->setBinds(accountID, accessCookieUUID, cookieLifetime.count());
  310. if(isAccountCookieValidStatement->execute())
  311. isAccountCookieValidStatement->getColumns(result);
  312. isAccountCookieValidStatement->reset();
  313. return result ? LobbyCookieStatus::VALID : LobbyCookieStatus::INVALID;
  314. }
  315. LobbyInviteStatus LobbyDatabase::getAccountInviteStatus(const std::string & accountID, const std::string & roomID)
  316. {
  317. return {};
  318. }
  319. LobbyRoomState LobbyDatabase::getGameRoomStatus(const std::string & roomID)
  320. {
  321. return {};
  322. }
  323. uint32_t LobbyDatabase::getGameRoomFreeSlots(const std::string & roomID)
  324. {
  325. return 0;
  326. }
  327. bool LobbyDatabase::isAccountNameExists(const std::string & displayName)
  328. {
  329. bool result = false;
  330. isAccountNameExistsStatement->setBinds(displayName);
  331. if(isAccountNameExistsStatement->execute())
  332. isAccountNameExistsStatement->getColumns(result);
  333. isAccountNameExistsStatement->reset();
  334. return result;
  335. }
  336. bool LobbyDatabase::isAccountIDExists(const std::string & accountID)
  337. {
  338. bool result = false;
  339. isAccountIDExistsStatement->setBinds(accountID);
  340. if(isAccountIDExistsStatement->execute())
  341. isAccountIDExistsStatement->getColumns(result);
  342. isAccountIDExistsStatement->reset();
  343. return result;
  344. }
  345. std::vector<LobbyGameRoom> LobbyDatabase::getActiveGameRooms()
  346. {
  347. std::vector<LobbyGameRoom> result;
  348. while(getActiveGameRoomsStatement->execute())
  349. {
  350. LobbyGameRoom entry;
  351. getActiveGameRoomsStatement->getColumns(entry.roomID, entry.hostAccountID, entry.hostAccountDisplayName, entry.roomStatus, entry.playersCount, entry.playersLimit);
  352. result.push_back(entry);
  353. }
  354. getActiveGameRoomsStatement->reset();
  355. return result;
  356. }
  357. std::vector<LobbyAccount> LobbyDatabase::getActiveAccounts()
  358. {
  359. std::vector<LobbyAccount> result;
  360. while(getActiveAccountsStatement->execute())
  361. {
  362. LobbyAccount entry;
  363. getActiveAccountsStatement->getColumns(entry.accountID, entry.displayName);
  364. result.push_back(entry);
  365. }
  366. getActiveAccountsStatement->reset();
  367. return result;
  368. }
  369. std::string LobbyDatabase::getIdleGameRoom(const std::string & hostAccountID)
  370. {
  371. std::string result;
  372. getIdleGameRoomStatement->setBinds(hostAccountID);
  373. if(getIdleGameRoomStatement->execute())
  374. getIdleGameRoomStatement->getColumns(result);
  375. getIdleGameRoomStatement->reset();
  376. return result;
  377. }
  378. std::string LobbyDatabase::getAccountGameRoom(const std::string & accountID)
  379. {
  380. std::string result;
  381. getAccountGameRoomStatement->setBinds(accountID);
  382. if(getAccountGameRoomStatement->execute())
  383. getAccountGameRoomStatement->getColumns(result);
  384. getAccountGameRoomStatement->reset();
  385. return result;
  386. }