SpielStatistik.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. #include "SpielStatistik.h"
  2. #include "../Initialisierung/Initialisierung.h"
  3. #include <MausEreignis.h>
  4. #include <Punkt.h>
  5. // Inhalt der SpielStatistik Klasse aus SpielStatistik.h
  6. // Konstruktor
  7. SpielStatistik::SpielStatistik()
  8. {
  9. InitializeCriticalSection( &cs );
  10. addNachrichtF = 0;
  11. addChatF = 0;
  12. addFreundF = 0;
  13. accountAnsehenF = 0;
  14. istFreundF = 0;
  15. nachrichtParam = 0;
  16. bilder = 0;
  17. schrift = 0;
  18. screen = 0;
  19. infoc = 0;
  20. spielc = 0;
  21. status = 0;
  22. gss = new Array< SSDGesamtSpieler* >();
  23. gts = new Array< SSDGesamtTeam* >();
  24. rs = new Array< SSDRunde* >();
  25. chat = 0;
  26. runde = 0;
  27. tabelle = 0;
  28. accountId = 0;
  29. rend = 0;
  30. ref = 1;
  31. }
  32. // Destruktor
  33. SpielStatistik::~SpielStatistik()
  34. {
  35. if( chat )
  36. chat->release();
  37. if( runde )
  38. runde->release();
  39. if( tabelle )
  40. tabelle->release();
  41. if( schrift )
  42. schrift->release();
  43. if( screen )
  44. screen->release();
  45. if( infoc )
  46. infoc->release();
  47. if( spielc )
  48. spielc->release();
  49. int anz = gss->getEintragAnzahl();
  50. for( int i = 0; i < anz; i++ )
  51. {
  52. if( gss->hat( i ) )
  53. delete gss->get( i );
  54. }
  55. gss->release();
  56. anz = gts->getEintragAnzahl();
  57. for( int i = 0; i < anz; i++ )
  58. {
  59. if( gts->hat( i ) )
  60. delete gts->get( i );
  61. }
  62. gts->release();
  63. anz = rs->getEintragAnzahl();
  64. for( int i = 0; i < anz; i++ )
  65. {
  66. if( rs->hat( i ) )
  67. {
  68. int anz2 = rs->get( i )->spieler.getEintragAnzahl();
  69. for( int j = 0; j < anz2; j++ )
  70. {
  71. if( rs->get( i )->spieler.hat( j ) )
  72. delete rs->get( i )->spieler.get( j );
  73. }
  74. anz2 = rs->get( i )->teams.getEintragAnzahl();
  75. for( int j = 0; j < anz2; j++ )
  76. {
  77. if( rs->get( i )->teams.hat( j ) )
  78. delete rs->get( i )->teams.get( j );
  79. }
  80. delete rs->get( i );
  81. }
  82. }
  83. rs->release();
  84. DeleteCriticalSection( &cs );
  85. }
  86. // nicht constant
  87. void SpielStatistik::lock()
  88. {
  89. EnterCriticalSection( &cs );
  90. }
  91. void SpielStatistik::unlock()
  92. {
  93. LeaveCriticalSection( &cs );
  94. }
  95. void SpielStatistik::bereit()
  96. {
  97. char byte = 1;
  98. spielc->statistikNachricht( 1, &byte );
  99. }
  100. void SpielStatistik::setBilder( BilderV *b )
  101. {
  102. bilder = b;
  103. }
  104. void SpielStatistik::setAccountId( int id )
  105. {
  106. accountId = id;
  107. }
  108. void SpielStatistik::setRückrufFunktionen( void( *addNachrichtF )( void *, Text *, Text *, Text *, Text *, char, void * ),
  109. void( *addChatF )( void *, int ), void( *addFreundF )( void *, int ),
  110. void( *accountAnsehenF )( void *, int ), bool( *istFreundF )( void *, int ), void *nachrichtParam )
  111. {
  112. this->addNachrichtF = addNachrichtF;
  113. this->addChatF = addChatF;
  114. this->addFreundF = addFreundF;
  115. this->accountAnsehenF = accountAnsehenF;
  116. this->istFreundF = istFreundF;
  117. this->nachrichtParam = nachrichtParam;
  118. }
  119. void SpielStatistik::setSchrift( Schrift *schrift )
  120. {
  121. if( this->schrift )
  122. this->schrift->release();
  123. this->schrift = schrift;
  124. }
  125. void SpielStatistik::setBildschirm( Bildschirm *screen )
  126. {
  127. if( this->screen )
  128. this->screen->release();
  129. this->screen = screen;
  130. }
  131. void SpielStatistik::setKlients( InformationKlientV *infoc, SpielKlientV *spielc )
  132. {
  133. if( this->infoc )
  134. this->infoc->release();
  135. if( this->spielc )
  136. this->spielc->release();
  137. this->infoc = infoc;
  138. this->spielc = spielc;
  139. }
  140. void SpielStatistik::nachricht( int län, char *bytes )
  141. {
  142. lock();
  143. char num = *bytes;
  144. bytes++;
  145. län--;
  146. switch( num )
  147. {
  148. case 1: // Ladevorgang abgeschlossen
  149. if( 1 )
  150. {
  151. chat = new StatistikChat( accountId, spielc->getThis(), infoc->getThis(), schrift->getThis(), bilder, addNachrichtF, addChatF, addFreundF, accountAnsehenF, nachrichtParam );
  152. int anz = gss->getEintragAnzahl();
  153. for( int i = 0; i < anz; i++ )
  154. {
  155. if( gss->hat( i ) )
  156. {
  157. int acc = infoc->getAccountId( gss->get( i )->spielerName );
  158. if( acc )
  159. chat->addSpieler( acc, istFreundF( nachrichtParam, acc ) || acc == accountId );
  160. }
  161. }
  162. runde = new StatistikRunde( schrift, rs->getThis() );
  163. tabelle = new StatistikTabelle( gss->getThis(), gts->getThis(), rs->getThis(), schrift, screen );
  164. status = 1;
  165. }
  166. break;
  167. case 2: // Gesammt Spieler Statistik
  168. if( 1 )
  169. {
  170. SSDGesamtSpieler *ssdgs = new SSDGesamtSpieler();
  171. ssdgs->spielerNummer = *(int*)bytes;
  172. bytes += 4;
  173. län -= 4;
  174. char snlän = *bytes;
  175. län--;
  176. bytes++;
  177. char *txt = new char[ snlän + 1 ];
  178. txt[ snlän ] = 0;
  179. for( int i = 0; i < snlän; i++ )
  180. txt[ i ] = bytes[ i ];
  181. bytes += snlän;
  182. län -= snlän;
  183. ssdgs->spielerName = txt;
  184. delete[] txt;
  185. char tnlän = *bytes;
  186. län--;
  187. bytes++;
  188. txt = new char[ tnlän + 1 ];
  189. txt[ tnlän ] = 0;
  190. for( int i = 0; i < tnlän; i++ )
  191. txt[ i ] = bytes[ i ];
  192. bytes += tnlän;
  193. län -= tnlän;
  194. ssdgs->teamName = txt;
  195. delete[] txt;
  196. ssdgs->spielerFarbe = *(int*)bytes;
  197. bytes += 4;
  198. län -= 4;
  199. ssdgs->teamFarbe = *(int*)bytes;
  200. bytes += 4;
  201. län -= 4;
  202. ssdgs->gewonneneRunden = *(int*)bytes;
  203. bytes += 4;
  204. län -= 4;
  205. ssdgs->punkte = *(int*)bytes;
  206. bytes += 4;
  207. län -= 4;
  208. ssdgs->linienLänge = *(int*)bytes;
  209. bytes += 4;
  210. län -= 4;
  211. ssdgs->zeitAmLeben = *(int*)bytes;
  212. bytes += 4;
  213. län -= 4;
  214. ssdgs->zeitTod = *(int*)bytes;
  215. bytes += 4;
  216. län -= 4;
  217. ssdgs->kills = *(int*)bytes;
  218. bytes += 4;
  219. län -= 4;
  220. ssdgs->tode = *(int*)bytes;
  221. bytes += 4;
  222. län -= 4;
  223. gss->add( ssdgs );
  224. }
  225. break;
  226. case 3: // Gesammt Team Statistik
  227. if( 1 )
  228. {
  229. SSDGesamtTeam *ssdgt = new SSDGesamtTeam();
  230. ssdgt->teamNummer = *(int*)bytes;
  231. bytes += 4;
  232. län -= 4;
  233. char tnlän = *bytes;
  234. län--;
  235. bytes++;
  236. char *txt = new char[ tnlän + 1 ];
  237. txt[ tnlän ] = 0;
  238. for( int i = 0; i < tnlän; i++ )
  239. txt[ i ] = bytes[ i ];
  240. bytes += tnlän;
  241. län -= tnlän;
  242. ssdgt->teamName = txt;
  243. delete[] txt;
  244. ssdgt->teamFarbe = *(int*)bytes;
  245. bytes += 4;
  246. län -= 4;
  247. ssdgt->gewonneneRunden = *(int*)bytes;
  248. bytes += 4;
  249. län -= 4;
  250. ssdgt->punkte = *(int*)bytes;
  251. bytes += 4;
  252. län -= 4;
  253. ssdgt->linienLänge = *(int*)bytes;
  254. bytes += 4;
  255. län -= 4;
  256. ssdgt->kills = *(int*)bytes;
  257. bytes += 4;
  258. län -= 4;
  259. ssdgt->tode = *(int*)bytes;
  260. bytes += 4;
  261. län -= 4;
  262. gts->add( ssdgt );
  263. }
  264. break;
  265. case 4: // Runde Statistik
  266. if( 1 )
  267. {
  268. SSDRunde *ssdr = new SSDRunde();
  269. ssdr->rundenNummer = *(int*)bytes;
  270. bytes += 4;
  271. län -= 4;
  272. ssdr->rundenDauer = *(int*)bytes;
  273. bytes += 4;
  274. län -= 4;
  275. ssdr->rundenSieger = *(int*)bytes;
  276. bytes += 4;
  277. län -= 4;
  278. ssdr->spielfeldNutzung = *(int*)bytes;
  279. bytes += 4;
  280. län -= 4;
  281. int breite = *(int*)bytes;
  282. bytes += 4;
  283. län -= 4;
  284. int höhe = *(int*)bytes;
  285. bytes += 4;
  286. län -= 4;
  287. ssdr->rundenBild.neuBild( breite, höhe, 0 );
  288. rs->add( ssdr );
  289. }
  290. break;
  291. case 5: // Runde Bild
  292. if( 1 )
  293. {
  294. int runde = *(int*)bytes;
  295. bytes += 4;
  296. län -= 4;
  297. int *buff = 0;
  298. int max = 0;
  299. int anz = rs->getEintragAnzahl();
  300. for( int i = 0; i < anz; i++ )
  301. {
  302. if( rs->hat( i ) && rs->get( i )->rundenNummer == runde )
  303. {
  304. buff = rs->get( i )->rundenBild.getBuffer();
  305. max = rs->get( i )->rundenBild.getBreite() * rs->get( i )->rundenBild.getHeight();
  306. break;
  307. }
  308. }
  309. if( !buff )
  310. break;
  311. int index = *(int*)bytes;
  312. bytes += 4;
  313. län -= 4;
  314. int plän = *(int*)bytes;
  315. bytes += 4;
  316. län -= 4;
  317. for( int i = 0; i < plän; i++ )
  318. {
  319. if( index + i >= max )
  320. break;
  321. buff[ index + i ] = *(int*)bytes;
  322. bytes += 4;
  323. län -= 4;
  324. }
  325. }
  326. break;
  327. case 6: // Runde Spieler Statistik
  328. if( 1 )
  329. {
  330. int runde = *(int*)bytes;
  331. bytes += 4;
  332. län -= 4;
  333. int anz = rs->getEintragAnzahl();
  334. for( int i = 0; i < anz; i++ )
  335. {
  336. if( rs->hat( i ) && rs->get( i )->rundenNummer == runde )
  337. {
  338. SSDRundeSpieler *ssdrs = new SSDRundeSpieler();
  339. ssdrs->spielerNummer = *(int*)bytes;
  340. bytes += 4;
  341. län -= 4;
  342. char snlän = *bytes;
  343. län--;
  344. bytes++;
  345. char *txt = new char[ snlän + 1 ];
  346. txt[ snlän ] = 0;
  347. for( int j = 0; j < snlän; j++ )
  348. txt[ j ] = bytes[ j ];
  349. bytes += snlän;
  350. län -= snlän;
  351. ssdrs->spielerName = txt;
  352. delete[] txt;
  353. char tnlän = *bytes;
  354. län--;
  355. bytes++;
  356. txt = new char[ tnlän + 1 ];
  357. txt[ tnlän ] = 0;
  358. for( int j = 0; j < tnlän; j++ )
  359. txt[ j ] = bytes[ j ];
  360. bytes += tnlän;
  361. län -= tnlän;
  362. ssdrs->teamName = txt;
  363. delete[] txt;
  364. ssdrs->spielerFarbe = *(int*)bytes;
  365. bytes += 4;
  366. län -= 4;
  367. ssdrs->teamFarbe = *(int*)bytes;
  368. bytes += 4;
  369. län -= 4;
  370. ssdrs->linienLänge = *(int*)bytes;
  371. bytes += 4;
  372. län -= 4;
  373. ssdrs->kills = *(int*)bytes;
  374. bytes += 4;
  375. län -= 4;
  376. ssdrs->todesZeit = *(int*)bytes;
  377. bytes += 4;
  378. län -= 4;
  379. rs->get( i )->spieler.add( ssdrs );
  380. break;
  381. }
  382. }
  383. }
  384. break;
  385. case 7: // Runde Team Statistik
  386. if( 1 )
  387. {
  388. int runde = *(int*)bytes;
  389. bytes += 4;
  390. län -= 4;
  391. int anz = rs->getEintragAnzahl();
  392. for( int i = 0; i < anz; i++ )
  393. {
  394. if( rs->hat( i ) && rs->get( i )->rundenNummer == runde )
  395. {
  396. SSDRundeTeam *ssdrt = new SSDRundeTeam();
  397. ssdrt->teamNummer = *(int*)bytes;
  398. bytes += 4;
  399. län -= 4;
  400. char tnlän = *bytes;
  401. län--;
  402. bytes++;
  403. char *txt = new char[ tnlän + 1 ];
  404. txt[ tnlän ] = 0;
  405. for( int j = 0; j < tnlän; j++ )
  406. txt[ j ] = bytes[ j ];
  407. bytes += tnlän;
  408. län -= tnlän;
  409. ssdrt->teamName = txt;
  410. delete[] txt;
  411. ssdrt->teamFarbe = *(int*)bytes;
  412. bytes += 4;
  413. län -= 4;
  414. ssdrt->ergebnis = *bytes;
  415. bytes++;
  416. län--;
  417. ssdrt->linienLänge = *(int*)bytes;
  418. bytes += 4;
  419. län -= 4;
  420. ssdrt->kills = *(int*)bytes;
  421. bytes += 4;
  422. län -= 4;
  423. ssdrt->tode = *(int*)bytes;
  424. bytes += 4;
  425. län -= 4;
  426. rs->get( i )->teams.add( ssdrt );
  427. break;
  428. }
  429. }
  430. }
  431. break;
  432. case 8: // Chat Nachricht
  433. if( 1 )
  434. {
  435. int vonAccount = *(int*)bytes;
  436. bytes += 4;
  437. län -= 4;
  438. char *txt = new char[ län + 1 ];
  439. txt[ län ] = 0;
  440. for( int i = 0; i < län; i++ )
  441. txt[ i ] = bytes[ i ];
  442. if( status == 1 )
  443. chat->addChatNachricht( vonAccount, txt );
  444. delete[] txt;
  445. }
  446. break;
  447. case 9: // Spieler hat verlassen
  448. if( 1 )
  449. {
  450. int acc = *(int*)bytes;
  451. bytes += 4;
  452. län -= 4;
  453. if( status == 1 )
  454. chat->spielerOffline( acc );
  455. }
  456. break;
  457. default:
  458. // Fehler
  459. break;
  460. }
  461. if( län != 0 )
  462. {
  463. // Fehler
  464. }
  465. unlock();
  466. }
  467. void SpielStatistik::doMausEreignis( MausEreignis &me )
  468. {
  469. if( !status )
  470. return;
  471. lock();
  472. if( status == 1 )
  473. {
  474. tabelle->doMausEreignis( me );
  475. runde->doMausEreignis( me );
  476. chat->doMausEreignis( me );
  477. }
  478. unlock();
  479. }
  480. void SpielStatistik::doTastaturEreignis( TastaturEreignis &te )
  481. {
  482. if( !status )
  483. return;
  484. lock();
  485. if( status == 1 )
  486. chat->doTastaturEreignis( te );
  487. unlock();
  488. }
  489. bool SpielStatistik::tick( double zeit )
  490. {
  491. if( !status )
  492. return 0;
  493. lock();
  494. if( status == 1 )
  495. {
  496. rend |= chat->tick( zeit );
  497. rend |= runde->tick( zeit );
  498. rend |= tabelle->tick( zeit );
  499. if( ( chat->hatVerlassen() || runde->mussVerlassen() ) && status == 1 )
  500. {
  501. spielc->trenne();
  502. status = 2;
  503. }
  504. runde->setRunde( tabelle->getRunde() );
  505. }
  506. bool ret = rend;
  507. rend = 0;
  508. unlock();
  509. return ret;
  510. }
  511. void SpielStatistik::render( Bild &zRObj )
  512. {
  513. if( !status )
  514. return;
  515. lock();
  516. chat->render( zRObj );
  517. runde->render( zRObj );
  518. tabelle->render( zRObj );
  519. unlock();
  520. }
  521. void SpielStatistik::verlassen()
  522. {
  523. if( spielc )
  524. spielc->trenne();
  525. status = 2;
  526. }
  527. // constant
  528. int SpielStatistik::getStatus() const // 0 = laden, 1 = läuft, 2 = fortsetzen
  529. {
  530. return status;
  531. }
  532. // Reference Counting
  533. SpielStatistikV *SpielStatistik::getThis()
  534. {
  535. ref++;
  536. return this;
  537. }
  538. SpielStatistikV *SpielStatistik::release()
  539. {
  540. ref--;
  541. if( !ref )
  542. delete this;
  543. return 0;
  544. }