· 6 years ago · May 31, 2019, 11:02 PM
1local handler
2local dbpTime = 500
3local max_player_houses = 5
4local sellhouse_value = 80
5local open_key = "F9"
6blip = {}
7
8addEvent("onHouseSystemHouseCreate", true)
9addEvent("onHouseSystemHouseLock", true)
10addEvent("onHouseSystemHouseDeposit", true)
11addEvent("onHouseSystemHouseWithdraw", true)
12addEvent("onHouseSystemWeaponDeposit", true)
13addEvent("onHouseSystemWeaponWithdraw", true)
14addEvent("onHouseSystemRentableSwitch", true)
15addEvent("onHouseSystemRentalprice", true)
16addEvent("onHouseSystemTenandRemove", true)
17addEvent("onHouseSystemInfoBuy", true)
18addEvent("onHouseSystemInfoRent", true)
19addEvent("onHouseSystemInfoEnter", true)
20
21local saveableValues = {
22 ["MONEY"] = "MONEY",
23 ["WEAP1"] = "WEAP1",
24 ["WEAP2"] = "WEAP2",
25 ["WEAP3"] = "WEAP3",
26 ["LOCKED"] = "LOCKED",
27 ["OWNER"] = "OWNER",
28 ["DONO"] = "DONO",
29 ["RENTABLE"] = "RENTABLE",
30 ["RENTALPRICE"] = "RENTALPRICE",
31 ["RENT1"] = "RENT1",
32 ["RENT2"] = "RENT2",
33 ["RENT3"] = "RENT3",
34 ["RENT4"] = "RENT4",
35 ["RENT5"] = "RENT5",
36}
37
38local created = false
39local houseid = 0
40
41local house = {}
42local houseData = {}
43local houseInt = {}
44local houseIntData = {}
45
46local buildStartTick
47local buildEndTick
48
49local rentTimer
50
51addEventHandler("onResourceStop", getResourceRootElement(), function()
52 for index, houses in pairs(house) do
53 houses = nil
54 end
55 for index, houseDatas in pairs(houseData) do
56 houseDatas = nil
57 end
58 for index, houseInts in pairs(houseInt) do
59 houseInts = nil
60 end
61 for index, houseIntDatas in pairs(houseIntData) do
62 houseIntDatas = nil
63 end
64 houseid = 0
65 created = false
66end)
67
68--------------
69-- COMMANDS --
70--------------
71-- /unrent --
72addCommandHandler("unrent", function(thePlayer)
73 if(getElementData(thePlayer, "house:lastvisit")) and (getElementData(thePlayer, "house:lastvisit") ~= false) then
74 local id = tonumber(getElementData(thePlayer, "house:lastvisit"))
75 if(isPlayerRentedHouse(thePlayer, id) == false) then
76 outputChatBox("Você não é morador dessa casa!", thePlayer, 255, 0, 0)
77 return
78 end
79 local sucess = removeHouseTenand(id, thePlayer)
80 if(sucess == true) then
81 outputChatBox("Você nao é mais morador dessa casa!", thePlayer, 0, 255, 0)
82 else
83 outputChatBox("Ocorreu um erro!", thePlayer, 255, 0, 0)
84 end
85 end
86end)
87
88-- /rent --
89addCommandHandler("rent", function(thePlayer)
90 if(getElementData(thePlayer, "house:lastvisit")) and (getElementData(thePlayer, "house:lastvisit") ~= false) then
91 local id = tonumber(getElementData(thePlayer, "house:lastvisit"))
92 if isGuestAccount( getPlayerAccount( thePlayer )) then
93 outputChatBox( '* Você precisa estar logado para alugar essa casa!', thePlayer, 255, 51, 36 ) return end
94 if(houseData[id]["DONO"] == getAccountName(getPlayerAccount(thePlayer))) then
95 outputChatBox("Você não pode alugar aqui! É a sua casa!", thePlayer, 255, 0, 0)
96 return
97 end
98 if(tonumber(houseData[id]["RENTABLE"]) ~= 1) then
99 outputChatBox("Essa casa não está em aluguel!", thePlayer, 255, 0, 0)
100 return
101 end
102 --if(getPlayerRentedHouse(thePlayer) ~= false) then
103 -- outputChatBox("Você ja alugou está casa! Use /unrent.", thePlayer, 255, 0, 0)
104 -- return
105 --end
106 local sucess = addHouseTenand(thePlayer, id)
107 if(sucess == true) then
108 outputChatBox("Você é o novo morador dessa casa!", thePlayer, 0, 255, 0)
109 else
110 outputChatBox("Você não pode alugar esta casa!", thePlayer, 255, 0, 0)
111 end
112 end
113end)
114
115addEventHandler( 'onResourceStart', resourceRoot, function()
116handler = dbConnect("sqlite", "Casas.db")
117dbExec( handler, "CREATE TABLE IF NOT EXISTS houses (ID INTEGER PRIMARY KEY AUTOINCREMENT, X REAL, Y REAL, Z REAL, INTERIOR INTEGER( 10 ), INTX REAL, INTY REAL, INTZ REAL, MONEY INTEGER( 20 ), WEAP1 VARCHAR( 45 ), WEAP2 VARCHAR( 45 ), WEAP3 VARCHAR( 45 ), LOCKED INTEGER( 2 ), PRICE INTEGER( 10 ), OWNER VARCHAR( 32 ) , DONO VARCHAR( 32 ), RENTABLE INTEGER( 2 ), RENTALPRICE INTEGER( 10 ), RENT1 VARCHAR( 32 ), RENT2 VARCHAR( 32 ), RENT3 VARCHAR( 32 ), RENT4 VARCHAR( 32 ), RENT5 VARCHAR( 32 ))")
118housesys_startup()
119--[[
120for index, houseDatas in pairs(houseData) do
121local owner = houseDatas["DONO"]
122for i, v in ipairs( getElementsByType( 'player' ) ) do
123if owner == getAccountName( getPlayerAccount(v)) then
124blip[v] = createBlip( 0, 0, 0, 32, 0, 0, 0, 255 )
125setBlipVisibleDistance(blip[v], 7000)
126attachElements (blip[v],houseDatas["PICKUP"])
127setElementVisibleTo ( blip[v], source, true )
128end
129end
130end]]
131end )
132
133-- /createhouse --
134addCommandHandler("casa", function(thePlayer)
135 if(hasObjectPermissionTo ( thePlayer, "command.mute", false ) ) then
136 if(getElementInterior(thePlayer) ~= 0) then
137 outputChatBox("Não é possivel criar casas em interior!", thePlayer, 255, 0, 0)
138 return
139 end
140 if(isPedInVehicle(thePlayer) == true) then
141 outputChatBox("Não é possivel criar casas dentro de veiculos!", thePlayer, 255, 0, 0)
142 return
143 end
144 triggerClientEvent(thePlayer, "onClientHouseSystemGUIStart", thePlayer)
145 else
146 outputChatBox("Você não tem permissão a esse comando!", thePlayer, 255, 0, 0)
147 end
148end)
149
150-- /in --
151addCommandHandler("in", function(thePlayer)
152 if(getElementData(thePlayer, "house:lastvisit")) and (getElementData(thePlayer, "house:lastvisit") ~= false) then
153 local house = getElementData(thePlayer, "house:lastvisit")
154 if(house) then
155 local id = tonumber(house)
156 if(tonumber(houseData[id]["LOCKED"]) == 0) or (houseData[id]["DONO"] == getAccountName( getPlayerAccount(thePlayer))) or (isPlayerRentedHouse(thePlayer, id) == true) then
157 local int, intx, inty, intz, dim = houseIntData[id]["INT"], houseIntData[id]["X"], houseIntData[id]["Y"], houseIntData[id]["Z"], id
158 setElementData(thePlayer, "house:in", true)
159 setInPosition(thePlayer, intx, inty, intz, int, false, dim)
160 unbindKey(thePlayer, open_key, "down", togglePlayerInfomenue, id)
161 outputChatBox ('#00FFFF●#FFFFFF¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯#00FFFF●', thePlayer, 255, 255, 255, true)
162 outputChatBox(" #FFFFFFPressione #00fFFF"..open_key.." #FFFFFFpara abrir as informações da casa.", thePlayer, 0, 255, 255 , true)
163 outputChatBox ('#00FFFF●#FFFFFF____________________________________________________#00FFFF●', thePlayer, 255, 255, 255, true)
164 setElementData(thePlayer, "house:lastvisitINT", id)
165 if(houseData[id]["DONO"] == getAccountName( getPlayerAccount(thePlayer))) or (isPlayerRentedHouse(thePlayer, id) == true) then
166 bindKey(thePlayer, open_key, "down", togglePlayerHousemenue, id)
167 end
168 else
169 outputChatBox("Casa fechada! Você não tem permissão para entrar!", thePlayer, 255, 0, 0)
170 end
171 end
172 end
173end)
174
175-- /out --
176addCommandHandler("out", function(thePlayer)
177 if(getElementData(thePlayer, "house:lastvisitINT")) and (getElementData(thePlayer, "house:lastvisitINT") ~= false) then
178 local house = getElementData(thePlayer, "house:lastvisitINT")
179 if(house) then
180 local id = tonumber(house)
181 local x, y, z = houseData[id]["X"], houseData[id]["Y"], houseData[id]["Z"]
182 setElementData(thePlayer, "house:in", false)
183 setInPosition(thePlayer, x, y, z, 0, false, 0)
184 end
185 end
186end)
187--[[
188addEventHandler( 'onPlayerLogin', root, function()
189 for _, houseDatas in pairs(houseData) do
190 local owner = houseDatas["DONO"]
191 if owner == getAccountName( getPlayerAccount(source)) then
192 blip[source] = createBlip( 0, 0, 0, 32, 0, 0, 0, 255 )
193 setBlipVisibleDistance(blip[source], 7000)
194 attachElements (blip[source],houseDatas["PICKUP"])
195 setElementVisibleTo ( blip[source], source, true )
196
197addEventHandler( 'onPlayerLogout', source, function()
198 for _, house2 in pairs(houseData) do
199 local pick = house2["PICKUP"]
200 for _, elem in ipairs( getAttachedElements( pick ) ) do
201 if getElementType( elem ) == 'blip' then
202 destroyElement( elem )
203 --elem = nil
204 end
205 end
206 end
207end)
208
209addEventHandler( 'onPlayerQuit', source, function()
210 for _, house2 in pairs(houseData) do
211 local pick = house2["PICKUP"]
212 for _, elem in ipairs( getAttachedElements( pick ) ) do
213 if getElementType( elem ) == 'blip' then
214 destroyElement( elem )
215 --elem = nil
216 end
217 end
218 end
219end)
220
221end
222end
223end)]]
224
225function destroyblip ()
226 for _, house2 in pairs(houseData) do
227 local owner = house2["DONO"]
228 local pick = house2["PICKUP"]
229 if getAccountName( getPlayerAccount(source)) == owner then
230 for k, elem in ipairs( getAttachedElements( pick ) ) do
231 if getElementType( elem ) == 'blip' then
232 destroyElement( elem )
233 elem = nil
234 end
235 end
236 end
237end
238end
239--addEventHandler("onPlayerQuit", root, destroyblip)
240--addEventHandler("onPlayerLogout", root, destroyblip)
241
242-- /buyhouse --
243addCommandHandler("buyhouse", function(thePlayer)
244 if(getElementData(thePlayer, "house:lastvisit")) and (getElementData(thePlayer, "house:lastvisit") ~= false) then
245 local house = getElementData(thePlayer, "house:lastvisit")
246 if(house) then
247 if isGuestAccount( getPlayerAccount( thePlayer )) then
248 outputChatBox( '* Você precisa estar logado para comprar essa casa!', thePlayer, 255, 51, 36 ) return end
249 local id = house
250 local owner = houseData[id]["OWNER"]
251 local pick = houseData[id]["PICKUP"]
252 if(owner ~= "Ninguém") then
253 outputChatBox("Você não pode comprar esta casa!", thePlayer, 255, 0, 0)
254 else
255 local houses = 0
256 for index, col in pairs(getElementsByType("colshape")) do
257 if(getElementData(col, "house") == true) and (houseData[getElementData(col, "ID")]["DONO"] == getAccountName( getPlayerAccount(thePlayer))) then
258 houses = houses+1
259 if(houses == max_player_houses) then
260 outputChatBox("Você já tem "..max_player_houses.." casas! Venda uma para comprar essa casa!", thePlayer, 255, 0, 0)
261 return
262 end
263 end
264 end
265 local money = getPlayerMoney(thePlayer)
266 local price = houseData[id]["PRICE"]
267 if(money < price) then outputChatBox("Você tem dinheiro suficiente! Você precisa de $"..(price-money).."!", thePlayer, 255, 0, 0) return end
268 setHouseData(id, "OWNER", getPlayerName(thePlayer))
269 setHouseData(id, "DONO", getAccountName( getPlayerAccount(thePlayer)))
270 givePlayerMoney(thePlayer, -price)
271 setHouseData(id, "RENTABLE", 0)
272 outputChatBox("Parabéns! Você comprou a casa!", thePlayer, 0, 255, 0)
273
274 --setElementModel(houseData[id]["PICKUP"], 1272)
275 --setElementModel(houseData[id]["BLIP"], 32)
276 --destroyElement(houseData[id]["BLIP"])
277
278 destroyElement(houseData[id]["PICKUP"])
279 x = houseData[id]["X"]
280 y = houseData[id]["Y"]
281 z = houseData[id]["Z"]
282 houseData[id]["PICKUP"] = createPickup(x, y, z-0.5, 3, 1272, 100)
283 --houseData[id]["BLIP"] = createBlip(x, y, z, 32, 2, 255, 0, 0, 255, 0, 50)
284
285 --blip[id] = createBlip( 0, 0, 0, 32, 0, 0, 0, 255 )
286 --setBlipVisibleDistance(blip[id], 7000)
287 --attachElements (blip[id], houseData[id]["PICKUP"])
288 --setElementVisibleTo ( blip[id], thePlayer, true )
289
290 end
291 end
292 end
293end)
294
295-- /sellhouse --
296addCommandHandler("sellhouse", function(thePlayer)
297 if(getElementData(thePlayer, "house:lastvisit")) and (getElementData(thePlayer, "house:lastvisit") ~= false) then
298 local house = getElementData(thePlayer, "house:lastvisit")
299 if(house) then
300 if isGuestAccount( getPlayerAccount( thePlayer )) then
301 outputChatBox( '* Você precisa estar logado para entrar em casa!', thePlayer, 255, 51, 36 ) return end
302 local id = house
303 local owner = houseData[id]["DONO"]
304 if(owner ~= getAccountName( getPlayerAccount(thePlayer))) then
305 outputChatBox("Você não pode vender esta casa!", thePlayer, 255, 0, 0)
306 else
307 local price = houseData[id]["PRICE"]
308 setHouseData(id, "OWNER", "Ninguém")
309 setHouseData(id, "RENTABLE", 0)
310 setHouseData(id, "RENTALPRICE", 0)
311 setHouseData(id, "DONO", "none")
312 for i = 1, 5, 1 do
313 setHouseData(id, "RENT"..i, "Ninguém")
314 end
315 givePlayerMoney(thePlayer, math.floor(price/100*sellhouse_value))
316 outputChatBox("Você vendeu a casa com sucesso e recebeu $"..math.floor(price/100*sellhouse_value).." devolta!", thePlayer, 0, 255, 0)
317
318 --setElementModel(houseData[id]["PICKUP"], 1273)
319 --setElementModel(houseData[id]["BLIP"], 31)
320 --destroyElement(houseData[id]["BLIP"])
321
322 destroyElement(houseData[id]["PICKUP"])
323 x = houseData[id]["X"]
324 y = houseData[id]["Y"]
325 z = houseData[id]["Z"]
326 houseData[id]["PICKUP"] = createPickup(x, y, z-0.5, 3, 1273, 100)
327 --houseData[id]["BLIP"] = createBlip(x, y, z, 31, 2, 255, 0, 0, 255, 0, 50)
328
329 --if blip[id] and isElement(blip[id]) then
330 --destroyElement(blip[id])
331 --blip[id] = nil
332 --end
333
334 end
335 end
336 end
337end)
338
339-- /deletehouse --
340addCommandHandler("deletehouse", function(thePlayer, cmd, id)
341 if(hasObjectPermissionTo ( thePlayer, "command.mute", false ) ) then
342 id = tonumber(id)
343 if not(id) then return end
344 if not(house[id]) then
345 outputChatBox("Não há casa com o ID "..id.."!", thePlayer, 255, 0, 0)
346 return
347 end
348 local query = dbQuery(handler, "DELETE FROM houses WHERE ID = '"..id.."';")
349 local result = dbPoll(query, dbpTime)
350 if(result) then
351 --destroyElement(houseData[id]["BLIP"])
352 destroyElement(houseData[id]["PICKUP"])
353 destroyElement(houseIntData[id]["PICKUP"])
354 houseData[id] = nil
355 houseIntData[id] = nil
356 destroyElement(house[id])
357 destroyElement(houseInt[id])
358 outputChatBox("Casa "..id.." destruída com sucesso!", thePlayer, 0, 255, 0)
359 house[id] = false
360
361 --if blip[id] and isElement(blip[id]) then
362 --destroyElement(blip[id])
363 --blip[id] = nil
364 --end
365
366 else
367 --error("House ID "..id.." has been created Ingame, but House is not in the database! WTF")
368 end
369 else
370 outputChatBox("Você não tem permissão a esse comando!", thePlayer, 255, 0, 0)
371 end
372end)
373
374--------------------
375-- BIND FUNCTIONS --
376--------------------
377function togglePlayerInfomenue(thePlayer, key, state, id)
378 if (id) then
379 local locked = houseData[id]["LOCKED"]
380 local rentable = houseData[id]["RENTABLE"]
381 local rentalprice = houseData[id]["RENTALPRICE"]
382 local owner = houseData[id]["OWNER"]
383 local dono = houseData[id]["DONO"]
384 local price = houseData[id]["PRICE"]
385 local x, y, z = getElementPosition(house[id])
386 local house = getPlayerRentedHouse(thePlayer)
387 if(house ~= false) then house = true end
388 local isrentedin = isPlayerRentedHouse(thePlayer, id)
389 triggerClientEvent(thePlayer, "onClientHouseSystemInfoMenueOpen", thePlayer, dono, owner, x, y, z, price, locked, rentable, rentalprice, id, house, isrentedin)
390 end
391end
392
393function togglePlayerHousemenue(thePlayer, key, state, id)
394 if(id) then
395 if(getElementInterior(thePlayer) ~= 0) then
396 local locked = houseData[id]["LOCKED"]
397 local money = houseData[id]["MONEY"]
398 local weap1 = houseData[id]["WEAPONS"][1]
399 local weap2 = houseData[id]["WEAPONS"][2]
400 local weap3 = houseData[id]["WEAPONS"][3]
401 local rentable = houseData[id]["RENTABLE"]
402 local rent = houseData[id]["RENTALPRICE"]
403 local tenands = getHouseTenands(id)
404 local owner = false
405 if(getAccountName( getPlayerAccount(thePlayer)) == houseData[id]["DONO"]) then
406 owner = true
407 end
408 local canadd = canAddHouseTenand(id)
409 triggerClientEvent(thePlayer, "onClientHouseSystemMenueOpen", thePlayer, owner, locked, money, weap1, weap2, weap3, id, rentable, rent, tenands, canadd)
410 end
411 else
412 triggerClientEvent(thePlayer, "onClientHouseSystemMenueOpen", thePlayer )
413 end
414end
415
416-------------------------------
417-- HOUSE CREATION ON STARTUP --
418-------------------------------
419addEventHandler("onPlayerLogin", getRootElement(),
420 function()
421 local accountName = getAccountName(getPlayerAccount(source))
422 setElementData(source, "account-name", accountName)
423 end
424)
425
426addEventHandler("onPlayerLogout", getRootElement(),
427 function()
428 setElementData(source, "account-name", "Não logado")
429 end
430)
431
432-- BUILDHOUSE FUNCTION --
433local function buildHouse(id, x, y, z, interior, intx, inty, intz, money, weapons, locked, price, owner, dono, rentable, rentalprice, rent1, rent2, rent3, rent4, rent5)
434 if(id) and (x) and(y) and (z) and (interior) and (intx) and (inty) and (intz) and (money) and (weapons) and (owner) then
435 houseid = id
436 house[id] = createColSphere(x, y, z, 2) -- This is the house, hell yeah
437 houseData[id] = {}
438 local house = house[id] -- I'm too lazy...
439 setElementData(house, "house", true) -- Just for client code only
440
441 local houseIntPickup = createPickup(intx, inty, intz, 3, 1318, 100)
442 setElementInterior(houseIntPickup, interior)
443 setElementDimension(houseIntPickup, id)
444
445 houseInt[id] = createColSphere(intx, inty, intz, 1.5) -- And this is the Exit
446 setElementInterior(houseInt[id], interior)
447 setElementDimension(houseInt[id], id) -- The House Dimension is the house ID
448 setElementData(houseInt[id], "house", false)
449 --------------------
450 -- EVENT HANDLERS --
451 --------------------
452 -- IN --
453 addEventHandler("onColShapeHit", house, function(hitElement)
454 if(getElementType(hitElement) == "player") then
455 setElementData(hitElement, "house:lastvisit", id)
456 bindKey(hitElement, open_key, "down", togglePlayerInfomenue, id)
457 outputChatBox ('#00FFFF●#FFFFFF¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯#00FFFF●', hitElement, 255, 255, 255, true)
458 outputChatBox(" #FFFFFFPressione #00fFFF"..open_key.."#FFFFFF para abrir as informações desta casa.", hitElement, 0, 255, 255, true)
459 outputChatBox ('#00FFFF●#FFFFFF____________________________________________________#00FFFF●', hitElement, 255, 255, 255, true)
460 end
461 end)
462
463 addEventHandler("onColShapeLeave", house, function(hitElement)
464 if(getElementType(hitElement) == "player") then
465 setElementData(hitElement, "house:lastvisit", false)
466 unbindKey(hitElement, open_key, "down", togglePlayerInfomenue, id)
467 --outputChatBox(id)
468 end
469 end)
470
471 -- OUT --
472 addEventHandler("onColShapeHit", houseInt[id], function(hitElement, dim)
473 if(dim == true) then
474 if(getElementType(hitElement) == "player") then
475 unbindKey(hitElement, open_key, "down", togglePlayerInfomenue, id)
476 setElementData(hitElement, "house:lastvisitINT", id)
477 if(houseData[id]["DONO"] == getAccountName( getPlayerAccount(hitElement))) or (isPlayerRentedHouse(hitElement, id) == true) then
478 bindKey(hitElement, open_key, "down", togglePlayerHousemenue, id)
479 end
480 --outputChatBox(id)
481 end
482 end
483 end)
484
485 --[[addEventHandler("onColShapeLeave", houseInt[id], function(hitElement, dim)
486 if(dim == true) then
487 if(getElementType(hitElement) == "player") then
488 setElementData(hitElement, "house:lastvisitINT", false)
489 if(houseData[id]["OWNER"] == getPlayerName(hitElement)) or (isPlayerRentedHouse(hitElement, id) == true) then
490 unbindKey(hitElement, open_key, "down", togglePlayerHousemenue, id)
491 end
492 --outputChatBox(id)
493 end
494 end
495 end)]]
496
497 -- Set data for HOUSE --
498 houseData[id]["HOUSE"] = house
499 houseData[id]["DIM"] = id
500 houseData[id]["MONEY"] = money
501 houseData[id]["WEAPONS"] = weapons
502 houseData[id]["INTHOUSE"] = houseInt[id]
503 houseData[id]["LOCKED"] = locked
504 houseData[id]["PRICE"] = price
505 houseData[id]["OWNER"] = owner
506 houseData[id]["DONO"] = dono
507 houseData[id]["X"] = x
508 houseData[id]["Y"] = y
509 houseData[id]["Z"] = z
510 houseData[id]["RENTABLE"] = rentable
511 houseData[id]["RENTALPRICE"] = rentalprice
512 houseData[id]["RENT1"] = rent1
513 houseData[id]["RENT2"] = rent2
514 houseData[id]["RENT3"] = rent3
515 houseData[id]["RENT4"] = rent4
516 houseData[id]["RENT5"] = rent5
517
518 -- HOUSE PICKUP --
519 local housePickup
520 if(owner ~= "Ninguém") then
521 housePickup = createPickup(x, y, z-0.5, 3, 1272, 100)
522 else
523 housePickup = createPickup(x, y, z-0.5, 3, 1273, 100)
524 end
525 -- HOUSE BLIP --
526 local houseBlip
527 if(owner ~= "Ninguém") then
528 -- houseBlip = createBlip(x, y, z, 32, 2, 255, 0, 0, 255, 0, 50)
529 else
530 -- houseBlip = createBlip(x, y, z, 31, 2, 255, 0, 0, 255, 0, 50)
531 end
532
533 -- SET THE DATA --
534 houseData[id]["PICKUP"] = housePickup
535 houseData[id]["BLIP"] = houseBlip
536
537 setElementData(house, "PRICE", price)
538 setElementData(house, "OWNER", owner)
539 setElementData(house, "DONO", dono)
540 setElementData(house, "LOCKED", locked)
541 setElementData(house, "ID", id)
542 setElementData(house, "RENTABLE", rentable)
543 setElementData(house, "RENTALPRICE", rentalprice)
544
545 -- SET DATA FOR HOUSEINTERIOR --
546 houseIntData[id] = {}
547 houseIntData[id]["OUTHOUSE"] = houseData[id]["HOUSE"]
548 houseIntData[id]["INT"] = interior
549 houseIntData[id]["X"] = intx
550 houseIntData[id]["Y"] = inty
551 houseIntData[id]["Z"] = intz
552 houseIntData[id]["PICKUP"] = houseIntPickup
553 outputServerLog("House with ID "..id.." created sucessfully!")
554 buildEndTick = getTickCount()
555
556 -- TRIGGER TO ALL CLIENTS THAT THE HOUSE HAS BEEN CREATEEEEEEEEEEEEEEEEEEEEEEED --
557 setTimer(triggerClientEvent, 1000, 1, "onClientHouseSystemColshapeAdd", getRootElement(), house)
558
559 addEventHandler( 'onPickupUse', houseIntPickup, function( player )
560 if getElementType( player ) == 'player' then
561 toggleAllControls( player, false )
562 fadeCamera( player, false )
563 setTimer( function( player )
564 if getPedOccupiedVehicle( player ) then
565 removePedFromVehicle( player )
566 end
567 local house = getElementData(player, "house:lastvisitINT")
568 if(house) then
569 local idd = tonumber(house)
570 local x, y, z = houseData[idd]["X"], houseData[idd]["Y"], houseData[idd]["Z"]
571 setElementData(player, "house:in", false)
572 --setInPosition(player, x, y, z+1, 0, false, 0)
573 toggleAllControls( player, true )
574 setElementPosition( player, x, y, z )
575 setElementInterior( player, 0 )
576 setElementDimension( player, 0 )
577 end
578 if(getElementType(player) == "player") then
579 setElementData(player, "house:lastvisitINT", false)
580 if(houseData[id]["DONO"] == getAccountName( getPlayerAccount(player))) or (isPlayerRentedHouse(player, id) == true) then
581 unbindKey(player, open_key, "down", togglePlayerHousemenue, id)
582 end
583 end
584 fadeCamera( player, true )
585 end, 1500, 1, player, source )
586 end
587 end )
588
589 else
590 if not(id) then
591 error("Arguments @buildHouse not valid! There is no Houseid!")
592 elseif not(x) then
593 error("Arguments @buildHouse not valid! Houseid = "..id.." Argument "..x)
594 elseif not(y) then
595 error("Arguments @buildHouse not valid! Houseid = "..id.." Argument "..y)
596 elseif not(z) then
597 error("Arguments @buildHouse not valid! Houseid = "..id.." Argument "..z)
598 elseif not(interior) then
599 error("Arguments @buildHouse not valid! Houseid = "..id.." Argument "..interior)
600 elseif not(intx) then
601 error("Arguments @buildHouse not valid! Houseid = "..id.." Argument "..intx)
602 elseif not(inty) then
603 error("Arguments @buildHouse not valid! Houseid = "..id.." Argument "..inty)
604 elseif not(intz) then
605 error("Arguments @buildHouse not valid! Houseid = "..id.." Argument "..intz)
606 elseif not(money) then
607 error("Arguments @buildHouse not valid! Houseid = "..id.." Argument "..money)
608 elseif not(weapons) then
609 error("Arguments @buildHouse not valid! Houseid = "..id.." Argument "..weapons)
610 elseif not(owner) then
611 error("Arguments @buildHouse not valid! Houseid = "..id.." Argument "..owner)
612 end
613 end
614end
615
616-- TAKE PLAYER RENT --
617local function takePlayerRent()
618 for index, player in pairs(getElementsByType("player")) do
619 if(getPlayerRentedHouse(player) ~= false) then
620 local id = getPlayerRentedHouse(player)
621 local owner = houseData[id]["OWNER"]
622 local rentable = tonumber(houseData[id]["RENTABLE"])
623 if(rentable == 1) then
624 local rentprice = tonumber(houseData[id]["RENTALPRICE"])
625 takePlayerMoney(player, rentprice) -- Takes the player money for the rent
626 outputChatBox("Você pagou $"..rentprice.." preço do aluguel!", player, 255, 255, 0, true)
627 if(getPlayerFromName(owner)) then
628 givePlayerMoney(getPlayerFromName(owner), rentprice) -- Gives the owner the rentalprice
629 outputChatBox("Você recebeu $"..rentprice.." dos moradores da sua casa!", getPlayerFromName(owner), 255, 255, 0, true)
630 end
631 end
632 end
633 end
634end
635
636-- HOUSE DATABASE EXECUTION --
637function housesys_startup()
638 if(created == true) then
639 error("Casas ja foram criadas!")
640 return
641 end
642 buildStartTick = getTickCount()
643 local query = dbQuery(handler, "SELECT * FROM houses;" )
644 local result, numrows = dbPoll(query, dbpTime)
645 if (result and numrows > 0) then
646 for index, row in pairs(result) do
647 local id = row['ID']
648 local x, y, z = row['X'], row['Y'], row['Z']
649 local int, intx, inty, intz = row['INTERIOR'], row['INTX'], row['INTY'], row['INTZ']
650 local money, weap1, weap2, weap3 = row['MONEY'] or 0, row['WEAP1'] or 0, row['WEAP2'] or 0, row['WEAP3'] or 0
651 local locked = row['LOCKED'] or 0
652 local price = row['PRICE'] or 0
653 local owner = row['OWNER'] or "Ninguém"
654 local dono = row['DONO'] or "none"
655 local rentable = row['RENTABLE'] or 0
656 local rentalprice = row['RENTALPRICE'] or 100
657 local rent1, rent2, rent3, rent4, rent5 = row['RENT1'] or "Ninguém" ,row['RENT2'] or "Ninguém" , row['RENT3'] or "Ninguém" , row['RENT4'] or "Ninguém" , row['RENT5'] or "Ninguém"
658 local weapontable = {}
659 weapontable[1] = weap1
660 weapontable[2] = weap2
661 weapontable[3] = weap3
662 buildHouse(id, x, y, z, int, intx, inty, intz, money, weapontable, locked, price, owner, dono, rentable, rentalprice, rent1, rent2, rent3, rent4, rent5)
663 end
664 dbFree(query)
665 else
666 error("Houses Table not Found/empty!")
667 end
668 created = true
669 setTimer(function()
670 local elapsed = (buildEndTick-buildStartTick)
671 outputServerLog("It took "..(elapsed/1000).." seconds to build all houses.")
672 end, 1000, 1)
673 rentTimer = setTimer(takePlayerRent, 3600000, 0)
674end
675
676-- House Data array set --
677function setHouseData(ID, typ, value)
678 -- Security array --
679 houseData[ID][typ] = value
680 setElementData(house[ID], typ, value)
681 if(saveableValues[typ]) then
682 local query = dbQuery(handler, "UPDATE houses SET "..saveableValues[typ].." = '"..value.."' WHERE ID = '"..ID.."';" )
683 local result = dbPoll(query, dbpTime)
684 if(result) then
685 dbFree(query)
686 else
687 error("Can't save Data: "..typ.." with the value: "..value.." for house ID "..ID.."!")
688 end
689 end
690end
691
692--------------------
693-- EVENT HANDLERS --
694--------------------
695-- INFO RENT --
696addEventHandler("onHouseSystemInfoRent", getRootElement(), function(id, value)
697 if(houseData[id]) then
698 if(value == true) then
699 executeCommandHandler("rent", source)
700 else
701 executeCommandHandler("unrent", source)
702 end
703 end
704end)
705
706-- INFO ENTER --
707addEventHandler("onHouseSystemInfoEnter", getRootElement(), function(id)
708 if(houseData[id]) then
709 executeCommandHandler("in", source)
710 end
711end)
712
713-- INFO BUY --
714addEventHandler("onHouseSystemInfoBuy", getRootElement(), function(id, value)
715 if(houseData[id]) then
716 if(value == true) then
717 executeCommandHandler("buyhouse", source)
718 else
719 executeCommandHandler("sellhouse", source)
720 end
721 end
722end)
723
724-- TENAND REMOVE --
725addEventHandler("onHouseSystemTenandRemove", getRootElement(), function(id, value)
726 if(houseData[id]) then
727 local sucess = removeHouseTenand(id, value)
728 if(sucess == true) then
729 outputChatBox("Você removeu com sucesso o morador "..value.."!", source, 0, 255, 0, true)
730 triggerClientEvent(source, "onClientHouseSystemMenueUpdate", source, "TENANDS", getHouseTenands(id))
731 end
732 end
733end)
734
735-- SET RENTALPRICE --
736addEventHandler("onHouseSystemRentalprice", getRootElement(), function(id, value)
737 if(houseData[id]) then
738 local oldvalue = tonumber(houseData[id]["RENTALPRICE"])
739 if(oldvalue < value) then
740 local tenands = getHouseTenands(id)
741 local users = {}
742 for i = 1, 5, 1 do
743 if(tenands[i] ~= "Ninguém") then
744 users[i] = tenands[i]
745 end
746 end
747 if(#users > 0) then
748 outputChatBox("Você não pode mudar o preço de aluguel para um valor mais alto, porque há moradores em sua casa!", source, 255, 0, 0)
749 return
750 end
751 end
752 setHouseData(id, "RENTALPRICE", value)
753 outputChatBox("Você configurou com êxito o preço de aluguel para $"..value.."!", source, 0, 255, 0)
754 triggerClientEvent(source, "onClientHouseSystemMenueUpdate", source, "RENTALPRICE", value)
755 end
756end)
757
758-- RENTABLE SWITCH --
759addEventHandler("onHouseSystemRentableSwitch", getRootElement(), function(id)
760 if(houseData[id]) then
761 local state = tonumber(houseData[id]["RENTABLE"])
762 if(state == 0) then
763 setHouseData(id, "RENTABLE", 1)
764 triggerClientEvent(source, "onClientHouseSystemMenueUpdate", source, "RENTABLE", true)
765 outputChatBox("A casa está alugavel!", source, 0, 255, 0)
766 else
767 setHouseData(id, "RENTABLE", 0)
768 triggerClientEvent(source, "onClientHouseSystemMenueUpdate", source, "RENTABLE", false)
769 outputChatBox("A casa não está mais alugavel!", source, 0, 255, 0)
770 end
771 end
772end)
773
774-- CREATE HOUSE --
775addEventHandler("onHouseSystemHouseCreate", getRootElement(), function(x, y, z, int, intx, inty, intz, price)
776 local query = dbQuery(handler, "INSERT INTO houses (X, Y, Z, INTERIOR, INTX, INTY, INTZ, PRICE) values ('"..x.."', '"..y.."', '"..z.."', '"..int.."', '"..intx.."', '"..inty.."', '"..intz.."', '"..price.."');")
777 local result, numrows = dbPoll(query, dbpTime)
778 if(result) then
779 local newid = houseid+1
780 outputChatBox("Casa "..newid.." criada com sucesso!", source, 0, 255, 0)
781 local weapontable = {}
782 weapontable[1] = 0
783 weapontable[2] = 0
784 weapontable[3] = 0
785 buildHouse(newid, x, y, z, int, intx, inty, intz, 0, weapontable, 0, price, "Ninguém", "none", 0, 0, "Ninguém", "Ninguém", "Ninguém", "Ninguém", "Ninguém")
786 else
787 outputChatBox("Ocorreu um erro durante a criação da casa!", source, 255, 0, 0)
788 error("House "..(houseid+1).." could not create!")
789 end
790end)
791
792-- WITHDRAW WEAPON --
793addEventHandler("onHouseSystemWeaponWithdraw", getRootElement(), function(id, value)
794 local weapons = houseData[id]["WEAPONS"]
795 if(gettok(weapons[value], 1, ",")) then
796 local weapon, ammo = gettok(weapons[value], 1, ","), gettok(weapons[value], 2, ",")
797 giveWeapon(source, weapon, ammo, true)
798 outputChatBox("Você retirou com sucesso a arma do slot "..value.."!", source, 0, 255, 0)
799 weapons[value] = 0
800 setHouseData(id, "WEAPONS", weapons)
801 setHouseData(id, "WEAP1", weapons[1])
802 setHouseData(id, "WEAP2", weapons[2])
803 setHouseData(id, "WEAP3", weapons[3])
804 triggerClientEvent(source, "onClientHouseSystemMenueUpdate", source, "WEAPON", value, 0)
805 end
806end)
807
808-- DEPOSIT WEAPON --
809addEventHandler("onHouseSystemWeaponDeposit", getRootElement(), function(id, value)
810 local weapons = houseData[id]["WEAPONS"]
811 if((weapons[value]) ~= 0) then
812 outputChatBox("O slot de arma "..value..", ja tem uma arma guardada!", source, 255, 0, 0)
813 return end
814 local weapon = getPedWeapon(source)
815 local ammo = getPedTotalAmmo(source)
816 if(weapon) and (ammo) and(weapon ~= 0) and (ammo ~= 0) then
817 weapons[value] = weapon..", "..ammo
818 takeWeapon(source, weapon)
819 outputChatBox("Você guardou com sucesso a sua arma "..getWeaponNameFromID(weapon).." em sua caixa de arma!", source, 0, 255, 0)
820 setHouseData(id, "WEAPONS", weapons)
821 setHouseData(id, "WEAP1", weapons[1])
822 setHouseData(id, "WEAP2", weapons[2])
823 setHouseData(id, "WEAP3", weapons[3])
824 triggerClientEvent(source, "onClientHouseSystemMenueUpdate", source, "WEAPON", value, weapons[value])
825 --else
826 --outputChatBox("You don't have a weapon!", source, 255, 0, 0)
827 else
828 outputChatBox("Você precisa estar segurando uma arma!", source, 255, 0, 0)
829 end
830end)
831
832-- LOCK HOUSE --
833addEventHandler("onHouseSystemHouseLock", getRootElement(), function(id)
834 local state = tonumber(houseData[id]["LOCKED"])
835 if(state == 1) then
836 setHouseData(id, "LOCKED", 0)
837 outputChatBox("A casa foi destrancada.", source, 0, 255, 0)
838 triggerClientEvent(source, "onClientHouseSystemMenueUpdate", source, "LOCKED", 0)
839 else
840 setHouseData(id, "LOCKED", 1)
841 outputChatBox("A casa foi trancada!", source, 0, 255, 255)
842 triggerClientEvent(source, "onClientHouseSystemMenueUpdate", source, "LOCKED", 1)
843 end
844end)
845
846-- DEPOSIT MONEY --
847addEventHandler("onHouseSystemHouseDeposit", getRootElement(), function(id, value)
848 if(value > getPlayerMoney(source)-1) then return end
849 setHouseData(id, "MONEY", tonumber(houseData[id]["MONEY"])+value)
850 outputChatBox("Você depositou "..value.."$ no cofre da sua casa!", source, 0, 255, 0)
851 triggerClientEvent(source, "onClientHouseSystemMenueUpdate", source, "MONEY", tonumber(houseData[id]["MONEY"]))
852 givePlayerMoney(source, -value)
853end)
854
855-- WITHDRAW MONEY --
856addEventHandler("onHouseSystemHouseWithdraw", getRootElement(), function(id, value)
857 local money = tonumber(houseData[id]["MONEY"])
858 if(money < value) then
859 outputChatBox("Você não "..value.." em seu cofre!", source, 255, 0, 0)
860 return
861 end
862 setHouseData(id, "MONEY", tonumber(houseData[id]["MONEY"])-value)
863 outputChatBox("Você sacou "..value.."$ do seu cofre!", source, 0, 255, 0)
864 triggerClientEvent(source, "onClientHouseSystemMenueUpdate", source, "MONEY", money-value)
865 givePlayerMoney(source, value)
866end)
867
868----------------------------
869-- SETTINGS AND FUNCTIONS --
870----------------------------
871
872-- FADE PLAYERS POSITION --
873local fadeP = {}
874function setInPosition(thePlayer, x, y, z, interior, typ, dim)
875 if not(thePlayer) then return end
876 if (getElementType(thePlayer) == "vehicle") then return end
877 if(isPedInVehicle(thePlayer)) then return end
878 if not(x) or not(y) or not(z) then return end
879 if not(interior) then interior = 0 end
880 if(fadeP[thePlayer] == 1) then return end
881 fadeP[thePlayer] = 1
882 fadeCamera(thePlayer, false)
883 setElementFrozen(thePlayer, true)
884 setTimer(
885 function()
886 fadeP[thePlayer] = 0
887 setElementPosition(thePlayer, x, y, z)
888 setElementInterior(thePlayer, interior)
889 if(dim) then setElementDimension(thePlayer, dim) end
890 fadeCamera(thePlayer, true)
891 if not(typ) then
892 setElementFrozen(thePlayer, false)
893 else
894 if(typ == true) then
895 setTimer(setElementFrozen, 1000, 1, thePlayer, false)
896 end
897 end
898 end, 1000, 1)
899end
900
901-- canAddHouseTenand
902-- Checks if there is a free slot in the house
903function canAddHouseTenand(id)
904 if not(houseData[id]) then return false end
905 for i = 1, 5, 1 do
906 local name = houseData[id]["RENT"..i]
907 if(name == "Ninguém") then
908 return true, i
909 end
910 end
911 return false;
912end
913
914-- addHouseTenand
915-- Adds a player to a house as tenand
916function addHouseTenand(player, id)
917 if not(houseData[id]) then return false end
918 for i = 1, 5, 1 do
919 local name = houseData[id]["RENT"..i]
920 if(name == "Ninguém") then
921 setHouseData(id,"RENT"..i, getAccountName( getPlayerAccount(player)))
922 return true, i
923 end
924 end
925 return false;
926end
927
928-- removeHouseTenand
929-- Removes a player from a house
930function removeHouseTenand(id, player)
931 if not(houseData[id]) then return false end
932 if(type(player) == "string") then
933 for i = 1, 5, 1 do
934 local name = houseData[id]["RENT"..i]
935 if(name == player) then
936 setHouseData(id,"RENT"..i,"Ninguém")
937 return true
938 end
939 end
940 else
941 for i = 1, 5, 1 do
942 local name = houseData[id]["RENT"..i]
943 if(name == getAccountName( getPlayerAccount(player))) then
944 setHouseData(id,"RENT"..i,"Ninguém")
945 return true
946 end
947 end
948 end
949 return false;
950end
951
952-- getHouseTenands(houseid)
953-- Returns a table within all tenands in this house
954function getHouseTenands(id)
955 if not(houseData[id]) then return false end
956 local rent = {}
957 for i = 1, 5, 1 do
958 rent[i] = houseData[id]["RENT"..i]
959 end
960 return rent;
961end
962
963-- getPlayerRentedHouse
964-- Gets the House where a player is rented in --
965function getPlayerRentedHouse(thePlayer)
966 for index, house in pairs(getElementsByType("colshape")) do
967 if(getElementData(house, "house") == true) and (getElementData(house, "ID")) then
968 local id = tonumber(getElementData(house, "ID"))
969 if not(id) then return false end
970 local rent = {}
971 for i = 1, 5, 1 do
972 rent[i] = houseData[id]["RENT"..i]
973 end
974 for index, player in pairs(rent) do
975 if(player == getAccountName( getPlayerAccount(thePlayer))) then
976 return id;
977 end
978 end
979 end
980 end
981 return false;
982end
983
984-- isPlayerRentedHouse
985-- Checks if a player is rented in a specific house
986function isPlayerRentedHouse(thePlayer, id)
987 if not(houseData[id]) then return false end
988 local rent = {}
989 for i = 1, 5, 1 do
990 rent[i] = houseData[id]["RENT"..i]
991 end
992 for index, player in pairs(rent) do
993 if(player == getAccountName( getPlayerAccount(thePlayer))) then
994 return true;
995 end
996 end
997 return false;
998end