Advertisement
Guest User

Untitled

a guest
Jun 20th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 32.62 KB | None | 0 0
  1. -- MySQL Housesystem created & released by Noneatme(MuLTi), Do not remove credits! --
  2. -- All Rights go to Noneatme --
  3.  
  4. --[[ Total time token:
  5.     - 3 hour
  6.     - 1.5 hour
  7.     ________
  8.     4.5 hours
  9. ]]
  10.  
  11. ------------------------
  12. -- CONNECTION HANDLER --
  13. ------------------------
  14.  
  15. -- Here you can change some settings --
  16. -- FIRST CONNECTION --
  17. -- Default
  18. host = "localhost" -- Database Host
  19. username = "root" -- Database Username
  20. password = "qwe123123az" -- Database Password
  21. db = "accounts" -- Database ( Where you got your tables )
  22. dbhouse = "mta"
  23.  
  24. --handler1 = mysql_connect( host, username, password, db )
  25.  
  26. local mysqlhost1 = "127.0.0.1"
  27. local mysqluser1 = "root"
  28. local mysqlpassword1 = "qwe123123az"
  29. local mysqldatabase1 = "mta"
  30.  
  31. -- SECOND CONNECTION, OPTIONAL IF CONNECTION 1 DON'T WORK
  32.  
  33. local mysqlhost2 = "127.0.0.1"
  34. local mysqluser2 = "root"
  35. local mysqlpassword2 = "qwe123123az"
  36. local mysqldatabase2 = "mta"
  37.  
  38. local dbpTime = 500 -- How many Miliseconds will use the dbPoll function for waiting for a result
  39.  
  40. local max_player_houses = 1 -- Define the buyable houses per player
  41. local sellhouse_value = 80 -- The ammount in percent that you get back if you sell a house
  42. local open_key = "F5" -- Define the key for the infomenue and the housepanel
  43.  
  44. -- I don't know whats the right time for that --
  45.  
  46. -----------------------------------------------------------------
  47. -- IF YOU CAN'T WRITE IN LUA, DO NOT EDIT ANYTHING ABOVE HERE! --
  48. -----------------------------------------------------------------
  49.  
  50. -- EVENTS --
  51.  
  52. addEvent("onHouseSystemHouseCreate", true)
  53. addEvent("onHouseSystemHouseLock", true)
  54. addEvent("onHouseSystemHouseDeposit", true)
  55. addEvent("onHouseSystemHouseWithdraw", true)
  56. addEvent("onHouseSystemWeaponDeposit", true)
  57. addEvent("onHouseSystemWeaponWithdraw", true)
  58. addEvent("onHouseSystemRentableSwitch", true)
  59. addEvent("onHouseSystemRentalprice", true)
  60. addEvent("onHouseSystemTenandRemove", true)
  61. addEvent("onHouseSystemInfoBuy", true)
  62. addEvent("onHouseSystemInfoRent", true)
  63. addEvent("onHouseSystemInfoEnter", true)
  64. addEvent("onHouseSystemInfoExit", true)
  65. addEvent("onHouseSystemInfoBuy", true)
  66. addEvent("onHouseSystemInfoSell", true)
  67.  
  68.  
  69. local handler -- local only, we don't need a global handler
  70.  
  71. local saveableValues = {
  72.     ["MONEY"] = "MONEY",
  73.     ["WEAP1"] = "WEAP1",
  74.     ["WEAP2"] = "WEAP2",
  75.     ["WEAP3"] = "WEAP3",
  76.     ["LOCKED"] = "LOCKED",
  77.     ["OWNER"] = "OWNER",
  78.     ["RENTABLE"] = "RENTABLE",
  79.     ["RENTALPRICE"] = "RENTALPRICE",
  80.     ["RENT1"] = "RENT1",
  81.     ["RENT2"] = "RENT2",
  82.     ["RENT3"] = "RENT3",
  83.     ["RENT4"] = "RENT4",
  84.     ["RENT5"] = "RENT5",
  85. }
  86.  
  87.  
  88. local created = false -- DONT EDIT
  89. local houseid = 0 -- Define the Houseid,
  90.  
  91. local house = {} -- The House array
  92. local houseData = {} -- The House Data arry
  93. local houseInt = {} -- The House Interior array
  94. local houseIntData = {} -- The House Interior Data Array xD
  95.  
  96. local buildStartTick
  97. local buildEndTick
  98.  
  99. local rentTimer
  100.  
  101. -- STARTUP EVENT HANDLER --
  102.  
  103. addEventHandler("onResourceStart", getResourceRootElement(), function()
  104.     handler = dbConnect("mysql", "dbname="..mysqldatabase1..";host="..mysqlhost1, mysqluser1, mysqlpassword1, "autoreconnect=1")
  105.  
  106.     -- If the Handler 1 dont work
  107.     if not(handler) then   
  108.         outputServerLog("[HOUSESYSTEM]MySQL handler 1 not accepted! Trying secondary handler...")  
  109.         handler = dbConnect("mysql", "dbname="..mysqldatabase2..";host="..mysqlhost2, mysqluser2, mysqlpassword2, "autoreconnect=1")
  110.         if not(handler) then
  111.             outputServerLog("[HOUSESYSTEM]MySQL handler 2 not accepted! Shutting down...")
  112.             cancelEvent()
  113.         else
  114.             outputServerLog("[HOUSESYSTEM]MySQL handler 2 accepted!")
  115.             housesys_startup()
  116.         end
  117.     else
  118.         outputServerLog("[HOUSESYSTEM]MySQL handler 1 accepted!")
  119.         housesys_startup()
  120.     end
  121. end)
  122.  
  123. -- SHUTDOWN EVENT HANDLER --
  124. addEventHandler("onResourceStop", getResourceRootElement(), function()
  125.     -- Free the arrays --
  126.     for index, houses in pairs(house) do
  127.         houses = nil
  128.     end
  129.     for index, houseDatas in pairs(houseData) do
  130.         houseDatas = nil
  131.     end
  132.     for index, houseInts in pairs(houseInt) do
  133.         houseInts = nil
  134.     end
  135.     for index, houseIntDatas in pairs(houseIntData) do
  136.         houseIntDatas = nil
  137.     end
  138.    
  139.     houseid = 0
  140.     created = false
  141. end)
  142.  
  143. --------------
  144. -- COMMANDS --
  145. --------------
  146.  
  147. -- /unrent --
  148.  
  149. addCommandHandler("unrent", function(thePlayer)
  150.     if(getElementData(thePlayer, "house:lastvisit")) and (getElementData(thePlayer, "house:lastvisit") ~= false)  then
  151.         local id = tonumber(getElementData(thePlayer, "house:lastvisit"))
  152.         if(isPlayerRentedHouse(thePlayer, id) == false) then
  153.             outputChatBox("You are not tenad of this house!", thePlayer, 255, 0, 0)
  154.             return
  155.         end
  156.         local sucess = removeHouseTenand(id, thePlayer)
  157.         if(sucess == true) then
  158.             outputChatBox("You have sucessfull terminate the tenancy!", thePlayer, 0, 255, 0)
  159.         else
  160.             outputChatBox("An error occurred!", thePlayer, 255, 0, 0)
  161.         end
  162.     end
  163. end)
  164.  
  165. -- /rent --
  166.  
  167. addCommandHandler("rent", function(thePlayer)
  168.     if(getElementData(thePlayer, "house:lastvisit")) and (getElementData(thePlayer, "house:lastvisit") ~= false)  then
  169.         local id = tonumber(getElementData(thePlayer, "house:lastvisit"))
  170.         if(houseData[id]["OWNER"] == getPlayerName(thePlayer)) then
  171.             outputChatBox("You can't rent here! It's your house!", thePlayer, 255, 0, 0)
  172.             return
  173.         end
  174.         if(tonumber(houseData[id]["RENTABLE"]) ~= 1) then
  175.             outputChatBox("This house is not rentable!", thePlayer, 255, 0, 0)
  176.             return
  177.         end
  178.         if(getPlayerRentedHouse(thePlayer) ~= false) then
  179.             outputChatBox("You are allready a tenand in a house! Use /unrent first.", thePlayer, 255, 0, 0)
  180.             return
  181.         end
  182.         local sucess = addHouseTenand(thePlayer, id)
  183.         if(sucess == true) then
  184.             outputChatBox("You are now tenand this house!", thePlayer, 0, 255, 0)
  185.         else
  186.             outputChatBox("You can't rent this house!", thePlayer, 255, 0, 0)
  187.         end
  188.     end
  189. end)
  190.  
  191. -- /createhouse --
  192.  
  193. addCommandHandler("createhouse", function(thePlayer)
  194.     if(hasObjectPermissionTo ( thePlayer, "function.kickPlayer", false ) ) then
  195.         if(getElementInterior(thePlayer) ~= 0) then
  196.             outputChatBox("Вы должны находится снаружи дома!", thePlayer, 255, 0, 0)
  197.             return
  198.         end
  199.         if(isPedInVehicle(thePlayer) == true) then
  200.             outputChatBox("Выйдите из авто!", thePlayer, 255, 0, 0)
  201.             return
  202.         end
  203.         -- INSERT SECURITY OPTIONS LIKE ADMINLEVEL HERE( if(adminlevel > shit) then ...)
  204.         triggerClientEvent(thePlayer, "onClientHouseSystemGUIStart", thePlayer)
  205.     else
  206.         outputChatBox("У вас не достаточно прав для использования данной команды!", thePlayer, 255, 0, 0)
  207.     end
  208. end)
  209.  
  210. -- /in --
  211. rootElement = getRootElement()
  212. addEventHandler("onHouseSystemInfoEnter", rootElement,function(thePlayer)
  213.     thePlayer =  source
  214.     if(getElementData(thePlayer, "house:lastvisit")) and (getElementData(thePlayer, "house:lastvisit") ~= false)  then
  215.         local house = getElementData(thePlayer, "house:lastvisit")
  216.         if(house) then
  217.             local id = tonumber(house)
  218.             if(tonumber(houseData[id]["LOCKED"]) == 0) or (houseData[id]["OWNER"] == getPlayerName(thePlayer)) or (isPlayerRentedHouse(thePlayer, id) == true) then
  219.                 local int, intx, inty, intz, dim = houseIntData[id]["INT"], houseIntData[id]["X"], houseIntData[id]["Y"], houseIntData[id]["Z"], id
  220.                 setElementData(thePlayer, "house:in", true)
  221.                 setInPosition(thePlayer, intx, inty, intz, int, false, dim)
  222.                 unbindKey(thePlayer, open_key, "down", togglePlayerInfomenue, id)
  223.                 setElementData(thePlayer, "house:lastvisitINT", id)
  224.                 if(houseData[id]["OWNER"] == getPlayerName(thePlayer)) or (isPlayerRentedHouse(thePlayer, id) == true) then
  225.                     bindKey(thePlayer, open_key, "down", togglePlayerHousemenue, id)
  226.                 end
  227.             else
  228.                 outputChatBox("You don't have the housekey for this House!", thePlayer, 255, 0, 0)
  229.             end
  230.         end
  231.     end
  232. end)
  233.  
  234. -- /out --
  235.  
  236. addEventHandler("onHouseSystemInfoExit", rootElement,function(thePlayer)
  237.     thePlayer =  source
  238.     if(getElementData(thePlayer, "house:lastvisitINT")) and (getElementData(thePlayer, "house:lastvisitINT") ~= false)  then
  239.         local house = getElementData(thePlayer, "house:lastvisitINT")
  240.         if(house) then
  241.             local id = tonumber(house)
  242.             local x, y, z = houseData[id]["X"], houseData[id]["Y"], houseData[id]["Z"]
  243.             setElementData(thePlayer, "house:in", false)
  244.             setInPosition(thePlayer, x, y, z, 0, false, 0)
  245.         end
  246.     end
  247. end)
  248.  
  249. -- /buyhouse --
  250.  
  251. addEventHandler("onHouseSystemInfoBuy", rootElement,function(thePlayer)
  252. thePlayer =  source
  253.     if(getElementData(thePlayer, "house:lastvisit")) and (getElementData(thePlayer, "house:lastvisit") ~= false)  then
  254.         local house = getElementData(thePlayer, "house:lastvisit")
  255.         if(house) then
  256.             local id = house
  257.             local owner = houseData[id]["OWNER"]
  258.             if(owner ~= "no-one") then
  259.                 outputChatBox("Этот дом уже куплен!", thePlayer, 255, 0, 0)
  260.             else
  261.                 local houses = 0
  262.                 for index, col in pairs(getElementsByType("colshape")) do
  263.                     if(getElementData(col, "house") == true) and (houseData[getElementData(col, "ID")]["OWNER"] == getPlayerName(thePlayer)) then
  264.                         houses = houses+1
  265.                         if(houses == max_player_houses) then
  266.                             outputChatBox("У вас уже есть "..max_player_houses.." дом! Продайте его, чтобы купить новый!", thePlayer, 255, 0, 0)
  267.                             return
  268.                         end
  269.                     end
  270.                 end
  271.                 local money = getPlayerMoney(thePlayer)
  272.                 local price = houseData[id]["PRICE"]
  273.                 if(money < price) then outputChatBox("Для покупки дома, не хватает "..(price-money).."$", thePlayer, 255, 0, 0) return end
  274.                 setHouseData(id, "OWNER", getPlayerName(thePlayer))
  275.                 givePlayerMoney(thePlayer, -price)
  276.                 local escapedName = getPlayerName(source)
  277.                 --buyhouse1 = id
  278.                 --handler1 = mysql_connect( host, username, password, db )
  279.                 --mysql_query(handler1, "UPDATE account_data SET housenumber='" .. buyhouse1 .. "' WHERE AccountName='" .. escapedName .. "'")
  280.                 outputChatBox("Поздравляем! Вы успешно купили дом.", thePlayer, 0, 255, 0)
  281.                 setElementModel(houseData[id]["PICKUP"], 1272)
  282.                 setElementModel(houseData[id]["BLIP"], 32)
  283.             end
  284.         end
  285.     end
  286. end)
  287.  
  288. -- /sellhouse --
  289.  
  290. addEventHandler("onHouseSystemInfoSell", rootElement,function(thePlayer)
  291. thePlayer =  source
  292.     if(getElementData(thePlayer, "house:lastvisit")) and (getElementData(thePlayer, "house:lastvisit") ~= false)  then
  293.         local house = getElementData(thePlayer, "house:lastvisit")
  294.         if(house) then
  295.             local id = house
  296.             local owner = houseData[id]["OWNER"]
  297.             if(owner ~= getPlayerName(thePlayer)) then
  298.                 outputChatBox("Этот дом не принадлежит вам!", thePlayer, 255, 0, 0)
  299.             else
  300.                 local price = houseData[id]["PRICE"]
  301.                 setHouseData(id, "OWNER", "no-one")
  302.                 setHouseData(id, "RENTABLE", 0)
  303.                 setHouseData(id, "RENTALPRICE", 0)
  304.                 for i = 1, 5, 1 do
  305.                     setHouseData(id, "RENT"..i, "no-one")
  306.                 end
  307.                 givePlayerMoney(thePlayer, math.floor(price/100*sellhouse_value))
  308.                 local escapedName = getPlayerName(source)
  309.                 --selltest = 0
  310.                 --spawnstandarter = 1
  311.                 --handler1 = mysql_connect( host, username, password, db )
  312.                 --mysql_query(handler1, "UPDATE account_data SET housenumber='" .. selltest .. "' WHERE AccountName='" .. escapedName .. "'")
  313.                 --mysql_query(handler1, "UPDATE account_data SET spawn='" .. spawnstandarter .. "' WHERE AccountName='" .. escapedName .. "'")
  314.                 outputChatBox("Поздравляем! Вы успешно продали дом за $"..math.floor(price/100*sellhouse_value).."", thePlayer, 0, 255, 0)
  315.                 setElementModel(houseData[id]["PICKUP"], 1273)
  316.                 setElementModel(houseData[id]["BLIP"], 31)
  317.             end
  318.         end
  319.     end
  320. end)
  321.  
  322. -- /deletehouse --
  323.  
  324. addCommandHandler("deletehouse", function(thePlayer, cmd, id)
  325.     if(hasObjectPermissionTo ( thePlayer, "function.kickPlayer", false ) ) then
  326.         id = tonumber(id)
  327.         if not(id) then return end
  328.         if not(house[id]) then
  329.             outputChatBox("Дом номер "..id.." не найден!", thePlayer, 255, 0, 0)
  330.             return
  331.         end
  332.         local query = dbQuery(handler, "DELETE FROM houses WHERE ID = '"..id.."';")
  333.         local result = dbPoll(query, dbpTime)
  334.         if(result) then
  335.             destroyElement(houseData[id]["BLIP"])
  336.             destroyElement(houseData[id]["PICKUP"])
  337.             destroyElement(houseIntData[id]["PICKUP"])
  338.             houseData[id] = nil
  339.             houseIntData[id] = nil
  340.             destroyElement(house[id])
  341.             destroyElement(houseInt[id])
  342.             outputChatBox("Дом "..id.." был успешно удален!", thePlayer, 0, 255, 0)
  343.             house[id] = false
  344.         else
  345.             error("Дом "..id.." был создан в игре, но не найден в базе! Какая-то хуйня твориться!")
  346.         end
  347.     else
  348.         outputChatBox("У вас не достаточно прав для использования данной команды!", thePlayer, 255, 0, 0)
  349.     end
  350. end)
  351.  
  352. -- INSERT INTO dbs_housesystem.houses (X, Y, Z, INTERIOR, INTX, INTY, INTZ, MONEY, WEAP1, WEAP2, WEAP3) values("0.1", "0.1", "0.1", "5", "0.2", "0.2", "0.2", "2000", "46,1", "22,200", "25, 200")
  353.  
  354. --------------------
  355. -- BIND FUNCTIONS --
  356. --------------------
  357.  
  358. function togglePlayerInfomenue(thePlayer, key, state, id)
  359.     if(id) then
  360.         local locked = houseData[id]["LOCKED"]
  361.         local rentable = houseData[id]["RENTABLE"]
  362.         local rentalprice = houseData[id]["RENTALPRICE"]
  363.         local owner = houseData[id]["OWNER"]
  364.         local price = houseData[id]["PRICE"]
  365.         local x, y, z = getElementPosition(house[id])
  366.         local house = getPlayerRentedHouse(thePlayer)
  367.         if(house ~= false) then house = true end
  368.         local isrentedin = isPlayerRentedHouse(thePlayer, id)
  369.         triggerClientEvent(thePlayer, "onClientHouseSystemInfoMenueOpen", thePlayer, owner, x, y, z, price, locked, rentable, rentalprice, id, house, isrentedin)
  370.     end
  371. end
  372.  
  373. function togglePlayerHousemenue(thePlayer, key, state, id)
  374.     if(id) then
  375.         if(getElementInterior(thePlayer) ~= 0) then
  376.             local locked = houseData[id]["LOCKED"]
  377.             local money = houseData[id]["MONEY"]
  378.             local weap1 = houseData[id]["WEAPONS"][1]
  379.             local weap2 = houseData[id]["WEAPONS"][2]
  380.             local weap3 = houseData[id]["WEAPONS"][3]
  381.             local rentable = houseData[id]["RENTABLE"]
  382.             local rent = houseData[id]["RENTALPRICE"]
  383.             local tenands = getHouseTenands(id)
  384.             local owner = false
  385.             if(getPlayerName(thePlayer) == houseData[id]["OWNER"]) then
  386.                 owner = true
  387.             end
  388.             local canadd = canAddHouseTenand(id)
  389.             triggerClientEvent(thePlayer, "onClientHouseSystemMenueOpen", thePlayer, owner, locked, money, weap1, weap2, weap3, id, rentable, rent, tenands, canadd)
  390.         end
  391.     else
  392.         triggerClientEvent(thePlayer, "onClientHouseSystemMenueOpen", thePlayer )
  393.     end
  394. end
  395.  
  396. -------------------------------
  397. -- HOUSE CREATION ON STARTUP --
  398. -------------------------------
  399.  
  400. -- BUILDHOUSE FUNCTION --
  401.  
  402. local function buildHouse(id, x, y, z, interior, intx, inty, intz, money, weapons, locked, price, owner, rentable, rentalprice, rent1, rent2, rent3, rent4, rent5)
  403.     if(id) and (x) and(y) and (z) and (interior) and (intx) and (inty) and (intz) and (money) and (weapons) then
  404.         houseid = id
  405.         house[id] = createColSphere(x, y, z, 1.5) -- This is the house, hell yeah
  406.         houseData[id] = {}
  407.         local house = house[id] -- I'm too lazy...
  408.         setElementData(house, "house", true) -- Just for client code only
  409.        
  410.         local houseIntPickup = createPickup(intx, inty, intz, 3, 1318, 100)
  411.         setElementInterior(houseIntPickup, interior)
  412.         setElementDimension(houseIntPickup, id)
  413.        
  414.         houseInt[id] = createColSphere(intx, inty, intz, 1.5) -- And this is the Exit
  415.         setElementInterior(houseInt[id], interior)
  416.         setElementDimension(houseInt[id], id) -- The House Dimension is the house ID
  417.         setElementData(houseInt[id], "house", false)
  418.         --------------------
  419.         -- EVENT HANDLERS --
  420.         --------------------
  421.    
  422.         -- IN --
  423.         addEventHandler("onColShapeHit", house, function(hitElement)
  424.             if(getElementType(hitElement) == "player") then
  425.                 setElementData(hitElement, "house:lastvisit", id)
  426.                 bindKey(hitElement, open_key, "down", togglePlayerInfomenue, id)
  427.                 outputChatBox("Нажмите "..open_key..", чтобы открыть информацию о доме.", hitElement, 0, 255, 255)
  428.             end
  429.         end)
  430.        
  431.         addEventHandler("onColShapeLeave", house, function(hitElement)
  432.             if(getElementType(hitElement) == "player") then
  433.                 setElementData(hitElement, "house:lastvisit", false)
  434.                 unbindKey(hitElement, open_key, "down", togglePlayerInfomenue, id)
  435.                 --outputChatBox(id)
  436.             end
  437.         end)
  438.        
  439.         -- OUT --
  440.        
  441.         addEventHandler("onColShapeHit", houseInt[id], function(hitElement, dim)
  442.             if(dim == true) then
  443.                 if(getElementType(hitElement) == "player") then
  444.                     unbindKey(hitElement, open_key, "down", togglePlayerInfomenue, id)
  445.                     setElementData(hitElement, "house:lastvisitINT", id)
  446.                     if(houseData[id]["OWNER"] == getPlayerName(hitElement)) or (isPlayerRentedHouse(hitElement, id) == true) then
  447.                         bindKey(hitElement, open_key, "down", togglePlayerHousemenue, id)
  448.                     end
  449.                     --outputChatBox(id)
  450.                 end
  451.             end
  452.         end)
  453.        
  454.         addEventHandler("onColShapeLeave", houseInt[id], function(hitElement, dim)
  455.             if(dim == true) then
  456.                 if(getElementType(hitElement) == "player") then
  457.                     setElementData(hitElement, "house:lastvisitINT", false)
  458.                     if(houseData[id]["OWNER"] == getPlayerName(hitElement)) or (isPlayerRentedHouse(hitElement, id) == true) then
  459.                         unbindKey(hitElement, open_key, "down", togglePlayerHousemenue, id)
  460.                     end
  461.                     --outputChatBox(id)
  462.                 end
  463.             end
  464.         end)
  465.        
  466.         -- Set data for HOUSE --
  467.         houseData[id]["HOUSE"] = house
  468.         houseData[id]["DIM"] = id
  469.         houseData[id]["MONEY"] = money
  470.         houseData[id]["WEAPONS"] = weapons
  471.         houseData[id]["INTHOUSE"] = houseInt[id]
  472.         houseData[id]["LOCKED"] = locked
  473.         houseData[id]["PRICE"] = price
  474.         houseData[id]["OWNER"] = owner
  475.         houseData[id]["X"] = x
  476.         houseData[id]["Y"] = y
  477.         houseData[id]["Z"] = z
  478.         houseData[id]["RENTABLE"] = rentable
  479.         houseData[id]["RENTALPRICE"] = rentalprice
  480.         houseData[id]["RENT1"] = rent1
  481.         houseData[id]["RENT2"] = rent2
  482.         houseData[id]["RENT3"] = rent3
  483.         houseData[id]["RENT4"] = rent4
  484.         houseData[id]["RENT5"] = rent5
  485.         -- HOUSE PICKUP --
  486.         local housePickup
  487.         if(owner ~= "no-one") then
  488.             housePickup = createPickup(x, y, z-0.5, 3, 1272, 100)
  489.         else
  490.             housePickup = createPickup(x, y, z-0.5, 3, 1273, 100)
  491.         end
  492.         -- HOUSE BLIP --
  493.         local houseBlip
  494.         if(owner ~= "no-one") then
  495.             houseBlip = createBlip(x, y, z, 32, 2, 255, 0, 0, 255, 0, 50)
  496.         else
  497.             houseBlip = createBlip(x, y, z, 31, 2, 255, 0, 0, 255, 0, 50)
  498.         end
  499.         -- SET THE DATA --
  500.         houseData[id]["PICKUP"] = housePickup
  501.         houseData[id]["BLIP"] = houseBlip
  502.        
  503.         setElementData(house, "PRICE", price)
  504.         setElementData(house, "OWNER", owner)
  505.         setElementData(house, "LOCKED", locked)
  506.         setElementData(house, "ID", id)
  507.         setElementData(house, "RENTABLE", rentable)
  508.         setElementData(house, "RENTALPRICE", rentalprice)
  509.        
  510.         -- SET DATA FOR HOUSEINTERIOR --
  511.         houseIntData[id] = {}
  512.         houseIntData[id]["OUTHOUSE"] = houseData[id]["HOUSE"]
  513.         houseIntData[id]["INT"] = interior
  514.         houseIntData[id]["X"] = intx
  515.         houseIntData[id]["Y"] = inty
  516.         houseIntData[id]["Z"] = intz
  517.         houseIntData[id]["PICKUP"] = houseIntPickup
  518.         outputServerLog("Дом с номером "..id.." успешно создан!")
  519.         buildEndTick = getTickCount()
  520.         -- TRIGGER TO ALL CLIENTS THAT THE HOUSE HAS BEEN CREATEEEEEEEEEEEEEEEEEEEEEEED --
  521.         setTimer(triggerClientEvent, 1000, 1, "onClientHouseSystemColshapeAdd", getRootElement(), house)
  522.     else
  523.         if not(id) then
  524.             error("Arguments @buildHouse not valid! There is no Houseid!")
  525.         else
  526.             error("Arguments @buildHouse not valid! Houseid = "..id)
  527.         end
  528.     end
  529. end
  530.  
  531. -- TAKE PLAYER RENT --
  532.  
  533. local function takePlayerRent()
  534.     for index, player in pairs(getElementsByType("player")) do
  535.         if(getPlayerRentedHouse(player) ~= false) then
  536.             local id = getPlayerRentedHouse(player)
  537.             local owner = houseData[id]["OWNER"]
  538.             local rentable = tonumber(houseData[id]["RENTABLE"])
  539.             if(rentable == 1) then
  540.                 local rentprice = tonumber(houseData[id]["RENTALPRICE"])
  541.                 takePlayerMoney(player, rentprice) -- Takes the player money for the rent
  542.                 outputChatBox("Вы уплатили $"..rentprice.." за аренду жилого помещения!", player, 255, 255, 0)
  543.                 if(getPlayerFromName(owner)) then
  544.                     givePlayerMoney(getPlayerFromName(owner), rentprice) -- Gives the owner the rentalprice
  545.                     outputChatBox("Вы получили $"..rentprice.." за аренду вашего дома!", getPlayerFromName(owner), 255, 255, 0)
  546.                 end
  547.             end
  548.         end
  549.     end
  550. end
  551.  
  552. -- HOUSE DATABASE EXECUTION --
  553.  
  554. function housesys_startup()
  555.     if(created == true) then
  556.         error("Дома успешно загружены!")
  557.         return
  558.     end
  559.     buildStartTick = getTickCount()
  560.     local query = dbQuery(handler, "SELECT * FROM houses;" )
  561.     local result, numrows = dbPoll(query, dbpTime)
  562.     if (result and numrows > 0) then
  563.         for index, row in pairs(result) do
  564.             local id = row['ID']
  565.             local x, y, z = row['X'], row['Y'], row['Z']
  566.             local int, intx, inty, intz = row['INTERIOR'], row['INTX'], row['INTY'], row['INTZ']
  567.             local money, weap1, weap2, weap3 = row['MONEY'], row['WEAP1'], row['WEAP2'], row['WEAP3']
  568.             local locked = row['LOCKED']
  569.             local price = row['PRICE']
  570.             local owner = row['OWNER']
  571.             local rentable = row['RENTABLE']
  572.             local rentalprice = row['RENTALPRICE']
  573.             local rent1, rent2, rent3, rent4, rent5 = row['RENT1'],row['RENT2'], row['RENT3'], row['RENT4'], row['RENT5']
  574.             local weapontable = {}
  575.             weapontable[1] = weap1
  576.             weapontable[2] = weap2
  577.             weapontable[3] = weap3
  578.             buildHouse(id, x, y, z, int, intx, inty, intz, money, weapontable, locked, price, owner, rentable, rentalprice, rent1, rent2, rent3, rent4, rent5)
  579.         end
  580.         dbFree(query)
  581.     else
  582.         error("Чувак, ошибка с базой, дома не загружены :-(")
  583.     end
  584.     created = true
  585.     setTimer(function()
  586.         local elapsed = (buildEndTick-buildStartTick)
  587.         outputServerLog("Время загрузки "..(elapsed/1000).." секунд")
  588.     end, 1000, 1)
  589.     rentTimer = setTimer(takePlayerRent, 60*60*1000, -1)
  590. end
  591.  
  592. -- House Data array set --
  593.  
  594. function setHouseData(ID, typ, value)
  595.     -- Security array --
  596.     houseData[ID][typ] = value
  597.     setElementData(house[ID], typ, value)
  598.     if(saveableValues[typ]) then
  599.         local query = dbQuery(handler, "UPDATE houses SET "..saveableValues[typ].." = '"..value.."' WHERE ID = '"..ID.."';" )
  600.         local result = dbPoll(query, dbpTime)
  601.         if(result) then
  602.             dbFree(query)
  603.         else
  604.             error("Can't save Data: "..typ.." with the value: "..value.." for house ID "..ID.."!")
  605.         end
  606.     end
  607. end
  608.  
  609.  
  610. --------------------
  611. -- EVENT HANDLERS --
  612. --------------------
  613.  
  614. -- INFO RENT -
  615.  
  616. addEventHandler("onHouseSystemInfoRent", getRootElement(), function(id, value)
  617.     if(houseData[id]) then
  618.         if(value == true) then
  619.             executeCommandHandler("rent", source)
  620.         else
  621.             executeCommandHandler("unrent", source)
  622.         end
  623.     end
  624. end)
  625.  
  626.  
  627. -- INFO ENTER --
  628.  
  629.  
  630.  
  631.  
  632. -- INFO BUY --
  633. addEventHandler("onHouseSystemInfoBuy", getRootElement(), function(id, value)
  634.     if(houseData[id]) then
  635.         if(value == true) then
  636.             executeCommandHandler("buyhouse", source)
  637.         else
  638.             executeCommandHandler("sellhouse", source)
  639.         end
  640.     end
  641. end)
  642.  
  643.  
  644. -- TENAND REMOVE --
  645.  
  646. addEventHandler("onHouseSystemTenandRemove", getRootElement(), function(id, value)
  647.     if(houseData[id]) then
  648.         local sucess = removeHouseTenand(id, value)
  649.         if(sucess == true) then
  650.             outputChatBox("Вы успешно удалили арендатора "..value.."!", source, 0, 255, 0)
  651.             triggerClientEvent(source, "onClientHouseSystemMenueUpdate", source, "TENANDS", getHouseTenands(id))
  652.         end
  653.     end
  654. end)
  655.  
  656. -- SET RENTALPRICE --
  657.  
  658. addEventHandler("onHouseSystemRentalprice", getRootElement(), function(id, value)
  659.     if(houseData[id]) then
  660.         local oldvalue = tonumber(houseData[id]["RENTALPRICE"])
  661.         if(oldvalue < value) then
  662.             local tenands = getHouseTenands(id)
  663.             local users = {}
  664.             for i = 1, 5, 1 do
  665.                 if(tenands[i] ~= "no-one") then
  666.                     users[i] = tenands[i]
  667.                 end
  668.             end
  669.             if(#users > 0) then
  670.                 outputChatBox("Вы не можете изменить стоимость аренды, пока в ваш дом кто-то арендует.", source, 255, 0, 0)
  671.                 return
  672.             end
  673.         end
  674.         setHouseData(id, "RENTALPRICE", value)
  675.         outputChatBox("Новая цена за аренду вашего дома составляет: $"..value.."!", source, 0, 255, 0)
  676.         triggerClientEvent(source, "onClientHouseSystemMenueUpdate", source, "RENTALPRICE", value)
  677.     end
  678. end)
  679.  
  680. -- RENTABLE SWITCH --
  681. addEventHandler("onHouseSystemRentableSwitch", getRootElement(), function(id)
  682.     if(houseData[id]) then
  683.         local state = tonumber(houseData[id]["RENTABLE"])
  684.         if(state == 0) then
  685.             setHouseData(id, "RENTABLE", 1)
  686.             triggerClientEvent(source, "onClientHouseSystemMenueUpdate", source, "RENTABLE", true)
  687.             outputChatBox("Вы сняли свой дом с аренды!", source, 0, 255, 0)
  688.         else
  689.             setHouseData(id, "RENTABLE", 0)
  690.             triggerClientEvent(source, "onClientHouseSystemMenueUpdate", source, "RENTABLE", false)
  691.             outputChatBox("Вы сдали свой дом в аренду!", source, 0, 255, 0)
  692.         end
  693.     end
  694. end)
  695.  
  696.  
  697. -- CREATE HOUSE --
  698.  
  699. addEventHandler("onHouseSystemHouseCreate", getRootElement(), function(x, y, z, int, intx, inty, intz, price)
  700.     local query = dbQuery(handler, "INSERT INTO houses (X, Y, Z, INTERIOR, INTX, INTY, INTZ, PRICE) values ('"..x.."', '"..y.."', '"..z.."', '"..int.."', '"..intx.."', '"..inty.."', '"..intz.."', '"..price.."');")
  701.     local result, numrows = dbPoll(query, dbpTime)
  702.     if(result) then
  703.         local newid = houseid+1
  704.         outputChatBox("Дом "..newid.." успешно создан!", source, 0, 255, 0)
  705.         local weapontable = {}
  706.         weapontable[1] = 0
  707.         weapontable[2] = 0
  708.         weapontable[3] = 0
  709.         buildHouse(newid, x, y, z, int, intx, inty, intz, 0, weapontable, 0, price, "no-one", 0, 0, "no-one", "no-one", "no-one", "no-one", "no-one")
  710.     else
  711.         outputChatBox("При создании дома произошла какая-то ошибка!", source, 255, 0, 0)
  712.         error("Дом "..(houseid+1).." создать не удалось!")
  713.     end
  714. end)
  715.  
  716. -- WITHDRAW WEAPON --
  717.  
  718. addEventHandler("onHouseSystemWeaponWithdraw", getRootElement(), function(id, value)
  719.     local weapons = houseData[id]["WEAPONS"]
  720.     if(gettok(weapons[value], 1, ",")) then
  721.         local weapon, ammo = gettok(weapons[value], 1, ","), gettok(weapons[value], 2, ",")
  722.         giveWeapon(source, weapon, ammo, true)
  723.         outputChatBox("Вы взяли оружие из "..value.." слота!", source, 0, 255, 0)
  724.         weapons[value] = 0
  725.         setHouseData(id, "WEAPONS", weapons)
  726.         setHouseData(id, "WEAP1", weapons[1])
  727.         setHouseData(id, "WEAP2", weapons[2])
  728.         setHouseData(id, "WEAP3", weapons[3])
  729.         triggerClientEvent(source, "onClientHouseSystemMenueUpdate", source, "WEAPON", value, 0)
  730.     end
  731. end)
  732.  
  733. -- DEPOSIT WEAPON --
  734.  
  735.  
  736. addEventHandler("onHouseSystemWeaponDeposit", getRootElement(), function(id, value)
  737.     local weapons = houseData[id]["WEAPONS"]
  738.     if(tonumber(weapons[value]) == 0) then
  739.         local weapon = getPedWeapon(source)
  740.         local ammo = getPedTotalAmmo(source)
  741.         if(weapon) and (ammo) and(weapon ~= 0) and (ammo ~= 0) then
  742.             weapons[value] = weapon..", "..ammo
  743.             takeWeapon(source, weapon)
  744.             outputChatBox("Вы положили оружие "..getWeaponNameFromID(weapon).." в ваш ящик!", source, 0, 255, 0)
  745.             setHouseData(id, "WEAPONS", weapons)
  746.             setHouseData(id, "WEAP1", weapons[1])
  747.             setHouseData(id, "WEAP2", weapons[2])
  748.             setHouseData(id, "WEAP3", weapons[3])
  749.             triggerClientEvent(source, "onClientHouseSystemMenueUpdate", source, "WEAPON", value, weapons[value])
  750.         else
  751.             outputChatBox("У вас нет оружия!", source, 255, 0, 0)
  752.         end
  753.     else
  754.         outputChatBox("В этом слоте уже есть оружие!", source, 255, 0, 0)
  755.     end
  756. end)
  757.  
  758. -- LOCK HOUSE --
  759.  
  760. addEventHandler("onHouseSystemHouseLock", getRootElement(), function(id)
  761.     local state = tonumber(houseData[id]["LOCKED"])
  762.     if(state == 1) then
  763.         setHouseData(id, "LOCKED", 0)
  764.         outputChatBox("Вы открыли дверь в ваш дом!", source, 0, 255, 0)
  765.         triggerClientEvent(source, "onClientHouseSystemMenueUpdate", source, "LOCKED", 0)
  766.     else
  767.         setHouseData(id, "LOCKED", 1)
  768.         outputChatBox("Вы закрыли дверь в ваш дом!", source, 0, 255, 255)
  769.         triggerClientEvent(source, "onClientHouseSystemMenueUpdate", source, "LOCKED", 1)
  770.     end
  771. end)
  772.  
  773. -- DEPOSIT MONEY --
  774.  
  775. addEventHandler("onHouseSystemHouseDeposit", getRootElement(), function(id, value)
  776.     if(value > getPlayerMoney(source)-1) then return end
  777.     setHouseData(id, "MONEY", tonumber(houseData[id]["MONEY"])+value)
  778.     outputChatBox("Вы положили "..value.."$ в ваш ящик!", source, 0, 255, 0)
  779.     triggerClientEvent(source, "onClientHouseSystemMenueUpdate", source, "MONEY", tonumber(houseData[id]["MONEY"]))
  780.     givePlayerMoney(source, -value)
  781. end)
  782.  
  783. -- WITHDRAW MONEY --
  784.  
  785. addEventHandler("onHouseSystemHouseWithdraw", getRootElement(), function(id, value)
  786.     local money = tonumber(houseData[id]["MONEY"])
  787.     if(money < value) then
  788.         outputChatBox("В ящике нет столько денег!", source, 255, 0, 0)
  789.         return
  790.     end
  791.     setHouseData(id, "MONEY", tonumber(houseData[id]["MONEY"])-value)
  792.     outputChatBox("Вы взяли "..value.."$ из вашего ящика!", source, 0, 255, 0)
  793.     triggerClientEvent(source, "onClientHouseSystemMenueUpdate", source, "MONEY", money-value)
  794.     givePlayerMoney(source, value)
  795. end)
  796.  
  797.  
  798. ----------------------------
  799. -- SETTINGS AND FUNCTIONS --
  800. ----------------------------
  801.  
  802.  
  803. -- FADE PLAYERS POSITION --
  804. local fadeP = {}
  805. function setInPosition(thePlayer, x, y, z, interior, typ, dim)
  806.     if not(thePlayer) then return end
  807.     if (getElementType(thePlayer) == "vehicle") then return end
  808.     if(isPedInVehicle(thePlayer)) then return end
  809.     if not(x) or not(y) or not(z) then return end
  810.     if not(interior) then interior = 0 end
  811.     if(fadeP[thePlayer] == 1) then return end
  812.     fadeP[thePlayer] = 1
  813.     fadeCamera(thePlayer, false)
  814.     setElementFrozen(thePlayer, true)
  815.     setTimer(
  816.         function()
  817.         fadeP[thePlayer] = 0
  818.         setElementPosition(thePlayer, x, y, z)
  819.         setElementInterior(thePlayer, interior)
  820.         if(dim) then setElementDimension(thePlayer, dim) end
  821.         fadeCamera(thePlayer, true)
  822.         if not(typ) then
  823.             setElementFrozen(thePlayer, false)
  824.         else
  825.             if(typ == true)  then
  826.                 setTimer(setElementFrozen, 1000, 1, thePlayer, false)
  827.             end
  828.         end
  829.     end, 1000, 1)
  830. end
  831.  
  832.  
  833. -- canAddHouseTenand
  834. -- Checks if there is a free slot in the house
  835.  
  836. function canAddHouseTenand(id)
  837.     if not(houseData[id]) then return false end
  838.     for i = 1, 5, 1 do
  839.         local name = houseData[id]["RENT"..i]
  840.         if(name == "no-one") then
  841.             return true, i
  842.         end
  843.     end
  844.     return false;
  845. end
  846.  
  847. -- addHouseTenand
  848. -- Adds a player to a house as tenand
  849.  
  850. function addHouseTenand(player, id)
  851.     if not(houseData[id]) then return false end
  852.     for i = 1, 5, 1 do
  853.         local name = houseData[id]["RENT"..i]
  854.         if(name == "no-one") then
  855.             setHouseData(id,"RENT"..i, getPlayerName(player))
  856.             return true, i
  857.         end
  858.     end
  859.     return false;
  860. end
  861.  
  862. -- removeHouseTenand
  863. -- Removes a player from a house
  864.  
  865. function removeHouseTenand(id, player)
  866.     if not(houseData[id]) then return false end
  867.     if(type(player) == "string") then
  868.         for i = 1, 5, 1 do
  869.             local name = houseData[id]["RENT"..i]
  870.             if(name == player) then
  871.                 setHouseData(id,"RENT"..i,"no-one")
  872.                 return true
  873.             end
  874.         end
  875.     else
  876.         for i = 1, 5, 1 do
  877.             local name = houseData[id]["RENT"..i]
  878.             if(name == getPlayerName(player)) then
  879.                 setHouseData(id,"RENT"..i,"no-one")
  880.                 return true
  881.             end
  882.         end
  883.     end
  884.     return false;
  885. end
  886.  
  887. -- getHouseTenands(houseid)
  888. -- Returns a table within all tenands in this house
  889.  
  890. function getHouseTenands(id)
  891.     if not(houseData[id]) then return false end
  892.     local rent = {}
  893.     for i = 1, 5, 1 do
  894.         rent[i] = houseData[id]["RENT"..i]
  895.     end
  896.     return rent;
  897. end
  898.  
  899. -- getPlayerRentedHouse
  900. -- Gets the House where a player is rented in --
  901.  
  902. function getPlayerRentedHouse(thePlayer)
  903.     for index, house in pairs(getElementsByType("colshape")) do
  904.         if(getElementData(house, "house") == true) and (getElementData(house, "ID")) then
  905.             local id = tonumber(getElementData(house, "ID"))
  906.             if not(id) then return false end
  907.             local rent = {}
  908.             for i = 1, 5, 1 do
  909.                 rent[i] = houseData[id]["RENT"..i]
  910.             end
  911.             for index, player in pairs(rent) do
  912.                 if(player == getPlayerName(thePlayer)) then
  913.                     return id;
  914.                 end
  915.             end
  916.         end
  917.     end
  918.     return false;
  919. end
  920.  
  921. -- isPlayerRentedHouse
  922. -- Checks if a player is rented in a specific house
  923.  
  924. function isPlayerRentedHouse(thePlayer, id)
  925.     if not(houseData[id]) then return false end
  926.     local rent = {}
  927.     for i = 1, 5, 1 do
  928.         rent[i] = houseData[id]["RENT"..i]
  929.     end
  930.     for index, player in pairs(rent) do
  931.         if(player == getPlayerName(thePlayer)) then
  932.             return true;
  933.         end
  934.     end
  935.     return false;
  936. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement