#include "robot.hpp"


//------------------------ ROBOT Module -------------------------//
//						  v 08 . 03 .  2007						 //
//																 //
//								MT								 //
//---------------------------------------------------------------//



// --- Constructor ROBOT --- //
CRobot :: CRobot (CData *d, CSocket *s)
{
	Data = d;
	Socket = s;
	Data -> win = 0;
};


void CRobot :: Pass()
{
	int raw_price = Data->market.min_raw_price;
	int prod_price = Data->market.max_prod_price;

	int prod_n = Data->you.raw;
	int sell_n = Data->you.prod;
	int buy_n = Data->you.fabrics;
	
// --- Fair analyZe ---
	// --- Raw buy 
	int tmp = 0;
	for (int i = 0; i<Data->players_count; i++)
		tmp += Data->players[i].raw;
	double scale = (tmp + Data->you.fabrics) / Data->market.raw;
	scale < 1 ?  : raw_price = (int) (raw_price * scale);
	

	// --- Prod sell 
	tmp = 0;
	for (int i = 0; i<Data->players_count; i++)
		tmp += Data->players[i].prod;
	scale = (tmp + Data->you.prod) / Data->market.prod;
	scale < 1 ?  : prod_price = (int) (prod_price / scale);
	//if ( (float)(Data->you.prod / tmp) > 0.5) prod_price = Data->market.min_raw_price*1.1;

// -------------------------
	Buy(buy_n,raw_price);
	Sell(sell_n,prod_price);
	Prod(prod_n);
	
	Turn();
};


void CRobot :: Turn()
{
	printf("	~Robot: Turn! \n");
	Socket->SendCommand(COMMAND_ROBOT_TURN);
};


void CRobot :: Prod(int n)
{
	printf("	~Robot: prod %d.\n", n);
	Socket->SendCommandWithInt(COMMAND_ROBOT_PROD, n);
}


void CRobot :: Sell(int n, int cost)
{
	printf("	~Robot: sell %d by %d$.\n", n, cost);
	Socket->SendCommandWithInt2(COMMAND_ROBOT_SELL, n, cost);
}


void CRobot :: Buy(int n, int cost)
{
	printf("	~Robot: buy %d by %d$.\n", n, cost);
	Socket->SendCommandWithInt2(COMMAND_ROBOT_BUY, n, cost);
}


int CRobot :: IsWin()
{
	if (Data -> win == 1) printf("~Game: %s won! :-) \n", Data->you.gamer_name);
	if (Data -> win == -1) printf("~Game: %s looser! :'-( \n", Data->you.gamer_name);
	if (Data -> win ==  0)  printf("~Game: %s are gaming... :-* \n", Data->you.gamer_name);
 return Data -> win;
};

