楼主: k621418

[通用] lua实现转职--已测试成功--求改进意见

[复制链接]

224

时沙

0

精华

9

主题

声望: 660   虚弱: 0

Lv.4(锻造者)

炉石

发表于 2024-11-19 10:20:28 | 显示全部楼层
学习 ,学习 。谢谢分享
回复

使用道具 举报

224

时沙

0

精华

9

主题

声望: 660   虚弱: 0

Lv.4(锻造者)

炉石

发表于 2024-11-19 10:26:17 | 显示全部楼层
-- 定义常量
local ITEM_ENTRY = 30001
local TRANSFER_TABLE = "_zhuanzhi"
local MAX_SKILL_LEVEL = 34000
local PROFESSION_SKILLS = {171, 164, 333, 202, 182, 165, 186, 197, 356, 393, 185, 129}

-- 安全执行数据库查询函数
local function safeQuery(db, query,...)
    local result = db:Query(query,...)
    if not result then
        print("数据库查询错误: ".. db:GetLastError())
        -- 这里可以根据实际情况决定如何处理错误,比如向玩家发送错误消息等
    end
    return result
end

-- 处理角色信息更新的函数
local function updateCharacterInfo(db, targetGuid, player)
    local race = player:GetRace()
    local class = player:GetClass()
    local gender = player:GetGender()
    local skin = player:GetSkin()
    local face = player:GetFace()
    local hairStyle = player:GetHairStyle()
    local hairColor = player:GetHairColor()
    local facialHair = player:GetFacialHair()

    safeQuery(db, "UPDATE characters set race =?, class =?, gender =?, skin =?, face =?, hair_style =?, hair_color =?, facial_hair =? where guid =?",
              race, class, gender, skin, face, hairStyle, hairColor, facialHair, targetGuid)
end

-- 清理角色技能的函数
local function clearCharacterSkills(db, targetGuid)
    safeQuery(db, "delete from character_spell where guid =? and spell <?", targetGuid, MAX_SKILL_LEVEL)
    local skills = safeQuery(db, "select * from character_skills where guid =?", targetGuid)
    if skills then
        local skillId = skills:GetUInt32(1)
        for _, v in pairs(PROFESSION_SKILLS) do
            if skillId ~= v then
                safeQuery(db, "delete from character_skills where guid =? and skill =?", targetGuid, skillId)
            end
        end
    end
end

-- 主要的Lua函数
local function lushi(event, player, item, target)
    local accountId = player:GetAccountId()
    local db = CharDB()

    -- 清空转账表中该账号的数据
    safeQuery(db, "delete from ".. TRANSFER_TABLE.. " where AC =?", accountId)

    player:GossipClearMenu()
    local characterGuids = safeQuery(db, "select guid from characters where account =?", accountId)
    local menuIndex = 1
    if characterGuids then
        for row in characterGuids:Iterator() do
            local guid = row:GetUInt32(0)
            local characterInfo = safeQuery(db, "select * from characters where guid =?", guid)
            if characterInfo then
                safeQuery(db, "INSERT INTO ".. TRANSFER_TABLE.. " VALUES(?,?,?)", menuIndex, guid, accountId)
                menuIndex = menuIndex + 1
            end
        end
    end

    -- 添加转职说明到菜单
    player:GossipMenuAddItem(0, "自助转职说明:", 0, 21)
    player:GossipMenuAddItem(0, "1.转职会清空职业技能和所有图纸。", 0, 22)
    player:GossipMenuAddItem(0, "2.转职会保留专业熟练度和所有被动收益", 0, 23)
    player:GossipMenuAddItem(0, "3.转职后不匹配的装备通过邮箱返还", 0, 24)
    player:GossipMenuAddItem(0, "4.转职不限制种族,职业,次数", 0, 24)
    player:GossipMenuAddItem(0, "5.转职后克隆当前角色的职业和外观", 0, 24)
    player:GossipMenuAddItem(0, "6.使用步骤:比如我现在是兽人战士,我想转职成矮人骑士,先建立一个矮人骑士,将这个转职凭证邮寄给矮人骑士,矮人骑士使用这个凭证后选择兽人战士,那么兽人战士就将转职成矮人骑士", 0, 24)
    player:GossipMenuAddItem(0, "                 >我已知晓,点击转职<", 0, 0)
    player:GossipSendMenu(1, item)
end

local function ResetMap_Select(event, player, item, sender, intid, code)
    local accountId = player:GetAccountId()
    local db = CharDB()
    local characterGuids = safeQuery(db, "select guid from characters where account =?", accountId)

    if intid == 0 then
        player:GossipMenuAddItem(0, "点击下方角色名字选项即可完成转职    不要选当前角色为转职对象,无效!", 0, 20)
        local characterCount = 0
        if characterGuids then
            characterCount = characterGuids:GetRowCount()
            for i = 1, characterCount do
                local guid = characterGuids:GetUInt32(i - 1)  -- 注意这里的索引从0开始
                local characterInfo = safeQuery(db, "select * from characters where guid =?", guid)
                if characterInfo then
                    player:GossipMenuAddItem(0, "第".. i.."个角色>>>".. characterInfo:GetString(2), 0, i)
                end
            end
        end
        player:GossipSendMenu(1, item)
    else
        local currentGuid = player:GetGUIDLow()
        local currentCharacterInfo = safeQuery(db, "select * from characters where guid =?", currentGuid)
        if currentCharacterInfo then
            local targetCharacterRow = safeQuery(db, "select * from ".. TRANSFER_TABLE.. " where ID =? and AC =?", intid, accountId)
            if targetCharacterRow then
                local targetGuid = targetCharacterRow:GetUInt32(1)
                updateCharacterInfo(db, targetGuid, player)
                clearCharacterSkills(db, targetGuid)
                SendWorldMessage("转职已完成,小退查看。")
                player:GossipComplete()
            end
        end
    end
end

-- 注册事件
RegisterItemGossipEvent(ITEM_ENTRY, 1, lushi)
RegisterItemGossipEvent(ITEM_ENTRY, 2, ResetMap_Select)




试试这个 AI 优化的!!!
[发帖际遇]: long910905为牛头人酋长贝恩捉虱,获得小费10 金币. 幸运榜 / 衰神榜
回复

使用道具 举报

227

时沙

0

精华

3

主题

声望: 696   虚弱: 0

Lv.4(锻造者)

一个渴求知识的迷路人~

炉石

发表于 2024-11-19 10:58:42 | 显示全部楼层
谢谢你的分享。。
回复

使用道具 举报

发表于 2024-11-24 12:43:43 | 显示全部楼层
感谢大佬
[发帖际遇]: 行者推倒一粒蛋,获得了狗蛋之颅,熬了一锅汤趁热喝下,获得1 声望. 幸运榜 / 衰神榜
回复

使用道具 举报

489

时沙

0

精华

18

主题

声望: 1521   虚弱: 0

Lv.5(无冕者)

zhutou

瓦格里的标记

发表于 2024-11-29 09:23:21 | 显示全部楼层
我一运转核心就崩,咋回事呢?
回复

使用道具 举报

489

时沙

0

精华

18

主题

声望: 1521   虚弱: 0

Lv.5(无冕者)

zhutou

瓦格里的标记

发表于 2024-11-29 09:34:21 | 显示全部楼层
long910905 发表于 2024-11-19 10:26
-- 定义常量
local ITEM_ENTRY = 30001
local TRANSFER_TABLE = "_zhuanzhi"

你这个点了没反应。。。
回复

使用道具 举报

477

时沙

0

精华

0

主题

声望: 106   虚弱: 0

Lv.3(忠诚者)

发表于 2024-12-6 19:33:07 | 显示全部楼层
学习代码
回复

使用道具 举报

434

时沙

0

精华

2

主题

声望: 257   虚弱: 0

Lv.3(忠诚者)

发表于 2024-12-7 12:14:39 | 显示全部楼层
好东西厉害,谢谢分享

[发帖际遇]: whb1688使用绝技三百六十度倒立脸滚键盘打出团队第一DPS,获得2 声望. 幸运榜 / 衰神榜
回复

使用道具 举报

744

时沙

0

精华

13

主题

声望: 424   虚弱: 0

Lv.4(锻造者)

发表于 2025-1-10 03:21:03 | 显示全部楼层
学习学习
[发帖际遇]: ruchengxin123推倒一粒蛋,获得了狗蛋之颅,熬了一锅汤趁热喝下,获得2 声望. 幸运榜 / 衰神榜
回复

使用道具 举报

211

时沙

0

精华

1

主题

声望: 102   虚弱: 1

Lv.2(潜行者)

炉石

发表于 2025-2-24 12:47:28 | 显示全部楼层
谢谢分享,数据库怎么改职业啊
回复

使用道具 举报

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