Klient.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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, Vertex pos, Vertex speed, float rot, float rotS )
  69. {
  70. if( !klient )
  71. return;
  72. short len = 33;
  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. *(float*)( bytes + 9 ) = pos.x;
  78. *(float*)( bytes + 13 ) = pos.y;
  79. *(float*)( bytes + 17 ) = speed.x;
  80. *(float*)( bytes + 21 ) = speed.y;
  81. *(float*)( bytes + 25 ) = rot;
  82. *(float*)( bytes + 29 ) = rotS;
  83. klient->spielNachricht( len, bytes );
  84. delete[] bytes;
  85. }
  86. void Klient::sendeSkillNachricht( int sNum, char art, int spielZeit )
  87. {
  88. if( !klient )
  89. return;
  90. short len = 10;
  91. char *bytes = new char[ len ];
  92. *(int*)bytes = spielZeit;
  93. *(char*)( bytes + 4 ) = 0xC;
  94. *(int*)( bytes + 5 ) = sNum;
  95. *(char*)( bytes + 9 ) = art;
  96. klient->spielNachricht( len, bytes );
  97. delete[] bytes;
  98. }
  99. void Klient::sendeSchuss( int id, int sNum, Vertex pos, Vertex speed, double intensity, int spielZeit )
  100. {
  101. if( !klient )
  102. return;
  103. short len = 37;
  104. char *bytes = new char[ len ];
  105. *(int*)bytes = spielZeit;
  106. *(char*)( bytes + 4 ) = 0xD;
  107. *(int*)( bytes + 5 ) = id;
  108. *(int*)( bytes + 9 ) = sNum;
  109. *(float*)( bytes + 13 ) = pos.x;
  110. *(float*)( bytes + 17 ) = pos.y;
  111. *(float*)( bytes + 21 ) = speed.x;
  112. *(float*)( bytes + 25 ) = speed.y;
  113. *(double*)( bytes + 29 ) = intensity;
  114. klient->spielNachricht( len, bytes );
  115. delete[] bytes;
  116. }
  117. void Klient::sendeTreffer( int id, int sNum, int spielZeit )
  118. {
  119. if( !klient )
  120. return;
  121. short len = 13;
  122. char *bytes = new char[ len ];
  123. *(int*)bytes = spielZeit;
  124. *(char*)( bytes + 4 ) = 0xE;
  125. *(int*)( bytes + 5 ) = id;
  126. *(int*)( bytes + 9 ) = sNum;
  127. klient->spielNachricht( len, bytes );
  128. delete[] bytes;
  129. }
  130. void Klient::sendeWiederbelebung( int sNum, int spielZeit )
  131. {
  132. if( !klient )
  133. return;
  134. short len = 9;
  135. char *bytes = new char[ len ];
  136. *(int*)bytes = spielZeit;
  137. *(char*)( bytes + 4 ) = 0xF;
  138. *(int*)( bytes + 5 ) = sNum;
  139. klient->spielNachricht( len, bytes );
  140. delete[] bytes;
  141. }
  142. void Klient::sendeTod( int sNum, int killSNum, int spielZeit )
  143. {
  144. if( !klient )
  145. return;
  146. short len = 13;
  147. char *bytes = new char[ len ];
  148. *(int*)bytes = spielZeit;
  149. *(char*)( bytes + 4 ) = 0x10;
  150. *(int*)( bytes + 5 ) = sNum;
  151. *(int*)( bytes + 9 ) = killSNum;
  152. klient->spielNachricht( len, bytes );
  153. delete[] bytes;
  154. }
  155. void Klient::sendeSpielEnde( char gewonnen, int spielZeit )
  156. {
  157. if( !klient )
  158. return;
  159. short len = 6;
  160. char *bytes = new char[ len ];
  161. *(int*)bytes = spielZeit;
  162. *(char*)( bytes + 4 ) = 0xB;
  163. *(char*)( bytes + 5 ) = gewonnen;
  164. klient->spielNachricht( len, bytes );
  165. delete[] bytes;
  166. }
  167. void Klient::sendeChatNachricht( char *txt, int spielZeit )
  168. {
  169. if( !klient )
  170. return;
  171. short len = (short)( 5 + textLength( txt ) );
  172. char *bytes = new char[ len ];
  173. *(int*)bytes = spielZeit;
  174. *(char*)( bytes + 4 ) = 0x8;
  175. for( int i = 5; i < len; i++ )
  176. bytes[ i ] = txt[ i - 5 ];
  177. klient->spielNachricht( len, bytes );
  178. delete[] bytes;
  179. }
  180. void Klient::sendeStatistikChatNachricht( int vonAccount, char *txt )
  181. {
  182. if( !klient )
  183. return;
  184. short len = (short)( 5 + textLength( txt ) );
  185. char *bytes = new char[ len ];
  186. *(char*)( bytes ) = 3;
  187. *(int*)( bytes + 1 ) = vonAccount;
  188. for( int i = 5; i < len; i++ )
  189. bytes[ i ] = txt[ i - 5 ];
  190. klient->statistikNachricht( len, bytes );
  191. delete[] bytes;
  192. }
  193. void Klient::sendeStatistikSpielerOffline( int account )
  194. {
  195. if( !klient )
  196. return;
  197. char *bytes = new char[ 5 ];
  198. *(char*)( bytes ) = 4;
  199. *(int*)( bytes + 1 ) = account;
  200. klient->statistikNachricht( 5, bytes );
  201. delete[] bytes;
  202. }
  203. void Klient::sendeSpielerStatistik( SpielerStatistik *zS )
  204. {
  205. if( !zS || !klient )
  206. return;
  207. char snl = (char)zS->zSpielerName()->getLength();
  208. char tnl = (char)zS->zTeamName()->getLength();
  209. int len = 55 + snl + tnl;
  210. char *bytes = new char[ len ];
  211. bytes[ 0 ] = 0;
  212. *(int*)( bytes + 1 ) = zS->getSpielerNummer();
  213. *(char*)( bytes + 5 ) = (char)zS->zSpielerName()->getLength();
  214. for( int i = 0; i < snl; i++ )
  215. bytes[ i + 6 ] = zS->zSpielerName()->getText()[ i ];
  216. *(char*)( bytes + 6 + snl ) = tnl;
  217. for( int i = 0; i < tnl; i++ )
  218. bytes[ i + 7 + snl ] = zS->zTeamName()->getText()[ i ];
  219. *(int*)( bytes + 7 + snl + tnl ) = zS->getSpielerFarbe();
  220. *(int*)( bytes + 11 + snl + tnl ) = zS->getTeamFarbe();
  221. *(int*)( bytes + 15 + snl + tnl ) = zS->getSchadenBekommen();
  222. *(int*)( bytes + 19 + snl + tnl ) = zS->getSchadenGemacht();
  223. *(int*)( bytes + 23 + snl + tnl ) = zS->getTreibstoffVerbraucht();
  224. *(int*)( bytes + 27 + snl + tnl ) = zS->getShots();
  225. *(int*)( bytes + 31 + snl + tnl ) = zS->getTreffer();
  226. *(int*)( bytes + 35 + snl + tnl ) = zS->getPunkte();
  227. *(int*)( bytes + 39 + snl + tnl ) = zS->getKills();
  228. *(int*)( bytes + 43 + snl + tnl ) = zS->getTode();
  229. *(int*)( bytes + 47 + snl + tnl ) = zS->getZeitAmLeben();
  230. *(int*)( bytes + 51 + snl + tnl ) = zS->getZeitTod();
  231. klient->statistikNachricht( (short)len, bytes );
  232. delete[] bytes;
  233. }
  234. void Klient::sendeTeamStatistik( TeamStatistik *zS )
  235. {
  236. if( !zS || !klient )
  237. return;
  238. char tnl = (char)zS->zTeamName()->getLength();
  239. int len = 43 + tnl;
  240. char *bytes = new char[ len ];
  241. bytes[ 0 ] = 1;
  242. *(int*)( bytes + 1 ) = zS->getTeamNummer();
  243. *(char*)( bytes + 5 ) = tnl;
  244. for( int i = 0; i < tnl; i++ )
  245. bytes[ i + 6 ] = zS->zTeamName()->getText()[ i ];
  246. *(int*)( bytes + 6 + tnl ) = zS->getTeamFarbe();
  247. *(int*)( bytes + 10 + tnl ) = zS->getSchadenBekommen();
  248. *(int*)( bytes + 14 + tnl ) = zS->getSchadenGemacht();
  249. *(int*)( bytes + 18 + tnl ) = zS->getTreibstoffVerbraucht();
  250. *(int*)( bytes + 22 + tnl ) = zS->getShots();
  251. *(int*)( bytes + 26 + tnl ) = zS->getTreffer();
  252. *(int*)( bytes + 30 + tnl ) = zS->getPunkte();
  253. *(int*)( bytes + 34 + tnl ) = zS->getKills();
  254. *(int*)( bytes + 38 + tnl ) = zS->getTode();
  255. *( bytes + 42 + tnl ) = (char)zS->hatGewonnen();
  256. klient->statistikNachricht( (short)len, bytes );
  257. delete[] bytes;
  258. }
  259. void Klient::sendeStatistikLadenFertig()
  260. {
  261. if( !klient )
  262. return;
  263. char byte = 2;
  264. klient->statistikNachricht( 1, &byte );
  265. }
  266. // constant
  267. bool Klient::istOnline() const
  268. {
  269. return klient != 0;
  270. }
  271. // reference Counting
  272. Klient *Klient::getThis()
  273. {
  274. ref++;
  275. return this;
  276. }
  277. Klient *Klient::release()
  278. {
  279. ref--;
  280. if( !ref )
  281. delete this;
  282. return 0;
  283. }