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