open.mp forum
[Pawn] [Help] Can't get Player Money to Update through MySQL - Printable Version

+ open.mp forum (https://forum.open.mp)
-- Forum: SA-MP (https://forum.open.mp/forumdisplay.php?fid=3)
--- Forum: Pawn Scripting (https://forum.open.mp/forumdisplay.php?fid=10)
--- Thread: [Pawn] [Help] Can't get Player Money to Update through MySQL (/showthread.php?tid=2608)



[Help] Can't get Player Money to Update through MySQL - YoungKrypt - 2024-02-25

Hello San Andreas...
So I have begun scripting a SAMP Server.

I found MySQL a little difficult so I used someone elses MySQL DB and it all works perfectly...
I thought I could just Add Money into the MySQL queries but no matter how I do it, the game loads the Money from MySQL (Default 25,000)
But I don't know how to get the Game to Save MySQL for Money.
I have tried adding it but it doesn't auto -update...

I would like some help with it if someone can help...
(Here is my entire code, some parts may not be neccessary).


Code:
// This is a comment
// uncomment the line below if you want to write a filterscript
//#define FILTERSCRIPT

#include <a_samp>
#include <a_sampdb>
#include <zcmd>
#include <sscanf2>
#include <a_mysql>
#include <streamer>
#include <bcrypt> //Bcrypt is the best way of encrypting passwords.
#include <foreach> //foreach standalone version

//Defining MySQL stuff here



#pragma warning disable 239

#pragma warning disable 217

#pragma warning disable 219

#pragma warning disable 203


#define DB_HOST "localhost" //IP of your host. In case of using it on same pc, use localhost or 127.0.0.1
#define DB_NAME "lascenturarp" //Name of Database you are gonna use.. I have used login, but change it according to your needs.
#define DB_USER "root" //User name of your MySQL client.
#define DB_PASS "" //Password of your MySQL client.

//Default Username is root and password is blank. You still gotta define them
//Make sure to install XAMPP server. Start Apache and MySQL service when you start the server. You can manage SQL databases from PHPMyAdmin which comes in built in XAMPP.

enum //Always use some kind of structure for Dialog IDs.
{
DIALOG_ASK,
DIALOG_REGISTER,
DIALOG_LOGIN,
DIALOG_GIVECAR
};

enum pinfo
{
MasterID,
Float:PX,
Float:PY,
Float:PZ,
Float:Rot,
Skin,
Level,
money,

bool:LoggedIn
};
new pInfo[MAX_PLAYERS][pinfo];

new MySQL:handle; //This connection handle of data type MySQL is required to carry out Mysql operations.



//------------------------------------------//

#define strcpy(%0,%1)  strcat(((%0[0] = 0), %0), %1);
#define SCM SendClientMessage;
#define SM SendMessage;


main()
{
print("\n----------------------------------");
print(" Las Centura RP by Xero Xipher");
print("----------------------------------\n");
}
//----------------------------------//
//Color Codes//
#define Yellow 0xd9ff00AA;
#define Orange 0xff8800AA;
#define DarkRed 0xa81100AA;
#define Red 0xff1900AA;
#define DarkPurple 0x960c74AA;
#define Purple 0xff14c4AA;
#define DarkBlue 0x1f1ac4AA;
#define Blue 0x5d52ffAA;
#define LightPurple 0x9263ffAA;
#define Green 0x00a613AA;
#define LightGreen 0x00e600AA;
//---------------------------------------//
//END OF COLORS//

public OnGameModeInit()
{
// Don't use these lines if it's a filterscript
SetGameModeText("RolePlay");
AddPlayerClass(0, 2186.054119, -1806.887207, 15.438260, 0, 0, 0, 0, 0, 0, 0);
SetWeather(2);
SetWorldTime(11);
handle = mysql_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

if(mysql_errno(handle) == 0) printf("[MYSQL] Connection successful"); //returns number of errors. 0 means no errors..
else
{
    new error[100];
    mysql_error(error, sizeof(error), handle);
printf("[MySQL] Connection Failed : %s", error);
}



//-------------------------------------------------------------------------------//
//----------------------------MAPPING--------------------------------------------//
//===============================================================================//

CreateObject(1410, 2492.00000, -1688.00000, 13.30000,  0.00000, 0.00000, 0.00000);
CreateObject(1410, 2487.52148, -1688.02161, 13.30000,  0.00000, 0.00000, 0.00000);
CreateObject(1410, 2498.62817, -1688.00000, 13.30000,  0.00000, 0.00000, 0.00000); //CJ Fence

CreateObject(19902, 2177.98682, -1770.43567, 12.53940,  0.00000, 0.00000, 0.00000); //Motel Marker

CreateObject(11699, 2437.24487, -1652.71094, 12.33990,  0.00000, 0.00000, 90.00000);

CreateVehicle(496, 2484.4080,-1654.6121,13.3145,89.9758, 044, 044, -1, 0); // Grove Blista Compact
CreateActor(270, 2522.2126, -1679.3553, 15.4970, 90);
AddStaticVehicle(402,2492.5657,-1683.4241,13.1326,271.5269,44,44); // CJ Parking
AddStaticVehicle(466,2509.7056,-1671.1899,13.1193,350.6347,44,44); // sweet parking
AddStaticVehicle(496,2157.8501,-1794.3157,13.0747,1.0815,006,006); // motel1
AddStaticVehicle(402,2162.5005,-1794.4071,13.0776,359.5714,181,181); // motel2

return 1;

}

public OnGameModeExit()
{
foreach(new i : Player)
{
if(pInfo[i][LoggedIn]) SavePlayerData(i);
}
mysql_close(handle);
return 1;
}

public OnPlayerRequestClass(playerid, classid)
{
return 1;
}

public OnPlayerConnect(playerid)
{
new query[64];
new pname[MAX_PLAYER_NAME];
GetPlayerName(playerid, pname, sizeof(pname));
mysql_format(handle, query, sizeof(query), "SELECT COUNT(Name) from `users` where Name = '%s' ", pname);
mysql_tquery(handle, query, "OnPlayerJoin", "d", playerid);


//-----------------------------------------------------------------------------------//
//-----------------------------------MAPPING-----------------------------------------//
//-----------------------------------------------------------------------------------//
RemoveBuildingForPlayer(playerid, 1410, 2487.3984, -1688.1016, 13.2813, 0.25);
RemoveBuildingForPlayer(playerid, 1410, 2491.8984, -1688.1016, 13.2813, 0.25);
RemoveBuildingForPlayer(playerid, 1410, 2498.6719, -1688.2344, 13.2813, 0.25);


return 1;
}


public OnPlayerDisconnect(playerid, reason)
{
if(pInfo[playerid][LoggedIn]) SavePlayerData(playerid);
pInfo[playerid][LoggedIn] = false;
return 1;
}

public OnPlayerSpawn(playerid)
{
SetPlayerScore(playerid, 1);
    SetPlayerSkin(playerid, 0);

return 1;
}

public OnPlayerDeath(playerid, killerid, reason)
{
return 1;
}

public OnVehicleSpawn(vehicleid)
{
return 1;
}

public OnVehicleDeath(vehicleid, killerid)
{
return 1;
}

public OnPlayerText(playerid, text[])
{
return 1;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
if (strcmp("/mycommand", cmdtext, true, 10) == 0)
{
// Do something here
return 1;
}
return 0;
}

public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
return 1;
}

public OnPlayerExitVehicle(playerid, vehicleid)
{
return 1;
}

public OnPlayerStateChange(playerid, newstate, oldstate)
{
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(playerid, objectid)
{
return 1;
}

public OnPlayerPickUpPickup(playerid, pickupid)
{
return 1;
}

public OnVehicleMod(playerid, vehicleid, componentid)
{
return 1;
}

public OnVehiclePaintjob(playerid, vehicleid, paintjobid)
{
return 1;
}

public OnVehicleRespray(playerid, vehicleid, color1, color2)
{
return 1;
}

public OnPlayerSelectedMenuRow(playerid, row)
{
return 1;
}

public OnPlayerExitedMenu(playerid)
{
return 1;
}

public OnPlayerInteriorChange(playerid, newinteriorid, oldinteriorid)
{
return 1;
}

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
return 1;
}

public OnRconLoginAttempt(ip[], password[], success)
{
return 1;
}

public OnPlayerUpdate(playerid)
{
return 1;
}

public OnPlayerStreamIn(playerid, forplayerid)
{
return 1;
}

public OnPlayerStreamOut(playerid, forplayerid)
{
return 1;
}

public OnVehicleStreamIn(vehicleid, forplayerid)
{
return 1;
}

public OnVehicleStreamOut(vehicleid, forplayerid)
{
return 1;
}

SavePlayerData(playerid)
{
new query[256], pname[MAX_PLAYER_NAME], Float:px, Float:py, Float:pz, Float:rot;
GetPlayerName(playerid, pname, sizeof(pname));
GetPlayerPos(playerid, px, py, pz);
GetPlayerFacingAngle(playerid, rot);
GetPlayerMoney(playerid);
mysql_format(handle, query, sizeof(query), "UPDATE `users` set PosX = %f, PosY = %f, PosZ = %f, Rot = %f, Skin = %d, Level = %d, money = %d, WHERE Master_ID = %d", px, py, pz, rot, pInfo[playerid][Skin], pInfo[playerid][Level], pInfo[playerid][money], pInfo[playerid][MasterID]);
mysql_query(handle, query);
printf("Saved %s's data", pname);
return 1;
}


public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
switch(dialogid)
{
case DIALOG_REGISTER:
    {
if(response)
{
    bcrypt_hash(inputtext, 12, "OnPassHash", "d", playerid);
}
else Kick(playerid);
}

case DIALOG_LOGIN:
{
if(response)
{
new query[128], pname[MAX_PLAYER_NAME];
GetPlayerName(playerid, pname, sizeof(pname));
    SetPVarString(playerid, "Unhashed_Pass",inputtext);
mysql_format(handle, query, sizeof(query), "SELECT password, Master_ID from `users` WHERE Name = '%s'", pname);
mysql_tquery(handle, query, "OnPlayerLogin", "d", playerid);
}
else Kick(playerid);


}

case DIALOG_GIVECAR:
{
switch(listitem)
{
case 0:
{
MakeCar(playerid, 496);
TakePlayerMoney(playerid, -450);
}
case 1:
{
MakeCar(playerid, 550);
TakePlayerMoney(playerid, -850);
}
case 2:
{
MakeCar(playerid, 560);
TakePlayerMoney(playerid, -2200);
}

}
}
}
return 1;
}

public OnPlayerClickPlayer(playerid, clickedplayerid, source)
{
return 1;
}


//===============COMMANDS=======================
CMD:addmoney(playerid, params[])
{
    new amount;
    new string[128];
    if(sscanf(params, "d", amount)) return SendClientMessage(playerid, 0x9ba803AA, "USAGE: /addMoney <amount>");
    // if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, ...);
    GivePlayerMoney(playerid, amount);
    format(string, sizeof string, "Congratulations, you have earned $%d", amount);
    SendClientSuccess(playerid, string);
    return 1;
}

/*
CMD:givecar(playerid, params[])
{
    new vehid;
    if(sscanf(params, "i", vehid))return SendClientMessage(playerid, 0x9ba803AA, "USAGE: /givecar <vehicle id>");
    MakeCar(playerid, vehid);
    return 1;
}*/
CMD:mypos(playerid, params[])
{
    new buffer[64], Float: px, Float: py, Float: pz;
    GetPlayerPos(playerid, px, py, pz);
    format(buffer, sizeof buffer, "My Position is: %f, %f, %f.", px, py, pz);
    SendClientMessage(playerid, 0x416bbfAA, buffer);
    return 1;
}
CMD:buycar(playerid, params[])
{
ShowPlayerDialog(playerid, DIALOG_GIVECAR, DIALOG_STYLE_TABLIST_HEADERS, "Car Dealership", "{d6b004}Vehicle: \t\t{12d600}Price:\n{d6b004}Blista Compact \t\t{12d600}$475\n{d6b004}Sunrise \t\t{12d600}$850\n{d6b004}Sultan \t\t{12d600}$2200", "Buy Vehicle", "");
return 1;
}

MakeCar(playerid, vehid)
{
    new Float:px, Float:py, Float:pz;
    GetPlayerPos(playerid, px, py, pz);
    CreateVehicle(vehid, px, py, pz, 0, 0, 0, -1);
    SendClientSuccess(playerid, "Congratulations, you have just received a new vehicle!");
    return 1;
}
TakePlayerMoney(playerid, amount)
{
new amount, string[128];
// if(sscanf(params, "i",  amount)) return SendClientMessage(playerid, 0x9ba803AA, "USAGE: /addMoney <amount>");
GivePlayerMoney(playerid, -amount);
    format(string, sizeof string, "You have $%d deducted from your bank account.", amount);
SendClientError(playerid, string);
    return 1;

}

SendClientSuccess(playerid, str[])
{
new nStr[128];
format(nStr, sizeof(nStr), "[SUCCESS]: {FFFFFF}%s", str);
SendClientMessage(playerid, 0x11a800AA, nStr);
return 1;
}
SendClientError(playerid, str[])
{
new nStr[128];
format(nStr, sizeof(nStr), "[ERROR]: {FFFFFF}%s", str);
SendClientMessage(playerid, 0xff1803AA, nStr);
return 1;
}


forward OnPlayerJoin(playerid);
public OnPlayerJoin(playerid)
{
new rows;
cache_get_value_index_int(0, 0, rows);

if(rows) ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "This account is found on your database. Please login", "Login", "Quit");


else ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Register", "This account not is found on your database. Please register", "Register", "Quit");
return 1;
}

forward OnPlayerRegister(playerid);
public OnPlayerRegister(playerid)
{
SendClientMessage(playerid, 0x0033FFFF /*Blue*/, "Thank you for registering! You can now Login");
    ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "Thank you for registering! You can now Login with\npassword you just used to register.", "Login", "Quit");
return 1;
}

forward OnPlayerLogin(playerid);
public OnPlayerLogin(playerid)
{
new pPass[255], unhashed_pass[128];
GetPVarString(playerid, "Unhashed_Pass",unhashed_pass,sizeof(unhashed_pass));
if(cache_num_rows())
{
cache_get_value_index(0, 0, pPass);
cache_get_value_index_int(0, 1, pInfo[playerid][MasterID]);
bcrypt_check(unhashed_pass, pPass, "OnPassCheck", "dd",playerid, pInfo[playerid][MasterID]);
  }
    else printf("ERROR ");
return 1;
}

forward OnPassHash(playerid);
public OnPassHash(playerid)
{
new pass[BCRYPT_HASH_LENGTH], query[128], pname[MAX_PLAYER_NAME];
    GetPlayerName(playerid, pname, sizeof(pname));
    bcrypt_get_hash(pass);
    mysql_format(handle, query, sizeof(query), "INSERT INTO `users`(Name, Password) VALUES('%s', '%e')", pname, pass);
mysql_tquery(handle, query, "OnPlayerRegister", "d", playerid);
return 1;
}

forward OnPassCheck(playerid, DBID);
public OnPassCheck(playerid, DBID)
{
    if(bcrypt_is_equal())
{
SetPlayerInfo(playerid, DBID);
}
else ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "The password you just entered is wrong.\nPlease Try again!", "Login", "Quit");
return 1;
}

SetPlayerInfo(playerid, dbid)
{
new query[128];
mysql_format(handle, query, sizeof(query), "SELECT  PosX, PosY, PosZ, Rot, Skin, Level, money FROM `users` WHERE Master_ID = %d", dbid);
new Cache:result = mysql_query(handle, query);

cache_get_value_index_float(0, 0, pInfo[playerid][PX]);
cache_get_value_index_float(0, 1, pInfo[playerid][PY]);
cache_get_value_index_float(0, 2, pInfo[playerid][PZ]);
cache_get_value_index_float(0, 3, pInfo[playerid][Rot]);
cache_get_value_index_int(0, 4, pInfo[playerid][Skin]);
cache_get_value_index_int(0, 5, pInfo[playerid][Level]);
cache_get_value_index_int(0, 6, pInfo[playerid][money]);

pInfo[playerid][LoggedIn] = true;

cache_delete(result);



SetPlayerScore(playerid, pInfo[playerid][Level]);
SetSpawnInfo(playerid, 0, pInfo[playerid][Skin], pInfo[playerid][PX], pInfo[playerid][PY], pInfo[playerid][PZ],pInfo[playerid][Rot], 0, 0, 0, 0, 0, 0);

new money;
cache_get_value_int(0, "money", money);
GivePlayerMoney(playerid, pInfo[playerid][money]);

TogglePlayerSpectating(playerid, false);
TogglePlayerControllable(playerid, true);

new name[MAX_PLAYER_NAME], str[80];
GetPlayerName(playerid, name, sizeof(name));
format(str, sizeof(str), "Welcome to the server, {FFFFFF}%s", name);
SendClientMessage(playerid, -1, str);
DeletePVar(playerid, "Unhashed_Pass");

SpawnPlayer(playerid);
return 1;
}



RE: [Help] Can't get Player Money to Update through MySQL - YoungKrypt - 2024-02-26

Damn, I feel like a Newb...
In Save Data Ihad saved the Data in the same structure it was given in:
Code:
mysql_format(handle, query, sizeof(query), "UPDATE `users` set PosX = %f, PosY = %f, PosZ = %f, Rot = %f, Skin = %d, Level = %d, money = %d, WHERE Master_ID = %d", px, py, pz, rot, pInfo[playerid][Skin], pInfo[playerid][Level], pInfo[playerid][money], pInfo[playerid][MasterID]);

All I had to do was delete: pInfo[playerid][money]
And replace it with GetPlayerMoney(playerid)

Code:
mysql_format(handle, query, sizeof(query), "UPDATE `users` set PosX = %f, PosY = %f, PosZ = %f, Rot = %f, Skin = %d, Level = %d, money = %d, WHERE Master_ID = %d", px, py, pz, rot, pInfo[playerid][Skin], pInfo[playerid][Level], GetPlayerMoney(playerid), pInfo[playerid][MasterID]);