Jump to content
RAGE Multiplayer Community

Save datas if server closed


J0NAH26
 Share

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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;
});

 

  • Like 1
Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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;
});

 

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...