Datenbank.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. #include "Datenbank.h"
  2. #include <iostream>
  3. // Inhalt der LSDatenbank Klasse aus Datenbank.h
  4. // Konstruktor
  5. MSDatenbank::MSDatenbank( InitDatei *zIni )
  6. {
  7. datenbank = new Datenbank( zIni->zWert( "DBBenutzer" )->getText(), zIni->zWert( "DBPasswort" )->getText(),
  8. zIni->zWert( "DBName" )->getText(), zIni->zWert( "DBIP" )->getText(),
  9. (unsigned short)TextZuInt( zIni->zWert( "DBPort" )->getText(), 10 ) );
  10. if( !datenbank->istOk() )
  11. {
  12. std::cout << "MS: Die Verbindung zur Datenbank konnte nicht hergestellt werden.\nDas Programm wird beendet.";
  13. exit( 1 );
  14. }
  15. InitializeCriticalSection( &cs );
  16. ref = 1;
  17. Text befehl = "SELECT port, admin_port FROM server WHERE id = ";
  18. befehl += zIni->zWert( "ServerId" )->getText();
  19. lock();
  20. datenbank->befehl( befehl );
  21. Result res = datenbank->getResult();
  22. unlock();
  23. if( res.zeilenAnzahl == 1 )
  24. {
  25. zIni->addWert( "ServerPort", res.values[ 0 ] );
  26. zIni->addWert( "AdminServerPort", res.values[ 1 ] );
  27. }
  28. res.destroy();
  29. }
  30. // Destruktor
  31. MSDatenbank::~MSDatenbank()
  32. {
  33. datenbank->release();
  34. DeleteCriticalSection( &cs );
  35. }
  36. // nicht constant
  37. void MSDatenbank::lock()
  38. {
  39. EnterCriticalSection( &cs );
  40. }
  41. void MSDatenbank::unlock()
  42. {
  43. LeaveCriticalSection( &cs );
  44. }
  45. int MSDatenbank::istAdministrator( const char *name, const char *passwort )
  46. {
  47. Text *befehl = new Text( "SELECT id FROM benutzer WHERE name = '" );
  48. Text n( name );
  49. n.ersetzen( "'", "''" );
  50. befehl->append( (char*)n );
  51. befehl->append( "' AND passwort = md5( '" );
  52. Text p( passwort );
  53. p.ersetzen( "'", "''" );
  54. befehl->append( (char*)p );
  55. befehl->append( "' )" );
  56. lock();
  57. datenbank->befehl( befehl->getText() );
  58. Result res = datenbank->getResult();
  59. unlock();
  60. befehl->release();
  61. int ret = 0;
  62. if( res.zeilenAnzahl > 0 )
  63. ret = TextZuInt( res.values[ 0 ].getText(), 10 );
  64. res.destroy();
  65. return ret;
  66. }
  67. bool MSDatenbank::adminHatRecht( int id, int recht )
  68. {
  69. Text *befehl = new Text( "SELECT * FROM benutzer_rechte WHERE benutzer_id = " );
  70. befehl->append( id );
  71. befehl->append( " AND rechte_id = " );
  72. befehl->append( recht );
  73. lock();
  74. datenbank->befehl( befehl->getText() );
  75. int ret = datenbank->getZeilenAnzahl();
  76. unlock();
  77. befehl->release();
  78. return ret != 0;
  79. }
  80. bool MSDatenbank::proveKlient( int num, int sNum )
  81. {
  82. Text *befehl = new Text( "SELECT * FROM server_client WHERE server_id = " );
  83. befehl->append( sNum );
  84. befehl->append( " AND client_id = " );
  85. befehl->append( num );
  86. lock();
  87. datenbank->befehl( befehl->getText() );
  88. Result res = datenbank->getResult();
  89. unlock();
  90. befehl->release();
  91. bool ret = 0;
  92. if( res.zeilenAnzahl == 1 )
  93. ret = 1;
  94. res.destroy();
  95. return ret;
  96. }
  97. Text *MSDatenbank::getKlientKey( int cId )
  98. {
  99. lock();
  100. if( !datenbank->befehl( Text( "SELECT schluessel FROM client WHERE id = " ) += cId ) )
  101. {
  102. unlock();
  103. return 0;
  104. }
  105. Result res = datenbank->getResult();
  106. unlock();
  107. if( !res.zeilenAnzahl )
  108. {
  109. res.destroy();
  110. return 0;
  111. }
  112. Text *ret = new Text( res.values[ 0 ].getText() );
  113. res.destroy();
  114. return ret;
  115. }
  116. void MSDatenbank::unregisterKlient( int num, int sNum )
  117. {
  118. Text *befehl = new Text( "DELETE FROM server_client WHERE client_id = " );
  119. befehl->append( num );
  120. befehl->append( " AND server_id = " );
  121. befehl->append( sNum );
  122. lock();
  123. datenbank->befehl( befehl->getText() );
  124. int za = datenbank->getZeilenAnzahl();
  125. unlock();
  126. if( za == 1 )
  127. {
  128. befehl->setText( "UPDATE server SET tasks = tasks - 1 WHERE id = " );
  129. befehl->append( sNum );
  130. lock();
  131. datenbank->befehl( befehl->getText() );
  132. unlock();
  133. }
  134. befehl->release();
  135. }
  136. bool MSDatenbank::setServerStatus( int id, int status )
  137. {
  138. Text *befehl = new Text( "UPDATE server SET server_status_id = " );
  139. *befehl += status;
  140. *befehl += "WHERE id = ";
  141. *befehl += id;
  142. lock();
  143. if( !datenbank->befehl( befehl->getText() ) )
  144. {
  145. unlock();
  146. befehl->release();
  147. return 0;
  148. }
  149. bool ret = datenbank->getZeilenAnzahl() != 0;
  150. unlock();
  151. befehl->release();
  152. return ret;
  153. }
  154. bool MSDatenbank::setMaxClients( int id, int maxC )
  155. {
  156. Text *befehl = new Text( "UPDATE server SET max_tasks = " );
  157. befehl->append( maxC );
  158. befehl->append( " WHERE id = " );
  159. befehl->append( id );
  160. lock();
  161. if( !datenbank->befehl( befehl->getText() ) )
  162. {
  163. unlock();
  164. befehl->release();
  165. return 0;
  166. }
  167. bool ret = datenbank->getZeilenAnzahl() > 0;
  168. unlock();
  169. befehl->release();
  170. return ret;
  171. }
  172. bool MSDatenbank::serverIstNichtPausiert( int id )
  173. {
  174. Text *befehl = new Text( "SELECT server_status_id FROM server WHERE id = " );
  175. befehl->append( id );
  176. lock();
  177. if( !datenbank->befehl( befehl->getText() ) )
  178. {
  179. unlock();
  180. befehl->release();
  181. return 0;
  182. }
  183. Result res = datenbank->getResult();
  184. unlock();
  185. befehl->release();
  186. if( !res.zeilenAnzahl )
  187. {
  188. res.destroy();
  189. return 0;
  190. }
  191. bool ret = (int)res.values[ 0 ] == 3;
  192. res.destroy();
  193. return ret;
  194. }
  195. int MSDatenbank::getMinigameOptionList( char *minigame, RCArray< Text > *zOptionList )
  196. {
  197. Text befehl = "SELECT a.options FROM minigame_option a, minigame b WHERE a.minigame_id = b.id AND b.name = '";
  198. Text m( minigame );
  199. m.ersetzen( "'", "''" );
  200. befehl += m;
  201. befehl += "' ORDER BY a.options";
  202. lock();
  203. if( !datenbank->befehl( befehl ) )
  204. {
  205. unlock();
  206. return 0;
  207. }
  208. Result res = datenbank->getResult();
  209. unlock();
  210. int anz = res.zeilenAnzahl;
  211. for( int i = 0; i < anz; i++ )
  212. zOptionList->add( new Text( (char*)res.values[ i ] ) );
  213. res.destroy();
  214. return anz;
  215. }
  216. int MSDatenbank::getMinigameBestscore( char *options, char *minigame, Text *zPlayer )
  217. {
  218. Text o( options );
  219. o.ersetzen( "'", "''" );
  220. Text m( minigame );
  221. m.ersetzen( "'", "''" );
  222. Text befehl = "SELECT b.score, a.ruf_name FROM account a, minigame_bestscore b, minigame_option o, minigame m "
  223. "WHERE m.id = o.minigame_id AND o.id = b.minigame_options_id AND a.id = b.account_id AND m.name = '";
  224. befehl += o;
  225. befehl += "' AND o.options = '";
  226. befehl += m;
  227. befehl += "'";
  228. lock();
  229. if( !datenbank->befehl( befehl ) )
  230. {
  231. unlock();
  232. return 0;
  233. }
  234. Result res = datenbank->getResult();
  235. unlock();
  236. if( !res.zeilenAnzahl )
  237. {
  238. res.destroy();
  239. return 0;
  240. }
  241. int ret = (int)res.values[ 0 ];
  242. zPlayer->setText( res.values[ 1 ] );
  243. res.destroy();
  244. return ret;
  245. }
  246. int MSDatenbank::getMinigameBestscore( char *minigame, Array< int > *zScoreList, RCArray< Text > *zPlayerList, RCArray< Text > *zOptionList )
  247. {
  248. Text m( minigame );
  249. m.ersetzen( "'", "''" );
  250. Text befehl = "SELECT b.score, a.ruf_name, o.options FROM account a, minigame_bestscore b, minigame_option o, minigame m "
  251. "WHERE m.id = o.minigame_id AND o.id = b.minigame_options_id AND a.id = b.account_id AND m.name = '";
  252. befehl += m;
  253. befehl += "' ORDER BY o.options, b.score";
  254. lock();
  255. if( !datenbank->befehl( befehl ) )
  256. {
  257. unlock();
  258. return 0;
  259. }
  260. Result res = datenbank->getResult();
  261. unlock();
  262. if( !res.zeilenAnzahl )
  263. {
  264. res.destroy();
  265. return 0;
  266. }
  267. int anz = res.zeilenAnzahl;
  268. for( int i = 0; i < anz; i++ )
  269. {
  270. zScoreList->add( (int)res.values[ i * 3 ] );
  271. zPlayerList->add( new Text( res.values[ i * 3 + 1 ] ) );
  272. zOptionList->add( new Text( res.values[ i * 3 + 2 ] ) );
  273. }
  274. res.destroy();
  275. return anz;
  276. }
  277. bool MSDatenbank::updateMinigameScore( int score, char *option, int cId, char *minigame, int sId, Text *zFileName )
  278. {
  279. Text o( option );
  280. o.ersetzen( "'", "''" );
  281. Text m( minigame );
  282. m.ersetzen( "'", "''" );
  283. Text befehl = "SELECT a.id FROM minigame_option a, minigame b WHERE a.minigame_id = b.id AND b.name = '";
  284. befehl += m;
  285. befehl += "' AND a.options = '";
  286. befehl += o;
  287. befehl += "'";
  288. lock();
  289. if( !datenbank->befehl( befehl ) )
  290. {
  291. unlock();
  292. return 0;
  293. }
  294. Result res = datenbank->getResult();
  295. unlock();
  296. if( !res.zeilenAnzahl )
  297. {
  298. res.destroy();
  299. return 0;
  300. }
  301. int optionId = res.values[ 0 ];
  302. *zFileName = optionId;
  303. *zFileName += ".mgc";
  304. res.destroy();
  305. befehl = "SELECT score FROM minigame_bestscore WHERE minigame_options_id = ";
  306. befehl += optionId;
  307. lock();
  308. if( !datenbank->befehl( befehl ) )
  309. {
  310. unlock();
  311. return 0;
  312. }
  313. res = datenbank->getResult();
  314. unlock();
  315. int anz = res.zeilenAnzahl;
  316. bool ret = !anz || (int)res.values[ 0 ] < score;
  317. res.destroy();
  318. if( ret )
  319. {
  320. if( anz )
  321. {
  322. befehl = "UPDATE minigame_bestscore b SET score = ";
  323. befehl += score;
  324. befehl += ", account_id = a.account_id, minigame_server_id = ";
  325. befehl += sId;
  326. befehl += " FROM account_client a WHERE a.client_id = ";
  327. befehl += cId;
  328. befehl += " AND b.minigame_options_id = ";
  329. befehl += optionId;
  330. }
  331. else
  332. {
  333. befehl = "INSERT INTO minigame_bestscore( account_id, score, minigame_options_id, minigame_server_id ) SELECT a.account_id AS account_id, ";
  334. befehl += score;
  335. befehl += " AS score, ";
  336. befehl += optionId;
  337. befehl += " AS minigame_options_id, ";
  338. befehl += sId;
  339. befehl += " AS minigame_server_id FROM account_client a WHERE a.client_id = ";
  340. befehl += cId;
  341. }
  342. lock();
  343. if( !datenbank->befehl( befehl ) )
  344. {
  345. unlock();
  346. return 0;
  347. }
  348. unlock();
  349. }
  350. return ret;
  351. }
  352. Text *MSDatenbank::getMinigameCaptureFileName( char *option, char *minigame )
  353. {
  354. Text o( option );
  355. o.ersetzen( "'", "''" );
  356. Text m( minigame );
  357. m.ersetzen( "'", "''" );
  358. Text befehl = "SELECT a.id FROM minigame_option a, minigame b WHERE a.minigame_id = b.id AND b.name = '";
  359. befehl += m;
  360. befehl += "' AND a.options = '";
  361. befehl += o;
  362. befehl += "'";
  363. lock();
  364. if( !datenbank->befehl( befehl ) )
  365. {
  366. unlock();
  367. return 0;
  368. }
  369. Result res = datenbank->getResult();
  370. unlock();
  371. if( !res.zeilenAnzahl )
  372. {
  373. res.destroy();
  374. return 0;
  375. }
  376. int optionId = res.values[ 0 ];
  377. Text *fileName = new Text();
  378. fileName->append( optionId );
  379. fileName->append( ".mgc" );
  380. res.destroy();
  381. return fileName;
  382. }
  383. int MSDatenbank::getMinigameServer( char *minigame, char *option )
  384. {
  385. Text o( option );
  386. o.ersetzen( "'", "''" );
  387. Text m( minigame );
  388. m.ersetzen( "'", "''" );
  389. Text befehl = "SELECT a.id FROM server a, minigame_bestscore b, minigame_option c, minigame d WHERE a.id = b.minigame_server_id AND d.id = c.minigame_id AND b.minigame_options_id = c.id AND c.options = '";
  390. befehl += o;
  391. befehl += "' AND d.name = '";
  392. befehl += m;
  393. befehl += "'";
  394. lock();
  395. if( !datenbank->befehl( befehl ) )
  396. {
  397. unlock();
  398. return 0;
  399. }
  400. Result res = datenbank->getResult();
  401. unlock();
  402. int ret = 0;
  403. if( res.zeilenAnzahl )
  404. ret = res.values[ 0 ];
  405. res.destroy();
  406. return ret;
  407. }
  408. bool MSDatenbank::getMinigameServer( char *minigame, char *option, Text *zIp, Text *zPort )
  409. {
  410. Text o( option );
  411. o.ersetzen( "'", "''" );
  412. Text m( minigame );
  413. m.ersetzen( "'", "''" );
  414. Text befehl = "SELECT a.ip, a.port FROM server a, minigame_bestscore b, minigame_option c, minigame d WHERE a.id = b.minigame_server_id AND d.id = c.minigame_id AND b.minigame_options_id = c.id AND c.options = '";
  415. befehl += o;
  416. befehl += "' AND d.name = '";
  417. befehl += m;
  418. befehl += "'";
  419. lock();
  420. if( !datenbank->befehl( befehl ) )
  421. {
  422. unlock();
  423. return 0;
  424. }
  425. Result res = datenbank->getResult();
  426. unlock();
  427. if( res.zeilenAnzahl )
  428. {
  429. zIp->setText( (char*)res.values[ 0 ] );
  430. zPort->setText( (char*)res.values[ 1 ] );
  431. res.destroy();
  432. return 1;
  433. }
  434. res.destroy();
  435. return 0;
  436. }
  437. // constant
  438. Text *MSDatenbank::getLetzterFehler() const
  439. {
  440. return datenbank->getLetzterFehler();
  441. }
  442. // Reference Counting
  443. MSDatenbank *MSDatenbank::getThis()
  444. {
  445. ref++;
  446. return this;
  447. }
  448. MSDatenbank *MSDatenbank::release()
  449. {
  450. ref--;
  451. if( !ref )
  452. delete this;
  453. return 0;
  454. }