----------------------------
-- CakeScript (March 29, 2008)
-- by LuaBanana
--
-- Alpha Version
-- player_data.lua
----------------------------

local  meta = FindMetaTable( "Player" );
CharTable = {}

											-- not NW string
function meta:SetField(fieldname, fielddata, NoNW)
	
	if( CharTable[ CAKE.FormatSteamID( self:SteamID( ) ) ] == nil ) then
		Msg("Problem with steamID\n");
		Msg("Steamid is: " .. self:SteamID() .. "\n");
		return;
	end
	if( CharTable[ CAKE.FormatSteamID( self:SteamID( ) ) ][self:GetNWString("uid")] == nil ) then
		Msg("Problem with steamID/uid \n");
	end
	if( CharTable[ CAKE.FormatSteamID( self:SteamID( ) ) ][self:GetNWString("uid")][fieldname] == nil ) then
		Msg("Problem with steamID/uid/fieldname\n");
		Msg("Fieldname is: " .. fieldname .. "\n");
	end
	
	CharTable[ CAKE.FormatSteamID( self:SteamID( ) ) ][self:GetNWString("uid")][fieldname] = fielddata;
	
	
	local NoNWString = NoNW or false;
	
	if(NoNWString == false) then
	
		self:SetNWString(fieldname, fielddata);
		
		-- only bother saving if its a string, waste of operations otherwise
		-- Let's save the data.
		
	end

self:SavePlayerData()
	
end

-- For setting global account variables
function meta:SetAccField(fieldname, fielddata, NoNW)
	
	CharTable[self:FormatSteamID()][fieldname] = fielddata;
	
	local NoNWString = NoNW or false;
	if( NoNWString == false) then
		self:SetNWString(fieldname,fielddata);
		-- Saving!
	end

self:SavePlayerData();
	
end

function meta:GetField(fieldname)

	if(CharTable[self:FormatSteamID()] != nil and CharTable[self:FormatSteamID()][self:GetNWString("uid")] != nil) then
	
		return CharTable[self:FormatSteamID()][self:GetNWString("uid")][fieldname];
		
	else
	
		return "0";
		
	end
	
end

-- for getting global account variables
function meta:GetAccField(fieldname)
	if(CharTable[self:FormatSteamID()] != nil and CharTable[self:FormatSteamID()][fieldname] != nil ) then
		return CharTable[self:FormatSteamID()][fieldname];
	else
		return "0";
	end
end

function meta:FormatSteamID()

	local steamid = self:SteamID();

	local formatted = string.gsub(steamid,"STEAM","");
	local formatted = string.gsub(formatted,":","");
	local formatted = string.gsub(formatted,"_","");
	
	return formatted;
	
end

function meta:SavePlayerData()

	local keys = util.TableToKeyValues(CharTable[self:FormatSteamID()]);
	file.Write("CakeScript/PlayerData/" .. self:FormatSteamID() .. ".txt", keys);
	
end

function meta:LoadPlayerData()

	if(file.Exists("CakeScript/PlayerData/" .. self:FormatSteamID() .. ".txt")) then
	
		local rawdata = file.Read("CakeScript/PlayerData/" .. self:FormatSteamID() .. ".txt");
		local tabledata = CAKE.NilFix( util.KeyValuesToTable(rawdata), {} );
		CharTable[self:FormatSteamID()] = tabledata;
	end
	
end

-- TS CODE
function meta:CheckLimit( str ) 
   
	// No limits in single player 
 	--if (SinglePlayer()) then return true end 
   
 	local c = self:GetField( "group_max_" .. str ) or server_settings.Int( "sbox_max"..str, 0 );
 	
 	if( self:GetField( "group_max_" .. str ) ) then
 	
 		if( server_settings.Int( "sbox_max"..str, 0 ) > self:GetNWInt( str ) ) then
 		
 			c = server_settings.Int( "sbox_max"..str, 0 );
 		
 		end
 	
 	end
 	 
 	if ( c < 0 ) then return true end 
 	if ( self:GetNWInt( str ) > c-1 ) then self:LimitHit( str ) return false end 
   
 	return true 
   
end 