J0NAH26 Posted August 2, 2021 Posted August 2, 2021 Hey, i want to change datas if the server close. ServerShutdown dont work to me. I saw something with process.on but that dont work for me too. Maybe i do it wrong. Can someone help me? Im using js server and client
Nutter Posted August 2, 2021 Posted August 2, 2021 serverShutdown used with with mp.events.delayShutdown does work if the server shuts down gracefully (e.g. it does not crash). It would help if you share some code.
J0NAH26 Posted August 2, 2021 Author Posted August 2, 2021 vor 2 Minuten schrieb Nutter: serverShutdown used with with mp.events.delayShutdown does work if the server shuts down gracefully (e.g. it does not crash). It would help if you share some code. Should it work if i close the ragemp-server.exe? I only want to save the position of all cars and change it in my database. If i use the playerquit event, it works. Can you give me a example how it should work with the delay?
Nutter Posted August 2, 2021 Posted August 2, 2021 It will work in that case. Here's an example: async function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } mp.events.add("serverShutdown", async () => { mp.events.delayShutdown = true; mp.vehicles.forEach(async (vehicle) => { // Represents some long asynchronous operation await sleep(3000); }); mp.events.delayShutdown = false; }); 1
J0NAH26 Posted August 3, 2021 Author Posted August 3, 2021 vor 21 Stunden schrieb Nutter: It will work in that case. Here's an example: async function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } mp.events.add("serverShutdown", async () => { mp.events.delayShutdown = true; mp.vehicles.forEach(async (vehicle) => { // Represents some long asynchronous operation await sleep(3000); }); mp.events.delayShutdown = false; }); I copied it but it dont work. If i doing an interval or useing for example playerjoin event, it works.
Nutter Posted August 3, 2021 Posted August 3, 2021 56 minutes ago, J0NAH26 said: I copied it but it dont work. If i doing an interval or useing for example playerjoin event, it works. Try mp.events.delayTermination instead of mp.events.delayShutdown
J0NAH26 Posted August 3, 2021 Author Posted August 3, 2021 vor 9 Minuten schrieb Nutter: Try mp.events.delayTermination instead of mp.events.delayShutdown Dont work too.
Nutter Posted August 3, 2021 Posted August 3, 2021 You could switch forEach with for loop so you wait for all promises to resolve, but it still works for me...
J0NAH26 Posted August 3, 2021 Author Posted August 3, 2021 vor 2 Minuten schrieb Nutter: You could switch forEach with for loop so you wait for all promises to resolve, but it still works for me... async function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } mp.events.add("serverShutdown", async () => { mp.events.delayTermination = true; mp.vehicles.forEach(async (vehicle) => { let x = vehicle.position.x; let y = vehicle.position.y; let z = vehicle.position.z; let rx = vehicle.rotation.x; let ry = vehicle.rotation.y; let rz = vehicle.rotation.z; let plate = vehicle.numberPlate; let volume = vehicle.getVariable("Volume"); connection.query("SELECT Garage FROM vehicledata WHERE Plate = ?", [plate], function (err, res) { let garage = res[0].Garage; if (garage < 1) { connection.query("UPDATE vehicledata SET PosX = ?, PosY = ?, PosZ = ?, Volume = ?, RotX = ?, RotY = ?, RotZ = ? WHERE Plate = ?", [x, y, z, volume, rx, ry, rz, plate], function (err,res) { if(err) { console.log(err); } }); } }); await sleep(3000); }); mp.events.delayTermination = false; }); Is there a mistake?
Nutter Posted August 3, 2021 Posted August 3, 2021 This would work too mp.events.add("serverShutdown", async () => { mp.events.delayTermination = true; const vehicles = mp.vehicles.toArray(); for(let i = 0; i < vehicles.length; i++) { const vehicle = vehicles[i]; if(!vehicle || !mp.vehicles.exists(vehicle)) { continue; } let x = vehicle.position.x; let y = vehicle.position.y; let z = vehicle.position.z; let rx = vehicle.rotation.x; let ry = vehicle.rotation.y; let rz = vehicle.rotation.z; let plate = vehicle.numberPlate; let volume = vehicle.getVariable("Volume"); connection.query("SELECT Garage FROM vehicledata WHERE Plate = ?", [plate], function (err, res) { let garage = res[0].Garage; if (garage < 1) { connection.query("UPDATE vehicledata SET PosX = ?, PosY = ?, PosZ = ?, Volume = ?, RotX = ?, RotY = ?, RotZ = ? WHERE Plate = ?", [x, y, z, volume, rx, ry, rz, plate], function (err,res) { if(err) { console.log(err); } }); } }); } mp.events.delayTermination = false; });
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now