D&F - DP插件

D&F - DP插件

Tim 293 2024-08-04

站在了巨人的肩膀头上,感谢造轮子的大佬。通过现有的dp2.9进行修改和添加功能

账号注册福利、角色每日奖励

原插件功能为账号每日奖励,稍微修改变成账号注册福利、角色每日奖励(可能需要切换角色才能收到邮件)

账号注册福利写在角色登录这块可能不太优雅

函数

-- 每日首次登录福利 参考朝暮1031【珏珏子自改】
function hook_first_login_gift(_user)
    local user = game.fac.user(_user)
    local env = luasql.mysql()
    local conn = env:connect("taiwan_billing", "game", "uu5!^%jg", "127.0.0.1", 3306)
    conn:execute "SET NAMES latin1"
    conn:execute "create database if not exists dp2"
    --conn:execute "create table if not exists dp2.login(id INT(10) not null primary key AUTO_INCREMENT,account INT(10) default 0 not null, loginTime INT(10) UNSIGNED default 0 not null, firstLogin INT(10) UNSIGNED default 0 not null)"
    --20240730 by Tim 添加一个角色名字段
    conn:execute "create table if not exists dp2.login(id INT(10) not null primary key AUTO_INCREMENT,account INT(10) default 0 not null, characname VARCHAR(50) default '1' not null, loginTime INT(10) UNSIGNED default 0 not null, firstLogin INT(10) UNSIGNED default 0 not null)"

    --20240804 by Tim 创建注册表
    conn:execute "create table if not exists dp2.register(id INT(10) not null primary key AUTO_INCREMENT,account INT(10) default 0 not null, firstLogin INT(10) UNSIGNED default 0 not null)"
    local curregister = conn:execute(string.format("select firstLogin from dp2.register where account = %d ", user:GetAccId()))
    local rowregister = curregister:fetch({}, "a")
    if not rowregister then
        --未查到说明新注册,发放注册福利
        conn:execute(string.format("insert into dp2.register(account,firstLogin) values (%d,0)", user:GetAccId(), 0))

        dpx.mail.item(user:GetCharacNo(), 0, "注册奖励",string.format("账号注册,奖励%d个璀璨水晶",30), 400001000, 30)
        user:SendNotiPacketMessage(string.format("\n账号注册,奖励%d个璀璨水晶",30), 2)   
    end
    -- 获取账号的登录是否当日首次登录
    --local cur = conn:execute(string.format("select firstLogin from dp2.login where account = %d and loginTime >= %d", user:GetAccId(), GetCurrentDayZeroTimestamp()))
    --20240730 by Tim 条件添加一个角色名
    local cur = conn:execute(string.format("select firstLogin from dp2.login where account = %d and characname = '%s' and loginTime >= %d", user:GetAccId(), user:GetCharacName(), GetCurrentDayZeroTimestamp()))
    local row = cur:fetch({}, "a")
    if not row then
        -- 未查询到用户记录 说明这次是首次登录 发放每日福利
        --conn:execute(string.format("insert into dp2.login(account,loginTime,firstLogin) values (%d,%d,0)", user:GetAccId(), os.time(), 0))
        
        --20240803 by Tim 删除角色历史登录记录
        conn:execute(string.format("delete from dp2.login where account = %d and characname = '%s' ", user:GetAccId(), user:GetCharacName()))
        --20240730 by Tim 插入记录加一个角色名
        conn:execute(string.format("insert into dp2.login(account,characname,loginTime,firstLogin) values (%d,'%s',%d,0)", user:GetAccId(), user:GetCharacName(), os.time(), 0))
        --点券奖励
        --local cera = 1000000 -- 需要发送的点券数量
        --conn:execute(string.format("update taiwan_billing.cash_cera set cera=cera+%d where account = %d", cera, user:GetAccId()))
        --dpx.cash.add(user.cptr, count) --通过dpx.cash.add充值点券
        -- 3037[无色晶体] 1000[num]
        --dpx.item.add(user.cptr, 3037, 1000)
        --邮件发送物品 会发一次空邮件,serverGroup不知道传啥
        --dpx.mail.item(user:GetCharacNo(), 0, "登录奖励", "当日首次登录,奖励1000个无色晶体", 3037, 1000)
        --user:SendNotiPacketMessage("\n当日首次登录,奖励1000个无色晶体", 2)
        --20240731 by Tim 修改为随机1-6个璀璨碎片
        local randomNum = math.random(1,6);
        dpx.mail.item(user:GetCharacNo(), 0, "登录奖励",string.format("角色当日首次登录,随机奖励%d个璀璨水晶",randomNum), 400001000, randomNum)
        user:SendNotiPacketMessage(string.format("\n角色当日首次登录,随机奖励%d个璀璨水晶",randomNum), 2)
    else
        user:SendNotiPacketMessage("\n角色当日非首次登录", 2)
    end
    conn:close()
    env:close()
end

调用

-- 玩家登录游戏hook
local function onLogin(_user)
    local user = game.fac.user(_user)
    local uid = user:GetAccId()
    hook_first_login_gift(_user) -- 每日首次登录福利
    autoAddguild(user)--检查是否加入公会
       -- 广播消息
    sendPacketMessage(string.format("玩家【%s】上线了", user:GetCharacName()), 14)
end
dpx.hook(game.HookType.Reach_GameWord, onLogin)

自动加入公会

前提是必须手动创建一个公会,自动加入后第一次需要切换角色或者重新登录游戏(有时候公会显示有问题,需要切换角色)

函数

--20240803 by Tim 自动添加公会
function autoAddguild(userinfo)
    --角色参数准备
    local mid = userinfo:GetAccId()
    local characNo = userinfo:GetCharacNo()
    local characName = userinfo:GetCharacName()
    local lev = userinfo:GetCharacLevel()
    local job = userinfo:GetCharacJob()
    local grow = userinfo:GetCharacGrowType()
    local now = os.date("%Y-%m-%d %H:%M:%S")
    local guildId = 1

    local env = luasql.mysql()
    local connGuild = env:connect("d_guild", "game", "uu5!^%jg", "127.0.0.1", 3306)
    connGuild:execute "SET NAMES latin1"
    --local connUser = env:connect("taiwan_cain", "game", "uu5!^%jg", "127.0.0.1", 3306)
    --connUser:execute "SET NAMES latin1"

    --判断角色是否有公会
    -- local curUser = connGuild:execute("SELECT m_id, charac_no, charac_name, lev, job, grow_type from taiwan_cain.charac_info where charac_no = " + characNo +" and guild_id = 0  LIMIT 1")
    local curUser = connGuild:execute("SELECT m_id, charac_no from d_guild.guild_member where charac_no = " .. characNo)
    local userRow = curUser:fetch({}, "a")
    if userRow then
        connGuild:close()
        --connUser:close()
        env:close()
        return
    end

    --判断是否存在可用公会
    local cur = connGuild:execute("select guild_id from d_guild.guild_info")
    local row = cur:fetch({}, "a")
    if not row then
    
        connGuild:close()
        --connUser:close()
        env:close()
        return
    end
    
    --开启事务
    connGuild:execute([[START TRANSACTION;]])
    local statusGuild,errGuild = pcall(function()
        --插入到公会成员表
        connGuild:execute(string.format("INSERT INTO d_guild.guild_member (guild_id, m_id, server_id, charac_no, charac_name, memo, grade, job, grow_type, lev, member_time, member_flag, last_play_time, age, born_year) VALUES (%d, %d, 3, %d, '%s', ' ', 3, %d, %d, %d, '%s', 1, '%s', 0, '00')", guildId, mid, characNo, characName, job, grow, lev, now,now))
        connGuild:execute("UPDATE d_guild.guild_info SET member_count = member_count + 1 WHERE guild_id = " .. guildId)
    end)
    if not statusGuild then
        connGuild:execute([[ROLLBACK;]])
        connGuild:close()
        --connUser:close()
        env:close()
        logger.info("Insert Guild Member Error: "+errGuild)
        return
    else
        connGuild:execute([[COMMIT;]])
    end
    --更新角色表(应该要判断是否写成功,失败得删除公会成员表,偷懒就不判断了)
    connGuild:execute("UPDATE taiwan_cain.charac_info SET guild_id = " .. guildId .. ", guild_right = 0 WHERE charac_no = " .. characNo)
    connGuild:close() 
    env:close()
    logger.info(characName .. " 加入公会成功!")
    userinfo:SendNotiPacketMessage("\n加入公会成功! 请重新登录游戏")
end

调用

-- 玩家登录游戏hook
local function onLogin(_user)
    local user = game.fac.user(_user)
    local uid = user:GetAccId()
    hook_first_login_gift(_user) -- 每日首次登录福利
    autoAddguild(user)--检查是否加入公会
    -- 广播消息
    sendPacketMessage(string.format("玩家【%s】上线了", user:GetCharacName()), 14)
end
dpx.hook(game.HookType.Reach_GameWord, onLogin)