Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update server.lua and DataBase #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions dump.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ CREATE TABLE `items` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`libelle` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

# Affichage de la table users
# ------------------------------------------------------------

ALTER TABLE `users`
ADD PRIMARY KEY (`identifier`);
#ALTER TABLE `users`
#ADD PRIMARY KEY (`identifier`);


# Affichage de la table user_inventory
Expand All @@ -36,11 +36,9 @@ ADD PRIMARY KEY (`identifier`);
DROP TABLE IF EXISTS `user_inventory`;

CREATE TABLE `user_inventory` (
`user_id` varchar(255) CHARACTER SET utf8mb4 NOT NULL DEFAULT '',
`item_id` int(11) unsigned NOT NULL,
`quantity` int(11) DEFAULT NULL,
PRIMARY KEY (`user_id`,`item_id`),
KEY `item_id` (`item_id`),
CONSTRAINT `user_inventory_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`identifier`),
CONSTRAINT `user_inventory_ibfk_2` FOREIGN KEY (`item_id`) REFERENCES `items` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`identifier` varchar(60) NOT NULL,
`item_id` int(11) DEFAULT NULL,
`quantity` int(11) DEFAULT NULL
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
25 changes: 12 additions & 13 deletions server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ require "resources/essentialmode/lib/MySQL"
MySQL:open(database.host, database.name, database.username, database.password)


RegisterServerEvent("item:getItems")
RegisterServerEvent("item:setItem")
RegisterServerEvent("item:updateQuantity")

local items = {}

RegisterServerEvent("item:getItems")
AddEventHandler("item:getItems", function()
items = {}
playerId = tonumber(source)
TriggerEvent('es:getPlayerFromId', playerId, function(user)
TriggerEvent('es:getPlayerFromId', source, function(user)
local player = user.identifier
local executed_query = MySQL:executeQuery("SELECT * FROM user_inventory JOIN items ON `user_inventory`.`item_id` = `items`.`id` WHERE user_id = '@username'", { ['@username'] = player })
local executed_query = MySQL:executeQuery("SELECT * FROM user_inventory JOIN items ON `user_inventory`.`item_id` = `items`.`id` WHERE identifier = '@username'", { ['@username'] = player })
local result = MySQL:getResults(executed_query, { 'quantity', 'libelle', 'item_id' }, "item_id")
if (result) then
for _, v in ipairs(result) do
Expand All @@ -26,20 +28,17 @@ AddEventHandler("item:getItems", function()
TriggerClientEvent("gui:getItems", source, items)
end)

RegisterServerEvent("item:setItem")
AddEventHandler("item:setItem", function(item, quantity)
playerId = tonumber(source)
TriggerEvent('es:getPlayerFromId', playerId, function(user)
MySQL:executeQuery("INSERT INTO user_inventory (`user_id`, `item_id`, `quantity`) VALUES ('@player', @item, @qty)",
{ ['@player'] = user.identifier, ['@item'] = item, ['@qty'] = quantity })
TriggerEvent('es:getPlayerFromId', source, function(user) --TriggerEvent('es:getPlayerFromId', playerId, function(user)
local player = user.identifier
MySQL:executeQuery("INSERT INTO user_inventory (`identifier`, `item_id`, `quantity`) VALUES ('@player', @item, @qty)",
{ ['@player'] = player, ['@item'] = item, ['@qty'] = quantity })
end)
end)

RegisterServerEvent("item:updateQuantity")
AddEventHandler("item:updateQuantity", function(qty, id)
playerId = tonumber(source)
TriggerEvent('es:getPlayerFromId', playerId, function(user)
TriggerEvent('es:getPlayerFromId', source, function(user)
local player = user.identifier
MySQL:executeQuery("UPDATE user_inventory SET `quantity` = @qty WHERE `user_id` = '@username' AND `item_id` = @id", { ['@username'] = player, ['@qty'] = tonumber(qty), ['@id'] = tonumber(id) })
MySQL:executeQuery("UPDATE user_inventory SET `quantity` = @qty WHERE `identifier` = '@username' AND `item_id` = @id", { ['@username'] = player, ['@qty'] = tonumber(qty), ['@id'] = tonumber(id) })
end)
end)