Untitled

mail@pastecode.io avatar
unknown
lua
6 months ago
3.2 kB
1
Indexable
Never
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

function onCreatureAppear(cid)                npcHandler:onCreatureAppear(cid)            end
function onCreatureDisappear(cid)            npcHandler:onCreatureDisappear(cid)            end
function onCreatureSay(cid, type, msg)            npcHandler:onCreatureSay(cid, type, msg)        end
function onThink()                    npcHandler:onThink()                    end

local softRechargeCost = 10000
local function creatureSayCallback(cid, _type, msg)

    if not npcHandler:isFocused(cid) then
        return false
    end

    local player = Player(cid)
    msg = msg:lower()

    local keyword = function(keyword)

		if type(keyword) == "table" then
			return isInArray(keyword, msg)
		end
		
		return msgcontains(msg, keyword)
	end

    if keyword("no") and npcHandler.topic[cid] == 1 then

        npcHandler:say("See you then, " .. player:getName() .. ".", cid)
        npcHandler:resetNpc(cid)
        npcHandler:releaseFocus(cid)

        return true
    end

    if keyword("soft") or keyword("boots") or keyword("repair") then
        npcHandler:say("Do you want to repair your worn soft boots for " .. format_number(softRechargeCost) .. " gold coins?", cid)
        npcHandler.topic[cid] = 1
        return true
    end

    if keyword("yes") and npcHandler.topic[cid] == 1 then

        local ammoSlotItem = player:getSlotItem(CONST_SLOT_AMMO)
        if not ammoSlotItem or not isInArray({10021, 6530}, ammoSlotItem:getId()) then
            return npcHandler:say("Please put on your ammunition slot the pair of worn soft boots you would like to recharge.", cid)
        end

        local playerMoney = player:getMoney()
        local playerBankMoney = player:getBankBalance() 
        local playerTotalMoney = playerMoney + playerBankMoney

        if playerTotalMoney < softRechargeCost then
            npcHandler:say("You don't have enough money neither in your character nor in your bank balance.", cid)
            npcHandler.topic[cid] = 0
            return true
        end

        local payAmountLeft = softRechargeCost
        local buffer = {}

        if playerMoney > 0 then

            local removeAmount = math.min(softRechargeCost, playerMoney)
            payAmountLeft = payAmountLeft - removeAmount

            player:removeMoney(removeAmount)
            buffer[1] = format_number(removeAmount) .. " gold coins from inventory"
        end

        if payAmountLeft > 0 then

            buffer[#buffer + 1] = format_number(payAmountLeft) .. " gold coins from your bank account"
            player:setBankBalance(player:getBankBalance() - payAmountLeft)

        end

        ammoSlotItem:transform(6132)
        npcHandler:say("Here are you. You have paid " .. table.concat(buffer, " and ") .. ".", cid)
    end

    return true
end

npcHandler:setMessage(MESSAGE_GREET, "Grettins |PLAYERNAME|! I can {repair} your soft boots.")

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
Leave a Comment