FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Netplay work · SossLab/EmulatorJS@6cdf8c7 · GitHub

Commit 6cdf8c7

Browse files
committed
Netplay work
1 parent 4e8d11e commit 6cdf8c7

3 files changed

Lines changed: 103 additions & 86 deletions

File tree

‎data/GameManager.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class EJS_GameManager {
2929
toggleRewind: this.Module.cwrap('toggle_rewind', 'null', ['number']),
3030
setRewindGranularity: this.Module.cwrap('set_rewind_granularity', 'null', ['number']),
3131
toggleSlowMotion: this.Module.cwrap('toggle_slow_motion', 'null', ['number']),
32-
setSlowMotionRatio: this.Module.cwrap('set_sm_ratio', 'null', ['number'])
32+
setSlowMotionRatio: this.Module.cwrap('set_sm_ratio', 'null', ['number']),
33+
getFrameNum: this.Module.cwrap('get_current_frame_count', 'number', [''])
3334
}
3435
this.mkdir("/home");
3536
this.mkdir("/home/web_user");
@@ -327,6 +328,9 @@ class EJS_GameManager {
327328
setRewindGranularity(value) {
328329
this.functions.setRewindGranularity(value);
329330
}
331+
getFrameNum() {
332+
return this.functions.getFrameNum();
333+
}
330334
}
331335

332336
window.EJS_GameManager = EJS_GameManager;

‎data/cores/fceumm-wasm.data‎

7.26 KB
Binary file not shown.

‎data/emulator.js‎

Lines changed: 98 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -4394,7 +4394,11 @@ class EmulatorJS {
43944394
let justReset = false;
43954395
this.netplay.dataMessage = (data) => {
43964396
//console.log(data);
4397+
if (data.sync === true && this.netplay.owner) {
4398+
this.netplay.sync();
4399+
}
43974400
if (data.state) {
4401+
this.netplay.wait = true;
43984402
this.netplay.setLoading(true);
43994403
this.pause(true);
44004404
this.gameManager.loadState(new Uint8Array(data.state));
@@ -4409,130 +4413,139 @@ class EmulatorJS {
44094413
if (data.ready && this.netplay.owner) {
44104414
this.netplay.ready++;
44114415
if (this.netplay.ready === this.netplay.getUserCount()) {
4412-
this.netplay.sendMessage({readyready:true, resetCurrentFrame: true});
4413-
setTimeout(() => this.play(true), 100);
4416+
this.netplay.sendMessage({readyready:true});
4417+
this.netplay.reset();
4418+
this.play(true);
44144419
this.netplay.setLoading(false);
4415-
this.netplay.current_frame = 0;
4416-
justReset = true;
44174420
}
44184421
}
44194422
if (data.readyready) {
44204423
this.netplay.setLoading(false);
4421-
this.netplay.current_frame = 0;
4422-
this.play(true)
4423-
}
4424-
if (data.resetCurrentFrame) {
4424+
this.netplay.reset();
44254425
this.play(true);
4426-
this.netplay.current_frame = 0;
4427-
this.netplay.inputs = {};
4428-
}
4429-
if (data.user_frame && this.netplay.owner) {
4430-
if (justReset) {
4431-
justReset = false;
4432-
this.netplay.current_frame = 0;
4433-
this.netplay.inputs = {};
4434-
}
4435-
this.netplay.users[data.user_frame.user] = data.user_frame.frame;
4436-
//console.log(data.user_frame.frame, this.netplay.current_frame);
44374426
}
4438-
if (data.shortPause === this.netplay.playerID) {
4427+
if (data.shortPause && data.shortPause !== this.netplay.playerID) {
44394428
this.pause(true);
4440-
setTimeout(() => this.play(true), 5);
4441-
} else if (data.lessShortPause === this.netplay.playerID) {
4442-
this.pause(true);
4443-
setTimeout(() => this.play(true), 10);
4444-
}
4445-
if (data.input && this.netplay.owner) {
4446-
this.netplay.simulateInput(this.netplay.getUserIndex(data.user), data.input[0], data.input[1], true);
4447-
}
4448-
if (data.connected_input && !this.netplay.owner) {
4449-
if (!this.netplay.inputs[data.frame]) {
4450-
this.netplay.inputs[data.frame] = [];
4451-
}
4452-
this.netplay.inputs[data.frame].push([data.connected_input[0], data.connected_input[1], data.connected_input[2]]);
4429+
this.netplay.wait = true;
4430+
setTimeout(() => this.play(true), 48);
4431+
}
4432+
if (data["sync-control"]) {
4433+
data["sync-control"].forEach((value) => {
4434+
let inFrame = parseInt(value.frame);
4435+
let frame = this.netplay.currentFrame;
4436+
this.netplay.inputsData[inFrame] || (this.netplay.inputsData[inFrame] = []);
4437+
if (!value.connected_input || value.connected_input[0] < 0) return;
4438+
if (inFrame === frame) {
4439+
this.gameManager.functions.simulateInput(value.connected_input[0], value.connected_input[1], value.connected_input[2]);
4440+
}
4441+
this.netplay.inputsData[frame] || (this.netplay.inputsData[frame] = []);
4442+
if (this.netplay.owner) {
4443+
this.netplay.inputsData[frame].push(value);
4444+
this.gameManager.functions.simulateInput(value.connected_input[0], value.connected_input[1], value.connected_input[2]);
4445+
if (frame - 10 >= inFrame) {
4446+
this.netplay.wait = true;
4447+
this.pause(true);
4448+
setTimeout(() => {
4449+
this.play(true);
4450+
this.netplay.wait = false;
4451+
}, 48)
4452+
}
4453+
} else {
4454+
this.netplay.inputsData[inFrame].push(value);
4455+
if (this.netplay.inputsData[frame]) {
4456+
this.play(true);
4457+
}
4458+
if (frame + 10 <= inFrame && inFrame > this.netplay.init_frame + 100) {
4459+
this.netplay.sendMessage({shortPause:this.netplay.playerID});
4460+
}
4461+
}
4462+
});
44534463
}
44544464
if (data.restart) {
44554465
this.gameManager.restart();
4456-
this.netplay.current_frame = 0;
4457-
this.netplay.inputs = {};
4466+
this.netplay.reset();
44584467
this.play(true);
44594468
}
44604469
}
44614470
this.netplay.simulateInput = (player, index, value, resp) => {
44624471
if (!this.isNetplay) return;
44634472
if (player !== 0 && !resp) return;
4473+
player = this.netplay.getUserIndex(this.netplay.playerID)
4474+
const frame = this.netplay.currentFrame;
44644475
if (this.netplay.owner) {
4465-
const frame = this.netplay.current_frame;
4466-
this.gameManager.functions.simulateInput(player, index, value);
4467-
this.netplay.sendMessage({
4476+
if (!this.netplay.inputsData[frame]) {
4477+
this.netplay.inputsData[frame] = [];
4478+
}
4479+
this.netplay.inputsData[frame].push({
44684480
frame: frame,
44694481
connected_input: [player, index, value]
4470-
});
4482+
})
4483+
this.gameManager.functions.simulateInput(player, index, value);
44714484
} else {
44724485
this.netplay.sendMessage({
4473-
user: this.netplay.playerID,
4474-
input: [index, value]
4475-
});
4486+
"sync-control": [{
4487+
frame: frame,
4488+
connected_input: [player, index, value]
4489+
}]
4490+
})
44764491
}
44774492
}
44784493
this.netplay.sendMessage = (data) => {
44794494
this.netplay.socket.emit("data-message", data);
44804495
}
4496+
this.netplay.reset = () => {
4497+
this.netplay.init_frame = this.netplay.currentFrame;
4498+
this.netplay.inputsData = {};
4499+
}
44814500
//let fps;
44824501
//let lastTime;
4502+
this.netplay.init_frame = 0;
4503+
this.netplay.currentFrame = 0;
4504+
this.netplay.inputsData = {};
44834505
this.Module.postMainLoop = () => {
44844506
//const newTime = window.performance.now();
44854507
//fps = 1000 / (newTime - lastTime);
44864508
//console.log(fps);
44874509
//lastTime = newTime;
4488-
if (!this.isNetplay || this.paused) return;
4489-
this.netplay.current_frame++;
4510+
4511+
//frame syncing - working
4512+
//control syncing - broken
4513+
if (!this.isNetplay) return;
4514+
this.netplay.currentFrame = parseInt(this.gameManager.getFrameNum()) - this.netplay.init_frame;
44904515
if (this.netplay.owner) {
4491-
for (const k in this.netplay.users) {
4492-
if (this.netplay.getUserIndex(k) === -1) {
4493-
delete this.netplay.users[k];
4494-
continue;
4495-
}
4496-
const diff = this.netplay.current_frame - this.netplay.users[k];
4497-
//console.log(diff);
4498-
if (Math.abs(diff) > 75) {
4499-
this.netplay.sync();
4500-
return;
4501-
}
4502-
//this'll be adjusted if needed
4503-
if (diff < 0) {
4504-
this.netplay.sendMessage({
4505-
lessShortPause: k
4506-
})
4507-
}
4508-
if (diff < 5) {
4509-
this.netplay.sendMessage({
4510-
shortPause: k
4511-
})
4512-
} else if (diff > 30) {
4513-
this.pause(true);
4514-
setTimeout(() => this.play(true), 10);
4515-
} else if (diff > 10) {
4516-
this.pause(true);
4517-
setTimeout(() => this.play(true), 5);
4518-
}
4516+
let to_send = [];
4517+
for (let i=this.netplay.currentFrame-1; i<this.netplay.currentFrame; i++) {
4518+
this.netplay.inputsData[i] ? this.netplay.inputsData[i].forEach((value) => {
4519+
to_send.push(value);
4520+
}) : to_send.push({frame: i});
45194521
}
4522+
this.netplay.sendMessage({"sync-control": to_send});
45204523
} else {
4521-
this.netplay.sendMessage({
4522-
user_frame: {
4523-
user: this.netplay.playerID,
4524-
frame: this.netplay.current_frame
4525-
}
4526-
});
4527-
for (const k in this.netplay.inputs) {
4528-
if (k <= this.netplay.current_frame) {
4529-
this.netplay.inputs[k].forEach(data => {
4530-
this.gameManager.functions.simulateInput(data[0], data[1], data[2]);
4531-
})
4532-
delete this.netplay.inputs[k];
4533-
}
4524+
if (this.netplay.currentFrame <= 0 || this.netplay.inputsData[this.netplay.currentFrame]) {
4525+
this.netplay.wait = false;
4526+
this.play();
4527+
this.netplay.inputsData[this.netplay.currentFrame].forEach((value) => {
4528+
console.log(value);
4529+
if (!value.connected_input) return;
4530+
this.gameManager.functions.simulateInput(value.connected_input[0], value.connected_input[1], value.connected_input[2]);
4531+
})
4532+
} else if (!this.netplay.syncing) {
4533+
console.log("sync");
4534+
this.pause(true);
4535+
this.netplay.sendMessage({sync:true});
4536+
this.netplay.syncing = true;
45344537
}
45354538
}
4539+
if (this.netplay.currentFrame % 100 === 0) {
4540+
Object.keys(this.netplay.inputsData).forEach(value => {
4541+
if (value < this.netplay.currentFrame - 50) {
4542+
this.netplay.inputsData[value] = null;
4543+
delete this.netplay.inputsData[value];
4544+
}
4545+
})
4546+
}
4547+
4548+
45364549
}
45374550

45384551
this.netplay.updateList = {

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL