//Nathan Viniconis
//CSE293
//Class to handle incoming connections...

import java.io.*;
import java.net.*;
import java.util.*;

//handles all back and forth communication between player and server
public class PlayerServer	{
	
	//**********************4 connection
	private ObjectOutputStream out;	//these are for writing to/from connectedServer
	private ObjectInputStream in;   // ^^^
	int counter;			//Which # of the client is is
	private AcceptConn acceptConn;
	private int Type; //0 for god and 1 for player
	private Handler handler;
	private boolean done = false;
	//**********************************
	
	//*********Player Info******************8
	String PlayerName = new String("defaultPlayer");
	String PlayerPass = new String("defaultPass");
	String InRoomNum = new String("000-000");
	String userIP = new String();
	String TypeOfConnection = new String();
	//*******************************
	
	
	//*****************Calendar for times
	Calendar cal = Calendar.getInstance(TimeZone.getDefault());
	String DATE_FORMAT = "MM-dd HH:mm:ss";
	java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
	//***********************************
	
	//*****************DataBase shit
	DataBase dataBase;
	//*****************************
	
	//****************Chatting Shit
	int cP = 0;
	int cR = 0;
	int cG = 0;
	//*****************************
	
	
//for type... 0 is god, 1 is player
	public PlayerServer(int type,ObjectOutputStream outIN, ObjectInputStream inIN,int counterIN, AcceptConn acceptConnIN, DataBase dataBaseIN, String UserIN, Handler handlerIN)	{
		
		counter = counterIN;
		out = outIN;
		in = inIN;
		
		//Set the type of connection..
		Type = type;
		if(Type == 0 )	{
			TypeOfConnection = "G";
		}
		else	{
			TypeOfConnection = "P";
		}
		
		dataBase = dataBaseIN;
		acceptConn = acceptConnIN;
		handler = handlerIN;
		userIP = UserIN;
		
		
		//If type = 0, a god is connecting and have to check for individualness
		if(type == 0)	{
			SetUpConnection2();
		}
		else if(type == 1)	{
			SetUpConnection1();
		}
		
		//Set up time things
		sdf.setTimeZone(TimeZone.getDefault());
	}
	
	public PlayerServer()	{
	}
//***********************************
	public String GetName()	{
		return(PlayerName);
	}

//*********************************
	public void disconnect()	{
		done = true;
	}
	
//****************************************************	
//Used to initially set the chat pointers to new stuff...
	private void SetChatPositions()	{
		//Gets the current sizes for all the chat vectors..
		int tcG = dataBase.getGlobalChatSize();
		int tcP = dataBase.getPlayerChatSize(PlayerName);
		int tcR = dataBase.getRoomChatSize(InRoomNum);
		
		//Set the "lines read from each catagory" to appropriate values
		cG = tcG;
		cP = tcP;
		cR = tcR;
	}
		
//***************************************************
//Will wait for the client to send room# and then will send back requested room
// Client has to send first chunk of info
	private void SetUpConnection1()	{
		
	//	dataBase.MakePlayer(PlayerName);
		AnalyzeIncomingStrings();

	}
	
//*************************************************
//Deals with the connection of the god client..
	private void SetUpConnection2()	{
		
		String IncomingString = new String();
		
	//TELL CLIENT FINE CONNECT
		String message = new String("YYY");
		try	{		
			out.writeObject(message);
		} catch(IOException e)	{System.out.println("IX");}
		
	//	dataBase.MakeGod(PlayerName);

		AnalyzeIncomingStrings();

	}
	

//******************************************************************************
//This function is going to look at the strings inputted by the client and 
// then send the program the the correct function.
	public void AnalyzeIncomingStrings()	{

		//String to take in the message from the clinet..
		String IncomingString = new String();
		
		StringTokenizer BrokenUpInputString;
		String CurToken = new String();
		
		try	{
			
			while(!IncomingString.trim().equals("EXIT") && done == false)	{
		
			   //Reads the string from the little client thing	
			   	IncomingString = "EXIT";
			
				IncomingString = (String)in.readObject();	
				
			   //Tokenizes the string for parsing reason  
			    BrokenUpInputString = new StringTokenizer(IncomingString," =");
			   //This loop will determine the purpose of the input string  
			    while(BrokenUpInputString.hasMoreTokens())	{
			    	
			    	CurToken = BrokenUpInputString.nextToken();

		//If the INPUT IS TO CHANGE THE ROOM NUMBER
			    	if(CurToken.equals("RoomNum"))	{
			    		
			    		RecRoomNum(BrokenUpInputString);
			    	}
		//If they want to verify a players INFO
			    	else if(CurToken.equals("CheckPlayerInfo"))	{
			    		
			    		RecCheckPlayerInfo(BrokenUpInputString);		
			    	}
		//If they want a list of all the rooms
					else if(CurToken.equals("getAllRooms"))	{
						
						RecGetAllRooms(BrokenUpInputString);
					}
		//Gets all the new chat for this player and returns all new chat..
					else if(CurToken.equals("GetNewChat"))	{
						RecGetAllNewChatString(BrokenUpInputString);
					}
		//Recieves messages from the ConnectionManager in room, player, global
					else if(CurToken.equals("RoomChat"))	{
						RecRoomChat(BrokenUpInputString);
					}
					else if(CurToken.equals("PlayerChat"))	{
						RecPlayerChat(BrokenUpInputString);
					}
					else if(CurToken.equals("GlobalChat"))	{
						RecGlobalChat(BrokenUpInputString);
					}
					else if(CurToken.equals("PlayersInWorld"))	{
						RecPlayersInWorld(BrokenUpInputString);
					}
					else if(CurToken.equals("PlayersInRoom"))	{
						RecPlayersInRoom(BrokenUpInputString);
					}
					else if(CurToken.equals("GetTime"))	{
						RecGetTime(BrokenUpInputString);
					}
					else if(CurToken.equals("getAllItems"))	{
						RecGetAllItems(BrokenUpInputString);
					}
					else if(CurToken.equals("PickUpItem"))	{
						RecPickUpItem(BrokenUpInputString);
					}
					else if(CurToken.equals("getItemsHeld"))	{
						RecGetItemsHeld(BrokenUpInputString);
					}
					else if(CurToken.equals("DropItem"))	{
						RecDropItem(BrokenUpInputString);
					}
					else if(CurToken.equals("GetAllStats"))	{
						RecGetAllStats(BrokenUpInputString);
					}
					else if(CurToken.equals("MovePlayer"))	{
						RecMovePlayer(BrokenUpInputString);
					}
					else if(CurToken.equals("ImageInfoForPlayers"))	{
						RecImageInfoForPlayers(BrokenUpInputString);
					}
					else if(CurToken.equals("GetAPlayersRoom"))	{
						RecGetAPlayersRoom(BrokenUpInputString);
					}
					else if(CurToken.equals("SetAllPlayerStats"))	{
						RecSetAllPlayerStats(BrokenUpInputString);
					}
					else if(CurToken.equals("ChangeAStat"))	{
						RecChangeAStat(BrokenUpInputString);
					}
					else if(CurToken.equals("GODdeleteItem"))	{
						RecGODdeleteItem(BrokenUpInputString);
					}
					else if(CurToken.equals("GODdeleteBackpackItem"))	{
						RecGODdeleteBackpackItem(BrokenUpInputString);
					}
					else if(CurToken.equals("GODforceItemDropped"))	{
						RecGODforceItemDropped(BrokenUpInputString);
					}
					else if(CurToken.equals("GODcreateItem"))	{
						RecGODcreateItem(BrokenUpInputString);
					}
					else if(CurToken.equals("GODputPlayerInRoom"))	{
						RecGODputPlayerInRoom(BrokenUpInputString);
					}
					else if(CurToken.equals("GetPickUpableItemsInRoom"))	{
						RecGetPickUpableItemsInRoom(BrokenUpInputString, 1);
					}
					else if(CurToken.equals("GetStructureItemsInRoom"))	{
						RecGetPickUpableItemsInRoom(BrokenUpInputString, 0);
					}
					else if(CurToken.equals("GetPlayerStats"))	{
						RecGetPlayerStats(BrokenUpInputString);
					}
					else if(CurToken.equals("GODcreateItemTYPE"))	{
						RecGODcreateItemTYPE(BrokenUpInputString);
					}
					else if(CurToken.equals("getItemsHeldFor"))	{
						RecgetItemsHeldFor(BrokenUpInputString);
					}
					else if(CurToken.equals("GetStoreItemsInRoom"))	{
						RecGetStoreItemsInRoom(BrokenUpInputString, 4);
					}
					else if(CurToken.equals("BuyItemFromSTore"))	{
						RecBuyItemFromSTore(BrokenUpInputString);
					}
					else if(CurToken.equals("MakeMeGod"))	{
						RecMakeMeGod(BrokenUpInputString);
					}
					else if(CurToken.equals("GetMonsters"))	{
						RecGetMonsters(BrokenUpInputString);
					}
					else if(CurToken.equals("AtkMonster"))	{
						RecAtkMonster(BrokenUpInputString);
					}
					else if(CurToken.equals("GetMonstersInRoom"))	{
						RecGetMonstersInRoom(BrokenUpInputString);
					}
					else if(CurToken.equals("KillMonsters"))	{
						RecKillMonsters(BrokenUpInputString);
					}
					else if(CurToken.equals("GetMonstersInRoomWrot"))	{
						RecGetMonstersInRoomWrot(BrokenUpInputString);
					}
					
					while(BrokenUpInputString.hasMoreTokens())	{
						System.out.println("Extra token sent :: " + BrokenUpInputString.nextToken());
					}
			    			
			    } // end while
			}            
		} catch(Exception e) { System.out.println("ERROR IN PS RECIEVING THING" + e);} //PUT SOMETHING HERE IF IMPORTANT
	}
	
//**************************************************************
//*******************MONSTER FUNCTIONS *************************
//**************************************************************
	
//*************************************************
//Kills a monster..
	private void RecKillMonsters(StringTokenizer BrokenUpInputString)	{
		
		String uID = new String();
		
		uID = BrokenUpInputString.nextToken();
		boolean success = false;
		success = dataBase.KillMonster(uID);
		
		String ToSend = "0";
		if(success)	{
			ToSend = "1";
		}
		
		try	{
			out.writeObject(ToSend);
		} catch(Exception e) { System.out.println("UUDI");}
	}
		

//******************************************************
//Returns a list of the monsters in the room in UID|MN|X|Y format
	private void RecGetMonstersInRoom(StringTokenizer BrokenUpInputString)	{
		
		String ToSend = new String();
		
		ToSend = dataBase.GetMonstersInRoom(InRoomNum);
		
		//And write it out to the client..
		try	{
			out.writeObject(ToSend);
		} catch(Exception e) { System.out.println("UUDasdfI");}
		
	}
	
//******************************************************
//Returns a list of the monsters in the room in UID|MN|X|Y format
	private void RecGetMonstersInRoomWrot(StringTokenizer BrokenUpInputString)	{
		
		String ToSend = new String();
		
		ToSend = dataBase.GetMonstersInRoomWrot(InRoomNum);
		
		//And write it out to the client..
		try	{
			out.writeObject(ToSend);
		} catch(Exception e) { System.out.println("UUgrarDI");}
		
	}
	
	
//******************************************************
//Returns a list of the monsters in the room in UID|MN|X|Y format
	private void RecGetMonsters(StringTokenizer BrokenUpInputString)	{
		
		String ToSend = new String();
		String RoomID = new String();
		
		if(BrokenUpInputString.hasMoreTokens())	{
			
			RoomID = BrokenUpInputString.nextToken();
		}
		else	{
			RoomID = "008-008";
		}
		
		ToSend = dataBase.GetMonstersInRoom(RoomID);
		
		
		
		//And write it out to the client..
		try	{
			out.writeObject(ToSend);
		} catch(Exception e) { System.out.println("UUDweradI");}
		
	}
	
//******************************************************
//Lowers a creatures HP by a certain amount...
	private void RecAtkMonster(StringTokenizer BrokenUpInputString)	{
		
		String ToSend = new String();
		String uID = new String();
		String delta = new String();
		boolean success = false;
		
		
		int del;
		int uid;
		if(BrokenUpInputString.hasMoreTokens())	{
			uID = BrokenUpInputString.nextToken();
			delta = BrokenUpInputString.nextToken();
		}
		else{
			uID = "0";
			delta = "0";
		}
				
		del = Integer.parseInt(delta);
		uid = Integer.parseInt(uID);
		
	//GIVE EXP IF THEY KILL THE CREATURE
		boolean wasAlive = false;
		boolean killedMonster = false;
		long exp = 0;
		if(dataBase.SeeIfMonsterAlive(uid))	{
			wasAlive = true;
		}
		success = dataBase.ChangeMonsterStat(uid,0,del);
		if(wasAlive && dataBase.SeeIfMonsterDead(uid))	{
			killedMonster = true;
			exp = (long)dataBase.getMonsterExp(uid);
		}
		
		if(killedMonster)	{
			dataBase.ChangeAStat(PlayerName,11, exp);
		}
		
		if(success)	{
			ToSend = "1";
		}
		else	{
			ToSend = "0";
		}
		
		
		try	{
			out.writeObject(ToSend);
		} catch(Exception e) { System.out.println("UUeasdfDI");}
	}
		
	
//**************************************************************
//********************* ROOM FUNCTIONS *************************
//**************************************************************

	
//*************************************************************
//Function to change the active room a player is in..
	private void RecRoomNum(StringTokenizer BrokenUpInputString)	{
		
		String tempRoomID = new String();
		String reply = new String("0");

		//get the requested ID, put in tempRoomID..
		if(BrokenUpInputString.hasMoreTokens())	{
			tempRoomID = BrokenUpInputString.nextToken();
		}
		else	{
			System.out.println("Invalid room #");				    		
		}
 		
 		//See if that room exists...
    	if(dataBase.SeeIfRoomExists(tempRoomID))	{
    		
    		//If the room is changing...
    		if(!tempRoomID.equals(InRoomNum))	{
    			cR = dataBase.getRoomChatSize(tempRoomID);
    		} 
    		InRoomNum = tempRoomID;
    		reply = "1";
    		dataBase.SetPlayerToRoom(PlayerName, InRoomNum);
    	} 
    	
    	//Write the status of the roomchange 
    	try	{
    		out.writeObject(reply);
    	} catch(Exception e) { System.out.println("UXffH");}        
        
	}
	
//***********************************************************
//Returns a list of all the rooms on the server in roomid|x|y|roomid format..
	private void RecGetAllRooms(StringTokenizer BrokenUpInputString)	{	
	
		String ToSend = new String();
		
		ToSend = dataBase.GetAllRoomsForSteve();
		
		
		
		//And write it out to the client..
		try	{
			out.writeObject(ToSend);
		} catch(Exception e) { System.out.println("UUasdfDI");}
		
	}
	
	
//***************************************************
	public void RecGetAllItems(StringTokenizer BrokenUpInputString)	{
		

		String ToSend = new String();
		
		ToSend = dataBase.GetItemsInRoom(InRoomNum);
		
		
		//And write it out to the client..
		try	{
			out.writeObject(ToSend);
		} catch(Exception e) { System.out.println("UUeaeDI");}
		
		
	}

//***************************************************
	public void RecGODdeleteItem(StringTokenizer BrokenUpInputString)	{
		
		String FromRoom = new String();
		String ItemID = new String();
		String atX = new String();
		String atY = new String();
		
		FromRoom = BrokenUpInputString.nextToken();
		ItemID = BrokenUpInputString.nextToken();
		atX = BrokenUpInputString.nextToken();
		atY = BrokenUpInputString.nextToken();
		
		int x = Integer.parseInt(atX);
		int y = Integer.parseInt(atY);
		
		boolean result = false;
		
		result = dataBase.RemoveItem(FromRoom,ItemID,x,y);
		
		String toSend = new String("0");
		
		if(result)	{
			toSend = "1";
		}
		
		try	{
			out.writeObject(toSend);
		} catch(Exception e) { System.out.println("UweawrfUDI");}
	}
	
//**********************************************
	public void RecgetItemsHeldFor(StringTokenizer BrokenUpInputString)	{
		
		String ToSend = new String();
		
		String Pname = new String();
		
		if(BrokenUpInputString.hasMoreTokens())	{
			Pname = BrokenUpInputString.nextToken();
		}
		else	{
			Pname = "IHATEMONKEYSDMANIT";
		}
		
		ToSend = dataBase.getItemsHeld(Pname);
		
		try	{
			out.writeObject(ToSend);
		} catch(Exception e) { System.out.println("UUasdfeDI");}
	}
			
	
//**************************************************
	public void RecGODcreateItemTYPE(StringTokenizer BrokenUpInputString)	{
					//String ItemID, String RoomID, int x, int y, int type)
		String ObjectID = new String();
		String RoomID = new String();
		String X = new String();
		String Y = new String();
		String Pos = new String();
		String Type = new String();
		int x = 0; int y = 0; int type = 0;int pos = 0;
		
		if(BrokenUpInputString.hasMoreTokens())	{
			ObjectID = BrokenUpInputString.nextToken();
		}
		else{
			ObjectID = "asf";
		}
		if(BrokenUpInputString.hasMoreTokens())	{
			RoomID = BrokenUpInputString.nextToken();
		}
		else{
			RoomID = "asf";
		}
		if(BrokenUpInputString.hasMoreTokens())	{
			X = BrokenUpInputString.nextToken();
		}
		else{
			X = "0";
		}
		if(BrokenUpInputString.hasMoreTokens())	{
			Y = BrokenUpInputString.nextToken();
		}
		else{
			Y = "0";
		}
		if(BrokenUpInputString.hasMoreTokens())	{
			Pos = BrokenUpInputString.nextToken();
		}
		else{
			Pos = "0";
		}
		if(BrokenUpInputString.hasMoreTokens())	{
			Type = BrokenUpInputString.nextToken();
		}
		else{
			Type = "0";
		}
		
		x = Integer.parseInt(X);
		y = Integer.parseInt(Y);
		type = Integer.parseInt(Type);
		pos = Integer.parseInt(Pos);
	
	
		boolean success = false;
		String ToSend = "0";
	
		
		success = dataBase.AddItem(RoomID, ObjectID, x, y, pos, type);
		
		
		if(success)	{
			ToSend = "1";
		}
		
		
		System.out.println("Sending");
		try	{
			out.writeObject(ToSend);
		} catch(Exception e) { System.out.println("UasereUDI");}
	
	
		
	}
		
		
		
		
		
	public void RecGetPickUpableItemsInRoom(StringTokenizer BrokenUpInputString, int type)	{
		
		String ToSend = new String();
		
		ToSend = dataBase.GetItemsInRoom(InRoomNum, type);
		
		try	{
			out.writeObject(ToSend);
		} catch(Exception e) { System.out.println("UUDIeawdf");}
	}
	
	
	public void RecGetStoreItemsInRoom(StringTokenizer BrokenUpInputString, int type)	{
		
		String ToSend = new String();
		
		ToSend = dataBase.GetItemsInRoom(InRoomNum, type);
		
		try	{
			out.writeObject(ToSend);
		} catch(Exception e) { System.out.println("UUeaffDI");}
	}
	
	public void RecBuyItemFromSTore(StringTokenizer BrokenUpInputString)	{
		
		String ItemID = new String();
		String Price = new String();
		int price;
		
		if(BrokenUpInputString.hasMoreTokens())	{
			ItemID = BrokenUpInputString.nextToken();
		}
		
		if(BrokenUpInputString.hasMoreTokens())	{
			Price = BrokenUpInputString.nextToken();
		}
		else	{
			Price = "0";
		}
		
		price = Integer.parseInt(Price);
		
		boolean enoughMoney = false;
		boolean enoughMoneyDeducted = false;
		//first have too see if players money > money needed..
		long moneyOnPlayer = dataBase.GetMoneyOnPlayer(PlayerName);
		
		if( moneyOnPlayer >= price)	{
			enoughMoney = true;
		}
		
		//if enough money was found..
		if(enoughMoney)	{
			
			//subtract the money from the player..
			enoughMoneyDeducted = dataBase.RemoveMoney(PlayerName, price);
		}
			
		//if enough money was deducted.. 
		if(enoughMoneyDeducted)	{
			
			//Add the item to the players Inventory..
			dataBase.AddItemToPlayer(PlayerName,ItemID);
			
		}
		
		String ToSend = new String("0");
		
		if(enoughMoneyDeducted)	{
			ToSend = "1";
		}
		
		
		//tell the server the result
		try{
			out.writeObject(ToSend);
		} catch(Exception e) { System.out.println("UUafeafX");} 
	}
			
		
		

//**************************************************************
//********************* PLAYER FUNCTIONS ***********************
//**************************************************************

//***********************************************************8
//Checks a login/pass for validity.. if good returns a true string..
	private void RecCheckPlayerInfo(StringTokenizer BrokenUpInputString)	{
		
	
		String Pname, Ppass;
		String ReplyP = new String();

		
		//Get name and pass from the message..	
		if(BrokenUpInputString.hasMoreTokens())	{
			Pname = BrokenUpInputString.nextToken();
			Ppass = BrokenUpInputString.nextToken();
		}
		else	{
			Pname = "bad";
			Ppass = "bad";
		}
		//DATABASE CHECKS STRINGS AND RETURNS TRUE/FALSE
		//IF TRUE, SEND "1" TO PLAYER, ELSE "0"
		
		if(dataBase.SeeIfLoginMatches(Pname,Ppass))	{
			ReplyP = "1";
			PlayerName = Pname;
			PlayerPass = Ppass;
			SetChatPositions();
			
			//Initialize the character..
			dataBase.addUserInfo(Pname,userIP);
			dataBase.SetPlayerToRoom(PlayerName, InRoomNum);
			handler.SetName(Pname);
			
			if(TypeOfConnection.equals("P")	){
				dataBase.MakePlayer(PlayerName);
			}
			else	{
				dataBase.MakeGod(PlayerName);
			}
		}
		else	{
			System.out.println(Pname+ "*"+ Ppass);
			ReplyP = "0";
		}		
		
		//Write the reply to the connecitonManager of the cilnnt..
		try{
			out.writeObject(ReplyP);
		} catch(Exception e) { System.out.println("UUeaerX");} 
	}
	
//*********************************************************
//Returns the name of the player..
	public String getName()	{
		return(PlayerName);
	}

//********************************************************	
//Returns all the players in the world.. seperated by |
	public void RecPlayersInWorld(StringTokenizer BrokenUpInputString)	{
		
		String allPlayers = new String();
		//Get the list of names from the DB
		allPlayers = dataBase.getAllPlayers();
		
		try{
			out.writeObject(allPlayers);
		} catch(Exception e) { System.out.println("UaesX");} 
	}
	
//******************************************************
//Returns all the players in the room.. seperated by |
	public void RecPlayersInRoom(StringTokenizer BrokenUpInputString)	{
		
		String roomPlayers = new String();

		roomPlayers = dataBase.getRoomPlayers(InRoomNum);
		
		try{
			out.writeObject(roomPlayers);
		} catch(Exception e) { System.out.println("YsasdfX");}
	}
	
//*********************************************************
//Returns the timestamp of the server..
	public void RecGetTime(StringTokenizer BrokenUpInputString)	{
		
		String chatLine = new String();
		
		chatLine = chatLine.concat("[");
		cal = Calendar.getInstance(TimeZone.getDefault());
		chatLine = chatLine.concat((String)(sdf.format(cal.getTime())));
		chatLine = chatLine.concat("]");
		
		try{
			out.writeObject(chatLine);
		} catch(Exception e) { System.out.println("ma4esX");}
	}
	
	
//***************************************************
	public void RecPickUpItem(StringTokenizer BrokenUpInputString)	{
		
		//will return a 1 or 0..
		String ToSend = new String();
		boolean ItemRemoved = false;
		String ItemToTake = BrokenUpInputString.nextToken();
		String X = new String();
		String Y = new String();
		int x; int y;
		boolean xy = false; //States if the object coming in has xy coords..
		
		//Take the item out of the room if it was found
		
		if(BrokenUpInputString.hasMoreTokens())	{
			X = BrokenUpInputString.nextToken();
			Y = BrokenUpInputString.nextToken();
			xy = true;
		}
		

		
		if(!xy)	{
			ItemRemoved = dataBase.RemoveItem(InRoomNum,ItemToTake);
		}
		else	{
			x = Integer.parseInt(X);
			y = Integer.parseInt(Y);
			ItemRemoved = dataBase.RemoveItem(InRoomNum,ItemToTake,x,y);
		}
		
		//Add the item to the players list of items held.
		
		//return a 1 if all went well.. or a 0 otherwise..
		
		if(ItemRemoved)	{
			
			dataBase.AddItemToPlayer(PlayerName,ItemToTake);
			ToSend = "1";
		}
		else	{
			ToSend = "0";
		}
		
		//And write it out to the client..
		try	{
			out.writeObject(ToSend);
		} catch(Exception e) { System.out.println("UUasdfasfdDI");}
		
		
	}
	
//***************************************************
	public void RecGetItemsHeld(StringTokenizer BrokenUpInputString)	{
		
		String backpack = new String();
		
		backpack = dataBase.getItemsHeld(PlayerName);
	
		try	{
			out.writeObject(backpack);
		} catch(Exception e) { System.out.println("UUasdfDI");}
	}
		
		
//***************************************
	public void RecDropItem(StringTokenizer BrokenUpInputString)	{
		
		String Item2Drop = BrokenUpInputString.nextToken();
		boolean ItemDropped = false;
		String X = new String();
		String Y = new String();
		int x = 0; 
		int y = 0;
		boolean xy = false; //States if the object coming in has xy coords..

		//Take the item out of the room if it was found
		
		if(BrokenUpInputString.hasMoreTokens())	{
			X = BrokenUpInputString.nextToken();
			Y = BrokenUpInputString.nextToken();
			xy = true;
			x = Integer.parseInt(X);
			y = Integer.parseInt(Y);
		}
		
		//go to DB and remove item from player..
		ItemDropped = dataBase.RemoveItemFromPlayer(PlayerName, Item2Drop);
		//go to DB and add item to room...
		
		if(ItemDropped && xy)	{
			dataBase.AddItem(InRoomNum,Item2Drop,x,y,0,1);
		}
		else if(ItemDropped)	{
			dataBase.AddItem(InRoomNum,Item2Drop);
		}
		
		
		String IfDropped = new String();
		if(ItemDropped)	{
			IfDropped = "1";
		}
		else	{
			IfDropped = "0";
		}
		try	{
			out.writeObject(IfDropped);
		} catch(Exception e) { System.out.println("UUeaesDI");}
		
	}
	
//************************************************************
	public void RecGetAllStats(StringTokenizer BrokenUpInputString)	{
		
		String ToSend = new String();
		
		ToSend = dataBase.getPlayerStats(PlayerName);
	
		try	{
			out.writeObject(ToSend);
		} catch(Exception e) { System.out.println("UUasdfDI");}
	}
	
//************************************************************
	public void RecGetPlayerStats(StringTokenizer BrokenUpInputString)	{
		
		String ToSend = new String();
		String Pname = new String();
		
		if(BrokenUpInputString.hasMoreTokens())	{
			Pname = BrokenUpInputString.nextToken();
		}
		else	{
			Pname = " ";
		}
		
		ToSend = dataBase.getPlayerStats(Pname);
	
		try	{
			out.writeObject(ToSend);
		} catch(Exception e) { System.out.println("UUasdfeDI");}
	}
		
		
//**************************************************************		
	public void RecMovePlayer(StringTokenizer BrokenUpInputString)	{
		
		String X = new String();
		int x = 0;
		String Y = new String();
		int y = 0;
		String ROT = new String();
		int rot = 0;
		boolean result = false;
		String ToSend = new String();
		
		X = BrokenUpInputString.nextToken();
		x = Integer.parseInt(X);
		Y = BrokenUpInputString.nextToken();
		y = Integer.parseInt(Y);
		ROT = BrokenUpInputString.nextToken();
		rot = Integer.parseInt(ROT);
		
		result = dataBase.MovePlayerTo(PlayerName,x,y,rot);
		
		if(result)	{
			ToSend = "1";
		}
		else	{
			ToSend = "0";
		}
		
		try	{
			out.writeObject(ToSend);
		} catch(Exception e) { System.out.println("U6asdfDI");}
		
		
		
	}
	
//************************************
	public void RecImageInfoForPlayers(StringTokenizer BrokenUpInputString)	{
		
		//PlayerName|ObjID|X|Y|ROT| 
		
		
		String FromDB = new String();

		FromDB = dataBase.GetImageInforForPlayers(InRoomNum);
		
		try	{
			out.writeObject(FromDB);
		} catch(Exception e) { System.out.println("U67asfdI");}
		
	}
		
//******************************************************************
	public void RecGetAPlayersRoom(StringTokenizer BrokenUpInputString)	{
		
		String TheName = new String();
		
		TheName = BrokenUpInputString.nextToken();
		
		String TheRoom = new String();
		
		TheRoom = dataBase.GetPlayersRoom(TheName);
		
		try	{
			out.writeObject(TheRoom);
		} catch(Exception e) { System.out.println("U6asdf7I");}
	}
	
	
//*******************************************************************
	public void RecSetAllPlayerStats(StringTokenizer BrokenUpInputString)	{
		
		String Reply = "0";
		String TheName = new String();	
		//hp|maxhp|str|sta|dex|cha|IQ|wis|shin|guk|don|exp
		
	
		TheName = BrokenUpInputString.nextToken();
		String Stats = BrokenUpInputString.nextToken();
		
		
		boolean Success = false;
		
		Success = dataBase.SetAllPlayersStats(TheName, Stats);
		
		if(Success)	{
			Reply = "1";
		}
		
		
		
		try	{
			out.writeObject(Reply);
		} catch(Exception e) { System.out.println("U67asfdI");}
		
	}
	
//*******************************************************************
	public void RecChangeAStat(StringTokenizer BrokenUpInputString)	{
		
		String TheName = new String();
		String TheStat = new String();
		String TheValue = new String();
		
		TheName = BrokenUpInputString.nextToken();
		TheStat = BrokenUpInputString.nextToken();
		TheValue = BrokenUpInputString.nextToken();

		int stat = Integer.parseInt(TheStat);
		long value = Long.parseLong(TheValue);
		
		String Reply = new String();
		Reply = "0";
		boolean success = false;
		
		success = dataBase.ChangeAStat(TheName, stat, value);
		
		if(success)	{
			Reply = "1";
		}
		
		
		try	{
			out.writeObject(Reply);
		} catch(Exception e) { System.out.println("Ur7Iea");}
	}
		
		
//**************************************************************
	public void RecGODdeleteBackpackItem(StringTokenizer BrokenUpInputString)	{
		
		String Pname = new String();
		String ObjectID = new String();
		
		Pname = BrokenUpInputString.nextToken();
		ObjectID = BrokenUpInputString.nextToken();
		
		boolean result = false;
		
		result = dataBase.RemoveItemFromPlayer(Pname,ObjectID);
		
		String ToSend = new String("0");
		
		if(result)	{
			ToSend = "1";
		}
		
		try	{
			out.writeObject(ToSend);
		} catch(Exception e) { System.out.println("Ur7teIe");}
	}

//**************************************************************
	public void RecGODforceItemDropped(StringTokenizer BrokenUpInputString)	{
		
		String Pname = new String();
		String ObjectID = new String();
		String atX = new String();
		String atY = new String();
		
		Pname = BrokenUpInputString.nextToken();
		ObjectID = BrokenUpInputString.nextToken();
		atX = BrokenUpInputString.nextToken();
		atY = BrokenUpInputString.nextToken();
		
		boolean result = false;
		
		String RoomIN = dataBase.GetPlayersRoom(Pname);
		result = dataBase.RemoveItemFromPlayer(Pname,ObjectID);
		
		int x = Integer.parseInt(atX);
		int y = Integer.parseInt(atY);
		
		if(result)	{	//PUTTING IN DEFAULT OF TYPE 1...
			result = dataBase.AddItem(RoomIN,ObjectID,x,y,1,1);
		}
		
		String ToSend = new String("0");
		
		if(result)	{
			ToSend = "1";
		}
		
		try	{
			out.writeObject(ToSend);
		} catch(Exception e) { System.out.println("wr7Ie");}
	}
	
//************************************************************************
	public void RecGODcreateItem(StringTokenizer BrokenUpInputString)	{
		
		String ObjectID = new String();
		
		
		
		ObjectID = BrokenUpInputString.nextToken();
		//now add this object to this players backpack..
		
		boolean success = true;
		
		dataBase.AddItemToPlayer(PlayerName,ObjectID);
		
		String ToSend = new String("0");
		
		if(success)	{
			ToSend = "1";
		}
		
		try	{
			out.writeObject(ToSend);
		} catch(Exception e) { System.out.println("wr7Irewge");}
		
	}
	
//**************************************************
	public void RecGODputPlayerInRoom(StringTokenizer BrokenUpInputString)	{
		
		String Pname = new String();
		String RoomID = new String();
		String atX = new String();
		String atY = new String();
		
		Pname = BrokenUpInputString.nextToken();
		RoomID = BrokenUpInputString.nextToken();
		atX = BrokenUpInputString.nextToken();
		atY = BrokenUpInputString.nextToken();
		
		int x = Integer.parseInt(atX);
		int y = Integer.parseInt(atY);
		
		boolean success = false;
		
		success = dataBase.SetPlayerToRoom(Pname,RoomID, x, y);
		
		String ToSend = new String("0");
		
		if(success)	{
			ToSend = "1";
		}
		
		try	{
			out.writeObject(ToSend);
		} catch(Exception e) { System.out.println("wy7ragIe");}
	}
	
	
//**************************************************************
	public void RecMakeMeGod(StringTokenizer BrokenUpInputString)	{
		
		String ToSend = new String("0");
		
		if(BrokenUpInputString.hasMoreTokens())	{
			String Pname = BrokenUpInputString.nextToken();
			
			if(dataBase.IsPlayerLoggedin(Pname)	)	{
				dataBase.MakeGod(Pname);
			
				ToSend = "1";
			}
		}
		
		try	{
			out.writeObject(ToSend);
		} catch(Exception e) { System.out.println("wy7asregIe");}
	}
	
//**************************************************************
//********************* CHAT FUNCTIONS *************************
//**************************************************************
	
	
//***************************************************************88
//Gets all the new chat for the player... all the chat.. global, player, and room...
	private void RecGetAllNewChatString(StringTokenizer BrokenUpInputString)	{
		
		//Gets send to the player
		String ToSend = new String();
		Vector NewChat = new Vector();

		//***Function in DataBase
		//public Vector getNewChat(int cG, String Pname,int cP, String RoomID,int cR)
		//***
		//Getting new chat info from the database
		//NewChat = vector of strings with an String at the end to keep track of cG cP cR)

		NewChat = dataBase.getNewChat(cG, PlayerName, cP, InRoomNum, cR);

		int count = NewChat.size();
		if(count > 0)	{
			count--;
		}
		//extract numbers and remove wierd string from list
		StringTokenizer breakUP = new StringTokenizer( ((String)NewChat.elementAt(count))," |,");
		NewChat.remove(count);
		
		int tcG = Integer.parseInt(breakUP.nextToken());
		int tcP = Integer.parseInt(breakUP.nextToken());
		int tcR = Integer.parseInt(breakUP.nextToken());
		
		//Set the "lines read from each catagory" to appropriate values
		
		cG = cG + tcG;
		cP = cP + tcP;
		cR = cR + tcR;
		
		//Ready the new chat to be sent to the player
		int count2 = 0;
		while (count2 < NewChat.size())	{
			
			ToSend = ToSend.concat(((String)NewChat.elementAt(count2))).concat("\n");
			
			count2++;
		}
		
		try{
			out.writeObject(ToSend);
		} catch(Exception e) { System.out.println("alergD");}
			
	}
	
//*******************************************************************
//Some chat was recieved from the user... put in proper place...
	public void RecRoomChat(StringTokenizer BrokenUpInputString)	{
		
		
		//Concats happy stuff to string.. name timestamp
		String chatLine = new String();
		chatLine = chatLine.concat("[");
		cal = Calendar.getInstance(TimeZone.getDefault());
		chatLine = chatLine.concat((String)(sdf.format(cal.getTime())));
		chatLine = chatLine.concat("]  ");
		chatLine = chatLine.concat(PlayerName);
		chatLine = chatLine.concat(" says: ");

		
		while(BrokenUpInputString.hasMoreTokens())	{
			chatLine = chatLine.concat(" ").concat(BrokenUpInputString.nextToken());
		}
		
		//adds the chat to this particular room..
		dataBase.addRoomChat(InRoomNum, chatLine);
		
	}
	
//*************************************************************
//Some chat was recieved from the user... put in proper place...
	public void RecGlobalChat(StringTokenizer BrokenUpInputString)	{
		
		//Concats happy stuff to string.. name timestamp
		String chatLine = new String();
		chatLine = chatLine.concat("[");
		cal = Calendar.getInstance(TimeZone.getDefault());
		chatLine = chatLine.concat((String)(sdf.format(cal.getTime())));
		chatLine = chatLine.concat("]  ");
		chatLine = chatLine.concat(PlayerName);
		chatLine = chatLine.concat(" shouts: ");
		
		while(BrokenUpInputString.hasMoreTokens())	{
			chatLine = chatLine.concat(" ").concat(BrokenUpInputString.nextToken());
		}
		
		//Add global chat to the DB..
		dataBase.addGlobalChat(chatLine);
		
	}
	
//************************************************************
//Some chat was recieved from the user... put in proper place...
	public void RecPlayerChat(StringTokenizer BrokenUpInputString)	{
				
		String toPlayer = new String();
		String reply = new String("0");
	
		toPlayer = BrokenUpInputString.nextToken();
		//Concats happy stuff to string.. name timestamp
		String chatLine = new String();
		chatLine = chatLine.concat("[");
		cal = Calendar.getInstance(TimeZone.getDefault());
		chatLine = chatLine.concat((String)(sdf.format(cal.getTime())));
		chatLine = chatLine.concat("]  ");
		chatLine = chatLine.concat(PlayerName);
		chatLine = chatLine.concat(" whispers: ");
		
		while(BrokenUpInputString.hasMoreTokens())	{
			chatLine = chatLine.concat(" ").concat(BrokenUpInputString.nextToken());
		}
		
		//If the player was found return a 1.. else a 0
		if(dataBase.addPlayerChat(toPlayer,chatLine))	{
			reply = "1";
		}
		else	{
			reply = "0";
		}
		
		try{
			out.writeObject(reply);
		} catch(Exception e) { System.out.println("aOOasfdD");}
		
		
	}
		
		

}
			
