Klient.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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::sendeAsteroid( int id, Vertex pos, Vertex speed, float rot, float rotS, int index, int spielZeit )
  94. {
  95. if( !klient )
  96. return;
  97. short len = 37;
  98. char *bytes = new char[ len ];
  99. *(int *)bytes = spielZeit;
  100. *(char *)( bytes + 4 ) = 0x11;
  101. *(int *)( bytes + 5 ) = id;
  102. *(float *)( bytes + 9 ) = pos.x;
  103. *(float *)( bytes + 13 ) = pos.y;
  104. *(float *)( bytes + 17 ) = speed.x;
  105. *(float *)( bytes + 21 ) = speed.y;
  106. *(float *)( bytes + 25 ) = rot;
  107. *(float *)( bytes + 29 ) = rotS;
  108. *(int *)( bytes + 33 ) = index;
  109. klient->spielNachricht( len, bytes );
  110. delete[] bytes;
  111. }
  112. void Klient::sendeSchuss( int id, int sNum, Vertex pos, Vertex speed, double intensity, int spielZeit )
  113. {
  114. if( !klient )
  115. return;
  116. short len = 37;
  117. char *bytes = new char[ len ];
  118. *(int *)bytes = spielZeit;
  119. *(char *)( bytes + 4 ) = 0xD;
  120. *(int *)( bytes + 5 ) = id;
  121. *(int *)( bytes + 9 ) = sNum;
  122. *(float *)( bytes + 13 ) = pos.x;
  123. *(float *)( bytes + 17 ) = pos.y;
  124. *(float *)( bytes + 21 ) = speed.x;
  125. *(float *)( bytes + 25 ) = speed.y;
  126. *(double *)( bytes + 29 ) = intensity;
  127. klient->spielNachricht( len, bytes );
  128. delete[] bytes;
  129. }
  130. void Klient::sendePixel( int asteroid, int pixelId, int spielZeit )
  131. {
  132. if( !klient )
  133. return;
  134. short len = 13;
  135. char *bytes = new char[ len ];
  136. *(int *)bytes = spielZeit;
  137. *(char *)( bytes + 4 ) = 0x13;
  138. *(int *)( bytes + 5 ) = asteroid;
  139. *(int *)( bytes + 9 ) = pixelId;
  140. klient->spielNachricht( len, bytes );
  141. delete[] bytes;
  142. }
  143. void Klient::sendeEp( int pixelId, int spielerId, 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 ) = 0x14;
  151. *(int *)( bytes + 5 ) = pixelId;
  152. *(int *)( bytes + 9 ) = spielerId;
  153. klient->spielNachricht( len, bytes );
  154. delete[] bytes;
  155. }
  156. void Klient::sendeTreffer( int id, int sNum, int spielZeit, float ep, int skillP )
  157. {
  158. if( !klient )
  159. return;
  160. short len = 21;
  161. char *bytes = new char[ len ];
  162. *(int *)bytes = spielZeit;
  163. *(char *)( bytes + 4 ) = 0xE;
  164. *(int *)( bytes + 5 ) = id;
  165. *(int *)( bytes + 9 ) = sNum;
  166. *(float *)( bytes + 13 ) = ep;
  167. *(int *)( bytes + 17 ) = skillP;
  168. klient->spielNachricht( len, bytes );
  169. delete[] bytes;
  170. }
  171. void Klient::sendeAsteroidTreffer( int asteroidId, int newAsteroidId, int schussId, Vertex pos, __int64 seed, int spielZeit, float ep, int skillP )
  172. {
  173. if( !klient )
  174. return;
  175. short len = 41;
  176. char *bytes = new char[ len ];
  177. *(int *)bytes = spielZeit;
  178. *(char *)( bytes + 4 ) = 0x12;
  179. *(int *)( bytes + 5 ) = schussId;
  180. *(int *)( bytes + 9 ) = asteroidId;
  181. *(float *)( bytes + 13 ) = pos.x;
  182. *(float *)( bytes + 17 ) = pos.y;
  183. *(__int64 *)( bytes + 21 ) = seed;
  184. *(int *)( bytes + 29 ) = newAsteroidId;
  185. *(float *)( bytes + 33 ) = ep;
  186. *(int *)( bytes + 37 ) = skillP;
  187. klient->spielNachricht( len, bytes );
  188. delete[] bytes;
  189. }
  190. void Klient::sendeWiederbelebung( int sNum, int spielZeit )
  191. {
  192. if( !klient )
  193. return;
  194. short len = 9;
  195. char *bytes = new char[ len ];
  196. *(int *)bytes = spielZeit;
  197. *(char *)( bytes + 4 ) = 0xF;
  198. *(int *)( bytes + 5 ) = sNum;
  199. klient->spielNachricht( len, bytes );
  200. delete[] bytes;
  201. }
  202. void Klient::sendeTod( int sNum, int killSNum, int spielZeit )
  203. {
  204. if( !klient )
  205. return;
  206. short len = 13;
  207. char *bytes = new char[ len ];
  208. *(int *)bytes = spielZeit;
  209. *(char *)( bytes + 4 ) = 0x10;
  210. *(int *)( bytes + 5 ) = sNum;
  211. *(int *)( bytes + 9 ) = killSNum;
  212. klient->spielNachricht( len, bytes );
  213. delete[] bytes;
  214. }
  215. void Klient::sendeSpielEnde( char gewonnen, int spielZeit )
  216. {
  217. if( !klient )
  218. return;
  219. short len = 6;
  220. char *bytes = new char[ len ];
  221. *(int *)bytes = spielZeit;
  222. *(char *)( bytes + 4 ) = 0xB;
  223. *(char *)( bytes + 5 ) = gewonnen;
  224. klient->spielNachricht( len, bytes );
  225. delete[] bytes;
  226. }
  227. void Klient::sendeChatNachricht( char *txt, int spielZeit )
  228. {
  229. if( !klient )
  230. return;
  231. short len = (short)( 5 + textLength( txt ) );
  232. char *bytes = new char[ len ];
  233. *(int *)bytes = spielZeit;
  234. *(char *)( bytes + 4 ) = 0x8;
  235. for( int i = 5; i < len; i++ )
  236. bytes[ i ] = txt[ i - 5 ];
  237. klient->spielNachricht( len, bytes );
  238. delete[] bytes;
  239. }
  240. void Klient::sendeStatistikChatNachricht( int vonAccount, char *txt )
  241. {
  242. if( !klient )
  243. return;
  244. short len = (short)( 5 + textLength( txt ) );
  245. char *bytes = new char[ len ];
  246. *(char *)( bytes ) = 3;
  247. *(int *)( bytes + 1 ) = vonAccount;
  248. for( int i = 5; i < len; i++ )
  249. bytes[ i ] = txt[ i - 5 ];
  250. klient->statistikNachricht( len, bytes );
  251. delete[] bytes;
  252. }
  253. void Klient::sendeStatistikSpielerOffline( int account )
  254. {
  255. if( !klient )
  256. return;
  257. char *bytes = new char[ 5 ];
  258. *(char *)( bytes ) = 4;
  259. *(int *)( bytes + 1 ) = account;
  260. klient->statistikNachricht( 5, bytes );
  261. delete[] bytes;
  262. }
  263. void Klient::sendeSpielerStatistik( SpielerStatistik * zS )
  264. {
  265. if( !zS || !klient )
  266. return;
  267. char snl = (char)zS->zSpielerName()->getLength();
  268. char tnl = (char)zS->zTeamName()->getLength();
  269. int len = 55 + snl + tnl;
  270. char *bytes = new char[ len ];
  271. bytes[ 0 ] = 0;
  272. *(int *)( bytes + 1 ) = zS->getSpielerNummer();
  273. *(char *)( bytes + 5 ) = (char)zS->zSpielerName()->getLength();
  274. for( int i = 0; i < snl; i++ )
  275. bytes[ i + 6 ] = zS->zSpielerName()->getText()[ i ];
  276. *(char *)( bytes + 6 + snl ) = tnl;
  277. for( int i = 0; i < tnl; i++ )
  278. bytes[ i + 7 + snl ] = zS->zTeamName()->getText()[ i ];
  279. *(int *)( bytes + 7 + snl + tnl ) = zS->getSpielerFarbe();
  280. *(int *)( bytes + 11 + snl + tnl ) = zS->getTeamFarbe();
  281. *(int *)( bytes + 15 + snl + tnl ) = zS->getSchadenBekommen();
  282. *(int *)( bytes + 19 + snl + tnl ) = zS->getSchadenGemacht();
  283. *(int *)( bytes + 23 + snl + tnl ) = zS->getTreibstoffVerbraucht();
  284. *(int *)( bytes + 27 + snl + tnl ) = zS->getShots();
  285. *(int *)( bytes + 31 + snl + tnl ) = zS->getTreffer();
  286. *(int *)( bytes + 35 + snl + tnl ) = zS->getPunkte();
  287. *(int *)( bytes + 39 + snl + tnl ) = zS->getKills();
  288. *(int *)( bytes + 43 + snl + tnl ) = zS->getTode();
  289. *(int *)( bytes + 47 + snl + tnl ) = zS->getZeitAmLeben();
  290. *(int *)( bytes + 51 + snl + tnl ) = zS->getZeitTod();
  291. klient->statistikNachricht( (short)len, bytes );
  292. delete[] bytes;
  293. }
  294. void Klient::sendeTeamStatistik( TeamStatistik * zS )
  295. {
  296. if( !zS || !klient )
  297. return;
  298. char tnl = (char)zS->zTeamName()->getLength();
  299. int len = 43 + tnl;
  300. char *bytes = new char[ len ];
  301. bytes[ 0 ] = 1;
  302. *(int *)( bytes + 1 ) = zS->getTeamNummer();
  303. *(char *)( bytes + 5 ) = tnl;
  304. for( int i = 0; i < tnl; i++ )
  305. bytes[ i + 6 ] = zS->zTeamName()->getText()[ i ];
  306. *(int *)( bytes + 6 + tnl ) = zS->getTeamFarbe();
  307. *(int *)( bytes + 10 + tnl ) = zS->getSchadenBekommen();
  308. *(int *)( bytes + 14 + tnl ) = zS->getSchadenGemacht();
  309. *(int *)( bytes + 18 + tnl ) = zS->getTreibstoffVerbraucht();
  310. *(int *)( bytes + 22 + tnl ) = zS->getShots();
  311. *(int *)( bytes + 26 + tnl ) = zS->getTreffer();
  312. *(int *)( bytes + 30 + tnl ) = zS->getPunkte();
  313. *(int *)( bytes + 34 + tnl ) = zS->getKills();
  314. *(int *)( bytes + 38 + tnl ) = zS->getTode();
  315. *( bytes + 42 + tnl ) = (char)zS->hatGewonnen();
  316. klient->statistikNachricht( (short)len, bytes );
  317. delete[] bytes;
  318. }
  319. void Klient::sendeStatistikLadenFertig()
  320. {
  321. if( !klient )
  322. return;
  323. char byte = 2;
  324. klient->statistikNachricht( 1, &byte );
  325. }
  326. // constant
  327. bool Klient::istOnline() const
  328. {
  329. return klient != 0;
  330. }
  331. // reference Counting
  332. Klient *Klient::getThis()
  333. {
  334. ref++;
  335. return this;
  336. }
  337. Klient *Klient::release()
  338. {
  339. ref--;
  340. if( !ref )
  341. delete this;
  342. return 0;
  343. }