优化下列代码 if arg == 101 then if GetMenPai(sceneId, selfId) ~= 9 then x990010_MsgBox( sceneId, selfId, targetId, "你已是别的门派的高徒了,我们不收你。" ) return end LuaFnJoinMenpai(sceneId, selfId, targetId, MP_XINGSU) LuaFnSetXinFaLevel(sceneId,selfId,31,30) LuaFnSetXinFaLevel(sceneId,selfId,32,30) LuaFnSetXinFaLevel(sceneId,selfId,33,30) LuaFnSetXinFaLevel(sceneId,selfId,34,30) LuaFnSetXinFaLevel(sceneId,selfId,35,30) LuaFnSetXinFaLevel(sceneId,selfId,36,30) --LuaFnSetXinFaLevel(sceneId,selfId,60,60) --LuaFnSetXinFaLevel(sceneId,selfId,77,60) LuaFnSendSpecificImpactToUnit(sceneId, selfId, selfId, selfId, 148, 0) local nam = LuaFnGetName( sceneId, selfId ) BroadMsgByChatPipe( sceneId, selfId, "#cFF0000[门派公告]:#c66ccff["..nam.."]#P已经加入了"..x990010_g_mpInfo[arg-101][1].."!", 4 ) CallScriptFunction( 999994, "ActionSkill",sceneId, selfId, -1 ) x990010_ooo( sceneId, selfId) end if arg == 102 then if GetMenPai(sceneId, selfId) ~= 9 then x990010_MsgBox( sceneId, selfId, targetId, "你已是别的门派的高徒了,我们不收你。" ) return end LuaFnJoinMenpai(sceneId, selfId, targetId, MP_XIAOYAO) LuaFnSetXinFaLevel(sceneId,selfId,49,30) LuaFnSetXinFaLevel(sceneId,selfId,50,30) LuaFnSetXinFaLevel(sceneId,selfId,51,30) LuaFnSetXinFaLevel(sceneId,selfId,52,30) LuaFnSetXinFaLevel(sceneId,selfId,53,30) LuaFnSetXinFaLevel(sceneId,selfId,54,30) --LuaFnSetXinFaLevel(sceneId,selfId,63,60) --LuaFnSetXinFaLevel(sceneId,selfId,80,60) LuaFnSendSpecificImpactToUnit(sceneId, selfId, selfId, selfId, 148, 0) local nam = LuaFnGetName( sceneId, selfId ) BroadMsgByChatPipe( sceneId, selfId, "#cFF0000[门派公告]:#c66ccff["..nam.."]#P已经加入了"..x990010_g_mpInfo[arg-101][1].."!", 4 ) x990010_ooo( sceneId, selfId) CallScriptFunction( 999994, "ActionSkill",sceneId, selfId, -1 ) end
时间: 2023-07-14 09:12:37 浏览: 142
优化建议如下:
1. 使用函数封装重复的代码,避免代码冗余和增加维护难度。
2. 将常量提取到变量中,方便统一管理和修改。
3. 使用字典代替 if-else 分支语句,提高代码可读性和可维护性。
4. 使用局部变量缓存重复调用的函数返回值,提高代码效率。
重构后的代码如下:
```
local MENPAI_INFO = {
[101] = {name = "星宿派", xinfas = {31, 32, 33, 34, 35, 36}},
[102] = {name = "逍遥派", xinfas = {49, 50, 51, 52, 53, 54}},
}
local function joinMenpai(sceneId, selfId, targetId, menpai)
if GetMenPai(sceneId, selfId) ~= 9 then
x990010_MsgBox(sceneId, selfId, targetId, "你已是别的门派的高徒了,我们不收你。")
return false
end
LuaFnJoinMenpai(sceneId, selfId, targetId, menpai)
for _, xinfaid in ipairs(MENPAI_INFO[menpai].xinfas) do
LuaFnSetXinFaLevel(sceneId, selfId, xinfaid, 30)
end
LuaFnSendSpecificImpactToUnit(sceneId, selfId, selfId, selfId, 148, 0)
local name = LuaFnGetName(sceneId, selfId)
BroadMsgByChatPipe(sceneId, selfId, string.format("#cFF0000[门派公告]:#c66ccff[%s]#P已经加入了%s!", name, MENPAI_INFO[menpai].name), 4)
x990010_ooo(sceneId, selfId)
CallScriptFunction(999994, "ActionSkill", sceneId, selfId, -1)
return true
end
if arg == 101 then
joinMenpai(sceneId, selfId, targetId, 101)
end
if arg == 102 then
joinMenpai(sceneId, selfId, targetId, 102)
end
```
阅读全文