SpielStatistik.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. : ReferenceCounter()
  9. {
  10. InitializeCriticalSection( &cs );
  11. addNachrichtF = 0;
  12. addChatF = 0;
  13. addFreundF = 0;
  14. accountAnsehenF = 0;
  15. istFreundF = 0;
  16. nachrichtParam = 0;
  17. bilder = 0;
  18. infoc = 0;
  19. spielc = 0;
  20. status = 0;
  21. gss = new Array< SSDSpieler * >();
  22. gts = new Array< SSDTeam * >();
  23. chat = 0;
  24. tabelle = 0;
  25. accountId = 0;
  26. rend = 0;
  27. }
  28. // Destruktor
  29. SpielStatistik::~SpielStatistik()
  30. {
  31. if( chat )
  32. chat->release();
  33. if( tabelle )
  34. tabelle->release();
  35. if( infoc )
  36. infoc->release();
  37. if( spielc )
  38. spielc->release();
  39. int anz = gss->getEintragAnzahl();
  40. for( int i = 0; i < anz; i++ )
  41. {
  42. if( gss->hat( i ) )
  43. delete gss->get( i );
  44. }
  45. gss->release();
  46. anz = gts->getEintragAnzahl();
  47. for( int i = 0; i < anz; i++ )
  48. {
  49. if( gts->hat( i ) )
  50. delete gts->get( i );
  51. }
  52. gts->release();
  53. DeleteCriticalSection( &cs );
  54. }
  55. // nicht constant
  56. void SpielStatistik::lock()
  57. {
  58. EnterCriticalSection( &cs );
  59. }
  60. void SpielStatistik::unlock()
  61. {
  62. LeaveCriticalSection( &cs );
  63. }
  64. void SpielStatistik::bereit()
  65. {
  66. char byte = 0;
  67. spielc->statistikNachricht( 1, &byte );
  68. }
  69. void SpielStatistik::setBilder( BilderV *b )
  70. {
  71. bilder = b;
  72. }
  73. void SpielStatistik::setAccountId( int id )
  74. {
  75. accountId = id;
  76. }
  77. void SpielStatistik::setRückrufFunktionen( void( *addNachrichtF )( void *, Text *, Text *, Text *, Text * ),
  78. void( *addChatF )( void *, int ), void( *addFreundF )( void *, int ),
  79. void( *accountAnsehenF )( void *, int ), bool( *istFreundF )( void *, int ), void *nachrichtParam )
  80. {
  81. this->addNachrichtF = addNachrichtF;
  82. this->addChatF = addChatF;
  83. this->addFreundF = addFreundF;
  84. this->accountAnsehenF = accountAnsehenF;
  85. this->istFreundF = istFreundF;
  86. this->nachrichtParam = nachrichtParam;
  87. }
  88. void SpielStatistik::setUIFactory( UIInit &uiFactory )
  89. {
  90. this->uiFactory = uiFactory;
  91. }
  92. void SpielStatistik::setKlients( KSGClient::InformationServerClient *infoc, KSGClient::SpielServerClient *spielc )
  93. {
  94. if( this->infoc )
  95. this->infoc->release();
  96. if( this->spielc )
  97. this->spielc->release();
  98. this->infoc = infoc;
  99. this->spielc = spielc;
  100. }
  101. void SpielStatistik::nachricht( int län, char *bytes )
  102. {
  103. lock();
  104. char num = *bytes;
  105. bytes++;
  106. län--;
  107. switch( num )
  108. {
  109. case 0: // Spieler Statistik
  110. if( 1 )
  111. {
  112. SSDSpieler *ssdgs = new SSDSpieler();
  113. ssdgs->spielerNummer = *(int *)bytes;
  114. bytes += 4;
  115. län -= 4;
  116. char snlän = *bytes;
  117. län--;
  118. bytes++;
  119. char *txt = new char[ snlän + 1 ];
  120. txt[ snlän ] = 0;
  121. for( int i = 0; i < snlän; i++ )
  122. txt[ i ] = bytes[ i ];
  123. bytes += snlän;
  124. län -= snlän;
  125. ssdgs->spielerName = txt;
  126. delete[] txt;
  127. char tnlän = *bytes;
  128. län--;
  129. bytes++;
  130. txt = new char[ tnlän + 1 ];
  131. txt[ tnlän ] = 0;
  132. for( int i = 0; i < tnlän; i++ )
  133. txt[ i ] = bytes[ i ];
  134. bytes += tnlän;
  135. län -= tnlän;
  136. ssdgs->teamName = txt;
  137. delete[] txt;
  138. ssdgs->spielerFarbe = *(int *)bytes;
  139. bytes += 4;
  140. län -= 4;
  141. ssdgs->teamFarbe = *(int *)bytes;
  142. bytes += 4;
  143. län -= 4;
  144. ssdgs->schadenBekommen = *(int *)bytes;
  145. bytes += 4;
  146. län -= 4;
  147. ssdgs->schadenGemacht = *(int *)bytes;
  148. bytes += 4;
  149. län -= 4;
  150. ssdgs->treibstoffVerbraucht = *(int *)bytes;
  151. bytes += 4;
  152. län -= 4;
  153. ssdgs->schüsse = *(int *)bytes;
  154. bytes += 4;
  155. län -= 4;
  156. ssdgs->treffer = *(int *)bytes;
  157. bytes += 4;
  158. län -= 4;
  159. ssdgs->punkte = *(int *)bytes;
  160. bytes += 4;
  161. län -= 4;
  162. ssdgs->kills = *(int *)bytes;
  163. bytes += 4;
  164. län -= 4;
  165. ssdgs->tode = *(int *)bytes;
  166. bytes += 4;
  167. län -= 4;
  168. ssdgs->zeitAmLeben = *(int *)bytes;
  169. bytes += 4;
  170. län -= 4;
  171. ssdgs->zeitTod = *(int *)bytes;
  172. bytes += 4;
  173. län -= 4;
  174. gss->add( ssdgs );
  175. }
  176. break;
  177. case 1: // Team Statistik
  178. if( 1 )
  179. {
  180. SSDTeam *ssdgt = new SSDTeam();
  181. ssdgt->teamNummer = *(int *)bytes;
  182. bytes += 4;
  183. län -= 4;
  184. char tnlän = *bytes;
  185. län--;
  186. bytes++;
  187. char *txt = new char[ tnlän + 1 ];
  188. txt[ tnlän ] = 0;
  189. for( int i = 0; i < tnlän; i++ )
  190. txt[ i ] = bytes[ i ];
  191. bytes += tnlän;
  192. län -= tnlän;
  193. ssdgt->teamName = txt;
  194. delete[] txt;
  195. ssdgt->teamFarbe = *(int *)bytes;
  196. bytes += 4;
  197. län -= 4;
  198. ssdgt->schadenBekommen = *(int *)bytes;
  199. bytes += 4;
  200. län -= 4;
  201. ssdgt->schadenGemacht = *(int *)bytes;
  202. bytes += 4;
  203. län -= 4;
  204. ssdgt->treibstoffVerbraucht = *(int *)bytes;
  205. bytes += 4;
  206. län -= 4;
  207. ssdgt->schüsse = *(int *)bytes;
  208. bytes += 4;
  209. län -= 4;
  210. ssdgt->treffer = *(int *)bytes;
  211. bytes += 4;
  212. län -= 4;
  213. ssdgt->punkte = *(int *)bytes;
  214. bytes += 4;
  215. län -= 4;
  216. ssdgt->kills = *(int *)bytes;
  217. bytes += 4;
  218. län -= 4;
  219. ssdgt->tode = *(int *)bytes;
  220. bytes += 4;
  221. län -= 4;
  222. ssdgt->gewonnen = *bytes != 0;
  223. bytes++;
  224. län--;
  225. gts->add( ssdgt );
  226. }
  227. break;
  228. case 2: // Ladevorgang abgeschlossen
  229. if( 1 )
  230. {
  231. chat = new StatistikChat( accountId, dynamic_cast<KSGClient::SpielServerClient *>( spielc->getThis() ), dynamic_cast<KSGClient::InformationServerClient *>( infoc->getThis() ), uiFactory, bilder, addNachrichtF, addChatF, addFreundF, accountAnsehenF, nachrichtParam );
  232. int anz = gss->getEintragAnzahl();
  233. for( int i = 0; i < anz; i++ )
  234. {
  235. if( gss->hat( i ) )
  236. {
  237. int acc = infoc->getAccountId( gss->get( i )->spielerName );
  238. if( acc )
  239. chat->addSpieler( acc, istFreundF( nachrichtParam, acc ) || acc == accountId );
  240. }
  241. }
  242. tabelle = new StatistikTabelle( dynamic_cast<Array<SSDSpieler *> *>( gss->getThis() ), dynamic_cast<Array<SSDTeam *> *>( gts->getThis() ), uiFactory );
  243. status = 1;
  244. }
  245. break;
  246. case 3: // Chat Nachricht
  247. if( 1 )
  248. {
  249. int vonAccount = *(int *)bytes;
  250. bytes += 4;
  251. län -= 4;
  252. char *txt = new char[ län + 1 ];
  253. txt[ län ] = 0;
  254. for( int i = 0; i < län; i++ )
  255. txt[ i ] = bytes[ i ];
  256. if( status == 1 )
  257. chat->addChatNachricht( vonAccount, txt );
  258. delete[] txt;
  259. }
  260. break;
  261. case 4: // Spieler hat verlassen
  262. if( 1 )
  263. {
  264. int acc = *(int *)bytes;
  265. bytes += 4;
  266. län -= 4;
  267. if( status == 1 )
  268. chat->spielerOffline( acc );
  269. }
  270. break;
  271. default:
  272. // Fehler
  273. break;
  274. }
  275. if( län != 0 )
  276. {
  277. // Fehler
  278. }
  279. unlock();
  280. }
  281. void SpielStatistik::doPublicMausEreignis( MausEreignis &me )
  282. {
  283. if( !status )
  284. return;
  285. lock();
  286. if( status == 1 )
  287. {
  288. tabelle->doPublicMausEreignis( me );
  289. chat->doPublicMausEreignis( me );
  290. }
  291. unlock();
  292. }
  293. void SpielStatistik::doTastaturEreignis( TastaturEreignis &te )
  294. {
  295. if( !status )
  296. return;
  297. lock();
  298. if( status == 1 )
  299. chat->doTastaturEreignis( te );
  300. unlock();
  301. }
  302. bool SpielStatistik::tick( double zeit )
  303. {
  304. if( !status )
  305. return 0;
  306. lock();
  307. if( status == 1 )
  308. {
  309. rend |= chat->tick( zeit );
  310. rend |= tabelle->tick( zeit );
  311. if( chat->hatVerlassen() && status == 1 )
  312. {
  313. spielc->trenne();
  314. status = 2;
  315. }
  316. }
  317. bool ret = rend;
  318. rend = 0;
  319. unlock();
  320. return ret;
  321. }
  322. void SpielStatistik::render( Bild &zRObj )
  323. {
  324. if( !status )
  325. return;
  326. lock();
  327. chat->render( zRObj );
  328. tabelle->render( zRObj );
  329. unlock();
  330. }
  331. void SpielStatistik::verlassen()
  332. {
  333. if( spielc )
  334. spielc->trenne();
  335. status = 2;
  336. }
  337. // constant
  338. int SpielStatistik::getStatus() const // 0 = laden, 1 = läuft, 2 = fortsetzen
  339. {
  340. return status;
  341. }