#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.fabrics;
	int raw_n = Data->you.fabrics;
	int sell_n = Data->you.fabrics;
	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;

	if (tmp + Data->you.fabrics > Data->market.raw) 
		raw_price = (int) (raw_price * 1.25);

	// --- Prod sell 
	tmp = 0;
	for (int i = 0; i<Data->players_count; i++)
		tmp += Data->players[i].prod;

	if (tmp + Data->you.fabrics > Data->market.raw) 
		prod_price = (int) (prod_price * 0.75);
	



	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;
};

