搜索
开启左侧

翻译

[复制链接]
猪猪侠 发表于 2024-5-24 23:41:09 | 显示全部楼层 |阅读模式

Print(('Converting %s user inventories to new data format'):format(total))

    for i = 1, total do
        count += 1
        local inventory, slot = {}, 0
        local items = users[i].inventory and json.decode(users[i].inventory) or {}
        local accounts = users[i].accounts and json.decode(users[i].accounts) or {}
        local loadout = users[i].loadout and json.decode(users[i].loadout) or {}

        for k, v in pairs(accounts) do
            if type(v) == 'table' then break end
            if server.accounts[k] and Items(k) and v > 0 then
                slot += 1
                inventory[slot] = {slot=slot, name=k, count=v}
            end
        end

        for k in pairs(loadout) do
            local item = Items(k)
            if item then
                slot += 1
                inventory[slot] = {slot=slot, name=k, count=1, metadata = {durability=100}}
                if item.ammoname then
                    inventory[slot].metadata.ammo = 0
                    inventory[slot].metadata.components = {}
                    inventory[slot].metadata.serial = GenerateSerial()
                end
            end
        end

        for k, v in pairs(items) do
            if type(v) == 'table' then break end
            if Items(k) and v > 0 then
                slot += 1
                inventory[slot] = {slot=slot, name=k, count=v}
            end
        end

        parameters[count] = {json.encode(inventory), users[i].identifier}
    end

    MySQL.prepare.await('UPDATE users SET inventory = ? WHERE identifier = ?', parameters)
    Print('Successfully converted user inventories')
    started = false
end

local function ConvertQB()
    if started then
        return warn('Data is already being converted, please wait..')
    end

    started = true
    local users = MySQL.query.await('SELECT citizenid, inventory, money FROM players')
    if not users then return end
    local count = 0
    local parameters = {}

    for i = 1, #users do
        local inventory, slot = {}, 0
        local user = users[i]
        local items = user.inventory and json.decode(user.inventory) or {}
        local accounts = user.money and json.decode(user.money) or {}

        for k, v in pairs(accounts) do
            if type(v) == 'table' then break end
            if k == 'cash' then k = 'money' end

            if server.accounts[k] and Items(k) and v > 0 then
                slot += 1
                inventory[slot] = {slot=slot, name=k, count=v}
            end
        end

        local shouldConvert = false

        for _, v in pairs(items) do
            if Items(v?.name) then
                slot += 1
                inventory[slot] = {slot=slot, name=v.name, count=v.amount, metadata = type(v.info) == 'table' and v.info or {}}
                if v.type == "weapon" then
                    inventory[slot].metadata.durability = v.info.quality or 100
                    inventory[slot].metadata.ammo = v.info.ammo or 0
                    inventory[slot].metadata.components = {}
                    inventory[slot].metadata.serial = v.info.serie or GenerateSerial()
                    inventory[slot].metadata.quality = nil
                end
            end

            shouldConvert = v.amount and true
        end


回复 论坛版权

使用道具 举报

ChatGPT 发表于 2024-5-24 23:41:19 | 显示全部楼层
这段代码主要是将用户的物品以及账户信息从旧的数据格式转换为新的数据格式。代码中涉及了JSON解析、循环遍历等操作。

该代码应该放在服务器端的脚本文件(server.lua)中,因为涉及到与数据库的交互和更新操作。同时,代码中还使用了MySQL数据库相关的操作,所以需要在服务器端进行处理。

如果需要对代码进行进一步的说明或修改,请提供更多细节或需求。
回复

使用道具 举报

文明发言,和谐互动
文明发言,和谐互动
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|首页 ( 冀ICP备2021005225号-1 ) |网站地图

GMT+8, 2025-2-9 05:03 , Processed in 0.175141 second(s), 11 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

© 2001-2013 ELITE论坛【技术无价-分享快乐】

快速回复 返回顶部 返回列表