[HELP]Weapons not saving when player crashes. -
Lilcuete - 10.07.2010
Hello i am usings snoobs weapon system FS and it works perfectly but when the server crashes it doesn't save the weapons he had only when a player disconnects and connect to the server.Please someone help me.
Код:
#include <a_samp>
/*
*Continuous Life by SnooB
=========================
This FilterScript will save all the information on a player when he
disconnect from the server. When the player connect back to the server
the player will be asked to spawn using the same class as he was
using last time he disconnect.
* THE DEFAULT FOLDER NAME IS "clfs"
AND YOU MUST CREAT IT IN THE scriptfiles FOLDER OF YOUR SERVER.
IT SHOULD LOOK LIKE /YourServerFolder/scriptfiles/clfs
You can change this folder in: #define SAVING_FOLDER "folder_name"
The scrip also save the data when the player change state.
It save on new state ON FOOT, DRIVER or PASSENGER.
this can be usefull in case of server crash to restore the player
in a more acurate place. This can be bypass by commenting
the line: #define SAVE_ON_STATE_CHANGE
email/msn => snoob@studio2015.ca
*/
#define SAVING_FOLDER "clfs"// YOU MUST CREATE A FOLDER "foldername" IN SCRIPTFILES OF YOUR SERVER
#define SAVE_ON_STATE_CHANGE //comment this line to turn off the save data on state change
#define COLOR_WHITE 0xFFFFFFAA//color white
forward refresh_player_data(playerid,write);
forward reload_player_data(playerid);
forward aply_player_data(playerid);
enum PLAYER_DATA//data array we will use to store our data
{
p_class, Float:p_pos[4], p_virworld, p_int, Float:p_hp[2], p_color, p_money,
p_score, p_skin, p_specact, p_team, p_wantlvl, p_wep[13], p_ammo[13], p_incar,
v_id, v_model, Float:v_pos[4], Float:v_healt
};
new LastSelectedClass[MAX_PLAYERS];// keep track of the last selected class
new all_data[MAX_PLAYERS][PLAYER_DATA];// we store all the player info in this.
new RestoredLife[MAX_PLAYERS];// to keep track of data aplication on spawn
new Text:TxtInfo[MAX_PLAYERS];// text for player connection and data load
public OnFilterScriptInit()
{
print("\n========================================================================");
print("== *Continuous Life by SnooB loaded.\r\n==");
printf("== MAKE SURE YOU HAVE THE FOLDER \"%s\" IN YOUR SCRIPTFILES FOLDER",SAVING_FOLDER);
print("== if this folder is not present YOUR SERVER MAY CRASH");
print("========================================================================\n");
//when we load the script we need all the currently connected player to re-select a class
SendClientMessageToAll(COLOR_WHITE,"Continuous Life loaded, you must start a new life.");
for (new i = 0; i < MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i))
{
OnPlayerConnect(i);
ForceClassSelection(i);
SetPlayerHealth(i,0.0);
}
}
return 1;
}
public OnFilterScriptExit()
{
for (new i = 0; i < MAX_PLAYERS; i++)//loop all the player
{
if(RestoredLife[i])// if we have loaded data for that player
{
TextDrawDestroy(TxtInfo[i]);// we destroy our text
}
}
return 1;
}
public OnPlayerRequestClass(playerid, classid)
{
if(RestoredLife[playerid])// if we have loaded data for that player
{
new tmp_string[128];//temp string
if(classid == all_data[playerid][p_class])//if the player select the same class as the one stored in the data
{
CallRemoteFunction("OnPlayerRequestSpawn","i",playerid);//we call the function for all the other loaded script for compatibilty reason
SpawnPlayer(playerid);//we spawn the player
}
else
{
format(tmp_string,sizeof(tmp_string),"~r~Select ~w~class %i ~r~to restore your life~n~~y~ Or type ~w~/newlife~y~ to start a new life.",
all_data[playerid][p_class]);//formating our text
TextDrawSetString(TxtInfo[playerid], tmp_string);// set the text string
}
}
LastSelectedClass[playerid] = classid;//store the current selected class
return 1;
}
public OnPlayerRequestSpawn(playerid)
{
if(RestoredLife[playerid])// if we have loaded data for that player
{
if(LastSelectedClass[playerid] != all_data[playerid][p_class])//if the player is not on the same class as the saved data
{
new tmp_string[128];//temp string
format(tmp_string,sizeof(tmp_string),"~r~Select ~w~class %i ~r~to restore your life~n~~y~ Or type ~w~/newlife~y~ to start a new life.",
all_data[playerid][p_class]);//formating our text
TextDrawSetString(TxtInfo[playerid], tmp_string);//set the text string
return 0;
}
}
return 1;
}
public OnPlayerConnect(playerid)
{
if(reload_player_data(playerid))
{
RestoredLife[playerid] = 1;//put the data loaded tag on that player.
}
else RestoredLife[playerid] = 0;//there is no data to load
return 1;
}
public OnPlayerDisconnect(playerid, reason)
{
if(RestoredLife[playerid]) TextDrawDestroy(TxtInfo[playerid]);//if the player was still at spawn screen we destroy the text
if(1 <= GetPlayerState(playerid) <= 3) refresh_player_data(playerid,1);//if the player is on foot, driver or in a car we save his current status
return 1;
}
public OnPlayerSpawn(playerid)
{
if(RestoredLife[playerid])// if we have loaded data for that player
{
TogglePlayerControllable(playerid,false);//toggle the player incontrollable
SetTimerEx("aply_player_data",2000,false,"i",playerid);
}
else refresh_player_data(playerid,1);
return 1;
}
public OnPlayerCommandText(playerid, cmdtext[])
{
if (strcmp("/newlife", cmdtext, true, 8) == 0)
{
if(RestoredLife[playerid])// if we have loaded data for that player
{
RestoredLife[playerid] = 0;// remove the load life tag
TextDrawDestroy(TxtInfo[playerid]);// destroy the text
GameTextForPlayer(playerid,"~r~Ready to start a new life.",4000,4);
}
return 1;
}
return 0;
}
public OnPlayerStateChange(playerid,newstate,oldstate)
{
#if defined SAVE_ON_STATE_CHANGE //if set to save data on state change
if( (1 <= newstate <= 3) && (!RestoredLife[playerid]) ) refresh_player_data(playerid,1);//if the player is on foot, driver or in a car we save his data
#endif
return 1;
}
//==============================================================================
//CUSTOM FUNCTION
public reload_player_data(playerid)
{
if(!fexist(get_file_path(playerid))) return 0;//if we dont have data on that player we return 0;
new File:CLFS_read = fopen(get_file_path(playerid),io_read);// onpen the file for reading
new tmp_string[64];// temp string
new wep_slot;//
for (new line = 1; line < 2; line++)// looping our 50 line of data
{
fread(CLFS_read,tmp_string);//reading the line
switch(line)//using a switch we assing each line to the corect variable
{
case 1:
{
//weapon and ammo
all_data[playerid][p_wep][wep_slot] = strval(tmp_string);
line++;//next line
fread(CLFS_read,tmp_string);//reading the ammo line
all_data[playerid][p_ammo][wep_slot] = strval(tmp_string);
wep_slot++;//next wep slot
}
}
}// all data are now loaded
fclose(CLFS_read);//we close the file
return 1;
}
public aply_player_data(playerid)
{
//this Function is use to aply the data we load for a player
ResetPlayerWeapons(playerid);//reseting the player weapon before we gave back his weapon
for (new i = 0; i < 13; i++)//looping all the weapon slot
{
GivePlayerWeapon(playerid, all_data[playerid][p_wep][i], all_data[playerid][p_ammo][i]);
}
return 1;
}
public refresh_player_data(playerid,write)
{
for (new i = 0; i < 13; i++)//loading weapon data
{
GetPlayerWeaponData(playerid, i, all_data[playerid][p_wep][i], all_data[playerid][p_ammo][i]);
}
//all our data are stored
if(write) write_player_data(playerid);//if requested we write the data to the file
return 1;
}
write_player_data(playerid)
{
new File:CLFS_write = fopen(get_file_path(playerid),io_write);//openin our file for writing
new tmp_string[128];// temp string
//formating second block of data ro write
fwrite(CLFS_write,tmp_string);//writing the string to file
for (new i = 0; i < 13; i++)//looping all the weapon slot
{
format(tmp_string,sizeof(tmp_string),"%i\r\n%i\r\n",
all_data[playerid][p_wep][i], all_data[playerid][p_ammo][i] );
//formating weapon slot "i"
fwrite(CLFS_write,tmp_string);//writing the string to the file
}
// all weapon data are writen to our file
//formating last block of info
fwrite(CLFS_write,tmp_string);//writing the string to the file
fclose(CLFS_write);//closing the file
return 1;
}
get_file_path(playerid)
{
//this is use to get a file path
new p_nameSTR[MAX_PLAYER_NAME];//var for the player name
GetPlayerName(playerid,p_nameSTR,sizeof(p_nameSTR));//get the player name
new File_Path[64];//string for our file path
format(File_Path,sizeof(File_Path),"%s/%s",SAVING_FOLDER,p_nameSTR);//formating the file name
return File_Path;
}
//EOF Continuous Life by SnooB email/msn => snoob@studio2015.ca
Re: [HELP]Weapons not saving when player crashes. -
Mauzen - 10.07.2010
I dont really know this FS, but in general, a Servercrash is unforseeable, so there cant be a callback for this.
So, you could add an timer, that saves the data for example every 60 or 120 seconds, that should be often enough.
I just looked over the script. I think, adding this should be enough:
pawn Код:
in global variables:
new savetimer[MAX_PLAYERS];
in OnPlayerConnect:
savetimer[playerid] = SetTimerEx("refresh_player_data", 120000 (interval in ms), 1, "ii", playerid, 1);
in onPlayerDisconnect:
KillTimer(savetimer[playerid]);
Re: [HELP]Weapons not saving when player crashes. -
Lilcuete - 10.07.2010
Well its still not saving when server crashes or i close the console and reopen it
Re: [HELP]Weapons not saving when player crashes. -
Mauzen - 10.07.2010
For this you could call refresh_player_data in OnFilterScriptExit, so it is called when you exit the fiterscript/close the server the normal way.
You can use the exitsing for(...) to call it for every single player:
refresh_player_data(i,1)
Re: [HELP]Weapons not saving when player crashes. -
Lilcuete - 10.07.2010
Damn didn't work.
Re: [HELP]Weapons not saving when player crashes. -
Mauzen - 10.07.2010
Hmm, you did it this way?
pawn Код:
for (new i = 0; i < MAX_PLAYERS; i++)//loop all the player
{
refresh_player_data(i,1);
if(RestoredLife[i])// if we have loaded data for that player
{
TextDrawDestroy(TxtInfo[i]);// we destroy our text
}
}