-
-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
89 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,12 @@ | ||
import type { IAdapter } from './adapter'; | ||
import { setPlayerAdapterContext } from './context'; | ||
import type { Adapter } from './Adapter'; | ||
|
||
export class Player<T> { | ||
public readonly adapter: IAdapter<T>; | ||
public constructor(adapter: PlayerAdapterInterface<T>) { | ||
this.adapter = setPlayerAdapterContext(this, adapter); | ||
public constructor(public readonly adapter: Adapter<T>) { | ||
this.adapter = adapter; | ||
this.adapter.setPlayer(this); | ||
} | ||
} | ||
|
||
export type PlayerAdapterInterface<T> = () => IAdapter<T>; | ||
|
||
export function createPlayer<T>(adapter: PlayerAdapterInterface<IAdapter<T>>) { | ||
export function createPlayer<T>(adapter: Adapter<T>) { | ||
return new Player(adapter); | ||
} |
This file was deleted.
Oops, something went wrong.
Empty file.
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,48 @@ | ||
import { randomInt } from 'crypto'; | ||
|
||
export class ArrayList<T> extends Array<T> { | ||
private _prev: T[] = []; | ||
|
||
public random() { | ||
return this[randomInt(this.length)]; | ||
} | ||
|
||
public flush() { | ||
this._prev = []; | ||
} | ||
|
||
public canUnshuffle() { | ||
return this._prev.length > 0; | ||
} | ||
|
||
public shuffle(inPlace = true) { | ||
const len = this.length; | ||
|
||
if (inPlace) { | ||
this._prev = this.slice(); | ||
} else { | ||
this._prev = []; | ||
} | ||
|
||
const list = inPlace ? this : this.slice(); | ||
|
||
for (let i = 0; i < len; i++) { | ||
const j = Math.floor(Math.random() * len); | ||
[list[i], list[j]] = [list[j], list[i]]; | ||
} | ||
} | ||
|
||
public unshuffle() { | ||
if (!this._prev.length) return false; | ||
|
||
this.splice(0, this.length, ...this._prev); | ||
this._prev = []; | ||
|
||
return true; | ||
} | ||
|
||
public clear(): void { | ||
this.flush(); | ||
this.length = 0; | ||
} | ||
} |
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 { ArrayList } from './ArrayList'; | ||
|
||
export class Queue<T> extends ArrayList<T> { | ||
public add(...items: T[]): void { | ||
this.push(...items); | ||
} | ||
|
||
public next(): T | undefined { | ||
return this.shift(); | ||
} | ||
|
||
public peek(): T | undefined { | ||
return this[0]; | ||
} | ||
} |
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 { ArrayList } from './ArrayList'; | ||
|
||
export class Stack<T> extends ArrayList<T> { | ||
public add(...items: T[]): void { | ||
this.unshift(...items); | ||
} | ||
|
||
public next(): T | undefined { | ||
return this.shift(); | ||
} | ||
|
||
public peek(): T | undefined { | ||
return this[0]; | ||
} | ||
} |