All pastes #916286 Raw Edit

Miscellany

public text v1 · immutable
#916286 ·published 2008-02-24 14:26 UTC
rendered paste body
/*
==========================================
      SSU: Stats Saving Utility v1.1
 		    Coded by Scarface
==========================================
*/

/*
=====================================================
Special Thanks To:
BetaMaster - Testing, Notes
DracoBlue - DUDB & His Contribution to the SA-MP Com.
=====================================================
*/

#include <a_samp>
#include <dudb>

#define COLOR_YELLOW 0xFFFF00AA
#define COLOR_WHITE 0xFFFFFFAA

new DeathCount[MAX_PLAYERS];
new KillCount[MAX_PLAYERS];
new gString[255];
new sType, sMoney, sDeaths, sScore; // Settings

/*
============
	Main
============
*/


public OnFilterScriptInit()
{
	print("\n  SSU v1.1 by Scarface [Lethal Developments]\n");
	print("  Loading Configuration...\n");
	rConfig();
	print("    Configuration: ");
	if(sType == 1){   print("      |Save-Type        [Automatic]"); }else if(sType==2){ print("      |Save-Type        [OnCommand]"); }
	if(sMoney > 0){   print("      |saveMoney        [Activated]"); }else{ print("      |saveMoney        [Disabled]"); }
	if(sScore > 0){   print("      |saveScore        [Activated]"); }else{ print("      |saveScore        [Disabled]"); }
	if(sDeaths > 0){  print("      |saveDeaths       [Activated]"); }else{ print("      |saveDeaths       [Disabled]"); }
	return 1;
}

public OnPlayerConnect(playerid)
{
if(fexist(encode(PlayerName(playerid))))
	{
	SendClientMessage(playerid, COLOR_YELLOW, "<SSU v1.1> Stats Restored!");
	new File:fhandle;
	new cname[256];
	new temp[256];
	new value[256];
	fhandle = fopen(encode(PlayerName(playerid)),io_readwrite);
		while(fread(fhandle,temp,sizeof(temp),false))
		{
		    cname = strtok2(temp,0);
			value = strtok2(temp,(strlen(cname)+1));
			if(sMoney > 0){
			if(strcmp(cname,"Cash",true) == 0) GivePlayerMoney(playerid,strval(value));}
			if(sDeaths > 0){
			if(strcmp(cname,"Score",true) == 0) KillCount[playerid] = strval(value);}
			if(sScore > 0){
			if(strcmp(cname,"DeathCount",true) == 0) DeathCount[playerid] = strval(value);}
		}
		fclose(fhandle);
}
return 1;
}




public PlayerName(playerid)
{
  new pName[32];
  GetPlayerName(playerid, pName, sizeof(pName));
  return pName;
}

public OnPlayerDisconnect(playerid)
{
	DeathCount[playerid] = 0;
	KillCount[playerid] = 0;
if(sType == 1)
{
sStats(playerid);
}
return 1;
}

public OnPlayerDeath(playerid, killerid, reason)
{
	DeathCount[playerid]++;
	KillCount[killerid]++;
}

/*
============
    Body
============
*/

sStats(playerid)
{
new File:fhandle;
	new temp[256];
	if(fexist(encode(PlayerName(playerid)))){
  	fremove(encode(PlayerName(playerid)));}

//    printf("Save Successful(%s)",PlayerName(playerid));
    
	fhandle = fopen(encode(PlayerName(playerid)),io_write);
	if(sMoney > 0){
	format(temp,sizeof(temp),"Cash %d\r\n", GetPlayerMoney(playerid));
	fwrite(fhandle,temp);}
	if(sScore > 0){
	format(temp,sizeof(temp),"Score %d\r\n", KillCount[playerid]);
	fwrite(fhandle,temp);}
	if(sDeaths > 0){
	format(temp,sizeof(temp),"DeathCount %d\r\n", DeathCount[playerid]);
	fwrite(fhandle,temp);}
	
	fclose(fhandle);
	return 1;
}

/*
============
  Commands
============
*/

public OnPlayerCommandText(playerid, cmdtext[])
{
	new tmp[256], string[256], cmd[256];
	new idplayer, idx;
	
	cmd = strtok(cmdtext, idx);
	
	if(strcmp(cmd, "/sstats", true) == 0)
	 {
		tmp = strtok(cmdtext, idx);
		if(!strlen(tmp))
		{
			SendClientMessage(playerid, COLOR_WHITE, "USAGE: /sstats [PlayerID]");
			return 1;
		}
		idplayer = strval(tmp);

	 	if(IsPlayerConnected(idplayer))
  		{
  		    format(string, sizeof(string), "%s's Stats:", PlayerName(idplayer));
			SendClientMessage(playerid, COLOR_YELLOW, string);
		   	format(string, sizeof(string), "Cash: $%d Kills: %d Deaths: %d Ratio: %0.2f", GetPlayerMoney(idplayer), KillCount[idplayer], DeathCount[idplayer], Float:KillCount[idplayer]/Float:DeathCount[idplayer]);
			SendClientMessage(playerid, COLOR_YELLOW, string);
   			return 1;
 		}
 		else {
 		SendClientMessage(playerid, COLOR_WHITE, "Player Not Connected!");
 		return 1;
 		}
	 }
	
	else if(strcmp(cmdtext, "/ssave", true) == 0)
	 {
	    if(sType != 2){ SendClientMessage(playerid, COLOR_WHITE, "SERVER: Unknown command."); return 1;}
		sStats(playerid);
		SendClientMessage(playerid, COLOR_YELLOW, "<SSU v1.0> Stats Saved!");
		return 1;
	 }
	 
	 else if(strcmp(cmdtext, "/statshelp", true) == 0)
	 	{
	    SendClientMessage(playerid, COLOR_YELLOW,"SSU: Stats Saving Utility v1.1 by Scarface");
        if(sType == 1){ SendClientMessage(playerid, COLOR_YELLOW,"Type: Automatic Save"); }else if(sType == 2) { SendClientMessage(playerid, COLOR_YELLOW,"Type: OnCommand");}
		SendClientMessage(playerid, COLOR_YELLOW,"Commands:");
		if(sType == 2){SendClientMessage(playerid, COLOR_YELLOW,"/ssave - Saves Your Stats");}
		SendClientMessage(playerid, COLOR_YELLOW,"/mystats - Displays your stats publicly");
		SendClientMessage(playerid, COLOR_YELLOW,"/sstats [PlayerID] - Displays Players Stats");
	    return 1;
	    }

	 else if(strcmp(cmdtext, "/mystats", true) == 0)
	 {
	        format(string, sizeof(string), "%s's Stats:", PlayerName(playerid));
			SendClientMessageToAll(COLOR_YELLOW, string);
		   	format(string, sizeof(string), "Cash: $%d Kills: %d Deaths: %d Ratio: %0.2f", GetPlayerMoney(playerid), KillCount[playerid], DeathCount[playerid], (Float:KillCount[playerid]/Float:DeathCount[playerid]));
			SendClientMessageToAll(COLOR_YELLOW, string);
			return 1;
   			}
   			return 0;
		}

/*
============
   Config
============
*/

rConfig()
{
	new File:fhandle;
	new cname[256];
	new temp[256];
	new value[256];
	fhandle = fopen("ssuconfig.txt",io_readwrite);
		while(fread(fhandle,temp,sizeof(temp),false))
		{
		    cname = strtok2(temp,0);
			value = strtok2(temp,(strlen(cname)+1));
			if(strcmp(cname,"Save-Type",true) == 0) sType = strval(value);
			else if(strcmp(cname,"saveMoney",true) == 0) sMoney = strval(value);
			else if(strcmp(cname,"saveScore",true) == 0) sScore = strval(value);
			else if(strcmp(cname,"saveDeaths",true) == 0) sDeaths = strval(value);
		}
		fclose(fhandle);
		return 1;
}

/*
========================
     String Defines
========================
*/

strtok2(const string[],valve)
{
	new index=valve;
	new length = strlen(string);
	while ((index < length) && (string[index] <= ' '))
	{
		index++;
	}

	new offset = index;
	new result[20];
	while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
	{
		result[index - offset] = string[index];
		index++;
	}
	result[index - offset] = EOS;
	return result;
}

public encode(nickname[]) {  // Based on DracoBlue's udb_encode()
  new tmp[MAX_STRING];
  set(tmp,nickname);
  tmp=strreplace("_"," 00",tmp);
  tmp=strreplace(";"," 01",tmp);
  tmp=strreplace("!"," 02",tmp);
  tmp=strreplace("/"," 03",tmp);
  tmp=strreplace("\\"," 04",tmp);
  tmp=strreplace("["," 05",tmp);
  tmp=strreplace("]"," 06",tmp);
  tmp=strreplace("?"," 07",tmp);
  tmp=strreplace("."," 08",tmp);
  tmp=strreplace("*"," 09",tmp);
  tmp=strreplace("<"," 10",tmp);
  tmp=strreplace(">"," 11",tmp);
  tmp=strreplace("{"," 12",tmp);
  tmp=strreplace("}"," 13",tmp);
  format(gString,sizeof(gString),"stats-%s",tmp);
  return gString;
}