Game Development Community

Files Erased...

by David West · in Technical Issues · 08/30/2006 (11:24 am) · 10 replies

Im not very good at scripting and stuff, but thats why i came here >.<

i made a stat script which keeps track of everyones kills, deaths, ect..
and everyone that comes on my server creates a file for the stats to be on. but everytime i quit the game and return, it erases my stats for no reason...but when i just go back to the main menu and go back into the game, it still has my stats, its just so confusing!! -.-

heres the stat code i put at the bottom of common/server/clientConnection.cs

function checkStats(%client)
{
%filename = "scripts/serverStats/user" @ %client.namebase @ ".cs";
if(!isFile(%filename))
{
createStats(%client);
}
else
{
%file = new FileObject()
{
};
if (%file.openForRead(%filename))
{
%file.readLine(); //first line is 0!!
%client.TotalServerKills = %file.readLine();
%client.TotalServerDeaths = %file.readLine();
%client.Level = %file.readLine();
%client.Experience = %file.readLine();
%file.close();
}
%file.delete();
}
}

function createStats(%client)
{
// the client has no record on this server, so we need to give him one
%filename = "scripts/serverStats/user" @ %client.namebase @ ".cs";
export('', %filename, False);
new MessageVector(TempVector);
TempVector.dump(%filename);
TempVector.delete();

%file = new FileObject()//;
{
};
if ( %file.openForAppend( %filename ) )
{
%file.writeLine(0);
%file.writeLine(0);
%file.writeLine(1);
%file.writeLine(0);
%file.close();
}
%file.delete();

checkStats(%client);
}

function saveStats(%client)
{
%filename = "scripts/serverStats/user" @ %client.namebase @ ".cs";
export('', %filename, Append);
new MessageVector(TempVector);
TempVector.dump(%filename);
TempVector.delete();

%file = new FileObject()//;
{
};
if(%file.openForAppend(%filename))
{
%file.writeLine(%client.TotalServerKills);
%file.writeLine(%client.TotalServerDeaths);
%file.writeLine(%client.Level);
%file.writeLine(%client.Experience);
%file.close();
}
%file.delete();
}

function moreXp(%client,%amount)
{
%levelup = getnextLevelUp(%client);
%client.experience += %amount;
if (%client.experience > %levelup)
{
%client.level++;
messageAll('MsgClientKilled','%1 Levels Up! New Level: %2',%client.name,%client.level);
}
else
messageClient(%client,'MsgClientXp', '\c2Xp Gained: %2.:.Total Xp: %3.:.Next Lvl Up: %1.', %levelup++,%amount,%client.experience);
}

function getNextLevelUP(%client)
{
%level = %client.level;
if (%level == 1)
return 99;
else if (%level == 2)
return 399;
else if (%level == 3)
return 999;
else if (%level == 4)
return 1999;
else if (%level == 5)
return 3299;
else if (%level == 6)
return 4999;
else if (%level == 7)
return 7099;
else if (%level == 8)
return 8999;
else if (%level == 9)
return 11099;
else if (%level == 10)
return 14100;
else if (%level == 11)
return 17499;
}

function RecalcServerKillStat(%client)
{
%client.TotalServerKills++;
saveStats(%client);
}

function RecalcServerDeathStat(%client)
{
%client.TotalServerDeaths++;
saveStats(%client);
}


Oh and i put checkStats(%client); on the bottom of GameConnection::setPlayerName
and put saveStats(%client); under GameConnection::onDrop

#1
08/30/2006 (3:37 pm)
And also sometimes when someone joins the server is crashes for no reason : /

but i did find out that it has to do something with

TempVector.dump(%filename);
TempVector.delete();

i need help! =)
#2
08/30/2006 (3:41 pm)
If you want someone to read that.
you should use the provided forum syntax to help clean up the formatting.

function checkStats(%client)
{
         %filename = "scripts/serverStats/user" @ %client.namebase @ ".cs";
         if(!isFile(%filename))
         {
                createStats(%client);
         }
         else
        {
                %file = new FileObject()
                {
                };
                if (%file.openForRead(%filename))
                {
                       %file.readLine(); //first line is 0!!
                       %client.TotalServerKills = %file.readLine();
                       %client.TotalServerDeaths = %file.readLine();
                       %client.Level = %file.readLine();
                       %client.Experience = %file.readLine();
                       %file.close();
                 }
                 %file.delete();
         }
}

what is going under %file = new FileObject() ??
delete that I think.
prolly not the problem but here is a cleaner easier way to read the code.
#3
08/30/2006 (4:04 pm)
function checkStats(%client)
{
  %filename = "scripts/serverStats/user" @ %client.namebase @ ".cs"; 
  if(!isFile(%filename))
  {
    createStats(%client);
  }
  else
  {
    %file = new FileObject();
    if (%file.openForRead(%filename))
    {
      %client.TotalServerKills = %file.readLine();
      %client.TotalServerDeaths = %file.readLine();
      %client.Level = %file.readLine();
      %client.Experience = %file.readLine();
      %file.close();
    }
    %file.delete();
  }
}

function createStats(%client) 
{ 
  %filename = "scripts/serverStats/user" @ %client.namebase @ ".cs"; 
  %file = new FileObject();
  if ( %file.openForWrite( %filename ) ) 
  {  
    %file.writeLine(0); 
    %file.writeLine(0); 
    %file.writeLine(1);
    %file.writeLine(0);
    %file.close(); 
  } 
  %file.delete(); 
  checkStats(%client); 
} 

function saveStats(%client) 
{ 
  %filename = "scripts/serverStats/user" @ %client.namebase @ ".cs"; 
  %file = new FileObject()
  if(%file.openForWrite(%filename)) 
  { 
    %file.writeLine(%client.TotalServerKills); 
    %file.writeLine(%client.TotalServerDeaths); 
    %file.writeLine(%client.Level); 
    %file.writeLine(%client.Experience); 
    %file.close();
  } 
  %file.delete(); 
}

function moreXp(%client,%amount)
{
  %levelup = getnextLevelUp(%client);
  %client.experience += %amount;
  if (%client.experience > %levelup)
  {
    %client.level++;
    messageAll('MsgClientKilled','%1 Levels Up! New Level: %2',%client.name,%client.level);
  }
  else
  {
    messageClient(%client,'MsgClientXp', '\c2Xp Gained: %2.:.Total Xp: %3.:.Next Lvl Up: %1.', %levelup++,%amount,%client.experience);
  }
}

function getNextLevelUP(%client)
{
  switch(%client.level)
  {
  case 1:
    %nextLevelUp = 99;
  case 2:
    %nextLevelUp = 399;
  case 3:
    %nextLevelUp = 999;
  case 4:
    %nextLevelUp = 1999;
  case 5:
    %nextLevelUp = 3299;
  case 6:
    %nextLevelUp = 4999;
  case 7:
    %nextLevelUp = 7099;
  case 8:
    %nextLevelUp = 8999;
  case 9:
    %nextLevelUp = 11099;
  case 10:
    %nextLevelUp = 14100;
  case 11:
    %nextLevelUp = 17499;
  }
  return %nextLevelUp;
}

function RecalcServerKillStat(%client)
{
  %client.TotalServerKills++;
  saveStats(%client);
}

function RecalcServerDeathStat(%client)
{
  %client.TotalServerDeaths++;
  saveStats(%client);
}
#4
08/30/2006 (4:32 pm)
Lol lab,
does that code have the fix's he needs? or just the formatting I requested?

sometimes your are such a smart ass :)
#5
08/30/2006 (4:35 pm)
It includes changes that should fix his code...

Including changing his If-Else-If chain into a Switch-Case with one return.
#6
08/30/2006 (4:39 pm)
Good work soldier.

David:
Lab here is king of this script stuff.

be sure to thank him for his gracious help.
#7
08/30/2006 (6:21 pm)
Thx lab =) u cleared up most of my problems.

but it still erases my stats for no reason sometimes...

just say i got on a random misson file(not multiplayer) and killed myself 3 times, then left to the main menu, then came back to the game, i would still have 3 deaths for my stats and everything would be okay. but if i would of exit the whole game then reload the game, it would delete my stats, and i would have 0 deaths again.

maybe im not puting the checkStats(); or saveStats(); in the right place? idk >.>
#8
08/30/2006 (10:36 pm)
David:
I've been wrong before.

but it looks like if you call createStats it might toast your current file.

so you need to check if the file exists already.

unless this sucker is always in append mode?
No idea really.

Edit:
open the file in a text editor and see
#9
08/31/2006 (12:07 am)
%filename = "scripts/serverStats/user" @ %client.namebase @ ".cs";
if(!isFile(%filename))
{
createStats(%client);
}

doesnt the !isFile(%filename)) part check to see if the file exists or not?
#10
08/31/2006 (1:22 am)
Try this:

function checkStats(%client)
{
  %filename = "scripts/serverStats/user" @ %client.namebase @ ".cs"; 
  if(!isFile(%filename))
  {
    createStats(%client);
  }
  else
  {
    %file = new FileObject();
    if (%file.openForRead(%filename))
    {
      %client.TotalServerKills = %file.readLine();
      %client.TotalServerDeaths = %file.readLine();
      %client.Level = %file.readLine();
      %client.Experience = %file.readLine();
      %file.close();
      %client.SaveOK = 1;
    }
    %file.delete();
  }
}

function createStats(%client) 
{
  %client.TotalServerKills = 0;
  %client.TotalServerDeaths = 0;
  %client.Level = 1;
  %client.Experience = 0;
  %client.SaveOK = 1;
	saveStats(%client)
} 

function saveStats(%client) 
{ 
	if(%client.SaveOK == 1)
	{
	  %filename = "scripts/serverStats/user" @ %client.namebase @ ".cs"; 
	  %file = new FileObject()
	  if(%file.openForWrite(%filename)) 
	  { 
	    %file.writeLine(%client.TotalServerKills); 
	    %file.writeLine(%client.TotalServerDeaths); 
	    %file.writeLine(%client.Level); 
	    %file.writeLine(%client.Experience); 
	    %file.close();
	  } 
	  %file.delete();
  }
}

function moreXp(%client,%amount)
{
  %levelup = getnextLevelUp(%client);
  %client.experience += %amount;
  if (%client.experience > %levelup)
  {
    %client.level++;
    messageAll('MsgClientKilled','%1 Levels Up! New Level: %2',%client.name,%client.level);
  }
  else
  {
    messageClient(%client,'MsgClientXp', '\c2Xp Gained: %2.:.Total Xp: %3.:.Next Lvl Up: %1.', %levelup++,%amount,%client.experience);
  }
}

function getNextLevelUP(%client)
{
  switch(%client.level)
  {
  case 1:
    %nextLevelUp = 99;
  case 2:
    %nextLevelUp = 399;
  case 3:
    %nextLevelUp = 999;
  case 4:
    %nextLevelUp = 1999;
  case 5:
    %nextLevelUp = 3299;
  case 6:
    %nextLevelUp = 4999;
  case 7:
    %nextLevelUp = 7099;
  case 8:
    %nextLevelUp = 8999;
  case 9:
    %nextLevelUp = 11099;
  case 10:
    %nextLevelUp = 14100;
  case 11:
    %nextLevelUp = 17499;
  }
  return %nextLevelUp;
}

function RecalcServerKillStat(%client)
{
  %client.TotalServerKills++;
  saveStats(%client);
}

function RecalcServerDeathStat(%client)
{
  %client.TotalServerDeaths++;
  saveStats(%client);
}