Sync frame count across devices

This commit is contained in:
Ethan O'Brien 2023-07-17 09:18:05 -05:00
parent f0c094a061
commit f1cf9641bc

View file

@ -2937,6 +2937,7 @@ class EmulatorJS {
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4()); return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
} }
this.netplay.url = this.config.netplayUrl; this.netplay.url = this.config.netplayUrl;
this.netplay.current_frame = 0;
this.netplay.getOpenRooms = async () => { this.netplay.getOpenRooms = async () => {
return JSON.parse(await (await fetch(this.netplay.url+"/list?domain="+window.location.host+"&game_id="+this.config.gameId)).text()); return JSON.parse(await (await fetch(this.netplay.url+"/list?domain="+window.location.host+"&game_id="+this.config.gameId)).text());
} }
@ -3068,6 +3069,7 @@ class EmulatorJS {
console.log(users); console.log(users);
this.netplay.players = users; this.netplay.players = users;
this.netplay.updatePlayersTable(); this.netplay.updatePlayersTable();
if (this.netplay.owner) this.netplay.sync();
}) })
this.netplay.socket.on("disconnect", () => this.netplay.roomLeft()); this.netplay.socket.on("disconnect", () => this.netplay.roomLeft());
this.netplay.socket.on("data-message", (data) => { this.netplay.socket.on("data-message", (data) => {
@ -3087,6 +3089,7 @@ class EmulatorJS {
sessionid: sessionid sessionid: sessionid
} }
this.netplay.players[this.netplay.playerID] = this.netplay.extra; this.netplay.players[this.netplay.playerID] = this.netplay.extra;
this.netplay.users = {};
this.netplay.startSocketIO((error) => { this.netplay.startSocketIO((error) => {
this.netplay.socket.emit("open-room", { this.netplay.socket.emit("open-room", {
@ -3136,6 +3139,7 @@ class EmulatorJS {
this.netplay.roomJoined = (isOwner, roomName, password, roomId) => { this.netplay.roomJoined = (isOwner, roomName, password, roomId) => {
//Will already assume this.netplay.players has been refreshed //Will already assume this.netplay.players has been refreshed
this.isNetplay = true; this.isNetplay = true;
this.netplay.owner = isOwner;
console.log(this.netplay.extra); console.log(this.netplay.extra);
this.netplay.roomNameElem.innerText = roomName; this.netplay.roomNameElem.innerText = roomName;
this.netplay.tabs[0].style.display = "none"; this.netplay.tabs[0].style.display = "none";
@ -3180,8 +3184,43 @@ class EmulatorJS {
this.netplay.createButton.innerText = "Create a Room"; this.netplay.createButton.innerText = "Create a Room";
this.netplay.socket.disconnect(); this.netplay.socket.disconnect();
} }
this.netplay.sync = async () => {
const state = await this.gameManager.getState();
this.netplay.sendMessage({
state: state,
resetCurrentFrame: true
});
this.netplay.current_frame = 0;
}
this.netplay.dataMessage = (data) => { this.netplay.dataMessage = (data) => {
console.log(data); console.log(data);
if (data.state) {
this.gameManager.loadState(new Uint8Array(data.state));
}
if (data.resetCurrentFrame) {
this.netplay.current_frame = 0;
}
if (data.user_frame && this.netplay.owner) {
this.netplay.users[data.user_frame.user] = data.user_frame.frame;
console.log(data.user_frame.frame, this.netplay.current_frame);
}
}
this.netplay.sendMessage = (data) => {
this.netplay.socket.emit("data-message", data);
}
this.Module.postMainLoop = () => {
if (!this.isNetplay || this.paused) return;
this.netplay.current_frame++;
if (this.netplay.owner) {
} else {
this.netplay.sendMessage({
user_frame: {
user: this.netplay.playerID,
frame: this.netplay.current_frame
}
});
}
} }
this.netplay.updateList = { this.netplay.updateList = {