[Release] Basic Stats System

Started by umar4911, Dec 02, 2017, 06:43 AM

Previous topic - Next topic

umar4911

Basic Stats System


Update:
Changed the stats using class

Installation:
Player Class
Add this on the top of your script
class PlayerStats
{
 kills = 0;
 headshots = 0;
 deaths = 0;
 rank = 0;

}
OnScriptLoad
function onScriptLoad()
{
stats <- ConnectSQL("stats.db");
QuerySQL(stats, "create table if not exists pstats ( Name TEXT, Rank TEXT, Kills INTEGER, Headshots INTEGER, Deaths INTEGER )");
}
onPlayerJoin
function onPlayerJoin(player)
{
StatsInfo(player);
}
Command
function onPlayerCommand(player, command, arguments)
{
local cmd, text;
cmd = command.tolower();
text = arguments;
 if(cmd == "stats")
 {
  if(!arguments)
  {
   MessagePlayer("[#FF0000]Information:[#FFFFFF] Your stats : ", player);
   MessagePlayer("[#FFFFFF]Kills : " + stats[player.ID].kills + " Headshots : " + stats[player.ID].headshots + " Deaths : " + stats[player.ID].deaths + " Rank : " + stats[player.ID].rank + ".", player);
  }
  else
  {
   local plr = FindPlayer(arguments);
   if(!plr) MessagePlayer("[#FF0000]Usage Error:[#FFFFFF] Unknown Player", player);
   else
   {
    local q = QuerySQL(stats, "SELECT * FROM pstats WHERE Name = '" + plr.Name + "'");
    MessagePlayer("[#FF0000]Information:[#FFFFFF] "+plr.Name+ " stats : ", player);
    MessagePlayer("[#FFFFFF]Kills : " + stats[player.ID].kills + " Headshots : " + stats[player.ID].headshots + " Deaths : " + stats[player.ID].deaths + " Rank : " + stats[player.ID].rank + ".", player);
   }
  }
 }

 
   else if(cmd == "resetstats")
 {
  if(!arguments) MessagePlayer("[#FF0000]Command Error:[#FFFFFF] Use /" + cmd + " [player] ", player);
  else
  {
   local plr = FindPlayer(arguments);
   if(!plr) MessagePlayer("[#FF0000]Usage Error:[#FFFFFF] Invalid player", player);
   else
   {   
    Message("[#514FFF]Administrator Command:[#FFFFFF] Admin[" + player.Name + "] reseted stats of player : " + plr.Name+ ".");
    MessagePlayer("[#FF0000]Information:[#FFFFFF] Your stats have been reseted by Admin " + player.Name + ".", plr);
    QuerySQL(stats, "UPDATE pstats SET Rank = '1', Kills = '0', Deaths = '0', Headshots = '0' WHERE Name = '" + plr.Name + "'");
stats[plr.ID].kills = 0;
stats[plr.ID].deaths = 0;
stats[plr.ID].headshots = 0;
stats[plr.ID].rank = 1;
   }
  }
 }
 
 

 
 else if(cmd == "setstats")
 {
  if(!arguments)
  {
   MessagePlayer("[#FF0000]Command Error:[#FFFFFF] Use /" + cmd + " [player] [stats(0 - 3)] [value]", player);
   MessagePlayer("0 = Rank, 1 = Kills, 2 = Headshots, 3 = Deaths", player);
  }
  else if(NumTok(arguments, " ") < 3 )
  {
   MessagePlayer("[#FF0000]Command Error:[#FFFFFF] Use /" + cmd + " [player] [stats(0 - 3)] [value]", player);
   MessagePlayer("0 = Rank, 1 = Kills, 2 = Headshots, 3 = Deaths", player);
  }
  else if(!IsNum(GetTok(arguments, " ", 2))) MessagePlayer("[#FF0000]Usage Error:[#FFFFFF] Stats should be between 0 - 4", player);
  else if(!IsNum(GetTok(arguments, " ", 3))) MessagePlayer("[#FF0000]Usage Error:[#FFFFFF] value should be in numbers", player);
  else if(GetTok(arguments, " ", 2).tointeger() < 0 ) MessagePlayer("[#FF0000]Usage Error:[#FFFFFF] Stats should be between 0 - 4", player);
  else if(GetTok(arguments, " ", 2).tointeger() > 4 ) MessagePlayer("[#FF0000]Usage Error:[#FFFFFF] Stats should be between 0 - 4", player);
  else if(GetTok(arguments, " ", 3).tointeger() < 0 ) MessagePlayer("[#FF0000]Usage Error:[#FFFFFF] Minimum value is 0", player);
  else
  {
   local plr = FindPlayer(GetTok(arguments, " ", 1));
   local stat = GetTok(arguments, " ", 2).tointeger();
   local val = GetTok(arguments, " ", 3).tointeger();
   if(!plr) MessagePlayer("[#FF0000]Usage Error:[#FFFFFF] Invalid player", player);
   else
   {
    if(stat == 0)
    {
     if(val < 1) MessagePlayer("[#FF0000]Usage Error:[#FFFFFF] Rank cannot be less than 1", player);
     else QuerySQL(stats, "UPDATE pstats SET Rank = '" + val + "' WHERE Name = '" + plr.Name + "'");
stats[plr.ID].rank = val;
     Message("[#514FFF]Developer Command:[#FFFFFF] Developer[" + player.Name + "] set Player : " + plr.Name +"'s Rank to " + val + ".");
    }
    else if(stat == 1)
    {
     QuerySQL(stats, "UPDATE pstats SET Kills = '" + val + "' WHERE Name = '" + plr.Name + "'");
     Message("[#514FFF]Developer Command:[#FFFFFF] Developer[" + player.Name + "]set Player : " + plr.Name + "'s Kills to " + val + ".");
     stats[plr.ID].kills = val;
    }
    else if(stat == 2)
    {
     QuerySQL(stats, "UPDATE pstats SET HeadShots = '" + val + "' WHERE Name = '" + plr.Name + "'");
     Message("[#514FFF]Developer Command:[#FFFFFF] Developer[" + player.Name + "] set Player : " + plr.Name+ "'s HeadShots to " + val + ".");
     stats[plr.ID].headshots = val;
    }
    else if(stat == 3)
    {
     QuerySQL(stats, "UPDATE pstats SET Deaths = '" + val + "' WHERE Name = '" + plr.Name + "'");
     Message("[#514FFF]Developer Command:[#FFFFFF] Developer[" + player.Name + "] set Player : " + plr.Name+ "'s Deaths to " + val + ".");
     stats[plr.ID].deaths = val;
    }
   }
  }
 }
}

Functions
function onPlayerKill(killer, player, reason, bodypart)
{
rankcheck(killer);
rankcheck(player);

 if(bodypart == 6)
 {
  MessagePlayer("[#FFFFFF]You received $1000 bonus for killing through headshot", killer);
  local pkill = stats[player.ID].kills + 1;
  local phead = stats[player.ID].headshots + 1;
  QuerySQL(stats, "UPDATE pstats SET Kills = '" + pkill + "' WHERE Name = '" + killer.Name + "'");
  QuerySQL(stats, "UPDATE pstats SET Headshots = '" + phead + "' WHERE Name = '" + killer.Name + "'");
  local pdeath = stats[player.ID].deaths + 1;
  QuerySQL(stats, "UPDATE pstats SET Deaths = '"+pdeath+"' WHERE Name = '"+player.Name+"'");

 }
 else
 {
  local q2 = QuerySQL(stats, "SELECT * FROM pstats WHERE Name = '" + killer.Name + "'");
  local pkill = GetSQLColumnData(q2, 2).tointeger() + 1;
  QuerySQL(stats, "UPDATE pstats SET Kills = '" + pkill + "' WHERE Name = '" + killer.Name + "'");
  local q3 = QuerySQL(stats, "SELECT * FROM pstats WHERE Name = '" + player.Name + "'");
  local pdeath = GetSQLColumnData(q3, 2).tointeger() + 1;
  QuerySQL(stats, "UPDATE pstats SET Deaths = '"+pdeath+"' WHERE Name = '"+player.Name+"'");
 }
 
}

function onPlayerDeath( player, reason)
{
  local pdeath = stats[player.ID].deaths + 1;
  QuerySQL(stats, "UPDATE pstats SET Deaths = '" + pdeath + "' WHERE Name = '" + player.Name + "'");
  rankcheck(player);
}
























function rankcheck(player)
{
 local q2 = QuerySQL(stats, "SELECT * FROM pstats WHERE Name = '" + player.Name+ "'");
 local ran = GetSQLColumnData(q2, 1).tointeger();
 
 local kills = GetSQLColumnData(q2, 2).tointeger();

 if(kills >= 5000 && ran < 8)
 {
  QuerySQL(stats, "UPDATE pstats SET Rank = '8' WHERE Name = '" + player.Name + "'");
  MessagePlayer("[#FF0000]Information:[#FFFFFF] You have completed 5000 kills. Now your rank is 8", player);
  MessagePlayer("[#FF0000]Information:[#FFFFFF] You received 000 cash", player);
 }
 
 else if(kills >= 4000 && ran < 7 && kills < 5000)
 {
  QuerySQL(stats, "UPDATE pstats SET Rank = '7' WHERE Name = '" + player.Name + "'");
  MessagePlayer("[#FF0000]Information:[#FFFFFF] You have completed 4000 kills. Now your rank is 8", player);
  MessagePlayer("[#FF0000]Information:[#FFFFFF] You received 000 cash", player);
 }

 else if(kills >= 3000 && ran < 6 && kills < 4000)
 {
  QuerySQL(stats, "UPDATE pstats SET Rank = '6' WHERE Name = '" + player.Name + "'");
  MessagePlayer("[#FF0000]Information:[#FFFFFF] You have completed 3000 kills. Now your rank is 8", player);
  MessagePlayer("[#FF0000]Information:[#FFFFFF] You received 000 cash", player);
 }

 else if(kills >= 2000 && ran < 5 && kills < 3000)
 {
  QuerySQL(stats, "UPDATE pstats SET Rank = '5' WHERE Name = '" + player.Name + "'");
  MessagePlayer("[#FF0000]Information:[#FFFFFF] You have completed 2000 kills. Now your rank is 8", player);
  MessagePlayer("[#FF0000]Information:[#FFFFFF] You received 000 cash", player);
 }

 else if(kills >= 1000 && ran < 4 && kills < 2000)
 {
  QuerySQL(stats, "UPDATE pstats SET Rank = '4' WHERE Name = '" + player.Name + "'");
  MessagePlayer("[#FF0000]Information:[#FFFFFF] You have completed 1000 kills. Now your rank is 8", player);
  MessagePlayer("[#FF0000]Information:[#FFFFFF] You received 000 cash", player);
 }

 else if(kills >= 500 && ran < 3 && kills < 1000)
 {
  QuerySQL(stats, "UPDATE pstats SET Rank = '3' WHERE Name = '" + player.Name + "'");
  MessagePlayer("[#FF0000]Information:[#FFFFFF] You have completed 500 kills. Now your rank is 8", player);
  MessagePlayer("[#FF0000]Information:[#FFFFFF] You received 000 cash", player);
 }

 else if(kills >= 100 && ran < 2 && kills < 500)
 {
  QuerySQL(stats, "UPDATE pstats SET Rank = '2' WHERE Name = '" + player.Name + "'");
  MessagePlayer("[#FF0000]Information:[#FFFFFF] You have completed 100 kills. Now your rank is 8", player);
  MessagePlayer("[#FF0000]Information:[#FFFFFF] You received 000 cash", player);
 }
 
}
function StatsInfo(player)
{
 local q = QuerySQL(stats, "SELECT * FROM pstats WHERE Name = '"+player.Name+"'");
 if(!q) QuerySQL(stats, "INSERT INTO pstats ( Name, Rank, Kills, Headshots, Deaths ) VALUES ( '" + escapeSQLString(player.Name) + "', '1', '0', '0', '0' )");
 else
 {
stats[player.ID].kills = GetSQLColumnData(q, 2).tointeger();
stats[player.ID].headshots = GetSQLColumnData(q, 3).tointeger();
stats[player.ID].deaths = GetSQLColumnData(q, 4).tointeger();
stats[player.ID].rank = GetSQLColumnData(q, 1).tointeger();

 }
 }


function GetTok(string, separator, n, ...)
{
local m = vargv.len() > 0 ? vargv[0] : n,
tokenized = split(string, separator),
text = "";
if (n > tokenized.len() || n < 1) return null;
for (; n <= m; n++)
{
text += text == "" ? tokenized[n-1] : separator + tokenized[n-1];
}
return text;
}

function NumTok(string, separator)
{
local tokenized = split(string, separator);
return tokenized.len();
}

function GetPlayer( plr )
{
if ( plr )
{
if ( IsNum( plr ) )
{
plr = FindPlayer( plr.tointeger() );
if ( plr ) return plr;
else return false;
}
else
{
plr = FindPlayer( plr );
if ( plr ) return plr;
else return false;
}
}
else return false;
}




If any bug found, please inform me
RT Member | Asian City Scripter | CTB Contributor | VKs Contributor | European City Contributor | EAD Scripter

Mohamed Boubekri

@umar4911 Just one ERRoR i Have Death Function But IF The Player Killed Not Delet Cash And Not UPDATED.

// Function onplayerKill Its Work But Function onplayerDeath Not Working // Its Work Just not Delet Cash And NOT UPDATE Death For Stats

function onPlayerDeath( player, reason ,weapon )
{
DecCash( player, 100 );
Announce("Wasted",player,5);
  local q3 = QuerySQL(stats, "SELECT * FROM pstats WHERE Name = '" + player.Name + "'");
  local pdeath = GetSQLColumnData(q3, 2).tointeger() + 1;
  QuerySQL(stats, "UPDATE pstats SET Deaths = '" + pdeath + "' WHERE Name = '" + player.Name + "'");
  rankcheck(player);
}
function onPlayerDeath( player, weapon )
{
DecCash( player, 100 );
Announce("Wasted",player,5);
  local q3 = QuerySQL(stats, "SELECT * FROM pstats WHERE Name = '" + player.Name + "'");
  local pdeath = GetSQLColumnData(q3, 2).tointeger() + 1;
  QuerySQL(stats, "UPDATE pstats SET Deaths = '" + pdeath + "' WHERE Name = '" + player.Name + "'");
  rankcheck(player);
}
function onPlayerDeath( player, reason )
{
DecCash( player, 100 );
Announce("Wasted",player,5);
  local q3 = QuerySQL(stats, "SELECT * FROM pstats WHERE Name = '" + player.Name + "'");
  local pdeath = GetSQLColumnData(q3, 2).tointeger() + 1;
  QuerySQL(stats, "UPDATE pstats SET Deaths = '" + pdeath + "' WHERE Name = '" + player.Name + "'");
  rankcheck(player);
}

HELPPPPPPPPP
| What now ? | Not yet ! |
Morrocan:- [ 🇲🇦 ].

Xmair

Delete all 3 onPlayerDeath functions and keep this one:
function onPlayerDeath( player, weapon )
{
DecCash( player, 100 );
Announce("Wasted",player,5);
  local q3 = QuerySQL(stats, "SELECT * FROM pstats WHERE Name = '" + player.Name + "'");
  local pdeath = GetSQLColumnData(q3, 2).tointeger() + 1;
  QuerySQL(stats, "UPDATE pstats SET Deaths = '" + pdeath + "' WHERE Name = '" + player.Name + "'");
  rankcheck(player);
}

Credits to Boystang!

VU Full Member | VCDC 6 Coordinator & Scripter | EG A/D Contributor | Developer of VCCNR | Developer of KTB | Ex-Scripter of EAD

Mohamed Boubekri

@Xmair Niice Work  But When i Do /kill Not UPDATED  // Lets Me tell You

That Function Its UPDATE If Kill With Weapon
function onPlayerDeath( player, weapon )
{
DecCash( player, 100 );
Announce("Wasted",player,5);
  local q3 = QuerySQL(stats, "SELECT * FROM pstats WHERE Name = '" + player.Name + "'");
  local pdeath = GetSQLColumnData(q3, 2).tointeger() + 1;
  QuerySQL(stats, "UPDATE pstats SET Deaths = '" + pdeath + "' WHERE Name = '" + player.Name + "'");
  rankcheck(player);
}

That Its UPDATED Just If Player Do /kill
function onPlayerDeath( player, reason )
{
DecCash( player, 100 );
Announce("Wasted",player,5);
  local q3 = QuerySQL(stats, "SELECT * FROM pstats WHERE Name = '" + player.Name + "'");
  local pdeath = GetSQLColumnData(q3, 2).tointeger() + 1;
  QuerySQL(stats, "UPDATE pstats SET Deaths = '" + pdeath + "' WHERE Name = '" + player.Name + "'");
  rankcheck(player);
}

Hmm Can Make Two Function ?
| What now ? | Not yet ! |
Morrocan:- [ 🇲🇦 ].

Xmair


Credits to Boystang!

VU Full Member | VCDC 6 Coordinator & Scripter | EG A/D Contributor | Developer of VCCNR | Developer of KTB | Ex-Scripter of EAD

Mohamed Boubekri

| What now ? | Not yet ! |
Morrocan:- [ 🇲🇦 ].

umar4911

Quote from: Mohamed on Dec 02, 2017, 02:23 PM
Quote from: Xmair on Dec 02, 2017, 02:17 PMI'm out.
:( Do You Have Any Solution Of it :/
You cant make 2 functions and death cant be with weapon. It is always playerkill as their is always killer
RT Member | Asian City Scripter | CTB Contributor | VKs Contributor | European City Contributor | EAD Scripter

Mohamed Boubekri

@umar4911 I Fixed All, Now I Have just one Problem, OnPlayerDeath Not UPDATE :(
| What now ? | Not yet ! |
Morrocan:- [ 🇲🇦 ].

umar4911

Quote from: Mohamed on Dec 02, 2017, 03:12 PM@umar4911 I Fixed All, Now I Have just one Problem, OnPlayerDeath Not UPDATE :(
give info
RT Member | Asian City Scripter | CTB Contributor | VKs Contributor | European City Contributor | EAD Scripter

Mohamed Boubekri

@umar4911 Problem Solved By Me.

Thanks Very much For it i Really Happy :D
| What now ? | Not yet ! |
Morrocan:- [ 🇲🇦 ].

Mohamed Boubekri

@umar4911 That iTs Work But If Me Death i Will Not UPDATED After 5min then i do /kill // then UPDATED

i Want UPDATE AUTO :( Same OnplayerKill :'(
| What now ? | Not yet ! |
Morrocan:- [ 🇲🇦 ].

umar4911

Quote from: Mohamed on Dec 02, 2017, 08:53 PM@umar4911 That iTs Work But If Me Death i Will Not UPDATED After 5min then i do /kill // then UPDATED

i Want UPDATE AUTO :( Same OnplayerKill :'(
I cant understand what are you saying
RT Member | Asian City Scripter | CTB Contributor | VKs Contributor | European City Contributor | EAD Scripter

D4rkR420R

Nice idea, however I prefer to add up the kills/deaths on the class & save the class in the database once the player leaves. FYI, "Installion" is spelled wrong in which it's spelled "Installation". :3 Your welcome.

Mohamed Boubekri

Btw My Problem of PlayerDeath Fixed :D
| What now ? | Not yet ! |
Morrocan:- [ 🇲🇦 ].

kennedyarz