- 13
- 0
крч, запросы к mysql от 2011 года, надо сделать так, чтобы подошло к версии R39-6, а так вылазит дюжина ошибок, код ниже.
обращение к mysql:
и то что вписывается в мод: (не обязательно)
Код:
#include <a_samp>
#define BYTES_PER_CELL (cellbits / 8)
#include <a_mysql>
#define Fl: Float:
new string[64];
#define MYSQL_HOST "localhost"
#define MYSQL_USER "root"
#define MYSQL_PASSWORD ""
#define MYSQL_DATABASE "database1"
enum Vehicles
{
baseid,model,id,
Fl:x,Fl:y,Fl:z,Fl:a,color1,
fraction,rank,
};
new Vh[1000][Vehicles];
public OnFilterScriptInit()
{
mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_DATABASE,MYSQL_PASSWORD);
return SetTimer("GoGoAutos",1000,0);
}
forward public GoGoAutos();
public GoGoAutos()
{
MySQLCheck();
mysql_query("Select * From `vehicles`");
mysql_store_result();
new i=0;
if(mysql_num_rows()>0)
{
while(mysql_retrieve_row())
{
BaseGetInt("ID",Vh[i][baseid]);
BaseGetInt("modelid",Vh[i][model]);
BaseGetInt("color1",Vh[i][color1]);
BaseGetFloat("x",Vh[i][x]);
BaseGetFloat("y",Vh[i][y]);
BaseGetFloat("z",Vh[i][z]);
BaseGetFloat("a",Vh[i][a]);
BaseGetInt("faction",Vh[i][fraction]);
BaseGetInt("rank",Vh[i][rank]);
Vh[i][id] = CreateVehicle(Vh[i][model],Vh[i][x],Vh[i][y],Vh[i][z],Vh[i][a],Vh[i][color1],Vh[i][color1],300);
i++;
}
}
mysql_free_result();
return 1;
}
stock GetPlayerData(playerid,rejim)
{
switch(rejim)
{
case 0:
{
return CallRemoteFunction("GetPlayerMember","i",playerid);
}
case 1:
{
return CallRemoteFunction("GetPlayerLeader","i",playerid);
}
case 2:
{
return CallRemoteFunction("GetPlayerRank","i",playerid);
}
}
return 1;
}
/*forward public GetPlayerMember(playerid);
forward public GetPlayerRank(playerid);
public GetPlayerMember(playerid)
{
new fraction;
if(PlayerInfo[playerid][pMember] !=0) fraction = PlayerInfo[playerid][pMember];
else if(PlayerInfo[playerid][pLeader] !=0) fraction = PlayerInfo[playerid][pLeader];
return fraction;
}
public GetPlayerRank(playerid)
{
return PlayerInfo[playerid][pRank];
}*/
stock GetVehicleId(vehicleid)
{
for(new i =0;i<1000;i++)
{
if(Vh[i][id] == vehicleid) return i;
}
return -1;
}
public OnPlayerStateChange(playerid,newstate,oldstate)
{
if(newstate == PLAYER_STATE_DRIVER)
{
new l = GetPlayerVehicleID(playerid);
if(Vh[GetVehicleId(l)][fraction]!=GetPlayerData(playerid,0)){RemovePlayerFromVehicle(playerid); return SendClientMessage(playerid,-1,"Это транспорт не вашей фракции");}
if(Vh[GetVehicleId(l)][rank]>GetPlayerData(playerid,2)){RemovePlayerFromVehicle(playerid); format(string,sizeof(string),"Для езды на этом транспорте вам нужен %d ранг",Vh[GetVehicleId(l)][rank]);return SendClientMessage(playerid,-1,string);}
}
return 1;
}
public OnPlayerCommandText(playerid,cmdtext[])
{
new idx,cmd[32],tmp[32];
cmd = strtok(cmdtext,idx);
if(!strcmp(cmd,"/addcar",true))
{
if(!IsPlayerAdmin(playerid)) return 1;
if(GetPlayerVehicleID(playerid) ==0) return SendClientMessage(playerid,-1,"Вам нужно находится в машине");
tmp =strtok(cmdtext,idx);
new smodel = strval(tmp);
tmp =strtok(cmdtext,idx);
new scolor = strval(tmp);
tmp =strtok(cmdtext,idx);
new sfraction=strval(tmp);
tmp =strtok(cmdtext,idx);
if(!strlen(tmp)) return SendClientMessage(playerid,-1,"/addcar model color fraction rank");
new srank = strval(tmp);
mysql_query("Select * From `vehicles`");
mysql_store_result();
Vh[mysql_num_rows()][model] = smodel;
Vh[mysql_num_rows()][color1] = scolor;
Vh[mysql_num_rows()][fraction] = sfraction;
Vh[mysql_num_rows()][rank] = srank;
GetVehiclePos(GetPlayerVehicleID(playerid),Vh[mysql_num_rows()][x],Vh[mysql_num_rows()][y],Vh[mysql_num_rows()][z]);
GetVehicleZAngle(GetPlayerVehicleID(playerid),Vh[mysql_num_rows()][a]);
Vh[mysql_num_rows()][id] = CreateVehicle(Vh[mysql_num_rows()][model],Vh[mysql_num_rows()][x],Vh[mysql_num_rows()][y],Vh[mysql_num_rows()][z],Vh[mysql_num_rows()][a],Vh[mysql_num_rows()][color1],Vh[mysql_num_rows()][color1],300);
new query[512];
new l = mysql_num_rows();
format(query,512,"INSERT INTO `vehicles` (modelid,x,y,z,a,color,rank,faction) VALUES (%d,%f,%f,%f,%f,%d,%d,%d)",smodel,Vh[mysql_num_rows()][x],Vh[mysql_num_rows()][y],Vh[mysql_num_rows()][z],Vh[mysql_num_rows()][a],scolor,srank,sfraction);
mysql_free_result();
mysql_query(query);
Vh[l][baseid]=mysql_insert_id();
return 1;
}
if(!strcmp(cmd,"/delcar",true))
{
if(!IsPlayerAdmin(playerid)) return 1;
if(GetVehicleId(GetPlayerVehicleID(playerid))==-1) return SendClientMessage(playerid,-1,"Вам нужно находится во фракционной машине");
new query[512];
format(query,512,"DELETE From `vehicles` where id = %d",Vh[GetVehicleId(GetPlayerVehicleID(playerid))][baseid]);
mysql_query(query);
Vh[GetPlayerVehicleID(playerid)][id] =0;
DestroyVehicle(GetPlayerVehicleID(playerid));
return 1;
}
return 0;
}
BaseGetInt(fieldi[],&permi)
{
MySQLCheck();
new fetch[256];
mysql_fetch_field_row(fetch,fieldi);
permi = strval(fetch);
}
BaseGetFloat(fieldf[],&Float:permf)
{
MySQLCheck();
new fetch[256];
mysql_fetch_field_row(fetch,fieldf);
permf = floatstr(fetch);
}
stock MySQLCheck()
{
if(mysql_ping() == 1)
{
}
else
{
if(!mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_DATABASE,MYSQL_PASSWORD))return print("[MYSQL]:No connect!"),SendClientMessageToAll(-1,"{FFFFFF}Падение базы MySQL"),SendRconCommand("exit");
// mysql_query("SET NAMES cp1251");
// mysql_query("SET CHARACTER SET 'cp1251'");
}
return 1;
}
public OnQueryError( errorid, error[], resultid, extraid, callback[], query[], connectionHandle )
{
printf("[MYSQL ERROR]: [%d] [%s] [%s] [%s]",errorid,error,callback,query);
return 1;
}
strtok(const stringa[], &index)
{
new length = strlen(stringa);
while ((index < length) && (stringa[index] <= ' '))
{
index++;
}
new offset = index;
new result[20];
while ((index < length) && (stringa[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
{
result[index - offset] = stringa[index];
index++;
}
result[index - offset] = EOS;
return result;
}
обращение к mysql:
PHP:
DROP TABLE IF EXISTS `Vehicles`;
CREATE TABLE `Vehicles` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`modelid` int(11) NOT NULL,
`x` float NOT NULL,
`y` float NOT NULL,
`z` float NOT NULL,
`a` float NOT NULL,
`rank` int(11) NOT NULL,
`faction` int(11) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=cp1251;
и то что вписывается в мод: (не обязательно)
Код:
forward public GetPlayerMember(playerid);
forward public GetPlayerRank(playerid);
public GetPlayerMember(playerid)
{
new fraction;
if(PlayerInfo[playerid][pMember] !=0) fraction = PlayerInfo[playerid][pMember];
else if(PlayerInfo[playerid][pLeader] !=0) fraction = PlayerInfo[playerid][pLeader];
return fraction;
}
public GetPlayerRank(playerid)
{
return PlayerInfo[playerid][pRank];
}