multi: Restored spectator mode
This commit is contained in:
parent
81fe35fcbb
commit
5599f7576d
|
@ -153,7 +153,7 @@ module TK.SpaceTac.UI {
|
|||
// If we are on a remote session, start the exchange
|
||||
if (!this.session.primary && this.gameui.session_token) {
|
||||
// TODO handle errors or timeout
|
||||
this.multi.setup(this, this.battle, this.gameui.session_token, false);
|
||||
this.multi.setup(this, this.actual_battle, this.gameui.session_token, false);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,9 @@ module TK.SpaceTac.UI {
|
|||
* Tool to synchronize two players sharing a battle over network
|
||||
*/
|
||||
export class MultiBattle {
|
||||
// Debug mode
|
||||
debug = false
|
||||
|
||||
// Network exchange of messages
|
||||
exchange: Multi.Exchange
|
||||
|
||||
|
@ -47,7 +50,7 @@ module TK.SpaceTac.UI {
|
|||
* Background work to maintain the battle state in sync between the two peers
|
||||
*/
|
||||
async backgroundSync() {
|
||||
while (true) {
|
||||
while (true) { // TODO Close the task on exchange close
|
||||
if (this.exchange.writing) {
|
||||
await this.sendDiffs();
|
||||
} else {
|
||||
|
@ -63,14 +66,16 @@ module TK.SpaceTac.UI {
|
|||
if (this.processed >= this.battle.log.count()) {
|
||||
await this.timer.sleep(500);
|
||||
} else {
|
||||
let event = this.battle.log.get(this.processed);
|
||||
let diff = this.battle.log.get(this.processed);
|
||||
this.processed++;
|
||||
|
||||
if (event instanceof ShipActionUsedDiff) {
|
||||
let data = this.serializer.serialize(event);
|
||||
// TODO "over" should be true if the current ship should be played by remote player
|
||||
await this.exchange.writeMessage(data, false);
|
||||
if (this.debug) {
|
||||
console.debug("Send diff to exchange", diff);
|
||||
}
|
||||
|
||||
let data = this.serializer.serialize(diff);
|
||||
// TODO "over" should be true if the current ship should be played by remote player
|
||||
await this.exchange.writeMessage(data, false);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -81,8 +86,11 @@ module TK.SpaceTac.UI {
|
|||
let message = await this.exchange.readMessage();
|
||||
let received = this.serializer.unserialize(message);
|
||||
if (received instanceof BaseBattleDiff) {
|
||||
console.log("Received diff from exchange", received);
|
||||
// TODO Apply the diff
|
||||
if (this.debug) {
|
||||
console.log("Received diff from exchange", received);
|
||||
}
|
||||
|
||||
this.battle.applyDiffs([received]);
|
||||
} else {
|
||||
console.error("Exchange received something that is not a battle diff", received);
|
||||
}
|
||||
|
|
|
@ -63,7 +63,7 @@ module TK.SpaceTac.UI {
|
|||
this.displayMultiplayerToken(token);
|
||||
|
||||
if (this.view instanceof BattleView) {
|
||||
await this.view.multi.setup(this.view, this.view.battle, token, true);
|
||||
await this.view.multi.setup(this.view, this.view.actual_battle, token, true);
|
||||
} else {
|
||||
// TODO
|
||||
this.displayConnectionError();
|
||||
|
|
Loading…
Reference in a new issue