Klient.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #include "Spieler.h"
  2. #include "SSKlient.h"
  3. #include <Text.h>
  4. // Inhalt der Klient Klasse aus Klient.h
  5. // Konstruktor
  6. Klient::Klient( SSKlientV *klient )
  7. {
  8. this->klient = klient;
  9. ref = 1;
  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 = (SSKlientV *)zKlient->getThis();
  27. }
  28. void Klient::sendeInit( RCArray< Spieler > * zSpieler, int spielZeit )
  29. {
  30. if( !klient )
  31. return;
  32. short len = (short)( 6 + zSpieler->getEintragAnzahl() * 8 );
  33. char *bytes = new char[ len ];
  34. *(int *)bytes = spielZeit;
  35. *(char *)( bytes + 4 ) = 6;
  36. *(char *)( bytes + 5 ) = (char)zSpieler->getEintragAnzahl();
  37. for( int i = 0; i < zSpieler->getEintragAnzahl(); i++ )
  38. {
  39. *(int *)( bytes + 6 + i * 8 ) = zSpieler->z( i )->getSpielerNummer();
  40. *(int *)( bytes + 10 + i * 8 ) = zSpieler->z( i )->getAccountId();
  41. }
  42. klient->spielNachricht( len, bytes );
  43. delete[] bytes;
  44. }
  45. void Klient::sendeSpielerNummer( int sNum, int spielZeit )
  46. {
  47. if( !klient )
  48. return;
  49. short len = 9;
  50. char *bytes = new char[ len ];
  51. *(int *)bytes = spielZeit;
  52. *(char *)( bytes + 4 ) = 9;
  53. *(int *)( bytes + 5 ) = sNum;
  54. klient->spielNachricht( len, bytes );
  55. delete[] bytes;
  56. }
  57. void Klient::sendeStart( int spielZeit )
  58. {
  59. if( !klient )
  60. return;
  61. short len = 5;
  62. char *bytes = new char[ len ];
  63. *(int *)bytes = spielZeit;
  64. *(char *)( bytes + 4 ) = 0xA;
  65. klient->spielNachricht( len, bytes );
  66. delete[] bytes;
  67. }
  68. void Klient::sendeTastaturStatus( int spielerId, TastaturStatus ts, bool aktiv, int spielZeit )
  69. {
  70. if( !klient )
  71. return;
  72. short len = 9;
  73. char *bytes = new char[ len ];
  74. *(int *)bytes = spielZeit;
  75. *(char *)( bytes + 4 ) = (char)( (char)ts * 2 + (char)!aktiv );
  76. *(int *)( bytes + 5 ) = spielerId;
  77. klient->spielNachricht( len, bytes );
  78. delete[] bytes;
  79. }
  80. void Klient::sendeSkillNachricht( int sNum, char art, int spielZeit )
  81. {
  82. if( !klient )
  83. return;
  84. short len = 10;
  85. char *bytes = new char[ len ];
  86. *(int *)bytes = spielZeit;
  87. *(char *)( bytes + 4 ) = 0xC;
  88. *(int *)( bytes + 5 ) = sNum;
  89. *(char *)( bytes + 9 ) = art;
  90. klient->spielNachricht( len, bytes );
  91. delete[] bytes;
  92. }
  93. void Klient::sendeUseSkillNachricht( int sNum, char id, int spielZeit )
  94. {
  95. if( !klient )
  96. return;
  97. short len = 10;
  98. char *bytes = new char[ len ];
  99. *(int *)bytes = spielZeit;
  100. *(char *)( bytes + 4 ) = 0x15;
  101. *(int *)( bytes + 5 ) = sNum;
  102. *(char *)( bytes + 9 ) = id;
  103. klient->spielNachricht( len, bytes );
  104. delete[] bytes;
  105. }
  106. void Klient::sendeAsteroid( int id, Vertex pos, Vertex speed, float rot, float rotS, int index, int spielZeit )
  107. {
  108. if( !klient )
  109. return;
  110. short len = 37;
  111. char *bytes = new char[ len ];
  112. *(int *)bytes = spielZeit;
  113. *(char *)( bytes + 4 ) = 0x11;
  114. *(int *)( bytes + 5 ) = id;
  115. *(float *)( bytes + 9 ) = pos.x;
  116. *(float *)( bytes + 13 ) = pos.y;
  117. *(float *)( bytes + 17 ) = speed.x;
  118. *(float *)( bytes + 21 ) = speed.y;
  119. *(float *)( bytes + 25 ) = rot;
  120. *(float *)( bytes + 29 ) = rotS;
  121. *(int *)( bytes + 33 ) = index;
  122. klient->spielNachricht( len, bytes );
  123. delete[] bytes;
  124. }
  125. void Klient::sendeSchuss( int id, int sNum, Vertex pos, Vertex speed, double intensity, int spielZeit )
  126. {
  127. if( !klient )
  128. return;
  129. short len = 37;
  130. char *bytes = new char[ len ];
  131. *(int *)bytes = spielZeit;
  132. *(char *)( bytes + 4 ) = 0xD;
  133. *(int *)( bytes + 5 ) = id;
  134. *(int *)( bytes + 9 ) = sNum;
  135. *(float *)( bytes + 13 ) = pos.x;
  136. *(float *)( bytes + 17 ) = pos.y;
  137. *(float *)( bytes + 21 ) = speed.x;
  138. *(float *)( bytes + 25 ) = speed.y;
  139. *(double *)( bytes + 29 ) = intensity;
  140. klient->spielNachricht( len, bytes );
  141. delete[] bytes;
  142. }
  143. void Klient::sendePixel( int asteroid, int pixelId, int spielZeit )
  144. {
  145. if( !klient )
  146. return;
  147. short len = 13;
  148. char *bytes = new char[ len ];
  149. *(int *)bytes = spielZeit;
  150. *(char *)( bytes + 4 ) = 0x13;
  151. *(int *)( bytes + 5 ) = asteroid;
  152. *(int *)( bytes + 9 ) = pixelId;
  153. klient->spielNachricht( len, bytes );
  154. delete[] bytes;
  155. }
  156. void Klient::sendeEp( int pixelId, int spielerId, int spielZeit )
  157. {
  158. if( !klient )
  159. return;
  160. short len = 13;
  161. char *bytes = new char[ len ];
  162. *(int *)bytes = spielZeit;
  163. *(char *)( bytes + 4 ) = 0x14;
  164. *(int *)( bytes + 5 ) = pixelId;
  165. *(int *)( bytes + 9 ) = spielerId;
  166. klient->spielNachricht( len, bytes );
  167. delete[] bytes;
  168. }
  169. void Klient::sendeTreffer( int id, int sNum, int spielZeit, float ep, int skillP )
  170. {
  171. if( !klient )
  172. return;
  173. short len = 21;
  174. char *bytes = new char[ len ];
  175. *(int *)bytes = spielZeit;
  176. *(char *)( bytes + 4 ) = 0xE;
  177. *(int *)( bytes + 5 ) = id;
  178. *(int *)( bytes + 9 ) = sNum;
  179. *(float *)( bytes + 13 ) = ep;
  180. *(int *)( bytes + 17 ) = skillP;
  181. klient->spielNachricht( len, bytes );
  182. delete[] bytes;
  183. }
  184. void Klient::sendeAsteroidTreffer( int asteroidId, int newAsteroidId, int schussId, Vertex pos, __int64 seed, int spielZeit, float ep, int skillP )
  185. {
  186. if( !klient )
  187. return;
  188. short len = 41;
  189. char *bytes = new char[ len ];
  190. *(int *)bytes = spielZeit;
  191. *(char *)( bytes + 4 ) = 0x12;
  192. *(int *)( bytes + 5 ) = schussId;
  193. *(int *)( bytes + 9 ) = asteroidId;
  194. *(float *)( bytes + 13 ) = pos.x;
  195. *(float *)( bytes + 17 ) = pos.y;
  196. *(__int64 *)( bytes + 21 ) = seed;
  197. *(int *)( bytes + 29 ) = newAsteroidId;
  198. *(float *)( bytes + 33 ) = ep;
  199. *(int *)( bytes + 37 ) = skillP;
  200. klient->spielNachricht( len, bytes );
  201. delete[] bytes;
  202. }
  203. void Klient::sendeWiederbelebung( int sNum, int spielZeit )
  204. {
  205. if( !klient )
  206. return;
  207. short len = 9;
  208. char *bytes = new char[ len ];
  209. *(int *)bytes = spielZeit;
  210. *(char *)( bytes + 4 ) = 0xF;
  211. *(int *)( bytes + 5 ) = sNum;
  212. klient->spielNachricht( len, bytes );
  213. delete[] bytes;
  214. }
  215. void Klient::sendeTod( int sNum, int killSNum, int spielZeit )
  216. {
  217. if( !klient )
  218. return;
  219. short len = 13;
  220. char *bytes = new char[ len ];
  221. *(int *)bytes = spielZeit;
  222. *(char *)( bytes + 4 ) = 0x10;
  223. *(int *)( bytes + 5 ) = sNum;
  224. *(int *)( bytes + 9 ) = killSNum;
  225. klient->spielNachricht( len, bytes );
  226. delete[] bytes;
  227. }
  228. void Klient::sendeSpielEnde( char gewonnen, int spielZeit )
  229. {
  230. if( !klient )
  231. return;
  232. short len = 6;
  233. char *bytes = new char[ len ];
  234. *(int *)bytes = spielZeit;
  235. *(char *)( bytes + 4 ) = 0xB;
  236. *(char *)( bytes + 5 ) = gewonnen;
  237. klient->spielNachricht( len, bytes );
  238. delete[] bytes;
  239. }
  240. void Klient::sendeChatNachricht( char *txt, int spielZeit )
  241. {
  242. if( !klient )
  243. return;
  244. short len = (short)( 5 + textLength( txt ) );
  245. char *bytes = new char[ len ];
  246. *(int *)bytes = spielZeit;
  247. *(char *)( bytes + 4 ) = 0x8;
  248. for( int i = 5; i < len; i++ )
  249. bytes[ i ] = txt[ i - 5 ];
  250. klient->spielNachricht( len, bytes );
  251. delete[] bytes;
  252. }
  253. void Klient::sendeStatistikChatNachricht( int vonAccount, char *txt )
  254. {
  255. if( !klient )
  256. return;
  257. short len = (short)( 5 + textLength( txt ) );
  258. char *bytes = new char[ len ];
  259. *(char *)( bytes ) = 3;
  260. *(int *)( bytes + 1 ) = vonAccount;
  261. for( int i = 5; i < len; i++ )
  262. bytes[ i ] = txt[ i - 5 ];
  263. klient->statistikNachricht( len, bytes );
  264. delete[] bytes;
  265. }
  266. void Klient::sendeStatistikSpielerOffline( int account )
  267. {
  268. if( !klient )
  269. return;
  270. char *bytes = new char[ 5 ];
  271. *(char *)( bytes ) = 4;
  272. *(int *)( bytes + 1 ) = account;
  273. klient->statistikNachricht( 5, bytes );
  274. delete[] bytes;
  275. }
  276. void Klient::sendeSpielerStatistik( SpielerStatistik * zS )
  277. {
  278. if( !zS || !klient )
  279. return;
  280. char snl = (char)zS->zSpielerName()->getLength();
  281. char tnl = (char)zS->zTeamName()->getLength();
  282. int len = 55 + snl + tnl;
  283. char *bytes = new char[ len ];
  284. bytes[ 0 ] = 0;
  285. *(int *)( bytes + 1 ) = zS->getSpielerNummer();
  286. *(char *)( bytes + 5 ) = (char)zS->zSpielerName()->getLength();
  287. for( int i = 0; i < snl; i++ )
  288. bytes[ i + 6 ] = zS->zSpielerName()->getText()[ i ];
  289. *(char *)( bytes + 6 + snl ) = tnl;
  290. for( int i = 0; i < tnl; i++ )
  291. bytes[ i + 7 + snl ] = zS->zTeamName()->getText()[ i ];
  292. *(int *)( bytes + 7 + snl + tnl ) = zS->getSpielerFarbe();
  293. *(int *)( bytes + 11 + snl + tnl ) = zS->getTeamFarbe();
  294. *(int *)( bytes + 15 + snl + tnl ) = zS->getSchadenBekommen();
  295. *(int *)( bytes + 19 + snl + tnl ) = zS->getSchadenGemacht();
  296. *(int *)( bytes + 23 + snl + tnl ) = zS->getTreibstoffVerbraucht();
  297. *(int *)( bytes + 27 + snl + tnl ) = zS->getShots();
  298. *(int *)( bytes + 31 + snl + tnl ) = zS->getTreffer();
  299. *(int *)( bytes + 35 + snl + tnl ) = zS->getPunkte();
  300. *(int *)( bytes + 39 + snl + tnl ) = zS->getKills();
  301. *(int *)( bytes + 43 + snl + tnl ) = zS->getTode();
  302. *(int *)( bytes + 47 + snl + tnl ) = zS->getZeitAmLeben();
  303. *(int *)( bytes + 51 + snl + tnl ) = zS->getZeitTod();
  304. klient->statistikNachricht( (short)len, bytes );
  305. delete[] bytes;
  306. }
  307. void Klient::sendeTeamStatistik( TeamStatistik * zS )
  308. {
  309. if( !zS || !klient )
  310. return;
  311. char tnl = (char)zS->zTeamName()->getLength();
  312. int len = 43 + tnl;
  313. char *bytes = new char[ len ];
  314. bytes[ 0 ] = 1;
  315. *(int *)( bytes + 1 ) = zS->getTeamNummer();
  316. *(char *)( bytes + 5 ) = tnl;
  317. for( int i = 0; i < tnl; i++ )
  318. bytes[ i + 6 ] = zS->zTeamName()->getText()[ i ];
  319. *(int *)( bytes + 6 + tnl ) = zS->getTeamFarbe();
  320. *(int *)( bytes + 10 + tnl ) = zS->getSchadenBekommen();
  321. *(int *)( bytes + 14 + tnl ) = zS->getSchadenGemacht();
  322. *(int *)( bytes + 18 + tnl ) = zS->getTreibstoffVerbraucht();
  323. *(int *)( bytes + 22 + tnl ) = zS->getShots();
  324. *(int *)( bytes + 26 + tnl ) = zS->getTreffer();
  325. *(int *)( bytes + 30 + tnl ) = zS->getPunkte();
  326. *(int *)( bytes + 34 + tnl ) = zS->getKills();
  327. *(int *)( bytes + 38 + tnl ) = zS->getTode();
  328. *( bytes + 42 + tnl ) = (char)zS->hatGewonnen();
  329. klient->statistikNachricht( (short)len, bytes );
  330. delete[] bytes;
  331. }
  332. void Klient::sendeStatistikLadenFertig()
  333. {
  334. if( !klient )
  335. return;
  336. char byte = 2;
  337. klient->statistikNachricht( 1, &byte );
  338. }
  339. // constant
  340. bool Klient::istOnline() const
  341. {
  342. return klient != 0;
  343. }
  344. // reference Counting
  345. Klient *Klient::getThis()
  346. {
  347. ref++;
  348. return this;
  349. }
  350. Klient *Klient::release()
  351. {
  352. ref--;
  353. if( !ref )
  354. delete this;
  355. return 0;
  356. }