-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minimal working multiplayer implementation
- Loading branch information
Marvin Schürz
committed
Jan 17, 2025
1 parent
fc17d95
commit 5c1c73a
Showing
35 changed files
with
315 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
packages/core/src/editor/multiplayer/MultiplayerClient.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import type { ClientMessages, IMutation, InitialStateServerMessage, MutationContext, MutationsSubmittedMessage, ServerMessages } from '@osucad/multiplayer'; | ||
import type { Socket } from 'socket.io-client'; | ||
import type { Beatmap } from '../../beatmap/Beatmap'; | ||
import type { FileStore } from '../../beatmap/io/FileStore'; | ||
import { Component } from '@osucad/framework'; | ||
import { MutationSource, UpdateHandler } from '@osucad/multiplayer'; | ||
import { io } from 'socket.io-client'; | ||
import { SimpleFile } from '../../beatmap/io/SimpleFile'; | ||
import { StaticFileStore } from '../../beatmap/io/StaticFileStore'; | ||
import { RulesetStore } from '../../rulesets/RulesetStore'; | ||
|
||
export class MultiplayerClient extends Component { | ||
constructor(url: string) { | ||
super(); | ||
this.ws = io(url, { | ||
autoConnect: false, | ||
transports: ['websocket'], | ||
}); | ||
|
||
this.ws.on('mutationsSubmitted', msg => this.mutationSubmitted(msg)); | ||
} | ||
|
||
updateHandler!: UpdateHandler; | ||
|
||
readonly ws: Socket<ServerMessages, ClientMessages>; | ||
|
||
clientId!: number; | ||
|
||
beatmap!: Beatmap<any>; | ||
|
||
fileStore!: FileStore; | ||
|
||
protected override loadComplete() { | ||
super.loadComplete(); | ||
|
||
this.scheduler.addDelayed(() => this.flush(), 25, true); | ||
} | ||
|
||
async connect() { | ||
this.ws.connect(); | ||
|
||
const initialState = await new Promise<InitialStateServerMessage>((resolve, reject) => { | ||
this.ws.once('connect_error', reject); | ||
this.ws.once('initialData', resolve); | ||
}); | ||
|
||
this.clientId = initialState.clientId; | ||
|
||
const rulesetInfo = RulesetStore.getByShortName(initialState.beatmap.ruleset); | ||
if (!rulesetInfo || !rulesetInfo.available) | ||
throw new Error(`Ruleset "${initialState.beatmap.ruleset}" not available`); | ||
|
||
const ruleset = rulesetInfo.createInstance(); | ||
|
||
this.beatmap = ruleset.createBeatmap(); | ||
this.beatmap.beatmapInfo.ruleset = rulesetInfo; | ||
this.beatmap.initializeFromSummary(initialState.beatmap.data); | ||
|
||
this.updateHandler = new UpdateHandler(this.beatmap); | ||
this.updateHandler.attach(this.beatmap); | ||
|
||
const assets = initialState.assets.map(it => new SimpleFile( | ||
it.path, | ||
() => fetch(`http://localhost:3000/assets/${it.id}`).then(it => it.arrayBuffer()), | ||
)); | ||
|
||
this.fileStore = new StaticFileStore(assets); | ||
|
||
this.updateHandler.commandApplied.addListener(mutation => this.onLocalMutation(mutation)); | ||
} | ||
|
||
private mutationSubmitted(msg: MutationsSubmittedMessage) { | ||
const ctx: MutationContext = { | ||
version: msg.version, | ||
source: msg.clientId === this.clientId | ||
? MutationSource.Ack | ||
: MutationSource.Remote, | ||
}; | ||
|
||
console.log(msg.mutations); | ||
|
||
for (const mutation of msg.mutations) | ||
this.updateHandler.apply(mutation, ctx); | ||
} | ||
|
||
bufferedMutations: IMutation[] = []; | ||
|
||
onLocalMutation(mutation: IMutation) { | ||
this.bufferedMutations.push(mutation); | ||
} | ||
|
||
flush() { | ||
if (this.bufferedMutations.length === 0) | ||
return; | ||
|
||
this.ws.emit('submitMutations', { | ||
version: this.updateHandler.version++, | ||
mutations: this.bufferedMutations, | ||
}); | ||
this.bufferedMutations = []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { ReadonlyDependencyContainer } from '@osucad/framework'; | ||
import type { MultiplayerEditorBeatmap } from './MultiplayerEditorBeatmap'; | ||
import { Editor } from '../Editor'; | ||
|
||
export class MultiplayerEditor extends Editor { | ||
constructor(editorBeatmap: MultiplayerEditorBeatmap) { | ||
super(editorBeatmap); | ||
} | ||
|
||
protected override async loadAsync(dependencies: ReadonlyDependencyContainer): Promise<void> { | ||
this.addInternal((this.editorBeatmap as MultiplayerEditorBeatmap).client); | ||
|
||
return await super.loadAsync(dependencies); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/core/src/editor/multiplayer/MultiplayerEditorBeatmap.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { UpdateHandler } from '@osucad/multiplayer'; | ||
import type { MultiplayerClient } from './MultiplayerClient'; | ||
import { EditorBeatmap } from '../EditorBeatmap'; | ||
|
||
export class MultiplayerEditorBeatmap extends EditorBeatmap { | ||
constructor( | ||
readonly client: MultiplayerClient, | ||
) { | ||
super(client.beatmap, client.fileStore); | ||
} | ||
|
||
protected override createUpdateHandler(): UpdateHandler { | ||
return this.client.updateHandler; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './MultiplayerClient'; | ||
export * from './MultiplayerEditor'; | ||
export * from './MultiplayerEditorBeatmap'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.