• 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] player:getAccountStorageValue | player:setAccountStorageValue

Infernum

Asshole
Support Team
Joined
Feb 14, 2015
Messages
5,384
Best answers
531
Reaction score
3,156
SQL Query:
SQL:
CREATE TABLE `account_storage` ( `account_id` INTEGER(4), `key` INTEGER(10), `value` INTEGER(10) )
data/lib/core/player.lua:
Lua:
function Player.getAccountStorageValue(self, key)
    if type(key) ~= "number" then
        return false
    end
    local query = db.storeQuery("SELECT `value` FROM `account_storage` WHERE `account_id` = ".. self:getAccountId() .." AND `key` = ".. key)
    if not query then
        return false
    end

    local value = result.getDataInt(query, "value")
    result.free(query)
    return value
end

function Player.setAccountStorageValue(self, key, value)
    if type(key) ~= "number" then
        return false
    end
    local query = ""
    if self:getAccountStorageValue(key) then
        query = ("UPDATE `account_storage` SET `value` = %d WHERE `account_id` = %d AND `key` = %d"):format(value, self:getAccountId(), key)
    else
        query = ("INSERT INTO `account_storage` (`account_id`, `key`, `value`) VALUES (%d, %d, %d)"):format(self:getAccountId(), key, value)
    end
    return db.query(query)
end
example:
Lua:
local player = Player("Xeraphus")
player:setAccountStorageValue(10000, 1)
print(player:getAccountStorageValue(10000)) -- prints 1
player:setAccountStorageValue(10000, 23487) -- update pre-existing storage
print(player:getAccountStorageValue(10000)) -- prints 23487
 
OP
Infernum

Infernum

Asshole
Support Team
Joined
Feb 14, 2015
Messages
5,384
Best answers
531
Reaction score
3,156
Thanks, i'm searching this *-*

You can adapt the script? Please?


function onLogin(cid

if getPlayerStorageValue(cid, 10000) <= -1 then
doPlayerAddPremiumDays(cid, 30)
setPlayerStorageValue(cid, 3333, 1)
end
return true

end
please don't use this thread for support. there's a board for it for a reason..
 

Grrrr

Banned User
Joined
Feb 27, 2017
Messages
17
Best answers
2
Reaction score
10
What if the account is deleted? Your code is incomplete.
You need to add additional sql code to remove the `account_storage` entries in the database when the account is deleted from the database.
 

jerryb1988

Member
Joined
Mar 10, 2008
Messages
79
Best answers
0
Reaction score
5
Code:
--
-- Table structure for table `account_storage`
--

CREATE TABLE `account_storage` (
  `account_id` int(4) NOT NULL,
  `key` int(10) DEFAULT NULL,
  `value` int(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `account_storage`
--
ALTER TABLE `account_storage`
  ADD PRIMARY KEY (`account_id`);

--
-- Constraints for dumped tables
--

--
-- Constraints for table `account_storage`
--
ALTER TABLE `account_storage`
  ADD CONSTRAINT `account_storage_ibfk_1` FOREIGN KEY (`account_id`) REFERENCES `accounts` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
 

Apollos

Dude who does stuff
Joined
Apr 22, 2009
Messages
752
Best answers
111
Reaction score
509
Location
United States
Sick script dude.
Edited the query to be a little easier to modify from phpmyadmin and you can add it into your current schema, if it's useful to anyone.

XML:
CREATE TABLE IF NOT EXISTS `account_storage` (
  `account_id` int(4) NOT NULL DEFAULT '0',
  `key` int(10) unsigned NOT NULL DEFAULT '0',
  `value` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`account_id`,`key`),
  FOREIGN KEY (`account_id`) REFERENCES `accounts`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB;
 

Evil Puncker

Unlucky
Premium User
Joined
May 30, 2009
Messages
6,295
Best answers
55
Reaction score
2,091
the possibilities with these functions are numberless, but every time I try to use it as a replacement of the old player:s/getStorageValue I end up with a different error (attempt to perform arithmetic on a boolean value), isn't there a way for it to work any other way beside the example one (that was the only way it worked)? @Infernum
 
OP
Infernum

Infernum

Asshole
Support Team
Joined
Feb 14, 2015
Messages
5,384
Best answers
531
Reaction score
3,156
the possibilities with these functions are numberless, but every time I try to use it as a replacement of the old player:s/getStorageValue I end up with a different error (attempt to perform arithmetic on a boolean value), isn't there a way for it to work any other way beside the example one (that was the only way it worked)? @Infernum
Can you post how you're trying to use it?
This here only works for numbers, the type is int in database and the method to get the data is getDataInt, if you're trying to store some other value it's probably returning a boolean because it cannot get a different type other than int.
 

Evil Puncker

Unlucky
Premium User
Joined
May 30, 2009
Messages
6,295
Best answers
55
Reaction score
2,091
Can you post how you're trying to use it?
Lua:
local login = CreatureEvent("loginCounter")

function login.onLogin(player)
    local v = ""
    local srt = ""
    local secs = ""
    cid = player:getId()
    secs = getPlayerOnlineTime(cid)
    -- converting secs --
    local hours = math.ceil(secs / 3600) - 1
    local minutes = math.ceil((secs - (3600 * hours)) / 60)
    if (minutes == 60) then
        minutes = 0
        hours = hours + 1
    end
    local days = math.ceil(hours / 24) - 1
    hours = math.ceil(hours - (24 * days))
    if (hours == 24) then
        hours = 0
        days = days + 1
    end
    -- end of conversion --
    str = "".. days .." days, ".. hours .." hours, ".. minutes .." minutes."
    local loginsCount = player:getAccountStorageValue(30109)
    player:sendTextMessage(MESSAGE_INFO_DESCR, loginsCount == -1 and "Welcome this is your first login! Enjoy" or "You have logged in ".. loginsCount .." times and played for ".. str .."")
    player:setAccountStorageValue(30109, math.max(0, loginsCount) + 1)
    return true
end

login:register()
and this:
Lua:
local rewards = {
    {1, 36},
    {37, 46, 2148, 80},
    {47, 55, 2148, 50},
    {56, 64, 2671, 5},
    {65, 73, 2789, 5},
    {74, 81, 7620},
    {82, 87, 7618},
    {88, 92, 9811},
    {93, 96, 9808},
    {97, 100, 2213}
}

local crateUsable = Action()

function crateUsable.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if (os.time() - player:getAccountStorageValue(30114)) >= 3600 then
        player:setAccountStorageValue(30114, os.time())
        local chance = math.random(100)
        for i = 1, #rewards do
            local k = rewards[i]
            if chance >= k[1] and chance <= k[2] then
                if k[3] then
                    local item = ItemType(k[3])
                    local count = k[4] or 1
                    player:addItem(k[3], count)
                    local str = ("You found %s %s!"):format(count > 1 and count or item:getArticle(), count > 1 and item:getPluralName() or item:getName())
                    player:say(str, TALKTYPE_MONSTER_SAY, false, player, toPosition)
                else
                    player:say("You found nothing useful.", TALKTYPE_MONSTER_SAY, false, player, toPosition)
                end
                break
            end
        end
    else
        player:say("You found nothing useful.", TALKTYPE_MONSTER_SAY, false, player, toPosition)
    end
    return true
end

crateUsable:id(9661)
crateUsable:register()
and I checked the sql table is empty
 
Top