Skip to content

Commit

Permalink
end game
Browse files Browse the repository at this point in the history
  • Loading branch information
tailuge committed Oct 20, 2023
1 parent 67b9da5 commit 7ea364a
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 21 deletions.
4 changes: 2 additions & 2 deletions dist/diagram.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/index.js

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion src/controller/end.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
import { ChatEvent } from "../events/chatevent"
import { Controller } from "./controller"

export class End extends Controller {}
export class End extends Controller {
constructor(container) {
super(container)
}

override handleChat(chatevent: ChatEvent): Controller {
const sender = chatevent.sender ? `${chatevent.sender}:` : ""
const message = `${sender} ${chatevent.message}`
this.container.chat.showMessage(message)
return this
}
}
8 changes: 8 additions & 0 deletions src/controller/rules/nineball.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { Aim } from "../../controller/aim"
import { Controller } from "../../controller/controller"
import { PlaceBall } from "../../controller/placeball"
import { WatchAim } from "../../controller/watchaim"
import { ChatEvent } from "../../events/chatevent"
import { PlaceBallEvent } from "../../events/placeballevent"
import { WatchEvent } from "../../events/watchevent"
import { Ball } from "../../model/ball"
import { Outcome } from "../../model/outcome"
import { Table } from "../../model/table"
import { Rack } from "../../utils/rack"
import { zero } from "../../utils/utils"
import { End } from "../end"
import { Rules } from "./rules"

export class NineBall implements Rules {
Expand Down Expand Up @@ -43,6 +45,12 @@ export class NineBall implements Rules {
}
if (Outcome.isBallPottedNoFoul(table.cueball, outcome)) {
this.container.sound.playSuccess(table.inPockets())
if (this.isEndOfGame(outcome)) {
console.log("end of game")
this.container.eventQueue.push(new ChatEvent(null, `game over`))
this.container.recoder.wholeGameLink()
return new End(this.container)
}
this.container.sendEvent(new WatchEvent(table.serialise()))
return new Aim(this.container)
}
Expand Down
30 changes: 19 additions & 11 deletions src/events/recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ export class Recorder {
}
}

replayGame() {
wholeGame() {
return this.state(this.states[0], this.shots)
}

replayLastShot() {
lastShot() {
const last = this.states.length - 1
return this.state(this.states[last], [this.shots[last]])
}

replayCurrentBreak() {
currentBreak() {
if (this.breakStart !== undefined) {
return this.state(
this.states[this.breakStart],
Expand All @@ -53,13 +53,13 @@ export class Recorder {
const isEndOfGame = this.container.rules.isEndOfGame(outcome)
const potCount = Outcome.potCount(outcome)
if (!isPartOfBreak) {
this.replayBreakLink(isEndOfGame)
this.breakLink(isEndOfGame)
}

this.replayLastShotLink(isPartOfBreak || isEndOfGame, potCount)
this.lastShotLink(isPartOfBreak || isEndOfGame, potCount)

if (isEndOfGame) {
this.replayBreakLink(isEndOfGame)
this.breakLink(isEndOfGame)
}

if (!isPartOfBreak) {
Expand All @@ -72,15 +72,15 @@ export class Recorder {
}
}

replayLastShotLink(isPartOfBreak, potCount) {
lastShotLink(isPartOfBreak, potCount) {
const pots = potCount > 1 ? potCount - 1 : 0
const shotIcon = "⚈".repeat(pots) + (isPartOfBreak ? "⚈" : "⚆")
const serialisedShot = JSON.stringify(this.replayLastShot())
const serialisedShot = JSON.stringify(this.lastShot())
this.generateLink(shotIcon, serialisedShot)
}

replayBreakLink(includeLastShot) {
const currentBreak = this.replayCurrentBreak()
breakLink(includeLastShot) {
const currentBreak = this.currentBreak()
if (!currentBreak) {
return
}
Expand All @@ -96,7 +96,15 @@ export class Recorder {
this.generateLink(text, compressed)
}

generateLink(text, state) {
wholeGameLink() {
const game = this.wholeGame()
const text = `game(${game.shots.length})`
const serialisedGame = JSON.stringify(game)
const compressed = JSONCrush.crush(serialisedGame)
this.generateLink(text, compressed)
}

private generateLink(text, state) {
const shotUri = `${this.replayUrl}${encodeURIComponent(state)}`
const shotLink = `<a class="pill" target="_blank" href="${shotUri}">${text}</a>`
this.container.eventQueue.push(new ChatEvent(null, `${shotLink}`))
Expand Down
6 changes: 3 additions & 3 deletions test/events/recorder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ describe("Recorder", () => {
const recorder = new Recorder(container)
const event: HitEvent = new HitEvent(container.table.serialise())
recorder.record(event)
expect(recorder.replayGame()).to.be.not.null
const replay = recorder.replayGame()
expect(JSON.stringify(recorder.replayLastShot())).to.equals(
expect(recorder.wholeGame()).to.be.not.null
const replay = recorder.wholeGame()
expect(JSON.stringify(recorder.lastShot())).to.equals(
JSON.stringify(replay)
)
done()
Expand Down
3 changes: 1 addition & 2 deletions test/gen/diamon.spec.ts → test/gen/diamond.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ describe("Controller", () => {
const event: HitEvent = new HitEvent(container.table.serialise())
container.recoder.record(event)
container.recoder.updateBreak([])

const state = container.recoder.replayLastShot()
const state = container.recoder.lastShot()
const shotUri = `${replayUrl}${encodeURIComponent(JSON.stringify(state))}`
return shotUri
}
Expand Down

0 comments on commit 7ea364a

Please sign in to comment.