复制内容到剪贴板
代码:
local limit_time = 10; --两次发喇叭间隔时间,单位秒
--local laba_itemid = 361000; --小喇叭的道具编号
local turn_on_key = "/世界喇叭"; --玩家开启/关闭喇叭开关的命令
-- ***************************************************************************************************** --
tbl_labaPlayer = {};
Delegate.RegDelLoginEvent("labaLoginEvent");
Delegate.RegDelAllOutEvent("labaOutEvent");
--Delegate.RegDelTalkEvent("labaTalkEvent");
--Delegate.RegDelTalkEvent("ng_TalkEvent");
ProtocolDelegate.on(%RECV_TK%,"RECV_TK_Event");--Protocol.OnRecv(nil,"RECV_TK_Event",%RECV_TK%);
function new_labaplayerobject(player)
local labaplayer =
{
index = player;
isopen = true;
limit_time = 0;
VIP = 0;
};
return labaplayer;
end
function labaLoginEvent(player)
tbl_labaPlayer[Playerkey(player)] = new_labaplayerobject(player);
local playerName = Char.GetData(player,%对象_名字%);
local playerCDK = Char.GetData(player,%对象_账号%);
local playerWZ = Char.GetData(player,48);
local playerLv = Char.GetData(player,%对象_等级%);
local playerJob = Char.GetData(player,%对象_职业%);
local ZhanLi = playerLv*10;
for i=0,7 do
local _itemindex = Char.GetItemIndex(player,i);
if(_itemindex>0)then
ZhanLi = ZhanLi+math.floor(tonumber(Item.GetData(_itemindex,%道具_HP%))/10);
ZhanLi = ZhanLi+math.floor(tonumber(Item.GetData(_itemindex,%道具_MP%))/5);
ZhanLi = ZhanLi+math.floor(tonumber(Item.GetData(_itemindex,%道具_攻击%))/2);
ZhanLi = ZhanLi+math.floor(tonumber(Item.GetData(_itemindex,%道具_防御%))*0.6);
ZhanLi = ZhanLi+math.floor(tonumber(Item.GetData(_itemindex,%道具_敏捷%))*0.6);
ZhanLi = ZhanLi+math.floor(tonumber(Item.GetData(_itemindex,%道具_闪躲%))*8);
ZhanLi = ZhanLi+math.floor(tonumber(Item.GetData(_itemindex,%道具_命中%))*12);
ZhanLi = ZhanLi+math.floor(tonumber(Item.GetData(_itemindex,%道具_必杀%))*12);
ZhanLi = ZhanLi+math.floor(tonumber(Item.GetData(_itemindex,%道具_反击%))*10);
ZhanLi = ZhanLi+math.floor(tonumber(Item.GetData(_itemindex,%道具_精神%))*10);
ZhanLi = ZhanLi+math.floor(tonumber(Item.GetData(_itemindex,%道具_回复%))*10);
ZhanLi = ZhanLi+math.floor(tonumber(Item.GetData(_itemindex,%道具_毒抗%))*10);
ZhanLi = ZhanLi+math.floor(tonumber(Item.GetData(_itemindex,%道具_睡抗%))*10);
ZhanLi = ZhanLi+math.floor(tonumber(Item.GetData(_itemindex,%道具_石抗%))*10);
ZhanLi = ZhanLi+math.floor(tonumber(Item.GetData(_itemindex,%道具_醉抗%))*10);
ZhanLi = ZhanLi+math.floor(tonumber(Item.GetData(_itemindex,%道具_乱抗%))*10);
ZhanLi = ZhanLi+math.floor(tonumber(Item.GetData(_itemindex,%道具_忘抗%))*10);
ZhanLi = ZhanLi+math.floor(tonumber(Item.GetData(_itemindex,%道具_属性一值%))*10);
ZhanLi = ZhanLi+math.floor(tonumber(Item.GetData(_itemindex,%道具_属性二值%))*10);
end
end
local query = "SELECT Reward,Name,VipLevel FROM tbl_char WHERE CDK='"..playerCDK.."' And Pos='"..playerWZ.."' Limit 1";
local resset = SQL.Run(query);
if(type(resset)=="table")then--如果返回结果是table类型的值
if (resset["0_0"]~="") then
NLG.SystemMessage(-1, "欢迎" .. resset["0_0"] ..":".. resset["0_1"] .. "上线了!")
end
tbl_labaPlayer[Playerkey(player)].VIP = tonumber(resset["0_2"]);
local query = "Update tbl_char Set IP = '0',Name = '"..playerName.."',Job="..playerJob..",ZhanLi="..ZhanLi.." WHERE CDK='"..playerCDK.."' And Pos="..playerWZ.."";
local resset = SQL.Run(query);
else
local query = "INSERT INTO tbl_char (CDK,Name,Pos,TitleName,IP) VALUES ('"..playerCDK.."','"..playerName.."',"..playerWZ..",'|','0')";
local resset = SQL.Run(query);
tbl_labaPlayer[Playerkey(player)].VIP = 0;
end
end
function labaOutEvent(player)
tbl_labaPlayer[Playerkey(player)] = nil;
local playerCDK = Char.GetData(player,%对象_CDK%);
local playerWZ = Char.GetData(player,48);
local query = "Update tbl_char Set IP = '' WHERE CDK='"..playerCDK.."' And Pos="..playerWZ.."";
local resset = SQL.Run(query);
end
function RECV_TK_Event( _fd, _head, _packet)
local _player = Protocol.GetCharByFd(_fd);
local pos1,pos2 = string.find(_packet,"|");
local _str1 = string.sub(_packet,pos1+1);
if(_player == -1)then
return 0;
end;
if string.find(_str1,"^/t") then
local msg2 = Split(_str1,"t");
local msg3 = Split(msg2[2],":");
local msg4 = "";
if string.find(msg3[1],"S") then
else
msg4 = string.sub(msg3[1], 1);
if CheckNumber(msg4) then
NLG.SystemMessage(_player,"离线挂机开启。");
NLG.SetOfflinePlayer(_player, msg4);
else
NLG.SystemMessage(_player,"输入的时间必须为数字!(单位:秒,最小为1秒,最大为10800秒)");
end
end
if string.find(msg3[1],"^S") then
else
msg4 = string.sub(msg3[1], 3);
if CheckNumber(msg4) then
NLG.SystemMessage(_player,"离线挂机开启。");
NLG.SetOfflinePlayer(_player, msg4);
else
NLG.SystemMessage(_player,"输入的时间必须为数字!(单位:秒,最小为1秒,最大为10800秒)");
end
end
return 1;
end
if string.find(_str1,"^/item") or string.find(_str1,"^/整理") or string.find(_str1,"^/整理包包") then
NLG.SystemMessage(_player,"包包已經整理。");
NLG.SortItem(_player);
return 1;
end
if string.find(_str1,"^/1") or string.find(_str1,"^/步步遇敌") then -- or string.find(_str1,"^"..turn_off_key.."")
local getXiangVar1 = Char.GetData(_player,%对象_香步数%);
local getXiangVar2 = Char.GetData(_player,%对象_香上限%);
if(getXiangVar1 > 0)then
Char.SetData(_player,%对象_香步数%,0);
Char.SetData(_player,%对象_香上限%,0);
NLG.SystemMessage(_player,"步步遇敌已经关闭!");
else
Char.SetData(_player,%对象_香步数%,999);
Char.SetData(_player,%对象_香上限%,999);
NLG.SystemMessage(_player,"步步遇敌已经开启!");
end
return 1;
end
if string.find(_str1,"^/2") or string.find(_str1,"^/不遇敌") then
local kg = Char.GetData(_player,%对象_不遇敌开关%);
if(kg == 0)then
Char.SetData(_player,%对象_不遇敌开关%,1);
NLG.SystemMessage(_player,"不遇敌已经开启!");
else
Char.SetData(_player,%对象_不遇敌开关%,0);
NLG.SystemMessage(_player,"不遇敌已经关闭!");
end
return 1;
end
if string.find(_str1,"^"..turn_on_key.."") and (tbl_labaPlayer[Playerkey(_player)].isopen == false or tbl_labaPlayer[Playerkey(_player)].isopen == nil) then
tbl_labaPlayer[Playerkey(_player)].isopen = true;
NLG.SystemMessage(_player,"世界喇叭功能已经开启。");
return 1;
end
if string.find(_str1,"^"..turn_on_key.."") and tbl_labaPlayer[Playerkey(_player)].isopen == true then
tbl_labaPlayer[Playerkey(_player)].isopen = false;
NLG.SystemMessage(_player,"世界喇叭功能已经关闭。");
return 1;
end
if string.find(_str1,"^>") and tbl_labaPlayer[Playerkey(_player)].isopen == true then
local myLabaNum = fieldget(_player,"LabaNum");
local litime = tbl_labaPlayer[Playerkey(_player)].limit_time;
if (os.time() - litime < limit_time )then
NLG.SystemMessage(_player,limit_time.."秒内禁止重复发喇叭。");
return 1;
end
if(myLabaNum > 0)then
local msg2 = Split(_str1,":");
local msg3 = string.sub(msg2[1],2);
NLG.SystemMessage(-1,"[世界喇叭]"..Char.GetData(_player,%对象_名字%)..": "..msg3);
fieldadd(_player,"LabaNum",-1);
myLabaNum = fieldget(_player,"LabaNum");
NLG.SystemMessage(_player,"世界喇叭发送成功!"..Char.GetData(_player,%对象_名字%).."身上还剩"..myLabaNum.."个喇叭。");
tbl_labaPlayer[Playerkey(_player)].limit_time = os.time();
else
NLG.SystemMessage(_player,"对不起您的喇叭不足!请使用喇叭道具进行充值!");
end
return 1;
end
if string.find(_str1,"^>") and (tbl_labaPlayer[Playerkey(_player)].isopen == false or tbl_labaPlayer[Playerkey(_player)].isopen == nil) then
NLG.SystemMessage(_player,"对不起您没有开启世界喇叭功能!");
return 1;
end
return 0; -- 通过该封包
end
--[[如果str仅由数字组成,则返回true,否则返回false。]]
function CheckNumber(_data,_max,_min)
if _max == nil then
_max = 10800;
end
if _min == nil then
_min = 1;
end
if string.find(_data,"^[-]?[1-9]%d*$") then
if tonumber(_data) > _max then
return false;
elseif tonumber(_data) < _min then
return false;
else
return true;
end
else
return false;
end
end
这样设置的tk的效果是可以不显示/1/2之类的东东的哦。你的不支持功能复用,看我的这个再改改吧。下边是封包的复用。
复制内容到剪贴板
代码:
--[[
将本文件放置于Module目录,并在Config.lua加入如下内容:
useModule("OnRecv");--封包复用 --注:无需添加文件后缀".lua"并放于最上边
--]]
if (_G.ProtocolDelegate) then
return _G.ProtocolDelegate;
end
local Module = {}
--注册全局函数
_G.ikki_OnRecv = function(Fd, Head, Packet)
return Module.emit(Head, Fd, Head, Packet);
end
--注册全局变量
_G.ProtocolDelegate = Module;
--错误处理函数
local hanldeError = _G.hanldeError;
if (hanldeError == nil) then
hanldeError = function(e)
print("runtime error: " .. (e or "") .. "\n" .. debug.traceback())
return e;
end
end
--注册事件
function Module.on(event, fn)
print("On package: " .. event, fn);
if (Module["_" .. event] == nil) then --第一次注册封包函数
Module["_" .. event] = {}
if (Module["_m_" .. event] ~= true) then --第一次注册Protocol
Module["_m_" .. event] = true
Protocol.OnRecv(nil, "ikki_OnRecv", event);
end
end
local m = Module["_" .. event];
for i, v in ipairs(m) do
if (v == fn) then
return
end
end
table.insert(m, fn);
end
--反注册事件
function Module.off(event, fn)
if (Module["_" .. event] ~= nil) then
for i, v in ipairs(Module["_" .. event]) do
if (v == fn) then
table.remove(Module["_" .. event], i)
return
end
end
end
end
--反注册所有
function Module.offAll(event)
Module["_" .. event] = nil;
end
--触发事件
function Module.emit(event, fd, head, data)
if (event == nil) then
print("emit package is nil\n" .. debug.traceback())
end
--print("emit package: " .. event, fd, head, data);
local ret = {};
if (Module["_" .. event]) then
for i, fn in ipairs(Module["_" .. event]) do --循环处理
if (ret[2] == true) then --阻止继续调用
break;
end
local r, e = xpcall(function(fn, ret, fd, head, data) --xpcall 没有fn的return值只好用个ret保存了,顺便加个break变量
if (type(fn)=="string") then --兼容字符串
fn = _G[fn]
end
ret[1] = fn(fd, head, data, ret); --调用注册进来的函数
end, hanldeError, fn, ret, fd, head, data);
if (r ~= true) then --出错啦
print("Raise " .. event .. " event error: " .. e);
return
end
end
end
if (ret[1] ~= nil) then --null会在出错或者没注册的时候出现所以返回0不过滤封包
return ret[1];
end
return 0;
end
function Module.init()
-- Test Code
-- local fn1 = function(...)
-- --print("recv 1 ", "return " .. 0, ...);
-- return 0;
-- end
-- local fn2 = function(...)
-- --print("recv 2 ", "return " .. 1, ...);
-- return 1;
-- end
-- local k = 0;
-- local fn3 = function(...)
-- k = k + 1;
-- if (k > 3) then
-- k = 0;
-- --print("recv 3 ", "return " .. 0, ...);
-- return 0;
-- end
-- --print("recv 3 ", "return " .. 1, ...);
-- return 1;
-- end
-- Module.on(0, fn1);
-- Module.on(0, fn2);
-- Module.on(0, fn3);
end
return Module;