Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/client/transport/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import type {
State,
} from '../../types';
import { getFilterPlayerView } from '../../master/filter-player-view';
import { createMatch } from '../../server/util';
import * as StorageAPI from '../../server/db/base';

/**
* Returns null if it is not a bot's turn.
Expand Down Expand Up @@ -63,6 +65,40 @@ export class LocalMaster extends Master {
callback: (data: TransportData) => void
) => void;

async onSync(
matchID: string,
playerID: string | null | undefined,
credentials?: string,
numPlayers = 2
): Promise<void | { error: string }> {
// If the game doesn't exist, then create one on demand.
// TODO: Move this out of the sync call.
const match = createMatch({
game: this.game,
unlisted: true,
numPlayers: numPlayers,
setupData: undefined,
});

if ('setupDataError' in match) {
return { error: 'game requires setupData' };
}

const state = match.initialState;
const initialState = state;
const metadata = match.metadata;

this.subscribeCallback({ state, matchID });

if (StorageAPI.isSynchronous(this.storageAPI)) {
this.storageAPI.createMatch(matchID, { initialState, metadata });
} else {
await this.storageAPI.createMatch(matchID, { initialState, metadata });
}

super.onSync(matchID, playerID, credentials, numPlayers);
}

constructor({ game, bots, storageKey, persist }: LocalMasterOpts) {
const clientCallbacks: Record<PlayerID, (data: TransportData) => void> = {};
const initializedBots = {};
Expand Down
29 changes: 1 addition & 28 deletions src/master/master.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import type {
PlayerID,
ChatMessage,
} from '../types';
import { createMatch } from '../server/util';
import type { Auth } from '../server/auth';
import * as StorageAPI from '../server/db/base';
import type { Operation } from 'rfc6902';
Expand Down Expand Up @@ -336,7 +335,7 @@ export class Master {
? this.storageAPI.fetch(key, fetchOpts)
: await this.storageAPI.fetch(key, fetchOpts);

let { state, initialState, log, metadata } = fetchResult;
const { state, initialState, log, metadata } = fetchResult;

if (this.auth && playerID !== undefined && playerID !== null) {
const isAuthentic = await this.auth.authenticateCredentials({
Expand All @@ -349,32 +348,6 @@ export class Master {
}
}

// If the game doesn't exist, then create one on demand.
// TODO: Move this out of the sync call.
if (state === undefined) {
const match = createMatch({
game: this.game,
unlisted: true,
numPlayers,
setupData: undefined,
});

if ('setupDataError' in match) {
return { error: 'game requires setupData' };
}

initialState = state = match.initialState;
metadata = match.metadata;

this.subscribeCallback({ state, matchID });

if (StorageAPI.isSynchronous(this.storageAPI)) {
this.storageAPI.createMatch(key, { initialState, metadata });
} else {
await this.storageAPI.createMatch(key, { initialState, metadata });
}
}

const filteredMetadata = metadata ? filterMatchData(metadata) : undefined;

const syncInfo: SyncInfo = {
Expand Down