I have 1 Server for test me mod in 1.0
Xfire: romain88120 and energyfun !!
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:
That saves data at the end of either a round or a map.PHP Code:
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;=@AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz_{}~";
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;
}
EnergY (10th April 2013),kung foo man (10th April 2013)
Better save them on GUID than on playername since not everybody plays with the same name every time
EnergY (11th April 2013),kung foo man (11th April 2013)
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.
No ... No ... this is not possible .......
EnergY (11th April 2013)
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.
EnergY (11th April 2013),EvoloZz (11th April 2013),kung foo man (11th April 2013)
I agree with tally, although his code seems inefficient on the following part:
You first get all elements, store them in a string, separated by , and then strtok that string on that very same ,PHP Code:
line = "";
for( pos = 0; pos < elems; pos++ )
{
line = line + fgetarg( file, pos );
if( pos < elems - 1 )
line = line + ",";
}
array = strtok( line, "," );
You can do that directly by:
-some guy who actually read the codePHP Code:
array = [];
for(pos = 0; pos < elems; pos++)
array[pos] = fgetarg(file, pos);
EnergY (11th April 2013),kung foo man (11th April 2013)
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:
PHP Code:
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)
Thanks All
Xfire: romain88120 and energyfun !!