Klient.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 )
  29. {
  30. if( !klient )
  31. return;
  32. short len = (short)( 2 + zSpieler->getEintragAnzahl() * 8 );
  33. char *bytes = new char[ len ];
  34. *(char *)( bytes ) = 0x1;
  35. *(char *)( bytes + 1 ) = (char)zSpieler->getEintragAnzahl();
  36. for( int i = 0; i < zSpieler->getEintragAnzahl(); i++ )
  37. {
  38. *(int *)( bytes + 2 + i * 8 ) = zSpieler->z( i )->getId();
  39. *(int *)( bytes + 6 + i * 8 ) = zSpieler->z( i )->getAccountId();
  40. }
  41. klient->spielNachricht( len, bytes );
  42. delete[] bytes;
  43. }
  44. void Klient::sendeSpielerNummer( int sNum )
  45. {
  46. if( !klient )
  47. return;
  48. short len = 5;
  49. char *bytes = new char[ len ];
  50. *(char *)( bytes ) = 0x2;
  51. *(int *)( bytes + 1 ) = sNum;
  52. klient->spielNachricht( len, bytes );
  53. delete[] bytes;
  54. }
  55. void Klient::sendeStart()
  56. {
  57. if( !klient )
  58. return;
  59. char b = 0x3;
  60. klient->spielNachricht( 1, &b );
  61. }
  62. void Klient::sendeTastaturStatus( int spielerId, char taste, bool aktiv )
  63. {
  64. if( !klient )
  65. return;
  66. short len = 7;
  67. char *bytes = new char[ len ];
  68. *(char *)( bytes ) = 0x4;
  69. *(char *)( bytes + 1 ) = taste;
  70. *(char *)( bytes + 2 ) = (char)aktiv;
  71. *(int *)( bytes + 3 ) = spielerId;
  72. klient->spielNachricht( len, bytes );
  73. delete[] bytes;
  74. }
  75. void Klient::sendeSpielEnde( char gewonnen )
  76. {
  77. if( !klient )
  78. return;
  79. short len = 2;
  80. char *bytes = new char[ len ];
  81. *(char *)( bytes ) = 0x5;
  82. *(char *)( bytes + 1 ) = gewonnen;
  83. klient->spielNachricht( len, bytes );
  84. delete[] bytes;
  85. }
  86. void Klient::sendeTick()
  87. {
  88. if( !klient )
  89. return;
  90. char b = 0x6;
  91. klient->spielNachricht( 1, &b );
  92. }
  93. void Klient::sendeChatNachricht( char *txt )
  94. {
  95. if( !klient )
  96. return;
  97. short len = (short)( 1 + textLength( txt ) );
  98. char *bytes = new char[ len ];
  99. *(char *)( bytes ) = 0x7;
  100. for( int i = 1; i < len; i++ )
  101. bytes[ i ] = txt[ i - 1 ];
  102. klient->spielNachricht( len, bytes );
  103. delete[] bytes;
  104. }
  105. void Klient::sendeStatistikChatNachricht( int vonAccount, char *txt )
  106. {
  107. if( !klient )
  108. return;
  109. short len = (short)( 5 + textLength( txt ) );
  110. char *bytes = new char[ len ];
  111. *(char *)( bytes ) = 3;
  112. *(int *)( bytes + 1 ) = vonAccount;
  113. for( int i = 5; i < len; i++ )
  114. bytes[ i ] = txt[ i - 5 ];
  115. klient->statistikNachricht( len, bytes );
  116. delete[] bytes;
  117. }
  118. void Klient::sendeStatistikSpielerOffline( int account )
  119. {
  120. if( !klient )
  121. return;
  122. char *bytes = new char[ 5 ];
  123. *(char *)( bytes ) = 4;
  124. *(int *)( bytes + 1 ) = account;
  125. klient->statistikNachricht( 5, bytes );
  126. delete[] bytes;
  127. }
  128. void Klient::sendeSpielerStatistik( Spieler *zS )
  129. {
  130. if( !zS || !klient )
  131. return;
  132. char snl = (char)textLength( zS->getName() );
  133. char tnl = (char)zS->zTeam()->getName().getLength();
  134. int len = 55 + snl + tnl;
  135. char *bytes = new char[ len ];
  136. bytes[ 0 ] = 0;
  137. *(int *)( bytes + 1 ) = zS->getId();
  138. *(char *)( bytes + 5 ) = snl;
  139. for( int i = 0; i < snl; i++ )
  140. bytes[ i + 6 ] = zS->getName()[ i ];
  141. *(char *)( bytes + 6 + snl ) = tnl;
  142. for( int i = 0; i < tnl; i++ )
  143. bytes[ i + 7 + snl ] = zS->zTeam()->getName()[ i ];
  144. *(int *)( bytes + 7 + snl + tnl ) = zS->getFarbe();
  145. *(int *)( bytes + 11 + snl + tnl ) = zS->zTeam()->getFarbe();
  146. *(int *)( bytes + 15 + snl + tnl ) = (int)zS->getErlittenerSchaden();
  147. *(int *)( bytes + 19 + snl + tnl ) = (int)zS->getGemachterSchaden();
  148. *(int *)( bytes + 23 + snl + tnl ) = (int)zS->getGeheiltesLeben();
  149. *(int *)( bytes + 27 + snl + tnl ) = zS->getGeschossen();
  150. *(int *)( bytes + 31 + snl + tnl ) = zS->getTreffer();
  151. *(int *)( bytes + 35 + snl + tnl ) = zS->getPunkte();
  152. *(int *)( bytes + 39 + snl + tnl ) = zS->getKills();
  153. *(int *)( bytes + 43 + snl + tnl ) = zS->getTode();
  154. *(int *)( bytes + 47 + snl + tnl ) = zS->getItemsAufgehoben();
  155. *(int *)( bytes + 51 + snl + tnl ) = zS->getItemsVerwendet();
  156. klient->statistikNachricht( (short)len, bytes );
  157. delete[] bytes;
  158. }
  159. void Klient::sendeTeamStatistik( Team *zS )
  160. {
  161. if( !zS || !klient )
  162. return;
  163. char tnl = (char)zS->getName().getLength();
  164. int len = 22 + tnl;
  165. char *bytes = new char[ len ];
  166. bytes[ 0 ] = 1;
  167. *(int *)( bytes + 1 ) = zS->getTeamNummer();
  168. *(char *)( bytes + 5 ) = tnl;
  169. for( int i = 0; i < tnl; i++ )
  170. bytes[ i + 6 ] = zS->getName()[ i ];
  171. *(int *)( bytes + 6 + tnl ) = zS->getFarbe();
  172. *(int *)( bytes + 10 + tnl ) = zS->getPunkte();
  173. *(int *)( bytes + 14 + tnl ) = zS->getKills();
  174. *(int *)( bytes + 18 + tnl ) = zS->getTode();
  175. klient->statistikNachricht( (short)len, bytes );
  176. delete[] bytes;
  177. }
  178. void Klient::sendeStatistikLadenFertig()
  179. {
  180. if( !klient )
  181. return;
  182. char byte = 2;
  183. klient->statistikNachricht( 1, &byte );
  184. }
  185. // constant
  186. bool Klient::istOnline() const
  187. {
  188. return klient != 0;
  189. }
  190. // reference Counting
  191. Klient *Klient::getThis()
  192. {
  193. ref++;
  194. return this;
  195. }
  196. Klient *Klient::release()
  197. {
  198. ref--;
  199. if( !ref )
  200. delete this;
  201. return 0;
  202. }