Compare commits

..

No commits in common. "d37c6a5cc5e2ce64cc74027ee8e52cb72ccf22d9" and "5688e499fc1a27b1151da3d904aee9328a46bc8b" have entirely different histories.

2 changed files with 6 additions and 114 deletions

View file

@ -1,87 +1,16 @@
import { PackBuilder, Cube, DraftOptions } from "."; import { PackBuilder, Cube, DraftOptions } from ".";
import EventEmitter from "eventemitter3";
import { Card } from "@/mlpccg";
export class Session { export class Session {
private options: DraftOptions; private options: DraftOptions;
private factory: PackBuilder; private factory: PackBuilder;
private pod: SessionPlayer[];
private players: string[];
private pending: number[];
constructor(options: DraftOptions, factory: PackBuilder) { constructor(options: DraftOptions, factory: PackBuilder) {
this.options = options; this.options = options;
this.factory = factory; this.factory = factory;
this.players = [];
this.pending = [];
this.pod = new Array(options.players).fill(0).map((x, i) => {
const player = new SessionPlayer();
player.on("pick", this.picked.bind(this, i));
return player;
});
}
public start() {
// Figure out how many players there are vs spots to be filled
const spots = this.options.players;
const players = this.players.length;
if (players > spots) {
throw new Error("too many players in the pod");
}
if (players < 1) {
throw new Error("not enough players");
}
// Place players in the pod
switch (this.options.spacing) {
case "evenly":
const playerRatio = spots / players;
let i = 0;
for (const player of this.players) {
const pos = Math.floor(playerRatio * i);
this.pod[pos].name = name;
i += 1;
}
break;
case "randomly":
for (const player of this.players) {
while (true) {
const idx = Math.floor(Math.random() * spots);
if (!this.pod[idx].name) {
this.pod[idx].name = player;
break;
}
}
}
break;
}
//TODO
}
public get order(): string[] {
return this.pod.map(p => p.name || "bot");
}
private picked(playerIndex: number, card: string) {
if (!this.pending.includes(playerIndex)) {
// Uh oh.
throw new Error(
`unexpected pick: player #${playerIndex} already picked their card`
);
}
const idx = this.pending.indexOf(playerIndex);
this.pending.splice(idx, 1);
// Check if everyone picked their card
if (this.pending.length < 1) {
//TODO
//NEXT PACK
}
} }
static async create(options: DraftOptions): Promise<Session> { static async create(options: DraftOptions): Promise<Session> {
switch (options.source) { switch (options.type) {
case "set": { case "set": {
const factory = await PackBuilder.fromSet(options.set); const factory = await PackBuilder.fromSet(options.set);
return new Session(options, factory); return new Session(options, factory);
@ -95,24 +24,6 @@ export class Session {
} }
case "i8pcube": case "i8pcube":
throw new Error("not implemented"); throw new Error("not implemented");
default:
throw new Error("Unknown draft source");
} }
} }
} }
export class SessionPlayer extends EventEmitter {
public name?: string;
public currentPack?: Card[];
public next?: SessionPlayer;
public pick(card: string) {
if (!this.currentPack) {
throw new Error("no pack to pick from");
}
if (!this.currentPack.find(c => c.ID == card)) {
throw new Error("card not in available picks");
}
this.emit("pick", card);
}
}

View file

@ -20,46 +20,27 @@ export interface AlternateProvider {
} }
export interface SetDraftOptions { export interface SetDraftOptions {
source: "set"; type: "set";
set: string; set: string;
} }
export interface BlockDraftOptions { export interface BlockDraftOptions {
source: "block"; type: "block";
block: string; block: string;
} }
export interface CubeDraftOptions { export interface CubeDraftOptions {
source: "cube"; type: "cube";
url: string; url: string;
} }
export interface I8PCubeDraftOptions { export interface I8PCubeDraftOptions {
source: "i8pcube"; type: "i8pcube";
url: string; url: string;
} }
export interface LimitedBoosterDraft { export type DraftOptions =
type: "booster-draft";
packs: number;
}
export interface LimitedSealedDraft {
type: "sealed";
packs: number;
}
export type LimitedGameType = LimitedBoosterDraft | LimitedSealedDraft;
export type DraftType =
| SetDraftOptions | SetDraftOptions
| BlockDraftOptions | BlockDraftOptions
| CubeDraftOptions | CubeDraftOptions
| I8PCubeDraftOptions; | I8PCubeDraftOptions;
export interface SessionOptions {
players: number;
spacing: "evenly" | "randomly";
}
export type DraftOptions = SessionOptions & LimitedGameType & DraftType;