Datenbank.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. #include "Datenbank.h"
  2. #include <iostream>
  3. #include <Datei.h>
  4. #ifndef WIN32
  5. #include <spawn.h>
  6. #endif
  7. // Inhalt der AppSDatenbank Klasse aus Datenbank.h
  8. // Konstruktor
  9. AppSDatenbank::AppSDatenbank( InitDatei *zDat )
  10. {
  11. datenbank = new Datenbank( zDat->zWert( "DBBenutzer" )->getText(), zDat->zWert( "DBPasswort" )->getText(),
  12. zDat->zWert( "DBName" )->getText(), zDat->zWert( "DBIP" )->getText(),
  13. (unsigned short)TextZuInt( zDat->zWert( "DBPort" )->getText(), 10 ) );
  14. if( !datenbank->istOk() )
  15. {
  16. std::cout << "AppS: Es konnte keine Verbindung zur Datenbank hergestellt werden:\n";
  17. Text *txt = datenbank->getLetzterFehler();
  18. std::cout << txt->getText() << "\nDas Programm wird beendet.";
  19. txt->release();
  20. exit( 1 );
  21. }
  22. InitializeCriticalSection( &ths );
  23. ref = 1;
  24. Text befehl = "SELECT port, admin_port FROM server WHERE id = ";
  25. befehl += zDat->zWert( "ServerId" )->getText();
  26. lock();
  27. datenbank->befehl( befehl );
  28. Result res = datenbank->getResult();
  29. unlock();
  30. if( res.zeilenAnzahl == 1 )
  31. {
  32. zDat->addWert( "AppSPort", res.values[ 0 ] );
  33. zDat->addWert( "AppSAPort", res.values[ 1 ] );
  34. }
  35. res.destroy();
  36. }
  37. // Detruktor
  38. AppSDatenbank::~AppSDatenbank()
  39. {
  40. datenbank->release();
  41. DeleteCriticalSection( &ths );
  42. }
  43. // nicht constant
  44. void AppSDatenbank::lock()
  45. {
  46. EnterCriticalSection( &ths );
  47. }
  48. void AppSDatenbank::unlock()
  49. {
  50. LeaveCriticalSection( &ths );
  51. }
  52. int AppSDatenbank::istAdministrator( const char *name, const char *passwort )
  53. {
  54. Text *befehl = new Text( "SELECT id FROM benutzer WHERE name = '" );
  55. Text n( name );
  56. n.ersetzen( "'", "''" );
  57. befehl->append( (char*)n );
  58. befehl->append( "' AND passwort = md5('" );
  59. Text p( passwort );
  60. p.ersetzen( "'", "''" );
  61. befehl->append( (char*)p );
  62. befehl->append( "')" );
  63. lock();
  64. datenbank->befehl( befehl->getText() );
  65. Result res = datenbank->getResult();
  66. unlock();
  67. befehl->release();
  68. int ret = 0;
  69. if( res.zeilenAnzahl > 0 )
  70. ret = TextZuInt( res.values[ 0 ].getText(), 10 );
  71. res.destroy();
  72. return ret;
  73. }
  74. bool AppSDatenbank::adminHatRecht( int id, int recht )
  75. {
  76. Text *befehl = new Text( "SELECT * FROM benutzer_rechte WHERE benutzer_id = " );
  77. befehl->append( id );
  78. befehl->append( " AND rechte_id = " );
  79. befehl->append( recht );
  80. lock();
  81. datenbank->befehl( befehl->getText() );
  82. int ret = datenbank->getZeilenAnzahl();
  83. unlock();
  84. befehl->release();
  85. return ret != 0;
  86. }
  87. Text *AppSDatenbank::getKey()
  88. {
  89. lock();
  90. datenbank->befehl( "SELECT get_next_schluessel()" );
  91. Result res = datenbank->getResult();
  92. unlock();
  93. if( !res.zeilenAnzahl )
  94. return 0;
  95. Text *ret = new Text( res.values[ 0 ].getText() );
  96. res.destroy();
  97. return ret;
  98. }
  99. int AppSDatenbank::login( char *name, char *passwort )
  100. {
  101. Text befehl = "SELECT id FROM account WHERE name = '";
  102. befehl += name;
  103. befehl += "' AND passwort = '";
  104. befehl += passwort;
  105. befehl += "'";
  106. lock();
  107. datenbank->befehl( befehl );
  108. Result res = datenbank->getResult();
  109. unlock();
  110. if( !res.zeilenAnzahl )
  111. return 0;
  112. int ret = res.values[ 0 ];
  113. res.destroy();
  114. return ret;
  115. }
  116. bool AppSDatenbank::istNeu( int account )
  117. {
  118. Text befehl = "SELECT * from app_5_minigames_score WHERE account_id = ";
  119. befehl += account;
  120. lock();
  121. datenbank->befehl( befehl );
  122. int zeilen = datenbank->getZeilenAnzahl();
  123. unlock();
  124. return zeilen == 0;
  125. }
  126. int AppSDatenbank::getWeltBloeckeScore( int anz, Array< int > *score, RCArray< Text > *namen )
  127. {
  128. Text befehl = "SELECT a.bloecke, b.ruf_name FROM app_5_minigames_score a, account b WHERE b.id = a.account_id ORDER BY a.bloecke DESC LIMIT ";
  129. befehl += anz;
  130. lock();
  131. datenbank->befehl( befehl );
  132. Result res = datenbank->getResult();
  133. unlock();
  134. int ret = res.zeilenAnzahl;
  135. for( int i = 0; i < ret; i++ )
  136. {
  137. score->set( res.values[ i * 2 ], i );
  138. namen->set( new Text( res.values[ i * 2 + 1 ].getText() ), i );
  139. }
  140. res.destroy();
  141. return ret;
  142. }
  143. int AppSDatenbank::getWeltMauerScore( int anz, Array< int > *score, RCArray< Text > *namen )
  144. {
  145. Text befehl = "SELECT a.mauer, b.ruf_name FROM app_5_minigames_score a, account b WHERE b.id = a.account_id ORDER BY a.mauer DESC LIMIT ";
  146. befehl += anz;
  147. lock();
  148. datenbank->befehl( befehl );
  149. Result res = datenbank->getResult();
  150. unlock();
  151. int ret = res.zeilenAnzahl;
  152. for( int i = 0; i < ret; i++ )
  153. {
  154. score->set( res.values[ i * 2 ], i );
  155. namen->set( new Text( res.values[ i * 2 + 1 ].getText() ), i );
  156. }
  157. res.destroy();
  158. return ret;
  159. }
  160. int AppSDatenbank::getWeltFarbenScore( int anz, Array< int > *score, RCArray< Text > *namen )
  161. {
  162. Text befehl = "SELECT a.farben, b.ruf_name FROM app_5_minigames_score a, account b WHERE b.id = a.account_id ORDER BY a.farben DESC LIMIT ";
  163. befehl += anz;
  164. lock();
  165. datenbank->befehl( befehl );
  166. Result res = datenbank->getResult();
  167. unlock();
  168. int ret = res.zeilenAnzahl;
  169. for( int i = 0; i < ret; i++ )
  170. {
  171. score->set( res.values[ i * 2 ], i );
  172. namen->set( new Text( res.values[ i * 2 + 1 ].getText() ), i );
  173. }
  174. res.destroy();
  175. return ret;
  176. }
  177. int AppSDatenbank::getWeltFangenScore( int anz, Array< int > *score, RCArray< Text > *namen )
  178. {
  179. Text befehl = "SELECT a.fangen, b.ruf_name FROM app_5_minigames_score a, account b WHERE b.id = a.account_id ORDER BY a.fangen DESC LIMIT ";
  180. befehl += anz;
  181. lock();
  182. datenbank->befehl( befehl );
  183. Result res = datenbank->getResult();
  184. unlock();
  185. int ret = res.zeilenAnzahl;
  186. for( int i = 0; i < ret; i++ )
  187. {
  188. score->set( res.values[ i * 2 ], i );
  189. namen->set( new Text( res.values[ i * 2 + 1 ].getText() ), i );
  190. }
  191. res.destroy();
  192. return ret;
  193. }
  194. int AppSDatenbank::getWeltRennenScore( int anz, Array< int > *score, RCArray< Text > *namen )
  195. {
  196. Text befehl = "SELECT a.rennen, b.ruf_name FROM app_5_minigames_score a, account b WHERE b.id = a.account_id ORDER BY a.rennen DESC LIMIT ";
  197. befehl += anz;
  198. lock();
  199. datenbank->befehl( befehl );
  200. Result res = datenbank->getResult();
  201. unlock();
  202. int ret = res.zeilenAnzahl;
  203. for( int i = 0; i < ret; i++ )
  204. {
  205. score->set( res.values[ i * 2 ], i );
  206. namen->set( new Text( res.values[ i * 2 + 1 ].getText() ), i );
  207. }
  208. res.destroy();
  209. return ret;
  210. }
  211. int AppSDatenbank::getWeltGesamtScore( int anz, Array< int > *score, RCArray< Text > *namen )
  212. {
  213. Text befehl = "SELECT a.gesamt, b.ruf_name FROM app_5_minigames_score a, account b WHERE b.id = a.account_id ORDER BY a.gesamt DESC LIMIT ";
  214. befehl += anz;
  215. lock();
  216. datenbank->befehl( befehl );
  217. Result res = datenbank->getResult();
  218. unlock();
  219. int ret = res.zeilenAnzahl;
  220. for( int i = 0; i < ret; i++ )
  221. {
  222. score->set( res.values[ i * 2 ], i );
  223. namen->set( new Text( res.values[ i * 2 + 1 ].getText() ), i );
  224. }
  225. res.destroy();
  226. return ret;
  227. }
  228. int AppSDatenbank::getBloeckeScore( int account )
  229. {
  230. Text befehl = "SELECT bloecke FROM app_5_minigames_score WHERE account_id = ";
  231. befehl += account;
  232. lock();
  233. datenbank->befehl( befehl );
  234. Result res = datenbank->getResult();
  235. unlock();
  236. if( !res.zeilenAnzahl )
  237. return 0;
  238. int ret = res.values[ 0 ];
  239. res.destroy();
  240. return ret;
  241. }
  242. int AppSDatenbank::getMauerScore( int account )
  243. {
  244. Text befehl = "SELECT mauer FROM app_5_minigames_score WHERE account_id = ";
  245. befehl += account;
  246. lock();
  247. datenbank->befehl( befehl );
  248. Result res = datenbank->getResult();
  249. unlock();
  250. if( !res.zeilenAnzahl )
  251. return 0;
  252. int ret = res.values[ 0 ];
  253. res.destroy();
  254. return ret;
  255. }
  256. int AppSDatenbank::getMauerZeit( int account, int level )
  257. {
  258. Text befehl = "SELECT sekunden FROM app_5_minigames_mauer_score WHERE account_id = ";
  259. befehl += account;
  260. befehl += " AND level = ";
  261. befehl += level;
  262. lock();
  263. datenbank->befehl( befehl );
  264. Result res = datenbank->getResult();
  265. unlock();
  266. if( !res.zeilenAnzahl )
  267. return 0;
  268. int ret = res.values[ 0 ];
  269. res.destroy();
  270. return ret;
  271. }
  272. int AppSDatenbank::getFarbenScore( int account )
  273. {
  274. Text befehl = "SELECT farben FROM app_5_minigames_score WHERE account_id = ";
  275. befehl += account;
  276. lock();
  277. datenbank->befehl( befehl );
  278. Result res = datenbank->getResult();
  279. unlock();
  280. if( !res.zeilenAnzahl )
  281. return 0;
  282. int ret = res.values[ 0 ];
  283. res.destroy();
  284. return ret;
  285. }
  286. int AppSDatenbank::getFangenScore( int account )
  287. {
  288. Text befehl = "SELECT fangen FROM app_5_minigames_score WHERE account_id = ";
  289. befehl += account;
  290. lock();
  291. datenbank->befehl( befehl );
  292. Result res = datenbank->getResult();
  293. unlock();
  294. if( !res.zeilenAnzahl )
  295. return 0;
  296. int ret = res.values[ 0 ];
  297. res.destroy();
  298. return ret;
  299. }
  300. int AppSDatenbank::getRennenScore( int account )
  301. {
  302. Text befehl = "SELECT rennen FROM app_5_minigames_score WHERE account_id = ";
  303. befehl += account;
  304. lock();
  305. datenbank->befehl( befehl );
  306. Result res = datenbank->getResult();
  307. unlock();
  308. if( !res.zeilenAnzahl )
  309. return 0;
  310. int ret = res.values[ 0 ];
  311. res.destroy();
  312. return ret;
  313. }
  314. int AppSDatenbank::getGesamtScore( int account )
  315. {
  316. Text befehl = "SELECT gesamt FROM app_5_minigames_score WHERE account_id = ";
  317. befehl += account;
  318. lock();
  319. datenbank->befehl( befehl );
  320. Result res = datenbank->getResult();
  321. unlock();
  322. if( !res.zeilenAnzahl )
  323. return 0;
  324. int ret = res.values[ 0 ];
  325. res.destroy();
  326. return ret;
  327. }
  328. void AppSDatenbank::setBloeckeScore( int account, int score )
  329. {
  330. if( score < getBloeckeScore( account ) )
  331. return;
  332. Text befehl = "UPDATE app_5_minigames_score SET bloecke = ";
  333. befehl += score;
  334. befehl += " WHERE account_id = ";
  335. befehl += account;
  336. lock();
  337. datenbank->befehl( befehl );
  338. int z = datenbank->getZeilenAnzahl();
  339. unlock();
  340. if( !z )
  341. {
  342. befehl = "INSERT INTO app_5_minigames_score( bloecke, account_id ) VALUES( ";
  343. befehl += score;
  344. befehl += ", ";
  345. befehl += account;
  346. befehl += " )";
  347. lock();
  348. datenbank->befehl( befehl );
  349. unlock();
  350. }
  351. calkGesamtScore( account );
  352. }
  353. void AppSDatenbank::setFarbenScore( int account, int score )
  354. {
  355. if( score < getFarbenScore( account ) )
  356. return;
  357. Text befehl = "UPDATE app_5_minigames_score SET farben = ";
  358. befehl += score;
  359. befehl += " WHERE account_id = ";
  360. befehl += account;
  361. lock();
  362. datenbank->befehl( befehl );
  363. int z = datenbank->getZeilenAnzahl();
  364. unlock();
  365. if( !z )
  366. {
  367. befehl = "INSERT INTO app_5_minigames_score( farben, account_id ) VALUES( ";
  368. befehl += score;
  369. befehl += ", ";
  370. befehl += account;
  371. befehl += " )";
  372. lock();
  373. datenbank->befehl( befehl );
  374. unlock();
  375. }
  376. calkGesamtScore( account );
  377. }
  378. void AppSDatenbank::setFangenScore( int account, int score )
  379. {
  380. if( score < getFangenScore( account ) )
  381. return;
  382. Text befehl = "UPDATE app_5_minigames_score SET fangen = ";
  383. befehl += score;
  384. befehl += " WHERE account_id = ";
  385. befehl += account;
  386. lock();
  387. datenbank->befehl( befehl );
  388. int z = datenbank->getZeilenAnzahl();
  389. unlock();
  390. if( !z )
  391. {
  392. befehl = "INSERT INTO app_5_minigames_score( fangen, account_id ) VALUES( ";
  393. befehl += score;
  394. befehl += ", ";
  395. befehl += account;
  396. befehl += " )";
  397. lock();
  398. datenbank->befehl( befehl );
  399. unlock();
  400. }
  401. calkGesamtScore( account );
  402. }
  403. void AppSDatenbank::setRennenScore( int account, int score )
  404. {
  405. if( score < getRennenScore( account ) )
  406. return;
  407. Text befehl = "UPDATE app_5_minigames_score SET rennen = ";
  408. befehl += score;
  409. befehl += " WHERE account_id = ";
  410. befehl += account;
  411. lock();
  412. datenbank->befehl( befehl );
  413. int z = datenbank->getZeilenAnzahl();
  414. unlock();
  415. if( !z )
  416. {
  417. befehl = "INSERT INTO app_5_minigames_score( rennen, account_id ) VALUES( ";
  418. befehl += score;
  419. befehl += ", ";
  420. befehl += account;
  421. befehl += " )";
  422. lock();
  423. datenbank->befehl( befehl );
  424. unlock();
  425. }
  426. calkGesamtScore( account );
  427. }
  428. void AppSDatenbank::setMauerZeit( int account, int level, int zeit )
  429. {
  430. if( zeit > getMauerZeit( account, level ) && getMauerZeit( account, level ) != 0 )
  431. return;
  432. if( zeit > 600 )
  433. zeit = 600;
  434. Text befehl = "UPDATE app_5_minigames_mauer_score SET sekunden = ";
  435. befehl += zeit;
  436. befehl += " WHERE account_id = ";
  437. befehl += account;
  438. befehl += " AND level = ";
  439. befehl += level;
  440. lock();
  441. datenbank->befehl( befehl );
  442. int z = datenbank->getZeilenAnzahl();
  443. unlock();
  444. if( !z )
  445. {
  446. befehl = "INSERT INTO app_5_minigames_mauer_score( account_id, level, sekunden ) VALUES( ";
  447. befehl += account;
  448. befehl += ", ";
  449. befehl += level;
  450. befehl += ", ";
  451. befehl += zeit;
  452. befehl += " )";
  453. lock();
  454. datenbank->befehl( befehl );
  455. unlock();
  456. }
  457. calkMauerScore( account );
  458. }
  459. void AppSDatenbank::calkMauerScore( int account )
  460. {
  461. Text befehl = "SELECT count( sekunden ), sum( sekunden ) FROM app_5_minigames_mauer_score WHERE account_id = ";
  462. befehl += account;
  463. lock();
  464. datenbank->befehl( befehl );
  465. Result res = datenbank->getResult();
  466. unlock();
  467. if( (int)res.values[ 0 ] == 0 )
  468. return;
  469. int score = (int)res.values[ 0 ] * 600 - (int)res.values[ 1 ];
  470. res.destroy();
  471. befehl = "UPDATE app_5_minigames_score SET mauer = ";
  472. befehl += score;
  473. befehl += " WHERE account_id = ";
  474. befehl += account;
  475. lock();
  476. datenbank->befehl( befehl );
  477. int z = datenbank->getZeilenAnzahl();
  478. unlock();
  479. if( !z )
  480. {
  481. befehl = "INSERT INTO app_5_minigames_score( mauer, account_id ) VALUES( ";
  482. befehl += score;
  483. befehl += ", ";
  484. befehl += account;
  485. befehl += " )";
  486. lock();
  487. datenbank->befehl( befehl );
  488. unlock();
  489. }
  490. calkGesamtScore( account );
  491. }
  492. void AppSDatenbank::calkGesamtScore( int account )
  493. {
  494. Text befehl = "SELECT bloecke, mauer, farben, fangen, rennen FROM app_5_minigames_score WHERE account_id = ";
  495. befehl += account;
  496. lock();
  497. datenbank->befehl( befehl );
  498. Result res = datenbank->getResult();
  499. unlock();
  500. if( !res.zeilenAnzahl )
  501. return;
  502. int gesamt = ( (int)res.values[ 0 ] + (int)res.values[ 1 ] + (int)res.values[ 2 ] + (int)res.values[ 3 ] + (int)res.values[ 4 ] ) / 5;
  503. res.destroy();
  504. befehl = "UPDATE app_5_minigames_score SET gesamt = ";
  505. befehl += gesamt;
  506. befehl += " WHERE account_id = ";
  507. befehl += account;
  508. lock();
  509. datenbank->befehl( befehl );
  510. unlock();
  511. }
  512. int AppSDatenbank::pruefNeuAccount( const char *name, const char *eMail )
  513. {
  514. Text *befehl = new Text( "SELECT account_neu_alt_check( '" );
  515. Text n( name );
  516. n.ersetzen( "'", "''" );
  517. befehl->append( (char*)n );
  518. befehl->append( "', '" );
  519. Text m( eMail );
  520. m.ersetzen( "'", "''" );
  521. befehl->append( (char*)m );
  522. befehl->append( "' )" );
  523. lock();
  524. datenbank->befehl( befehl->getText() );
  525. Result res = datenbank->getResult();
  526. unlock();
  527. if( res.values[ 0 ].istGleich( "t" ) )
  528. {
  529. befehl->release();
  530. res.destroy();
  531. return 0;
  532. }
  533. else
  534. {
  535. res.destroy();
  536. befehl->setText( "SELECT account.id FROM account, account_neu WHERE account.name = '" );
  537. befehl->append( (char*)n );
  538. befehl->append( "' OR account_neu.name = '" );
  539. befehl->append( (char*)n );
  540. befehl->append( "'" );
  541. lock();
  542. datenbank->befehl( befehl->getText() );
  543. res = datenbank->getResult();
  544. unlock();
  545. if( res.zeilenAnzahl > 0 )
  546. {
  547. befehl->release();
  548. res.destroy();
  549. return 1;
  550. }
  551. else
  552. {
  553. res.destroy();
  554. befehl->release();
  555. return 2;
  556. }
  557. }
  558. }
  559. bool AppSDatenbank::neuAccount( const char *name, const char *pass, const char *geheim, const char *eMail, const char *gebDatum, InitDatei *zIni )
  560. {
  561. Text *befehl = new Text( "INSERT INTO account_neu( name, passwort, geheimnis, e_mail, geb_datum ) VALUES ( '" );
  562. Text n( name );
  563. n.ersetzen( "'", "''" );
  564. befehl->append( (char*)n );
  565. befehl->append( "', '" );
  566. Text p( pass );
  567. p.ersetzen( "'", "''" );
  568. befehl->append( (char*)p );
  569. befehl->append( "', '" );
  570. Text g( geheim );
  571. g.ersetzen( "'", "''" );
  572. befehl->append( (char*)g );
  573. befehl->append( "', '" );
  574. Text m( eMail );
  575. m.ersetzen( "'", "''" );
  576. befehl->append( (char*)m );
  577. befehl->append( "', '" );
  578. Text d( gebDatum );
  579. d.ersetzen( "'", "''" );
  580. befehl->append( (char*)d );
  581. befehl->append( "' )" );
  582. lock();
  583. if( datenbank->befehl( befehl->getText() ) )
  584. {
  585. unlock();
  586. befehl->setText( "SELECT schluessel FROM account_neu WHERE name = '" );
  587. befehl->append( (char*)n );
  588. befehl->append( "'" );
  589. lock();
  590. datenbank->befehl( befehl->getText() );
  591. Result res = datenbank->getResult();
  592. unlock();
  593. Datei md;
  594. if( res.zeilenAnzahl )
  595. {
  596. md.setDatei( res.values[ 0 ].getText() );
  597. if( md.open( Datei::Style::schreiben ) )
  598. {
  599. Text txt = eMail;
  600. txt += "\naccount activation\n Account Details:\n";
  601. txt += "\r\nname: ";
  602. txt += name;
  603. txt += "\r\npassword: ";
  604. txt += pass;
  605. txt += "\r\nsecret: ";
  606. txt += geheim;
  607. txt += "\r\ndate of birth: ";
  608. txt += gebDatum;
  609. txt += "\r\nactivation key: ";
  610. txt += res.values[ 0 ].getText();
  611. md.schreibe( txt, txt.getLength() );
  612. md.close();
  613. // e_mail senden
  614. #ifdef WIN32
  615. PROCESS_INFORMATION prozessinfo1;
  616. STARTUPINFO startinfo1;
  617. startinfo1.cb = sizeof( STARTUPINFOW );
  618. ZeroMemory( &startinfo1, sizeof( STARTUPINFOW ) );
  619. Text cmdl = zIni->zWert( "Java" )->getText();
  620. cmdl += " Mailer ";
  621. cmdl += res.values[ 0 ].getText();
  622. if( CreateProcess( 0, cmdl, 0, 0, 0, 0, 0, 0, &startinfo1, &prozessinfo1 ) == 1 )
  623. CloseHandle( prozessinfo1.hThread );
  624. #else
  625. pid_t pid;
  626. char *pargs[] = { zIni->zWert( "Java" )->getText(), ( char* )"Mailer", res.values[ 0 ].getText(), (char*)0 };
  627. posix_spawn( &pid, zIni->zWert( "Java" )->getText(), 0, 0, pargs, 0 );
  628. #endif
  629. }
  630. }
  631. res.destroy();
  632. befehl->release();
  633. return 1;
  634. }
  635. unlock();
  636. befehl->release();
  637. return 0;
  638. }
  639. void AppSDatenbank::neuAccountAbbruch( const char *name )
  640. {
  641. Text *befehl = new Text( "DELETE FROM account_neu WHERE name = '" );
  642. Text n( name );
  643. n.ersetzen( "'", "''" );
  644. befehl->append( (char*)n );
  645. befehl->append( "'" );
  646. lock();
  647. datenbank->befehl( befehl->getText() );
  648. unlock();
  649. befehl->release();
  650. }
  651. bool AppSDatenbank::aktiviereAccount( const char *name, const char *key )
  652. {
  653. Text *befehl = new Text( "SELECT account_aktivieren( '" );
  654. Text n( name );
  655. n.ersetzen( "'", "''" );
  656. befehl->append( (char*)n );
  657. befehl->append( "', '" );
  658. Text s( key );
  659. s.ersetzen( "'", "''" );
  660. befehl->append( (char*)s );
  661. befehl->append( "' )" );
  662. lock();
  663. if( datenbank->befehl( befehl->getText() ) )
  664. {
  665. befehl->release();
  666. Result res = datenbank->getResult();
  667. unlock();
  668. if( res.values[ 0 ].istGleich( "f" ) )
  669. {
  670. res.destroy();
  671. return 0;
  672. }
  673. return 1;
  674. }
  675. unlock();
  676. befehl->release();
  677. return 0;
  678. }
  679. char AppSDatenbank::suchBestaetigung( const char *name, const char *pass )
  680. {
  681. Text *befehl = new Text( "select such_account_bestaetigung( '" );
  682. Text n( name );
  683. n.ersetzen( "'", "''" );
  684. befehl->append( (char*)n );
  685. befehl->append( "', '" );
  686. Text p( pass );
  687. p.ersetzen( "'", "''" );
  688. befehl->append( (char*)p );
  689. befehl->append( "' )" );
  690. lock();
  691. datenbank->befehl( befehl->getText() );
  692. Result res = datenbank->getResult();
  693. unlock();
  694. befehl->release();
  695. char ret = (char)TextZuInt( res.values[ 0 ].getText(), 10 );
  696. res.destroy();
  697. return ret;
  698. }
  699. void AppSDatenbank::sendeErstellEMail( const char *name, InitDatei *zIni )
  700. {
  701. Text *befehl = new Text( "SELECT passwort, geheimnis, e_mail, geb_datum, schluessel FROM account_neu WHERE name = '" );
  702. Text n( name );
  703. n.ersetzen( "'", "''" );
  704. befehl->append( (char*)n );
  705. befehl->append( "'" );
  706. lock();
  707. datenbank->befehl( befehl->getText() );
  708. Result res = datenbank->getResult();
  709. unlock();
  710. befehl->release();
  711. Datei md;
  712. if( res.zeilenAnzahl )
  713. {
  714. md.setDatei( res.values[ 4 ].getText() );
  715. if( md.open( Datei::Style::schreiben ) )
  716. {
  717. Text txt = res.values[ 2 ].getText();
  718. txt += "\naccount activation\n Account Details:\n";
  719. txt += "\r\nname: ";
  720. txt += name;
  721. txt += "\r\npassword: ";
  722. txt += res.values[ 0 ].getText();
  723. txt += "\r\nsecret: ";
  724. txt += res.values[ 1 ].getText();
  725. txt += "\r\ndate of birth: ";
  726. txt += res.values[ 3 ].getText();
  727. txt += "\r\nactivation key: ";
  728. txt += res.values[ 4 ].getText();
  729. md.schreibe( txt, txt.getLength() );
  730. md.close();
  731. // e_mail senden
  732. #ifdef WIN32
  733. PROCESS_INFORMATION prozessinfo1;
  734. STARTUPINFO startinfo1;
  735. startinfo1.cb = sizeof( STARTUPINFOW );
  736. ZeroMemory( &startinfo1, sizeof( STARTUPINFOW ) );
  737. Text cmdl = zIni->zWert( "Java" )->getText();
  738. cmdl += " Mailer ";
  739. cmdl += res.values[ 4 ].getText();
  740. if( CreateProcess( 0, cmdl, 0, 0, 0, 0, 0, 0, &startinfo1, &prozessinfo1 ) == 1 )
  741. CloseHandle( prozessinfo1.hThread );
  742. #else
  743. pid_t pid;
  744. char *pargs[] = { zIni->zWert( "Java" )->getText(), ( char* )"Mailer", res.values[ 4 ].getText(), (char*)0 };
  745. posix_spawn( &pid, zIni->zWert( "Java" )->getText(), 0, 0, pargs, 0 );
  746. #endif
  747. }
  748. }
  749. res.destroy();
  750. }
  751. void AppSDatenbank::markballsFinish( int level, int sek, int account, char *device, int diff, int kupfer, int gId )
  752. {
  753. if( !device && kupfer > 0 )
  754. {
  755. Text befehl = "UPDATE account SET kupfer = kupfer + ";
  756. befehl += kupfer;
  757. befehl += " WHERE id = ";
  758. befehl += account;
  759. lock();
  760. datenbank->befehl( befehl );
  761. unlock();
  762. }
  763. if( gId > 0 )
  764. {
  765. Text befehl = "SELECT markballs_gegenstand_id FROM markballs_gegenstand_account WHERE markballs_gegenstand_id = ";
  766. befehl += gId;
  767. befehl += " AND diff = ";
  768. befehl += diff;
  769. if( !device )
  770. {
  771. befehl += " AND account_id = ";
  772. befehl += account;
  773. }
  774. else
  775. {
  776. befehl += " AND device = '";
  777. befehl += device;
  778. befehl += "'";
  779. }
  780. lock();
  781. datenbank->befehl( befehl );
  782. int rows = datenbank->getZeilenAnzahl();
  783. unlock();
  784. if( !rows )
  785. {
  786. befehl = "INSERT INTO markballs_gegenstand_account( account_id, device, markballs_gegenstand_id, diff ) VALUES( ";
  787. if( !device )
  788. {
  789. befehl += account;
  790. befehl += ", NULL, ";
  791. }
  792. else
  793. {
  794. befehl += "NULL, '";
  795. befehl += device;
  796. befehl += "', ";
  797. }
  798. befehl += gId;
  799. befehl += ", ";
  800. befehl += diff;
  801. befehl += " )";
  802. lock();
  803. datenbank->befehl( befehl );
  804. unlock();
  805. }
  806. }
  807. Text befehl = "SELECT * FROM markballs WHERE level = ";
  808. befehl += level;
  809. befehl += " AND diff = ";
  810. befehl += diff;
  811. lock();
  812. datenbank->befehl( befehl );
  813. int rows = datenbank->getZeilenAnzahl();
  814. unlock();
  815. if( !rows )
  816. {
  817. befehl = "INSERT INTO markballs( level, diff ) VALUES( ";
  818. befehl += level;
  819. befehl += ", ";
  820. befehl += diff;
  821. befehl += " )";
  822. lock();
  823. datenbank->befehl( befehl );
  824. unlock();
  825. }
  826. Text spalte = "verloren";
  827. if( sek > 0 && sek <= 10 )
  828. spalte = "s_1_10";
  829. else if( sek > 10 && sek <= 20 )
  830. spalte = "s_11_20";
  831. else if( sek > 20 && sek <= 30 )
  832. spalte = "s_21_30";
  833. else if( sek > 30 && sek <= 40 )
  834. spalte = "s_31_40";
  835. else if( sek > 40 && sek <= 50 )
  836. spalte = "s_41_50";
  837. else if( sek > 50 && sek <= 60 )
  838. spalte = "s_51_60";
  839. else if( sek > 60 )
  840. spalte = "more";
  841. befehl = "UPDATE markballs SET ";
  842. befehl += spalte.getText();
  843. befehl += " = ";
  844. befehl += spalte.getText();
  845. befehl += " + 1, account_id = ";
  846. if( account )
  847. befehl += account;
  848. else
  849. befehl += "null";
  850. befehl += ", device = ";
  851. if( !device )
  852. befehl += "null";
  853. else
  854. {
  855. befehl += "'";
  856. befehl += device;
  857. befehl += "'";
  858. }
  859. befehl += ", best = ";
  860. befehl += sek;
  861. befehl += " WHERE level = ";
  862. befehl += level;
  863. befehl += " AND diff = ";
  864. befehl += diff;
  865. lock();
  866. datenbank->befehl( befehl );
  867. unlock();
  868. }
  869. int AppSDatenbank::getMarkballsScore( Array< int > *level, Array< int > *score, RCArray< Text > *name, int diff )
  870. {
  871. Text befehl = "SELECT a.level, a.best, b.ruf_name from markballs a left join account b ON a.account_id = b.id WHERE a.diff = ";
  872. befehl += diff;
  873. lock();
  874. datenbank->befehl( befehl );
  875. Result res = datenbank->getResult();
  876. unlock();
  877. int ret = res.zeilenAnzahl;
  878. for( int i = 0; i < ret; i++ )
  879. {
  880. level->set( res.values[ i * 3 ], i );
  881. score->set( res.values[ i * 3 + 1 ], i );
  882. name->set( new Text( res.values[ i * 3 + 2 ].getText() ), i );
  883. }
  884. res.destroy();
  885. return ret;
  886. }
  887. int AppSDatenbank::getMarkballsScore( Array< int > *level, Array< int > *score, int diff )
  888. {
  889. Text befehl = "SELECT level, best from markballs WHERE diff = ";
  890. befehl += diff;
  891. lock();
  892. datenbank->befehl( befehl );
  893. Result res = datenbank->getResult();
  894. unlock();
  895. int ret = res.zeilenAnzahl;
  896. for( int i = 0; i < ret; i++ )
  897. {
  898. level->set( res.values[ i * 2 ], i );
  899. score->set( res.values[ i * 2 + 1 ], i );
  900. }
  901. res.destroy();
  902. return ret;
  903. }
  904. void AppSDatenbank::deviceAccount( char *device, int account )
  905. {
  906. Text befehl = "UPDATE markballs SET account_id = ";
  907. befehl += account;
  908. befehl += ", device = null WHERE account_id IS NULL AND device = '";
  909. befehl += device;
  910. befehl += "'";
  911. lock();
  912. datenbank->befehl( befehl );
  913. unlock();
  914. befehl = "SELECT app_device_to_account( '";
  915. befehl += device;
  916. befehl += "', ";
  917. befehl += account;
  918. befehl += " )";
  919. lock();
  920. datenbank->befehl( befehl );
  921. unlock();
  922. befehl = "UPDATE markballs_fortschritt SET account_id = ";
  923. befehl += account;
  924. befehl += ", device = null WHERE account_id IS NULL AND device = '";
  925. befehl += device;
  926. befehl += "'";
  927. lock();
  928. datenbank->befehl( befehl );
  929. unlock();
  930. befehl = "UPDATE markballs_gegenstand_account SET account_id = ";
  931. befehl += account;
  932. befehl += ", device = null WHERE account_id IS NULL AND device = '";
  933. befehl += device;
  934. befehl += "'";
  935. lock();
  936. datenbank->befehl( befehl );
  937. unlock();
  938. }
  939. int AppSDatenbank::getGegenstaende( int accountId, char *device, Array< int > *gId, Array< int > *diff )
  940. {
  941. Text befehl = "SELECT markballs_gegenstand_id, diff FROM markballs_gegenstand_account WHERE ";
  942. if( !device )
  943. {
  944. befehl += " account_id = ";
  945. befehl += accountId;
  946. }
  947. else
  948. {
  949. befehl += " device = '";
  950. befehl += device;
  951. befehl += "'";
  952. }
  953. lock();
  954. datenbank->befehl( befehl );
  955. Result r = datenbank->getResult();
  956. unlock();
  957. if( !r.zeilenAnzahl )
  958. {
  959. r.destroy();
  960. return 0;
  961. }
  962. for( int i = 0; i < r.zeilenAnzahl; i++ )
  963. {
  964. gId->add( (int)r.values[ i * 2 ] );
  965. diff->add( (int)r.values[ i * 2 + 1 ] );
  966. }
  967. int ret = r.zeilenAnzahl;
  968. r.destroy();
  969. return ret;
  970. }
  971. int AppSDatenbank::getKupfer( int accountId )
  972. {
  973. Text befehl = "SELECT kupfer FROM account WHERE id = ";
  974. befehl += accountId;
  975. lock();
  976. datenbank->befehl( befehl );
  977. Result r = datenbank->getResult();
  978. unlock();
  979. int ret = r.zeilenAnzahl ? (int)r.values[ 0 ] : 0;
  980. r.destroy();
  981. return ret;
  982. }
  983. int AppSDatenbank::getMarkballsFortschritt( int accountId, char *device, Array< int > *f )
  984. {
  985. Text befehl = "SELECT level, diff FROM markballs_fortschritt WHERE ";
  986. if( !device )
  987. {
  988. befehl += " account_id = ";
  989. befehl += accountId;
  990. }
  991. else
  992. {
  993. befehl += " device = '";
  994. befehl += device;
  995. befehl += "'";
  996. }
  997. befehl += " ORDER BY diff";
  998. lock();
  999. datenbank->befehl( befehl );
  1000. Result r = datenbank->getResult();
  1001. unlock();
  1002. int anz = 0;
  1003. int lastDiff = 0;
  1004. for( int i = 0; i < r.zeilenAnzahl; i++, anz++ )
  1005. {
  1006. int l = (int)r.values[ i * 2 ];
  1007. int d = (int)r.values[ i * 2 + 1 ];
  1008. for( int j = lastDiff + 1; j < d; j++, anz++ )
  1009. f->add( 0 );
  1010. f->add( l );
  1011. lastDiff = d;
  1012. }
  1013. r.destroy();
  1014. return anz;
  1015. }
  1016. void AppSDatenbank::curvesnakeFinish( int score, int accId, char *device, int map, int kupfer )
  1017. {
  1018. Text befehl = "SELECT id FROM curvesnake WHERE map = ";
  1019. befehl += map;
  1020. lock();
  1021. datenbank->befehl( befehl );
  1022. int anz = datenbank->getZeilenAnzahl();
  1023. unlock();
  1024. if( !anz )
  1025. {
  1026. befehl = "INSERT INTO curvesnake( map, score ) VALUES( ";
  1027. befehl += map;
  1028. befehl += ", 0 )";
  1029. lock();
  1030. datenbank->befehl( befehl );
  1031. unlock();
  1032. }
  1033. befehl = "UPDATE curvesnake SET score = ";
  1034. befehl += score;
  1035. if( device )
  1036. {
  1037. befehl += ", device = '";
  1038. befehl += device;
  1039. befehl += "'";
  1040. }
  1041. else
  1042. {
  1043. befehl += ", account_id = ";
  1044. befehl += accId;
  1045. }
  1046. befehl += " WHERE map = ";
  1047. befehl += map;
  1048. lock();
  1049. datenbank->befehl( befehl );
  1050. unlock();
  1051. if( !device && kupfer )
  1052. {
  1053. befehl = "UPDATE account SET kupfer = kupfer + ";
  1054. befehl += kupfer;
  1055. befehl += " WHERE id = ";
  1056. befehl += accId;
  1057. lock();
  1058. datenbank->befehl( befehl );
  1059. unlock();
  1060. }
  1061. }
  1062. int AppSDatenbank::getCurvesnakeScore( Array< int > *map, Array< int > *score )
  1063. {
  1064. lock();
  1065. datenbank->befehl( "SELECT map, score from curvesnake ORDER BY map" );
  1066. Result r = datenbank->getResult();
  1067. unlock();
  1068. for( int i = 0; i < r.zeilenAnzahl; i++ )
  1069. {
  1070. map->add( r.values[ i * 2 ] );
  1071. score->add( r.values[ i * 2 + 1 ] );
  1072. }
  1073. int ret = r.zeilenAnzahl;
  1074. r.destroy();
  1075. return ret;
  1076. }
  1077. int AppSDatenbank::getCurvesnakeMapList( Array< int > *map, int account )
  1078. {
  1079. Text befehl = "SELECT id from curvesnake_map WHERE preis = 0 ";
  1080. if( account )
  1081. {
  1082. befehl += "UNION SELECT map_id FROM curvesnake_map_account WHERE account_id = ";
  1083. befehl += account;
  1084. }
  1085. lock();
  1086. datenbank->befehl( befehl );
  1087. Result r = datenbank->getResult();
  1088. unlock();
  1089. for( int i = 0; i < r.zeilenAnzahl; i++ )
  1090. map->add( r.values[ i ] );
  1091. int ret = r.zeilenAnzahl;
  1092. r.destroy();
  1093. return ret;
  1094. }
  1095. void AppSDatenbank::addKupfer( int account, int kupfer )
  1096. {
  1097. Text befehl = "UPDATE account SET kupfer = kupfer + ";
  1098. befehl += kupfer;
  1099. befehl += " WHERE id = ";
  1100. befehl += account;
  1101. lock();
  1102. datenbank->befehl( befehl );
  1103. unlock();
  1104. }
  1105. bool AppSDatenbank::getFreeAds( int account )
  1106. {
  1107. Text befehl = "SELECT get_account_free_ad( ";
  1108. befehl += account;
  1109. befehl += " )";
  1110. lock();
  1111. datenbank->befehl( befehl );
  1112. Result r = datenbank->getResult();
  1113. unlock();
  1114. if( r.zeilenAnzahl < 1 )
  1115. {
  1116. r.destroy();
  1117. return 0;
  1118. }
  1119. bool ret = r.values[ 0 ].istGleich( "t" );
  1120. r.destroy();
  1121. return ret;
  1122. }
  1123. // Reference Counting
  1124. AppSDatenbank *AppSDatenbank::getThis()
  1125. {
  1126. ref++;
  1127. return this;
  1128. }
  1129. AppSDatenbank *AppSDatenbank::release()
  1130. {
  1131. ref--;
  1132. if( !ref )
  1133. delete this;
  1134. return 0;
  1135. }