Klient.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 = klient->release();
  21. }
  22. void Klient::online( SSKlientV *zKlient )
  23. {
  24. if( klient )
  25. klient = klient->release();
  26. klient = 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 )
  157. {
  158. if( !klient )
  159. return;
  160. short len = 13;
  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. klient->spielNachricht( len, bytes );
  167. delete[] bytes;
  168. }
  169. void Klient::sendeAsteroidTreffer( int asteroidId, int newAsteroidId, int schussId, Vertex pos, __int64 seed, int spielZeit )
  170. {
  171. if( !klient )
  172. return;
  173. short len = 33;
  174. char *bytes = new char[ len ];
  175. *(int*)bytes = spielZeit;
  176. *(char*)( bytes + 4 ) = 0x12;
  177. *(int*)( bytes + 5 ) = schussId;
  178. *(int*)( bytes + 9 ) = asteroidId;
  179. *(float*)( bytes + 13 ) = pos.x;
  180. *(float*)( bytes + 17 ) = pos.y;
  181. *(__int64*)( bytes + 21 ) = seed;
  182. *(int*)( bytes + 29 ) = newAsteroidId;
  183. klient->spielNachricht( len, bytes );
  184. delete[] bytes;
  185. }
  186. void Klient::sendeWiederbelebung( int sNum, int spielZeit )
  187. {
  188. if( !klient )
  189. return;
  190. short len = 9;
  191. char *bytes = new char[ len ];
  192. *(int*)bytes = spielZeit;
  193. *(char*)( bytes + 4 ) = 0xF;
  194. *(int*)( bytes + 5 ) = sNum;
  195. klient->spielNachricht( len, bytes );
  196. delete[] bytes;
  197. }
  198. void Klient::sendeTod( int sNum, int killSNum, int spielZeit )
  199. {
  200. if( !klient )
  201. return;
  202. short len = 13;
  203. char *bytes = new char[ len ];
  204. *(int*)bytes = spielZeit;
  205. *(char*)( bytes + 4 ) = 0x10;
  206. *(int*)( bytes + 5 ) = sNum;
  207. *(int*)( bytes + 9 ) = killSNum;
  208. klient->spielNachricht( len, bytes );
  209. delete[] bytes;
  210. }
  211. void Klient::sendeSpielEnde( char gewonnen, int spielZeit )
  212. {
  213. if( !klient )
  214. return;
  215. short len = 6;
  216. char *bytes = new char[ len ];
  217. *(int*)bytes = spielZeit;
  218. *(char*)( bytes + 4 ) = 0xB;
  219. *(char*)( bytes + 5 ) = gewonnen;
  220. klient->spielNachricht( len, bytes );
  221. delete[] bytes;
  222. }
  223. void Klient::sendeChatNachricht( char *txt, int spielZeit )
  224. {
  225. if( !klient )
  226. return;
  227. short len = (short)( 5 + textLength( txt ) );
  228. char *bytes = new char[ len ];
  229. *(int*)bytes = spielZeit;
  230. *(char*)( bytes + 4 ) = 0x8;
  231. for( int i = 5; i < len; i++ )
  232. bytes[ i ] = txt[ i - 5 ];
  233. klient->spielNachricht( len, bytes );
  234. delete[] bytes;
  235. }
  236. void Klient::sendeStatistikChatNachricht( int vonAccount, char *txt )
  237. {
  238. if( !klient )
  239. return;
  240. short len = (short)( 5 + textLength( txt ) );
  241. char *bytes = new char[ len ];
  242. *(char*)( bytes ) = 3;
  243. *(int*)( bytes + 1 ) = vonAccount;
  244. for( int i = 5; i < len; i++ )
  245. bytes[ i ] = txt[ i - 5 ];
  246. klient->statistikNachricht( len, bytes );
  247. delete[] bytes;
  248. }
  249. void Klient::sendeStatistikSpielerOffline( int account )
  250. {
  251. if( !klient )
  252. return;
  253. char *bytes = new char[ 5 ];
  254. *(char*)( bytes ) = 4;
  255. *(int*)( bytes + 1 ) = account;
  256. klient->statistikNachricht( 5, bytes );
  257. delete[] bytes;
  258. }
  259. void Klient::sendeSpielerStatistik( SpielerStatistik *zS )
  260. {
  261. if( !zS || !klient )
  262. return;
  263. char snl = (char)zS->zSpielerName()->getLength();
  264. char tnl = (char)zS->zTeamName()->getLength();
  265. int len = 55 + snl + tnl;
  266. char *bytes = new char[ len ];
  267. bytes[ 0 ] = 0;
  268. *(int*)( bytes + 1 ) = zS->getSpielerNummer();
  269. *(char*)( bytes + 5 ) = (char)zS->zSpielerName()->getLength();
  270. for( int i = 0; i < snl; i++ )
  271. bytes[ i + 6 ] = zS->zSpielerName()->getText()[ i ];
  272. *(char*)( bytes + 6 + snl ) = tnl;
  273. for( int i = 0; i < tnl; i++ )
  274. bytes[ i + 7 + snl ] = zS->zTeamName()->getText()[ i ];
  275. *(int*)( bytes + 7 + snl + tnl ) = zS->getSpielerFarbe();
  276. *(int*)( bytes + 11 + snl + tnl ) = zS->getTeamFarbe();
  277. *(int*)( bytes + 15 + snl + tnl ) = zS->getSchadenBekommen();
  278. *(int*)( bytes + 19 + snl + tnl ) = zS->getSchadenGemacht();
  279. *(int*)( bytes + 23 + snl + tnl ) = zS->getTreibstoffVerbraucht();
  280. *(int*)( bytes + 27 + snl + tnl ) = zS->getShots();
  281. *(int*)( bytes + 31 + snl + tnl ) = zS->getTreffer();
  282. *(int*)( bytes + 35 + snl + tnl ) = zS->getPunkte();
  283. *(int*)( bytes + 39 + snl + tnl ) = zS->getKills();
  284. *(int*)( bytes + 43 + snl + tnl ) = zS->getTode();
  285. *(int*)( bytes + 47 + snl + tnl ) = zS->getZeitAmLeben();
  286. *(int*)( bytes + 51 + snl + tnl ) = zS->getZeitTod();
  287. klient->statistikNachricht( (short)len, bytes );
  288. delete[] bytes;
  289. }
  290. void Klient::sendeTeamStatistik( TeamStatistik *zS )
  291. {
  292. if( !zS || !klient )
  293. return;
  294. char tnl = (char)zS->zTeamName()->getLength();
  295. int len = 43 + tnl;
  296. char *bytes = new char[ len ];
  297. bytes[ 0 ] = 1;
  298. *(int*)( bytes + 1 ) = zS->getTeamNummer();
  299. *(char*)( bytes + 5 ) = tnl;
  300. for( int i = 0; i < tnl; i++ )
  301. bytes[ i + 6 ] = zS->zTeamName()->getText()[ i ];
  302. *(int*)( bytes + 6 + tnl ) = zS->getTeamFarbe();
  303. *(int*)( bytes + 10 + tnl ) = zS->getSchadenBekommen();
  304. *(int*)( bytes + 14 + tnl ) = zS->getSchadenGemacht();
  305. *(int*)( bytes + 18 + tnl ) = zS->getTreibstoffVerbraucht();
  306. *(int*)( bytes + 22 + tnl ) = zS->getShots();
  307. *(int*)( bytes + 26 + tnl ) = zS->getTreffer();
  308. *(int*)( bytes + 30 + tnl ) = zS->getPunkte();
  309. *(int*)( bytes + 34 + tnl ) = zS->getKills();
  310. *(int*)( bytes + 38 + tnl ) = zS->getTode();
  311. *( bytes + 42 + tnl ) = (char)zS->hatGewonnen();
  312. klient->statistikNachricht( (short)len, bytes );
  313. delete[] bytes;
  314. }
  315. void Klient::sendeStatistikLadenFertig()
  316. {
  317. if( !klient )
  318. return;
  319. char byte = 2;
  320. klient->statistikNachricht( 1, &byte );
  321. }
  322. // constant
  323. bool Klient::istOnline() const
  324. {
  325. return klient != 0;
  326. }
  327. // reference Counting
  328. Klient *Klient::getThis()
  329. {
  330. ref++;
  331. return this;
  332. }
  333. Klient *Klient::release()
  334. {
  335. ref--;
  336. if( !ref )
  337. delete this;
  338. return 0;
  339. }