#include "Klient.h"
#include "Spieler.h"
#include <Text.h>

// 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 = klient->release();
}

void Klient::online( SSKlientV *zKlient )
{
	if( klient )
		klient = klient->release();
	klient = zKlient->getThis();
}

void Klient::sendeSpielerNummer( int sNum, double spielZeit )
{
	if( !klient )
		return;
	short len = 13;
	char *bytes = new char[ len ];
	*(double*)bytes = spielZeit;
	*(char*)( bytes + 8 ) = 0;
	*(int*)( bytes + 9 ) = sNum;
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeInitSpieler( int anzahl, RCArray< Spieler > *zSpieler, double spielZeit )
{
	if( !klient )
		return;
	short len = (short)( 13 + 20 * anzahl );
	char *bytes = new char[ len ];
	*(double*)bytes = spielZeit;
	*(char*)( bytes + 8 ) = 1;
	*(int*)( bytes + 9 ) = anzahl;
	for( int i = 0; i < anzahl; i++ )
	{
		*(int*)( bytes + i * 20 + 13 ) = zSpieler->z( i )->getSpielerNummer();
		*(int*)( bytes + i * 20 + 17 ) = zSpieler->z( i )->getSpielerTeam();
		*(int*)( bytes + i * 20 + 21 ) = zSpieler->z( i )->getSpielerFarbe();
		*(int*)( bytes + i * 20 + 25 ) = zSpieler->z( i )->getTeamFarbe();
		*(int*)( bytes + i * 20 + 29 ) = zSpieler->z( i )->getAccountId();
	}
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeSpielerPosition( Spieler *zSpieler, double spielZeit )
{
	if( !klient )
		return;
	short len = 29;
	char *bytes = new char[ len ];
	*(double*)bytes = spielZeit;
	*(char*)( bytes + 8 ) = 2;
	*(int*)( bytes + 9 ) = zSpieler->getSpielerNummer();
	*(double*)( bytes + 13 ) = zSpieler->getX();
	*(double*)( bytes + 21 ) = zSpieler->getY();
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeKammeraSize( int br, int hi, double spielZeit )
{
	if( !klient )
		return;
	short len = 17;
	char *bytes = new char[ len ];
	*(double*)bytes = spielZeit;
	*(char*)( bytes + 8 ) = 3;
	*(int*)( bytes + 9 ) = br;
	*(int*)( bytes + 13 ) = hi;
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeStart( double spielZeit )
{
	if( !klient )
		return;
	short len = 9;
	char *bytes = new char[ len ];
	*(double*)bytes = spielZeit;
	*(char*)( bytes + 8 ) = 4;
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeSpielerTod( Spieler *zSpieler, double spielZeit )
{
	if( !klient )
		return;
	short len = 13;
	char *bytes = new char[ len ];
	*(double*)bytes = spielZeit;
	*(char*)( bytes + 8 ) = 5;
	*(int*)( bytes + 9 ) = zSpieler->getSpielerNummer();
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeSpielEnde( char gewonnen, double spielZeit )
{
	if( !klient )
		return;
	short len = 10;
	char *bytes = new char[ len ];
	*(double*)bytes = spielZeit;
	*(char*)( bytes + 8 ) = 6;
	*(char*)( bytes + 9 ) = gewonnen;
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeSpielerGeschwindigkeit( Spieler *zSpieler, double spielZeit )
{
	if( !klient )
		return;
	short len = 29;
	char *bytes = new char[ len ];
	*(double*)bytes = spielZeit;
	*(char*)( bytes + 8 ) = 7;
	*(int*)( bytes + 9 ) = zSpieler->getSpielerNummer();
	*(double*)( bytes + 13 ) = zSpieler->getSpeedX();
	*(double*)( bytes + 21 ) = zSpieler->getSpeedY();
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeSpielerWendigkeit( Spieler *zSpieler, double spielZeit )
{
	if( !klient )
		return;
	short len = 21;
	char *bytes = new char[ len ];
	*(double*)bytes = spielZeit;
	*(char*)( bytes + 8 ) = 8;
	*(int*)( bytes + 9 ) = zSpieler->getSpielerNummer();
	*(double*)( bytes + 13 ) = zSpieler->getKurve();
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeSpielerKurve( int sNum, char kurve, double spielZeit )
{
	if( !klient )
		return;
	short len = 14;
	char *bytes = new char[ len ];
	*(double*)bytes = spielZeit;
	*(char*)( bytes + 8 ) = 9;
	*(int*)( bytes + 9 ) = sNum;
	bytes[ 13 ] = kurve;
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeKartenSize( int br, int hi, double spielZeit )
{
	if( !klient )
		return;
	short len = 17;
	char *bytes = new char[ len ];
	*(double*)bytes = spielZeit;
	*(char*)( bytes + 8 ) = 0xA;
	*(int*)( bytes + 9 ) = br;
	*(int*)( bytes + 13 ) = hi;
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeChatNachricht( char *txt, double spielZeit )
{
	if( !klient )
		return;
	short len = (short)( 9 + textLength( txt ) );
	char *bytes = new char[ len ];
	*(double*)bytes = spielZeit;
	*(char*)( bytes + 8 ) = 0xB;
	for( int i = 9; i < len; i++ )
		bytes[ i ] = txt[ i - 9 ];
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeLinienUnterbrechung( int sNum, bool unterbrochen, double spielZeit )
{
	if( !klient )
		return;
	char *bytes = new char[ 14 ];
	*(double*)bytes = spielZeit;
	*(char*)( bytes + 8 ) = 0xC;
	*(int*)( bytes + 9 ) = sNum;
	*( bytes + 13 ) = (char)unterbrochen;
	klient->spielNachricht( 14, bytes );
	delete[] bytes;
}

void Klient::sendeTeamMaxPunkte( int team, int mP, double spielZeit )
{
	if( !klient )
		return;
	char *bytes = new char[ 17 ];
	*(double*)bytes = spielZeit;
	*(char*)( bytes + 8 ) = 0xD;
	*(int*)( bytes + 9 ) = team;
	*(int*)( bytes + 13 ) = mP;
	klient->spielNachricht( 17, bytes );
	delete[] bytes;
}

void Klient::sendeTeamPunkte( int team, int p, double spielZeit )
{
	if( !klient )
		return;
	char *bytes = new char[ 17 ];
	*(double*)bytes = spielZeit;
	*(char*)( bytes + 8 ) = 0xE;
	*(int*)( bytes + 9 ) = team;
	*(int*)( bytes + 13 ) = p;
	klient->spielNachricht( 17, bytes );
	delete[] bytes;
}

void Klient::sendeSpielerPunkte( int sNum, int p, double spielZeit )
{
	if( !klient )
		return;
	char *bytes = new char[ 17 ];
	*(double*)bytes = spielZeit;
	*(char*)( bytes + 8 ) = 0xF;
	*(int*)( bytes + 9 ) = sNum;
	*(int*)( bytes + 13 ) = p;
	klient->spielNachricht( 17, bytes );
	delete[] bytes;
}

void Klient::sendeTeamTod( int team, double spielZeit )
{
	if( !klient )
		return;
	char *bytes = new char[ 13 ];
	*(double*)bytes = spielZeit;
	*(char*)( bytes + 8 ) = 0x10;
	*(int*)( bytes + 9 ) = team;
	klient->spielNachricht( 13, bytes );
	delete[] bytes;
}

void Klient::sendeTeamName( int team, char *name )
{
	if( !klient )
		return;
	int nL = textLength( name );
	char *bytes = new char[ 13 + nL ];
	*(double*)bytes = 0;
	*(char*)( bytes + 8 ) = 0x13;
	*(int*)( bytes + 9 ) = team;
	memcpy( bytes + 13, name, nL );
	klient->spielNachricht( (short)( 13 + nL ), bytes );
	delete[] bytes;
}

void Klient::sendeRundenEnde( int winTeam, double spielZeit )
{
	if( !klient )
		return;
	char *bytes = new char[ 13 ];
	*(double*)bytes = spielZeit;
	*(char*)( bytes + 8 ) = 0x11;
	*(int*)( bytes + 9 ) = winTeam;
	klient->spielNachricht( 13, bytes );
	delete[] bytes;
}

void Klient::sendeZeitVerbleibend( int sekunden, double spielZeit )
{
	if( !klient )
		return;
	char *bytes = new char[ 13 ];
	*(double*)bytes = spielZeit;
	*(char*)( bytes + 8 ) = 0x12;
	*(int*)( bytes + 9 ) = sekunden;
	klient->spielNachricht( 13, 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 ) = 8;
	*(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 ) = 9;
	*(int*)( bytes + 1 ) = account;
	klient->statistikNachricht( 5, bytes );
	delete[] bytes;
}

void Klient::sendeGesamtSpielerStatistik( GesamtSpielerStatistik *zS )
{
	if( !zS || !klient )
		return;
	char snl = (char)zS->zSpielerName()->getLength();
	char tnl = (char)zS->zTeamName()->getLength();
	int len = 43 + snl + tnl;
	char *bytes = new char[ len ];
	bytes[ 0 ] = 2;
	*(int*)( bytes + 1 ) = zS->getSpielerNummer();
	*(char*)( bytes + 5 ) = (char)zS->zSpielerName()->getLength();
	for( int i = 0; i < snl; i++ )
		bytes[ i + 6 ] = zS->zSpielerName()->getText()[ i ];
	*(char*)( bytes + 6 + snl ) = tnl;
	for( int i = 0; i < tnl; i++ )
		bytes[ i + 7 + snl ] = zS->zTeamName()->getText()[ i ];
	*(int*)( bytes + 7 + snl + tnl ) = zS->getSpielerFarbe();
	*(int*)( bytes + 11 + snl + tnl ) = zS->getTeamFarbe();
	*(int*)( bytes + 15 + snl + tnl ) = zS->getGewonneneRunden();
	*(int*)( bytes + 19 + snl + tnl ) = zS->getPunkte();
	*(int*)( bytes + 23 + snl + tnl ) = zS->getLinienLength();
	*(int*)( bytes + 27 + snl + tnl ) = zS->getZeitAmLeben();
	*(int*)( bytes + 31 + snl + tnl ) = zS->getZeitTod();
	*(int*)( bytes + 35 + snl + tnl ) = zS->getKills();
	*(int*)( bytes + 39 + snl + tnl ) = zS->getTode();
	klient->statistikNachricht( (short)len, bytes );
	delete[] bytes;
}

void Klient::sendeGesamtTeamStatistik( GesamtTeamStatistik *zS )
{
	if( !zS || !klient )
		return;
	char tnl = (char)zS->zTeamName()->getLength();
	int len = 30 + tnl;
	char *bytes = new char[ len ];
	bytes[ 0 ] = 3;
	*(int*)( bytes + 1 ) = zS->getTeamNummer();
	*(char*)( bytes + 5 ) = tnl;
	for( int i = 0; i < tnl; i++ )
		bytes[ i + 6 ] = zS->zTeamName()->getText()[ i ];
	*(int*)( bytes + 6 + tnl ) = zS->getTeamFarbe();
	*(int*)( bytes + 10 + tnl ) = zS->getGewonneneRunden();
	*(int*)( bytes + 14 + tnl ) = zS->getPunkte();
	*(int*)( bytes + 18 + tnl ) = zS->getLinienLength();
	*(int*)( bytes + 22 + tnl ) = zS->getKills();
	*(int*)( bytes + 26 + tnl ) = zS->getTode();
	klient->statistikNachricht( (short)len, bytes );
	delete[] bytes;
}

void Klient::sendeRundeStatistik( RundenRundenStatistik *zS )
{
	if( !zS || !klient )
		return;
	char *bytes = new char[ 25 ];
	bytes[ 0 ] = 4;
	*(int*)( bytes + 1 ) = zS->getRundenNummer();
	*(int*)( bytes + 5 ) = zS->getRundenDauer();
	*(int*)( bytes + 9 ) = zS->getSiegerTeam();
	*(int*)( bytes + 13 ) = zS->getSpielFeldNutzung();
	*(int*)( bytes + 17 ) = zS->zRundenBild()->getBreite();
	*(int*)( bytes + 21 ) = zS->zRundenBild()->getHeight();
	klient->statistikNachricht( 25, bytes );
	delete[] bytes;
	Bild *zB = zS->zRundenBild();
	int pixel = zB->getBreite() * zB->getHeight();
	while( pixel )
	{
		int pStart = zB->getBreite() * zB->getHeight() - pixel;
		int pLen = pixel > 200 ? 200 : pixel;
		char *bytes = new char[ 13 + pLen * 4 ];
		bytes[ 0 ] = 5;
		*(int*)( bytes + 1 ) = zS->getRundenNummer();
		*(int*)( bytes + 5 ) = pStart;
		*(int*)( bytes + 9 ) = pLen;
		for( int i = 0; i < pLen; i++ )
			*(int*)( bytes + 13 + i * 4 ) = zB->getBuffer()[ pStart + i ];
		klient->statistikNachricht( (short)( 13 + pLen * 4 ), bytes );
		delete[] bytes;
		pixel -= pLen;
	}
	int anz = zS->getSpielerAnzahl();
	for( int i = 0; i < anz; i++ )
		sendeRundeSpielerStatistik( zS->zSpielerStatistik( i ), zS->getRundenNummer() );
	anz = zS->getTeamAnzahl();
	for( int i = 0; i < anz; i++ )
		sendeRundeTeamStatistik( zS->zTeamStatistik( i ), zS->getRundenNummer() );
}

void Klient::sendeRundeSpielerStatistik( RundenSpielerStatistik *zS, int runde )
{
	if( !zS || !klient )
		return;
	char snl = (char)zS->zSpielerName()->getLength();
	char tnl = (char)zS->zTeamName()->getLength();
	int len = 31 + snl + tnl;
	char *bytes = new char[ len ];
	bytes[ 0 ] = 6;
	*(int*)( bytes + 1 ) = runde;
	*(int*)( bytes + 5 ) = zS->getSpielerNummer();
	*(char*)( bytes + 9 ) = snl;
	for( int i = 0; i < snl; i++ )
		bytes[ i + 10 ] = zS->zSpielerName()->getText()[ i ];
	*(char*)( bytes + 10 + snl ) = tnl;
	for( int i = 0; i < tnl; i++ )
		bytes[ i + 11 + snl ] = zS->zTeamName()->getText()[ i ];
	*(int*)( bytes + 11 + snl + tnl ) = zS->getSpielerFarbe();
	*(int*)( bytes + 15 + snl + tnl ) = zS->getTeamFarbe();
	*(int*)( bytes + 19 + snl + tnl ) = zS->getLinienLength();
	*(int*)( bytes + 23 + snl + tnl ) = zS->getKills();
	*(int*)( bytes + 27 + snl + tnl ) = zS->getTodesZeit();
	klient->statistikNachricht( (short)len, bytes );
	delete[] bytes;
}

void Klient::sendeRundeTeamStatistik( RundenTeamStatistik *zS, int runde )
{
	if( !zS || !klient )
		return;
	char tnl = (char)zS->zTeamName()->getLength();
	int len = 27 + tnl;
	char *bytes = new char[ len ];
	bytes[ 0 ] = 7;
	*(int*)( bytes + 1 ) = runde;
	*(int*)( bytes + 5 ) = zS->getTeamNummer();
	*(char*)( bytes + 9 ) = tnl;
	for( int i = 0; i < tnl; i++ )
		bytes[ i + 10 ] = zS->zTeamName()->getText()[ i ];
	*(int*)( bytes + 10 + tnl ) = zS->getTeamFarbe();
	*(char*)( bytes + 14 + tnl ) = (char)zS->getErgebnis();
	*(int*)( bytes + 15 + tnl ) = zS->getLinienLength();
	*(int*)( bytes + 19 + tnl ) = zS->getKills();
	*(int*)( bytes + 23 + tnl ) = zS->getTode();
	klient->statistikNachricht( (short)len, bytes );
	delete[] bytes;
}

void Klient::sendeStatistikLadenFertig()
{
	if( !klient )
		return;
	char byte = 1;
	klient->statistikNachricht( 1, &byte );
}

// constant
bool Klient::istOnline() const
{
	return klient != 0;
}

// reference Counting
Klient *Klient::getThie()
{
	ref++;
	return this;
}

Klient *Klient::release()
{
	ref--;
	if( !ref )
		delete this;
	return 0;
}