Jump to content

[REL] Business System v1.3.1 - OOP, MySQL


JR10

Recommended Posts

  • 8 months later...
  • 2 months later...

A new version is released

Version 1.3.0

  • Fully rewritten in OOP
  • New directx user interface with the dxGUI library
  • Can now use MySQL or SQLite
  • Several bug fixes

The resource needs some performance optimizations, unfortunately, this is all I had time for. I will be working on this in the future, perhaps adding interactive businesses.

Link to comment
  • 3 weeks later...
addEventHandler("onResourceStart", resourceRoot, function()

if (settings.database == "mysql") then

local host, db, username, password, port, socket = unpack(settings.database_data);

if not (host and db and username and password) then outputDebugString("Business: Failed to connect to the MySQL server - The data is invalid"); return; end

if (tonumber(port)) then port = "port="..port..";"; else port = ""; end

if (socket and socket ~= "") then socket = "socket="..socket..";"; else socket = ""; end

database = Connection("mysql", "host="..host..";dbname="..db..";"..port..socket, username, password);

if (not database) then outputDebugString("Business: Failed to connect to the MySQL server"); return; end

else

database = Connection("sqlite", "files/business.db");

if (not database) then outputDebugString("Business: Failed to connect to the SQLite file"); return; end

end

database:exec("CREATE TABLE IF NOT EXISTS business(id INT, name TEXT, owner TEXT, cost INT, pos TEXT, payout INT, payout_time INT, payout_otime INT, payout_unit TEXT, payout_cur_time INT, bank INT)");

database:query(dbCreateBusinessesCallback, "SELECT * FROM business");

end);

Where i must paste my MySQL data information?

Link to comment

I'm guessing you removed the classlib file. Just remove the Cursor.lua file from the meta.xml and try again. If it still spits errors, just download the latest version and don't mess with the meta.xml. If you haven't removed anything, I guess I will have to take a look.

Link to comment
  • 2 weeks later...

I'm getting some errors, I cant create a business..

the resource name is 'business' and it's at acl group admin, when I type /business it works good but after pick the coords and set prices, when I click CREATE BUSINESS, the sound plays but nothing happens, the business doesn't create..

the only warning it shows is

WARNING: business\scripts\core.server.lua:28: Bad argument @ 'query' [Expected string at argument 2, got function] 

Link to comment
  • 5 weeks later...

well hi JR10 i have 2 suggestion....

first please remove the marker or make it invesible and add T-shirt pickup to it. this pickup should be the blue / green pickup if it was for sale then green / if owned then blue

pickup :

Clothespickup-GTASA.jpg

and second thing is edit the GUI panel and make it look like housing panel with less buttons...and or when you click owner options panel ( you only can access it as the business owner ) with all stuff inside like ( put for sale , bank ..etc )

and remove blips as it's not good for RP

Link to comment
  • 3 months later...
  • 5 weeks later...

i fix it but now i can't deposit / withdraw money from the bank xD

Code :

local _settings = get(""); 
local settings = {};
for k, v in pairs(_settings) do
    k = split(k, ".")[2];
    settings[k] = v;
end
 
if (settings.key:len() < 1 or settings.key:len() > 1) then
    settings.key = "N";
end
if (not settings.blip or tonumber(settings.blip) == nil) then
    settings.blip = false;
end
 
addEventHandler("onResourceStart", resourceRoot, function()
    if (settings.database == "mysql") then
        local host, db, username, password, port, socket = unpack(settings.database_data);
        if not (host and db and username and password) then outputDebugString("Business: Failed to connect to the MySQL server - The data is invalid"); return; end
        if (tonumber(port)) then port = "port="..port..";"; else port = ""; end
        if (socket and socket ~= "") then socket = "socket="..socket..";"; else socket = ""; end
        database = Connection("mysql", "host="..host..";dbname="..db..";"..port..socket, username, password);
        if (not database) then outputDebugString("Business: Failed to connect to the MySQL server"); return; end
    else
        database = Connection("sqlite", "files/business.db");
        if (not database) then outputDebugString("Business: Failed to connect to the SQLite file"); return; end
    end
    database:exec("CREATE TABLE IF NOT EXISTS business(id INT, name TEXT, owner TEXT, cost INT, pos TEXT, payout INT, payout_time INT, payout_otime INT, payout_unit TEXT, payout_cur_time INT, bank INT)");
    database:query(dbCreateBusinessesCallback,  "SELECT * FROM business");
end);
 
function dbCreateBusinessesCallback(query_handle)
    local sql = query_handle:poll(0);
    if (sql and #sql > 0) then
        for index, row in ipairs(sql) do
            local pos = split(row["pos"], ",");
            local b_marker = Marker(pos[1], pos[2], pos[3], "cylinder", 1.5, settings.marker_color[1], settings.marker_color[2], settings.marker_color[3], settings.marker_color[4]);
            b_marker.interior = pos[4];
            b_marker.dimension = pos[5];
            if (settings.blip ~= false) then
                if (row["owner"] == "For Sale") then
                    local b_blip = Blip.createAttachedTo(b_marker, settings.blip, 2, 255, 0, 0, 255, 0, 100.0);
                    b_blip.interior = pos[4];
                    b_blip.dimension = pos[5];
                else
                    local b_blip = Blip.createAttachedTo(b_marker, settings.blip, 2, 255, 0, 0, 255, 0, 100.0);
                    b_blip.interior = pos[4];
                    b_blip.dimension = pos[5];
                end
            end
            addEventHandler("onMarkerHit", b_marker, onBusinessMarkerHit);
            addEventHandler("onMarkerLeave", b_marker, onBusinessMarkerLeave);
            local timer = Timer(businessPayout, row["payout_cur_time"] , 1, b_marker);
            b_marker:setData("b_data", {row["id"], row["name"], row["owner"], row["cost"], row["payout"], row["payout_time"], row["payout_otime"], row["payout_unit"], row["bank"], timer});
        end
    end
end
 
addCommandHandler("business", function(player)
    if (ACL.hasObjectPermissionTo(player, "function.banPlayer")) then
        triggerClientEvent(player, "business.showCreateBusinessWindow", player);
    else
        player:outputMessage("Business: You don't have access to this command.", 255, 0, 0);
    end
end);
 
function Player:outputMessage(message, r, g, b)
    if (settings.info_messages_type == "dx") then
        dxOutputMessage(message, self, r, g, b);
    else
        self:outputChat(message, r, g, b, true);
    end
end
 
function outputMessage(message, player, r, g, b)
    if (settings.info_messages_type == "dx") then
        dxOutputMessage(message, player, r, g, b);
    else
        player:outputChat(message, r, g, b, true);
    end
end
 
function dxOutputMessage(message, player, r, g, b)
    triggerClientEvent(player, "business.dxOutputMessage", player, message, r, g, b);
end
 
addEvent("business.outputMessage", true);
addEventHandler("business.outputMessage", root, function(message, r, g, b)
    source:outputMessage(message, r, g, b);
end);
 
addEvent("business.createBusiness", true);
addEventHandler("business.createBusiness", root, function(x, y, z, interior, dimension, name, cost, payout, payout_time, payout_unit)
    database:query(dbCreateBusinessCallback,  {client, x, y, z, interior, dimension, name, cost, payout, payout_time, payout_unit}, "SELECT * FROM business");
end);
 
function dbCreateBusinessCallback(query_handle, client, x, y, z, interior, dimension, name, cost, payout, payout_time, payout_unit)
    local sql = query_handle:poll(0);
    if (sql) then
        local id;
        if (#sql > 0) then
            id = sql[#sql]["id"] + 1;
        else
            id = 1;
        end
        local unit;
        if (payout_unit == "Seconds") then
            unit = 1000;
        elseif (payout_unit == "Minutes") then
            unit = 60000;
        elseif (payout_unit == "Hours") then
            unit = 3600000;
        elseif (payout_unit == "Days") then
            unit = 86400000;
        end
 
        x = tonumber(x);
        y = tonumber(y);
        z = tonumber(z);
        interior = tonumber(interior);
        dimension = tonumber(dimension);
        cost = tonumber(cost);
        payout = tonumber(payout);
        payout_time = tonumber(payout_time);
 
        z = z - 1;
 
        database:exec("INSERT INTO business(id,name,owner,cost,pos,payout,payout_time,payout_otime,payout_unit,payout_cur_time,bank) VALUES(?,?,?,?,?,?,?,?,?,?,?)", id, name, "For Sale", cost, x..","..y..","..z..","..interior..","..dimension, payout, payout_time * unit, payout_time, payout_unit, payout_time * unit, 0);
 
        local b_marker = Marker(x, y, z, "cylinder", 1.5, settings.marker_color[1], settings.marker_color[2], settings.marker_color[3], settings.marker_color[4]);
        b_marker.interior = interior;
        b_marker.dimension = dimension;
        if (settings.blip ~= false) then
            local b_blip = Blip.createAttachedTo(b_marker, settings.blip, 2, 255, 0, 0, 255, 0, 100.0);
            b_blip.interior = interior;
            b_blip.dimension = dimension;
        end
        local timer = Timer(businessPayout, payout_time * unit , 1, b_marker);
        b_marker:setData("b_data", {id, name, "For Sale", cost, payout, payout_time * unit, payout_time, payout_unit, 0, timer});
        addEventHandler("onMarkerHit", b_marker, onBusinessMarkerHit);
        addEventHandler("onMarkerLeave", b_marker, onBusinessMarkerLeave);
        if (#tostring(id) == 1) then id = "0".. tostring(id) end
        client:outputMessage("Business: Business(ID #"..id..") has been created successfully", 0, 255, 0);
    end
end
 
function onBusinessMarkerHit(hElement, mDim)
    if (hElement:getType() ~= "player") then return; end
    if (hElement:isInVehicle()) then return; end
    if (not mDim) then return; end
    triggerClientEvent(hElement, "business.showInstructions", hElement);
end
 
function onBusinessMarkerLeave(hElement, mDim)
    if (hElement:getType() ~= "player") then return; end
    if (hElement:isInVehicle()) then return; end
    if (not mDim) then return; end
    triggerClientEvent(hElement, "business.hideInstructions", hElement);
end
 
function businessPayout(b_marker)
    local b_data = b_marker:getData("b_data");
    local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
    if (owner ~= "For Sale") then
        bank = bank + payout;
        database:exec("UPDATE business SET bank = ? WHERE id = ?", bank, id);
        if (settings.inform_player_of_payout) then
            local account = Account(owner);
            if (account) then
                local player = account:getPlayer();
                if (player and player.isElement) then
                    player:outputMessage("Business: Business \" "..name.." \" has paid out($"..payout..")", 0, 255, 0);
                end
            end
        end
    end
    timer = Timer(businessPayout, payout_time, 1, b_marker);
    b_marker:setData("b_data", {id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer});
end
 
addEventHandler("onResourceStop", resourceRoot, function()
    for index, b_marker in ipairs(Element.getAllByType("marker", resourceRoot)) do
        local b_data = b_marker:getData("b_data");
        local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
        if (timer and timer:isValid()) then
            local left = timer:getDetails();
            if (left >= 50) then
                database:exec("UPDATE business SET payout_cur_time = ? WHERE id = ?", left, id);
            else
                database:exec("UPDATE business SET payout_cur_time = ? WHERE id = ?", payout_time, id);
            end
        end
    end
end);
 
function Ped:isInMarker(marker)
    local colshape = marker.colShape;
    return self:isWithinColShape(colshape);
end
 
addEventHandler("onResourceStart", resourceRoot, function()
    for index, player in ipairs(Element.getAllByType("player")) do
        bindKey(player, settings.key, "up", onPlayerAttemptToOpenBusiness);
    end
end);
 
addEventHandler("onPlayerJoin", root,function()
    bindKey(source, settings.key, "up", onPlayerAttemptToOpenBusiness);
end);
 
function onPlayerAttemptToOpenBusiness(player)
    for index, b_marker in ipairs(Element.getAllByType("marker", resourceRoot)) do
        if (player:isInMarker(b_marker)) then
            local b_data = b_marker:getData("b_data");
            local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
            triggerClientEvent(player, "business.showBusinessWindow", player, b_marker, getAccountName(getPlayerAccount(player)) == Owner, ACL.hasObjectPermissionTo(player, "function.showbus"));
            break;
        end
    end
end
 
function Ped:getMarker()
    for index, b_marker in ipairs(Element.getAllByType("marker", resourceRoot)) do
        if (self:isInMarker(b_marker)) then
            return b_marker;
        end
    end
end
 
addEvent("business.buy", true);
addEventHandler("business.buy", root, function()
    local account = client.account;
    if (not account or account:isGuest()) then return; end
    local b_marker = client:getMarker();
    if (not isElement(b_marker)) then return; end
    local b_data = b_marker:getData("b_data");
    local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
    if (owner ~= "For Sale") then
        client:outputMessage("Business: This business is owned", 255, 0, 0);
        return;
    end
    database:query(dbBuyBusinessCallback, {client, b_marker, id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer}, "SELECT * FROM business WHERE owner = ?", account.name);
end);
 
addEvent("business.sell", true);
addEventHandler("business.sell", root, function()
    local account = client.account;
    if (not account or account:isGuest()) then return; end
    local b_marker = client:getMarker();
    if (not isElement(b_marker)) then return; end
    local b_data = b_marker:getData("b_data");
   
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...