How would I create a mysql weapon system
#9

I've took the tutorial and modified the code, please check if it is correct or not and tell me my mistakes.
Код:
SaveWeaponData(playerid)
{
	if(PlayerInfo[playerid][LoggedIn] == false) return 0;
	new mysqlquery[512];
	mysql_format(MySQL_Database, mysqlquery, sizeof(mysqlquery), "DELETE FROM `PlayerWeapons` WHERE accountid = %d;", PlayerInfo[playerid][ID]);
	mysql_query(MySQL_Database, mysqlquery);
	for(new i = 0; i < 13; i++)
	{
		if(PlayerInfo[playerid][WeaponSlot][i] < 1 || PlayerInfo[playerid][WeaponSlot][i] > 46) continue;
		if(PlayerInfo[playerid][WeaponSlotAmmo][i] < 1) continue;
		mysql_format(MySQL_Database, mysqlquery, sizeof(mysqlquery), "INSERT INTO `PlayerWeapons` VALUES (%d, %d, %d) ON DUPLICATE KEY UPDATE ammo = %d;", PlayerInfo[playerid][ID], PlayerInfo[playerid][WeaponSlot][i], PlayerInfo[playerid][WeaponSlotAmmo][i], PlayerInfo[playerid][WeaponSlotAmmo][i]);
		mysql_pquery(MySQL_Database, mysqlquery);
	}
	return 1;
}

SaveAccount(playerid, bool:gettingBanned = false, bool:sendMsg = false)
{
	if(PlayerInfo[playerid][LoggedIn] == false) return 0;
        // saving other statistics
	SaveWeaponData(playerid);
	return 1;
}
Creating database:
Код:
mysql_query(MySQL_Database, "CREATE TABLE IF NOT EXISTS `PlayerWeapons` ( \
	`accountid` INT(11) UNSIGNED NOT NULL , \
	`weaponid` TINYINT(3) UNSIGNED NOT NULL , \
	`ammo` INT(10) UNSIGNED NOT NULL , \
	INDEX `accountid` (`accountid`), \
	CONSTRAINT `accountid` FOREIGN KEY (`accountid`) REFERENCES `accounts`(`ID`) ON DELETE CASCADE ON UPDATE CASCADE\
	) ENGINE = InnoDB;");
OnPlayerConnect
Код:
for(new slot = 0; slot < 13; slot++)
	{
	    PlayerInfo[playerid][WeaponSlot][slot] = 0;
	    PlayerInfo[playerid][WeaponSlotAmmo][slot] = 0;
	}
	PlayerInfo[playerid][WeaponsLoaded] = 0;
OnPlayerLogin
Код:
new querySnd[256];
				mysql_format(MySQL_Database, querySnd, sizeof(querySnd), "SELECT weaponid, ammo FROM `PlayerWeapons` WHERE accountid = %d;", PlayerInfo[playerid][ID]);
				mysql_tquery(MySQL_Database, querySnd, "OnLoadWeaponData", "ii", playerid, Corrupt_Check[playerid]);
Loading Weapons
Код:
public OnLoadWeaponData(playerid, corrupt_check)
{
	if(corrupt_check != Corrupt_Check[playerid]) return SFM(playerid, COLOR_RED, "Please reconnect as there has been an unknown error - Our apologies!"), Kick(playerid);
    new
        weaponid,
        ammo,
        j;
        
	cache_get_row_count(j);
    for(new i; i < j; i++)
    {
        cache_get_value_index_int(i, 0, weaponid);
        cache_get_value_index_int(i, 1, ammo);

        if(!(0 <= weaponid <= 46))
        {
            printf("[info] Warning: OnLoadWeaponData - Unknown weaponid '%d'. Skipping.", weaponid);
            continue;
        }
		if(ammo < 1)
		{
		    printf("[info] Warning: OnLoadWeaponData - Invalid ammo '%d'. Skipping.", ammo);
		    continue;
		}
		new slot = GetWeaponSlot(weaponid);
        PlayerInfo[playerid][WeaponSlot][slot] = weaponid;
        PlayerInfo[playerid][WeaponSlotAmmo][slot] = ammo;
    }
	PlayerInfo[playerid][WeaponsLoaded] = 1;
	SetTimerEx("LoadPlayerWeapons", 1000, false, "ii", playerid, Corrupt_Check[playerid]);
    return 1;
}

public LoadPlayerWeapons(playerid, corrupt_check)
{
	if(!PlayerInfo[playerid][LoggedIn]) return Kick(playerid);
    if(corrupt_check != Corrupt_Check[playerid])
    {
        return SFM(playerid, COLOR_RED, "An inrecoverable error has occured, PLease reconnect - Our apologies!"), Kick(playerid);
    }
    if(!IsPlayerSpawned(playerid)) return SetTimerEx("LoadPlayerWeapons", 1000, false, "ii", playerid, Corrupt_Check[playerid]);
    new weaponid, ammo;
    for(new slot = 0; slot < 13; slot++)
    {
        weaponid = PlayerInfo[playerid][WeaponSlot][slot];
        ammo = PlayerInfo[playerid][WeaponSlotAmmo][slot];
		if(weaponid < 1 || weaponid > 46) continue;
		if(ammo < 1) continue;
		GivePlayerWeapon(playerid, weaponid, ammo);
    }
    PlayerInfo[playerid][WeaponsLoaded] = 2;
    return 1;
}
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)