--[[
Author: SLWS_Gao_Q1968023646
Date: 2024-06-25 09:27:14
Description: 实物兑换,查询,发货
--]]
require("Envir/Market_Def/NpcLua/zh.lua") --个人封装函数
local prize_list = {
{id=1, name="华硕平板", goods="华硕平板", stock=10, var = "G301"},
{id=2, name="小米净化器", goods="小米净化器", stock=20, var ="G302"},
{id=3, name="罗技键鼠", goods="罗技键鼠", stock=40, var = "G303"},
{id=4, name="鼠标垫", goods="鼠标垫", stock=200, var = "G304"}
}
function redeem_prize(actor, prize_id, recipient, tel, address) --兑换实物数据保存,传入奖品id
local recipient = getstr(actor, "收件人姓名")
local tel = getstr(actor, "收件人电话")
local address = getstr(actor, "收件人地址")
if recipient == "" or not recipient then
sendmsg9(actor, "请先填写你的收件人姓名,在来兑换!")
return
end
if tel == "" or not tel then
sendmsg9(actor, "请先填写你的收件人电话,在来兑换!")
return
end
if address == "" or not address then
sendmsg9(actor, "请先填写你的地址,在来兑换!")
return
end
prize_id = tonumber(prize_id) or 0
local prize = prize_list[prize_id] -- 获取奖品信息
if not prize then
return sendmsg9(actor, "Error: 实物奖励物品配置不存在,请检查传入奖励ID.")
end
if itemcount(actor, prize["goods"]) < 1 then
return sendmsg9(actor, "你的包裹没有" .. prize["goods"] )
end
if not ( getsysvar(prize["var"]) < prize["stock"] ) then
return sendmsg9(actor, prize["name"] .. "[" .. prize["goods"] .. "]已经兑换了" .. getsysvar(prize["var"]) .. "个,没有名额了!")
end
if takeitemex(actor, prize["goods"], 1, 0, "实物兑换") then --兑换写入
save_redeem_detail(actor, prize)
setsysvar(prize["var"], getsysvar(prize["var"]) + 1)
opennpcshow(actor, 45, 10 )
else
return sendmsg9(actor, "扣除" .. prize["goods"] .. "无法完成兑换")
end
end
function save_redeem_detail(actor, prize) --保存兑换详情
local recipient = getstr(actor, "收件人姓名")
local tel = getstr(actor, "收件人电话")
local address = getstr(actor, "收件人地址")
local uid = getbaseinfo(actor, 2)
local username = getname(actor)
-- 将时间戳转换为日期时间格式
local date_time_str = os.date("%Y-%m-%d %H:%M:%S", os.time() )
local sql = string.format("INSERT INTO prize (uid, username, recipient, goods, status, tel, addr, time) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", uid, username, recipient, prize["goods"], "未发货", tel, address, date_time_str)
sql = GBK2UTF8(sql)
db_exec(sql)
sendmsg9(actor, "恭喜你成功兑换" .. prize["goods"] .. "!官方将统一安排处理。")
sendmsg6(actor, "恭喜你成功兑换" .. prize["goods"] .. "!官方将统一安排处理。")
end
function save_address(actor) --保存收货地址
local recipient = getconst(actor, '<$NPCINPUT(4)>')
local tel = getconst(actor, '<$NPCINPUT(5)>')
local address = getconst(actor, '<$NPCINPUT(6)>')
if recipient == "" then
sendmsg9(actor, "请输入你的收件人姓名!")
return
end
if tel == "" then
sendmsg9(actor, "请输入你的收件人电话!")
return
end
if address == "" then
sendmsg9(actor, "请输入你的收件人地址!")
return
end
setstr(actor, "收件人姓名", recipient)
setstr(actor, "收件人电话", tel)
setstr(actor, "收件人地址", address)
sendmsg9(actor, "奖品收件人地址信息已保存!")
sendmsg6(actor, "奖品收件人地址信息已保存!")
opennpcshow(acotr, 45, 10)
end
function query_parcel_status(actor) --显示兑奖信息
local msg = [[
<Img|x=0|y=-1|width=530|height=250|img=image/bg2.png|bg=1|scale9r=10|scale9t=10|scale9b=10|reset=1|scale9l=10|move=0|show=0>
<Layout|x=529|y=0|width=80|height=80|link=@exit>
<Button|x=529|y=0|nimg=public/1900000510.png|mimg=public/1900000510.png|pimg=public/1900000511.png|width=26|height=42|link=@exit>
<Img|a=4|percentx=50|y=55|id=title_3|img=slws/203.png|esc=0>
]]
local uid = getbaseinfo(actor, 2)
local sql = "SELECT id, goods, time from prize where uid = '" .. uid .. "'"
sql = GBK2UTF8(sql)
--sendmsg6(actor, sql )
local results = db_exec(sql)
local id, goods ,time = results:fetch()
if not goods then
sendmsg9(actor, "没查到你的兑奖信息!")
return
end
goods = UTF82GBK(goods)
time = UTF82GBK(time)
local i = 1
local y = 10
msg = msg .. "<RText|id=title|x=60|y=25|color=250|size=16|text= 编号 奖品 兑换时间>"
local children = "Rtext_1,"
while goods do
goods = UTF82GBK(goods)
time = UTF82GBK(time)
i = i + 1
y = y + 20
msg = msg .. "<RText|id=Rtext_".. i .. "|x=65|y=" .. y .. "|color=255|size=16|text= " .. id .. " " .. goods .. " " .. time.. ">"
id, goods ,time = results:fetch()
children = children .. "Rtext_" .. i .. ","
end
results:close()
msg = msg .. "<RText|id=排版空行|x=20|y=10|color=255|size=16|text=>"
msg = msg .. "<ListView|children={" .. children .. "排版空行}|x=40|y=70|width=460|height=158|margin=10|bounce=1|direction=1|reload=0>"
say(actor, msg)
end
function db_exec(sql)
local env = sqlite3.sqlite3()
local db = env:connect("Envir/QuestDiary/db.sqlite")
local results = db:execute(sql)
db:close()
env:close()
end
function db_exec(sql)
local env = sqlite3.sqlite3()
local db = env:connect("Envir/QuestDiary/db.sqlite")
local results = db:execute(sql)
if results then
return results
end
db:close()
env:close()
end
function cleardb(actor)
local sql = "delete from prize"
db_exec(sql)
sendmsg9(actor, "实物兑换已清空")
end
local toutf8 = iconv.new("UTF-8", "GBK")
function GBK2UTF8(str)
local s, err = toutf8:iconv(str)
if err then
release_print("err:"..tostring(err))
return str
end
return s
end
local togbk = iconv.new("GBK", "UTF-8")
function UTF82GBK(str)
local s, err = togbk:iconv(str)
if err then
release_print("err:"..tostring(err))
return str
end
return s
end