#include "Spieler.h" #include "SSKlient.h" #include // Inhalt der Klient Klasse aus Klient.h // Konstruktor Klient::Klient( SSKlientV *klient ) { this->klient = klient; ref = 1; } // Destruktor Klient::~Klient() { if( klient ) klient->release(); } // nicht constant void Klient::offline() { klient = (SSKlientV *)klient->release(); } void Klient::online( SSKlientV *zKlient ) { if( klient ) klient = (SSKlientV *)klient->release(); klient = (SSKlientV *)zKlient->getThis(); } void Klient::sendeInit( RCArray< Spieler > *zSpieler ) { if( !klient ) return; short len = (short)( 2 + zSpieler->getEintragAnzahl() * 8 ); char *bytes = new char[ len ]; *(char *)( bytes ) = 0x1; *(char *)( bytes + 1 ) = (char)zSpieler->getEintragAnzahl(); for( int i = 0; i < zSpieler->getEintragAnzahl(); i++ ) { *(int *)( bytes + 2 + i * 8 ) = zSpieler->z( i )->getId(); *(int *)( bytes + 6 + i * 8 ) = zSpieler->z( i )->getAccountId(); } klient->spielNachricht( len, bytes ); delete[] bytes; } void Klient::sendeSpielerNummer( int sNum ) { if( !klient ) return; short len = 5; char *bytes = new char[ len ]; *(char *)( bytes ) = 0x2; *(int *)( bytes + 1 ) = sNum; klient->spielNachricht( len, bytes ); delete[] bytes; } void Klient::sendeStart() { if( !klient ) return; char b = 0x3; klient->spielNachricht( 1, &b ); } void Klient::sendeTastaturStatus( int spielerId, char taste, bool aktiv ) { if( !klient ) return; short len = 7; char *bytes = new char[ len ]; *(char *)( bytes ) = 0x4; *(char *)( bytes + 1 ) = taste; *(char *)( bytes + 2 ) = (char)aktiv; *(int *)( bytes + 3 ) = spielerId; klient->spielNachricht( len, bytes ); delete[] bytes; } void Klient::sendeSpielEnde( char gewonnen ) { if( !klient ) return; short len = 2; char *bytes = new char[ len ]; *(char *)( bytes ) = 0x5; *(char *)( bytes + 1 ) = gewonnen; klient->spielNachricht( len, bytes ); delete[] bytes; } void Klient::sendeTick() { if( !klient ) return; char b = 0x6; klient->spielNachricht( 1, &b ); } void Klient::sendeChatNachricht( char *txt ) { if( !klient ) return; short len = (short)( 1 + textLength( txt ) ); char *bytes = new char[ len ]; *(char *)( bytes ) = 0x7; for( int i = 1; i < len; i++ ) bytes[ i ] = txt[ i - 1 ]; klient->spielNachricht( len, bytes ); delete[] bytes; } void Klient::sendeStatistikChatNachricht( int vonAccount, char *txt ) { if( !klient ) return; short len = (short)( 5 + textLength( txt ) ); char *bytes = new char[ len ]; *(char *)( bytes ) = 3; *(int *)( bytes + 1 ) = vonAccount; for( int i = 5; i < len; i++ ) bytes[ i ] = txt[ i - 5 ]; klient->statistikNachricht( len, bytes ); delete[] bytes; } void Klient::sendeStatistikSpielerOffline( int account ) { if( !klient ) return; char *bytes = new char[ 5 ]; *(char *)( bytes ) = 4; *(int *)( bytes + 1 ) = account; klient->statistikNachricht( 5, bytes ); delete[] bytes; } void Klient::sendeSpielerStatistik( Spieler *zS ) { if( !zS || !klient ) return; char snl = (char)textLength( zS->getName() ); char tnl = (char)zS->zTeam()->getName().getLength(); int len = 55 + snl + tnl; char *bytes = new char[ len ]; bytes[ 0 ] = 0; *(int *)( bytes + 1 ) = zS->getId(); *(char *)( bytes + 5 ) = snl; for( int i = 0; i < snl; i++ ) bytes[ i + 6 ] = zS->getName()[ i ]; *(char *)( bytes + 6 + snl ) = tnl; for( int i = 0; i < tnl; i++ ) bytes[ i + 7 + snl ] = zS->zTeam()->getName()[ i ]; *(int *)( bytes + 7 + snl + tnl ) = zS->getFarbe(); *(int *)( bytes + 11 + snl + tnl ) = zS->zTeam()->getFarbe(); *(int *)( bytes + 15 + snl + tnl ) = (int)zS->getErlittenerSchaden(); *(int *)( bytes + 19 + snl + tnl ) = (int)zS->getGemachterSchaden(); *(int *)( bytes + 23 + snl + tnl ) = (int)zS->getGeheiltesLeben(); *(int *)( bytes + 27 + snl + tnl ) = zS->getGeschossen(); *(int *)( bytes + 31 + snl + tnl ) = zS->getTreffer(); *(int *)( bytes + 35 + snl + tnl ) = zS->getPunkte(); *(int *)( bytes + 39 + snl + tnl ) = zS->getKills(); *(int *)( bytes + 43 + snl + tnl ) = zS->getTode(); *(int *)( bytes + 47 + snl + tnl ) = zS->getItemsAufgehoben(); *(int *)( bytes + 51 + snl + tnl ) = zS->getItemsVerwendet(); klient->statistikNachricht( (short)len, bytes ); delete[] bytes; } void Klient::sendeTeamStatistik( Team *zS ) { if( !zS || !klient ) return; char tnl = (char)zS->getName().getLength(); int len = 22 + tnl; char *bytes = new char[ len ]; bytes[ 0 ] = 1; *(int *)( bytes + 1 ) = zS->getTeamNummer(); *(char *)( bytes + 5 ) = tnl; for( int i = 0; i < tnl; i++ ) bytes[ i + 6 ] = zS->getName()[ i ]; *(int *)( bytes + 6 + tnl ) = zS->getFarbe(); *(int *)( bytes + 10 + tnl ) = zS->getPunkte(); *(int *)( bytes + 14 + tnl ) = zS->getKills(); *(int *)( bytes + 18 + tnl ) = zS->getTode(); klient->statistikNachricht( (short)len, bytes ); delete[] bytes; } void Klient::sendeStatistikLadenFertig() { if( !klient ) return; char byte = 2; klient->statistikNachricht( 1, &byte ); } // constant bool Klient::istOnline() const { return klient != 0; } // reference Counting Klient *Klient::getThis() { ref++; return this; } Klient *Klient::release() { ref--; if( !ref ) delete this; return 0; }