· 6 years ago · Jul 30, 2019, 11:46 AM
1--[[
2Copyright (c) 2016 Grosu `Tekken` Mihaita
3MTA:DayZ Backup System v1.7
4--]]
5
6local dbType = "SQLite"; --Here set your database type, "MySQL" or "SQLite" <!>
7local dbName = ""; --Your database name <!>
8local dbHost = ""; --Your hostname/IP <!>
9local dbUser = ""; --Username for database <!>
10local dbPass = ""; --Password for database <!>
11local dbPort = 0; --This is an port exemple CHANGE IT <!>
12
13local last_veh_id = 0; --Don't edit here <!>
14local last_tent_id = 0; --Don't edit here <!>
15
16local itemsTable = { -- If your server have different items be sure you imported your item table here!
17 {"Heavy Armor"}, {"M136"}, {"M136 Rocket"}, {"CZ 550"},
18 {"MP5A5"}, {"PDW"}, {"SPAZ-12 Combat Shotgun"},
19 {"Sawn-Off Shotgun"}, {"Winchester 1866"},
20 {"M9 SD"}, {"M911"}, {"Desert Eagle"},
21 {"Binoculars"}, {"Tear Gas"}, {"Grenade"},
22 {"Satchel"}, {"Baseball Bat"}, {"Shovel"},
23 {"Golf Club"}, {"Hunting Knife"}, {"Hatchet"},
24 {"M4 Mag"}, {"AK Mag"}, {"Lee Enfield Mag"},
25 {"CZ 550 Mag"}, {"MP5A5 Mag"}, {"PDW Mag"},
26 {"SPAZ-12 Pellet"}, {"2Rnd. Slug"}, {"1866 Slug"},
27 {"M9 SD Mag"}, {"M911 Mag"}, {"Desert Eagle Mag"},
28 {"Water Bottle"}, {"Pasta Can"}, {"Beans Can"},
29 {"Burger"}, {"Pizza"},{"Soda Bottle"}, {"Milk"},
30 {"Empty Water Bottle"}, {"Empty Soda Cans"},
31 {"Scruffy Burgers"}, {"Raw Meat"}, {"Cooked Meat"},
32 {"Coyote Backpack"}, {"Czech Backpack"},
33 {"Assault Pack (ACU)"}, {"Alice Pack"},
34 {"Box of Matches"}, {"Infrared Goggles"},
35 {"Night Vision Goggles"}, {"GPS"}, {"Map"},
36 {"Toolbox"}, {"Watch"}, {"Radio Device"},
37 {"Bandage"}, {"Morphine"}, {"Medic Kit"},
38 {"Heat Pack"}, {"Blood Bag"}, {"Painkiller"},
39 {"Tire"}, {"Engine"}, {"Tank Parts"},
40 {"Camouflage Clothing"}, {"Ghillie Suit"},
41 {"Civilian Clothing"}, {"Survivor Clothing"},
42 {"Wood Pile"}, {"Empty Gas Canister"},
43 {"Full Gas Canister"}, {"Roadflare"},
44 {"Wire Fence"}, {"Tent"}
45};
46
47function noteAdmins(msg)
48 for _,v in ipairs(getElementsByType("player")) do
49 if (isObjectInACLGroup("user."..getAccountName(getPlayerAccount(v)), aclGetGroup("Admin"))) then
50 outputChatBox("#FF0000[Backup]: #FFFFFF"..tostring(msg), v, 255, 255, 255, true);
51 end
52 end
53end
54
55if (dbType == "MySQL") then
56 db = dbConnect("mysql", "dbname="..dbName..";host="..dbHost..";port="..dbPort, dbUser, dbPass, "share=1");
57 noteAdmins("Database connected to MySQL!");
58elseif (dbType == "SQLite") then
59 db = dbConnect("sqlite", "database.db");
60 noteAdmins("Database connected to SQLite!");
61else
62 noteAdmins("Error: `dbType` wrong attribute!");
63 return;
64end
65
66dbExec(db, "CREATE TABLE IF NOT EXISTS vehicles(model Int(10), x VARCHAR(60), y VARCHAR(60), z VARCHAR(60), rX VARCHAR(60), rY VARCHAR(60), rZ VARCHAR(60), slots Int(5), fuel VARCHAR(30), engines Int(5), moving Int(5), parts Int(5), items VARCHAR(10000), id Int(5))");
67dbExec(db, "CREATE TABLE IF NOT EXISTS tents(model Int(10), x VARCHAR(60), y VARCHAR(60), z VARCHAR(60), rX VARCHAR(60), rY VARCHAR(60), rZ VARCHAR(60), slots Int(5), scale Int(5), items VARCHAR(10000), id Int(5))");
68
69function backup_vehs()
70 dbExec(db, "DELETE FROM vehicles");
71 local vc = 0;
72 for _,veh in ipairs(getElementsByType("vehicle")) do
73 if (not getElementData(veh, "helicrash") and not getElementData(veh, "isExploded")) then
74 local col = getElementData(veh, "parent");
75 if (col and getElementType(col) == "colshape") then
76 local x, y, z = getElementPosition(veh);
77 local rX, rY, rZ = getElementRotation(veh);
78 local items = {};
79 vc = vc+1;
80 for _,item in ipairs(itemsTable) do
81 local quantity = getElementData(col, item[1]) or 0;
82 if (quantity > 0) then
83 table.insert(items, {item[1], quantity});
84 end
85 end
86 dbExec(db, "INSERT INTO vehicles(model, x, y, z, rX, rY, rZ, slots, fuel, engines, moving, parts, items, id) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
87 getElementModel(veh), x, y, z, rX, rY, rZ, getElementData(col, "MAX_Slots") or 20, getElementData(col, "fuel") or 0, getElementData(col, "Engine_inVehicle") or 0,
88 getElementData(col, "Tire_inVehicle") or 0, getElementData(col, "Parts_inVehicle") or 0, toJSON(items), vc);
89 end
90 end
91 end
92 noteAdmins("Vehicles saved ["..vc.."]");
93end
94
95function backup_tents()
96 dbExec(db, "DELETE FROM tents");
97 local tc = 0;
98 for _,col in ipairs(getElementsByType("colshape")) do
99 if (getElementData(col, "tent")) then
100 local tent = getElementData(col, "parent");
101 local x, y, z = getElementPosition(tent);
102 local rX, rY, rZ = getElementRotation(tent);
103 local items = {};
104 tc = tc+1;
105 for _,item in ipairs(itemsTable) do
106 local quantity = getElementData(col, item[1]) or 0;
107 if (quantity > 0) then
108 table.insert(items, {item[1], quantity});
109 end
110 end
111 dbExec(db, "INSERT INTO tents(model, x, y, z, rX, rY, rZ, slots, scale, items, id) VALUES(?,?,?,?,?,?,?,?,?,?,?)",
112 getElementModel(tent), x, y, z, rX, rY, rZ, getElementData(col, "MAX_Slots") or 100, getObjectScale(tent), toJSON(items), tc);
113 end
114 end
115 noteAdmins("Tents saved ["..tc.."]");
116end
117
118function create_veh(model, x, y, z, rX, rY, rZ, slots, fuel, engines, moving, parts, items, id)
119 local veh = createVehicle(model, x, y, z);
120 local vehCol = createColSphere(x+5, y, z, 4);
121 setElementRotation(veh, rX, rY, rZ);
122 attachElements(vehCol, veh, 0, 0, 0);
123 setElementData(vehCol, "parent", veh);
124 setElementData(veh, "parent", vehCol);
125 setElementData(vehCol, "vehicle", true);
126 setElementData(vehCol, "MAX_Slots", tonumber(slots));
127 setElementData(vehCol, "Tire_inVehicle", tonumber(moving));
128 setElementData(vehCol, "Engine_inVehicle", tonumber(engines));
129 setElementData(vehCol, "Parts_inVehicle", tonumber(parts));
130 setElementData(vehCol, "spawn", {model, x, y, z});
131 setElementData(vehCol, "fuel", tonumber(fuel));
132 for _,v in ipairs(fromJSON(items)) do
133 setElementData(vehCol, v[1], v[2]);
134 end
135end
136
137function create_tent(model, x, y, z, rX, rY, rZ, slots, scale, items, id)
138 local tent = createObject(model, x, y, z);
139 local tentCol = createColSphere(x, y, z, 3);
140 setElementRotation(tent, rX, rY, rZ);
141 setObjectScale(tent, scale);
142 attachElements(tentCol, tent, 0, 0, 0);
143 setElementData(tentCol, "parent", tent);
144 setElementData(tent, "parent", tentCol);
145 setElementData(tentCol, "tent", true);
146 setElementData(tentCol, "MAX_Slots", slots);
147 setElementData(tentCol,'hidetent',true)
148 for _,v in ipairs(fromJSON(items)) do
149 setElementData(tentCol, v[1], v[2]);
150 end
151end
152
153function load_vehs(q)
154 if (q) then
155 local p = dbPoll(q, 0);
156 if (#p > 0) then
157 for _,d in pairs(p) do
158 last_veh_id = d["id"];
159 create_veh(d["model"], d["x"], d["y"], d["z"], d["rX"], d["rY"], d["rZ"], d["slots"], d["fuel"], d["engines"], d["moving"], d["parts"], d["items"], d["id"]);
160 end
161 end
162 end
163 noteAdmins("Vehicles loaded ["..tonumber(last_veh_id).."]");
164end
165function load_tents(q)
166 if (q) then
167 local p = dbPoll(q, 0);
168 if (#p > 0) then
169 for _,d in pairs(p) do
170 last_tent_id = d["id"];
171 create_tent(d["model"], d["x"], d["y"], d["z"], d["rX"], d["rY"], d["rZ"], d["slots"], d["scale"], d["items"], d["id"]);
172 end
173 end
174 end
175 noteAdmins("Tents loaded ["..tonumber(last_tent_id).."]");
176end
177function dVehs()
178 for _,veh in ipairs(getElementsByType("vehicle")) do
179 local parent = getElementData(veh, "parent");
180 if (parent) then
181 destroyElement(parent);
182 destroyElement(veh);
183 end
184 end
185 noteAdmins("Vehs deleted!");
186end
187function dTents()
188 for _,tent in ipairs(getElementsByType("colshape")) do
189 local parent = getElementData(tent, "parent");
190 if (parent) then
191 destroyElement(parent);
192 destroyElement(tent);
193 end
194 end
195 noteAdmins("Tents deleted!");
196end
197
198addEventHandler("onPlayerCommand", getRootElement(), function(cmd)
199 if (isObjectInACLGroup("user."..getAccountName(getPlayerAccount(source)), aclGetGroup("Admin"))) then
200 if (cmd == "loadtents") then
201 dbQuery(load_tents, {}, db, "SELECT * FROM `tents`");
202 elseif (cmd == "loadvehs") then
203 dbQuery(load_vehs, {}, db, "SELECT * FROM `vehicles`");
204 elseif (cmd == "vehbk") then
205 backup_vehs();
206 elseif (cmd == "tentbk") then
207 backup_tents();
208 elseif (cmd == "dvehs") then
209 dVehs();
210 elseif (cmd == "dtents") then
211 dTents();
212 elseif (cmd == "togbk") then
213 if (isTimer(vehbkTimer) and isTimer(tentbkTimer)) then
214 killTimer(vehbkTimer);
215 killTimer(tentbkTimer);
216 noteAdmins(getPlayerName(source).."#FFFFFF stopped automatic backup!");
217 else
218 vehbkTimer = setTimer(backup_vehs, 30*60000, 0);
219 tentbkTimer = setTimer(backup_tents, 30*60000, 0);
220 noteAdmins(getPlayerName(source).."#FFFFFF started automatic backup!");
221 end
222 end
223 end
224end);