Klient.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. : ReferenceCounter()
  8. {
  9. this->klient = klient;
  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 = dynamic_cast<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::sendeObjektTreffer(int id, int oId, int spielZeit, float ep, int skillP)
  185. {
  186. if (!klient)
  187. return;
  188. short len = 21;
  189. char* bytes = new char[len];
  190. *(int*)bytes = spielZeit;
  191. *(char*)(bytes + 4) = 0x16;
  192. *(int*)(bytes + 5) = id;
  193. *(int*)(bytes + 9) = oId;
  194. *(float*)(bytes + 13) = ep;
  195. *(int*)(bytes + 17) = skillP;
  196. klient->spielNachricht(len, bytes);
  197. delete[] bytes;
  198. }
  199. void Klient::sendeObjektTod(int oId, int killSNum, int spielZeit)
  200. {
  201. if (!klient)
  202. return;
  203. short len = 13;
  204. char* bytes = new char[len];
  205. *(int*)bytes = spielZeit;
  206. *(char*)(bytes + 4) = 0x17;
  207. *(int*)(bytes + 5) = oId;
  208. *(int*)(bytes + 9) = killSNum;
  209. klient->spielNachricht(len, bytes);
  210. delete[] bytes;
  211. }
  212. void Klient::sendeAsteroidTreffer(int asteroidId, int newAsteroidId, int schussId, Vertex pos, __int64 seed, int spielZeit, float ep, int skillP)
  213. {
  214. if (!klient)
  215. return;
  216. short len = 41;
  217. char* bytes = new char[len];
  218. *(int*)bytes = spielZeit;
  219. *(char*)(bytes + 4) = 0x12;
  220. *(int*)(bytes + 5) = schussId;
  221. *(int*)(bytes + 9) = asteroidId;
  222. *(float*)(bytes + 13) = pos.x;
  223. *(float*)(bytes + 17) = pos.y;
  224. *(__int64*)(bytes + 21) = seed;
  225. *(int*)(bytes + 29) = newAsteroidId;
  226. *(float*)(bytes + 33) = ep;
  227. *(int*)(bytes + 37) = skillP;
  228. klient->spielNachricht(len, bytes);
  229. delete[] bytes;
  230. }
  231. void Klient::sendeWiederbelebung(int sNum, int spielZeit)
  232. {
  233. if (!klient)
  234. return;
  235. short len = 9;
  236. char* bytes = new char[len];
  237. *(int*)bytes = spielZeit;
  238. *(char*)(bytes + 4) = 0xF;
  239. *(int*)(bytes + 5) = sNum;
  240. klient->spielNachricht(len, bytes);
  241. delete[] bytes;
  242. }
  243. void Klient::sendeTod(int sNum, int killSNum, int spielZeit)
  244. {
  245. if (!klient)
  246. return;
  247. short len = 13;
  248. char* bytes = new char[len];
  249. *(int*)bytes = spielZeit;
  250. *(char*)(bytes + 4) = 0x10;
  251. *(int*)(bytes + 5) = sNum;
  252. *(int*)(bytes + 9) = killSNum;
  253. klient->spielNachricht(len, bytes);
  254. delete[] bytes;
  255. }
  256. void Klient::sendeSpielEnde(char gewonnen, int spielZeit)
  257. {
  258. if (!klient)
  259. return;
  260. short len = 6;
  261. char* bytes = new char[len];
  262. *(int*)bytes = spielZeit;
  263. *(char*)(bytes + 4) = 0xB;
  264. *(char*)(bytes + 5) = gewonnen;
  265. klient->spielNachricht(len, bytes);
  266. delete[] bytes;
  267. }
  268. void Klient::sendeChatNachricht(const char* txt, int spielZeit)
  269. {
  270. if (!klient)
  271. return;
  272. short len = (short)(5 + textLength(txt));
  273. char* bytes = new char[len];
  274. *(int*)bytes = spielZeit;
  275. *(char*)(bytes + 4) = 0x8;
  276. for (int i = 5; i < len; i++)
  277. bytes[i] = txt[i - 5];
  278. klient->spielNachricht(len, bytes);
  279. delete[] bytes;
  280. }
  281. void Klient::sendeStatistikChatNachricht(int vonAccount, char* txt)
  282. {
  283. if (!klient)
  284. return;
  285. short len = (short)(5 + textLength(txt));
  286. char* bytes = new char[len];
  287. *(char*)(bytes) = 3;
  288. *(int*)(bytes + 1) = vonAccount;
  289. for (int i = 5; i < len; i++)
  290. bytes[i] = txt[i - 5];
  291. klient->statistikNachricht(len, bytes);
  292. delete[] bytes;
  293. }
  294. void Klient::sendeStatistikSpielerOffline(int account)
  295. {
  296. if (!klient)
  297. return;
  298. char* bytes = new char[5];
  299. *(char*)(bytes) = 4;
  300. *(int*)(bytes + 1) = account;
  301. klient->statistikNachricht(5, bytes);
  302. delete[] bytes;
  303. }
  304. void Klient::sendeSpielerStatistik(SpielerStatistik* zS)
  305. {
  306. if (!zS || !klient)
  307. return;
  308. char snl = (char)zS->zSpielerName()->getLength();
  309. char tnl = (char)zS->zTeamName()->getLength();
  310. int len = 55 + snl + tnl;
  311. char* bytes = new char[len];
  312. bytes[0] = 0;
  313. *(int*)(bytes + 1) = zS->getSpielerNummer();
  314. *(char*)(bytes + 5) = (char)zS->zSpielerName()->getLength();
  315. for (int i = 0; i < snl; i++)
  316. bytes[i + 6] = zS->zSpielerName()->getText()[i];
  317. *(char*)(bytes + 6 + snl) = tnl;
  318. for (int i = 0; i < tnl; i++)
  319. bytes[i + 7 + snl] = zS->zTeamName()->getText()[i];
  320. *(int*)(bytes + 7 + snl + tnl) = zS->getSpielerFarbe();
  321. *(int*)(bytes + 11 + snl + tnl) = zS->getTeamFarbe();
  322. *(int*)(bytes + 15 + snl + tnl) = zS->getSchadenBekommen();
  323. *(int*)(bytes + 19 + snl + tnl) = zS->getSchadenGemacht();
  324. *(int*)(bytes + 23 + snl + tnl) = zS->getTreibstoffVerbraucht();
  325. *(int*)(bytes + 27 + snl + tnl) = zS->getShots();
  326. *(int*)(bytes + 31 + snl + tnl) = zS->getTreffer();
  327. *(int*)(bytes + 35 + snl + tnl) = zS->getPunkte();
  328. *(int*)(bytes + 39 + snl + tnl) = zS->getKills();
  329. *(int*)(bytes + 43 + snl + tnl) = zS->getTode();
  330. *(int*)(bytes + 47 + snl + tnl) = zS->getZeitAmLeben();
  331. *(int*)(bytes + 51 + snl + tnl) = zS->getZeitTod();
  332. klient->statistikNachricht((short)len, bytes);
  333. delete[] bytes;
  334. }
  335. void Klient::sendeTeamStatistik(TeamStatistik* zS)
  336. {
  337. if (!zS || !klient)
  338. return;
  339. char tnl = (char)zS->zTeamName()->getLength();
  340. int len = 43 + tnl;
  341. char* bytes = new char[len];
  342. bytes[0] = 1;
  343. *(int*)(bytes + 1) = zS->getTeamNummer();
  344. *(char*)(bytes + 5) = tnl;
  345. for (int i = 0; i < tnl; i++)
  346. bytes[i + 6] = zS->zTeamName()->getText()[i];
  347. *(int*)(bytes + 6 + tnl) = zS->getTeamFarbe();
  348. *(int*)(bytes + 10 + tnl) = zS->getSchadenBekommen();
  349. *(int*)(bytes + 14 + tnl) = zS->getSchadenGemacht();
  350. *(int*)(bytes + 18 + tnl) = zS->getTreibstoffVerbraucht();
  351. *(int*)(bytes + 22 + tnl) = zS->getShots();
  352. *(int*)(bytes + 26 + tnl) = zS->getTreffer();
  353. *(int*)(bytes + 30 + tnl) = zS->getPunkte();
  354. *(int*)(bytes + 34 + tnl) = zS->getKills();
  355. *(int*)(bytes + 38 + tnl) = zS->getTode();
  356. *(bytes + 42 + tnl) = (char)zS->hatGewonnen();
  357. klient->statistikNachricht((short)len, bytes);
  358. delete[] bytes;
  359. }
  360. void Klient::sendeStatistikLadenFertig()
  361. {
  362. if (!klient)
  363. return;
  364. char byte = 2;
  365. klient->statistikNachricht(1, &byte);
  366. }
  367. // constant
  368. bool Klient::istOnline() const
  369. {
  370. return klient != 0;
  371. }