From 7247aa1789eea823a410cedc3b17f447f826acaf Mon Sep 17 00:00:00 2001 From: rubin Date: Thu, 2 Feb 2023 17:13:25 +0300 Subject: [PATCH] initial commit --- squad-mats.lua | 272 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 272 insertions(+) create mode 100644 squad-mats.lua diff --git a/squad-mats.lua b/squad-mats.lua new file mode 100644 index 0000000..bcba9ae --- /dev/null +++ b/squad-mats.lua @@ -0,0 +1,272 @@ +script_name('squad-mats') +script_author("Serhiy_Rubin") +script_version("02.02.2023") +sampev = require 'samp.events' +inicfg = require "inicfg" +dlstatus = require("moonloader").download_status + +function main() + if not isSampLoaded() or not isSampfuncsLoaded() then return end + while not isSampAvailable() do wait(0) end + repeat wait(0) until sampGetCurrentServerName() ~= "SA-MP" + repeat wait(0) until sampGetCurrentServerName():find("Samp%-Rp.Ru") or sampGetCurrentServerName():find("SRP") + local server = getSampRpServerName() + if server == "" then + thisScript():unload() + end + config.init() + sampRegisterChatCommand("squadid",function(para) + if para:find("%d+") then + config.data.squad_id = tonumber(para:match("(%d+)")) + config.save(config.data) + addChatMessage("Установлен новый ID сквада: "..config.data.squad_id) + end + end) + wait(-1) +end + +-->> CONFIG +config = {} +config.data = {} +local x1, y1 = convertGameScreenCoordsToWindowScreenCoords(14, 310) +config.default = { + squad_id = -1, + mats = 0, + mats_price = 8 +} +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) + local file, error = io.open(config.address, "w") + if file == nil then + addChatMessage(error) + end + file:write(encodeJson(data)) + file:flush() + io.close(file) +end +config.read = function() + local readJson = function() + local file, error = io.open(config.address, "r") + if file then + config.data = decodeJson(file:read("*a")) + io.close(file) + if config.data == nil then + addChatMessage("Ошибка чтения конфига! Сбрасываю конфиг!") + config.save(config.default) + end + end + end + local result = pcall(readJson) + if not result then + addChatMessage("Ошибка чтения конфига! Сбрасываю конфиг!") + config.save(config.default) + end + if config.data == nil then + config.error = true + addChatMessage("Ошибка чтения конфига! Пробую ещё раз прочесть") + config.read() + else + if config.error then + addChatMessage("Конфиг был успешно загружен!") + config.error = false + end + end +end + +-->> COUNTER +counter = {} +counter.reset = 0 +counter.weapon = { + ["SD Pistol"] = 34, + ["Desert Eagle"] = 63, + ["Shotgun"] = 90, + ["MP5"] = 180, + ["AK-47"] = 180, + ["M4"] = 300, + ["Rifle"] = 300 +} + +dialog = {} +dialog.id = -1 +dialog.text = [[[0] SD Pistol [34 п. 34 м.] +[1] Desert Eagle [21 п. 63 м.] +[2] Shotgun [30 п. 90 м.] +[3] MP5 [90 п. 180 м.] +[4] AK-47 [90 п. 180 м.] +[5] M4 [100 п. 300 м.] +[6] Rifle [30 п. 300 м.] +]] +dialog.convertText = function(text) + text = string.format("%s\n \nВсего потрачено: %d матов\nСтоимость: %d вирт\n> Оплатить сейчас", text, config.data.mats, (config.data.mats * config.data.mats_price)) + return text +end +dialog.reshow = function() + local text = dialog.convertText(dialog.text) + local bs = raknetNewBitStream() + raknetBitStreamWriteInt16(bs, dialog.id) + raknetBitStreamWriteInt8(bs, 2) + raknetBitStreamWriteInt8(bs, 4) + raknetBitStreamWriteString(bs, "Меню") + raknetBitStreamWriteInt8(bs, 7) + raknetBitStreamWriteString(bs, "Выбрать") + raknetBitStreamWriteInt8(bs, 7) + raknetBitStreamWriteString(bs, "Закрыть") + raknetBitStreamWriteInt8(bs, string.len(text)) + raknetBitStreamWriteString(bs, text) + raknetEmulRpcReceiveBitStream(61,bs) + raknetDeleteBitStream(bs) +end + +-->> EVENTS +function sampev.onServerMessage(color, message) + if message:find("%[Офис%]{FFFFFF} Вы взяли {00FFBF}(.+) %(%d+ .+%) {FFFFFF}с оружейного шкафа офиса!") then + local weapon = message:match("%[Офис%]{FFFFFF} Вы взяли {00FFBF}(.+) %(%d+ .+%) {FFFFFF}с оружейного шкафа офиса!") + config.data.mats = config.data.mats + counter.weapon[weapon] + config.save(config.data) + dialog.reshow() + end + if message == " Операция выполнена" and os.time() - counter.reset <= 2 then + addChatMessage("Счетчик материалов обнулен!") + config.data.mats = 0 + config.save(config.data) + end + if message == " Вы не в сообществе" and os.time() - counter.reset <= 2 then + addChatMessage("Укажите ID сообщества командой /squadid [ID]") + end +end +function sampev.onShowDialog(dialogId, style, title, button1, button2, text) + addChatMessage(style) + if title == "Меню" and text == dialog.text then + dialog.id = dialogId + text = dialog.convertText(text) + return { dialogId, style, title, button1, button2, text } + end +end +function sampev.onSendDialogResponse(dialogId, button, listboxId, input) + if listboxId == 10 then + counter.reset = os.time() + sampSendChat("/squad bank "..config.data.squad_id.." "..(config.data.mats * config.data.mats_price)) + return false + end +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 +function convertTableToString(table) + local result = "" + for i = 1, #table do + result = string.format("%s%s\n", result, table[i]) + end + return result +end +function start_dialog(menu, put) -- module by trefa & modify (put & list in []) + function _dialog(menu, id, outs, put) + sampShowDialog(id, menu.settings.title, tbl_split(menu.settings.style, menu, menu.settings.forward ,menu.settings.backwards ,menu.settings.score), menu.settings.btn1, (menu.settings.btn2 ~= nil and menu.settings.btn2 or _), menu.settings.style) + repeat + wait(0) + if put ~= nil and sampIsDialogActive() then + sampSetCurrentDialogEditboxText(put) + put = nil + end + local result, button, list, input = sampHasDialogRespond(id) + if result then + local out, outs = menu[((menu.settings.style == 0 or menu.settings.style == 1 or menu.settings.style == 3) and 1 or ((list + 1) > #menu[1] and 2 or 1))][((menu.settings.style == 0 or menu.settings.style == 1 or menu.settings.style == 3) and 1 or ((list + 1) > #menu[1] and (list - #menu[1]) + 1 or list + 1))].click(button, list, input, outs) + if type(out) == "table" then + return _dialog(out, id - 1, outs, put) + elseif type(out) == "boolean" then + if not out then + return out + end + return _dialog(menu, id, outs, put) + end + end + until result + end + + function tbl_split(style, tbl, forward ,backwards ,score) + if style == 2 or style == 4 or style == 5 then + text = (style == 5 and tbl[1].text.."\n" or "") + for i, val in ipairs(tbl[1]) do + text = text..""..forward..""..(score and "["..(i-1).."] " or "")..""..val.title..""..backwards + end + if tbl[2] ~= nil then + for _, val in ipairs(tbl[2]) do + text = text..""..forward..""..val.title..""..backwards + end + end + return text + end + return tbl[1].text + end + + return _dialog(menu, 1337, outs, put) +end +function getNicknamesOnline() + local result = {} + for i = 0, sampGetMaxPlayerId(false) do + if sampIsPlayerConnected(i) or select(2, sampGetPlayerIdByCharHandle(PLAYER_PED)) == i then + result[sampGetPlayerNickname(i)] = i + end + end + return result +end +function addChatMessage(text) + local tag = string.format("{667dff}[%s]{FFFFFF} ", thisScript().name) + sampAddChatMessage(tag..text, 0xFFFFFFFF) +end + +-->> SCRIPT UTF-8 +-->> utf8(table path, incoming variables encoding, outcoming variables encoding) +-->> table path example { "sampev", "onShowDialog" } +-->> encoding options nil | AnsiToUtf8 | Utf8ToAnsi +_utf8 = load([=[return function(utf8_func, in_encoding, out_encoding); if encoding == nil then; encoding = require("encoding"); encoding.default = "CP1251"; u8 = encoding.UTF8; end; if type(utf8_func) ~= "table" then; return false; end; if AnsiToUtf8 == nil or Utf8ToAnsi == nil then; AnsiToUtf8 = function(text); return u8(text); end; Utf8ToAnsi = function(text); return u8:decode(text); end; end; if _UTF8_FUNCTION_SAVE == nil then; _UTF8_FUNCTION_SAVE = {}; end; local change_var = "_G"; for s = 1, #utf8_func do; change_var = string.format('%s["%s"]', change_var, utf8_func[s]); end; if _UTF8_FUNCTION_SAVE[change_var] == nil then; _UTF8_FUNCTION = function(...); local pack = table.pack(...); readTable = function(t, enc); for k, v in next, t do; if type(v) == 'table' then; readTable(v, enc); else; if enc ~= nil and (enc == "AnsiToUtf8" or enc == "Utf8ToAnsi") then; if type(k) == "string" then; k = _G[enc](k); end; if type(v) == "string" then; t[k] = _G[enc](v); end; end; end; end; return t; end; return table.unpack(readTable({_UTF8_FUNCTION_SAVE[change_var](table.unpack(readTable(pack, in_encoding)))}, out_encoding)); end; local text = string.format("_UTF8_FUNCTION_SAVE['%s'] = %s; %s = _UTF8_FUNCTION;", change_var, change_var, change_var); load(text)(); _UTF8_FUNCTION = nil; end; return true; end]=]) +function utf8(...) + pcall(_utf8(), ...) +end + +utf8({ "sampShowDialog" }, "Utf8ToAnsi") +utf8({ "sampSendChat" }, "Utf8ToAnsi") +utf8({ "sampAddChatMessage" }, "Utf8ToAnsi") +utf8({ "print" }, "Utf8ToAnsi") +utf8({ "renderGetFontDrawTextLength" }, "Utf8ToAnsi") +utf8({ "renderFontDrawText" }, "Utf8ToAnsi") +utf8({ "sampHasDialogRespond" }, nil, "AnsiToUtf8") +utf8({ "sampev", "onServerMessage" }, "AnsiToUtf8", "Utf8ToAnsi") +utf8({ "sampev", "onShowDialog" }, "AnsiToUtf8", "Utf8ToAnsi") +utf8({ "sampev", "onSendDialogResponse" }, "AnsiToUtf8", "Utf8ToAnsi") \ No newline at end of file