-
Notifications
You must be signed in to change notification settings - Fork 25
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
168 additions
and
18 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,19 @@ | ||
import { FourteenOne } from "./fourteenone" | ||
import { NineBall } from "./nineball" | ||
import { Snooker } from "./snooker" | ||
import { ThreeCushion } from "./threecushion" | ||
|
||
export class RuleFactory { | ||
static create(ruletype, container) { | ||
if (ruletype === "threecushion") { | ||
return new ThreeCushion(container) | ||
switch (ruletype) { | ||
case "threecushion": | ||
return new ThreeCushion(container) | ||
case "fourteenone": | ||
return new FourteenOne(container) | ||
case "snooker": | ||
return new Snooker(container) | ||
default: | ||
return new NineBall(container) | ||
} | ||
if (ruletype === "fourteenone") { | ||
return new FourteenOne(container) | ||
} | ||
return new NineBall(container) | ||
} | ||
} |
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,71 @@ | ||
import { WatchEvent } from "../../events/watchevent" | ||
import { Ball, State } from "../../model/ball" | ||
import { Outcome, OutcomeType } from "../../model/outcome" | ||
import { R } from "../../model/physics/constants" | ||
import { Table } from "../../model/table" | ||
import { Rack } from "../../utils/rack" | ||
import { TableGeometry } from "../../view/tablegeometry" | ||
import { Controller } from "../controller" | ||
import { NineBall } from "./nineball" | ||
import { Rules } from "./rules" | ||
|
||
export class Snooker extends NineBall implements Rules { | ||
override rack() { | ||
return Rack.snooker() | ||
} | ||
|
||
override update(outcome: Outcome[]): Controller { | ||
const table = this.container.table | ||
if (Outcome.potCount(outcome) > 0) { | ||
if ( | ||
this.redsOnTable(table) || | ||
Outcome.isCueBallPotted(table.cueball, outcome) | ||
) { | ||
if (this.respotAllPottedColours(outcome)) { | ||
const state = table.serialise() | ||
const rerack = new WatchEvent({ ...state, rerack: true }) | ||
this.container.sendEvent(rerack) | ||
this.container.recoder.record(rerack) | ||
} | ||
} | ||
} | ||
return super.update(outcome) | ||
} | ||
|
||
redsOnTable(table: Table) { | ||
return table.balls.filter((b, i) => i > 6 && b.onTable()).length > 0 | ||
} | ||
|
||
respotAllPottedColours(outcome: Outcome[]) { | ||
return outcome | ||
.filter((o) => o.type === OutcomeType.Pot) | ||
.map((o) => o.ballA!) | ||
.filter((ball) => ball.id < 7) | ||
.filter((ball) => ball.id !== 0) | ||
.map((ball) => this.respot(ball)) | ||
.some((e) => e === true) | ||
} | ||
|
||
respot(ball: Ball) { | ||
const positions = Rack.snookerColourPositions() | ||
positions.push(positions[ball.id - 1]) | ||
positions.reverse() | ||
// add positions as close as possible to spot behind, then infront | ||
let pos = positions[0].clone() | ||
for (let x = pos.x; x < TableGeometry.tableX; x += R / 4) { | ||
positions.push(pos.setX(x).clone()) | ||
} | ||
pos = positions[0].clone() | ||
for (let x = pos.x; x > -TableGeometry.tableX; x -= R / 4) { | ||
positions.push(pos.setX(x).clone()) | ||
} | ||
return positions.some((p) => { | ||
if (!this.container.table.overlapsAny(p, ball)) { | ||
ball.pos.copy(p) | ||
ball.state = State.Stationary | ||
return true | ||
} | ||
return false | ||
}) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import "mocha" | ||
import { expect } from "chai" | ||
import { Container } from "../../src/container/container" | ||
import { GameEvent } from "../../src/events/gameevent" | ||
import { initDom } from "../view/dom" | ||
|
||
initDom() | ||
|
||
const jestConsole = console | ||
|
||
beforeEach(() => { | ||
global.console = require("console") | ||
}) | ||
|
||
afterEach(() => { | ||
global.console = jestConsole | ||
}) | ||
|
||
describe("Snooker", () => { | ||
let container: Container | ||
let broadcastEvents: GameEvent[] | ||
const rule = "snooker" | ||
|
||
beforeEach(function (done) { | ||
container = new Container(undefined, (_) => {}, false, rule) | ||
broadcastEvents = [] | ||
container.broadcast = (x) => broadcastEvents.push(x) | ||
done() | ||
}) | ||
|
||
it("Fourteenone has 13 balls", (done) => { | ||
expect(container.table.balls).to.be.length(13) | ||
done() | ||
}) | ||
}) |