SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Discussion

Reply
 
Thread Tools Display Modes
Old 21/10/2018, 02:28 PM   #1
GospodinX
Big Clucker
 
Join Date: Nov 2017
Posts: 143
Reputation: 20
Default Saving players stats on changes or in timer? [MySQL]

Hi guys,

I want to start an discussion about saving player stats.What you think,is better to save player stats immediately(When there is any change in player stats) or in timer(For example every 10 minutes to save all player stats).

1) Save accounts in timer
Code:
UPDATE `users` SET `column1` = '%i',`column2` = '%i', `column3` = '%i', `column4` = '%i',`column5` = '%i' WHERE `ID`='%i'
2) Save column on which we have changes..
Code:
UPDATE `users` SET `column1` = '%i' WHERE `ID`='%i'

UPDATE `users` SET `column2` = '%i' WHERE `ID`='%i'

UPDATE `users` SET `column3` = '%i' WHERE `ID`='%i'

UPDATE `users` SET `column4` = '%i' WHERE `ID`='%i'

UPDATE `users` SET `column5` = '%i' WHERE `ID`='%i'
Now I'm use both option :/ .Half of player stats I save in timer(for example money which changes more often) and half I save on changes(Which changes rarely;for example Level which changes very rarely).
I have Roleplay mode.

What you use? (Sorry for my bad english)
GospodinX is offline   Reply With Quote
Old 22/10/2018, 03:04 PM   #2
KinderClans
Banned
 
Join Date: May 2018
Location: Italy
Posts: 720
Reputation: 43
Default Re: Saving players stats on changes or in timer? [MySQL]

I use a timer which saves players stats every 10 minutes with the UPDATE query. I prefer to do in this way.

I don't care if something changes every second or very rarely, you can never know when server/player will crash (if he will). Also i don't see any increase of memory usage while doing this, so, it's ok for me.
KinderClans is offline   Reply With Quote
Old 22/10/2018, 04:15 PM   #3
Dignity
High-roller
 
Dignity's Avatar
 
Join Date: May 2013
Location: Belgium
Posts: 1,625
Reputation: 1163
Default Re: Saving players stats on changes or in timer? [MySQL]

Using a timer is inefficient. Imagine a timer running every 10 minutes on a 100 pb server.

Save when you change something in the script.
Dignity is offline   Reply With Quote
Old 22/10/2018, 04:17 PM   #4
DaniceMcHarley
High-roller
 
DaniceMcHarley's Avatar
 
Join Date: Feb 2012
Posts: 1,744
Reputation: 95
Default Re: Saving players stats on changes or in timer? [MySQL]

Save spontaneously.
DaniceMcHarley is offline   Reply With Quote
Old 30/10/2018, 06:07 PM   #5
Garr
Big Clucker
 
Join Date: Feb 2014
Posts: 51
Reputation: 29
Default Re: Saving players stats on changes or in timer? [MySQL]

Quote:
Originally Posted by Dignity View Post
Using a timer is inefficient. Imagine a timer running every 10 minutes on a 100 pb server.

Save when you change something in the script.
it would be better to have per player timers instead of looping through everyone connected. timers are not resource hogging features lol they work well and you can have a lot of them. heck, thousands of timers doesn't have much of a performance hit.

Last edited by Garr; 30/10/2018 at 06:56 PM.
Garr is offline   Reply With Quote
Old 31/10/2018, 12:10 AM   #6
BlackBank
Gangsta
 
Join Date: Dec 2010
Location: The Netherlands
Posts: 525
Reputation: 437
Default Re: Saving players stats on changes or in timer? [MySQL]

The best way is to just save the results immediately when you change the value of any stats (money, weapons, etc) via a threaded query that doesn't cache any results of the query, so the script doesn't have to 'wait' to finish the query function or even 'watch' it through a callback.

Quote:
Originally Posted by Dignity View Post
Using a timer is inefficient. Imagine a timer running every 10 minutes on a 100 pb server.

Save when you change something in the script.
If you are doing it the right it way, using threaded queries without caching the results, it doesn't matter that you sync 100 players every 10mins with just one loop.

Quote:
Originally Posted by Garr View Post
it would be better to have per player timers instead of looping through everyone connected. timers are not resource hogging features lol they work well and you can have a lot of them. heck, thousands of timers doesn't have much of a performance hit.
It doesn't matter if you loop through them when you use threaded queries without caching the results, as i also stated above.
BlackBank is offline   Reply With Quote
Old 31/10/2018, 12:13 AM   #7
TheToretto
Gangsta
 
TheToretto's Avatar
 
Join Date: Jul 2015
Posts: 600
Reputation: 66
Default Re: Saving players stats on changes or in timer? [MySQL]

Why don't you save the data once OnPlayerDisconnect is called? I don't get the point of all this..
__________________






Toretto#9902

Rest in peace grandpa, 03 November 2018
TheToretto is offline   Reply With Quote
Old 31/10/2018, 12:18 AM   #8
BlackBank
Gangsta
 
Join Date: Dec 2010
Location: The Netherlands
Posts: 525
Reputation: 437
Default Re: Saving players stats on changes or in timer? [MySQL]

Quote:
Originally Posted by TheToretto View Post
Why don't you save the data once OnPlayerDisconnect is called? I don't get the point of all this..
Well, it would be horrible if you lose money or any other stats when the server crash... With this method the change of losing data is minimized. Also stats are more up-to-date on your stats pages or your User Control Panel, if you have one of those running.
BlackBank is offline   Reply With Quote
Old 31/10/2018, 12:35 AM   #9
TheToretto
Gangsta
 
TheToretto's Avatar
 
Join Date: Jul 2015
Posts: 600
Reputation: 66
Default Re: Saving players stats on changes or in timer? [MySQL]

Just use this..

pawn Code:
public OnGameModeExit()
{
   for(new i; i < MAX_PLAYERS; i++)
        OnPlayerDisconnect(i, 1);
    return 1;
}
__________________






Toretto#9902

Rest in peace grandpa, 03 November 2018
TheToretto is offline   Reply With Quote
Old 31/10/2018, 12:39 AM   #10
BlackBank
Gangsta
 
Join Date: Dec 2010
Location: The Netherlands
Posts: 525
Reputation: 437
Default Re: Saving players stats on changes or in timer? [MySQL]

Quote:
Originally Posted by TheToretto View Post
Just use this..

pawn Code:
public OnGameModeExit()
{
   for(new i; i < MAX_PLAYERS; i++)
        OnPlayerDisconnect(i, 1);
    return 1;
}
That won't work when the server crash or you force close the server, OnGameModeExit won't be called.

Also described on the wiki of OnGameModeExit (https://wiki.sa-mp.com/wiki/OnGameModeExit)
Quote:
This callback will NOT be called if the server crashes or the process is killed by other means, such as using the Linux kill command or pressing the close-button on the Windows console.
BlackBank is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[MySQL] All players saving the same stats. ounce Scripting Help 7 30/07/2015 05:54 PM
Saving players' stats in OnGameModeExit. Denying Scripting Help 6 24/05/2014 01:26 PM
Timer Help, saving stats LiamM Scripting Help 5 01/11/2011 06:43 PM
saving players stats on server restart. Face9000 Help Archive 8 13/12/2010 05:04 PM
saving all player stats timer. iggy1 Help Archive 2 24/11/2010 04:32 AM


All times are GMT. The time now is 09:01 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.