Mobile Arizona Парсинг чата для подсчёта дохода/расхода

Annanel

Участник
Автор темы
88
8
Пытаюсь сделать перехват строки на мобильной версии чтобы можно было подсчитать доход расход но это почему-то не получается

11498.jpg


Lua:
local imgui = require 'mimgui'
local encoding = require 'encoding'
local sampev = require 'samp.events'

encoding.default = 'CP1251'
local u8 = encoding.UTF8

-- Переменные
local income = 0
local expense = 0
local show_window = imgui.new.bool(true)

-- Функция очистки суммы (удаляет все, кроме цифр)
function cleanMoney(str)
    if not str then return 0 end
    local cleaned = str:gsub("%D", "") -- Оставляет только цифры
    return tonumber(cleaned) or 0
end

-- Красивое форматирование (120000 -> 120,000)
function formatMoney(amount)
    local formatted = tostring(amount)
    while true do
        formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
        if (k==0) then break end
    end
    return formatted
end

imgui.OnFrame(function() return show_window end, function(player)
    imgui.SetNextWindowSize(imgui.ImVec2(250, 160), imgui.Cond.FirstUseEver)
    imgui.Begin("Arizona Tracker", show_window)
   
    imgui.Text("Income: $" .. formatMoney(income))
    imgui.Text("Expense: $" .. formatMoney(expense))
   
    local profit = income - expense
    imgui.Separator()
   
    -- Подсветка профита
    local color = profit >= 0 and imgui.ImVec4(0, 1, 0, 1) or imgui.ImVec4(1, 0, 0, 1)
    imgui.TextColored(color, "Profit: $" .. formatMoney(profit))
   
    imgui.Separator()
    if imgui.Button("Reset Statistics", imgui.ImVec2(-1, 30)) then
        income, expense = 0, 0
    end
   
    imgui.End()
end)

function main()
    while not isSampAvailable() do wait(100) end
    sampRegisterChatCommand("money", function() show_window = not show_window end)
end

-- Основной обработчик сообщений
function sampev.onServerMessage(color, text)
    -- Убираем цветовые коды типа {FFFFFF}
    local cleanText = text:gsub("{%x%x%x%x%x%x}", "")

    -- 1. ЛОВИМ РАСХОД
    -- Паттерн ищет "за $" и любые цифры с точками/запятыми после
    if cleanText:find("купили") and cleanText:find("за %$") then
        local sumStr = cleanText:match("за %$([%d%.,%s]+)")
        if sumStr then
            expense = expense + cleanMoney(sumStr)
        end
    end

    -- 2. ЛОВИМ ДОХОД (PayDay)
    if cleanText:find("Зарплата:") then
        local sumStr = cleanText:match("Зарплата:.-(%d[%d%.,]*)")
        if sumStr then
            income = income + cleanMoney(sumStr)
        end
    end

    -- 3. ЛОВИМ ДОХОД (Работы / Переводы)
    if cleanText:find("получили") and cleanText:find("%$") then
        local sumStr = cleanText:match("([%d%.,]+)%$") or cleanText:match("получили ([%d%.,]+)")
        if sumStr then
            income = income + cleanMoney(sumStr)
        end
    end
end