· 9 years ago · Oct 14, 2016, 07:54 AM
1--[[
2Copyright (c) 2016 Grosu `mihayy5` Mihaita
3MTA:DayZ Backup System v1.6
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 {"Wood Pile"},
18{"Bandage"},
19{"Water Bottle"},
20{"Pasta Can"},
21{"Beans Can"},
22{"Burger"},
23{"Box of Matches"},
24{"M911 Mag"},
25{"M9 SD Mag"},
26{"Desert Eagle Mag"},
27{"M911"},
28{"M9 SD"},
29{"Winchester 1866"},
30{"PDW"},
31{"Hunting Knife"},
32{"Morphine"},
33{"Hatchet"},
34{"Pizza"},
35{"Soda Bottle"},
36{"Empty Gas Canister"},
37{"Full Gas Canister"},
38{"Army Backpack"},
39{"Legendary Backpack"},
40{"Mountaion Backpack"},
41{"Roadflare"},
42{"Milk"},
43{"PDW Mag"},
44{"MP5A5 Mag"},
45{"AK Mag"},
46{"M4 Mag"},
47{"Tear Gas"},
48{"Grenade"},
49{"Desert Eagle"},
50{"Sawn-Off Shotgun"},
51{"SPAZ-12 Combat Shotgun"},
52{"MP5A5"},
53{"Watch"},
54{"Medic Kit"},
55{"Heat Pack"},
56{"Lee Enfield"},
57--{"TEC-9"},
58{"AK-47"},
59{"M136 Rocket Launcher"},
60{"Blood Bag"},
61{"GPS"},
62{"Map"},
63{"Toolbox"},
64{"Wire Fence"},
65{"Tire"},
66{"Engine"},
67{"Tank Parts"},
68{"M136 Rocket"},
69{"CZ 550 Mag"},
70{"Lee Enfield Mag"},
71{"M4"},
72{"CZ 550"},
73--{"Heat-Seeking RPG"},
74{"Satchel"},
75{"Infrared Goggles"},
76{"Night Vision Goggles"},
77{"Tent"},
78{"Raw Meat"},
79{"Cooked Meat"},
80{"Camouflage Clothing"},
81{"Terrorist Clothing"},
82{"Policeman Clothing"},
83{"Bat Clothing"},
84{"Ghillie Suit"},
85{"Desert Ghillie Suit"},
86{"Civilian Clothing"},
87{"Survivor Clothing"},
88{"Painkiller"},
89{"Binoculars"},
90{"Empty Water Bottle"},
91{"Empty Soda Cans"},
92{"Scruffy Burgers"},
93{"1866 Slug"},
94{"2Rnd. Slug"},
95{"SPAZ-12 Pellet"},
96{"Assault Pack (ACU)"},
97{"Alice Pack"},
98{"Radio Device"},
99{"Coyote Backpack"},
100{"Sniper Backpack"},
101{"Ghillie Backpack"},
102{"Czech Backpack"},
103{"Shop Tokens"},
104{"Baseball Bat"},
105{"Shovel"},
106{"Golf Club"}
107};
108
109function noteAdmins(msg)
110 for _,v in ipairs(getElementsByType("player")) do
111 if (isObjectInACLGroup("user."..getAccountName(getPlayerAccount(v)), aclGetGroup("Owner"))) then
112 outputChatBox("#FF0000[Backup]: #FFFFFF"..tostring(msg), v, 255, 255, 255, true);
113 end
114 end
115end
116
117if (dbType == "MySQL") then
118 db = dbConnect("mysql", "dbname="..dbName..";host="..dbHost..";port="..dbPort, dbUser, dbPass, "share=1");
119 dbExec(db, "CREATE TABLE IF NOT EXISTS vehicles(model, x, y, z, rX, rY, rZ, slots, fuel, engines, moving, parts, items, id)");
120 dbExec(db, "CREATE TABLE IF NOT EXISTS tents(model, x, y, z, rX, rY, rZ, slots, scale, items, id)");
121 noteAdmins("Database connected to MySQL!");
122elseif (dbType == "SQLite") then
123 db = dbConnect("sqlite", "database.db");
124 dbExec(db, "CREATE TABLE IF NOT EXISTS vehicles(model, x, y, z, rX, rY, rZ, slots, fuel, engines, moving, parts, items, id)");
125 dbExec(db, "CREATE TABLE IF NOT EXISTS tents(model, x, y, z, rX, rY, rZ, slots, scale, items, id)");
126 noteAdmins("Database connected to SQLite!");
127else return noteAdmins("Error: `dbType` wrong attribute!"); end
128
129function backup_vehs()
130 dbExec(db, "DELETE FROM vehicles");
131 local vc = 0;
132 for _,veh in ipairs(getElementsByType("vehicle")) do
133 if (not getElementData(veh, "helicrash") and not getElementData(veh, "isExploded")) then
134 local col = getElementData(veh, "parent");
135 if (col and getElementType(col) == "colshape") then
136 local x, y, z = getElementPosition(veh);
137 local rX, rY, rZ = getElementRotation(veh);
138 local items = {};
139 vc = vc+1;
140 for _,item in ipairs(itemsTable) do
141 local quantity = getElementData(col, item[1]) or 0;
142 if (quantity > 0) then
143 table.insert(items, {item[1], quantity});
144 end
145 end
146 dbExec(db, "INSERT INTO vehicles(model, x, y, z, rX, rY, rZ, slots, fuel, engines, moving, parts, items, id) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
147 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,
148 getElementData(col, "Tire_inVehicle") or 0, getElementData(col, "Parts_inVehicle") or 0, toJSON(items), vc);
149 end
150 end
151 end
152 noteAdmins("Vehicles saved ["..vc.."]");
153end
154
155function backup_tents()
156 dbExec(db, "DELETE FROM tents");
157 local tc = 0;
158 for _,col in ipairs(getElementsByType("colshape")) do
159 if (getElementData(col, "tent")) then
160 local tent = getElementData(col, "parent");
161 local x, y, z = getElementPosition(tent);
162 local rX, rY, rZ = getElementRotation(tent);
163 local items = {};
164 tc = tc+1;
165 for _,item in ipairs(itemsTable) do
166 local quantity = getElementData(col, item[1]) or 0;
167 if (quantity > 0) then
168 table.insert(items, {item[1], quantity});
169 end
170 end
171 dbExec(db, "INSERT INTO tents(model, x, y, z, rX, rY, rZ, slots, scale, items, id) VALUES(?,?,?,?,?,?,?,?,?,?,?)",
172 getElementModel(tent), x, y, z, rX, rY, rZ, getElementData(col, "MAX_Slots") or 100, getObjectScale(tent), toJSON(items), tc);
173 end
174 end
175 noteAdmins("Tents saved ["..tc.."]");
176end
177
178function create_veh(model, x, y, z, rX, rY, rZ, slots, fuel, engines, moving, parts, items, id)
179 local veh = createVehicle(model, x, y, z);
180 local vehCol = createColSphere(x+5, y, z, 4);
181 setElementRotation(veh, rX, rY, rZ);
182 attachElements(vehCol, veh, 0, 0, 0);
183 setElementData(vehCol, "parent", veh);
184 setElementData(veh, "parent", vehCol);
185 setElementData(vehCol, "vehicle", true);
186 setElementData(vehCol, "MAX_Slots", tonumber(slots));
187 setElementData(vehCol, "Tire_inVehicle", tonumber(moving));
188 setElementData(vehCol, "Engine_inVehicle", tonumber(engines));
189 setElementData(vehCol, "Parts_inVehicle", tonumber(parts));
190 setElementData(vehCol, "spawn", {model, x, y, z});
191 setElementData(vehCol, "fuel", tonumber(fuel));
192 for _,v in ipairs(fromJSON(items)) do
193 setElementData(vehCol, v[1], v[2]);
194 end
195end
196
197function create_tent(model, x, y, z, rX, rY, rZ, slots, scale, items, id)
198 local tent = createObject(model, x, y, z);
199 local tentCol = createColSphere(x, y, z, 3);
200 setElementRotation(tent, rX, rY, rZ);
201 setObjectScale(tent, scale);
202 attachElements(tentCol, tent, 0, 0, 0);
203 setElementData(tentCol, "parent", tent);
204 setElementData(tent, "parent", tentCol);
205 setElementData(tentCol, "tent", true);
206 --setElementData(tentCol, "vehicle", true);
207 setElementData(tentCol, "MAX_Slots", slots);
208 for _,v in ipairs(fromJSON(items)) do
209 setElementData(tentCol, v[1], v[2]);
210 end
211end
212
213function load_vehs(q)
214 if (q) then
215 local p = dbPoll(q, 0);
216 if (#p > 0) then
217 for _,d in pairs(p) do
218 last_veh_id = d["id"];
219 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"]);
220 end
221 end
222 end
223 noteAdmins("Vehicles loaded ["..tonumber(last_veh_id).."]");
224end
225function load_tents(q)
226 if (q) then
227 local p = dbPoll(q, 0);
228 if (#p > 0) then
229 for _,d in pairs(p) do
230 last_tent_id = d["id"];
231 create_tent(d["model"], d["x"], d["y"], d["z"], d["rX"], d["rY"], d["rZ"], d["slots"], d["scale"], d["items"], d["id"]);
232 end
233 end
234 end
235 noteAdmins("Tents loaded ["..tonumber(last_tent_id).."]");
236end
237function dVehs()
238 for _,veh in ipairs(getElementsByType("vehicle")) do
239 local parent = getElementData(veh, "parent");
240 if (parent) then
241 destroyElement(veh);
242 destroyElement(parent);
243 end
244 end
245 noteAdmins("Vehs deleted!");
246end
247function dTents()
248 for _,tent in ipairs(getElementsByType("colshape")) do
249 local parent = getElementData(tent, "parent");
250 if (parent) then
251 destroyElement(tent);
252 destroyElement(parent);
253 end
254 end
255 noteAdmins("Tents deleted!");
256end
257
258addEventHandler("onPlayerCommand", getRootElement(), function(cmd)
259 if (isObjectInACLGroup("user."..getAccountName(getPlayerAccount(source)), aclGetGroup("Owner"))) then
260 if (cmd == "loadtents") then
261 dbQuery(load_tents, {}, db, "SELECT * FROM `tents`");
262 elseif (cmd == "loadvehicles") then
263 dbQuery(load_vehs, {}, db, "SELECT * FROM `vehicles`");
264 elseif (cmd == "loadbackup") then
265 dbQuery(load_tents, {}, db, "SELECT * FROM `tents`");
266 dbQuery(load_vehs, {}, db, "SELECT * FROM `vehicles`");
267
268 elseif (cmd == "vehiclebackup") then
269 backup_vehs();
270 elseif (cmd == "backup") then
271 backup_vehs();
272 backup_tents();
273 elseif (cmd == "tentbackup") then
274 backup_tents();
275 elseif (cmd == "destroyvehicles") then
276 dVehs();
277 elseif (cmd == "destroytents") then
278 dTents();
279 elseif (cmd == "togglebackup") then
280 if (isTimer(vehbkTimer) and isTimer(tentbkTimer)) then
281 killTimer(vehbkTimer);
282 killTimer(tentbkTimer);
283 noteAdmins(getPlayerName(source).."#FFFFFF stopped automatic backup!");
284 else
285 vehbkTimer = setTimer(backup_vehs, 30*60000, 0);
286 tentbkTimer = setTimer(backup_tents, 30*60000, 0);
287 noteAdmins(getPlayerName(source).."#FFFFFF started automatic backup!");
288 end
289 end
290 end
291end);