| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4e8d11e commit 6cdf8c7
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,7 +29,8 @@ class EJS_GameManager { | |||
| 29 | 29 | toggleRewind: this.Module.cwrap('toggle_rewind', 'null', ['number']), | |
| 30 | 30 | setRewindGranularity: this.Module.cwrap('set_rewind_granularity', 'null', ['number']), | |
| 31 | 31 | 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', ['']) | ||
| 33 | 34 | } | |
| 34 | 35 | this.mkdir("/home"); | |
| 35 | 36 | this.mkdir("/home/web_user"); | |
@@ -327,6 +328,9 @@ class EJS_GameManager { | |||
| 327 | 328 | setRewindGranularity(value) { | |
| 328 | 329 | this.functions.setRewindGranularity(value); | |
| 329 | 330 | } | |
| 331 | + getFrameNum() { | ||
| 332 | + return this.functions.getFrameNum(); | ||
| 333 | + } | ||
| 330 | 334 | } | |
| 331 | 335 | ||
| 332 | 336 | window.EJS_GameManager = EJS_GameManager; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4394,7 +4394,11 @@ class EmulatorJS { | |||
| 4394 | 4394 | let justReset = false; | |
| 4395 | 4395 | this.netplay.dataMessage = (data) => { | |
| 4396 | 4396 | //console.log(data); | |
| 4397 | + if (data.sync === true && this.netplay.owner) { | ||
| 4398 | + this.netplay.sync(); | ||
| 4399 | + } | ||
| 4397 | 4400 | if (data.state) { | |
| 4401 | + this.netplay.wait = true; | ||
| 4398 | 4402 | this.netplay.setLoading(true); | |
| 4399 | 4403 | this.pause(true); | |
| 4400 | 4404 | this.gameManager.loadState(new Uint8Array(data.state)); | |
@@ -4409,130 +4413,139 @@ class EmulatorJS { | |||
| 4409 | 4413 | if (data.ready && this.netplay.owner) { | |
| 4410 | 4414 | this.netplay.ready++; | |
| 4411 | 4415 | 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); | ||
| 4414 | 4419 | this.netplay.setLoading(false); | |
| 4415 | - this.netplay.current_frame = 0; | ||
| 4416 | - justReset = true; | ||
| 4417 | 4420 | } | |
| 4418 | 4421 | } | |
| 4419 | 4422 | if (data.readyready) { | |
| 4420 | 4423 | this.netplay.setLoading(false); | |
| 4421 | - this.netplay.current_frame = 0; | ||
| 4422 | - this.play(true) | ||
| 4423 | - } | ||
| 4424 | - if (data.resetCurrentFrame) { | ||
| 4424 | + this.netplay.reset(); | ||
| 4425 | 4425 | 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); | ||
| 4437 | 4426 | } | |
| 4438 | - if (data.shortPause === this.netplay.playerID) { | ||
| 4427 | + if (data.shortPause && data.shortPause !== this.netplay.playerID) { | ||
| 4439 | 4428 | 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 | + }); | ||
| 4453 | 4463 | } | |
| 4454 | 4464 | if (data.restart) { | |
| 4455 | 4465 | this.gameManager.restart(); | |
| 4456 | - this.netplay.current_frame = 0; | ||
| 4457 | - this.netplay.inputs = {}; | ||
| 4466 | + this.netplay.reset(); | ||
| 4458 | 4467 | this.play(true); | |
| 4459 | 4468 | } | |
| 4460 | 4469 | } | |
| 4461 | 4470 | this.netplay.simulateInput = (player, index, value, resp) => { | |
| 4462 | 4471 | if (!this.isNetplay) return; | |
| 4463 | 4472 | if (player !== 0 && !resp) return; | |
| 4473 | + player = this.netplay.getUserIndex(this.netplay.playerID) | ||
| 4474 | + const frame = this.netplay.currentFrame; | ||
| 4464 | 4475 | 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({ | ||
| 4468 | 4480 | frame: frame, | |
| 4469 | 4481 | connected_input: [player, index, value] | |
| 4470 | - }); | ||
| 4482 | + }) | ||
| 4483 | + this.gameManager.functions.simulateInput(player, index, value); | ||
| 4471 | 4484 | } else { | |
| 4472 | 4485 | 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 | + }) | ||
| 4476 | 4491 | } | |
| 4477 | 4492 | } | |
| 4478 | 4493 | this.netplay.sendMessage = (data) => { | |
| 4479 | 4494 | this.netplay.socket.emit("data-message", data); | |
| 4480 | 4495 | } | |
| 4496 | + this.netplay.reset = () => { | ||
| 4497 | + this.netplay.init_frame = this.netplay.currentFrame; | ||
| 4498 | + this.netplay.inputsData = {}; | ||
| 4499 | + } | ||
| 4481 | 4500 | //let fps; | |
| 4482 | 4501 | //let lastTime; | |
| 4502 | + this.netplay.init_frame = 0; | ||
| 4503 | + this.netplay.currentFrame = 0; | ||
| 4504 | + this.netplay.inputsData = {}; | ||
| 4483 | 4505 | this.Module.postMainLoop = () => { | |
| 4484 | 4506 | //const newTime = window.performance.now(); | |
| 4485 | 4507 | //fps = 1000 / (newTime - lastTime); | |
| 4486 | 4508 | //console.log(fps); | |
| 4487 | 4509 | //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; | ||
| 4490 | 4515 | 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}); | ||
| 4519 | 4521 | } | |
| 4522 | + this.netplay.sendMessage({"sync-control": to_send}); | ||
| 4520 | 4523 | } 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; | ||
| 4534 | 4537 | } | |
| 4535 | 4538 | } | |
| 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 | + | ||
| 4536 | 4549 | } | |
| 4537 | 4550 | ||
| 4538 | 4551 | this.netplay.updateList = { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments