Facebook
From Blush Giraffe, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 268
  1. mysql = exports.mysql
  2. local useShopsWithNoItems = false
  3. local profitRate = tonumber(get( "profitRate" )) or 5
  4.  
  5. -- respawn dead npcs after two minute
  6. --[[
  7. addEventHandler("onPedWasted", getResourceRootElement(),
  8.         function()
  9.                 setTimer(
  10.                         function( source )
  11.                                 local x,y,z = getElementPosition(source)
  12.                                 local rotation = getElementData(source, "rotation")
  13.                                 local interior = getElementInterior(source)
  14.                                 local dimension = getElementDimension(source)
  15.                                 local dbid = getElementData(source, "dbid")
  16.                                 local shoptype = getElementData(source, "shoptype")
  17.                                 local skin = getElementModel(source)
  18.                                 local sPendingWage = getElementData(source, "sPendingWage")
  19.                                 local sIncome = getElementData(source, "sIncome")
  20.                                 local sCapacity = getElementData(source, "sCapacity")
  21.                                 local currentCap = getElementData(source, "currentCap")
  22.                                 local sSales = getElementData(source, "sSales")
  23.                                 local pedName = getElementData(source, "name")
  24.                                 destroyElement(source)
  25.                                
  26.                                 createShopKeeper(x,y,z,interior,dimension,dbid,shoptype,rotation,skins, sPendingWage, sIncome, sCapacity, currentCap, sSales, pedName)
  27.                         end,
  28.                         120000, 1, source
  29.                 )
  30.         end
  31. )
  32. ]]
  33.  
  34. local skins = { { 211, 217 }, { 179 }, false, { 178 }, { 82 }, { 80, 81 }, { 28, 29 }, { 169 }, { 171, 172 }, { 142 }, { 171 }, { 171, 172 }, {71}, { 50 }, { 1 }, { 118 }, {118} }
  35.  
  36. function createShopKeeper(x,y,z,interior,dimension,id,shoptype,rotation, skin, sPendingWage, sIncome, sCapacity, currentCap, sSales, pedName, sContactInfo, faction_belong, faction_access)
  37.         if not g_shops[shoptype] then
  38.                 outputDebugString("Trying to locate shop #" .. id .. " with invalid shoptype " .. shoptype)
  39.                 return
  40.         end
  41.        
  42.         if shoptype == 17 then
  43.                 if tonumber(dimension) == 0 and tonumber(interior) == 0 then
  44.                         return false
  45.                 end
  46.         end
  47.        
  48.         if not skin then
  49.                 skin = 0
  50.                
  51.                 if shoptype == 3 then
  52.                         skin = 168
  53.                         -- needs differences for burgershot etc
  54.                         if interior == 5 then
  55.                                 skin = 155
  56.                         elseif interior == 9 then
  57.                                 skin = 167
  58.                         elseif interior == 10 then
  59.                                 skin = 205
  60.                         end
  61.                         -- interior 17 = donut shop
  62.                 elseif shoptype == 16 then
  63.                         skin = 27
  64.                 else
  65.                         -- clothes, interior 5 = victim
  66.                         -- clothes, interior 15 = binco
  67.                         -- clothes, interior 18 = zip
  68.                         skin = skins[shoptype][math.random( 1, #skins[shoptype] )]
  69.                 end
  70.         end
  71.        
  72.         local ped = createPed(skin, x, y, z)
  73.         setElementRotation(ped, 0, 0, rotation)
  74.         setElementDimension(ped, dimension)
  75.         setElementInterior(ped, interior)
  76.         exports.pool:allocateElement(ped)
  77.        
  78.         if shoptype == 17 then
  79.                 setElementData(ped, "customshop", true)
  80.         elseif shoptype == 18 or shoptype == 19 then --Faction Drop NPCs
  81.                 exports.anticheat:changeProtectedElementDataEx(ped, "faction_belong", faction_belong, true)
  82.                 exports.anticheat:changeProtectedElementDataEx(ped, "faction_access", faction_access, true)
  83.         end
  84.        
  85.         setElementData(ped, "talk", 1, true)
  86.         setElementData(ped, "name", pedName, true)
  87.         setElementData(ped, "shopkeeper", true)
  88.                
  89.         setElementFrozen(ped, true)
  90.        
  91.         setElementData(ped, "dbid", tonumber(id), true)
  92.         setElementData(ped, "ped:type", "shop", false)
  93.         setElementData(ped, "shoptype", shoptype, false)
  94.         setElementData(ped, "rotation", rotation, false)
  95.         setElementData(ped, "sPendingWage", sPendingWage, true)
  96.         setElementData(ped, "sIncome", (shoptype == 14 and 0 or tonumber(sIncome)), true)
  97.         setElementData(ped, "sCapacity", sCapacity, true)
  98.         setElementData(ped, "currentCap", currentCap, true)
  99.         setElementData(ped, "sSales", sSales, true)
  100.         setElementData(ped, "sContactInfo", sContactInfo, true)
  101. end
  102.  
  103. function delNearbyGeneralshops(thePlayer, commandName)
  104.         if (exports.integration:isPlayerTrialAdmin(thePlayer)) then
  105.                 local posX, posY, posZ = getElementPosition(thePlayer)
  106.                 outputChatBox("Deleting Nearby Shop NPC(s):", thePlayer, 255, 126, 0)
  107.                 local count = 0
  108.                
  109.                 local dimension = getElementDimension(thePlayer)
  110.                
  111.                 for k, thePed in ipairs(getElementsByType("ped", resourceRoot)) do
  112.                         local pedType = getElementData(thePed, "ped:type")
  113.                         if (pedType) then
  114.                                 if (pedType=="shop") then
  115.                                         local x, y = getElementPosition(thePed)
  116.                                         local distance = getDistanceBetweenPoints2D(posX, posY, x, y)
  117.                                         local cdimension = getElementDimension(thePed)
  118.                                         if (distance<=10) and (dimension==cdimension) then
  119.                                                 local dbid = getElementData(thePed, "dbid")
  120.                                                 local shoptype = getElementData(thePed, "shoptype")
  121.                                                 if deleteGeneralShop(thePlayer, "delshop" , dbid) then
  122.                                                         --outputChatBox("   Deleted Shop with ID #" .. dbid .. " and type "..shoptype..".", thePlayer, 255, 126, 0)
  123.                                                         count = count + 1
  124.                                                 end
  125.                                         end
  126.                                 end
  127.                         end
  128.                 end
  129.                
  130.                 if (count==0) then
  131.                         outputChatBox("   Deleted None.", thePlayer, 255, 126, 0)
  132.                 else
  133.                         outputChatBox("   Deleted "..count.." None.", thePlayer, 255, 126, 0)
  134.                 end
  135.         end
  136. end
  137. addCommandHandler("delnearbyshops", delNearbyGeneralshops, false, false)
  138. addCommandHandler("delnearbynpcs", delNearbyGeneralshops, false, false)
  139.  
  140. -- function createDynamic(x,y,z,interior,dimension,id,rotation,skin ~= -1 and skin, products)
  141.         -- if not skin then
  142.                 -- skin = skins[8][math.random( 1, #skins[8] )]
  143.         -- end
  144.         -- local ped = createPed(skin, x, y, z)
  145.         -- setElementDimension(ped, dimension)
  146.         -- setElementInterior(ped, interior)
  147.         -- exports.pool:allocateElement(ped)
  148.        
  149.         -- setElementData(ped, "shopkeeper", true)
  150.         -- setElementFrozen(ped, true)
  151.         -- setElementData(ped, "dbid", id, false)
  152.         -- setElementData(ped, "ped:type", "shop", false)
  153.         -- setElementData(ped, "shoptype", 0, false)
  154.         -- setElementData(ped, "rotation", rotation, false)
  155. -- end
  156.  
  157. function SmallestID() -- finds the smallest ID in the SQL instead of auto increment
  158.     local result1 = dbPoll(dbQuery(mysql:getConnection(), "SELECT MIN(e1.id+1) AS nextID FROM shops AS e1 LEFT JOIN shops AS e2 ON e1.id +1 = e2.id WHERE e2.id IS NULL"), -1)
  159.     if result1 then
  160.         local id1 = tonumber(result1["nextID"]) or 1
  161.         return id1
  162.     end
  163.     return false
  164. end
  165.  
  166. function createGeneralshop(thePlayer, commandName, shoptype, skin, ...)
  167.         if (exports.integration:isPlayerTrialAdmin(thePlayer)) then
  168.                 local shoptype = tonumber(shoptype)
  169.                 if not shoptype or not g_shops[shoptype] then
  170.                         outputChatBox("SYNTAX: /" .. commandName .. " [shop type] [skin, -1 = random] [Firstname Lastname, -1 = random]", thePlayer, 255, 194, 14)
  171.                         for k, v in ipairs(g_shops) do
  172.                                 outputChatBox("TYPE " .. k .. " = " .. v.name, thePlayer, 200, 200, 200)
  173.                         end
  174.                         return false
  175.                 end
  176.  
  177.                 local skin = tonumber(skin)
  178.                
  179.                 if not skin or skin == -1 then --Random
  180.                         skin = exports.global:getRandomSkin()
  181.                 end
  182.                
  183.                 if skin then
  184.                         local ped = createPed(skin, 0, 0, 3)
  185.                         if not ped then
  186.                                 outputChatBox("Invalid Skin.", thePlayer, 255, 0, 0)
  187.                                 return
  188.                         else
  189.                                 destroyElement(ped)
  190.                         end
  191.                 else
  192.                         skin = -1
  193.                 end
  194.                
  195.                 local x, y, z = getElementPosition(thePlayer)
  196.                 local dimension = getElementDimension(thePlayer)
  197.                 local interior = getElementInterior(thePlayer)
  198.                 local _, _, rotation = getElementRotation(thePlayer)
  199.                
  200.                 if shoptype == 17 then
  201.                         if dimension == 0 and interior == 0 then
  202.                                 outputChatBox("Custom shop must be created in a business interior.", thePlayer, 255, 0, 0)
  203.                                 return false
  204.                         end
  205.                 end
  206.                
  207.                 local pedName = table.concat({...}, "_") or false
  208.                
  209.                 if not pedName or pedName=="" or (tonumber(pedName) and tonumber(pedName) == -1) then
  210.                         pedName = exports.global:createRandomMaleName()
  211.                         pedName = string.gsub(pedName, " ", "_")
  212.                 end
  213.                
  214.                 local iCan, why = canIUseThisName(pedName)
  215.                 if not iCan then
  216.                         outputChatBox(why, thePlayer, 255, 0, 0)
  217.                         return false
  218.                 end
  219.                
  220.                  local id = false
  221.         id = dbExec(mysql:getConnection(), "INSERT INTO shops SET pedName='"..exports.global:toSQL(pedName).."', x='" .. (x) .. "', y='" .. (y) .. "', z='" .. (z) .. "', dimension='" .. (dimension) .. "', interior='" .. (interior) .. "', shoptype='" .. (shoptype) .. "', rotationz='" .. (rotation) .. "', skin='".. (skin).."' ")
  222.         dbQuery(
  223.             function(qh)
  224.                 local res, rows, err = dbPoll(qh, 0)
  225.                 if rows > 0 then
  226.                     local id = res[1]['id']
  227.                     createShopKeeper(x,y,z,interior,dimension,id,tonumber(shoptype),rotation,skin ~= -1 and skin, 0, 0, 10, 0, "", pedName, {"", "", "", ""}, 0, 0)
  228.                 end
  229.             end,
  230.         mysql:getConnection(), "SELECT id FROM shops WHERE id = LAST_INSERT_ID()")
  231.     end
  232. end
  233.  
  234. function getNearbyGeneralshops(thePlayer, commandName)
  235.         if (exports.integration:isPlayerTrialAdmin(thePlayer) or exports.integration:isPlayerSupporter(thePlayer)) then
  236.                 local posX, posY, posZ = getElementPosition(thePlayer)
  237.                 outputChatBox("Nearby Shop NPC(s):", thePlayer, 255, 126, 0)
  238.                 local count = 0
  239.                
  240.                 local dimension = getElementDimension(thePlayer)
  241.                
  242.                 for k, thePed in ipairs(getElementsByType("ped", resourceRoot)) do
  243.                         local pedType = getElementData(thePed, "ped:type")
  244.                         if (pedType) then
  245.                                 if (pedType=="shop") then
  246.                                         local x, y = getElementPosition(thePed)
  247.                                         local distance = getDistanceBetweenPoints2D(posX, posY, x, y)
  248.                                         local cdimension = getElementDimension(thePed)
  249.                                         if (distance<=10) and (dimension==cdimension) then
  250.                                                 local dbid = getElementData(thePed, "dbid")
  251.                                                 local shoptype = getElementData(thePed, "shoptype")
  252.                                                 local pedName = getElementData(thePed, "name") or "Unnamed"
  253.                                                 outputChatBox("   Shop ID #" .. dbid .. ", type "..shoptype..", name: "..tostring(pedName):gsub("_", " "), thePlayer, 255, 126, 0)
  254.                                                 count = count + 1
  255.                                         end
  256.                                 end
  257.                         end
  258.                 end
  259.                
  260.                 if (count==0) then
  261.                         outputChatBox("   None.", thePlayer, 255, 126, 0)
  262.                 end
  263.         end
  264. end
  265. addCommandHandler("nearbyshops", getNearbyGeneralshops, false, false)
  266. addCommandHandler("nearbynpcs", getNearbyGeneralshops, false, false)
  267.  
  268. function moveNPCshop(thePlayer, commandName, value)
  269.         if (exports.integration:isPlayerTrialAdmin(thePlayer) or exports.integration:isPlayerSupporter(thePlayer)) then
  270.        
  271.         if not tonumber(value) then
  272.                 outputChatBox("SYNTAX: /" .. commandName .. " [Shop ID]", thePlayer, 255, 194, 14)
  273.                 return
  274.         end
  275.  
  276.         local dimension = getElementDimension(thePlayer)
  277.  
  278.         local possibleShops = getElementsByType("ped", resourceRoot)
  279.         local foundShop = false
  280.                 for _, shop in ipairs(possibleShops) do
  281.                         if getElementData(shop,"shopkeeper") and (tonumber(getElementData(shop, "dbid")) == tonumber(value)) then
  282.                                 foundShop = shop
  283.                                 break
  284.                         end
  285.                 end
  286.  
  287.         if not foundShop then
  288.                 outputChatBox("No shop founded with ID #"..value, thePlayer, 255, 0, 0)
  289.                 return
  290.         end
  291.  
  292.         local x, y, z = getElementPosition(thePlayer)
  293.         local dim = getElementDimension(thePlayer)
  294.         local int = getElementInterior(thePlayer)
  295.         local rot, rot1, rot2 = getElementRotation(thePlayer)
  296.  
  297.          change = dbExec(mysql:getConnection(), "UPDATE shops SET x='" .. (x) .. "', y='" .. (y) .. "', z='" .. (z) .. "', dimension='" .. (dim) .. "', interior='" .. (int) .. "', rotationz='" .. (rot2) .. "' WHERE id=".. (tonumber(value)))
  298.  
  299.         setElementPosition(foundShop, x, y, z)
  300.         setElementDimension(foundShop, dim)
  301.         setElementInterior(foundShop, int)
  302.         setElementRotation(foundShop, rot, rot1, rot2)
  303.  
  304.         outputChatBox("Updated shop position.", thePlayer, 0, 255, 0)
  305.  
  306.         end
  307. end
  308. addCommandHandler("moveshop", moveNPCshop)
  309. addCommandHandler("moveNPC", moveNPCshop)
  310. addCommandHandler("movenpc", moveNPCshop)
  311.  
  312. function gotoShop(thePlayer, commandName, shopID)
  313.         if (exports.integration:isPlayerTrialAdmin(thePlayer)) then
  314.                 if not tonumber(shopID) then
  315.                         outputChatBox("SYNTAX: /" .. commandName .. " [Shop ID]", thePlayer, 255, 194, 14)
  316.                 else
  317.                         local possibleShops = getElementsByType("ped", resourceRoot)
  318.                         local foundShop = false
  319.                         for _, shop in ipairs(possibleShops) do
  320.                                 if getElementData(shop,"shopkeeper") and (tonumber(getElementData(shop, "dbid")) == tonumber(shopID)) then
  321.                                         foundShop = shop
  322.                                         break
  323.                                 end
  324.                         end
  325.                        
  326.                         if not foundShop then
  327.                                 outputChatBox("No shop founded with ID #"..shopID, thePlayer, 255, 0, 0)
  328.                                 return false
  329.                         end
  330.                                
  331.                         local x, y, z = getElementPosition(foundShop)
  332.                         local dim = getElementDimension(foundShop)
  333.                         local int = getElementInterior(foundShop)
  334.                         local _, _, rot = getElementRotation(foundShop)
  335.                         startGoingToShop(thePlayer, x,y,z,rot,int,dim,shopID)
  336.                 end
  337.         end
  338. end
  339. addCommandHandler("gotoshop", gotoShop, false, false)
  340.  
  341. function startGoingToShop(thePlayer, x,y,z,r,interior,dimension,shopID)
  342.         -- Maths calculations to stop the player being stuck in the target
  343.         x = x + ( ( math.cos ( math.rad ( r ) ) ) * 2 )
  344.         y = y + ( ( math.sin ( math.rad ( r ) ) ) * 2 )
  345.        
  346.         setCameraInterior(thePlayer, interior)
  347.        
  348.         if (isPedInVehicle(thePlayer)) then
  349.                 local veh = getPedOccupiedVehicle(thePlayer)
  350.                 setElementAngularVelocity(veh, 0, 0, 0)
  351.                 setElementInterior(thePlayer, interior)
  352.                 setElementDimension(thePlayer, dimension)
  353.                 setElementInterior(veh, interior)
  354.                 setElementDimension(veh, dimension)
  355.                 setElementPosition(veh, x, y, z + 1)
  356.                 warpPedIntoVehicle ( thePlayer, veh )
  357.                 setTimer(setElementAngularVelocity, 50, 20, veh, 0, 0, 0)
  358.         else
  359.                 setElementPosition(thePlayer, x, y, z)
  360.                 setElementInterior(thePlayer, interior)
  361.                 setElementDimension(thePlayer, dimension)
  362.         end
  363.         outputChatBox(" You have teleported to shop ID#"..shopID, thePlayer)
  364. end
  365.  
  366. function restoreGeneralShop(thePlayer, commandName, id)
  367.     if (exports.integration:isPlayerTrialAdmin(thePlayer)) then
  368.         if not (id) then
  369.             id = getElementData(thePlayer, "shop:mostRecentDeleteShop") or false
  370.             if not id then
  371.                 outputChatBox("SYNTAX: /" .. commandName .. " [ID]", thePlayer, 255, 194, 14)
  372.                 return false
  373.             end
  374.         end
  375.        
  376.         local dbid = id
  377.        
  378.         dbQuery(
  379.             function(qh, thePlayer)
  380.                 local res, rows, err = dbPoll(qh, 0)
  381.                 if rows > 0 then
  382.                     for index, row in ipairs(res) do
  383.                          if not (row) then
  384.                             outputChatBox("Shop ID #" .. dbid .. " isn't found in deleted shop database.", thePlayer, 255, 0, 0)
  385.                             return false
  386.                         end
  387.                        
  388.                         dbExec(mysql:getConnection(),"UPDATE `shops` SET `deletedBy` = '0' WHERE id='" .. (dbid) .. "' LIMIT 1")
  389.                         loadOneShop(dbid)
  390.                         outputChatBox("Restored shop with ID #" .. dbid .. ".", thePlayer, 0, 255, 0)
  391.                     end
  392.                 end
  393.             end,
  394.         {thePlayer}, mysql:getConnection(), "SELECT `id` FROM `shops` WHERE `id`='"..tostring(dbid).."' AND `deletedBy` != '0'")
  395.     end
  396. end
  397. addCommandHandler("restoreshop", restoreGeneralShop, false, false)
  398. addCommandHandler("restorenpc", restoreGeneralShop, false, false)
  399. addCommandHandler("restoreped", restoreGeneralShop, false, false)
  400.  
  401.  
  402. function deleteGeneralShop(thePlayer, commandName, id)
  403.         if (exports.integration:isPlayerTrialAdmin(thePlayer)) then
  404.                 if not (id) then
  405.                         outputChatBox("SYNTAX: /" .. commandName .. " [ID]", thePlayer, 255, 194, 14)
  406.                 else
  407.                         local counter = 0
  408.                        
  409.                         for k, thePed in ipairs(getElementsByType("ped", resourceRoot)) do
  410.                                 local pedType = getElementData(thePed, "ped:type")
  411.                                 if (pedType) then
  412.                                         if (pedType=="shop") then
  413.                                                 local dbid = getElementData(thePed, "dbid")
  414.                                                 if (tonumber(id)==dbid) then
  415.                                                         destroyElement(thePed)
  416.                                                         local adminID = getElementData(thePlayer,"account:id")
  417.                                                          dbExec(mysql:getConnection(),"UPDATE `shops` SET `deletedBy` = '"..tostring(adminID).."' WHERE id='" .. (dbid) .. "' LIMIT 1")
  418.                             --dbExec(mysql:getConnection(),"DELETE FROM shop_products WHERE npcID='" .. (dbid) .. "' ")
  419.                             --dbExec(mysql:getConnection(),"DELETE FROM shop_contacts_info WHERE npcID='" .. (dbid) .. "' ")
  420.                             outputChatBox("      Deleted shop with ID #" .. id .. ".", thePlayer, 0, 255, 0)
  421.                                                         counter = counter + 1
  422.                                                         setElementData(thePlayer, "shop:mostRecentDeleteShop",dbid, true )
  423.                                                 end
  424.                                         end
  425.                                 end
  426.                         end
  427.                        
  428.                         if (counter==0) then
  429.                                 outputChatBox("No shops with such an ID exists.", thePlayer, 255, 0, 0)
  430.                                 return false
  431.                         end
  432.                         return true
  433.                 end
  434.         end
  435. end
  436. addCommandHandler("delshop", deleteGeneralShop, false, false)
  437. addCommandHandler("deleteshop", deleteGeneralShop, false, false)
  438.  
  439. function removeGeneralShop(thePlayer, commandName, id)
  440.     if (exports.integration:isPlayerSeniorAdmin(thePlayer)) then
  441.         if not (id) then
  442.             id = getElementData(thePlayer, "shop:mostRecentDeleteShop") or false
  443.             if not id then
  444.                 outputChatBox("SYNTAX: /" .. commandName .. " [ID]", thePlayer, 255, 194, 14)
  445.                 return false
  446.             end
  447.         end
  448.        
  449.         local dbid = id
  450.         dbQuery(
  451.             function(qh, thePlayer)
  452.                 local res, rows, err = dbPoll(qh, 0)
  453.                 if rows > 0 then
  454.                     for index, row in ipairs(res) do
  455.                         if not (row) then
  456.                             outputChatBox("Shop ID #" .. dbid .. " isn't found in deleted shop database, /delshop first.", thePlayer, 255, 0, 0)
  457.                             return false
  458.                         end
  459.                        
  460.                        
  461.                         if dbExec(mysql:getConnection(),"DELETE FROM shops WHERE id='" .. (dbid) .. "' LIMIT 1") and    dbExec(mysql:getConnection(),"DELETE FROM shop_products WHERE npcID='" .. (dbid) .. "' ") and dbExec(mysql:getConnection(),"DELETE FROM shop_contacts_info WHERE npcID='" .. (dbid) .. "' ") then
  462.                             outputChatBox("Removed shop ID #" .. dbid .. " from SQL.", thePlayer, 0, 255, 0)
  463.                             setElementData(thePlayer, "shop:mostRecentDeleteShop",false, true )
  464.                         else
  465.                             outputChatBox("No shops with such an ID exists.", thePlayer, 255, 0, 0)
  466.                         end
  467.                     end
  468.                 end
  469.             end,
  470.         {thePlayer}, "SELECT `id` FROM `shops` WHERE `id`='"..tostring(dbid).."' AND `deletedBy` != '0'")
  471.        
  472.     end
  473. end
  474. addCommandHandler("removeshop", removeGeneralShop, false, false)
  475. addCommandHandler("removenpc", removeGeneralShop, false, false)
  476. addCommandHandler("removeped", removeGeneralShop, false, false)
  477.  
  478. function loadAllGeneralshops(res)
  479.     dbQuery(
  480.         function(qh)
  481.             local res, rows, err = dbPoll(qh, 0)
  482.             if rows > 0 then
  483.                 for index, row in ipairs(res) do
  484.                     local id = tonumber(row["id"])
  485.                     local x = tonumber(row["x"])
  486.                     local y = tonumber(row["y"])
  487.                     local z = tonumber(row["z"])
  488.                        
  489.                     local dimension = tonumber(row["dimension"])
  490.                     local interior = tonumber(row["interior"])
  491.                     local shoptype = tonumber(row["shoptype"])
  492.                     local rotation = tonumber(row["rotationz"])
  493.                     local skin = tonumber(row["skin"])
  494.                     local sPendingWage = tonumber(row["sPendingWage"])
  495.                     local sIncome = tonumber(row["sIncome"])
  496.                     local sCapacity = tonumber(row["sCapacity"])
  497.                     local currentCap = 0
  498.                     local sSales = row["sSales"]
  499.                     local pedName = row["pedName"] or false
  500.  
  501.                     local sContactInfo = {row["sOwner"],row["sPhone"],row["sEmail"],row["sForum"]}
  502.                     local faction_belong = tonumber(row["faction_belong"])
  503.                     local faction_access = tonumber(row["faction_access"])
  504.                    
  505.                     createShopKeeper(x,y,z,interior,dimension,id,shoptype,rotation,skin ~= -1 and skin, sPendingWage, sIncome, sCapacity, currentCap, sSales, pedName, sContactInfo, faction_belong, faction_access)
  506.                    
  507.                 end
  508.             end
  509.         end,
  510.     mysql:getConnection(), "SELECT `shops`.`id` AS `id`, `x`, `y`, `z`, `dimension`, `interior`, `shoptype`, `rotationz`, `skin`, `sPendingWage`, `sIncome`, `sCapacity`, `sSales`, `pedName`, `sOwner`, `sPhone`, `sEmail`, `sForum`, `faction_belong`, `faction_access` FROM `shops` LEFT JOIN `shop_contacts_info` ON `shops`.`id` = `shop_contacts_info`.`npcID` WHERE `shops`.`deletedBy` = '0'")
  511. end
  512. addEventHandler("onResourceStart", getResourceRootElement(), loadAllGeneralshops)
  513.  
  514. function loadOneShop(shopID)
  515.     dbQuery(
  516.         function(qh)
  517.             local res, rows, err = dbPoll(qh, 0)
  518.             if rows > 0 then
  519.                 for index, row in ipairs(res) do
  520.                     local id = tonumber(row["id"])
  521.                     local x = tonumber(row["x"])
  522.                     local y = tonumber(row["y"])
  523.                     local z = tonumber(row["z"])
  524.                        
  525.                     local dimension = tonumber(row["dimension"])
  526.                     local interior = tonumber(row["interior"])
  527.                     local shoptype = tonumber(row["shoptype"])
  528.                     local rotation = tonumber(row["rotationz"])
  529.                     local skin = tonumber(row["skin"])
  530.                     local sPendingWage = tonumber(row["sPendingWage"])
  531.                     local sIncome = tonumber(row["sIncome"])
  532.                     local sCapacity = tonumber(row["sCapacity"])
  533.                     local currentCap = 0
  534.                     local sSales = row["sSales"]
  535.                     local pedName = row["pedName"] or false
  536.  
  537.                     local sContactInfo = {row["sOwner"],row["sPhone"],row["sEmail"],row["sForum"]}
  538.                     local faction_belong = tonumber(row["faction_belong"])
  539.                     local faction_access = tonumber(row["faction_access"])
  540.                    
  541.                     createShopKeeper(x,y,z,interior,dimension,id,shoptype,rotation,skin ~= -1 and skin, sPendingWage, sIncome, sCapacity, currentCap, sSales, pedName, sContactInfo, faction_belong, faction_access)
  542.                    
  543.                 end
  544.             end
  545.         end,
  546.     mysql:getConnection(), "SELECT COUNT(*) as `currentCap` FROM `shop_products` WHERE `npcID` = '"..tostring(id).."' ")
  547.     return true
  548. end
  549.  
  550. function reloadGeneralShop(thePlayer, commandName, id)
  551.         if (exports.integration:isPlayerTrialAdmin(thePlayer)) then
  552.                 if not (id) then
  553.                         id = getElementData(thePlayer, "shop:mostRecentDeleteShop") or false
  554.                         if not id then
  555.                                 outputChatBox("SYNTAX: /" .. commandName .. " [ID]", thePlayer, 255, 194, 14)
  556.                                 return false
  557.                         end
  558.                 end
  559.                
  560.                 if loadOneShop(id) then
  561.                         outputChatBox("Reloaded shop ID#"..id..".",thePlayer, 0,255,0)
  562.                 else
  563.                         outputChatBox("Reloaded shop ID#"..id..".",thePlayer, 255,0,0)
  564.                 end
  565.         end
  566. end
  567. addCommandHandler("reloadshop", reloadGeneralShop, false, false)
  568. addCommandHandler("reloadnpc", reloadGeneralShop, false, false)
  569. addCommandHandler("reloadped", reloadGeneralShop, false, false)
  570.  
  571. function renamePed(thePlayer, commandName, id, ...)
  572.         if (exports.integration:isPlayerTrialAdmin(thePlayer) or exports.integration:isPlayerSupporter(thePlayer)) then
  573.                 if not tonumber(id) or not (...) then
  574.                         outputChatBox("SYNTAX: /" .. commandName .. " [Shop ID] [Firstname LastName]", thePlayer, 255, 194, 14)
  575.                         return false
  576.                 end
  577.                 id = math.floor(tonumber(id))
  578.                 local pedName = table.concat({...}, "_")
  579.                
  580.                 if pedName == "" then
  581.                         outputChatBox("SYNTAX: /" .. commandName .. " [Shop ID] [Firstname LastName]", thePlayer, 255, 194, 14)
  582.                         return false
  583.                 end
  584.                
  585.                 local iCan, why = canIUseThisName(pedName)
  586.                 if not iCan then
  587.                         outputChatBox(why, thePlayer, 255, 0, 0)
  588.                         return false
  589.                 end
  590.                
  591.                 if not dbExec(mysql:getConnection(),"UPDATE `shops` SET `pedName`='"..tostring(pedName):gsub("'","''").."' WHERE `id`='"..tostring(id).."' ") then
  592.             outputChatBox("Failed to rename this NPC, please contact Maxime.",thePlayer, 255,0,0)
  593.                         return false
  594.                 end
  595.                
  596.                 for k, thePed in ipairs(getElementsByType("ped", resourceRoot)) do
  597.                         local pedType = getElementData(thePed, "ped:type")
  598.                         if (pedType) then
  599.                                 if (pedType=="shop") then
  600.                                         local dbid = getElementData(thePed, "dbid")
  601.                                         if (tonumber(id)==dbid) then
  602.                                                 destroyElement(thePed)
  603.                                         end
  604.                                 end
  605.                         end
  606.                 end
  607.                
  608.                 if loadOneShop(id) then
  609.                         outputChatBox("Renamed shop ID#"..id.." to '"..tostring(pedName):gsub("_"," ").."'.",thePlayer, 0,255,0)
  610.                 else
  611.                         outputChatBox("Failed to reload this NPC, please contact Maxime.",thePlayer, 255,0,0)
  612.                 end
  613.         end
  614. end
  615. addCommandHandler("renameped", renamePed, false, false)
  616. addCommandHandler("renamenpc", renamePed, false, false)
  617. addCommandHandler("renameshop", renamePed, false, false)
  618.  
  619. -- end of loading shops, this be store keeper thing below --
  620. local function getDiscount( player, shoptype )
  621.         local discount = 1
  622.         if shoptype == 7 and tonumber( getElementData( player, "faction" ) ) == 125 then
  623.                 discount = discount * 0.5
  624.         elseif shoptype == 14 and tonumber( getElementData( player, "faction" ) ) == 30 then
  625.                 discount = discount * 0.5
  626.         end
  627.        
  628.         if exports.donators:hasPlayerPerk( player, 8 ) then
  629.                 discount = discount * 0.8
  630.         end
  631.         return discount
  632. end
  633.  
  634. function clickStoreKeeper()
  635.     local success, currentUser = canIAccessThisShop(source, client)
  636.     if not success then
  637.         outputChatBox(currentUser.." is currently using this NPC, please wait a moment.", client, 255,0,0)
  638.         return false
  639.     end
  640.    
  641.     local shoptype = getElementData(source, "shoptype")
  642.     local id = getElementData(source, "dbid")
  643.    
  644.     local race, gender = nil, nil
  645.     if(shoptype == 5) then -- if its a clothes shop, we also need the players race
  646.         gender = getElementData(client,"gender")
  647.         race = getElementData(client,"race")
  648.     end
  649.    
  650.     if tonumber(shoptype) == 17 then
  651.         local products = {}
  652.         dbQuery(
  653.             function(qh, client, source)
  654.                 local res, rows, err = dbPoll(qh, 0)
  655.                 if rows > 0 then
  656.                     for index, pRow in ipairs(res) do
  657.                         table.insert(products, { id, pRow["pItemID"], pRow["pItemValue"], pRow["pDesc"], pRow["pPrice"], pRow["pDate"], pRow["pID"] } )
  658.                     end
  659.                     if setShopCurrentUser(source, client) then
  660.                         triggerClientEvent(client, "showGeneralshopUI", source, shoptype, race, gender, 0, products)
  661.                     else
  662.                         outputDebugString("setShopCurrentUser failed.")
  663.                     end
  664.                 end
  665.             end,
  666.         {client, source}, mysql:getConnection(), "SELECT * FROM `shop_products` WHERE `npcID`='"..id.."' ORDER BY `pDate` DESC")
  667.     elseif tonumber(shoptype) == 18 then --Faction Drop NPC - General Items
  668.        
  669.     elseif tonumber(shoptype) == 19 then -- Faction Drop NPC - WEAPONS
  670.         local products = {}
  671.         dbQuery(
  672.             function(qh, client, source)
  673.                 local res, rows, err = dbPoll(qh, 0)
  674.                 if rows > 0 then
  675.                     for index, pRow in ipairs(res) do
  676.                         table.insert(products, pRow )
  677.                     end
  678.                     if setShopCurrentUser(source, client) then
  679.                         triggerClientEvent(client, "showGeneralshopUI", source, shoptype, race, gender, 0, products)
  680.                     else
  681.                         outputDebugString("setShopCurrentUser failed.")
  682.                     end
  683.                 end
  684.             end,
  685.         {client, source}, mysql:getConnection(), "SELECT `npcID`, `pItemID`, `pItemValue`, `pDesc`, `pPrice`, `pDate`, `pID`, `pQuantity`, `pSetQuantity`, `pRestockInterval`, `pRestockedDate`, DATEDIFF((`pRestockedDate` + interval `pRestockInterval` day),NOW()) AS `pRestockIn` FROM `shop_products` WHERE `npcID`='"..id.."' ORDER BY `pID` DESC")
  686.     else
  687.         if setShopCurrentUser(source, client) then
  688.             -- perk 8 = 20% discount in shops
  689.             triggerClientEvent(client, "showGeneralshopUI", source, shoptype, race, gender, getDiscount(client, shoptype))
  690.         else
  691.             outputDebugString("setShopCurrentUser failed.")
  692.         end
  693.     end
  694.    
  695. end
  696. addEvent("shop:keeper", true)
  697. addEventHandler("shop:keeper", getResourceRootElement(), clickStoreKeeper)
  698.  
  699.  
  700. function calcSupplyCosts(thePlayer, itemID, isWeapon, supplyCost)
  701.         if not isweapon and id ~= 68 then
  702.                 if exports.donators:hasPlayerPerk(thePlayer, 8) then
  703.                         return math.ceil( 0.8 * supplyCost )
  704.                 end
  705.         end
  706.         return supplyCost
  707. end
  708.  
  709. function getInteriorOwner( dimension )
  710.         if dimension == 0 then
  711.                 return nil, nil
  712.         end
  713.        
  714.         local dbid, theEntrance, theExit, interiorType, interiorElement = exports["interior-system"]:findProperty(source)
  715.         interiorStatus = getElementData(interiorElement, "status")
  716.         local owner = interiorStatus[4]
  717.        
  718.         for key, value in ipairs(exports.pool:getPoolElementsByType("player")) do
  719.                 local id = getElementData(value, "dbid")
  720.                 if (id==owner) then
  721.                         return owner, value
  722.                 end
  723.         end
  724.         return owner, nil -- no player found
  725. end
  726.  
  727. -- source = the ped clicked
  728. -- client = the player
  729. -- this has no code for the out-of-date lottery.
  730. addEvent("shop:buy", true)
  731. addEventHandler( "shop:buy", resourceRoot, function( index )
  732.         local shoptype = getElementData( source, "shoptype")
  733.         local error = "S-" .. tostring( shoptype ) .. "-" .. tostring( getElementData( source, "dbid") )
  734.  
  735.         local shop = g_shops[ shoptype or -1 ]
  736.         _G['shop'] = shop
  737.         if not shop then
  738.                 outputChatBox("Error " .. error .. "-NE, report at www.owlgaming.net/mantis.", client, 255, 0, 0 )
  739.                 return
  740.         end
  741.        
  742.         local race = getElementData( client, "race" )
  743.         local gender = getElementData( client, "gender" )
  744.         updateItems( shoptype, race, gender ) -- should modify /shop/ too, as shop is a reference to g_shops[type].
  745.        
  746.         -- fetch the selected item
  747.         local item = getItemFromIndex( shoptype, index )
  748.         if not item then
  749.                 outputChatBox("Error " .. error .. "-NEI-" .. index .. ", report at www.owlgaming.net/mantis.", client, 255, 0, 0 )
  750.                 return
  751.         end
  752.        
  753.         if item.minimum_age and getElementData(client, "age") < item.minimum_age then
  754.                 outputChatBox( "You need to be " .. item.minimum_age .. " years or older to buy this.", client, 255, 0, 0 )
  755.                 return
  756.         end
  757.        
  758.                 --[[Check if its a generic, and if they have approval yet
  759.         if item.name == "Other" and item.itemID == 80 and not getElementData(client, "shop:generic:pending") then
  760.                 triggerClientEvent(client, "shop:generic:buy", client, index)
  761.                 return
  762.         end]]
  763.        
  764.         -- check for monies
  765.         local price = math.ceil( getDiscount( client, shoptype ) * item.price )
  766.         if not exports.global:hasMoney( client, price ) then
  767.                 outputChatBox( "You lack the money to buy this " .. item.name .. ".", client, 255, 0, 0 )
  768.                 return
  769.         end
  770.        
  771.         -- @@ --
  772.         -- do some item-specific stuff, such as assigning a serial.
  773.         -- @@ --
  774.          local wonTheLottery = false
  775.     local itemID, itemValue = item.itemID, item.itemValue or 1
  776.     if itemID == 2 then
  777.         local attempts = 0
  778.         while true do
  779.             attempts = attempts + 1
  780.             itemValue = math.random(100000, attempts < 20 and 899999 or 8999999)
  781.            
  782.            
  783.         end
  784.     elseif itemID == 68 then -- Lottery Tickets
  785.                 --[[
  786.                 if not exports.integration:isPlayerScripter(client) then
  787.                         outputChatBox( "This item is temporarily disabled by scripters.", client, 255, 0, 0 )
  788.                         return
  789.                 end
  790.                 ]]
  791.                 local dimension = getElementDimension( source )
  792.                 local suppliesToTake = 0
  793.                 suppliesToTake = item.supplies or math.ceil( 3.5 * exports['item-system']:getItemWeight( itemID, itemValue ) )
  794.                
  795.                 if not suppliesToTake then
  796.                         outputChatBox( "Error " .. error .. "-SE-I" .. index .. "-" .. tostring( suppliesToTake ) )
  797.                         return false
  798.                 end
  799.  
  800.                 local success, why = solveSupplies(source, client, suppliesToTake, dimension)
  801.                 if not success then
  802.                         outputChatBox( why, client, 255, 0, 0 )
  803.                         return false
  804.                 end
  805.  
  806.                 if not exports["lottery-system"]:canThisPlayerBuyTicket(client) then
  807.                         outputChatBox( "One player now can only buy one lottery ticket every 20 minutes.", client, 255, 0, 0 )
  808.                         outputChatBox( "You've already bought another lottery ticket not long ago, please try again later.", client, 255, 0, 0 )
  809.                         return false
  810.                 end
  811.  
  812.                 local lotteryJackpot = exports['lottery-system']:getLotteryJackpot()
  813.                 if tonumber(lotteryJackpot) == -1 then
  814.                         outputChatBox( "Sorry, someone already won the lottery. Please wait for the next draw.", client, 255, 0, 0 )
  815.                         return
  816.                 elseif not exports.global:hasSpaceForItem( client, itemID, itemValue ) then
  817.                         outputChatBox("Your inventory is full.", client, 255, 0, 0)
  818.                 else
  819.                         local updatedJackpot = tonumber(lotteryJackpot) + math.ceil(price * 2 / 3)
  820.                         exports['lottery-system']:updateLotteryJackpot(updatedJackpot)
  821.                
  822.                         local lotteryTicketNumber = 0
  823.                         local lotteryTicketNumber = getElementData(client, 'test:nextPickedLotteryNumber') or math.random(2,48) -- Pick a random number for the lottery ticket number between 2 and 48
  824.                         itemValue = tonumber(lotteryTicketNumber)
  825.                        
  826.                         if tonumber(lotteryTicketNumber) == tonumber(exports['lottery-system']:getLotteryNumber()) then
  827.                                 setTimer(function(player, jp) exports['global']:giveMoney(player, jp) end, 100, 1, client, updatedJackpot)
  828.                                 outputChatBox( "You won! Jackpot: $" .. exports.global:formatMoney(updatedJackpot) .. ".", client, 0, 255, 0 )
  829.  
  830.                                 exports['lottery-system']:lotteryDraw()
  831.  
  832.                                 for key, value in ipairs(exports.pool:getPoolElementsByType("player")) do
  833.                                         if (getElementData(value, "loggedin")==1) then
  834.                                                 outputChatBox("[NEWS] " .. getPlayerName(client):gsub("_"," ") .. " won the lottery jackpot of $" .. exports.global:formatMoney(updatedJackpot) .. ".", value, 200, 100, 200)
  835.                                         end
  836.                                 end
  837.                                 exports['lottery-system']:updateLotteryJackpot(-1)
  838.                                 -- Timer to re-enable lottery 10 minutes after a ticket has been drawn.
  839.                                 setTimer(function ()
  840.                                         exports['lottery-system']:updateLotteryJackpot(0)
  841.                                 end, 600000, 1)
  842.  
  843.                                 wonTheLottery = true
  844.                         else
  845.                                 outputChatBox( "Sorry, your number did not get picked. You lost. You got number " .. lotteryTicketNumber .. ".", client, 255, 0, 0 )
  846.                         end
  847.                         lotteryTicketNumber = 0
  848.                 end
  849.         elseif itemID == 115 or itemID == 116 then -- now here's the trick. If item.license is set, it checks for a gun license, if item.ammo is set it gives as much ammo
  850.                 if item.license and getElementData( client, "license.gun" ) ~= 1 then
  851.                         outputChatBox( "You lack a weapon license.", client, 255, 0, 0 )
  852.                         return
  853.                 else
  854.                         local w = itemValue
  855.                         if itemID == 115 then
  856.                                 local serial = "1"
  857.                                 if item.license then -- licensed weapon, thus needs a serial
  858.                                         local characterDatabaseID = getElementData(client, "account:character:id")
  859.                                         serial = exports.global:createWeaponSerial( 3, characterDatabaseID, characterDatabaseID )
  860.                                 end
  861.                                 itemValue = itemValue .. ":" .. serial .. ":" .. getWeaponNameFromID( itemValue )
  862.  
  863.                                 addPurchaseLogs(tonumber(getElementData(source, "dbid")), client, tostring( getWeaponNameFromID( w ) ), itemValue, price, serial, "N/A", FORUM_AMMUNATION)
  864.                         elseif itemID == 116 then
  865.                                 local amount = item.ammo or exports.weaponcap:getGTACap( itemValue ) or 1
  866.                                 itemValue = itemValue .. ":" .. amount .. ":" .. getWeaponNameFromID( itemValue )
  867.  
  868.                                 addPurchaseLogs(tonumber(getElementData(source, "dbid")), client, "Ammo for " .. tostring( getWeaponNameFromID( w ) ), amount .. " Ammo for " .. tostring( getWeaponNameFromID( w ) ), price, nil, "N/A", FORUM_AMMUNATION)
  869.                         end
  870.                 end
  871.         end
  872.        
  873.         local dimension = getElementDimension( source )
  874.         local suppliesToTake = 0
  875.        
  876.         if dimension > 0 then -- is even in any interior -- calculate the supplies amount /MAXIME
  877.                 --suppliesToTake = item.supplies or math.ceil(item.price/(profitRate*2)) or math.ceil( 3.5 * exports['item-system']:getItemWeight( itemID, itemValue ) )
  878.                 suppliesToTake = item.supplies or math.ceil( 3.5 * exports['item-system']:getItemWeight( itemID, itemValue ) )
  879.                
  880.                 if not suppliesToTake then
  881.                         outputChatBox( "Error " .. error .. "-SE-I" .. index .. "-" .. tostring( suppliesToTake ) )
  882.                         return false
  883.                 end
  884.                
  885.                 -- get the current supply count and check for enough supplies
  886.                 local success, why = solveSupplies(source, client, suppliesToTake, dimension)
  887.                 if not success then
  888.                         outputChatBox( why, client, 255, 0, 0 )
  889.                         return false
  890.                 end
  891.         end
  892.        
  893.         if wonTheLottery or exports.global:giveItem( client, itemID, itemValue ) then
  894.                 -- Money
  895.                 local playerMoney = getElementData(client, "money")
  896.                 for i = 134, 134 do
  897.                         while exports['item-system']:takeItem(client, i) do
  898.                         end
  899.                 end
  900.                 if tonumber(playerMoney) > 0 then
  901.                         exports.global:giveItem(client, 134, tonumber(playerMoney)-tonumber(price))
  902.                 end
  903.                 exports.global:takeMoney( client, price ) -- this is assumed not to fail as we checked with :hasMoney before.
  904.                 -- and now for what happens after buying?
  905.                 outputChatBox( "You bought this " .. item.name .. " for $" .. exports.global:formatMoney( price ) .. ".", client, 0, 255, 0 )
  906.                
  907.                 -- some post-buying things, item-specific
  908.                 if itemID == 2 then
  909.                          dbExec(mysql:getConnection(),"INSERT INTO `phones` (`phonenumber`, `boughtby`) VALUES ('"..tostring(itemValue).."', '"..(tostring(getElementData(client, "account:character:id") or 0)).."')")
  910.            outputChatBox("Your number is #" .. itemValue .. ".", client, 255, 194, 14 )
  911.                 elseif itemID == 16 and item.fitting then -- it's a skin, so set it.
  912.                         setElementModel( client, itemValue )
  913.                         dbExec(mysql:getConnection(),"UPDATE characters SET skin = " .. ( itemValue ) .. " WHERE id = " .. (getElementData( client, "dbid" )) )
  914.         elseif itemID == 114 then -- vehicle mods
  915.                         outputChatBox("To add this item to any vehicle, go into a garage and double-click the item while sitting inside.", client, 255, 194, 14 )
  916.                 elseif itemID == 115 then -- log weapon purchases
  917.                         exports.logs:dbLog( client, 36, client, "bought WEAPON - " .. itemValue )
  918.                        
  919.                         local govMoney = math.floor( price / 2 )
  920.                         exports.global:giveMoney(getTeamFromName("Government of Los Santos"), govMoney)
  921.                         price = price - govMoney -- you'd obviously get less if the gov asks for percentage.
  922.                 elseif itemID == 116 then -- log weapon purchases
  923.                         exports.logs:dbLog( client, 36, client, "bought AMMO - " .. itemValue )
  924.                        
  925.                         local govMoney = math.floor( price / 2 )
  926.                         exports.global:giveMoney(getTeamFromName("Government of Los Santos"), govMoney)
  927.                         price = price - govMoney -- you'd obviously get less if the gov asks for percentage.
  928.                 end
  929.                
  930.                 -- What's left undone? Giving shop owner money!
  931.                
  932.                 if price > 0 and dimension > 0 then
  933.                         local currentIncome = tonumber(getElementData(source, "sIncome")) or 0
  934.                         setElementData(source, "sIncome", currentIncome + price, true)
  935.                         playBuySound(source)
  936.                         local playerGender = getElementData(client,"gender")
  937.                         local pedName = tostring(getElementData(source, "name"))
  938.                         if string.sub(pedName, 1, 8) == "userdata" then
  939.                                 pedName = "The Storekeeper"
  940.                         end
  941.                         pedName = string.gsub(pedName,"_", " ")
  942.                         local playerName = getPlayerName(client):gsub("_", " ")
  943.                         if playerGender == 0 then
  944.                                 triggerEvent('sendAme', client, "takes out a couple of dollar notes from his wallet, hands it over to "..pedName)
  945.                         else                                   
  946.                                 triggerEvent('sendAme', client, "takes out a couple of dollar notes from her wallet, hands it over to "..pedName)
  947.                         end
  948.                         local r = getRealTime()
  949.                         local timeString = ("%02d/%02d/%04d %02d:%02d"):format(r.monthday, r.month + 1, r.year+1900, r.hour, r.minute)
  950.                         local ownerNoti = "A customer bought a "..item.name.." for $"..exports.global:formatMoney(price).."."
  951.                         local logString = "- "..timeString.." : A customer bought a "..item.name.." for $"..exports.global:formatMoney(price)..".\n"
  952.                        
  953.                         exports.global:sendLocalText(client, "* "..pedName.." gave "..playerName.." a "..item.name..".", 255, 51, 102, 30, {}, true)
  954.                         storeKeeperSay(client, "Here you are. And..", pedName)
  955.                         if playerGender == 0 then
  956.                                 storeKeeperSay(client, "Thank you sir, Have a nice day!", pedName)
  957.                         else
  958.                                 storeKeeperSay(client, "Thank you ma'ma, Have a nice day!", pedName)
  959.                         end
  960.                        
  961.                         --notifyAllShopOwners(source, ownerNoti.." Come and collect the money when you got time ;)")
  962.                        
  963.                         local previousSales = getElementData(source, "sSales") or ""
  964.                         logString = string.sub(logString..previousSales,1,5000)
  965.                         setElementData(source, "sSales", logString, true)
  966.                         dbExec(mysql:getConnection(),"UPDATE `shops` SET `sIncome` = `sIncome` + '" .. tostring(price) .. "', `sSales` = '"..logString:gsub("'","''").."' WHERE `id` = '"..tostring(getElementData(source,"dbid")).."'")
  967.         end
  968.         else
  969.                 outputChatBox( "You do not have enough space to carry this " .. item.name .. ".", client, 255, 0, 0 )
  970.         end
  971. end )
  972.  
  973. function solveSupplies(source, client, suppliesToTake, dimension)
  974.         for key, interior in pairs(getElementsByType("interior")) do
  975.                 if getElementData(interior, "dbid") == dimension then
  976.                         local status = getElementData(interior, "status")
  977.                         local currentSupplies = status[6] or 0
  978.                         local ownerID = status[4]
  979.                         local interiorType = tonumber(status[1] or 2)
  980.                         if ownerID == getElementData(client, "dbid") then
  981.                                 --suppliesToTake = suppliesToTake*profitRate
  982.                                 --ownerPlayer = ownerID
  983.                                 --outputChatBox( "Buying items from your own shop will not make you profit.", client, 255, 0, 0 )
  984.                         end
  985.                         local remainingSupplies = currentSupplies - suppliesToTake
  986.                         --outputDebugString(currentSupplies.."-"..suppliesToTake)
  987.                         if remainingSupplies < 0 and (interiorType ~= 2) then
  988.                                 return false, "This item is out of stock."
  989.                         else
  990.                                 status[6] = remainingSupplies
  991.                                 setElementData(interior, "status", status, true)
  992.                                 if remainingSupplies < 50 and ownerID == getElementData(client, "dbid") then
  993.                                         outputChatBox( "Supplies in your business #" .. dimension .. " are low. Fill 'em up. ((use /ordersupplies))", client, 255, 194, 14 )
  994.                                 end
  995.                                
  996.                                 -- take the outstanding supplies
  997.                                 dbExec(mysql:getConnection(),"UPDATE `interiors` SET `supplies` = '"..remainingSupplies.."' WHERE id = " .. (dimension))
  998.                
  999.                                 return true, "Cool."
  1000.                         end
  1001.                         return false, "Error code 'ESDAFE1241', please report to Maxime"
  1002.                 end
  1003.         end
  1004. end
  1005.  
  1006. globalSupplies = 0
  1007.  
  1008. function updateGlobalSupplies(value)
  1009.     globalSupplies = globalSupplies + value
  1010.     dbExec(mysql:getConnection(),"UPDATE settings SET value='" .. (tostring(globalSupplies)) .. "' WHERE name='globalsupplies'")
  1011. end
  1012. addEvent("updateGlobalSupplies", true)
  1013. addEventHandler("updateGlobalSupplies", getRootElement(), updateGlobalSupplies)
  1014.  
  1015.  
  1016. addEvent("shop:handleSupplies", true)
  1017.  
  1018. function handleSupplies(element, slot, event, worldItem)
  1019.    
  1020.     return false
  1021. end
  1022. addEventHandler("shop:handleSupplies", getRootElement(), handleSupplies)
  1023.  
  1024. function canIUseThisName(pedName)
  1025.     return true, "This name is cool"
  1026. end
  1027.  
  1028. function shopRemoteOrderSupplies(thePlayer, dim, weight)
  1029.  
  1030.     return success
  1031. end
  1032. addEvent("shop:shopRemoteOrderSupplies", true)
  1033. addEventHandler("shop:shopRemoteOrderSupplies", getRootElement(), shopRemoteOrderSupplies)
  1034.  
  1035.  
  1036. function resStart()
  1037.     dbQuery(
  1038.         function(qh)
  1039.             local res, rows, err = dbPoll(qh, 0)
  1040.             if rows > 0 then
  1041.                 for index, row in ipairs(res) do
  1042.                      globalSupplies = tonumber(row["value"]) or 0
  1043.                 end
  1044.             end
  1045.         end,
  1046.     mysql:getConnection(), "SELECT value FROM settings WHERE name='globalsupplies' LIMIT 1")
  1047. end
  1048. addEventHandler("onResourceStart", getResourceRootElement(), resStart)