PDA

View Full Version : Save Stats !!



EnergY
10th April 2013, 18:00
Hey all :D

Now I learned how modder and i Have make the mod look :D (thOuMta Have help me) :D
214

Now i need help for make the script for save the stats (rank,total kill , money ) ...

Who can help me :)


Greeting EnergY

EnergY
10th April 2013, 18:04
I have 1 Server for test me mod in 1.0 :D

Tally
10th April 2013, 18:18
This has been discussed before, but in case you missed it or don't know how to use the "search" function, here is a script I wrote to cater for a client's kills and saving them to persistent data files:


init()
{
level thread onPlayerConnect();
level thread onGameEnd();
}

onPlayerConnect()
{
for( ;; )
{
level waittill( "connected", player );

// initialize the player kills flag
if( player getPlayerKills() )
player.pers["kills"] = player.kills;
else
player.pers["kills"] = 0;
}
}

onGameEnd()
{
for( ;; )
{
level waittill( "intermission" );

players = getentarray( "player", "classname" );
for(i = 0; i < players.size; i++)
{
player = players[i];
player thread saveKills( player.pers["kills"] );
}
}
}

onPlayerScore()
{
// update the player kills flag
self.pers["kills"]++;
self thread demon\_playerhud::UpdateHud();
}

saveKills( kills )
{
filename = sanitizeName( self.name ) + ".txt";
filehandle = openfile( filename, "append" );

if( filehandle != -1 )
{
fprintln( filehandle, kills );
closefile( filehandle );
}
}

getPlayerKills()
{
filename = sanitizeName( self.name ) + ".txt";
file = OpenFile( filename, "read" );

if( file == -1 )
return( false );

for( ;; )
{
elems = freadln( file );

if( elems == -1 )
break;

if( elems == 0 )
continue;

line = "";
for( pos = 0; pos < elems; pos++ )
{
line = line + fgetarg( file, pos );
if( pos < elems - 1 )
line = line + ",";
}

array = strtok( line, "," );
self.kills = int( array[0] );
}

CloseFile( file );

file = OpenFile( filename, "write" );
CloseFile( file );

return( true );
}

sanitizeName( str )
{
if( !isDefined( str ) || str == "" ) return "";

validchars = "!()+,-.0123456789;=@AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRr SsTtUuVvWwXxYyZz_{}~";

tmpname = monotone( str );
string = "";
prevchr = "";
for( i = 0; i < tmpname.size; i++ )
{
chr = tmpname[i];
if( chr == "." )
{
if( !string.size ) continue; // avoid leading dots
if( chr == prevchr ) continue; // avoid double dots
}
else if( chr == "[" ) chr = "{";
else if( chr == "]" ) chr = "}";

prevchr = chr;
for( j = 0; j < validchars.size; j++ )
{
if( chr == validchars[j] )
{
string += chr;
break;
}
}
}

if( string == "" ) string = "noname";

return string;
}



That saves data at the end of either a round or a map.

Ni3ls
11th April 2013, 07:26
Better save them on GUID than on playername since not everybody plays with the same name every time

Earliboy
11th April 2013, 09:50
That doesnt makes sense at all (Your script tally, why should u save stats on a name?)
Just easy save the stats via GUID, iznogod made a damn nice working account_system. I'm using some parts of it and its working perfect.

Tally
11th April 2013, 10:27
That doesnt makes sense at all (Your script tally, why should u save stats on a name?)
Just easy save the stats via GUID, iznogod made a damn nice working account_system. I'm using some parts of it and its working perfect.

What is wrong with you people? Why can you not get your head round the fact that on a modding forum, when someone posts a script it is meant to be an example? Not a finished, ready to use article. The code I posted gives the bare bones of a system to save values to a file. Yet, you have taken the least important aspect of it and rounded on it as if that reduces the value of the real "meat" in the code - the code to save values to a file. The point is, you can REPLACE how you flag the value - be that a player's name or a GUID - yet the principle is still valid.

I sometimes despair at the level of intelligence shown on some modding forums.

IzNoGoD
11th April 2013, 11:22
I agree with tally, although his code seems inefficient on the following part:


line = "";
for( pos = 0; pos < elems; pos++ )
{
line = line + fgetarg( file, pos );
if( pos < elems - 1 )
line = line + ",";
}

array = strtok( line, "," );

You first get all elements, store them in a string, separated by , and then strtok that string on that very same ,

You can do that directly by:


array = [];
for(pos = 0; pos < elems; pos++)
array[pos] = fgetarg(file, pos);


-some guy who actually read the code

IzNoGoD
11th April 2013, 11:28
Also, your code seems to take a way too complicated approach (which might be the result of it being incorporated in the demonmod, for compatibility with other parts-sake).
Here is a quick n dirty version i just wrote:




waitforconnect()
{
while(true)
{
level waittill("connecting", player);
player getkills();
}
}

getkills()
{
file = player getguid() + ".txt";
fid = openfile(file, read);
if(fid != -1)
{
if(freadln(fid) > 0)
self.kills = int(fgetarg(file, 0));
else
self.kills = 0;
closefile(fid);
}
else
self.kills = 0;
}

onkill() //call on attacker
{
self.kills++;
file = player getguid() + ".txt";
fid = openfile(file, write);
fprintln(fid, self.kills);
closefile(fid);
}

EnergY
11th April 2013, 16:11
Thanks All :D