SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 09/09/2019, 10:50 PM   #1
PlayHard
Huge Clucker
 
PlayHard's Avatar
 
Join Date: Nov 2011
Location: In an apartment
Posts: 215
Reputation: 22
Default Can't get a number to save in Database

I can't get p_admin to get saved or updated in the database of SQLite, why?

Code:
//08/09/2019
//

#include <a_samp>
#include <easy-sqlite>

enum p_info
{
    p_id,
    p_name[24],
    p_password[64],
    p_score,
    Float:p_posx,
    Float:p_posy,
    Float:p_posz,
    p_loggedin,
    p_admin
};
new UserInfo[MAX_PLAYERS][p_info];
#define DIALOG_LOGIN 0
#define DIALOG_REGISTER 1
stock ret_pName(playerid)
{
    new name[24];
    GetPlayerName(playerid, name, sizeof(name));
    return name;
}

main()
{

}

public OnGameModeInit()
{
	SetGameModeText("GTA: NARCOS");
	AddPlayerClass(0, 4046, 1479.5234, -1852.6406, 24.5156, 0, 0, 0, 0, 0, 0);
	
	    //Connecting to the database
    SL::Connect("server.db");
    //Checking if the table 'players' exists


    //Checking if the table 'players' exists
    if(!SL::ExistsTable("players"))
    {
        //If not, then create a table called 'players'.
        new handle = SL::Open(SL::CREATE, "players"); //Opening a valid handle to create a table called 'players'
        SL::AddTableEntry(handle, "p_id", SL_TYPE_INT, 11, true);
        SL::AddTableEntry(handle, "p_name", SL_TYPE_VCHAR, 24);
        SL::AddTableEntry(handle, "p_password", SL_TYPE_VCHAR, 64);
        SL::AddTableEntry(handle, "p_score", SL_TYPE_INT);
        SL::AddTableEntry(handle, "p_posx", SL_TYPE_FLOAT);
        SL::AddTableEntry(handle, "p_posy", SL_TYPE_FLOAT);
        SL::AddTableEntry(handle, "p_posz", SL_TYPE_FLOAT);
        SL::AddTableEntry(handle, "p_admin", SL_TYPE_INT);
        SL::Close(handle);//Closing the previous opened handle.
        print("Table 'players' was successfully created");
    }
    else
    {
        print("Table 'players' was successfully loaded");
    }
    
	return 1;
}

public OnGameModeExit()
{
	return 1;
}

public OnPlayerRequestClass(playerid, classid)
{
	SetPlayerPos(playerid, 1958.3783, 1343.1572, 15.3746);
	SetPlayerCameraPos(playerid, 1958.3783, 1343.1572, 15.3746);
	SetPlayerCameraLookAt(playerid, 1958.3783, 1343.1572, 15.3746);
	return 1;
}

public OnPlayerConnect(playerid)
{
    UserInfo[playerid][p_loggedin] = 0; UserInfo[playerid][p_score] = 0;  UserInfo[playerid][p_posx] = 1958.3783;
    UserInfo[playerid][p_posy] = 1343.1572; UserInfo[playerid][p_posz] = 15.3746;
    if(SL::RowExistsEx("players", "p_name", ret_pName(playerid))) //Check if the name is registered in the database
    {
        //Get the player password and unique ID.
        new handle = SL::OpenEx(SL::READ, "players", "p_name", ret_pName(playerid));
        SL::ReadString(handle, "p_password", UserInfo[playerid][p_password], 64);
        SL::ReadInt(handle, "p_id", UserInfo[playerid][p_id]);
		SL::ReadInt(handle, "p_admin", UserInfo[playerid][p_admin]);
        SL::Close(handle);
        //Show the login dialog
        ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "{0080FF}Login", "Please input your password below to log in.", "Login", "Exit");
    }
    else
    {
        //If not registered, then show the register DIALOG.
        ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "{0080FF}Register", "Please input a password below to register in.", "Login", "Exit");
    }
    
    	//City Hall removal
	RemoveBuildingForPlayer(playerid, 4024, 1479.8672, -1790.3984, 56.0234, 0.25);
	RemoveBuildingForPlayer(playerid, 4044, 1481.1875, -1785.0703, 22.3828, 0.25);
	RemoveBuildingForPlayer(playerid, 4046, 1479.5234, -1852.6406, 24.5156, 0.25);
	RemoveBuildingForPlayer(playerid, 1527, 1448.2344, -1755.8984, 14.5234, 0.25);
	RemoveBuildingForPlayer(playerid, 4170, 1433.9531, -1844.4063, 21.4219, 0.25);
	RemoveBuildingForPlayer(playerid, 4004, 1479.5234, -1852.6406, 24.5156, 0.25);
	RemoveBuildingForPlayer(playerid, 4171, 1503.3984, -1848.3359, 21.4609, 0.25);
	RemoveBuildingForPlayer(playerid, 4173, 1427.2734, -1756.1797, 15.0000, 0.25);
	RemoveBuildingForPlayer(playerid, 4002, 1479.8672, -1790.3984, 56.0234, 0.25);
	RemoveBuildingForPlayer(playerid, 3980, 1481.1875, -1785.0703, 22.3828, 0.25);
	RemoveBuildingForPlayer(playerid, 4003, 1481.0781, -1747.0313, 33.5234, 0.25);
	RemoveBuildingForPlayer(playerid, 4172, 1534.7656, -1756.1797, 15.0000, 0.25);
	
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    if(UserInfo[playerid][p_loggedin] == 1)
    {
        //Save the player data.
        GetPlayerPos(playerid, UserInfo[playerid][p_posx], UserInfo[playerid][p_posy], UserInfo[playerid][p_posz]);
        new handle = SL::Open(SL::UPDATE, "players", "p_id", UserInfo[playerid][p_id]);
        SL::WriteInt(handle, "p_score", GetPlayerScore(playerid));
        SL::WriteInt(handle, "p_admin", UserInfo[playerid][p_admin]);
        SL::WriteFloat(handle, "p_posx", UserInfo[playerid][p_posx]);
        SL::WriteFloat(handle, "p_posy", UserInfo[playerid][p_posy]);
        SL::WriteFloat(handle, "p_posz", UserInfo[playerid][p_posz]);
        SL::Close(handle);
    }
    return 1;
}

public OnPlayerSpawn(playerid)
{
	//Gets the last saved position of a player
    SetPlayerPos(playerid, UserInfo[playerid][p_posx], UserInfo[playerid][p_posy], UserInfo[playerid][p_posz]);
	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("/setadmin", cmdtext, true, 10) == 0)
	{
		if(UserInfo[playerid][p_admin] >= 5)
		{
		    SendClientMessage(playerid, 0x9844531, "success to level 10");
		    UserInfo[playerid][p_admin] = 10;
		}
		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;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    switch(dialogid)
    {
        case DIALOG_REGISTER:
        {
            if(!response) return Kick(playerid);
            if(strlen(inputtext) < 5)
            {
                ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "{0080FF}Register", "Please input a password below to register in.", "Login", "Exit");
                return 1;
            }
            SHA256_PassHash(inputtext, "", UserInfo[playerid][p_password], 64);
            new handle = SL::Open(SL::INSERT, "players");
            SL::ToggleAutoIncrement(handle, true);
            SL::WriteString(handle, "p_name", ret_pName(playerid));
            SL::WriteString(handle, "p_password", UserInfo[playerid][p_password]);
            SL::WriteInt(handle, "p_score", 0);
            SL::WriteFloat(handle, "p_posx", 0.0);
            SL::WriteFloat(handle, "p_posy", 0.0);
            SL::WriteFloat(handle, "p_posz", 0.0);
            SL::WriteInt(handle, "p_admin", 0);
            UserInfo[playerid][p_id] = SL::Close(handle);//If auto_increment was set to true, SL::Close will return the ID of the inserted row.
            SendClientMessage(playerid, -1, "Successfully registered in!");
            UserInfo[playerid][p_loggedin] = 1;
        }
        case DIALOG_LOGIN:
        {
            if(!response) Kick(playerid);
            new hash[64];
            SHA256_PassHash(inputtext, "", hash, 64);
            if(!strcmp(hash, UserInfo[playerid][p_password]))
            {
                //Load player data
                new handle = SL::Open(SL::READ, "players", "p_id", UserInfo[playerid][p_id]);
                SL::ReadInt(handle, "p_score", UserInfo[playerid][p_score]);
                SL::ReadFloat(handle, "p_posx", UserInfo[playerid][p_posx]);
                SL::ReadFloat(handle, "p_posy", UserInfo[playerid][p_posy]);
                SL::ReadFloat(handle, "p_posz", UserInfo[playerid][p_posz]);
                SL::ReadInt(handle, "p_admin", UserInfo[playerid][p_admin]);
                SL::Close(handle);//You must close the handle.
                SetPlayerScore(playerid, UserInfo[playerid][p_score]);
                UserInfo[playerid][p_loggedin] = 1;
                SendClientMessage(playerid, -1, "Successfully logged in!");

            }
            else
            {
                ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "{0080FF}Login", "Please input your password below to log in.", "Login", "Exit");
            }
        }
    }
    return 1;
}

public OnPlayerClickPlayer(playerid, clickedplayerid, source)
{
	return 1;
}
PlayHard is offline   Reply With Quote
Old 10/09/2019, 09:53 AM   #2
DaniceMcHarley
High-roller
 
DaniceMcHarley's Avatar
 
Join Date: Feb 2012
Posts: 1,742
Reputation: 95
Default Re: Can't get a number to save in Database

Stop treating SQL like a file system, just use it the way it is!
DaniceMcHarley 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
my acc won't save on the database. HeroDR22 Scripting Help 8 04/06/2018 02:24 PM
Mysql Won't save in database DerickClark Scripting Help 16 22/01/2017 12:19 AM
Name wont save on database! daghost111 Scripting Help 8 21/12/2016 04:42 PM
player stats won't save in database DarkLored Scripting Help 2 07/06/2015 12:45 AM
Player Number in database [MYSQL] thimo Scripting Help 3 28/01/2012 03:23 PM


All times are GMT. The time now is 01:57 AM.


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