Klient.cpp 7.4 KB

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