· 5 years ago · Jun 24, 2020, 05:12 PM
1--[[
2CREATE TABLE IF NOT EXISTS `account_storage` (
3 `account_id` int(4) NOT NULL DEFAULT '0',
4 `key` int(10) unsigned NOT NULL DEFAULT '0',
5 `value` int(11) NOT NULL DEFAULT '0',
6 PRIMARY KEY (`account_id`,`key`),
7 FOREIGN KEY (`account_id`) REFERENCES `accounts`(`id`) ON DELETE CASCADE
8) ENGINE=InnoDB;
9--
10-- Constraints for table `account_storage`
11--
12ALTER TABLE `account_storage` ADD CONSTRAINT `account_storage_ibfk_1`
13FOREIGN KEY (`account_id`) REFERENCES `accounts` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
14]]--
15
16function Player.getAccountStorageValue(self, key)
17 local query = db.storeQuery("SELECT `value` FROM `account_storage` WHERE `account_id` = ".. self:getAccountId() .." AND `key` = ".. key)
18 if not query then
19 return -1
20 end
21
22 local value = result.getNumber(query, "value")
23 result.free(query)
24 return tonumber(value) or value
25end
26
27function Player.setAccountStorageValue(self, key, value)
28 if not tonumber(key) then
29 return false
30 end
31 local query = ""
32 if self:getAccountStorageValue(key) ~= -1 then
33 query = ("UPDATE `account_storage` SET `value` = ".. value .." WHERE `account_id` = ".. self:getAccountId() .." AND `key` = "..key)
34 else
35 query = ("INSERT INTO `account_storage` (`account_id`, `key`, `value`) VALUES (".. self:getAccountId() ..", ".. key ..", ".. value ..")")
36 end
37 return db.query(query)
38end