魔力宝贝

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz

[GA] SellGold.lua--金币寄售员

[复制链接]
发表于 前天 02:26 | 显示全部楼层 |阅读模式
local SellGoldNpcInfo = {100452,"金币寄售员",6,901,57,36} --npc信息
local SellGoldMainMsg = "1.每张金卡的最小金额不能少于1W魔币。\n\n2.寄售时会全服提示(不会提示具体玩家名字)\n\n3.请按要求写明卖出的价格(输错,不予赔偿)\n\n4.卖出以后请点击“领取金币”领取卖出的金币\n\n5.寄售后,可点击“取消寄售”重新上架\n\n6.购买金卡时,请看清价格后,再进行够买操作" --说明
local MaxGold = 10 --最大金币
local GoldItemid = 88888 --金卡id
local GoldItemName = "商城金币" --金卡名字
local GoldItemMaxNum = 1000 --金卡道具最大单次寄售数量(堆叠数量)
local TaxRate = 0.95 --税率 出售者得到的魔币将乘以此值
local GoldItemMinValue = 20000 --底价 金卡出售不得低于此值
local GoldItemMaxValue = 1000000 --顶价 金卡出售不得高于此值
local SqlInfo = {"tbl_sellgold","sellgoldindex","cdkey","playername","itemnumber","itemvalue","unitprice"} --新建sql表字段名 其中 sellgoldindex 设置为自动递增
local UserWord = "SellGoldValue" --tbl_user表记录魔币


------------------------------
SellGoldTalkInfo = SellGoldTalkInfo or {}

function savestring(str)
        if string.len(str) > 0 then
                for i = 1,string.len(str) do
                        local str1 = string.sub(str,i,i)
                        if str1 == "\\" or str1 == "'" or str1 == '"' then
                                return false
                        end
                end
                return true
        else
                return false
        end
end

function isrealstr(str)
        if str == nil then
                return nil, "the string parameter is nil"
        end
        local len = string.len(str)
        if len > 0 then
                for i = 1, len do
                        local ch = string.sub(str, i, i)
                        if not ((ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z') or (ch >= '0' and ch <= '9')) then
                                return false
                        end
                end
                return true
        else
                return nil, "the string parameter is nil"
        end
        return nil
end

function PutCard(player,num,value)
        num = tonumber(num)
        value = tonumber(value)
        if num and math.floor(num) == num and num > 0 and num <= GoldItemMaxNum then
                local untivalue = math.floor(value/num)
                if value and math.floor(value) == value and value > 0 and untivalue >= GoldItemMinValue and untivalue <= GoldItemMaxValue then
                        local cdkey = Char.GetData(player,%对象_CDK%)
                        if isrealstr(cdkey) then
                                local playername = Char.GetData(player,%对象_原名%)
                                if savestring(playername) then
                                        local cashnumber = Char.ItemNum(player,GoldItemid)
                                        if cashnumber >= num then
                                                local query = "INSERT INTO "..SqlInfo[1].." ("..SqlInfo[3]..","..SqlInfo[4]..","..SqlInfo[5]..","..SqlInfo[6]..","..SqlInfo[7]..") VALUES ('"..cdkey.."','"..playername.."',"..num..","..value..","..(value/num)..")"
                                                local resset = SQL.Run(query);
                                                if tonumber(resset) == 0 then
                                                        Char.DelItem(player,GoldItemid,num)
                                                        NLG.SystemMessage(player,"寄售"..num.."个"..GoldItemName.."成功!总价:"..value.."金币。")
                                                        NLG.SystemMessage(-1,"["..GoldItemName.."寄售提示]:一位玩家寄售了"..num.."个"..GoldItemName..",售价"..value.."金币。")
                                                end
                                        else
                                                NLG.SystemMessage(player,"您没有足够的"..GoldItemName.."。")
                                        end
                                else
                                        NLG.SystemMessage(player,"您的名字含有非法字符。")
                                end
                        else
                                NLG.SystemMessage(player,"您的帐号只能包括大小写字母和数字。")
                        end
                else
                        NLG.SystemMessage(player,"当前单价"..untivalue.."金币不合理,请确保出售的单价处于合理范围("..GoldItemMinValue.."-"..GoldItemMaxValue.."的整数)。")
                end
        else
                NLG.SystemMessage(player,"请输入合理的数量(1-"..GoldItemMaxNum.."的整数)。")
        end
end

function BuyCard(player,buyindex)
        buyindex = tonumber(buyindex)
        if buyindex and buyindex > 0 then
                if Char.ItemSlot(player) < 20 then
                        local cdkey = Char.GetData(player,%对象_CDK%)
                        if isrealstr(cdkey) then
                                local query = "SELECT "..SqlInfo[3]..","..SqlInfo[4]..","..SqlInfo[5]..","..SqlInfo[6].." FROM "..SqlInfo[1].." WHERE "..SqlInfo[2].." = "..buyindex
                                local resset = SQL.Run(query)
                                if type(resset) == "table" then
                                        if resset["0_0"] == cdkey then
                                                local delline = "DELETE FROM "..SqlInfo[1].." WHERE "..SqlInfo[2].." = "..buyindex
                                                SQL.Run(delline)
                                                Char.GiveItem(player,GoldItemid,tonumber(resset["0_2"]))
                                                NLG.SystemMessage(player,"撤销了寄售的"..resset["0_2"]..GoldItemName.."。")
                                        else
                                                local playergold = Char.GetData(player,%对象_金币%)
                                                if playergold >= tonumber(resset["0_3"]) then
                                                        Char.SetData(player,%对象_金币%,playergold-tonumber(resset["0_3"]))
                                                        NLG.UpChar(player)
                                                        local delline = "DELETE FROM "..SqlInfo[1].." WHERE "..SqlInfo[2].." = "..buyindex
                                                        SQL.Run(delline)
                                                        local upline = "UPDATE tbl_user SET "..UserWord.." = "..UserWord.." + "..math.floor(tonumber(resset["0_3"])*TaxRate).." WHERE CdKey = '"..resset["0_0"].."'"
                                                        SQL.Run(upline)
                                                        Char.GiveItem(player,GoldItemid,tonumber(resset["0_2"]))
                                                        NLG.SystemMessage(player,"购买"..resset["0_1"].."寄售的"..resset["0_2"]..GoldItemName..",花费"..resset["0_3"].."金币。")
                                                else
                                                        NLG.SystemMessage(player,"您的金币不足。")
                                                end
                                        end
                                else
                                        NLG.SystemMessage(player,"错误的内部编号,您选定的"..GoldItemName.."可能已被他人买走。")
                                end
                        else
                                NLG.SystemMessage(player,"您的帐号只能包括大小写字母和数字。")
                        end
                else
                        NLG.SystemMessage(player,"你的东西太多了。")
                end
        else
                NLG.SystemMessage(player,"不能输入负数。")
        end
end

function GetCardInfo()
        local query = "SELECT "..SqlInfo[2]..","..SqlInfo[3]..","..SqlInfo[4]..","..SqlInfo[5]..","..SqlInfo[6]..","..SqlInfo[7].." FROM "..SqlInfo[1].." ORDER BY "..SqlInfo[7].." ASC , "..SqlInfo[2].." ASC"
        --local query = "SELECT * FROM "..SqlInfo[1].." ORDER BY "..SqlInfo[7] --.." , "..SqlInfo[2] --.." ASC"
        local resset = SQL.Run(query)
        return resset
end

function GetSelfCard(player)
        local cdkey = Char.GetData(player,%对象_CDK%)
        if isrealstr(cdkey) then
                local query = "SELECT "..SqlInfo[2]..","..SqlInfo[3]..","..SqlInfo[4]..","..SqlInfo[5]..","..SqlInfo[6]..","..SqlInfo[7].." FROM "..SqlInfo[1].." WHERE "..SqlInfo[3].." = '"..cdkey.."'"
                --local query = "SELECT * FROM "..SqlInfo[1].." WHERE "..SqlInfo[3].." = '"..cdkey.."'"
                local resset = SQL.Run(query)
                return resset
        else
                NLG.SystemMessage(player,"您的帐号只能包括大小写字母和数字。")
        end
end

function SellGoldNpcTalkEvent(npc,player)
        if NLG.CanTalk(npc,player) then
                local msg = "2\n蔚蓝魔力金卡寄售系统:\n\n        ★★使用说明★★\n        ★★寄售"..GoldItemName.."★★\n        ★★购买"..GoldItemName.."★★\n        ★★领取金币★★\n        ★★取消寄售★★"
                NLG.ShowWindowTalked(player,npc,2,2,0,msg)
        end
end

function SellGoldNpcWindowTalkedCallBack(npc,player,Seqno,Select,Data)
        if NLG.CanTalk(npc,player) then
                if Select ~= 2 then
                        if Seqno == 0 then
                                if tonumber(Data) == 1 then
                                        NLG.ShowWindowTalked(player,npc,0,2,0,SellGoldMainMsg)
                                elseif tonumber(Data) == 2 then
                                        local msg = "\n请输入您要寄售的"..GoldItemName.."数量和总价\n\n格式:数量/总价\n\n例:5/100000\n表示寄售5个"..GoldItemName..",总价100000(十万)金币。\n寄售后显示为:\n金卡*5 总价100000金币(单价20000)"
                                        NLG.ShowWindowTalked(player,npc,1,3,1,msg)
                                elseif tonumber(Data) == 3 then
                                        local tempint = GetCardInfo()
                                        SellGoldTalkInfo[player] = nil
                                        SellGoldTalkInfo[player] = {}
                                        if type(tempint) == "table" then
                                                for i = 0,599 do
                                                        if tempint[i.."_0"] then
                                                                SellGoldTalkInfo[player][i+1] = {tempint[i.."_0"],tempint[i.."_1"],tempint[i.."_2"],tempint[i.."_3"],tempint[i.."_4"],tempint[i.."_5"]}
                                                        else
                                                                break
                                                        end
                                                end
                                                SellGoldNpcWindowTalkedCallBack(npc,player,11,16,0)
                                        else
                                                NLG.SystemMessage(player,"当前没有正在出售的"..GoldItemName.."。")
                                        end
                                elseif tonumber(Data) == 4 then
                                        local cdkey = Char.GetData(player,%对象_CDK%)
                                        if isrealstr(cdkey) then
                                                local query = "SELECT "..UserWord.." FROM tbl_user WHERE CdKey = '"..cdkey.."'"
                                                local resset = SQL.Run(query);
                                                if type(resset) == "table" then
                                                        local sqlgold = tonumber(resset["0_0"]) or 0
                                                        if sqlgold > 0 then
                                                                local playergold = Char.GetData(player,%对象_金币%)
                                                                local givegold = 0
                                                                if playergold + sqlgold > MaxGold then
                                                                        givegold = MaxGold - playergold
                                                                else
                                                                        givegold = sqlgold
                                                                end
                                                                if givegold > 0 then
                                                                        query = "UPDATE tbl_user SET "..UserWord.." = "..UserWord.." - "..givegold.." WHERE CdKey = '"..cdkey.."'"
                                                                        resset = SQL.Run(query)
                                                                        if resset == 0 then
                                                                                Char.SetData(player,%对象_金币%,playergold+givegold)
                                                                                NLG.UpChar(player)
                                                                                NLG.SystemMessage(player,"领取了"..givegold.."金币。")
                                                                        else
                                                                                NLG.SystemMessage(player,"数据库写入失败。")
                                                                        end
                                                                else
                                                                        NLG.SystemMessage(player,"你拿不了更多的金币了。")
                                                                end
                                                        else
                                                                NLG.SystemMessage(player,"你没有未领取的金币。")
                                                        end
                                                else
                                                        NLG.SystemMessage(player,"数据库读取失败。")
                                                end
                                        else
                                                NLG.SystemMessage(player,"您的帐号只能包含数字和字母。")
                                        end
                                elseif tonumber(Data) == 5 then
                                        local tempint = GetSelfCard(player)
                                        SellGoldTalkInfo[player] = nil
                                        SellGoldTalkInfo[player] = {}
                                        if type(tempint) == "table" then
                                                for i = 0,599 do
                                                        if tempint[i.."_0"] then
                                                                SellGoldTalkInfo[player][i+1] = {tempint[i.."_0"],tempint[i.."_1"],tempint[i.."_2"],tempint[i.."_3"],tempint[i.."_4"],tempint[i.."_5"]}
                                                        else
                                                                break
                                                        end
                                                end
                                                SellGoldNpcWindowTalkedCallBack(npc,player,11,16,0)
                                        else
                                                NLG.SystemMessage(player,"你没有正在寄售的"..GoldItemName.."。")                                       
                                        end
                                end
                        elseif Seqno == 1 then
                                if Select == 1 then
                                        local splitpos = string.find(Data,"/")
                                        if splitpos then
                                                PutCard(player,tonumber(string.sub(Data,1,splitpos-1)),tonumber(string.sub(Data,splitpos+1,-1)))
                                        else
                                                NLG.SystemMessage(player,"请按照正确的格式输入。")
                                        end
                                end
                        elseif Seqno >= 10 and Seqno < 110  then
                                if #SellGoldTalkInfo[player] > 0 then
                                        local nowpage = (Seqno-10)
                                        local nextseq
                                        local nextsel = 2
                                        local msg = ""
                                        if Select == 0 then
                                                if tonumber(Data) and SellGoldTalkInfo[player][nowpage*6+tonumber(Data)] then
                                                        msg = "请确认您的选择:\n\n出售者:"..SellGoldTalkInfo[player][nowpage*6+tonumber(Data)][3].."\n数量:"..SellGoldTalkInfo[player][nowpage*6+tonumber(Data)][4]..GoldItemName.."\n总价:"..SellGoldTalkInfo[player][nowpage*6+tonumber(Data)][5].."金币\n单价:"..SellGoldTalkInfo[player][nowpage*6+tonumber(Data)][6].."金币"
                                                        SellGoldTalkInfo[player] = SellGoldTalkInfo[player][nowpage*6+tonumber(Data)][1]
                                                        NLG.ShowWindowTalked(player,npc,0,12,120,msg)
                                                else
                                                        NLG.SystemMessage(player,"请选择正确的选项。")
                                                end
                                        else
                                                if Select == 32 then
                                                        nowpage = nowpage + 1
                                                elseif Select == 16 then
                                                        nowpage = nowpage - 1
                                                end
                                                if #SellGoldTalkInfo[player] > (nowpage+1)*6 then
                                                        nextsel = nextsel + 32
                                                end
                                                if nowpage > 0 then
                                                        nextsel = nextsel + 16
                                                end
                                                msg = "2\n请选择你要购买的"..GoldItemName.."\n"
                                                for i = 1,6 do
                                                        if SellGoldTalkInfo[player][nowpage*6+i] then
                                                                msg = msg.."\n"..i.."."..GoldItemName.."*"..SellGoldTalkInfo[player][nowpage*6+i][4].." 总价"..SellGoldTalkInfo[player][nowpage*6+i][5].."金币 (单价"..SellGoldTalkInfo[player][nowpage*6+i][6]..")"
                                                        else
                                                                msg = msg.."\n".."无"
                                                        end
                                                end
                                                NLG.ShowWindowTalked(player,npc,2,nextsel,nowpage+10,msg)
                                        end
                                else
                                        NLG.SystemMessage(player,"当前没有正在出售的"..GoldItemName.."。")
                                end
                        elseif Seqno == 120 then
                                if Select == 4 then
                                        BuyCard(player,SellGoldTalkInfo[player])
                                end
                        end
                end
        end
end

function SellGoldNpcInit(npc)
        Char.SetData(npc,%对象_形象%,SellGoldNpcInfo[1])
        Char.SetData(npc,%对象_原形%,SellGoldNpcInfo[1])
        Char.SetData(npc,%对象_地图%,SellGoldNpcInfo[4])
        Char.SetData(npc,%对象_X%,SellGoldNpcInfo[5])
        Char.SetData(npc,%对象_Y%,SellGoldNpcInfo[6])
        Char.SetData(npc,%对象_方向%,SellGoldNpcInfo[3])
        Char.SetData(npc,%对象_原名%,SellGoldNpcInfo[2])
        Char.SetTalkedEvent(nil,"SellGoldNpcTalkEvent",npc)
        Char.SetWindowTalkedEvent(nil,"SellGoldNpcWindowTalkedCallBack",npc)
        NLG.UpChar(npc)
        return true
end

if SellGoldNpcIndex == nil then
        SellGoldNpcIndex = NL.CreateNpc(nil,"SellGoldNpcInit")
end

回复

使用道具 举报

Archiver|魔力研究社

GMT+8, 2025-5-12 18:07 , Processed in 0.112647 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表