SA-MP Forums Archive
How could I do a system to update player informations in Database ? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: How could I do a system to update player informations in Database ? (/showthread.php?tid=489905)



How could I do a system to update player informations in Database ? - anou1 - 24.01.2014

Hi everyone,


I want to know, how could I do a system to update player informations every X time.
I'm using a mysql database, with BlueG (+ maddinat0r) plugin.

Thank you.


Re : How could I do a system to update player informations in Database ? - anou1 - 24.01.2014

No ideas ?


Re: How could I do a system to update player informations in Database ? - Shetch - 24.01.2014

http://dev.mysql.com/doc/refman/5.0/en/update.html


Re: How could I do a system to update player informations in Database ? - ikey07 - 24.01.2014

you dont need to update every x time, you need to update when field value changes, like player money, save it only when when player get/lose money.

GivePlayerMoneyEx(playerid,amount)
{
mysql_query...
GivePlayerMoney(playerid,amount);
}


Re : How could I do a system to update player informations in Database ? - anou1 - 24.01.2014

@iKey07

But me I want to save it every X time. Cause if someone DDOS server and the server just crash. Informations will be as they were before the attack


Re: How could I do a system to update player informations in Database ? - Shetch - 24.01.2014

Quote:
Originally Posted by ikey07
Посмотреть сообщение
you dont need to update every x time, you need to update when field value changes, like player money, save it only when when player get/lose money.

GivePlayerMoneyEx(playerid,amount)
{
mysql_query...
GivePlayerMoney(playerid,amount);
}
This is definetly a good idea.
Or you can updeate the player's data only when he disconnects.

EDIT: In that case just set up a timer under OnGameModeInit.
Be sure to use threaded queries, and don't use them too often, or you will experience lag with large ammount of players online.


Re : How could I do a system to update player informations in Database ? - anou1 - 24.01.2014

Okay, thank you !

How can I do to repeat a timer every X time please ?

Thank you


Re: How could I do a system to update player informations in Database ? - Shetch - 24.01.2014

https://sampwiki.blast.hk/wiki/SetTimer

Код:
SetTimer("SaveData", 10000, true); // Will repeat every 10 seconds. 10000ms

forward SaveData();
public SaveData()
{
    // Save your data here
}



Re: How could I do a system to update player informations in Database ? - ikey07 - 24.01.2014

mysql and timers is bad combination, you will overheat mysql server on your host, also when someone ddos your data will be saved eventually.


Re : Re: How could I do a system to update player informations in Database ? - anou1 - 24.01.2014

Quote:
Originally Posted by Shetch
Посмотреть сообщение
This is definetly a good idea.
Or you can updeate the player's data only when he disconnects.

EDIT: In that case just set up a timer under OnGameModeInit.
Be sure to use threaded queries, and don't use them too often, or you will experience lag with large ammount of players online.

Thank you, I did this:

Код:
public SauvegardeJoueurs()
{
	for(new i = 0; i < MAX_PLAYERS; i++)
	{
		if(IsPlayerConnected(i))
		{
			SauvegardeJoueur(i);
		}
	}
}

public SauvegardeJoueur(playerid)
{
	if(IsPlayerConnected(playerid))
	{
		new query[1024];
		mysql_format(mysql, query, sizeof(query),"SELECT * FROM `joueurs` WHERE `Username` = '%e' LIMIT 1", Name[playerid]);
		new Float:pos[3]; 
		GetPlayerPos(playerid, pos[0], pos[1], pos[2]); 
		mysql_format(mysql, query, sizeof(query), "UPDATE `joueurs` SET `DerniereIP`='%s', `Admin`=%d, `VIP`=%d, `Argent`=%d, `posX`=%f, `posY`=%f, `posZ`=%f, `Interieur`=%i, `World`=%i, `Skin`=%d, `Niveau`=%d WHERE `ID`=%d",\
		IP[playerid], pInfo[playerid][Admin], pInfo[playerid][VIP], pInfo[playerid][Argent], pos[0], pos[1], pos[2], GetPlayerInterior(playerid), GetPlayerVirtualWorld(playerid), pInfo[playerid][Skin], pInfo[playerid][Niveau], pInfo[playerid][ID]);
		mysql_tquery(mysql, query, "", "");
		new Float:vie[1], Float:armure[1];
		GetPlayerHealth(playerid, vie[0]);
		GetPlayerArmour(playerid, armure[0]);
		mysql_format(mysql, query, sizeof(query), "UPDATE `joueurs` SET `Vie`=%f, `Armure`=%f WHERE `ID`=%d", vie[0], armure[0], pInfo[playerid][ID]);
		mysql_tquery(mysql, query, "", "");
	}
}
Is this correct ?
I put a timer every 5 minutes, is it too much and will cause lags or is that good ?
Can I do it every minute or it's useless ?


Are my queries good ? Or it isn't threaded queries ?