
#include "GameWorld.h"
#include "GameWorldClient.h"
#include "Player.h"
#include "GameNetInterface.h"
#include "GameConnection.h"

using namespace TNL;
using namespace DinastyOnline::Network;

namespace DinastyOnline {
	namespace Network {

		GameWorldClient::GameWorldClient(const TNL::Address &interfaceAddr, TNL::StringTableEntry login, TNL::StringTableEntry password) {
			Address anyBindAddress(IPProtocol, Address::Any, 0);
			theNetInterface = new GameNetInterface(this, anyBindAddress);
	
			TNL::AsymmetricKey *theKey = new TNL::AsymmetricKey(32);
			theNetInterface->setPrivateKey(theKey);
			theNetInterface->setRequiresKeyExchange(true);
	
			clientConn = new GameConnection(login, password);
			clientConn->connect(theNetInterface, interfaceAddr);
	
			TNL::logprintf("Created a client...");
		}
		
		GameWorldClient::~GameWorldClient() {
			delete theNetInterface;
			for(TNL::S32 i = 0; i < mGameObjects.size(); i++) {
				delete mGameObjects[i];
			}
	
			TNL::logprintf("Destroyed a client...");
		}
		
		void GameWorldClient::setClientPlayer(Player *player) {
			this->clientPlayer = player;
		}
		
		void GameWorldClient::setClientName(const char *string) {
			clientConn->setClientName(string);
		}
		
		void GameWorldClient::moveMyPlayerTo(Position newPosition) {
			if (clientConn != NULL) {
				TNL::logprintf("posting new position (%i, %i) to server", newPosition.x, newPosition.y);
				if(!clientPlayer.isNull()) {
					// Just testing RPC calls to parentGhost
					clientPlayer->rpcPlayerWillMove(0);
				}
					clientConn->rpcSetPlayerPos(newPosition.x, newPosition.y);
			}
		}
		
	}
}
