Save time game in MySQL
#1

Hi,
I have a problem. Create this code in callback OnPlayerDisconnect.

Код:
 new czas_gry_ogol[128];

	new  Hour, Minute, Second;
	gettime( Hour, Minute, Second);
	format(czas_gry_ogol,sizeof czas_gry_ogol,"UPDATE `Gracze` SET `Czas_gry_ogol` = `Czas_gry_ogol` + '%02d:%02d:%02d'  WHERE `Nick` = '%s'",  Hour, Minute, Second, PlayerName(playerid));
	mysql_query(czas_gry_ogol);
If a player plays 2 sec to the database is recorded as 30 sec.
How to save time game player?
Please help.
Reply
#2

connect

StartTime[playerid] = gettime();

disonnect
pawn Код:
format(czas_gry_ogol,sizeof czas_gry_ogol,"UPDATE `Gracze` SET `Czas_gry_ogol` = `Czas_gry_ogol` + %d WHERE `Nick` = '%s'",  (gettime()-StartTime[playerid]), PlayerName(playerid));
and Czas_gry_ogol must be MEDIUMINT( 8 ) UNSIGNED NOT NULL or INT(10)
Reply
#3

It does not work. Set the time for 30.
Reply
#4

Show table structure, connect save and disconnect save
Reply
#5

I count time in OnPlayerSpawn.

OnPlayerSpawn:

Код:
SetPVarInt(playerid, "GraszDzis", GetTickCount());
OnPlayerDisconnect:

Код:
    new czas_gry_ogol[128];

	new  Hour, Minute, Second;
	gettime( Hour, Minute, Second);


	ConvertMS(GetTickCount()-GetPVarInt(playerid, "GraszDzis"), Hour, Minute, Second);
	printf("%02d:%02d:%02d", Hour, Minute, Second);
	format(czas_gry_ogol,sizeof czas_gry_ogol,"UPDATE `Gracze` SET `Czas_gry_ogol` = `Czas_gry_ogol` + '%02d:%02d:%02d'  WHERE `Nick` = '%s'",  Hour, Minute, Second,  PlayerName(playerid));
	mysql_query(czas_gry_ogol);
Structure table:


Код:
CREATE TABLE  `Gracze` (
 `Id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
 `Nick` VARCHAR( 24 ) NOT NULL ,
 `Haslo` VARCHAR( 32 ) NOT NULL ,
 `Data_urodzin` DATE NOT NULL COMMENT  'W urodziny dostaje się prezent',
 `Data_rejestracji` DATETIME NOT NULL ,
 `Punkty` INT( 11 ) NOT NULL DEFAULT  '0',
 `Lvl` INT( 11 ) UNSIGNED NOT NULL ,
 `Exp` INT( 11 ) UNSIGNED NOT NULL ,
 `Zabojstwa` INT( 11 ) UNSIGNED NOT NULL DEFAULT  '0',
 `Smierci` INT( 11 ) NOT NULL DEFAULT  '0',
 `Wizyty` INT( 11 ) UNSIGNED NOT NULL ,
 `Vip` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT  '0',
 `Pozycja` DECIMAL( 65, 0 ) NOT NULL COMMENT  'Zapisana pozycja po wyjściu',
 `Czas_gry_dzis` TIME NOT NULL COMMENT  'Łączny czas gry dziś',
 `Czas_gry_ogol` TIME NOT NULL COMMENT  'Łączny czas gry w ogуle',
PRIMARY KEY (  `Id` ,  `Nick` ) ,
FULLTEXT KEY  `HASLO` (  `Haslo` )
) ENGINE = MYISAM DEFAULT CHARSET = latin1;
Reply
#6

pawn Код:
format(czas_gry_ogol,sizeof czas_gry_ogol,"UPDATE `Gracze` SET `Czas_gry_ogol` = '%02d:%02d:%02d'  WHERE `Nick` = '%s'",  Hour, Minute, Second,  PlayerName(playerid));
Try it like this maybe ?
Reply
#7

Ok this code good work, but if player exit game and again login, time count anew. Yours code work once.
Reply
#8

Connect
pawn Код:
SetPVarInt(playerid, "GraszDzis", gettime());
Disconnect
pawn Код:
new str[128];
format(str,sizeof str,"UPDATE `Gracze` SET `Czas_gry_ogol` = `Czas_gry_ogol` + %d WHERE `Nick` = '%s'", (gettime()-GetPVarInt(playerid, "GraszDzis")), PlayerName(playerid));
mysql_query(str);
replace
pawn Код:
`Czas_gry_ogol` TIME NOT NULL COMMENT  'Łączny czas gry w ogуle',
to

pawn Код:
`Czas_gry_ogol` INT(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT 'Łączny czas gry w ogуle',
then if you want to see how many hours you play use

http://www.w3resource.com/mysql/date...e-function.php

or some samp function to converting seconds
Reply
#9

Your code working good, but if player dead time reset itself. How repair this problem?
Reply
#10

In this case I think you should make a variable under OnPlayerConnect to store the existing time and then,when he disconnects add the new time to the old one and then insert it into the database. Here is an example :
pawn Код:
// at the top of your gamemode
new playedtime[MAX_PLAYERS];
// then
public OnPlayerConnect
{
    new czas_gry_ogol[128];
    playedtime[playerid] = format(czas_gry_ogol,sizeof czas_gry_ogol,"SELECT `Czas_gry_ogol` FROM `Gracze` WHERE Nick` = '%s'", PlayerName(playerid)); // now it stored the last updated played hours
   mysql_query(czas_gry_ogol);
}

public OnPlayerDisconnect
{
        new czas_gry_ogol[128];

    new  Hour, Minute, Second;
    gettime( Hour, Minute, Second);


    ConvertMS(GetTickCount()-GetPVarInt(playerid, "GraszDzis"), Hour, Minute, Second);
    printf("%02d:%02d:%02d", Hour, Minute, Second);
        new update_playedtime = playedtime[playerid] + "Hour, Minute, Second" // or something like this,im not sure
    format(czas_gry_ogol,sizeof czas_gry_ogol,"UPDATE `Gracze` SET `Czas_gry_ogol` = '%02d:%02d:%02d'  WHERE `Nick` = '%s'",  update_playedtime,  PlayerName(playerid));
    mysql_query(czas_gry_ogol);
}
This is just an example of how you might be able to do it.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)