From f2ee17e748a2bbdea6afc788dbfc2ab79e453dbf Mon Sep 17 00:00:00 2001 From: rubin Date: Sun, 29 Jan 2023 19:53:48 +0300 Subject: [PATCH] added new function & added folder config & set name config to server-name.json --- mafia-tools.lua | 62 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/mafia-tools.lua b/mafia-tools.lua index be2772a..e879fbf 100644 --- a/mafia-tools.lua +++ b/mafia-tools.lua @@ -12,11 +12,7 @@ function main() lua_thread.create(script_update.main) repeat wait(0) until sampGetCurrentServerName() ~= "SA-MP" repeat wait(0) until sampGetCurrentServerName():find("Samp%-Rp.Ru") or sampGetCurrentServerName():find("SRP") - server = sampGetCurrentServerName():gsub("|", "") - server = (server:find("02") and "Two" or - (server:find("Revo") and "Revolution" or - (server:find("Legacy") and "Legacy" or (server:find("Classic") and "Classic" or - (server:find("Zero Two") and "Two" or ""))))) + local server = getSampRpServerName() if server == "" then thisScript():unload() end @@ -32,26 +28,24 @@ end -->> REQUEST request = {} +request.send = {} +request.base = {} request.loop = function() request.wait = 0 while true do wait(0) if os.time() - request.wait >= 5 then request.wait = os.time() - local ip, port = sampGetCurrentServerAddress() - local _, myid = sampGetPlayerIdByCharHandle(PLAYER_PED) - local myname = sampGetPlayerNickname(myid) - local request_table = { - sender = myname, - server = string.format("%s:%s", ip, port), - room = "test", - send = {} + sender = getLocalPlayerNickname(), + server = getServerAddress(), + room = config.data.room, + send = request.send } + request.send = {} local url = string.format("http://mafia.deadpoo.net/%s", encodeJson(request_table)) local result, text = pcall(openURL, url, os.tmpname(), true) if result then - sampfuncsLog(text) local result = pcall(request.handler, text) if not result then addChatMessage("Сервер 'mafia-tools' не отвечает!") @@ -73,7 +67,9 @@ end ammo_timer = {} ammo_timer.onServerMessage = function(color, message) if message:find("^ Следующее ограбление будет доступно в (%d+:%d+:%d+)") then + request.send[#request.send + 1] = { + } end end ammo_timer.loop = function() @@ -118,15 +114,27 @@ end config = {} config.data = {} config.default = { - time_2min = 0 - + time_2min = 0, + room = "all" } -config.address = string.format("%s\\moonloader\\%s.json", getGameDirectory(), thisScript().name) +config.directory = string.format("%s\\moonloader\\config\\%s\\", getGameDirectory(), thisScript().name) config.init = function() + if not doesDirectoryExist("moonloader\\config") then + createDirectory("moonloader\\config") + end + if not doesDirectoryExist(config.directory) then + createDirectory(config.directory) + end + config.address = string.format("%s\\%s-%s.json", config.directory, getSampRpServerName(), getLocalPlayerNickname()) if not doesFileExist(config.address) then config.save(config.default) end config.read() + for k,v in pairs(config.default) do + if config.data[k] == nil then + config.data[k] = v + end + end config.save(config.data) end config.save = function(data) @@ -172,6 +180,26 @@ function sampev.onServerMessage(color, message) timer_2min.onServerMessage(color, message) end +-->> NEW FUNCTION +function getLocalPlayerNickname() + return sampGetPlayerNickname(select(2, sampGetPlayerIdByCharHandle(PLAYER_PED))) +end +function getServerAddress() + local ip, port = sampGetCurrentServerAddress() + return string.format("%s:%s", ip, port) +end +function getSampRpServerName() + local result = "" + local server = sampGetCurrentServerName():gsub("|", "") + local server_find = { "02", "Two", "Revo", "Legacy", "Classic" } + for i = 1, #server_find do + if server:find(server_find[i]) then + result = server_find[i] + end + end + return result +end + -->> UPDATE MODULE function openURL(url, fpath, message_off) local text = ""