Lua function revisions

This commit is contained in:
Anonymous275 2020-06-22 18:59:48 +03:00
parent 354ff30b90
commit 3d29067cab
5 changed files with 43 additions and 18 deletions

View File

@ -63,11 +63,7 @@ int lua_RegisterEvent(lua_State *L)
Lua* Script = GetScript(L);
if(Args == 2 && lua_isstring(L,1) && lua_isstring(L,2)){
Script->RegisterEvent(lua_tostring(L,1),lua_tostring(L,2));
}else if(Args > 2){
SendError(L,"RegisterEvent too many arguments");
}else if(Args < 2){
SendError(L,"RegisterEvent not enough arguments");
}
}else SendError(L,"RegisterEvent invalid argument count expected 2 got " + std::to_string(Args));
return 0;
}
int lua_TriggerEventL(lua_State *L)
@ -81,7 +77,7 @@ int lua_TriggerEventL(lua_State *L)
SendError(L,"TriggerLocalEvent wrong argument [1] need string");
}
}else{
SendError(L,"TriggerLocalEvent not enough arguments");
SendError(L,"TriggerLocalEvent not enough arguments expected 1 got 0");
}
return 0;
}
@ -170,7 +166,7 @@ int lua_GetDID(lua_State *L){
}
return 1;
}
int lua_GetAllIDs(lua_State *L){
int lua_GetAllPlayers(lua_State *L){
lua_newtable(L);
int i = 1;
for (Client *c : Clients) {
@ -186,15 +182,15 @@ int lua_GetCars(lua_State *L){
if(lua_isnumber(L,1)){
int ID = lua_tonumber(L, 1);
Client*c = GetClient(ID);
if(c != nullptr){
int i = 1;
for (const std::pair<int,std::string> &a : c->GetAllCars()) {
lua_pushinteger(L, a.first);
lua_pushstring(L, a.second.c_str());
lua_pushstring(L, a.second.substr(3).c_str());
lua_settable(L,-3);
i++;
}
if(c->GetAllCars().empty())return 0;
}else return 0;
}else{
SendError(L,"GetPlayerVehicles not enough arguments");
@ -204,11 +200,16 @@ int lua_GetCars(lua_State *L){
}
void Respond(Client*c, const std::string& MSG, bool Rel);
int lua_dropPlayer(lua_State *L){
int Args = lua_gettop(L);
if(lua_isnumber(L,1)){
int ID = lua_tonumber(L, 1);
Client*c = GetClient(ID);
std::string Reason;
if(Args > 1 && lua_isstring(L,2)){
Reason = std::string(" Reason : ")+lua_tostring(L,2);
}
if(c != nullptr){
Respond(c,"C:Server:You have been Kicked from the server!",true);
Respond(c,"C:Server:You have been Kicked from the server!" + Reason,true);
c->SetStatus(-2);
closesocket(c->GetTCPSock());
}
@ -234,11 +235,30 @@ int lua_sendChat(lua_State *L){
}else SendError(L,"SendChatMessage invalid argument [1] expected number");
return 0;
}
int lua_RemoveVehicle(lua_State *L){
int Args = lua_gettop(L);
if(Args != 2){
SendError(L,"RemoveVehicle invalid argument count expected 2 got " + std::to_string(Args));
return 0;
}
if((lua_isinteger(L,1) || lua_isnumber(L,1)) && (lua_isinteger(L,2) || lua_isnumber(L,2))){
int PID = lua_tointeger(L,1);
int VID = lua_tointeger(L,2);
Client *c = GetClient(PID);
if(c != nullptr){
if(!c->GetCarData(VID).empty()){
std::string Destroy = "Od:" + std::to_string(PID)+"-"+std::to_string(VID);
SendToAll(nullptr,Destroy,true,true);
c->DeleteCar(VID);
}
}else SendError(L,"RemoveVehicle invalid Player ID");
}else SendError(L,"RemoveVehicle invalid argument expected number");
return 0;
}
int lua_HWID(lua_State *L){
lua_pushinteger(L, -1);
return 1;
}
void Lua::Init(){
luaL_openlibs(luaState);
lua_register(luaState,"TriggerGlobalEvent",lua_TriggerEventG);
@ -247,12 +267,13 @@ void Lua::Init(){
lua_register(luaState,"isPlayerConnected",lua_isConnected);
lua_register(luaState,"RegisterEvent",lua_RegisterEvent);
lua_register(luaState,"GetPlayerName",lua_GetPlayerName);
lua_register(luaState,"RemoveVehicle",lua_RemoveVehicle);
lua_register(luaState,"GetPlayerDiscordID",lua_GetDID);
lua_register(luaState,"GetPlayerVehicles",lua_GetCars);
lua_register(luaState,"CreateThread",lua_CreateThread);
lua_register(luaState,"SendChatMessage",lua_sendChat);
lua_register(luaState,"GetPlayers",lua_GetAllPlayers);
lua_register(luaState,"DropPlayer",lua_dropPlayer);
lua_register(luaState,"GetPlayers",lua_GetAllIDs);
lua_register(luaState,"GetPlayerHWID",lua_HWID);
lua_register(luaState,"Sleep",lua_Sleep);
Reload();

View File

@ -21,6 +21,7 @@ private:
int Status = 0;
int ID = -1; //PlayerID
public:
bool isDownloading = true;
std::set<std::pair<int,std::string>> GetAllCars();
void AddNewCar(int ident,const std::string& Data);
void SetName(const std::string& name);

View File

@ -36,11 +36,13 @@ void SendToAll(Client*c, const std::string& Data, bool Self, bool Rel){
char C = Data.at(0);
for(Client*client : Clients){
if(Self || client != c){
if(Rel){
if(C == 'C' || C == 'O' || C == 'T' || Data.length() > 1000)SendLarge(client,Data);
else TCPSend(client,Data);
if(!client->isDownloading){
if(Rel){
if(C == 'C' || C == 'O' || C == 'T' || Data.length() > 1000)SendLarge(client,Data);
else TCPSend(client,Data);
}
else UDPSend(client,Data);
}
else UDPSend(client,Data);
}
}
}

View File

@ -114,4 +114,5 @@ bool STCPRecv(Client*c){
}
void SyncResources(Client*c){
while(c->GetStatus() > -1 && STCPRecv(c));
c->isDownloading = false;
}

View File

@ -17,8 +17,8 @@ void ParseConfig();
void addToLog(const std::string& Data);
//void ServerMain(int Port, int MaxClients);
void HeartbeatInit();
std::string ServerVersion = "0.3";
std::string ClientVersion = "1.3+";
std::string ServerVersion = "0.4";
std::string ClientVersion = "1.4+";
std::string CustomIP;
void HandleResources(std::string path);
void StatInit();