Advertisement
Guest User

Untitled

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