123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541 |
- --[[
- 扣除玩家的物品
- ]]
- local skynet = require "skynet"
- require "skynet.manager"
- local logger = require "logger"
- local config = require "model.asset"
- local queue = require "skynet.queue"
- local cjson = require "cjson"
- local synchronized = queue()
- local namecenter = skynet.localname(".namecenter")
- local temp_config = mt
- local cjson = require "cjson"
- local redisdriver = require "skynet.db.redis"
- local stringify = require "stringify"
- local redis
- -- local z_trace = require "zlf_log"
- -- local z_trace = z_trace.trace
- local authz = {
- yytx = "lee@YY-Games.520"
- }
- local whitelist = {
- ["127.0.0.1"] = true,
- ["14.29.136.211"] = true,
- ["222.212.88.4"] = true,
- }
- -- 更具名字添加id
- local function get_id(name)
- return skynet.call(namecenter,"lua","findby", name)
- end
- -- 读取数据
- local function get_data(uid, key)
- local ret = redis:HMGET(string.format("character:%s", uid), key)
- if ret[1] then
- if key == "coins" -- 金币
- -- or key == "exp" -- 角色经验
- or key == "pkpoint" -- 竞技积分
- or key == "diamonds" -- 钻石
- or key == "friendship" -- 友情点
- or key == "contribute" -- 公会贡献
- or key == "advintegral" -- 公会冒险积分
- or key == "recharge_point" -- 充值积分
- or key == "research_note" -- 研究笔记 --PK3-1148
- or key == "megacoin" -- mege点
- or key == "bind_diamonds" -- 绑定钻石
- or key == "budo_point" -- 道馆积分
- or key == "manual_exp" -- 训练师手册积分 --PK3-1077
- or key == "explore" -- 探险值
- or key == "explore_exp" -- 探险经验
- or key == "gashapon_num" -- 扭蛋积分
- or key == "smeltpoint" -- 熔炼点
- or key == "uid"
- or key == "nickname"
- or key == "span_elf_king_point" -- 跨服至尊天王积分 -- CDPK3-86
- or key == "ruins_point" -- 古代金币 -- CDPK3-276 宝可梦遗迹
- then
- return ret[1]
- else
- return skynet.unpack(ret[1])
- end
- else
- return nil
- end
- end
- -- 设置数据
- local function set_data(uid, key, data)
- if data then
- if type(data) == "table" then
- redis:HMSET(string.format("character:%s", uid), key, skynet.packstring(data))
- else
- redis:HMSET(string.format("character:%s", uid), key, data)
- end
- end
- end
- -- 检查玩家是否存在
- local function check_receiver(uid)
- local ret = get_data(uid, "uid")
- return ret
- end
- local cmd = {}
- local err = {
- err_info = cjson.encode({state = 403, msg = "没有找到那个字段 3"}),
- err_than = cjson.encode({state = 403, msg = "扣除的值过大 4"}),
- err_find = cjson.encode({state = 403, msg = "扣除的sid不存在"}),
- err_empty = cjson.encode({state = 403,msg = "table里面的数据为空 8"}),
- err_elf = cjson.encode({state = 403, msg = "没有找到该精灵 ------ 3"}),
- err_set = cjson.encode({state = 403, msg = "精灵使用中"}),
- }
- function cmd.currency(key, value, uid)
- local info = get_data(uid, key)
- if not info then return err.err_info end
- info = tonumber(info)
- logger.trace(" key = %s, value = %s", key, value)
- if value > info then return err.err_than end
- info = info - value
- set_data(uid, key, info)
- end
- function cmd.pokemon_exp(key, value, uid)
- local info = get_data(uid, "elfdata")
- if not info then return err.err_info end
- if value > info.experience then return err.err_than end
- info.experience = info.experience - value
- set_data(uid, "elfdata", info)
- end
- function cmd.item(sid, num, uid)
- local info = get_data(uid, "bagdata")
- if not info then return err.err_info end
- if info.item[sid] then
- local value = (info.item[sid].num or 0) - num
- if value == 0 then
- info.item[sid] = nil
- info.num = info.num - 1 -- 被占用格子的数量
- elseif value > 0 then
- info.item[sid].num = value
- else
- return err.err_than
- end
- else
- return err.err_find
- end
- -- 保存数据
- set_data(uid, "bagdata", info)
- end
- -- TODO: 扣除符文碎片
- function cmd.rune(sid, num, uid)
- local info = get_data(uid, "elf_rune")
- if not info then return err.err_info end
- -- 统计数量
- local del = {}
- for k, v in pairs(info.rune_bag) do
- if v.sid == sid then
- table.insert(del, k)
- end
- end
- if #del == num then
- for _, id in pairs(del) do
- info.rune_bag[id] = nil
- info.rune_bagnum = info.rune_bagnum or 1
- info.rune_bagnum = info.rune_bagnum - 1
- end
- set_data(uid, "elf_rune", info)
- else
- return err.err_than
- end
- end
- -- TODO: 扣除携带品--CDPK3-271
- function cmd.carry(sid, num, uid)
- local info = get_data(uid, "elf_carry_items")
- if not info then return err.err_info end
- -- 统计数量
- local del = {}
- for k, v in pairs(info.carry_items_bag) do
- if v.sid == sid then
- table.insert(del, k)
- end
- end
- if #del == num then
- for _, id in pairs(del) do
- info.carry_items_bag[id] = nil
- info.carry_items_num = info.carry_items_num or 1
- info.carry_items_num = info.carry_items_num - 1
- end
- set_data(uid, "elf_carry_items", info)
- else
- return err.err_than
- end
- end
- -- TODO: 扣除代金券 CDPK3-1002
- function cmd.coupon(cid, sid, uid) -- cid:配置id sid:唯一id
- local info = get_data(uid, "coupon")
- if not info then return err.err_info end
- logger.trace("################ sid:%s, cid:%s", sid, cid)
- if info.bag[sid] and info.bag[sid].cid == cid then
- info.bag[sid] = nil
- set_data(uid, "coupon", info)
- else
- return err.err_than
- end
- end
- function cmd.fashion(id, num, uid)
- local info = get_data(uid, "dress")
- if not info then return err.err_info end
- --[[
- if info.owned[id] then
- if id == info.carry then
- info.carry = nil
- end
- info.owned[id] = nil
- set_data(uid, "dress", info)
- end
- ]]
- if info.owned[id] then
- if id == info.carry then
- info.carry = nil
- end
- -- info.owned[id] = nil
- if info.owned[id][1] > num then
- info.owned[id][1] = info.owned[id][1] - num
- elseif info.owned[id][1] == num then
- -- 已经进阶过得时装必须保留一件
- if info.owned[id][2] > 0 then
- return err.err_than
- end
- info.owned[id] = nil
- else
- return err.err_than
- end
- set_data(uid, "dress", info)
- end
- end
- function cmd.title(id, _, uid) --PK3-1030
- local info = get_data(uid, "title")
- if not info then return err.err_info end
- if info.titlelist[id] then
- if id == info.settitle then
- info.settitle = nil
- end
- info.titlelist[id] = nil
- set_data(uid, "title", info)
- end
- end
- function cmd.debris(sid, num, uid)
- local info = get_data(uid, "oncedata")
- if not info then return err.err_info end
- if info.debris and info.debris[sid] and info.debris[sid].num and info.debris[sid].num > 0 then
- local snum = info.debris[sid].num - num
- if snum == 0 then
- info.debris[sid] = nil
- elseif snum > 0 then
- info.debris[sid].num = snum
- else
- return err.err_than
- end
- set_data(uid, "oncedata", info)
- else
- return err.err_than
- end
- end
- function cmd.rune_debris(sid, num , uid)
- local info = get_data(uid, "rune_debris")
- if not info then return err.err_info end
- local rnum = 0
- if info.runedebris and info.runedebris[sid] and info.runedebris[sid].num and info.runedebris[sid].num > 0 then
- rnum = info.runedebris[sid].num
- end
- if rnum == 0 then
- return err.err_than
- end
- local value = rnum - num
- if value == 0 then
- info.runedebris[sid] = nil
- elseif value > 0 then
- info.runedebris[sid].num = value
- else
- return err.err_than
- end
- set_data(uid, "rune_debris", info)
- end
- function cmd.carry_debris(sid, num , uid)
- local info = get_data(uid, "carry_debris")
- if not info then return err.err_info end
- local rnum = 0
- if info.carrydebris and info.carrydebris[sid] and info.carrydebris[sid].num and info.carrydebris[sid].num > 0 then
- rnum = info.carrydebris[sid].num
- end
- if rnum == 0 then
- return err.err_than
- end
- local value = rnum - num
- if value == 0 then
- info.carrydebris[sid] = nil
- elseif value > 0 then
- info.carrydebris[sid].num = value
- else
- return err.err_than
- end
- set_data(uid, "carry_debris", info)
- end
- -- elf_awaken={[1]={sid = sid, lv = 0}}}, -- 觉醒降级 sid 精灵的模板id lv 觉醒的等级
- function cmd.elf_awaken(key, value, uid)
- if not next(value) then return err.err_empty end
- local info = get_data(uid, "elf_awaken")
- if not info then return err.err_info end
- local elfdata = get_data(uid, "elfdata")
- local function add(individual)
- local he = 0
- for _, v in pairs(individual) do
- he = he + v
- end
- return he
- end
- for _, elf in pairs(value) do
- if (not elf.individual) or elf.individual <= 0 then
- return cjson.encode({errno = 400, host = header.host, info = "精灵的个体值未空或0 ------- 1"})
- end
- local sign = true
- for id, elf_info in pairs(elfdata.elflist) do
- if elf_info.sid == elf.sid then
- local he = add(elf_info.individual)
- -- 精灵定位
- if he == elf.individual then
- -- 查找修改觉醒数据
- for k, v in pairs(info.awaken_list) do
- if v.sid == elf.sid and v.lv == elf.lv then
- if info.awaken_list[k].lv > 0 then
- -- d.awaken_list[k].lv = d.awaken_list[k].lv - 1
- info.awaken_list[k].lv = info.awaken_list[k].lv - 1
- else
- info.awaken_list[k] = nil
- end
- sign = false
- break
- end
- end
- -- break
- end
- end
- end
- if sign then return err.err_elf end
- end
- set_data(uid, "elf_awaken", info)
- end
- -- TODO: 扣除精灵
- function cmd.pokemon(e_sid, e_id, uid)
- local info = get_data(uid, "elfdata")
- if not info then return err.err_info end
- local state_info = get_data(uid, "elf_state")
- if not state_info then return err.err_info end
- local function check_elf(id, state_info)
- if state_info.elf_employ[id] then
- return true
- end
- end
- local detail = {}
- if info.elflist[e_id] and info.elflist[e_id].sid == e_sid then
- if check_elf(e_id, state_info) then
- return err.err_set -- 精灵使用中
- end
- info.elfcount.bagnum = info.elfcount.bagnum - 1
- detail = info.elflist[e_id]
- info.elflist[e_id] = nil
- set_data(uid, "elfdata", info)
- else
- return err.err_elf
- end
- return nil, detail
- end
- -- 类型邮件系统
- -- http://服务器ip地址:端口号/delitem?code="xxx"&uid="00-xx"&json=移除的道具or代币列表
- -- http://192.168.108.19:9002/delitem?code="xxx"&uid="00-xx"&json={"items":[[104,0,10000000],[102,0,3000],[1,20014,100]]}
- --[[ -- PK3-1366 数据统计
- local log = skynet.localname('.log')
- local function record(character, opcode, context)
- assert(character)
- assert(opcode)
- local bytes = cjson.encode({
- opcode=opcode,
- uid=character.uid,
- nickname=character.nickname,
- context=context,
- date=os.time()
- })
- skynet.send(log, "lua", "record", character.uid, bytes)
- end
- ]]
- local log = require "model.log" -- PK3-1366 数据统计
- local function fire(character, module, event, specific)
- assert(module)
- assert(event)
- assert(type(module) == "string")
- assert(type(event) == "string")
- --[[
- record(character, "event", {
- module=module,
- type=event,
- specific=specific
- })
- ]] -- PK3-1366 数据统计
- logger.trace(" ### specific %s", stringify(specific))
- log.fire(character,module,event, specific)
- end
- local delitem = function(args, ipaddr,header)
- return synchronized(function()
- logger.trace("处理来自主机 %s 的 扣除 请求", ipaddr)
- -- 验证主机id
- if not whitelist[ipaddr] then
- -- return cjson.encode({errno = 403, host = header.host, info = "不信任ip"})
- end
- -- 验证gm账号
- local code = string.sub(args.code, 1, 16)
- -- if code ~= content.sign then
- -- return cjson.encode({state = 403, msg = "账号或密码错误"})
- -- end
- -- 连接数据库
- local conf = assert(option.redis)
- redis = redisdriver.connect(conf)
- redis:select(0)
- -- 目标玩家的处理过程: 验证玩家uid是否存在
- logger.trace(" args = %s", stringify(args))
- --[[local uid = check_receiver(args.uid)
- if not uid then
- redis:disconnect()
- return cjson.encode({state = 403, msg = "没有找到该玩家 2 "})
- end]]
- local uid
- if args.name then
- uid =get_id(args.name)
- elseif args.uid then
- uid=args.uid
- elseif (not args.uid) and (not args.name) then
- return cjson.encode({state = 403,msg= "请输入玩家uid或者玩家name"})
- end
- uid=check_receiver(uid)
- if not uid then
- redis:disconnect()
- return cjson.encode({state = 403, msg = "没有找到该玩家 2 "})
- end
- local ok, delinfo = pcall(cjson.decode, args.json)
- logger.trace(" delinfo = %s", stringify(delinfo))
- if not ok then
- redis:disconnect()
- return cjson.encode({state = 403, msg = "内容错误"})
- end
- -- 循环处理需要处理得数据
- local entity = {}
- local key, value, ret
- for _, v in ipairs(delinfo.items) do
- if v[1] == GOODS_MONEY then -- 货币 精灵经验单独处理
- key = MONEY_TYPE[v[2]] -- 货币字段名
- value = v[3] -- 需要扣除得值
- if key == "exp1" then
- ret = cmd.pokemon_exp(key, value, uid)
- else
- ret = cmd.currency(key, value, uid)
- end
- entity[key] = value
- elseif v[1] == GOODS_ITEM then -- 道具
- ret = cmd.item(v[2], v[3], uid) -- 道具id, 数量
- entity["item"] = {[v[2]] = v[3] }
- elseif v[1] == TYPE_ELF then -- 精灵
- local detail
- ret,detail = cmd.pokemon(v[2], v[3], uid) -- 精灵模板, 精灵唯一id
- entity["pokemon"] = {[v[2]] = detail }
- elseif v[1] == TYPE_FASHION then -- 时装
- ret = cmd.fashion(v[2], v[3], uid) -- 时装id
- entity["fashion"] = {v[2]}
- elseif v[1] == TYPE_DEBRIS then -- 碎片
- ret = cmd.debris(v[2], v[3], uid) -- 碎片id, 数量
- entity["debris"] = {[v[2]] = v[3]}
- elseif v[1] == TYPE_RUNE then -- 符文
- ret = cmd.rune(v[2], v[3], uid) -- 符文id, 数量
- entity["rune"] = {v[2]}
- elseif v[1] == TYPE_RUNEDEBRIS then -- 符文碎片
- ret = cmd.rune_debris(v[2], v[3], uid) -- 碎片id, 数量
- entity["rune_debris"] = {[v[2]] = v[3]}
- elseif v[1] == TYPE_TITLE then -- 称号 --PK3-1030
- ret = cmd.title(v[2], v[3], uid) -- 称号
- entity["title"] = {v[2]}
- elseif v[1] == TYPE_CARRY then -- 符文
- ret = cmd.carry(v[2], v[3], uid) -- 符文id, 数量
- entity["carry"] = {v[2]}
- elseif v[1] == TYPE_CARRYDEBRIS then -- 符文碎片
- ret = cmd.carry_debris(v[2], v[3], uid) -- 碎片id, 数量
- entity["carry_debris"] = {[v[2]] = v[3]}
- elseif v[1] == TYPE_COUPON then -- CDPK3-1002 代金券
- ret = cmd.coupon(v[2], v[3], uid) -- 符文id, 数量
- entity["coupon"] = {v[2]}
- else
- return cjson.encode({state = 403, msg = "没有需要修改的数据 9"})
- end
- end
- if ret then
- -- 数据库断开
- redis:disconnect()
- return ret
- else
- logger.trace(" entity = %s" , stringify(entity))
- entity.other = delinfo.items -- PK3-1366 数据统计
- entity.note = "web_delitem"
- fire({
- uid = uid,
- nickname = get_data(uid, "nickname")
- }, "delitem", "delitem", entity)
- redis:disconnect()
- return cjson.encode({state = 200, msg = "success"})
- end
- end)
- end
- return delitem
|