Код:
#include <a_samp>
#include <a_mysql>
#include <zcmd>
#include <sscanf2>
main(){}
#define MYSQL_HOST "127.0.0.1"
#define MYSQL_USER "root"
#define MYSQL_PASSWORD ""
#define MYSQL_DATABASE "db"
new MySQL: g_SQL;
enum vInfo
{
SQLID,
vModel,
Text3D:vLabel
};
new VehicleInfo[MAX_VEHICLES][vInfo];
public OnGameModeInit()
{
AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
Connect();
SetupVehicleTable();
mysql_tquery(g_SQL, "SELECT * FROM `veh`", "LoadVehicles");
return 1;
}
public OnGameModeExit()
{
new saved;
if(cache_num_rows())
{
while(saved < cache_num_rows())
{
SaveVehicle(saved);
saved++;
}
}
printf("%d saved vehicles", saved);
mysql_close(g_SQL);
return 1;
}
Connect()
{
new MySQLOpt: option_id = mysql_init_options();
mysql_set_option(option_id, AUTO_RECONNECT, true);
g_SQL = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, option_id);
if(g_SQL == MYSQL_INVALID_HANDLE || mysql_errno(g_SQL) != 0)
{
print("OFF SERVER!");
SendRconCommand("exit");
return 1;
}
else
{
print("ON SERVER!");
}
return 1;
}
SetupVehicleTable()
{
mysql_tquery(g_SQL, "CREATE TABLE IF NOT EXISTS `veh` (`vehid` int(11), `vehmodel` int(5) default '0', PRIMARY KEY (`vehid`), UNIQUE KEY `vehid_2` (`vehid`), KEY `vehid` (`vehid`))");
mysql_tquery(g_SQL, "ALTER TABLE `veh` CHANGE `vehid` `vehid` INT(11) NOT NULL AUTO_INCREMENT;");
return 1;
}
CreateVehicle_2(model)
{
new query[512], str[512];
new ID = CreateVehicle(model, -23.9678, 30.5966, 3.1172, 324.9064, -1, -1, -1);
VehicleInfo[ID][vModel] = model;
SetVehicleToRespawn(ID);
mysql_format(g_SQL, query, sizeof(query), "INSERT INTO `veh` (`vehmodel`) VALUES ('%d')",model);
mysql_tquery(g_SQL, query,"OnVehicleCreate","i",ID);
format(str, sizeof(str), "{0b62ed}VID: {00d820}%d", ID);
VehicleInfo[ID][vLabel] = Create3DTextLabel(str, -1, 0.0, 0.0, 0.0, 50.0, 0);
Attach3DTextLabelToVehicle(VehicleInfo[ID][vLabel], ID, 0.0, 0.0, 1.0);
return 1;
}
forward OnVehicleCreate(vehicleid);
public OnVehicleCreate(vehicleid)
{
VehicleInfo[vehicleid][SQLID] = cache_insert_id();
return 1;
}
forward LoadVehicles();
public LoadVehicles()
{
new loaded, ID, str[512];
if(cache_num_rows())
{
while(loaded < cache_num_rows())
{
ID = CreateVehicle(VehicleInfo[ID][vModel], -23.9678, 30.5966, 3.1172, 324.9064, -1, -1, -1);
cache_get_value_int(loaded, "vehid", VehicleInfo[ID][SQLID]);
cache_get_value_int(loaded, "vehmodel", VehicleInfo[ID][vModel]);
SetVehicleToRespawn(ID);
loaded++;
format(str, sizeof(str), "{0b62ed}VID: {00d820}%d", ID);
VehicleInfo[ID][vLabel] = Create3DTextLabel(str, -1, 0.0, 0.0, 0.0, 50.0, 0);
Attach3DTextLabelToVehicle(VehicleInfo[ID][vLabel], ID, 0.0, 0.0, 1.0);
}
}
printf("%d loaded vehicles", loaded);
return 1;
}
SaveVehicle(S_VID)
{
new query[512];
mysql_format(g_SQL, query, sizeof(query), "UPDATE `veh` SET `vehmodel` = %d WHERE `vehid` = %d LIMIT 1", VehicleInfo[S_VID][vModel], VehicleInfo[S_VID][SQLID]);
mysql_tquery(g_SQL, query);
return 1;
}
DeleteVehicle(D_VID)
{
new query[512];
DestroyVehicle(D_VID);
mysql_format(g_SQL, query, sizeof(query), "DELETE FROM `veh` WHERE `vehid` = %d LIMIT 1", VehicleInfo[D_VID][SQLID]);
mysql_tquery(g_SQL, query);
VehicleInfo[D_VID][SQLID] = 0;
VehicleInfo[D_VID][vModel] = 0;
return 1;
}
CMD:create(playerid, params[])
{
new model;
if(sscanf(params, "d", model)) return SendClientMessage(playerid, -1, "USAGE: /create [MODEL ID]");
CreateVehicle_2(model);
return 1;
}
CMD:delete(playerid, params[])
{
new del_vid;
if(sscanf(params, "d", del_vid)) return SendClientMessage(playerid, -1, "USAGE: /delete [VID]");
DeleteVehicle(del_vid);
return 1;
}
public OnPlayerSpawn(playerid)
{
SetPlayerSkin(playerid, 29);
SetPlayerPos(playerid, -23.9678, 30.5966, 3.1172);
SetPlayerFacingAngle(playerid, 324.9064);
return 1;
}