Advertisement
Guest User

Untitled

a guest
Oct 14th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.13 KB | None | 0 0
  1. --[[
  2. Copyright (c) 2016 Grosu `mihayy5` Mihaita
  3. MTA:DayZ Backup System v1.6
  4. --]]
  5.  
  6. local dbType = "SQLite"; --Here set your database type, "MySQL" or "SQLite" <!>
  7. local dbName = ""; --Your database name <!>
  8. local dbHost = ""; --Your hostname/IP <!>
  9. local dbUser = ""; --Username for database <!>
  10. local dbPass = ""; --Password for database <!>
  11. local dbPort = 0; --This is an port exemple CHANGE IT <!>
  12.  
  13. local last_veh_id = 0; --Don't edit here <!>
  14. local last_tent_id = 0; --Don't edit here <!>
  15.  
  16. local 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.  
  109. function 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
  115. end
  116.  
  117. if (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!");
  122. elseif (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!");
  127. else return noteAdmins("Error: `dbType` wrong attribute!"); end
  128.  
  129. function 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.."]");
  153. end
  154.  
  155. function 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.."]");
  176. end
  177.  
  178. function 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
  195. end
  196.  
  197. function 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
  211. end
  212.  
  213. function 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).."]");
  224. end
  225. function 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).."]");
  236. end
  237. function 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!");
  246. end
  247. function 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!");
  256. end
  257.  
  258. addEventHandler("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
  291. end);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement