· 8 years ago · May 14, 2018, 10:04 PM
1addEventHandler("onResourceStart", resourceRoot, function()
2 polaczenie = dbConnect( "mysql", "dbname=DesorveRPG_MTA;host=127.0.0.1", "root", "JebacKosta", "share=1")
3end)
4
5
6addEventHandler("onResourceStart",resourceRoot,function()
7 local create_table = dbQuery ( polaczenie, "CREATE TABLE IF NOT EXISTS cars (id INT, model INT, owner TEXT, color1 INT, color2 INT, color3 INT, color4 INT, color5 INT, color6 INT, posx FLOAT, posy FLOAT, posz FLOAT, rotz FLOAT)")
8 if create_table then
9 outputDebugString("Utworzono tabele")
10 else
11 outputDebugString("Nie utworzono tabeli")
12 end
13end)
14
15local cars = {}
16
17function carCreate(plr,cmd,id,player)
18 if id and player then
19 id = tonumber(id)
20 if id then
21 local x,y,z = getElementPosition(plr)
22 local pojazd = createVehicle(id,x,y,z,0,0,0)
23 if not pojazd then
24 outputChatBox("Nie udało się utworzyć pojazdu!",plr) return end
25 table.insert(cars,pojazd)
26 setElementData(pojazd,"veh:id",#cars)
27 local k1,k2,k3,k4,k5,k6 = getVehicleColor(pojazd,true)
28 local x,y,z = getElementPosition(pojazd)
29 local _,_,rotacjaz = getElementRotation(pojazd)
30 if dbQuery(polaczenie, "INSERT INTO `cars`(`id`,`model`,`owner`,`color1`,`color2`,`color3`,`color4`,`color5`,`color6`,`posx`,`posy`,`posz`,`rotz`) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)", getElementData(pojazd,"veh:id"), id, player, k1,k2,k3,k4,k5,k6,x,y,z,rotacjaz ) then
31 warpPedIntoVehicle(plr,pojazd)
32 setElementData(pojazd,"veh:owner",player)
33 outputChatBox("Utworzono pojazd "..getVehicleName(pojazd).." z ID "..#cars.."",plr)
34 setVehiclePlateText(pojazd,#cars)
35 else
36 outputChatBox("BLAD",plr)
37 end
38 else
39 outputChatBox("ID musi byc liczba",plr)
40 end
41 else
42 outputChatBox("/veh <model> <gracz>",plr)
43 end
44end
45addCommandHandler( "veh", carCreate)
46
47
48
49function carSave()
50 for i,v in ipairs(getElementsByType("vehicle",resourceRoot)) do
51 if getElementData(v,"veh:id") then
52 local x,y,z = getElementPosition(v)
53 local _,_,rotz = getElementRotation(v)
54 local k1,k2,k3,k4,k5,k6 = getVehicleColor(v,true)
55 local owner = getElementData(v,"veh:owner")
56 local vehid = getElementData(v,"veh:id")
57 if dbQuery(polaczenie, "UPDATE `cars`SET `owner`=?, `color1`=?, `color2`=?, `color3`=?, `color4`=?, `color5`=?, `color6`=?, `posx`=?, `posy`=?, `posz`=?, `rotz`=? WHERE id=?", owner, k1,k2,k3,k4,k5,k6,x,y,z,rotz,vehid ) then
58 outputDebugString("Zapisano pojazd "..getElementData(v,"veh:id").."")
59 end
60 end
61 end
62end
63
64
65
66addEventHandler("onVehicleExit",resourceRoot,function(plr,seat)
67carSave()
68end)
69
70
71
72addEventHandler("onVehicleStartEnter",resourceRoot,function(plr,seat)
73if seat == 0 then
74 local name = getPlayerName(plr)
75 local data = getElementData(source,"veh:owner")
76 if data then
77 if data == name then return end
78 end
79 outputChatBox("To nie twoje auto",plr)
80 cancelEvent()
81end
82end)
83
84function carLoad(vehicle)
85 local pojazd = createVehicle(vehicle.model,vehicle.posx,vehicle.posy,vehicle.posz)
86 setElementRotation(pojazd,0,0,vehicle.rotz)
87 setElementData(pojazd,"veh:id",vehicle.id)
88 setElementData(pojazd,"veh:owner",vehicle.owner)
89 setVehicleColor(pojazd,vehicle.color1,vehicle.color2,vehicle.color3,vehicle.color4,vehicle.color5,vehicle.color6)
90 setVehiclePlateText(pojazd,getElementData(pojazd,"veh:id"))
91 table.insert(cars,pojazd)
92end
93
94
95
96
97addEventHandler("onResourceStart",resourceRoot,function()
98 local zapytanie = dbQuery(polaczenie, "SELECT * FROM `cars`")
99 local result = dbPoll ( zapytanie, -1 )
100 if result and #result > 0 then
101 for i,v in ipairs(result) do
102 carLoad(v)
103 end
104 else
105 outputDebugString("Blad pojazdow w bazie")
106 end
107end)