NPCMgr 扩展 b:NPC商店(限购、折扣、多货币)

业务规则

  1. 一个NPC绑定一个shop_id,配表驱动商品;支持金币/元宝/绑元等多种货币
  2. 限购分:每日限购、终身限购,限购数据挂玩家身上落库
  3. 支持NPC专属折扣、VIP折扣叠加(可配置是否叠加)
  4. 购买前校验背包空位、货币、限购;购买成功发奖励、扣货币、记限购流水
  5. 支持商品售罄隐藏、等级/行会条件锁商品

1. 配置表 npc_shop_cfg

ini 复制代码
npc_shop_cfg = {
    [1001] = { -- shop_id
        npc_id = 5001,
        discount = 100, -- 默认折扣 100=原价,80=8折
        discount_stack_vip = true, -- 是否和vip折扣叠加
        goods_list = {
            {
                goods_id = 101,
                item_id = 2001,
                item_cnt = 1,
                price = 5000,
                currency = "gold", -- gold / yuanbao / bind_yuanbao
                limit_type = "day", -- day每日 once终身 nil不限
                limit_num = 5,
                need_lv = 0,
                need_guild = false
            },
            {
                goods_id = 102,
                item_id = 2002,
                item_cnt = 10,
                price = 10,
                currency = "bind_yuanbao",
                limit_type = "once",
                limit_num = 1,
                need_lv = 35
            }
        }
    }
}

2. 玩家数据结构(落库)

diff 复制代码
-- player.shop_limit_data
-- key: shop_id_goods_id
-- val = { day_cnt=0, once_cnt=0, last_day=0 }

3. Protobuf 协议

ini 复制代码
// 请求打开商店
message CS_NpcShopOpen {
    uint32 npc_uid = 1;
}
// 请求购买商品
message CS_NpcShopBuy {
    uint32 npc_uid = 1;
    uint32 shop_id = 2;
    uint32 goods_id = 3;
    uint32 buy_cnt = 1;
}
// 商店面板推送
message SC_NpcShopPanel {
    uint32 shop_id = 1;
    repeated ShopGoods goods = 2;
}
message ShopGoods {
    uint32 goods_id = 1;
    uint32 item_id = 2;
    uint32 item_cnt = 3;
    uint64 price = 4;
    string currency = 5;
    uint32 remain_cnt = 6; -- 剩余可买数量
    bool lock = 7; -- 是否条件锁定
    string lock_tip = 8;
}

4. 错误码

ini 复制代码
ERR_NPC_SHOP = {
    OK = 0,
    NPC_NOT_EXIST = 22001,
    SHOP_CFG_NOT_FOUND = 22002,
    GOODS_NOT_FOUND = 22003,
    NOT_NEAR = 22004,
    NO_ENOUGH_MONEY = 22005,
    NO_BAG_SLOT = 22006,
    LIMIT_OVER = 22007,
    COND_NOT_MEET = 22008
}

5. NpcMgr 新增核心代码

ini 复制代码
-- 初始化限购数据
function NpcMgr:InitShopLimitData(player)
    player.shop_limit_data = player.shop_limit_data or {}
end

-- 获取商品剩余可购买数量
function NpcMgr:GetGoodsRemainCnt(player, shop_id, goods)
    if not goods.limit_type then
        return 999999
    end
    local key = shop_id.."_"..goods.goods_id
    local data = player.shop_limit_data[key]
    if not data then
        data = {day_cnt = 0, once_cnt = 0, last_day = self:GetDayStamp()}
        player.shop_limit_data[key] = data
    end
    -- 跨天重置
    if goods.limit_type == "day" and data.last_day ~= self:GetDayStamp() then
        data.day_cnt = 0
        data.last_day = self:GetDayStamp()
    end
    if goods.limit_type == "day" then
        return math.max(0, goods.limit_num - data.day_cnt)
    else
        return math.max(0, goods.limit_num - data.once_cnt)
    end
end

-- 计算最终价格
function NpcMgr:CalcGoodsPrice(player, shop_cfg, goods)
    local base = goods.price
    local d1 = shop_cfg.discount / 100
    local d2 = 1.0
    if shop_cfg.discount_stack_vip then
        d2 = VipMgr:GetVipDiscount(player)
    end
    return math.floor(base * d1 * d2)
end

-- 打开商店
function NpcMgr:OpenShop(player, npc_uid)
    local npc = self:GetNpcByUid(npc_uid)
    if not npc then return ERR_NPC_SHOP.NPC_NOT_EXIST end
    if not self:CheckPlayerNearNpc(player, npc) then return ERR_NPC_SHOP.NOT_NEAR end

    local shop_id = npc.cfg.shop_id
    local shop_cfg = npc_shop_cfg[shop_id]
    if not shop_cfg then return ERR_NPC_SHOP.SHOP_CFG_NOT_FOUND end

    local msg = {shop_id = shop_id, goods = {}}
    for _, goods in ipairs(shop_cfg.goods_list) do
        local lock = false
        local tip = ""
        if goods.need_lv > 0 and player:GetLevel() < goods.need_lv then
            lock = true
            tip = "等级不足"
        end
        if goods.need_guild and player:GetGuildId() == 0 then
            lock = true
            tip = "需要加入行会"
        end
        local remain = self:GetGoodsRemainCnt(player, shop_id, goods)
        local price = self:CalcGoodsPrice(player, shop_cfg, goods)
        table.insert(msg.goods, {
            goods_id = goods.goods_id,
            item_id = goods.item_id,
            item_cnt = goods.item_cnt,
            price = price,
            currency = goods.currency,
            remain_cnt = remain,
            lock = lock,
            lock_tip = tip
        })
    end
    MsgMgr:SendMsg(player, "SC_NpcShopPanel", msg)
    return ERR_NPC_SHOP.OK
end

-- 购买商品
function NpcMgr:BuyGoods(player, npc_uid, shop_id, goods_id, buy_cnt)
    local npc = self:GetNpcByUid(npc_uid)
    if not npc then return ERR_NPC_SHOP.NPC_NOT_EXIST end
    local shop_cfg = npc_shop_cfg[shop_id]
    if not shop_cfg then return ERR_NPC_SHOP.SHOP_CFG_NOT_FOUND end

    local target_goods = nil
    for _, g in ipairs(shop_cfg.goods_list) do
        if g.goods_id == goods_id then
            target_goods = g
            break
        end
    end
    if not target_goods then return ERR_NPC_SHOP.GOODS_NOT_FOUND end

    -- 条件校验
    if target_goods.need_lv > 0 and player:GetLevel() < target_goods.need_lv then
        return ERR_NPC_SHOP.COND_NOT_MEET
    end
    if target_goods.need_guild and player:GetGuildId() == 0 then
        return ERR_NPC_SHOP.COND_NOT_MEET
    end

    local remain = self:GetGoodsRemainCnt(player, shop_id, target_goods)
    if remain < buy_cnt then
        return ERR_NPC_SHOP.LIMIT_OVER
    end

    local single_price = self:CalcGoodsPrice(player, shop_cfg, target_goods)
    local total_price = single_price * buy_cnt

    -- 扣货币
    local ok = MoneyMgr:CostMoney(player, target_goods.currency, total_price, "npc_shop_buy")
    if not ok then
        return ERR_NPC_SHOP.NO_ENOUGH_MONEY
    end

    -- 发物品
    local item_cnt = target_goods.item_cnt * buy_cnt
    local ret = BagMgr:AddItem(player, target_goods.item_id, item_cnt)
    if ret ~= 0 then
        MoneyMgr:AddMoney(player, target_goods.currency, total_price, "npc_shop_rollback")
        return ERR_NPC_SHOP.NO_BAG_SLOT
    end

    -- 更新限购计数
    local key = shop_id.."_"..goods_id
    local limit_data = player.shop_limit_data[key]
    if target_goods.limit_type == "day" then
        limit_data.day_cnt = limit_data.day_cnt + buy_cnt
    elseif target_goods.limit_type == "once" then
        limit_data.once_cnt = limit_data.once_cnt + buy_cnt
    end

    -- 日志
    LogMgr:LogShopBuy(player, shop_id, goods_id, buy_cnt, total_price, target_goods.currency)
    -- 刷新面板
    self:OpenShop(player, npc_uid)
    return ERR_NPC_SHOP.OK
end

function NpcMgr:GetDayStamp()
    -- 返回当日0点时间戳,用于每日重置
    return TimeMgr:GetDayStamp()
end

6. 消息注册

lua 复制代码
MsgMgr:Reg("CS_NpcShopOpen", function(player, req)
    local ret = NpcMgr:OpenShop(player, req.npc_uid)
    if ret ~= 0 then
        MsgMgr:SendErr(player, ret)
    end
end)

MsgMgr:Reg("CS_NpcShopBuy", function(player, req)
    local ret = NpcMgr:BuyGoods(player, req.npc_uid, req.shop_id, req.goods_id, req.buy_cnt)
    if ret ~= 0 then
        MsgMgr:SendErr(player, ret)
    end
end)

7. GM指令

lua 复制代码
-- /npc_shop_reset roleid shop_id goods_id 重置限购
GmMgr:RegCmd("npc_shop_reset", function(player, args)
    local pid = tonumber(args.roleid)
    local sid = tonumber(args.shop_id)
    local gid = tonumber(args.goods_id)
    local target = PlayerMgr:GetPlayerByPid(pid)
    if not target then return "玩家不在线" end
    local key = sid.."_"..gid
    target.shop_limit_data[key] = nil
    return "重置成功"
end)

坑点清单

  • 购买成功后背包满要回滚货币,必须事务顺序:先扣钱 → 再加物品,失败退款
  • 折扣浮点取整,向下取整,防止分厘货币漏洞
  • 每日重置用统一DayStamp,不要用本地时间
  • 玩家下线时持久化 shop_limit_data
相关推荐
房西杭2 天前
NpcMgr NPC管理器(Lua 传奇MMO服务端)
游戏开发
花椒技术4 天前
从“实时生成游戏”出发,我们用 AI 做了四次游戏开发尝试
人工智能·aigc·游戏开发
浪浪山_大橙子4 天前
我用 GPT‑6.2 打磨《大国崛起》4.0.1,从雪地脚印改到运输跑道
游戏开发
翼帆4 天前
我用 AI 复刻了 CodeTank,重温代码坦克
前端·javascript·开源·ai编程·游戏开发·codetank
花椒技术5 天前
客户端团队用 AI 做直播间桌球,状态同步和渲染怎么落地
agent·ai编程·游戏开发
羲云网络6 天前
Claude Fable 5.2 创意编程分析:程序化生成、多智能体协作与工程验收
人工智能·大语言模型·blender·游戏开发
SmalBox7 天前
10-04-实战-资源组织与依赖管理实战
unity3d·游戏开发
浪浪山_大橙子9 天前
被1.4 MB的射击游戏震撼后,我认真拆了一遍Fable的实现
ai编程·游戏开发·three.js
SmalBox9 天前
10-02-实战-Unity原生AssetBundle实战
unity3d·游戏开发