Mysql saving & loading problems + timing
#2

I had fixxed your gamemode and even make some little comments



PHP код:
#include <a_samp>
#include <sscanf2>
#include <a_mysql>
#include <zcmd>
#include <foreach>
#define                 COLOR_WHITE                             0xFFFFFFAA
#define                 THREAD_LOE_ANDMED
#define                 MAX_TIMERS                              (5)
#define                 INTERIOR_WEATHER_ID                     (1)
#define                 MAX_WEATHER_POINTS                      (9)
#define ColorClassTruckDriver       0xFF8000FF // Orange
#define ColorClassBusDriver         0x80FFFFFF // Light blue
#define ColorClassPilot             0x008080FF // Dark blue
#define ColorClassPolice            0x0000FFFF // Blue
#define ColorClassCourier           0xFF0080FF // Pink
#define ColorClassAssistance        0x80FF00FF // Dark green
#define ColorClassRoadWorker        0xFFFF80FF // Light yellow
main(){}
enum playerEnum {
    
pPassword,
    
pRaha,
    
pSkoor,
    
pLogged,
    
ppiloot,
    
ppolitsei,
    
pConnectedSeconds,
    
pPlayingHours,
    
pSeconds,
    
pClass,
}
new 
andmed[MAX_PLAYERS][playerEnum];
new 
szMessage[128];
new 
scriptTimers[MAX_TIMERS];
new 
weatherVariables[2];
new 
validWeatherIDs[17] = { 12345679101112131415171820 };
new 
gTime[3];
forward                 globalPlayerLoop();
@
Get_Player_Time_On_Server(playerid);
public 
OnGameModeInit()
{
    
mysql_debug(true);
    
mysql_connect("localhost""root""test""");
    
scriptTimers[0] = SetTimer("globalPlayerLoop"1000true);
    
weatherVariables[0] = validWeatherIDs[random(sizeof(validWeatherIDs))];
    
SetWeather(weatherVariables[0]);
    
SetGameModeText("Blank Script");
    
AddPlayerClass(01958.37831343.157215.3746269.1425000000);
    return 
1;
}
public 
OnGameModeExit()
{
    new
        
x;
    while(
MAX_TIMERS) {
        
KillTimer(scriptTimers[x]);
        
x++;
    }
    return 
1;
}
public 
OnPlayerRequestClass(playeridclassid)
{
    return 
1;
}
public 
OnPlayerConnect(playerid)
{
    
TogglePlayerSpectating(playeridtrue);
    
SetPlayerColor(playeridCOLOR_WHITE);
    
nulliandmed(playerid);
    new 
logi[126], Name[MAX_PLAYER_NAME];
    
GetPlayerName(playeridNamesizeof(Name));
    
mysql_real_escape_string(NameName);
    
format(logisizeof(logi), "SELECT 1 FROM kasutajad WHERE nimi = '%s' LIMIT 1"Name);
    
mysql_query(logi);
    
mysql_store_result();
    if(
mysql_num_rows() != 0)
    {
        
SendClientMessage(playerid, -1"See kasutaja on juba registreeritud");
        
ShowPlayerDialog(playerid0DIALOG_STYLE_INPUT"Logimine""Palun logi sisse kasutades enda parooli""Logi""Keeldu");
    }
    else
    {
        
ShowPlayerDialog(playerid1DIALOG_STYLE_INPUT"Registreeri""Palun sisesta parool enda uuele kasutajale""Registreeri""Keeldu");
    }
    return 
1;
}
public 
OnPlayerDisconnect(playeridreason)
{
    if(
andmed[playerid][pLogged] == 1) {
        
salvestaandmed(playerid);
        
andmed[playerid][pLogged] = -1;
    }
    return 
1;
}
public 
OnPlayerSpawn(playerid)
{
    return 
1;
}
public 
OnPlayerDeath(playeridkilleridreason)
{
    return 
1;
}
public 
OnVehicleSpawn(vehicleid)
{
    return 
1;
}
public 
OnVehicleDeath(vehicleidkillerid)
{
    return 
1;
}
public 
OnPlayerText(playeridtext[])
{
    return 
1;
}
public 
OnPlayerCommandText(playeridcmdtext[])
{
    if (
strcmp("/mycommand"cmdtexttrue10) == 0)
    {
        
// Do something here
        
return 1;
    }
    return 
0;
}
public 
OnPlayerEnterVehicle(playeridvehicleidispassenger)
{
    return 
1;
}
public 
OnPlayerExitVehicle(playeridvehicleid)
{
    return 
1;
}
public 
OnPlayerStateChange(playeridnewstateoldstate)
{
    return 
1;
}
public 
OnPlayerEnterCheckpoint(playerid)
{
    return 
1;
}
public 
OnPlayerLeaveCheckpoint(playerid)
{
    return 
1;
}
public 
OnPlayerEnterRaceCheckpoint(playerid)
{
    return 
1;
}
public 
OnPlayerLeaveRaceCheckpoint(playerid)
{
    return 
1;
}
public 
OnRconCommand(cmd[])
{
    return 
1;
}
public 
OnPlayerRequestSpawn(playerid)
{
    return 
1;
}
public 
OnObjectMoved(objectid)
{
    return 
1;
}
public 
OnPlayerObjectMoved(playeridobjectid)
{
    return 
1;
}
public 
OnPlayerPickUpPickup(playeridpickupid)
{
    return 
1;
}
public 
OnVehicleMod(playeridvehicleidcomponentid)
{
    return 
1;
}
public 
OnVehiclePaintjob(playeridvehicleidpaintjobid)
{
    return 
1;
}
public 
OnVehicleRespray(playeridvehicleidcolor1color2)
{
    return 
1;
}
public 
OnPlayerSelectedMenuRow(playeridrow)
{
    return 
1;
}
public 
OnPlayerExitedMenu(playerid)
{
    return 
1;
}
public 
OnPlayerInteriorChange(playeridnewinterioridoldinteriorid)
{
    return 
1;
}
public 
OnPlayerKeyStateChange(playeridnewkeysoldkeys)
{
    return 
1;
}
public 
OnRconLoginAttempt(ip[], password[], success)
{
    return 
1;
}
public 
OnPlayerUpdate(playerid)
{
    return 
1;
}
public 
OnPlayerStreamIn(playeridforplayerid)
{
    return 
1;
}
public 
OnPlayerStreamOut(playeridforplayerid)
{
    return 
1;
}
public 
OnVehicleStreamIn(vehicleidforplayerid)
{
    return 
1;
}
public 
OnVehicleStreamOut(vehicleidforplayerid)
{
    return 
1;
}
public 
OnDialogResponse(playeriddialogidresponselistiteminputtext[])
{
    switch(
dialogid)
    {
        case 
0:
        {
            if(
response)
            {
                new 
query[150], pName[MAX_PLAYER_NAME];
                
GetPlayerName(playeridpNamesizeof(pName));
                
mysql_real_escape_string(pNamepName);
                
format(querysizeof(query), "SELECT * FROM kasutajad WHERE nimi = '%s' AND parool = '%s' LIMIT 1"pNameinputtext);
                
mysql_query(query);
                
                
mysql_store_result();
                if(
mysql_num_rows() == 1)
                {
                    
SendClientMessage(playerid, -1"Sa oled edukalt sisse logitud!");
                    
TogglePlayerSpectating(playeridfalse);
                    
andmed[playerid][pLogged] = 1;
                    
laeandmed(playerid);
                    
ShowPlayerDialog(playerid2DIALOG_STYLE_LIST"Amet""Pitsapoiss\nKuller\nBussijuht\nRekkamees\nPiloot\nPolitseinik""Vali""");
                }
                else
                {
                    
SendClientMessage(playerid, -1"Sa sisestasid vale parooli!");
                    
ShowPlayerDialog(playerid0DIALOG_STYLE_INPUT"Logimine""Palun logi sisse kasutades enda parooli""Logi""Keeldu");
                }
            }
            else
            {
                
Kick(playerid);
            }
        }
        case 
1:
        {
            if(
response)
            {
                new 
regi[256], pName[MAX_PLAYER_NAME];
                
GetPlayerName(playeridpNamesizeof(pName));
                
format(regisizeof(regi), "INSERT INTO kasutajad (nimi, parool, registreerus) VALUES('%s', '%s', NOW())"pNameinputtext);
                
mysql_query(regi);
                
mysql_store_result();
                
SendClientMessage(playerid, -1"Sinu kasutaja on nььd registreeritud!");
                
mysql_free_result();
                
ShowPlayerDialog(playerid0DIALOG_STYLE_INPUT"Logimine""Palun logi sisse kasutades enda parooli""Logi""Keeldu");
            }
            else
            {
                
Kick(playerid);
            }
        }
        case 
2:
        {
            switch(
listitem)
            {
                case 
0:
                {
                    
SendClientMessage(playerid, -1"Asusid tццle pitsapoisina!");
                    
andmed[playerid][pClass] = 1;
                    
SetPlayerColor(playeridColorClassRoadWorker);
                    
SetSpawnInfo(playerid1293192.3013, -218.88361.5781321.2913000000);
                    
SpawnPlayer(playerid);
                }
                case 
1:
                {
                    
SendClientMessage(playerid, -1"Asusid tццle kullerina!");
                    
andmed[playerid][pClass] = 2;
                    
SetPlayerColor(playeridColorClassCourier);
                    
SetSpawnInfo(playerid12941231000000);
                    
SpawnPlayer(playerid);
                }
                case 
2:
                {
                    
SendClientMessage(playerid, -1"Asusid tццle bussijuhina!");
                    
andmed[playerid][pClass] = 3;
                    
SetPlayerColor(playeridColorClassBusDriver);
                    
SetSpawnInfo(playerid12951231000000);
                    
SpawnPlayer(playerid);
                }
                case 
3:
                {
                    
SendClientMessage(playerid, -1"Asusid tццle rekkamehena!");
                    
andmed[playerid][pClass] = 4;
                    
SetPlayerColor(playeridColorClassTruckDriver);
                    
SetSpawnInfo(playerid12951231000000);
                    
SpawnPlayer(playerid);
                }
                case 
4:
                {
                    if(
andmed[playerid][ppiloot] != 1)
                    {
                        
SendClientMessage(playerid, -1"Sul pole veel piloodi litsentsi! Info saamiseks vali mхni teine tцц ja uuri /abi");
                        
ShowPlayerDialog(playerid2DIALOG_STYLE_LIST"Amet""Pitsapoiss\nKuller\nBussijuht\nRekkamees\nPiloot\nPolitseinik""Vali""");
                    }
                    else
                    {
                        
SendClientMessage(playerid, -1"Asusid tццle piloodina!");
                        
andmed[playerid][pClass] = 5;
                        
SetPlayerColor(playeridColorClassPilot);
                        
SetSpawnInfo(playerid12951231000000);
                        
SpawnPlayer(playerid);
                    }
                }
                case 
5:
                {
                    if(
andmed[playerid][ppolitsei] != 1)
                    {
                        
SendClientMessage(playerid, -1"Sul pole veel politsei litsentsi! Info saamiseks vali mхni teine tцц ja uuri /abi");
                        
ShowPlayerDialog(playerid2DIALOG_STYLE_LIST"Amet""Pitsapoiss\nKuller\nBussijuht\nRekkamees\nPiloot\nPolitseinik""Vali""");
                    }
                    else
                    {
                        
SendClientMessage(playerid, -1"Asusid tццle politeinikuna!");
                        
andmed[playerid][pClass] = 6;
                        
SetPlayerColor(playeridColorClassPolice);
                        
SetSpawnInfo(playerid12951231000000);
                        
SpawnPlayer(playerid);
                    }
                }
            }
        }
    }
    return 
1;
}
public 
OnPlayerClickPlayer(playeridclickedplayeridsource)
{
    return 
1;
}
public 
globalPlayerLoop() {
    
gettime(gTime[0], gTime[1], gTime[2]);
    if(
gTime[1] >= 59 && gTime[2] >= 59) {
        
weatherVariables[1] += random(3) + 1// Weather changes aren't regular.
        
SetWorldTime(gTime[0]); // Set the world time to keep the worldtime variable updated (and ensure it syncs instantly for connecting players).
        
if(weatherVariables[1] >= MAX_WEATHER_POINTS) {
            
weatherVariables[0] = validWeatherIDs[random(sizeof(validWeatherIDs))];
            foreach(
Playeri) {
                if(!
GetPlayerInterior(i)) {
                    
SetPlayerWeather(iweatherVariables[0]);
                }
                else 
SetPlayerWeather(iINTERIOR_WEATHER_ID);
            }
            
weatherVariables[1] = 0;
        }
    }
    
/* ------------------------------------------------------ */
    
foreach(Playerx) {
        if(
gTime[2] >= 59syncPlayerTime(x);
        if(
andmed[x][pLogged] == 1) {
            if(
GetPlayerMoney(x) != andmed[x][pRaha]) {
                
ResetPlayerMoney(x);
                
GivePlayerMoney(xandmed[x][pRaha]);
            }
        }
    }
    
    return 
true;
}
stock unixTimeConvert(timestampcompare = -1) {
    if(
compare == -1)
        
compare gettime();
    new
        
n,
        
Float:= (timestamp compare) ? timestamp compare compare timestamp,
        
returnstr[32];
    if (
60) {
        
format(returnstrsizeof(returnstr), "< 1 minute");
        return 
returnstr;
    } else if (
3600) { // 3600 = 1 hour
        
floatround(floatdiv(d60.0), floatround_floor);
        
format(returnstrsizeof(returnstr), "minute");
    } else if (
86400) { // 86400 = 1 day
        
floatround(floatdiv(d3600.0), floatround_floor);
        
format(returnstrsizeof(returnstr), "hour");
    } else if (
2592000) { // 2592000 = 1 month
        
floatround(floatdiv(d86400.0), floatround_floor);
        
format(returnstrsizeof(returnstr), "day");
    } else if (
31536000) { // 31536000 = 1 year
        
floatround(floatdiv(d2592000.0), floatround_floor);
        
format(returnstrsizeof(returnstr), "month");
    } else {
        
floatround(floatdiv(d31536000.0), floatround_floor);
        
format(returnstrsizeof(returnstr), "year");
    }
    
format(returnstr32, ((!= 1)?("%d %ss"):("1 %s")), returnstr);  // you can use even their the ternary operator
    
return returnstr;
}
@
Get_Player_Time_On_Server(playerid){
    new 
Time = (andmed[playerid][pSeconds] +(gettime()-andmed[playerid][pConnectedSeconds]));
    
andmed[playerid][pPlayingHours] += (Time/3600);
    
andmed[playerid][pSeconds] = Time 3600;
    return 
1;
}
stock nulliandmed(playerid)
{
    new 
i;
    for(; 
playerEnum:playerEnum; ++iandmed[playerid][playerEnum:i] = 0;  //this way to reset the variabel in the enum is easyier
    
return true;
}
stock laeandmed(playerid)
{
    new 
query[300], pname[24];
    
GetPlayerName(playeridpname24);
    
format(querysizeof(query), "SELECT raha, skoor, piloot, politsei, playerHours, playerSeconds FROM kasutajad WHERE nimi = '%s' LIMIT 1"pname);
    
mysql_query(query);
    
mysql_store_result();
    
mysql_fetch_row_format(query,"|");
    
sscanf(query,"p<|>dddddd",andmed[playerid][pRaha],andmed[playerid][pSkoor],andmed[playerid][ppiloot],andmed[playerid][ppolitsei],andmed[playerid][pPlayingHours],andmed[playerid][pSeconds]);
    
GivePlayerMoney(playeridandmed[playerid][pRaha]);
    
SetPlayerScore(playeridandmed[playerid][pSkoor]);
    
andmed[playerid][pConnectedSeconds] = gettime();
    
mysql_free_result(); // changed loading this way is fast than your old one and please use if you have static numbers use them it`s faster  to get the right size
    
return 1;
}
stock salvestaandmed(const playerid)
{
    new 
saveQuery[400];
    new 
Name[MAX_PLAYER_NAME];
    
GetPlayerName(playeridNamesizeof(Name));
    @
Get_Player_Time_On_Server(playerid);
    
format(saveQuerysizeof(saveQuery), "UPDATE kasutajad SET raha = '%d', skoor = '%d', playerSeconds = '%d', playerHours = '%d' WHERE nimi = '%s'",
    
andmed[playerid][pRaha],
    
andmed[playerid][pSkoor],
    
andmed[playerid][pSeconds],
    
andmed[playerid][pPlayingHours],
    
Name);
    
mysql_query(saveQuery); //changed you don`t need to clear the memory which is in used by storing results
    
return 1;
}
stock syncPlayerTime(const playerid) {
    if(!
GetPlayerInterior(playerid)) {
        
SetPlayerWeather(playeridweatherVariables[0]);
    }
    else 
SetPlayerWeather(playeridINTERIOR_WEATHER_ID);
    return 
SetPlayerTime(playeridgTime[0], gTime[1]);
}
RewardPlayer(playeridMoneyPoints)
{
    
// Add the given amount of money to the player's account
    
andmed[playerid][pRaha] = andmed[playerid][pRaha] + Money;
    
// Add the given amount of scorepoints to the player's account
    
andmed[playerid][pSkoor] = andmed[playerid][pSkoor] + Points;
}
//============================================================================//
//                                  Player Commands                           //
//============================================================================//
CMD:statistika(playeridparams[])
{
    @
Get_Player_Time_On_Server(playerid);
    
format(szMessagesizeof(szMessage), "Litsentsid | Piloot: %d | Politsei: %d\nRaha: %d | Sekundeid: %d | Tunde: %d"andmed[playerid][ppiloot], andmed[playerid][ppolitsei], andmed[playerid][pRaha], andmed[playerid][pSeconds], andmed[playerid][pPlayingHours]);
    
SendClientMessage(playerid, -1szMessage);
    return 
1;
}
CMD:raha(playeridparams[])
{
    new 
AmountMsg[128];
    if (
sscanf(params"i"Amount)) SendClientMessage(playerid0xFF0000AA"Kasuta: \"/raha <Summa>\"");
    else
    {
        
// Check if the player gave himself too much at once
        
if (Amount 1000000)
        {
            
// Inform the player and quit the command
            
SendClientMessage(playerid0xFF0000AA"Sa ei saa panna endale raha korraga ьle 10m korraga");
            return 
1;
        }
        
// Reward the player (give cash and points)
        
RewardPlayer(playeridAmount0);
        
format(Msg128"Sa kandsid endale $%i"Amount);
        
SendClientMessage(playerid0x00FF00AAMsg);
    }
    return 
1;

Reply


Messages In This Thread
Mysql saving & loading problems + timing - by Trooja - 23.05.2013, 19:30
AW: Mysql saving & loading problems + timing - by IPrototypeI - 23.05.2013, 21:53
Re: Mysql saving & loading problems + timing - by Trooja - 25.05.2013, 10:21
Re: Mysql saving & loading problems + timing - by Trooja - 27.05.2013, 11:02
Re: Mysql saving & loading problems + timing - by Trooja - 28.05.2013, 16:55

Forum Jump:


Users browsing this thread: 1 Guest(s)