• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

TFS 1.X+ Help with war system talkaction

icekis

Member
Joined
Jan 18, 2018
Messages
91
Reaction score
5
I am trying to use this talkaction in my OTX 3.

Lua:
function onSay(cid, words, param)
        local player = Player(cid)
        local guild = player:getGuild()
        if(guild == nil) then
                player:sendCancelMessage("You need to be in a guild in order to execute this talkaction.")
                return false
        end
        local guild = getPlayerGuildId(cid)
        if not guild or (player:getGuildLevel() < 3) then
                player:sendCancelMessage("You cannot execute this talkaction.")
                return false
        end
        local t = string.split(param, ",")
        if(not t[2]) then     
                player:sendChannelMessage("", "Not enough param(s).", TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
                return false
        end
        local enemy = getGuildId(t[2])
        if(not enemy) then
                player:sendChannelMessage("", "Guild \"" .. t[2] .. "\" does not exists.", TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
                return false
        end
        if(enemy == guild) then
                player:sendChannelMessage("", "You cannot perform war action on your own guild.", TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
                return false
        end
        local enemyName, tmp = "", db.storeQuery("SELECT `name` FROM `guilds` WHERE `id` = " .. enemy)
        if tmp ~= false then
                enemyName = result.getDataString(tmp, "name")
                result.free(tmp)
        end
        if(isInArray({"accept", "reject", "cancel"}, t[1])) then
                local query = "`guild1` = " .. enemy .. " AND `guild2` = " .. guild
                if(t[1] == "cancel") then
                        query = "`guild1` = " .. guild .. " AND `guild2` = " .. enemy
                end
                tmp = db.storeQuery("SELECT `id`, `started`, `ended`, `payment` FROM `guild_wars` WHERE " .. query .. " AND `status` = 0")
                if(tmp == false) then
                        player:sendChannelMessage("", "Currently there's no pending invitation for a war with " .. enemyName .. ".", TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
                        return false
                end
                if(t[1] == "accept") then
                        local _tmp = db.storeQuery("SELECT `balance` FROM `guilds` WHERE `id` = " .. guild)
                        local state = result.getDataInt(_tmp, "balance") < result.getDataInt(tmp, "payment")
                        result.free(_tmp)
                        if(state) then
                                player:sendChannelMessage("", "Your guild balance is too low to accept this invitation.", TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
                                return false
                        end
                        db.query("UPDATE `guilds` SET `balance` = `balance` - " .. result.getDataInt(tmp, "payment") .. " WHERE `id` = " .. guild)
                end
                query = "UPDATE `guild_wars` SET "
                local msg = "accepted " .. enemyName .. " invitation to war."
                if(t[1] == "reject") then
                        query = query .. "`ended` = " .. os.time() .. ", `status` = 2"
                        msg = "rejected " .. enemyName .. " invitation to war."
                elseif(t[1] == "cancel") then
                        query = query .. "`ended` = " .. os.time() .. ", `status` = 3"
                        msg = "canceled invitation to a war with " .. enemyName .. "."
                else
                        query = query .. "`started` = " .. os.time() .. ", `ended` = " .. (result.getDataInt(tmp, "ended") > 0 and (os.time() + ((result.getDataInt(tmp, "started") - result.getDataInt(tmp, "ended")) / 86400)) or 0) .. ", `status` = 1"
                end
                query = query .. " WHERE `id` = " .. result.getDataInt(tmp, "id")
                result.free(tmp)
                db.query(query)
                broadcastMessage(getPlayerGuildName(cid) .. " has " .. msg, MESSAGE_EVENT_ADVANCE)
                return false
        end
        if(t[1] == "invite") then
                local str = ""
                tmp = db.storeQuery("SELECT `guild1`, `status` FROM `guild_wars` WHERE `guild1` IN (" .. guild .. "," .. enemy .. ") AND `guild2` IN (" .. enemy .. "," .. guild .. ") AND `status` IN (0, 1)")
        if(tmp ~= false) then
                      
                        if(result.getDataInt(tmp, "status") == 0) then
                                if(result.getDataInt(tmp, "guild1") == guild) then
                                        str = "You have already invited " .. enemyName .. " to war."
                                else
                                        str = enemyName .. " have already invited you to war."
                                end
                        else
                                str = "You are already on a war with " .. enemyName .. "."
                        end
                        result.free(tmp)
                end
                if(str ~= "") then
                        player:sendChannelMessage("", str, TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
                        return false
                end
                local frags = tonumber(t[3])
                if(frags ~= nil) then
                        frags = math.max(10, math.min(1000, frags))
                else
                        frags = 100
                end
                local payment = tonumber(t[4])
                if(payment ~= nil) then
                        payment = math.floor(payment)+1000
                        tmp = db.storeQuery("SELECT `balance` FROM `guilds` WHERE `id` = " .. guild)
                        local state = result.getDataInt(tmp, "balance") < payment
                        result.free(tmp)
                        if(state) then
                                player:sendChannelMessage("", "Your guild balance is too low for such payment.", TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
                                return false
                        end
                        db.query("UPDATE `guilds` SET `balance` = `balance` - " .. payment .. " WHERE `id` = " .. guild)
                else
                        payment = 0
                end
                local begining, ending = os.time(), tonumber(t[5])
                if(ending ~= nil and ending ~= 0) then
                        ending = begining + (ending * 86400)
                else
                        ending = 0
                end
                db.query("INSERT INTO `guild_wars` (`guild1`, `guild2`, `started`, `ended`, `frags`, `payment`) VALUES (" .. guild .. ", " .. enemy .. ", " .. begining .. ", " .. ending .. ", " .. frags .. ", " .. payment .. ");")
                broadcastMessage(getPlayerGuildName(cid) .. " has invited " .. enemyName .. " to war till " .. frags .. " frags.", MESSAGE_EVENT_ADVANCE)
                return false
        end
        if(not isInArray({"end", "finish"}, t[1])) then
                return false
        end
        local status = (t[1] == "end" and 1 or 4)
        tmp = db.storeQuery("SELECT `id` FROM `guild_wars` WHERE `guild1` = " .. guild .. " AND `guild2` = " .. enemy .. " AND `status` = " .. status)
        if(tmp ~= false) then
                local query = "UPDATE `guild_wars` SET `ended` = " .. os.time() .. ", `status` = 5 WHERE `id` = " .. result.getDataInt(tmp, "id")
                result.free(tmp)
                db.query(query)
                broadcastMessage(getPlayerGuildName(cid) .. " has " .. (status == 4 and "mend fences" or "ended up a war") .. " with " .. enemyName .. ".", MESSAGE_EVENT_ADVANCE)
                return false
        end
        if(status == 4) then
                player:sendChannelMessage("", "Currently there's no pending war truce from " .. enemyName .. ".", TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
                return false
        end
        tmp = db.storeQuery("SELECT `id`, `ended` FROM `guild_wars` WHERE `guild1` = " .. enemy .. " AND `guild2` = " .. guild .. " AND `status` = 1")
        if(tmp ~= false) then
                if(result.getDataInt(tmp, "ended") > 0) then
                        result.free(tmp)
                        player:sendChannelMessage("", "You cannot request ending for war with " .. enemyName .. ".", TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
                        return false
                end
                local query = "UPDATE `guild_wars` SET `status` = 4, `ended` = " .. os.time() .. " WHERE `id` = " .. result.getDataInt(tmp, "id")
                result.free(tmp)
                db.query(query)
                broadcastMessage(getPlayerGuildName(cid) .. " has signed an armstice declaration on a war with " .. enemyName .. ".", MESSAGE_EVENT_ADVANCE)
                return false
        end
      
        player:sendChannelMessage("", "Currently there's no active war with " .. enemyName .. ".", TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
        return false
end

But when i type /war invite, Guild, Frags, nothing happens.

Can someone help me?
 
Yes.

Lua:
<talkaction words="/war" script="war.lua"/>

If i don't have a guild, the script tell me. if i have, nothing happens.
u need to ADD the war system in the otx but i don't know how i know how to make in the tfs :D
 
try this script iam using it and haven't faced any bugs from it before.
Lua:
    GUILDLEVEL_LEADER = 3
    CHANNEL_GUILD = 0x00

    function getGuildId(name)
        local result_ptr = db.storeQuery("SELECT `id` FROM `guilds` WHERE `name` = " .. db.escapeString(name) .. ";")
        if not result_ptr then
            return nil
        end
        return result.getDataInt(result_ptr, 'id')
    end

    function getGuildNameById(id)
        local result_ptr = db.storeQuery("SELECT `name` FROM `guilds` WHERE `id` = " .. id .. ";")
        if not result_ptr then
            return nil
        end
        return result.getDataString(result_ptr, 'name')
    end

    function doCreateInvitationGuildWar(g, status, started, ended)
        local queryString = "INSERT INTO `guild_wars` (`guild1`, `guild2`, `name1`, `name2`, `status`, `started`, `ended`) "
        local queryValues = "VALUES (" .. g[1].id .. ", " .. g[2].id .. ", " .. db.escapeString(g[1].name) .. ", " .. db.escapeString(g[2].name) .. ", " .. status .. ", " .. started .. ", " .. ended .. ");"
        local result_ptr = db.query(queryString .. queryValues)
        if not result_ptr then
            return false
        end
        return true
    end

    function setGuildWarStatus(status, query)
        local queryString = "UPDATE `guild_wars` SET `status` = " .. status .. ", `ended` = 0 WHERE " .. query .. " AND `status` = 0;"
        local result_ptr = db.query(queryString)
        if not result_ptr then
            return false
        end
        return true
    end

    function setGuildWarDelete(query)
        local queryString = "DELETE FROM `guild_wars` WHERE " .. query .. ";"
        local result_ptr = db.query(queryString)
        if not result_ptr then
            return false
        end
        return true
    end

    function onSay(player, words, param)
    local storage = 54073 -- Make sure to select non-used storage. This is used to prevent SQL load attacks.
    local cooldown = 15 -- in seconds.

    if player:getStorageValue(storage) <= os.time() then
        player:setStorageValue(storage, os.time() + cooldown)
   
        local guild = player:getGuild()
        if not guild or player:getGuildLevel() < GUILDLEVEL_LEADER then
            player:sendCancelMessage('You cannot execute this talkaction.')
            return false
        end

        local split = param:split(',')
        if not split[2] then
            sendGuildChannelMessage(guild:getId(), TALKTYPE_CHANNEL_R1, 'Not enough param(s).')
            return false
        end

        local action = split[1]
        local enemy = getGuildId(split[2])
        if not enemy then
            sendGuildChannelMessage(guild:getId(), TALKTYPE_CHANNEL_R1, 'Guild \'' .. split[2] .. '\' does not exists.')
            return false
        end

        if enemy == guild:getId() then
            sendGuildChannelMessage(guild:getId(), TALKTYPE_CHANNEL_R1, 'You cannot perform war action on your own guild.')
            return false
        end

        local enemyName = getGuildNameById(enemy)
        if action == 'accept' then

            local query = "`guild1` = " .. enemy .. " AND `guild2` = " .. guild:getId()
            local tmp = db.storeQuery("SELECT `id`, `ended`, `started` FROM `guild_wars` WHERE " .. query .. " AND `status` = 0;")
            if not tmp then
                sendGuildChannelMessage(guild:getId(), TALKTYPE_CHANNEL_R1, "Currently there's no pending invitation for a war with " .. enemyName .. ".")
                return false
            end

            sendGuildChannelMessage(guild:getId(), TALKTYPE_CHANNEL_R1, 'Has aceptado la invitacion de ' .. enemyName)
            setGuildWarStatus(1, query)
            return false

        end

        if action == 'cancel' or action == 'reject' then

            local query = "`guild1` = " .. enemy .. " AND `guild2` = " .. guild:getId()
            local tmp = db.storeQuery("SELECT `id`, `ended`, `started` FROM `guild_wars` WHERE " .. query .. ";")
            if not tmp then
                sendGuildChannelMessage(guild:getId(), TALKTYPE_CHANNEL_R1, "Currently there's no pending invitation for a war with " .. enemyName .. ".")
                return false
            end

            sendGuildChannelMessage(guild:getId(), TALKTYPE_CHANNEL_R1, 'Has cancelado la invitacion de ' .. enemyName)
            setGuildWarDelete(query)
            return false

        end

        if action == 'invite' then
            local str = ''

            local query = "`guild1` = " .. guild:getId() .. " AND `guild2` = " .. enemy
            local result_ptr = db.storeQuery("SELECT `id` FROM `guild_wars` WHERE " .. query .. ";")
            if result_ptr then
                str = enemyName .. " have already invited you to war."
                sendGuildChannelMessage(guild:getId(), TALKTYPE_CHANNEL_R1, str)
                return false
            else
                str = "You are already on a war with " .. enemyName .. "."
            end

            if str ~= '' then
                sendGuildChannelMessage(guild:getId(), TALKTYPE_CHANNEL_R1, str)
            end

            local started, ended = os.time(), tonumber(split[3])
            if (ended ~= nil and ended ~= 0) then
                ended = started + (ended * 86400)
            else
                ended = 0
            end

            broadcastMessage(guild:getName() .. " has invited " .. enemyName .. " to war.", MESSAGE_EVENT_ADVANCE)
            local g = {}
            g[1], g[2] = {}, {}
            g[1].id = guild:getId()
            g[1].name = guild:getName()
            g[2].id = enemy
            g[2].name = enemyName
            doCreateInvitationGuildWar(g, 0, started, ended)
        end
        else
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Can only be executed once every " .. cooldown .. " seconds. Remaining cooldown: " .. player:getStorageValue(storage) - os.time())
     end     
        return false
    end
[code]
 
u need to ADD the war system in the otx but i don't know how i know how to make in the tfs :D

Hi! I think otx already have these modifications.. or am i wrong?


try this script iam using it and haven't faced any bugs from it before.
Lua:
    GUILDLEVEL_LEADER = 3
    CHANNEL_GUILD = 0x00

    function getGuildId(name)
        local result_ptr = db.storeQuery("SELECT `id` FROM `guilds` WHERE `name` = " .. db.escapeString(name) .. ";")
        if not result_ptr then
            return nil
        end
        return result.getDataInt(result_ptr, 'id')
    end

    function getGuildNameById(id)
        local result_ptr = db.storeQuery("SELECT `name` FROM `guilds` WHERE `id` = " .. id .. ";")
        if not result_ptr then
            return nil
        end
        return result.getDataString(result_ptr, 'name')
    end

    function doCreateInvitationGuildWar(g, status, started, ended)
        local queryString = "INSERT INTO `guild_wars` (`guild1`, `guild2`, `name1`, `name2`, `status`, `started`, `ended`) "
        local queryValues = "VALUES (" .. g[1].id .. ", " .. g[2].id .. ", " .. db.escapeString(g[1].name) .. ", " .. db.escapeString(g[2].name) .. ", " .. status .. ", " .. started .. ", " .. ended .. ");"
        local result_ptr = db.query(queryString .. queryValues)
        if not result_ptr then
            return false
        end
        return true
    end

    function setGuildWarStatus(status, query)
        local queryString = "UPDATE `guild_wars` SET `status` = " .. status .. ", `ended` = 0 WHERE " .. query .. " AND `status` = 0;"
        local result_ptr = db.query(queryString)
        if not result_ptr then
            return false
        end
        return true
    end

    function setGuildWarDelete(query)
        local queryString = "DELETE FROM `guild_wars` WHERE " .. query .. ";"
        local result_ptr = db.query(queryString)
        if not result_ptr then
            return false
        end
        return true
    end

    function onSay(player, words, param)
    local storage = 54073 -- Make sure to select non-used storage. This is used to prevent SQL load attacks.
    local cooldown = 15 -- in seconds.

    if player:getStorageValue(storage) <= os.time() then
        player:setStorageValue(storage, os.time() + cooldown)
  
        local guild = player:getGuild()
        if not guild or player:getGuildLevel() < GUILDLEVEL_LEADER then
            player:sendCancelMessage('You cannot execute this talkaction.')
            return false
        end

        local split = param:split(',')
        if not split[2] then
            sendGuildChannelMessage(guild:getId(), TALKTYPE_CHANNEL_R1, 'Not enough param(s).')
            return false
        end

        local action = split[1]
        local enemy = getGuildId(split[2])
        if not enemy then
            sendGuildChannelMessage(guild:getId(), TALKTYPE_CHANNEL_R1, 'Guild \'' .. split[2] .. '\' does not exists.')
            return false
        end

        if enemy == guild:getId() then
            sendGuildChannelMessage(guild:getId(), TALKTYPE_CHANNEL_R1, 'You cannot perform war action on your own guild.')
            return false
        end

        local enemyName = getGuildNameById(enemy)
        if action == 'accept' then

            local query = "`guild1` = " .. enemy .. " AND `guild2` = " .. guild:getId()
            local tmp = db.storeQuery("SELECT `id`, `ended`, `started` FROM `guild_wars` WHERE " .. query .. " AND `status` = 0;")
            if not tmp then
                sendGuildChannelMessage(guild:getId(), TALKTYPE_CHANNEL_R1, "Currently there's no pending invitation for a war with " .. enemyName .. ".")
                return false
            end

            sendGuildChannelMessage(guild:getId(), TALKTYPE_CHANNEL_R1, 'Has aceptado la invitacion de ' .. enemyName)
            setGuildWarStatus(1, query)
            return false

        end

        if action == 'cancel' or action == 'reject' then

            local query = "`guild1` = " .. enemy .. " AND `guild2` = " .. guild:getId()
            local tmp = db.storeQuery("SELECT `id`, `ended`, `started` FROM `guild_wars` WHERE " .. query .. ";")
            if not tmp then
                sendGuildChannelMessage(guild:getId(), TALKTYPE_CHANNEL_R1, "Currently there's no pending invitation for a war with " .. enemyName .. ".")
                return false
            end

            sendGuildChannelMessage(guild:getId(), TALKTYPE_CHANNEL_R1, 'Has cancelado la invitacion de ' .. enemyName)
            setGuildWarDelete(query)
            return false

        end

        if action == 'invite' then
            local str = ''

            local query = "`guild1` = " .. guild:getId() .. " AND `guild2` = " .. enemy
            local result_ptr = db.storeQuery("SELECT `id` FROM `guild_wars` WHERE " .. query .. ";")
            if result_ptr then
                str = enemyName .. " have already invited you to war."
                sendGuildChannelMessage(guild:getId(), TALKTYPE_CHANNEL_R1, str)
                return false
            else
                str = "You are already on a war with " .. enemyName .. "."
            end

            if str ~= '' then
                sendGuildChannelMessage(guild:getId(), TALKTYPE_CHANNEL_R1, str)
            end

            local started, ended = os.time(), tonumber(split[3])
            if (ended ~= nil and ended ~= 0) then
                ended = started + (ended * 86400)
            else
                ended = 0
            end

            broadcastMessage(guild:getName() .. " has invited " .. enemyName .. " to war.", MESSAGE_EVENT_ADVANCE)
            local g = {}
            g[1], g[2] = {}, {}
            g[1].id = guild:getId()
            g[1].name = guild:getName()
            g[2].id = enemy
            g[2].name = enemyName
            doCreateInvitationGuildWar(g, 0, started, ended)
        end
        else
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Can only be executed once every " .. cooldown .. " seconds. Remaining cooldown: " .. player:getStorageValue(storage) - os.time())
     end    
        return false
    end
[code]

Thank you bro.. I tested this script and got the same error.

Using the command /war many times i got:

Can only be executed once every 15 seconds. Remaining cooldown: 10

But, if i use /war invite, Test, 10, nothing happens either.

Which distro do you use?
 
tfs 1.2 , can you tell me the error ?


No error appears, it just does anything.

When i type /war with a guild, got Not enough param(s).

When i type /war without a guild, i got You cannot execute this talkaction

When i type /war many times, i got Can only be executed once every 15 seconds. Remaining cooldown: 12

When i put any parameter(invite, cancel, accept), it returns nothing.

I am using OTX 3, and i believe already have the war system modifications.

any suggestion?
 
Back
Top