• 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 0.X War Castle Problem

Daniel Kopeć

Member
Joined
Dec 8, 2018
Messages
110
Solutions
2
Reaction score
11
Location
Poland
The guild that won the castle can enter the castle and exp. It works fine.
But the problem is that players who are not in the guild can enter the castle and exp also too.
Only players who are in the guild but do not own the castle cannot enter.

piece of script w-castle MOD:

XML:
<action actionid='4992' event='script'>
        <![CDATA[
            domodlib('castle_config')
            domodlib('castle_lib')

            function onUse(cid, item, fromPosition, itemEx, toPosition)
                if castleCfg.castleGetReward ~= false then
                    if getPlayerGuildName(cid) == getCastleOwner() then
                        if getPlayerLevel(cid) >= castleCfg.levelToGetReward then
                            if getCreatureStorage(cid, item.actionid) <= os.time() then
                                local cash, rewardItem = math.random(castleCfg.cash.min, castleCfg.cash.max), castleCfg.itemsToGive[math.random(#castleCfg.itemsToGive)]

                                doPlayerAddItem(cid, 2160, cash)
                                doPlayerAddItem(cid, rewardItem, 1)
                                doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, 'Dostajesz ' .. cash .. ' crystal coins and ' .. getItemNameById(rewardItem) .. ' od twojego krola!')
                                doCreatureSetStorage(cid, item.actionid, os.time() + 60 * 60 * 24)
                            else
                                doPlayerSendCancel(cid, 'Otrzymales juz swoj prezent! Mozesz dostac to ponownie za ' ..  secondsToClock(math.abs(os.difftime(getCreatureStorage(cid, item.actionid), os.time())))  .. '.')
                            end
                        else
                            doPlayerSendCancel(cid, 'Twoj poziom jest za niski. Powinienes miec ' .. castleCfg.levelToGetReward .. '.')
                        end
                    else
                        doPlayerSendCancel(cid, 'Nie masz dostepu do tej nagrody!')
                        doTeleportThing(cid, getTownTemplePosition(getPlayerTown(cid)), true)
                        doCreatureSetStorage(cid, castleCfg.storage.winner, -1)
                        doCreatureSetStorage(cid, castleCfg.storage.playersEvent, -1)
                    end
                end
                return true
            end
        ]]>
    </action>

    <movement type='StepIn' actionid='4991' event='script'>
        <![CDATA[
            domodlib('castle_config')
            domodlib('castle_lib')

            function onStepIn(cid, item, position)
                if getPlayerGuildName(cid) ~= getCastleOwner() then
                    doPlayerSendCancel(cid, 'Nie masz dostepu, aby sie tam udac!')
                    doTeleportThing(cid, getTownTemplePosition(getPlayerTown(cid)), true)
                    return false
                end
                return true
            end
        ]]>
    </movement>

castle stepin.png

How to fix it? How to correct the code to check if the player is in the guild?
So that players without a guild could not pass.
Post automatically merged:

Or maybe someone has a script that only passes people who are in the guild.
So I would have put it on the sqm beforehand.
There where the GOD Verbania is standing in the photo.
 
Last edited:

worthdavi

Member
Joined
Aug 17, 2019
Messages
15
Reaction score
10
Location
Brazil
GitHub
worthdavi
Can you send me your getCastleOwner function?

Lua:
 function onStepIn(cid, item, position)
     if getPlayerGuildId(cid) ~= getCastleOwner() then
         doPlayerSendCancel(cid, 'Nie masz dostepu, aby sie tam udac!')
         doTeleportThing(cid, getTownTemplePosition(getPlayerTown(cid)), true)
         return false
    end
    return true
end
 
OP
Daniel Kopeć

Daniel Kopeć

Member
Joined
Dec 8, 2018
Messages
110
Solutions
2
Reaction score
11
Location
Poland
Can you send me your getCastleOwner function?

Lua:
 function onStepIn(cid, item, position)
     if getPlayerGuildId(cid) ~= getCastleOwner() then
         doPlayerSendCancel(cid, 'Nie masz dostepu, aby sie tam udac!')
         doTeleportThing(cid, getTownTemplePosition(getPlayerTown(cid)), true)
         return false
    end
    return true
end
I don't know exactly which part of the script you mean.

XML:
function getCastleOwner()
                local res, guildName = '', ''

                tmp = db.getResult("SELECT `guild_name` FROM `castle` WHERE `owner` > 0")
                if tmp:getID() ~= -1 then
                    guildName = guildName .. tmp:getDataString("guild_name")
                    if guildName == 'No one' then
                        tmp:free()
                    else
                        res = guildName
                    end
                end
                return res
            end

            function getCastleWarId()
                tmp = db.getResult("SELECT `id` FROM `castle` ORDER BY `id` DESC LIMIT 1")
                local res = tmp:getDataInt("id")
                tmp:free()
                return res
            end

            function doSetCastleOwner(guildId)
                db.executeQuery("INSERT INTO `castle` (`id`, `owner`, `guild_name`)    VALUES ('NULL', '1', '" .. tostring(getCastleGuildName(guildId)) .. "');")
            end

XML:
<event type='login' name='castleLogin' event='script'>
        <![CDATA[
            domodlib('castle_config')
            domodlib('castle_lib')

            function onLogin(cid)
                if getStorage(castleCfg.storage.event) == 1 then
                    if getCreatureStorage(cid, castleCfg.storage.winner) > 0 then
                        doCreatureSetStorage(cid, castleCfg.storage.winner, -1)
                        doCreatureSetStorage(cid, castleCfg.storage.playersEvent, -1)
                        doTeleportThing(cid, getTownTemplePosition(getPlayerTown(cid)), true)
                    end
                else
                    if getCastleOwner() ~= '' then
                        if getPlayerGuildName(cid) == getCastleOwner() then
                            doCreatureSetStorage(cid, castleCfg.storage.winner, 1)
                            doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, 'Twoja gildia wygrala wojne o zamek.')
                        end
                        
                        if getCreatureStorage(cid, castleCfg.storage.winner) > 0 then
                            if getPlayerGuildName(cid) ~= getCastleOwner() then
                                doCreatureSetStorage(cid, castleCfg.storage.winner, -1)
                            end
                        end
                    end
                    
                    if getCreatureStorage(cid, castleCfg.storage.playersEvent) == 1 then
                        doCreatureSetStorage(cid, castleCfg.storage.playersEvent, -1)
                        doTeleportThing(cid, getTownTemplePosition(getPlayerTown(cid)), true)
                    end
                end

                registerCreatureEvent(cid, 'castleDeathPlayer')
                return true
            end
        ]]>
 

worthdavi

Member
Joined
Aug 17, 2019
Messages
15
Reaction score
10
Location
Brazil
GitHub
worthdavi
I think it is better using the guild id instead of the guild name when setting the conditions.
But as I do not know how are your database columns, lets debug your code and see if its working properly

Change your onStepIn function to it and watch out your terminal, then copy and paste the messages here
Lua:
function onStepIn(cid, item, position)
    print("Player guild name: " .. getPlayerGuildName(cid) .. "\nGuild owning the castle: " .. getCastleOwner())
    if getPlayerGuildName(cid) ~= getCastleOwner() then
            doPlayerSendCancel(cid, 'Nie masz dostepu, aby sie tam udac!')
            doTeleportThing(cid, getTownTemplePosition(getPlayerTown(cid)), true)
            return false
        end
    return true
end
 
OP
Daniel Kopeć

Daniel Kopeć

Member
Joined
Dec 8, 2018
Messages
110
Solutions
2
Reaction score
11
Location
Poland
I think it is better using the guild id instead of the guild name when setting the conditions.
But as I do not know how are your database columns, lets debug your code and see if its working properly

Change your onStepIn function to it and watch out your terminal, then copy and paste the messages here
Lua:
function onStepIn(cid, item, position)
    print("Player guild name: " .. getPlayerGuildName(cid) .. "\nGuild owning the castle: " .. getCastleOwner())
    if getPlayerGuildName(cid) ~= getCastleOwner() then
            doPlayerSendCancel(cid, 'Nie masz dostepu, aby sie tam udac!')
            doTeleportThing(cid, getTownTemplePosition(getPlayerTown(cid)), true)
            return false
        end
    return true
end
The console shows this information.
I stood on the grate with one player without a guild and one with a guild.
The one with the guild was thrown into the temple and the one without the guild could enter the castle.
So the problem still persists.
castle owning.png
Post automatically merged:

I think it is better using the guild id instead of the guild name when setting the conditions.
But as I do not know how are your database columns, lets debug your code and see if its working properly

Change your onStepIn function to it and watch out your terminal, then copy and paste the messages here
Lua:
function onStepIn(cid, item, position)
    print("Player guild name: " .. getPlayerGuildName(cid) .. "\nGuild owning the castle: " .. getCastleOwner())
    if getPlayerGuildName(cid) ~= getCastleOwner() then
            doPlayerSendCancel(cid, 'Nie masz dostepu, aby sie tam udac!')
            doTeleportThing(cid, getTownTemplePosition(getPlayerTown(cid)), true)
            return false
        end
    return true
end
Maybe add something of that ?

XML:
getPlayerGuildId(cid)
getPlayerGuildLevel(cid)
getPlayerGuildName(cid)
getPlayerGuildNick(cid)
getPlayerGuildRank(cid)
 

worthdavi

Member
Joined
Aug 17, 2019
Messages
15
Reaction score
10
Location
Brazil
GitHub
worthdavi
So the problem is in your getCastleOwner() function.
Lets change its return type to number so.

Before everything, you must add the column 'guild_id' to your castle table.
SQL:
ALTER TABLE `castle`
ADD `guild_id` int DEFAULT 0;

In your castle lib functions, change these functions to it:
Lua:
function getCastleOwner()
    local guildId = 0
    local resultId = db.getResult("SELECT `guild_id` FROM `castle` WHERE `castle`.`owner` > 0")
    if resultId ~= false then
        local guildId = result.getDataInt("guild_id")
        result.free(resultId)
    end
    print("Returning getCastleOwner() value: " .. guildId)
    return guildId
end

function doSetCastleOwner(guildId)
    db.executeQuery("INSERT INTO `castle` (`id`, `owner`, `guild_name`, `guild_id`)    VALUES ('NULL', '1', '" .. tostring(getCastleGuildName(guildId)) .. ", '".. guildId .. "');")
end

PS: After the codes above, you have to win the castle event again. It will set the new values within the castle table.

In your onStepIn function, change to it:
Lua:
 function onStepIn(cid, item, position)
    print("Player with guild id " .. getPlayerGuildId(cid) .. " is trying to enter the castle.")
     if getPlayerGuildId(cid) ~= getCastleOwner() then
         doPlayerSendCancel(cid, 'Nie masz dostepu, aby sie tam udac!')
         doTeleportThing(cid, getTownTemplePosition(getPlayerTown(cid)), true)
         return false
    end
    return true
end
 
Last edited:
OP
Daniel Kopeć

Daniel Kopeć

Member
Joined
Dec 8, 2018
Messages
110
Solutions
2
Reaction score
11
Location
Poland
So the problem is in your getCastleOwner() function.
Lets change its return type to number so.

Before everything, you must add the column 'guild_id' to your castle table.
SQL:
ALTER TABLE `castle`
ADD `guild_id` int DEFAULT 0;

In your castle lib functions, change these functions to it:
Lua:
function getCastleOwner()
    local guildId = 0
    local resultId = db.getResult("SELECT `guild_id` FROM `castle` WHERE `castle`.`owner` > 0")
    if resultId ~= false then
        local guildId = result.getDataInt("guild_id")
        result.free(resultId)
    end
    print("Returning getCastleOwner() value: " .. guildId)
    return guildId
end

function doSetCastleOwner(guildId)
    db.executeQuery("INSERT INTO `castle` (`id`, `owner`, `guild_name`, `guild_id`)    VALUES ('NULL', '1', '" .. tostring(getCastleGuildName(guildId)) .. ", '".. guildId .. "');")
end

PS: After the codes above, you have to win the castle event again. It will set the new values within the castle table.

In your onStepIn function, change to it:
Lua:
 function onStepIn(cid, item, position)
    print("Player with guild id " .. getPlayerGuildId(cid) .. " is trying to enter the castle.")
     if getPlayerGuildId(cid) ~= getCastleOwner() then
         doPlayerSendCancel(cid, 'Nie masz dostepu, aby sie tam udac!')
         doTeleportThing(cid, getTownTemplePosition(getPlayerTown(cid)), true)
         return false
    end
    return true
end

Unfortunately it does not work
castle owning.png
 
Top