Klient.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. #include "Klient.h"
  2. #include "Spieler.h"
  3. #include <Text.h>
  4. // Inhalt der Klient Klasse aus Klient.h
  5. // Konstruktor
  6. Klient::Klient( SSKlientV *klient )
  7. : ReferenceCounter()
  8. {
  9. this->klient = klient;
  10. }
  11. // Destruktor
  12. Klient::~Klient()
  13. {
  14. if( klient )
  15. klient->release();
  16. }
  17. // nicht constant
  18. void Klient::offline()
  19. {
  20. klient = (SSKlientV *)klient->release();
  21. }
  22. void Klient::online( SSKlientV *zKlient )
  23. {
  24. if( klient )
  25. klient = (SSKlientV *)klient->release();
  26. klient = dynamic_cast<SSKlientV *>( zKlient->getThis() );
  27. }
  28. void Klient::sendeSpielerNummer( int sNum, double spielZeit )
  29. {
  30. if( !klient )
  31. return;
  32. short len = 13;
  33. char *bytes = new char[ len ];
  34. *(double *)bytes = spielZeit;
  35. *(char *)( bytes + 8 ) = 0;
  36. *(int *)( bytes + 9 ) = sNum;
  37. klient->spielNachricht( len, bytes );
  38. delete[] bytes;
  39. }
  40. void Klient::sendeInitSpieler( int anzahl, RCArray< Spieler > *zSpieler, double spielZeit )
  41. {
  42. if( !klient )
  43. return;
  44. short len = (short)( 13 + 20 * anzahl );
  45. char *bytes = new char[ len ];
  46. *(double *)bytes = spielZeit;
  47. *(char *)( bytes + 8 ) = 1;
  48. *(int *)( bytes + 9 ) = anzahl;
  49. for( int i = 0; i < anzahl; i++ )
  50. {
  51. *(int *)( bytes + i * 20 + 13 ) = zSpieler->z( i )->getSpielerNummer();
  52. *(int *)( bytes + i * 20 + 17 ) = zSpieler->z( i )->getSpielerTeam();
  53. *(int *)( bytes + i * 20 + 21 ) = zSpieler->z( i )->getSpielerFarbe();
  54. *(int *)( bytes + i * 20 + 25 ) = zSpieler->z( i )->getTeamFarbe();
  55. *(int *)( bytes + i * 20 + 29 ) = zSpieler->z( i )->getAccountId();
  56. }
  57. klient->spielNachricht( len, bytes );
  58. delete[] bytes;
  59. }
  60. void Klient::sendeSpielerPosition( Spieler *zSpieler, double spielZeit )
  61. {
  62. if( !klient )
  63. return;
  64. short len = 29;
  65. char *bytes = new char[ len ];
  66. *(double *)bytes = spielZeit;
  67. *(char *)( bytes + 8 ) = 2;
  68. *(int *)( bytes + 9 ) = zSpieler->getSpielerNummer();
  69. *(double *)( bytes + 13 ) = zSpieler->getX();
  70. *(double *)( bytes + 21 ) = zSpieler->getY();
  71. klient->spielNachricht( len, bytes );
  72. delete[] bytes;
  73. }
  74. void Klient::sendeKammeraSize( int br, int hi, double spielZeit )
  75. {
  76. if( !klient )
  77. return;
  78. short len = 17;
  79. char *bytes = new char[ len ];
  80. *(double *)bytes = spielZeit;
  81. *(char *)( bytes + 8 ) = 3;
  82. *(int *)( bytes + 9 ) = br;
  83. *(int *)( bytes + 13 ) = hi;
  84. klient->spielNachricht( len, bytes );
  85. delete[] bytes;
  86. }
  87. void Klient::sendeStart( double spielZeit )
  88. {
  89. if( !klient )
  90. return;
  91. short len = 9;
  92. char *bytes = new char[ len ];
  93. *(double *)bytes = spielZeit;
  94. *(char *)( bytes + 8 ) = 4;
  95. klient->spielNachricht( len, bytes );
  96. delete[] bytes;
  97. }
  98. void Klient::sendeSpielerTod( Spieler *zSpieler, double spielZeit )
  99. {
  100. if( !klient )
  101. return;
  102. short len = 13;
  103. char *bytes = new char[ len ];
  104. *(double *)bytes = spielZeit;
  105. *(char *)( bytes + 8 ) = 5;
  106. *(int *)( bytes + 9 ) = zSpieler->getSpielerNummer();
  107. klient->spielNachricht( len, bytes );
  108. delete[] bytes;
  109. }
  110. void Klient::sendeSpielEnde( char gewonnen, double spielZeit )
  111. {
  112. if( !klient )
  113. return;
  114. short len = 10;
  115. char *bytes = new char[ len ];
  116. *(double *)bytes = spielZeit;
  117. *(char *)( bytes + 8 ) = 6;
  118. *(char *)( bytes + 9 ) = gewonnen;
  119. klient->spielNachricht( len, bytes );
  120. delete[] bytes;
  121. }
  122. void Klient::sendeSpielerGeschwindigkeit( Spieler *zSpieler, double spielZeit )
  123. {
  124. if( !klient )
  125. return;
  126. short len = 29;
  127. char *bytes = new char[ len ];
  128. *(double *)bytes = spielZeit;
  129. *(char *)( bytes + 8 ) = 7;
  130. *(int *)( bytes + 9 ) = zSpieler->getSpielerNummer();
  131. *(double *)( bytes + 13 ) = zSpieler->getSpeedX();
  132. *(double *)( bytes + 21 ) = zSpieler->getSpeedY();
  133. klient->spielNachricht( len, bytes );
  134. delete[] bytes;
  135. }
  136. void Klient::sendeSpielerWendigkeit( Spieler *zSpieler, double spielZeit )
  137. {
  138. if( !klient )
  139. return;
  140. short len = 21;
  141. char *bytes = new char[ len ];
  142. *(double *)bytes = spielZeit;
  143. *(char *)( bytes + 8 ) = 8;
  144. *(int *)( bytes + 9 ) = zSpieler->getSpielerNummer();
  145. *(double *)( bytes + 13 ) = zSpieler->getKurve();
  146. klient->spielNachricht( len, bytes );
  147. delete[] bytes;
  148. }
  149. void Klient::sendeSpielerKurve( int sNum, char kurve, double spielZeit )
  150. {
  151. if( !klient )
  152. return;
  153. short len = 14;
  154. char *bytes = new char[ len ];
  155. *(double *)bytes = spielZeit;
  156. *(char *)( bytes + 8 ) = 9;
  157. *(int *)( bytes + 9 ) = sNum;
  158. bytes[ 13 ] = kurve;
  159. klient->spielNachricht( len, bytes );
  160. delete[] bytes;
  161. }
  162. void Klient::sendeKartenSize( int br, int hi, double spielZeit )
  163. {
  164. if( !klient )
  165. return;
  166. short len = 17;
  167. char *bytes = new char[ len ];
  168. *(double *)bytes = spielZeit;
  169. *(char *)( bytes + 8 ) = 0xA;
  170. *(int *)( bytes + 9 ) = br;
  171. *(int *)( bytes + 13 ) = hi;
  172. klient->spielNachricht( len, bytes );
  173. delete[] bytes;
  174. }
  175. void Klient::sendeChatNachricht( char *txt, double spielZeit )
  176. {
  177. if( !klient )
  178. return;
  179. short len = (short)( 9 + textLength( txt ) );
  180. char *bytes = new char[ len ];
  181. *(double *)bytes = spielZeit;
  182. *(char *)( bytes + 8 ) = 0xB;
  183. for( int i = 9; i < len; i++ )
  184. bytes[ i ] = txt[ i - 9 ];
  185. klient->spielNachricht( len, bytes );
  186. delete[] bytes;
  187. }
  188. void Klient::sendeLinienUnterbrechung( int sNum, bool unterbrochen, double spielZeit )
  189. {
  190. if( !klient )
  191. return;
  192. char *bytes = new char[ 14 ];
  193. *(double *)bytes = spielZeit;
  194. *(char *)( bytes + 8 ) = 0xC;
  195. *(int *)( bytes + 9 ) = sNum;
  196. *( bytes + 13 ) = (char)unterbrochen;
  197. klient->spielNachricht( 14, bytes );
  198. delete[] bytes;
  199. }
  200. void Klient::sendeTeamMaxPunkte( int team, int mP, double spielZeit )
  201. {
  202. if( !klient )
  203. return;
  204. char *bytes = new char[ 17 ];
  205. *(double *)bytes = spielZeit;
  206. *(char *)( bytes + 8 ) = 0xD;
  207. *(int *)( bytes + 9 ) = team;
  208. *(int *)( bytes + 13 ) = mP;
  209. klient->spielNachricht( 17, bytes );
  210. delete[] bytes;
  211. }
  212. void Klient::sendeTeamPunkte( int team, int p, double spielZeit )
  213. {
  214. if( !klient )
  215. return;
  216. char *bytes = new char[ 17 ];
  217. *(double *)bytes = spielZeit;
  218. *(char *)( bytes + 8 ) = 0xE;
  219. *(int *)( bytes + 9 ) = team;
  220. *(int *)( bytes + 13 ) = p;
  221. klient->spielNachricht( 17, bytes );
  222. delete[] bytes;
  223. }
  224. void Klient::sendeSpielerPunkte( int sNum, int p, double spielZeit )
  225. {
  226. if( !klient )
  227. return;
  228. char *bytes = new char[ 17 ];
  229. *(double *)bytes = spielZeit;
  230. *(char *)( bytes + 8 ) = 0xF;
  231. *(int *)( bytes + 9 ) = sNum;
  232. *(int *)( bytes + 13 ) = p;
  233. klient->spielNachricht( 17, bytes );
  234. delete[] bytes;
  235. }
  236. void Klient::sendeTeamTod( int team, double spielZeit )
  237. {
  238. if( !klient )
  239. return;
  240. char *bytes = new char[ 13 ];
  241. *(double *)bytes = spielZeit;
  242. *(char *)( bytes + 8 ) = 0x10;
  243. *(int *)( bytes + 9 ) = team;
  244. klient->spielNachricht( 13, bytes );
  245. delete[] bytes;
  246. }
  247. void Klient::sendeTeamName( int team, char *name )
  248. {
  249. if( !klient )
  250. return;
  251. int nL = textLength( name );
  252. char *bytes = new char[ 13 + nL ];
  253. *(double *)bytes = 0;
  254. *(char *)( bytes + 8 ) = 0x13;
  255. *(int *)( bytes + 9 ) = team;
  256. memcpy( bytes + 13, name, nL );
  257. klient->spielNachricht( (short)( 13 + nL ), bytes );
  258. delete[] bytes;
  259. }
  260. void Klient::sendeRundenEnde( int winTeam, double spielZeit )
  261. {
  262. if( !klient )
  263. return;
  264. char *bytes = new char[ 13 ];
  265. *(double *)bytes = spielZeit;
  266. *(char *)( bytes + 8 ) = 0x11;
  267. *(int *)( bytes + 9 ) = winTeam;
  268. klient->spielNachricht( 13, bytes );
  269. delete[] bytes;
  270. }
  271. void Klient::sendeZeitVerbleibend( int sekunden, double spielZeit )
  272. {
  273. if( !klient )
  274. return;
  275. char *bytes = new char[ 13 ];
  276. *(double *)bytes = spielZeit;
  277. *(char *)( bytes + 8 ) = 0x12;
  278. *(int *)( bytes + 9 ) = sekunden;
  279. klient->spielNachricht( 13, bytes );
  280. delete[] bytes;
  281. }
  282. void Klient::sendeStatistikChatNachricht( int vonAccount, char *txt )
  283. {
  284. if( !klient )
  285. return;
  286. short len = (short)( 5 + textLength( txt ) );
  287. char *bytes = new char[ len ];
  288. *(char *)( bytes ) = 8;
  289. *(int *)( bytes + 1 ) = vonAccount;
  290. for( int i = 5; i < len; i++ )
  291. bytes[ i ] = txt[ i - 5 ];
  292. klient->statistikNachricht( len, bytes );
  293. delete[] bytes;
  294. }
  295. void Klient::sendeStatistikSpielerOffline( int account )
  296. {
  297. if( !klient )
  298. return;
  299. char *bytes = new char[ 5 ];
  300. *(char *)( bytes ) = 9;
  301. *(int *)( bytes + 1 ) = account;
  302. klient->statistikNachricht( 5, bytes );
  303. delete[] bytes;
  304. }
  305. void Klient::sendeGesamtSpielerStatistik( GesamtSpielerStatistik *zS )
  306. {
  307. if( !zS || !klient )
  308. return;
  309. char snl = (char)zS->zSpielerName()->getLength();
  310. char tnl = (char)zS->zTeamName()->getLength();
  311. int len = 43 + snl + tnl;
  312. char *bytes = new char[ len ];
  313. bytes[ 0 ] = 2;
  314. *(int *)( bytes + 1 ) = zS->getSpielerNummer();
  315. *(char *)( bytes + 5 ) = (char)zS->zSpielerName()->getLength();
  316. for( int i = 0; i < snl; i++ )
  317. bytes[ i + 6 ] = zS->zSpielerName()->getText()[ i ];
  318. *(char *)( bytes + 6 + snl ) = tnl;
  319. for( int i = 0; i < tnl; i++ )
  320. bytes[ i + 7 + snl ] = zS->zTeamName()->getText()[ i ];
  321. *(int *)( bytes + 7 + snl + tnl ) = zS->getSpielerFarbe();
  322. *(int *)( bytes + 11 + snl + tnl ) = zS->getTeamFarbe();
  323. *(int *)( bytes + 15 + snl + tnl ) = zS->getGewonneneRunden();
  324. *(int *)( bytes + 19 + snl + tnl ) = zS->getPunkte();
  325. *(int *)( bytes + 23 + snl + tnl ) = zS->getLinienLength();
  326. *(int *)( bytes + 27 + snl + tnl ) = zS->getZeitAmLeben();
  327. *(int *)( bytes + 31 + snl + tnl ) = zS->getZeitTod();
  328. *(int *)( bytes + 35 + snl + tnl ) = zS->getKills();
  329. *(int *)( bytes + 39 + snl + tnl ) = zS->getTode();
  330. klient->statistikNachricht( (short)len, bytes );
  331. delete[] bytes;
  332. }
  333. void Klient::sendeGesamtTeamStatistik( GesamtTeamStatistik *zS )
  334. {
  335. if( !zS || !klient )
  336. return;
  337. char tnl = (char)zS->zTeamName()->getLength();
  338. int len = 30 + tnl;
  339. char *bytes = new char[ len ];
  340. bytes[ 0 ] = 3;
  341. *(int *)( bytes + 1 ) = zS->getTeamNummer();
  342. *(char *)( bytes + 5 ) = tnl;
  343. for( int i = 0; i < tnl; i++ )
  344. bytes[ i + 6 ] = zS->zTeamName()->getText()[ i ];
  345. *(int *)( bytes + 6 + tnl ) = zS->getTeamFarbe();
  346. *(int *)( bytes + 10 + tnl ) = zS->getGewonneneRunden();
  347. *(int *)( bytes + 14 + tnl ) = zS->getPunkte();
  348. *(int *)( bytes + 18 + tnl ) = zS->getLinienLength();
  349. *(int *)( bytes + 22 + tnl ) = zS->getKills();
  350. *(int *)( bytes + 26 + tnl ) = zS->getTode();
  351. klient->statistikNachricht( (short)len, bytes );
  352. delete[] bytes;
  353. }
  354. void Klient::sendeRundeStatistik( RundenRundenStatistik *zS )
  355. {
  356. if( !zS || !klient )
  357. return;
  358. char *bytes = new char[ 25 ];
  359. bytes[ 0 ] = 4;
  360. *(int *)( bytes + 1 ) = zS->getRundenNummer();
  361. *(int *)( bytes + 5 ) = zS->getRundenDauer();
  362. *(int *)( bytes + 9 ) = zS->getSiegerTeam();
  363. *(int *)( bytes + 13 ) = zS->getSpielFeldNutzung();
  364. *(int *)( bytes + 17 ) = zS->zRundenBild()->getBreite();
  365. *(int *)( bytes + 21 ) = zS->zRundenBild()->getHeight();
  366. klient->statistikNachricht( 25, bytes );
  367. delete[] bytes;
  368. Bild *zB = zS->zRundenBild();
  369. int pixel = zB->getBreite() * zB->getHeight();
  370. while( pixel )
  371. {
  372. int pStart = zB->getBreite() * zB->getHeight() - pixel;
  373. int pLen = pixel > 200 ? 200 : pixel;
  374. char *bytes = new char[ 13 + pLen * 4 ];
  375. bytes[ 0 ] = 5;
  376. *(int *)( bytes + 1 ) = zS->getRundenNummer();
  377. *(int *)( bytes + 5 ) = pStart;
  378. *(int *)( bytes + 9 ) = pLen;
  379. for( int i = 0; i < pLen; i++ )
  380. *(int *)( bytes + 13 + i * 4 ) = zB->getBuffer()[ pStart + i ];
  381. klient->statistikNachricht( (short)( 13 + pLen * 4 ), bytes );
  382. delete[] bytes;
  383. pixel -= pLen;
  384. }
  385. int anz = zS->getSpielerAnzahl();
  386. for( int i = 0; i < anz; i++ )
  387. sendeRundeSpielerStatistik( zS->zSpielerStatistik( i ), zS->getRundenNummer() );
  388. anz = zS->getTeamAnzahl();
  389. for( int i = 0; i < anz; i++ )
  390. sendeRundeTeamStatistik( zS->zTeamStatistik( i ), zS->getRundenNummer() );
  391. }
  392. void Klient::sendeRundeSpielerStatistik( RundenSpielerStatistik *zS, int runde )
  393. {
  394. if( !zS || !klient )
  395. return;
  396. char snl = (char)zS->zSpielerName()->getLength();
  397. char tnl = (char)zS->zTeamName()->getLength();
  398. int len = 31 + snl + tnl;
  399. char *bytes = new char[ len ];
  400. bytes[ 0 ] = 6;
  401. *(int *)( bytes + 1 ) = runde;
  402. *(int *)( bytes + 5 ) = zS->getSpielerNummer();
  403. *(char *)( bytes + 9 ) = snl;
  404. for( int i = 0; i < snl; i++ )
  405. bytes[ i + 10 ] = zS->zSpielerName()->getText()[ i ];
  406. *(char *)( bytes + 10 + snl ) = tnl;
  407. for( int i = 0; i < tnl; i++ )
  408. bytes[ i + 11 + snl ] = zS->zTeamName()->getText()[ i ];
  409. *(int *)( bytes + 11 + snl + tnl ) = zS->getSpielerFarbe();
  410. *(int *)( bytes + 15 + snl + tnl ) = zS->getTeamFarbe();
  411. *(int *)( bytes + 19 + snl + tnl ) = zS->getLinienLength();
  412. *(int *)( bytes + 23 + snl + tnl ) = zS->getKills();
  413. *(int *)( bytes + 27 + snl + tnl ) = zS->getTodesZeit();
  414. klient->statistikNachricht( (short)len, bytes );
  415. delete[] bytes;
  416. }
  417. void Klient::sendeRundeTeamStatistik( RundenTeamStatistik *zS, int runde )
  418. {
  419. if( !zS || !klient )
  420. return;
  421. char tnl = (char)zS->zTeamName()->getLength();
  422. int len = 27 + tnl;
  423. char *bytes = new char[ len ];
  424. bytes[ 0 ] = 7;
  425. *(int *)( bytes + 1 ) = runde;
  426. *(int *)( bytes + 5 ) = zS->getTeamNummer();
  427. *(char *)( bytes + 9 ) = tnl;
  428. for( int i = 0; i < tnl; i++ )
  429. bytes[ i + 10 ] = zS->zTeamName()->getText()[ i ];
  430. *(int *)( bytes + 10 + tnl ) = zS->getTeamFarbe();
  431. *(char *)( bytes + 14 + tnl ) = (char)zS->getErgebnis();
  432. *(int *)( bytes + 15 + tnl ) = zS->getLinienLength();
  433. *(int *)( bytes + 19 + tnl ) = zS->getKills();
  434. *(int *)( bytes + 23 + tnl ) = zS->getTode();
  435. klient->statistikNachricht( (short)len, bytes );
  436. delete[] bytes;
  437. }
  438. void Klient::sendeStatistikLadenFertig()
  439. {
  440. if( !klient )
  441. return;
  442. char byte = 1;
  443. klient->statistikNachricht( 1, &byte );
  444. }
  445. // constant
  446. bool Klient::istOnline() const
  447. {
  448. return klient != 0;
  449. }