mirror of
https://github.com/EmulatorJS/EmulatorJS.git
synced 2024-09-20 02:46:23 +00:00
Cleanup syncing
This commit is contained in:
parent
5ff3257894
commit
6b484a6eca
2 changed files with 43 additions and 6 deletions
2
a.html
2
a.html
|
@ -20,7 +20,7 @@
|
||||||
EJS_volume = 1;
|
EJS_volume = 1;
|
||||||
EJS_startOnLoaded = true;
|
EJS_startOnLoaded = true;
|
||||||
//EJS_loadStateURL = "mega_mountain.state";
|
//EJS_loadStateURL = "mega_mountain.state";
|
||||||
EJS_netplayServer = "http://"+window.location.hostname+":3000/";
|
//EJS_netplayServer = "http://"+window.location.hostname+":3000/";
|
||||||
EJS_gameID = 1;
|
EJS_gameID = 1;
|
||||||
</script>
|
</script>
|
||||||
<script src='data/loader.js'></script>
|
<script src='data/loader.js'></script>
|
||||||
|
|
|
@ -155,6 +155,7 @@ class EmulatorJS {
|
||||||
}
|
}
|
||||||
constructor(element, config) {
|
constructor(element, config) {
|
||||||
this.ejs_version = "4.0";
|
this.ejs_version = "4.0";
|
||||||
|
//this.netplay = false; //DO NOT ENABLE UNLESS YOU KNOW WHAT YOU'RE DOING
|
||||||
this.config = config;
|
this.config = config;
|
||||||
window.EJS_TESTING = this;
|
window.EJS_TESTING = this;
|
||||||
this.currentPopup = null;
|
this.currentPopup = null;
|
||||||
|
@ -911,7 +912,7 @@ class EmulatorJS {
|
||||||
this.elements.contextMenu.save.style.display = "none";
|
this.elements.contextMenu.save.style.display = "none";
|
||||||
this.elements.contextMenu.load.style.display = "none";
|
this.elements.contextMenu.load.style.display = "none";
|
||||||
}
|
}
|
||||||
if (typeof this.config.gameId !== "number" || !this.config.netplayUrl) {
|
if (typeof this.config.gameId !== "number" || !this.config.netplayUrl || this.netplay === false) {
|
||||||
this.elements.bottomBar.netplay[0].style.display = "none";
|
this.elements.bottomBar.netplay[0].style.display = "none";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1208,6 +1209,8 @@ class EmulatorJS {
|
||||||
this.gameManager.saveSaveFiles();
|
this.gameManager.saveSaveFiles();
|
||||||
this.gameManager.restart();
|
this.gameManager.restart();
|
||||||
this.netplay.sendMessage({restart:true});
|
this.netplay.sendMessage({restart:true});
|
||||||
|
this.netplay.current_frame = 0;
|
||||||
|
this.play();
|
||||||
} else if (!this.isNetplay) {
|
} else if (!this.isNetplay) {
|
||||||
this.gameManager.saveSaveFiles();
|
this.gameManager.saveSaveFiles();
|
||||||
this.gameManager.restart();
|
this.gameManager.restart();
|
||||||
|
@ -3025,9 +3028,9 @@ class EmulatorJS {
|
||||||
return join;
|
return join;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.netplay.table.innerHTML = "";
|
|
||||||
const open = await this.netplay.getOpenRooms();
|
const open = await this.netplay.getOpenRooms();
|
||||||
//console.log(open);
|
//console.log(open);
|
||||||
|
this.netplay.table.innerHTML = "";
|
||||||
for (const k in open) {
|
for (const k in open) {
|
||||||
addToTable(k, open[k].room_name, open[k].current, open[k].max);//todo: password
|
addToTable(k, open[k].room_name, open[k].current, open[k].max);//todo: password
|
||||||
}
|
}
|
||||||
|
@ -3282,18 +3285,27 @@ class EmulatorJS {
|
||||||
}
|
}
|
||||||
this.updateCheatUI();
|
this.updateCheatUI();
|
||||||
}
|
}
|
||||||
|
this.netplay.setLoading = (loading) => {
|
||||||
|
console.log("loading:", loading);
|
||||||
|
}
|
||||||
|
let syncing = false;
|
||||||
this.netplay.sync = async () => {
|
this.netplay.sync = async () => {
|
||||||
|
if (syncing) return;
|
||||||
|
syncing = true;
|
||||||
|
console.log("sync")
|
||||||
this.netplay.ready = 0;
|
this.netplay.ready = 0;
|
||||||
const state = await this.gameManager.getState();
|
const state = await this.gameManager.getState();
|
||||||
this.netplay.sendMessage({
|
this.netplay.sendMessage({
|
||||||
state: state
|
state: state
|
||||||
});
|
});
|
||||||
|
this.netplay.setLoading(true);
|
||||||
this.pause(true);
|
this.pause(true);
|
||||||
this.netplay.ready++;
|
this.netplay.ready++;
|
||||||
this.netplay.current_frame = 0;
|
this.netplay.current_frame = 0;
|
||||||
if (this.netplay.ready === this.netplay.getUserCount()) {
|
if (this.netplay.ready === this.netplay.getUserCount()) {
|
||||||
this.play(true);
|
this.play(true);
|
||||||
}
|
}
|
||||||
|
syncing = false;
|
||||||
}
|
}
|
||||||
this.netplay.getUserIndex = (user) => {
|
this.netplay.getUserIndex = (user) => {
|
||||||
let i=0;
|
let i=0;
|
||||||
|
@ -3308,9 +3320,11 @@ class EmulatorJS {
|
||||||
for (const k in this.netplay.players) i++;
|
for (const k in this.netplay.players) i++;
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
let justReset = false;
|
||||||
this.netplay.dataMessage = (data) => {
|
this.netplay.dataMessage = (data) => {
|
||||||
//console.log(data);
|
//console.log(data);
|
||||||
if (data.state) {
|
if (data.state) {
|
||||||
|
this.netplay.setLoading(true);
|
||||||
this.pause(true);
|
this.pause(true);
|
||||||
this.gameManager.loadState(new Uint8Array(data.state));
|
this.gameManager.loadState(new Uint8Array(data.state));
|
||||||
this.netplay.sendMessage({ready:true});
|
this.netplay.sendMessage({ready:true});
|
||||||
|
@ -3324,23 +3338,38 @@ class EmulatorJS {
|
||||||
if (data.ready && this.netplay.owner) {
|
if (data.ready && this.netplay.owner) {
|
||||||
this.netplay.ready++;
|
this.netplay.ready++;
|
||||||
if (this.netplay.ready === this.netplay.getUserCount()) {
|
if (this.netplay.ready === this.netplay.getUserCount()) {
|
||||||
this.netplay.sendMessage({play:true, resetCurrentFrame: true});
|
this.netplay.sendMessage({readyready:true, resetCurrentFrame: true});
|
||||||
setTimeout(() => this.play(true), 100);
|
setTimeout(() => this.play(true), 100);
|
||||||
|
this.netplay.setLoading(false);
|
||||||
this.netplay.current_frame = 0;
|
this.netplay.current_frame = 0;
|
||||||
|
justReset = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (data.readyready) {
|
||||||
|
this.netplay.setLoading(false);
|
||||||
|
this.netplay.current_frame = 0;
|
||||||
|
this.play(true)
|
||||||
|
}
|
||||||
if (data.resetCurrentFrame) {
|
if (data.resetCurrentFrame) {
|
||||||
this.play(true);
|
this.play(true);
|
||||||
this.netplay.current_frame = 0;
|
this.netplay.current_frame = 0;
|
||||||
this.netplay.inputs = {};
|
this.netplay.inputs = {};
|
||||||
}
|
}
|
||||||
if (data.user_frame && this.netplay.owner) {
|
if (data.user_frame && this.netplay.owner) {
|
||||||
|
if (justReset) {
|
||||||
|
justReset = false;
|
||||||
|
this.netplay.current_frame = 0;
|
||||||
|
this.netplay.inputs = {};
|
||||||
|
}
|
||||||
this.netplay.users[data.user_frame.user] = data.user_frame.frame;
|
this.netplay.users[data.user_frame.user] = data.user_frame.frame;
|
||||||
//console.log(data.user_frame.frame, this.netplay.current_frame);
|
//console.log(data.user_frame.frame, this.netplay.current_frame);
|
||||||
}
|
}
|
||||||
if (data.shortPause === this.netplay.playerID) {
|
if (data.shortPause === this.netplay.playerID) {
|
||||||
this.pause(true);
|
this.pause(true);
|
||||||
setTimeout(() => this.play(true), 5);
|
setTimeout(() => this.play(true), 5);
|
||||||
|
} else if (data.lessShortPause === this.netplay.playerID) {
|
||||||
|
this.pause(true);
|
||||||
|
setTimeout(() => this.play(true), 10);
|
||||||
}
|
}
|
||||||
if (data.input && this.netplay.owner) {
|
if (data.input && this.netplay.owner) {
|
||||||
this.netplay.simulateInput(this.netplay.getUserIndex(data.user), data.input[0], data.input[1], true);
|
this.netplay.simulateInput(this.netplay.getUserIndex(data.user), data.input[0], data.input[1], true);
|
||||||
|
@ -3394,16 +3423,24 @@ class EmulatorJS {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const diff = this.netplay.current_frame - this.netplay.users[k];
|
const diff = this.netplay.current_frame - this.netplay.users[k];
|
||||||
if (Math.abs(diff) > 50 || diff < -5) {
|
//console.log(diff);
|
||||||
|
if (Math.abs(diff) > 75) {
|
||||||
this.netplay.sync();
|
this.netplay.sync();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//console.log(diff);
|
|
||||||
//this'll be adjusted if needed
|
//this'll be adjusted if needed
|
||||||
|
if (diff < 0) {
|
||||||
|
this.netplay.sendMessage({
|
||||||
|
lessShortPause: k
|
||||||
|
})
|
||||||
|
}
|
||||||
if (diff < 5) {
|
if (diff < 5) {
|
||||||
this.netplay.sendMessage({
|
this.netplay.sendMessage({
|
||||||
shortPause: k
|
shortPause: k
|
||||||
})
|
})
|
||||||
|
} else if (diff > 30) {
|
||||||
|
this.pause(true);
|
||||||
|
setTimeout(() => this.play(true), 10);
|
||||||
} else if (diff > 10) {
|
} else if (diff > 10) {
|
||||||
this.pause(true);
|
this.pause(true);
|
||||||
setTimeout(() => this.play(true), 5);
|
setTimeout(() => this.play(true), 5);
|
||||||
|
|
Loading…
Reference in a new issue