AppServer.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. #include "AppServer.h"
  2. #include "Datenbank.h"
  3. #include <Text.h>
  4. #include <iostream>
  5. #include <Klient.h>
  6. #include <Globals.h>
  7. // Inhalt der AppServer Klasse aus AppServer.h
  8. // Konstruktor
  9. AppServer::AppServer( InitDatei *zDat )
  10. : Thread()
  11. {
  12. Network::Start( 100 );
  13. std::cout << "AppS: Verbindung mit Datenbank wird hergestellt...\n";
  14. db = new AppSDatenbank( zDat );
  15. clients = 0;
  16. empfangen = 0;
  17. gesendet = 0;
  18. dat = zDat->getThis();
  19. server = new Server();
  20. aServer = new Server();
  21. std::cout << "AppS: Starten des Admin Servers...\n";
  22. if( !aServer->verbinde( (unsigned short)TextZuInt( zDat->zWert( "AppSAPort" )->getText(), 10 ), 10 ) )
  23. {
  24. std::cout << "AppS: Der Admin Server konnte nicht gestartet werden. Das Programm wird beendet.\n";
  25. exit( 1 );
  26. }
  27. end = 0;
  28. InitializeCriticalSection( &cs );
  29. ref = 1;
  30. if( zDat->wertExistiert( "Aktiv" ) && zDat->zWert( "Aktiv" )->istGleich( "TRUE" ) )
  31. serverStarten();
  32. }
  33. // Destruktor
  34. AppServer::~AppServer()
  35. {
  36. ende();
  37. server->trenne();
  38. server->release();
  39. aServer->trenne();
  40. aServer->release();
  41. dat->release();
  42. db->release();
  43. Network::Exit();
  44. DeleteCriticalSection( &cs );
  45. }
  46. // nicht constant
  47. void AppServer::runn()
  48. {
  49. while( !end )
  50. {
  51. SKlient *klient;
  52. klient = aServer->getKlient();
  53. if( end && klient )
  54. {
  55. klient->trenne();
  56. klient = klient->release();
  57. Sleep( 1000 );
  58. return;
  59. }
  60. if( !klient )
  61. return;
  62. AppSAKlient *clHandle = new AppSAKlient( klient, getThis() );
  63. clHandle->start();
  64. }
  65. }
  66. void AppServer::thread()
  67. {
  68. while( 1 )
  69. {
  70. SKlient *klient;
  71. klient = server->getKlient();
  72. if( !klient )
  73. break;
  74. Framework::getThreadRegister()->cleanUpClosedThreads();
  75. AppSKlient *clHandle = new AppSKlient( klient, getThis() );
  76. clHandle->start();
  77. clients++;
  78. }
  79. run = 0;
  80. }
  81. void AppServer::close()
  82. {
  83. server->trenne();
  84. #ifdef WIN32
  85. warteAufThread( 1000 );
  86. #endif
  87. ende();
  88. run = 0;
  89. end = 1;
  90. Klient *klient = new Klient();
  91. klient->verbinde( aServer->getPort(), "127.0.0.1" );
  92. Sleep( 500 );
  93. aServer->trenne();
  94. klient->release();
  95. }
  96. bool AppServer::serverStarten()
  97. {
  98. if( run )
  99. return 1;
  100. bool ret = server->verbinde( (unsigned short)TextZuInt( dat->zWert( "AppSPort" )->getText(), 10 ), 10 );
  101. start();
  102. return ret;
  103. }
  104. void AppServer::serverBeenden()
  105. {
  106. if( !run )
  107. return;
  108. server->trenne();
  109. #ifdef WIN32
  110. warteAufThread( 1000 );
  111. #endif
  112. ende();
  113. server->release();
  114. server = new Server();
  115. run = 0;
  116. }
  117. void AppServer::addGesendet( int bytes )
  118. {
  119. gesendet += bytes;
  120. }
  121. void AppServer::addEmpfangen( int bytes )
  122. {
  123. empfangen += empfangen;
  124. }
  125. void AppServer::clientTrennung()
  126. {
  127. clients--;
  128. }
  129. // constant
  130. InitDatei *AppServer::zIni() const
  131. {
  132. return dat;
  133. }
  134. Server *AppServer::zServer() const
  135. {
  136. return server;
  137. }
  138. AppSDatenbank *AppServer::zDB() const
  139. {
  140. return db;
  141. }
  142. int AppServer::getClients() const
  143. {
  144. return clients;
  145. }
  146. bool AppServer::hatClients() const
  147. {
  148. return clients > 0;
  149. }
  150. // Reference Counting
  151. AppServer *AppServer::getThis()
  152. {
  153. EnterCriticalSection( &cs );
  154. ref++;
  155. LeaveCriticalSection( &cs );
  156. return this;
  157. }
  158. AppServer *AppServer::release()
  159. {
  160. EnterCriticalSection( &cs );
  161. ref--;
  162. LeaveCriticalSection( &cs );
  163. if( !ref )
  164. delete this;
  165. return 0;
  166. }
  167. // Inhalt der AppSKlient Klasse aus AppServer.h
  168. // Konstruktor
  169. AppSAKlient::AppSAKlient( SKlient *klient, AppServer *appS )
  170. : Thread()
  171. {
  172. this->klient = klient;
  173. unsigned char key[ 20 ] = { 14, 143, 241, 74, 52, 68, 158, 32, 83, 67, 205, 80, 206, 243, 151, 156, 46, 72, 120, 161 };
  174. klient->setSendeKey( (char*)key, 20 );
  175. klient->setEmpfangKey( (char*)key, 20 );
  176. name = new Text( "" );
  177. passwort = new Text( "" );
  178. adminId = 0;
  179. this->appS = appS;
  180. }
  181. // Destruktor
  182. AppSAKlient::~AppSAKlient()
  183. {
  184. klient->trenne();
  185. klient->release();
  186. appS->release();
  187. name->release();
  188. passwort->release();
  189. }
  190. // nicht constant
  191. void AppSAKlient::thread()
  192. {
  193. while( 1 )
  194. {
  195. char c = 0;
  196. if( !klient->getNachrichtEncrypted( &c, 1 ) )
  197. break;
  198. else
  199. {
  200. bool br = 0;
  201. switch( c )
  202. {
  203. case 1: // Login
  204. if( 1 )
  205. {
  206. klient->sendeEncrypted( "\1", 1 );
  207. char nLen = 0;
  208. klient->getNachrichtEncrypted( &nLen, 1 );
  209. char *n = new char[ nLen + 1 ];
  210. n[ (int)nLen ] = 0;
  211. if( nLen )
  212. klient->getNachrichtEncrypted( n, nLen );
  213. char pLen = 0;
  214. klient->getNachrichtEncrypted( &pLen, 1 );
  215. char *p = new char[ pLen + 1 ];
  216. p[ (int)pLen ] = 0;
  217. if( pLen )
  218. klient->getNachrichtEncrypted( p, pLen );
  219. int adminId = appS->zDB()->istAdministrator( n, p );
  220. if( adminId )
  221. {
  222. klient->sendeEncrypted( "\1", 1 );
  223. name->setText( n );
  224. passwort->setText( p );
  225. this->adminId = adminId;
  226. }
  227. else
  228. errorZuKlient( "Falsche Kombination aus Name und Passwort." );
  229. delete[] n;
  230. delete[] p;
  231. }
  232. break;
  233. case 2: // Logout
  234. adminId = 0;
  235. name->setText( "" );
  236. passwort->setText( "" );
  237. klient->sendeEncrypted( "\1", 1 );
  238. break;
  239. case 3: // Trennen
  240. br = 1;
  241. klient->sendeEncrypted( "\1", 1 );
  242. break;
  243. case 4: // Server starten
  244. if( !adminId )
  245. errorZuKlient( "Du musst dich einloggen." );
  246. else
  247. {
  248. if( appS->zDB()->adminHatRecht( adminId, Admin_Recht::AppSStarten ) )
  249. {
  250. if( !appS->serverStarten() )
  251. errorZuKlient( "Beim starten des Servers ist ein Fehler aufgetreten." );
  252. else
  253. klient->sendeEncrypted( "\1", 1 );
  254. }
  255. else
  256. errorZuKlient( "Du bist nicht berechtigt den Server zu starten." );
  257. }
  258. break;
  259. case 5: // Server beenden
  260. if( !adminId )
  261. errorZuKlient( "Du musst dich einloggen." );
  262. else
  263. {
  264. if( appS->zDB()->adminHatRecht( adminId, Admin_Recht::AppSBeenden ) )
  265. {
  266. appS->serverBeenden();
  267. klient->sendeEncrypted( "\1", 1 );
  268. }
  269. else
  270. errorZuKlient( "Du bist nicht berechtigt den Server zu beenden." );
  271. }
  272. break;
  273. case 6: // Programm Schließen
  274. if( !adminId )
  275. errorZuKlient( "Du musst dich einloggen." );
  276. else
  277. {
  278. bool ok = 0;
  279. if( appS->isRunning() )
  280. {
  281. if( appS->zDB()->adminHatRecht( adminId, Admin_Recht::AppSBeenden ) )
  282. {
  283. appS->serverBeenden();
  284. ok = 1;
  285. }
  286. else
  287. errorZuKlient( "Du bist nicht berechtigt den Server zu beenden." );
  288. }
  289. else
  290. ok = 1;
  291. if( ok && appS->hatClients() )
  292. {
  293. errorZuKlient( "Es sind noch Klients Online. Bitte versuche es später erneut." );
  294. break;
  295. }
  296. if( ok )
  297. {
  298. klient->sendeEncrypted( "\1", 1 );
  299. std::cout << "AppS: Der Server wird von Benutzer " << adminId << " heruntergefahren.\n";
  300. appS->close();
  301. br = 1;
  302. }
  303. }
  304. break;
  305. case 7: // Progtamm abstürzen
  306. if( !adminId )
  307. errorZuKlient( "Du musst dich einloggen." );
  308. else
  309. {
  310. bool ok = 0;
  311. if( appS->isRunning() )
  312. {
  313. if( appS->zDB()->adminHatRecht( adminId, Admin_Recht::AppSBeenden ) )
  314. {
  315. appS->serverBeenden();
  316. ok = 1;
  317. }
  318. else
  319. errorZuKlient( "Du bist nicht berechtigt den Server zu beenden." );
  320. }
  321. else
  322. ok = 1;
  323. if( ok )
  324. {
  325. klient->sendeEncrypted( "\1", 1 );
  326. std::cout << "AppS: Der Server wurde von Benutzer " << adminId << " terminiert.\n";
  327. appS->close();
  328. br = 1;
  329. }
  330. }
  331. break;
  332. case 8: // Status Frage
  333. if( 1 )
  334. {
  335. klient->sendeEncrypted( "\1", 1 );
  336. char status = (int)appS->isRunning();
  337. klient->sendeEncrypted( &status, 1 );
  338. }
  339. break;
  340. default:
  341. errorZuKlient( "Unbekannte Nachricht!" );
  342. br = true;
  343. break;
  344. }
  345. if( br )
  346. break;
  347. appS->addEmpfangen( klient->getDownloadBytes( 1 ) );
  348. appS->addGesendet( klient->getUploadBytes( 1 ) );
  349. }
  350. }
  351. appS->addEmpfangen( klient->getDownloadBytes( 1 ) );
  352. appS->addGesendet( klient->getUploadBytes( 1 ) );
  353. delete this;
  354. }
  355. void AppSAKlient::errorZuKlient( const char *nachricht ) const // sendet eine Fehlernachricht zum Klient
  356. {
  357. klient->sendeEncrypted( "\3", 1 );
  358. char len = (char)textLength( nachricht );
  359. klient->sendeEncrypted( &len, 1 );
  360. klient->sendeEncrypted( nachricht, len );
  361. }
  362. // Inhalt der AppSKlient Klasse aus AppServer.h
  363. // Konstruktor
  364. AppSKlient::AppSKlient( SKlient *klient, AppServer *appS )
  365. : Thread()
  366. {
  367. std::cout << "New Client aktuell:" << appS->getClients() << "\n";
  368. this->klient = klient;
  369. unsigned char key[ 20 ] = { 74, 103, 6, 115, 185, 240, 35, 53, 108, 55, 120, 253, 1, 232, 78, 254, 183, 223, 101, 9 };
  370. klient->setSendeKey( (char*)key, 20 );
  371. klient->setEmpfangKey( (char*)key, 20 );
  372. this->appS = appS;
  373. accountId = 0;
  374. tmo = new AppSKTimeOut( this );
  375. }
  376. // Destruktor
  377. AppSKlient::~AppSKlient()
  378. {
  379. tmo->stop();
  380. tmo->warteAufThread( 1000 );
  381. delete tmo;
  382. appS->clientTrennung();
  383. appS->release();
  384. klient->release();
  385. std::cout << "Client Deleted aktuell:" << appS->getClients() << "\n";
  386. }
  387. // nicht constant
  388. void AppSKlient::thread()
  389. {
  390. while( 1 )
  391. {
  392. char c = 0;
  393. if( !klient->getNachrichtEncrypted( &c, 1 ) )
  394. break;
  395. else
  396. {
  397. tmo->addConnect();
  398. bool br = 0;
  399. switch( c )
  400. {
  401. case 0: // Schlüssel Anfrage
  402. if( 1 )
  403. {
  404. klient->sendeEncrypted( "\1", 1 );
  405. Text *s = appS->zDB()->getKey();
  406. char l = (char)s->getLength();
  407. klient->sendeEncrypted( &l, 1 );
  408. klient->sendeEncrypted( s->getText(), l );
  409. klient->setSendeKey( s->getText(), s->getLength() );
  410. klient->setEmpfangKey( s->getText(), s->getLength() );
  411. s->release();
  412. break;
  413. }
  414. case 1: // Login 5 Minigames
  415. if( 1 )
  416. {
  417. klient->sendeEncrypted( "\1", 1 );
  418. char l = 0;
  419. klient->getNachrichtEncrypted( &l, 1 );
  420. char *name = new char[ l + 1 ];
  421. name[ (int)l ] = 0;
  422. if( l )
  423. klient->getNachrichtEncrypted( name, l );
  424. klient->getNachrichtEncrypted( &l, 1 );
  425. char *passwort = new char[ l + 1 ];
  426. passwort[ (int)l ] = 0;
  427. if( l )
  428. klient->getNachrichtEncrypted( passwort, l );
  429. accountId = appS->zDB()->login( name, passwort );
  430. delete[] name;
  431. delete[] passwort;
  432. if( accountId > 0 )
  433. klient->sendeEncrypted( "\1", 1 );
  434. else
  435. klient->sende( "\0", 1 );
  436. if( accountId && appS->zDB()->istNeu( accountId ) )
  437. klient->sendeEncrypted( "\1", 1 );
  438. else if( accountId )
  439. klient->sendeEncrypted( "\0", 1 );
  440. break;
  441. }
  442. case 2: // Frage nach Welt Blöcke Score
  443. if( 1 )
  444. {
  445. klient->sendeEncrypted( "\1", 1 );
  446. int num = 0;
  447. klient->getNachrichtEncrypted( (char*)&num, 4 );
  448. Array< int > *score = new Array< int >();
  449. RCArray< Text > *namen = new RCArray< Text >();
  450. num = appS->zDB()->getWeltBloeckeScore( num, score, namen );
  451. klient->sendeEncrypted( (char*)&num, 4 );
  452. for( int i = 0; i < num; i++ )
  453. {
  454. int s = score->get( i );
  455. klient->sendeEncrypted( (char*)&s, 4 );
  456. char l = (char)namen->z( i )->getLength();
  457. klient->sendeEncrypted( &l, 1 );
  458. klient->sendeEncrypted( namen->z( i )->getText(), l );
  459. }
  460. score->release();
  461. namen->release();
  462. break;
  463. }
  464. case 3: // Frage nach Welt Mauer Score
  465. if( 1 )
  466. {
  467. klient->sendeEncrypted( "\1", 1 );
  468. int num = 0;
  469. klient->getNachrichtEncrypted( (char*)&num, 4 );
  470. Array< int > *score = new Array< int >();
  471. RCArray< Text > *namen = new RCArray< Text >();
  472. num = appS->zDB()->getWeltMauerScore( num, score, namen );
  473. klient->sendeEncrypted( (char*)&num, 4 );
  474. for( int i = 0; i < num; i++ )
  475. {
  476. int s = score->get( i );
  477. klient->sendeEncrypted( (char*)&s, 4 );
  478. char l = (char)namen->z( i )->getLength();
  479. klient->sendeEncrypted( &l, 1 );
  480. klient->sendeEncrypted( namen->z( i )->getText(), l );
  481. }
  482. score->release();
  483. namen->release();
  484. break;
  485. }
  486. case 4: // Frage nach Welt Farben Score
  487. if( 1 )
  488. {
  489. klient->sendeEncrypted( "\1", 1 );
  490. int num = 0;
  491. klient->getNachrichtEncrypted( (char*)&num, 4 );
  492. Array< int > *score = new Array< int >();
  493. RCArray< Text > *namen = new RCArray< Text >();
  494. num = appS->zDB()->getWeltFarbenScore( num, score, namen );
  495. klient->sendeEncrypted( (char*)&num, 4 );
  496. for( int i = 0; i < num; i++ )
  497. {
  498. int s = score->get( i );
  499. klient->sendeEncrypted( (char*)&s, 4 );
  500. char l = (char)namen->z( i )->getLength();
  501. klient->sendeEncrypted( &l, 1 );
  502. klient->sendeEncrypted( namen->z( i )->getText(), l );
  503. }
  504. score->release();
  505. namen->release();
  506. break;
  507. }
  508. case 5: // Frage nach Welt Fangen Score
  509. if( 1 )
  510. {
  511. klient->sendeEncrypted( "\1", 1 );
  512. int num = 0;
  513. klient->getNachrichtEncrypted( (char*)&num, 4 );
  514. Array< int > *score = new Array< int >();
  515. RCArray< Text > *namen = new RCArray< Text >();
  516. num = appS->zDB()->getWeltFangenScore( num, score, namen );
  517. klient->sendeEncrypted( (char*)&num, 4 );
  518. for( int i = 0; i < num; i++ )
  519. {
  520. int s = score->get( i );
  521. klient->sendeEncrypted( (char*)&s, 4 );
  522. char l = (char)namen->z( i )->getLength();
  523. klient->sendeEncrypted( &l, 1 );
  524. klient->sendeEncrypted( namen->z( i )->getText(), l );
  525. }
  526. score->release();
  527. namen->release();
  528. break;
  529. }
  530. case 6: // Frage nach Welt Rennen Score
  531. if( 1 )
  532. {
  533. klient->sendeEncrypted( "\1", 1 );
  534. int num = 0;
  535. klient->getNachrichtEncrypted( (char*)&num, 4 );
  536. Array< int > *score = new Array< int >();
  537. RCArray< Text > *namen = new RCArray< Text >();
  538. num = appS->zDB()->getWeltRennenScore( num, score, namen );
  539. klient->sendeEncrypted( (char*)&num, 4 );
  540. for( int i = 0; i < num; i++ )
  541. {
  542. int s = score->get( i );
  543. klient->sendeEncrypted( (char*)&s, 4 );
  544. char l = (char)namen->z( i )->getLength();
  545. klient->sendeEncrypted( &l, 1 );
  546. klient->sendeEncrypted( namen->z( i )->getText(), l );
  547. }
  548. score->release();
  549. namen->release();
  550. break;
  551. }
  552. case 7: // Frage nach Welt Gesamt Score
  553. if( 1 )
  554. {
  555. klient->sendeEncrypted( "\1", 1 );
  556. int num = 0;
  557. klient->getNachrichtEncrypted( (char*)&num, 4 );
  558. Array< int > *score = new Array< int >();
  559. RCArray< Text > *namen = new RCArray< Text >();
  560. num = appS->zDB()->getWeltGesamtScore( num, score, namen );
  561. klient->sendeEncrypted( (char*)&num, 4 );
  562. for( int i = 0; i < num; i++ )
  563. {
  564. int s = score->get( i );
  565. klient->sendeEncrypted( (char*)&s, 4 );
  566. char l = (char)namen->z( i )->getLength();
  567. klient->sendeEncrypted( &l, 1 );
  568. klient->sendeEncrypted( namen->z( i )->getText(), l );
  569. }
  570. score->release();
  571. namen->release();
  572. break;
  573. }
  574. case 8: // Frage nach Blöcke Score
  575. if( 1 )
  576. {
  577. if( accountId )
  578. klient->sendeEncrypted( "\1", 1 );
  579. else
  580. {
  581. klient->sendeEncrypted( "\0", 1 );
  582. break;
  583. }
  584. int score = appS->zDB()->getBloeckeScore( accountId );
  585. klient->sendeEncrypted( (char*)&score, 4 );
  586. break;
  587. }
  588. case 9: // Frage nach Mauer Score
  589. if( 1 )
  590. {
  591. if( accountId )
  592. klient->sendeEncrypted( "\1", 1 );
  593. else
  594. {
  595. klient->sendeEncrypted( "\0", 1 );
  596. break;
  597. }
  598. int score = appS->zDB()->getMauerScore( accountId );
  599. klient->sendeEncrypted( (char*)&score, 4 );
  600. break;
  601. }
  602. case 10: // Frage nach Farben Score
  603. if( 1 )
  604. {
  605. if( accountId )
  606. klient->sendeEncrypted( "\1", 1 );
  607. else
  608. {
  609. klient->sendeEncrypted( "\0", 1 );
  610. break;
  611. }
  612. int score = appS->zDB()->getFarbenScore( accountId );
  613. klient->sendeEncrypted( (char*)&score, 4 );
  614. break;
  615. }
  616. case 11: // Frage nach Fangen Score
  617. if( 1 )
  618. {
  619. if( accountId )
  620. klient->sendeEncrypted( "\1", 1 );
  621. else
  622. {
  623. klient->sendeEncrypted( "\0", 1 );
  624. break;
  625. }
  626. int score = appS->zDB()->getFangenScore( accountId );
  627. klient->sendeEncrypted( (char*)&score, 4 );
  628. break;
  629. }
  630. case 12: // Frage nach Rennen Score
  631. if( 1 )
  632. {
  633. if( accountId )
  634. klient->sendeEncrypted( "\1", 1 );
  635. else
  636. {
  637. klient->sendeEncrypted( "\0", 1 );
  638. break;
  639. }
  640. int score = appS->zDB()->getRennenScore( accountId );
  641. klient->sendeEncrypted( (char*)&score, 4 );
  642. break;
  643. }
  644. case 13: // Frage nach Gesamt Score
  645. if( 1 )
  646. {
  647. if( accountId )
  648. klient->sendeEncrypted( "\1", 1 );
  649. else
  650. {
  651. klient->sendeEncrypted( "\0", 1 );
  652. break;
  653. }
  654. int score = appS->zDB()->getGesamtScore( accountId );
  655. klient->sendeEncrypted( (char*)&score, 4 );
  656. break;
  657. }
  658. case 14: // Frage nach Mauer Zeit
  659. if( 1 )
  660. {
  661. if( accountId )
  662. klient->sendeEncrypted( "\1", 1 );
  663. else
  664. {
  665. klient->sendeEncrypted( "\0", 1 );
  666. break;
  667. }
  668. int level = 0;
  669. klient->getNachrichtEncrypted( (char*)&level, 4 );
  670. int sek = appS->zDB()->getMauerZeit( accountId, level );
  671. klient->sendeEncrypted( (char*)&sek, 4 );
  672. break;
  673. }
  674. case 15: // Set Blöcke Score
  675. if( 1 )
  676. {
  677. if( accountId )
  678. klient->sendeEncrypted( "\1", 1 );
  679. else
  680. {
  681. klient->sendeEncrypted( "\0", 1 );
  682. break;
  683. }
  684. int score = 0;
  685. klient->getNachrichtEncrypted( (char*)&score, 4 );
  686. appS->zDB()->setBloeckeScore( accountId, score );
  687. klient->sendeEncrypted( "\1", 1 );
  688. break;
  689. }
  690. case 16: // Set Farben Score
  691. if( 1 )
  692. {
  693. if( accountId )
  694. klient->sendeEncrypted( "\1", 1 );
  695. else
  696. {
  697. klient->sendeEncrypted( "\0", 1 );
  698. break;
  699. }
  700. int score = 0;
  701. klient->getNachrichtEncrypted( (char*)&score, 4 );
  702. appS->zDB()->setFarbenScore( accountId, score );
  703. klient->sendeEncrypted( "\1", 1 );
  704. break;
  705. }
  706. case 17: // Set Fangen Score
  707. if( 1 )
  708. {
  709. if( accountId )
  710. klient->sendeEncrypted( "\1", 1 );
  711. else
  712. {
  713. klient->sendeEncrypted( "\0", 1 );
  714. break;
  715. }
  716. int score = 0;
  717. klient->getNachrichtEncrypted( (char*)&score, 4 );
  718. appS->zDB()->setFangenScore( accountId, score );
  719. klient->sendeEncrypted( "\1", 1 );
  720. break;
  721. }
  722. case 18: // Set Rennen Score
  723. if( 1 )
  724. {
  725. if( accountId )
  726. klient->sendeEncrypted( "\1", 1 );
  727. else
  728. {
  729. klient->sendeEncrypted( "\0", 1 );
  730. break;
  731. }
  732. int score = 0;
  733. klient->getNachrichtEncrypted( (char*)&score, 4 );
  734. appS->zDB()->setRennenScore( accountId, score );
  735. klient->sendeEncrypted( "\1", 1 );
  736. break;
  737. }
  738. case 19: // Set Mauer Zeit
  739. if( 1 )
  740. {
  741. if( accountId )
  742. klient->sendeEncrypted( "\1", 1 );
  743. else
  744. {
  745. klient->sendeEncrypted( "\0", 1 );
  746. break;
  747. }
  748. int level = 0;
  749. klient->getNachrichtEncrypted( (char*)&level, 4 );
  750. int zeit = 0;
  751. klient->getNachrichtEncrypted( (char*)&zeit, 4 );
  752. appS->zDB()->setMauerZeit( accountId, level, zeit );
  753. klient->sendeEncrypted( "\1", 1 );
  754. break;
  755. }
  756. case 20: // Account erstellen
  757. if( 1 )
  758. {
  759. klient->sendeEncrypted( "\1", 1 );
  760. unsigned char len[ 4 ] = { 0, 0, 0, 0 };
  761. klient->getNachrichtEncrypted( (char*)len, 4 );
  762. char *acc_name = new char[ len[ 0 ] + 1 ];
  763. acc_name[ len[ 0 ] ] = 0;
  764. klient->getNachrichtEncrypted( acc_name, len[ 0 ] );
  765. char *acc_pass = new char[ len[ 1 ] + 1 ];
  766. acc_pass[ len[ 1 ] ] = 0;
  767. klient->getNachrichtEncrypted( acc_pass, len[ 1 ] );
  768. char *acc_geheim = new char[ len[ 2 ] + 1 ];
  769. acc_geheim[ len[ 2 ] ] = 0;
  770. klient->getNachrichtEncrypted( acc_geheim, len[ 2 ] );
  771. char *acc_mail = new char[ len[ 3 ] + 1 ];
  772. acc_mail[ len[ 3 ] ] = 0;
  773. klient->getNachrichtEncrypted( acc_mail, len[ 3 ] );
  774. unsigned short acc_geb_jahr = 0;
  775. klient->getNachrichtEncrypted( (char*)&acc_geb_jahr, 2 );
  776. char acc_geb_monat = 0;
  777. klient->getNachrichtEncrypted( &acc_geb_monat, 1 );
  778. char acc_geb_tag = 0;
  779. klient->getNachrichtEncrypted( &acc_geb_tag, 1 );
  780. int pres = appS->zDB()->pruefNeuAccount( acc_name, acc_mail );
  781. if( !pres )
  782. {
  783. Text *gebDatum = new Text( "" );
  784. gebDatum->append( (int)acc_geb_jahr );
  785. gebDatum->append( "-" );
  786. gebDatum->append( (int)acc_geb_monat );
  787. gebDatum->append( "-" );
  788. gebDatum->append( (int)acc_geb_tag );
  789. if( !appS->zDB()->neuAccount( acc_name, acc_pass, acc_geheim, acc_mail, gebDatum->getText(), appS->zIni() ) )
  790. klient->sendeEncrypted( "\0", 1 );
  791. else
  792. klient->sendeEncrypted( "\1", 1 );
  793. gebDatum->release();
  794. }
  795. else
  796. klient->sendeEncrypted( "\0", 1 );
  797. delete[]acc_name;
  798. delete[]acc_pass;
  799. delete[]acc_geheim;
  800. delete[]acc_mail;
  801. }
  802. break;
  803. case 21: // Account bestätigen
  804. if( 1 )
  805. {
  806. klient->sendeEncrypted( "\1", 1 );
  807. unsigned char len[ 2 ];
  808. klient->getNachrichtEncrypted( (char*)len, 2 );
  809. char *accName = new char[ len[ 0 ] + 1 ];
  810. accName[ len[ 0 ] ] = 0;
  811. klient->getNachrichtEncrypted( accName, len[ 0 ] );
  812. char *accPasswort = new char[ len[ 1 ] + 1 ];
  813. accPasswort[ len[ 1 ] ] = 0;
  814. klient->getNachrichtEncrypted( accPasswort, len[ 1 ] );
  815. char byte = appS->zDB()->suchBestaetigung( accName, accPasswort );
  816. if( !byte )
  817. klient->sendeEncrypted( "\0", 1 );
  818. else if( byte == -1 )
  819. klient->sendeEncrypted( "\0", 1 );
  820. else if( byte > 0 )
  821. {
  822. klient->sendeEncrypted( &byte, 1 );
  823. char befehl = 0;
  824. while( 1 )
  825. {
  826. klient->getNachrichtEncrypted( &befehl, 1 );
  827. if( !befehl )
  828. {
  829. if( byte == 1 )
  830. appS->zDB()->neuAccountAbbruch( accName );
  831. break;
  832. }
  833. if( befehl == -1 )
  834. break;
  835. if( befehl == -2 )
  836. {
  837. if( byte == 1 )
  838. appS->zDB()->sendeErstellEMail( accName, appS->zIni() );
  839. }
  840. if( befehl > 0 )
  841. {
  842. char *key = new char[ befehl + 1 ];
  843. key[ (int)befehl ] = 0;
  844. klient->getNachrichtEncrypted( key, befehl );
  845. if( byte == 1 )
  846. {
  847. if( !appS->zDB()->aktiviereAccount( accName, key ) )
  848. klient->sendeEncrypted( "\0", 1 );
  849. else
  850. {
  851. klient->sendeEncrypted( "\1", 1 );
  852. delete[]key;
  853. break;
  854. }
  855. }
  856. delete[]key;
  857. }
  858. }
  859. }
  860. delete[]accName;
  861. delete[]accPasswort;
  862. }
  863. break;
  864. case 22: // Trennen
  865. klient->sendeEncrypted( "\1", 1 );
  866. br = 1;
  867. break;
  868. case 23: // Mark Balls finish
  869. if( 1 )
  870. {
  871. klient->sendeEncrypted( "\1", 1 );
  872. int level = 0;
  873. klient->getNachrichtEncrypted( (char*)&level, 4 );
  874. int sek = 0;
  875. klient->getNachrichtEncrypted( (char*)&sek, 4 );
  876. if( accountId )
  877. appS->zDB()->markballsFinish( level, sek, accountId, 0, 0, 0, 0 );
  878. else
  879. {
  880. char l = 0;
  881. klient->getNachrichtEncrypted( &l, 1 );
  882. char *device = new char[ l + 1 ];
  883. device[ (int)l ] = 0;
  884. klient->getNachrichtEncrypted( device, l );
  885. appS->zDB()->markballsFinish( level, sek, accountId, device, 0, 0, 0 );
  886. delete[] device;
  887. }
  888. klient->sendeEncrypted( "\1", 1 );
  889. break;
  890. }
  891. case 24: // Get Mark Balls Score
  892. if( 1 )
  893. {
  894. klient->sendeEncrypted( "\1", 1 );
  895. Array< int > *level = new Array< int >();
  896. Array< int > *score = new Array< int >();
  897. RCArray< Text > *name = new RCArray< Text >();
  898. int anz = appS->zDB()->getMarkballsScore( level, score, name, 0 );
  899. klient->sendeEncrypted( (char*)&anz, 4 );
  900. for( int i = 0; i < anz; i++ )
  901. {
  902. int l = level->get( i );
  903. klient->sendeEncrypted( (char*)&l, 4 );
  904. int s = score->get( i );
  905. klient->sendeEncrypted( (char*)&s, 4 );
  906. char nl = (char)name->z( i )->getLength();
  907. klient->sendeEncrypted( &nl, 1 );
  908. if( nl )
  909. klient->sendeEncrypted( name->z( i )->getText(), nl );
  910. }
  911. level->release();
  912. score->release();
  913. name->release();
  914. break;
  915. }
  916. case 25: // Login Mark Balls
  917. if( 1 )
  918. {
  919. klient->sendeEncrypted( "\1", 1 );
  920. char l = 0;
  921. klient->getNachrichtEncrypted( &l, 1 );
  922. char *name = new char[ l + 1 ];
  923. name[ (int)l ] = 0;
  924. if( l )
  925. klient->getNachrichtEncrypted( name, l );
  926. klient->getNachrichtEncrypted( &l, 1 );
  927. char *passwort = new char[ l + 1 ];
  928. passwort[ (int)l ] = 0;
  929. if( l )
  930. klient->getNachrichtEncrypted( passwort, l );
  931. klient->getNachrichtEncrypted( &l, 1 );
  932. char *device = new char[ l + 1 ];
  933. device[ (int)l ] = 0;
  934. if( l )
  935. klient->getNachrichtEncrypted( device, l );
  936. accountId = appS->zDB()->login( name, passwort );
  937. delete[] name;
  938. delete[] passwort;
  939. if( accountId > 0 )
  940. {
  941. appS->zDB()->deviceAccount( device, accountId );
  942. klient->sendeEncrypted( "\1", 1 );
  943. }
  944. else
  945. klient->sende( "\0", 1 );
  946. delete[] device;
  947. break;
  948. }
  949. case 26: // Mark Balls finish NEW
  950. if( 1 )
  951. {
  952. klient->sendeEncrypted( "\1", 1 );
  953. int level = 0;
  954. klient->getNachrichtEncrypted( (char*)&level, 4 );
  955. int sek = 0;
  956. klient->getNachrichtEncrypted( (char*)&sek, 4 );
  957. int diff = 0;
  958. klient->getNachrichtEncrypted( (char*)&diff, 4 );
  959. int gId = 0;
  960. klient->getNachrichtEncrypted( (char*)&gId, 4 );
  961. if( accountId )
  962. {
  963. int kupfer = 0;
  964. klient->getNachrichtEncrypted( (char*)&kupfer, 4 );
  965. appS->zDB()->markballsFinish( level, sek, accountId, 0, diff, kupfer, gId );
  966. }
  967. else
  968. {
  969. char l = 0;
  970. klient->getNachrichtEncrypted( &l, 1 );
  971. char *device = new char[ l + 1 ];
  972. device[ (int)l ] = 0;
  973. klient->getNachrichtEncrypted( device, l );
  974. appS->zDB()->markballsFinish( level, sek, accountId, device, diff, 0, gId );
  975. delete[] device;
  976. }
  977. klient->sendeEncrypted( "\1", 1 );
  978. break;
  979. }
  980. case 27: // Get Mark Balls Score NEW
  981. if( 1 )
  982. {
  983. klient->sendeEncrypted( "\1", 1 );
  984. int diff = 0;
  985. klient->getNachrichtEncrypted( (char*)&diff, 4 );
  986. Array< int > *level = new Array< int >();
  987. Array< int > *score = new Array< int >();
  988. int anz = appS->zDB()->getMarkballsScore( level, score, diff );
  989. klient->sendeEncrypted( (char*)&anz, 4 );
  990. for( int i = 0; i < anz; i++ )
  991. {
  992. int l = level->get( i );
  993. klient->sendeEncrypted( (char*)&l, 4 );
  994. int s = score->get( i );
  995. klient->sendeEncrypted( (char*)&s, 4 );
  996. }
  997. level->release();
  998. score->release();
  999. break;
  1000. }
  1001. case 28: // Get gegenstand Liste
  1002. if( 1 )
  1003. {
  1004. klient->sendeEncrypted( "\1", 1 );
  1005. Array< int > *gId = new Array< int >();
  1006. Array< int > *diff = new Array< int >();
  1007. int anz = 0;
  1008. if( accountId )
  1009. anz = appS->zDB()->getGegenstaende( accountId, 0, gId, diff );
  1010. else
  1011. {
  1012. char l = 0;
  1013. klient->getNachrichtEncrypted( &l, 1 );
  1014. char *device = new char[ l + 1 ];
  1015. device[ (int)l ] = 0;
  1016. klient->getNachrichtEncrypted( device, l );
  1017. anz = appS->zDB()->getGegenstaende( 0, device, gId, diff );
  1018. delete[] device;
  1019. }
  1020. klient->sendeEncrypted( (char*)&anz, 4 );
  1021. for( int i = 0; i < anz; i++ )
  1022. {
  1023. int g = gId->get( i );
  1024. klient->sendeEncrypted( (char*)&g, 4 );
  1025. int d = diff->get( i );
  1026. klient->sendeEncrypted( (char*)&d, 4 );
  1027. }
  1028. gId->release();
  1029. diff->release();
  1030. break;
  1031. }
  1032. case 29: // Get Kupfer
  1033. if( 1 )
  1034. {
  1035. if( !accountId )
  1036. klient->sendeEncrypted( "\0", 1 );
  1037. else
  1038. {
  1039. klient->sendeEncrypted( "\1", 1 );
  1040. int ret = appS->zDB()->getKupfer( accountId );
  1041. klient->sendeEncrypted( (char*)&ret, 4 );
  1042. }
  1043. break;
  1044. }
  1045. case 30: // Get Markballs Fortschritt
  1046. if( 1 )
  1047. {
  1048. klient->sendeEncrypted( "\1", 1 );
  1049. Array< int > *fortschritt = new Array< int >();
  1050. int anz = 0;
  1051. if( accountId )
  1052. anz = appS->zDB()->getMarkballsFortschritt( accountId, 0, fortschritt );
  1053. else
  1054. {
  1055. char l = 0;
  1056. klient->getNachrichtEncrypted( &l, 1 );
  1057. char *device = new char[ l + 1 ];
  1058. device[ (int)l ] = 0;
  1059. klient->getNachrichtEncrypted( device, l );
  1060. anz = appS->zDB()->getMarkballsFortschritt( 0, device, fortschritt );
  1061. delete[] device;
  1062. }
  1063. klient->sendeEncrypted( (char*)&anz, 4 );
  1064. for( int i = 0; i < anz; i++ )
  1065. {
  1066. int f = fortschritt->get( i );
  1067. klient->sendeEncrypted( (char*)&f, 4 );
  1068. }
  1069. fortschritt->release();
  1070. break;
  1071. }
  1072. case 31: // Global Login
  1073. {
  1074. klient->sendeEncrypted( "\1", 1 );
  1075. char l = 0;
  1076. klient->getNachrichtEncrypted( &l, 1 );
  1077. char *name = new char[ l + 1 ];
  1078. name[ (int)l ] = 0;
  1079. if( l )
  1080. klient->getNachrichtEncrypted( name, l );
  1081. klient->getNachrichtEncrypted( &l, 1 );
  1082. char *passwort = new char[ l + 1 ];
  1083. passwort[ (int)l ] = 0;
  1084. if( l )
  1085. klient->getNachrichtEncrypted( passwort, l );
  1086. klient->getNachrichtEncrypted( &l, 1 );
  1087. char *device = new char[ l + 1 ];
  1088. device[ (int)l ] = 0;
  1089. if( l )
  1090. klient->getNachrichtEncrypted( device, l );
  1091. accountId = appS->zDB()->login( name, passwort );
  1092. delete[] name;
  1093. delete[] passwort;
  1094. if( accountId > 0 )
  1095. klient->sendeEncrypted( "\1", 1 );
  1096. else
  1097. klient->sende( "\0", 1 );
  1098. delete[] device;
  1099. break;
  1100. }
  1101. case 32: // Curvesnake finish Game
  1102. {
  1103. klient->sendeEncrypted( "\1", 1 );
  1104. int score = 0;
  1105. klient->getNachrichtEncrypted( (char*)&score, 4 );
  1106. int map = 0;
  1107. klient->getNachrichtEncrypted( (char*)&map, 4 );
  1108. if( accountId )
  1109. {
  1110. int kupfer = 0;
  1111. klient->getNachrichtEncrypted( (char*)&kupfer, 4 );
  1112. klient->getNachrichtEncrypted( (char*)&kupfer, 4 );
  1113. appS->zDB()->curvesnakeFinish( score, accountId, 0, map, kupfer );
  1114. }
  1115. else
  1116. {
  1117. char l = 0;
  1118. klient->getNachrichtEncrypted( &l, 1 );
  1119. char *device = new char[ l + 1 ];
  1120. device[ (int)l ] = 0;
  1121. klient->getNachrichtEncrypted( device, l );
  1122. appS->zDB()->curvesnakeFinish( score, accountId, device, map, 0 );
  1123. delete[] device;
  1124. }
  1125. klient->sendeEncrypted( "\1", 1 );
  1126. break;
  1127. }
  1128. case 33: // Curvesnake best score
  1129. {
  1130. klient->sendeEncrypted( "\1", 1 );
  1131. Array< int > *map = new Array< int >();
  1132. Array< int > *score = new Array< int >();
  1133. int anz = appS->zDB()->getCurvesnakeScore( map, score );
  1134. klient->sendeEncrypted( (char*)&anz, 4 );
  1135. for( int i = 0; i < anz; i++ )
  1136. {
  1137. int l = map->get( i );
  1138. klient->sendeEncrypted( (char*)&l, 4 );
  1139. int s = score->get( i );
  1140. klient->sendeEncrypted( (char*)&s, 4 );
  1141. }
  1142. map->release();
  1143. score->release();
  1144. break;
  1145. }
  1146. case 34: // Curvesnake get Map List
  1147. {
  1148. klient->sendeEncrypted( "\1", 1 );
  1149. Array< int > *map = new Array< int >();
  1150. int anz = appS->zDB()->getCurvesnakeMapList( map, accountId );
  1151. klient->sendeEncrypted( (char*)&anz, 4 );
  1152. for( int i = 0; i < anz; i++ )
  1153. {
  1154. int l = map->get( i );
  1155. klient->sendeEncrypted( (char*)&l, 4 );
  1156. }
  1157. map->release();
  1158. break;
  1159. }
  1160. case 35: // Add Kupfer
  1161. {
  1162. if( accountId )
  1163. {
  1164. klient->sendeEncrypted( "\1", 1 );
  1165. int kupfer = 0;
  1166. klient->getNachrichtEncrypted( (char*)&kupfer, 4 );
  1167. appS->zDB()->addKupfer( accountId, kupfer );
  1168. }
  1169. else
  1170. klient->sendeEncrypted( "\0", 1 );
  1171. break;
  1172. }
  1173. case 36: // getFreeAds
  1174. {
  1175. if( accountId )
  1176. {
  1177. if( appS->zDB()->getFreeAds( accountId ) )
  1178. klient->sendeEncrypted( "\1", 1 );
  1179. else
  1180. klient->sendeEncrypted( "\0", 1 );
  1181. }
  1182. else
  1183. klient->sendeEncrypted( "\0", 1 );
  1184. break;
  1185. }
  1186. case 37: // keppAlive
  1187. {
  1188. klient->sendeEncrypted( "\1", 1 );
  1189. break;
  1190. }
  1191. default:
  1192. klient->sendeEncrypted( "\0", 1 );
  1193. br = true;
  1194. break;
  1195. }
  1196. if( br )
  1197. break;
  1198. appS->addEmpfangen( klient->getDownloadBytes( 1 ) );
  1199. appS->addGesendet( klient->getUploadBytes( 1 ) );
  1200. }
  1201. }
  1202. appS->addEmpfangen( klient->getDownloadBytes( 1 ) );
  1203. appS->addGesendet( klient->getUploadBytes( 1 ) );
  1204. delete this;
  1205. }
  1206. void AppSKlient::timeout()
  1207. {
  1208. klient->trenne();
  1209. }
  1210. void AppSKlient::errorZuKlient( const char *nachricht ) const // sendet eine Fehlernachricht zum Klient
  1211. {
  1212. klient->sendeEncrypted( "\3", 1 );
  1213. char len = (char)textLength( nachricht );
  1214. klient->sendeEncrypted( &len, 1 );
  1215. klient->sendeEncrypted( nachricht, len );
  1216. }
  1217. // Inhalt der AppSKTimeOut Klasse aus AppServer.h
  1218. // Konstruktor
  1219. AppSKTimeOut::AppSKTimeOut( AppSKlient *zK )
  1220. {
  1221. zKlient = zK;
  1222. zm = new ZeitMesser();
  1223. zm->messungStart();
  1224. st = 0;
  1225. start();
  1226. }
  1227. // Destruktor
  1228. AppSKTimeOut::~AppSKTimeOut()
  1229. {
  1230. zm->release();
  1231. }
  1232. // nicht constant
  1233. void AppSKTimeOut::thread()
  1234. {
  1235. while( !st )
  1236. {
  1237. zm->messungEnde();
  1238. if( zm->getSekunden() > 10 )
  1239. zKlient->timeout();
  1240. for( int i = 0; i < 300 && !st; i++ )
  1241. Sleep( 100 );
  1242. }
  1243. }
  1244. void AppSKTimeOut::stop()
  1245. {
  1246. st = 1;
  1247. }
  1248. void AppSKTimeOut::addConnect()
  1249. {
  1250. zm->messungStart();
  1251. }