Skip to content

Commit

Permalink
record 14-1 break
Browse files Browse the repository at this point in the history
  • Loading branch information
tailuge committed Oct 22, 2023
1 parent bc159c9 commit dc7c8c1
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 13 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.

1 change: 1 addition & 0 deletions dist/lobby/lobby.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ <h1>billiards lobby</h1>
><select id="ruletype" size="1">
<option value="threecushion">Three Cushion</option>
<option value="nineball">Nine Ball</option>
<option value="fourteenone">14-1</option>
</select>

<label for="player1">You: </label
Expand Down
4 changes: 3 additions & 1 deletion dist/multi.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ <h1>tailuge/billiards multiplayer test page</h1>
github assets and local websocket server:
<ul>
<li>
<a id="github" href="https://tailuge.github.io/billiards/dist/">single player</a>
<a id="github" href="https://tailuge.github.io/billiards/dist/"
>single player</a
>
</li>
</ul>
github assets and render websocket server:
Expand Down
5 changes: 3 additions & 2 deletions src/controller/playshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ export class PlayShot extends ControllerBase {
override handleStationary(_) {
const table = this.container.table
const outcome = table.outcome
this.container.recoder.updateBreak(outcome)
table.cue.aimAtNext(table)
return this.container.rules.update(outcome)
const nextController = this.container.rules.update(outcome)
this.container.recoder.updateBreak(outcome)
return nextController
}

override handleInput(input: Input): Controller {
Expand Down
18 changes: 16 additions & 2 deletions src/controller/replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import { AimEvent } from "../events/aimevent"
import { Controller, Input } from "./controller"
import { BreakEvent } from "../events/breakevent"
import { Aim } from "./aim"
import { GameEvent } from "../events/gameevent"
import { EventType } from "../events/eventtype"
import { RerackEvent } from "../events/rerackevent"

export class Replay extends ControllerBase {
delay: number
shots: AimEvent[]
firstShot: AimEvent
shots: GameEvent[]
firstShot: GameEvent
timer
init
constructor(container, init, shots, retry = false, delay = 1500) {
Expand All @@ -19,6 +22,7 @@ export class Replay extends ControllerBase {
this.delay = delay
this.container.table.showTraces(true)
this.container.table.updateFromShortSerialised(this.init)
console.log(shots)
if (retry) {
const retryEvent = new BreakEvent(init, shots)
retryEvent.retry = true
Expand All @@ -31,6 +35,16 @@ export class Replay extends ControllerBase {

playNextShot(delay) {
const shot = this.shots.shift()

if (shot?.type === EventType.RERACK) {
const rerack = RerackEvent.fromJson((shot as RerackEvent).ballinfo)
console.log(rerack)
rerack.applyToController(this)
if (this.shots.length > 0) {
return this.playNextShot(delay)
}
return
}
const aim = AimEvent.fromJson(shot)
this.container.table.cueball = this.container.table.balls[aim.i]
this.container.table.cueball.pos.copy(aim.pos)
Expand Down
7 changes: 4 additions & 3 deletions src/controller/rules/fourteenone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ export class FourteenOne extends NineBall implements Rules {
const onTable = table.balls.filter((b) => b.onTable())
if (onTable.length === 2) {
Rack.rerack(onTable[1], table)
this.container.recoder.wholeGameLink()
this.container.sound.playSuccess(table.inPockets())
const state = table.serialise()
const rerack = { ...state, rerack: true }
this.container.sendEvent(new WatchEvent(rerack))
const rerack = new WatchEvent({ ...state, rerack: true })
this.container.sendEvent(rerack)
this.container.recoder.record(rerack)
this.container.recoder.wholeGameLink()
return new Aim(this.container)
}
}
Expand Down
1 change: 1 addition & 0 deletions src/events/eventtype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export enum EventType {
ABORT = "ABORT",
PLACEBALL = "PLACEBALL",
REJOIN = "REJOIN",
RERACK = "RERACK",
}
3 changes: 3 additions & 0 deletions src/events/eventutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { BeginEvent } from "./beginevent"
import { ChatEvent } from "./chatevent"
import { RejoinEvent } from "./rejoinevent"
import { PlaceBallEvent } from "./placeballevent"
import { RerackEvent } from "./rerackevent"

export class EventUtil {
static serialise(event: GameEvent) {
Expand All @@ -35,6 +36,8 @@ export class EventUtil {
return new AbortEvent()
case EventType.PLACEBALL:
return PlaceBallEvent.fromJson(parsed)
case EventType.RERACK:
return RerackEvent.fromJson(parsed)
default:
throw Error("Unknown GameEvent :" + parsed)
}
Expand Down
11 changes: 10 additions & 1 deletion src/events/recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { ChatEvent } from "./chatevent"
import { EventType } from "./eventtype"
import { HitEvent } from "./hitevent"
import JSONCrush from "jsoncrush"
import { RerackEvent } from "./rerackevent"
import { GameEvent } from "./gameevent"

export class Recorder {
container: Container
shots: string[] = []
shots: GameEvent[] = []
states: number[][] = []
breakStart: number | undefined
replayUrl
Expand All @@ -16,6 +18,13 @@ export class Recorder {
}

record(event) {
if (event.type === EventType.WATCHAIM && "rerack" in event.json) {
this.states.push(this.container.table.shortSerialise())
const rerack = RerackEvent.fromJson({
balls: this.container.table.serialise().balls,
})
this.shots.push(rerack)
}
if (event.type === EventType.HIT) {
this.states.push(this.container.table.shortSerialise())
this.shots.push((<HitEvent>event).tablejson.aim)
Expand Down
23 changes: 23 additions & 0 deletions src/events/rerackevent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { GameEvent } from "./gameevent"
import { EventType } from "./eventtype"
import { Controller } from "../controller/controller"

export class RerackEvent extends GameEvent {
ballinfo

override applyToController(controller: Controller): Controller {
controller.container.table.updateFromSerialised(this.ballinfo)
return controller
}

constructor() {
super()
this.type = EventType.RERACK
}

static fromJson(json) {
const event = new RerackEvent()
event.ballinfo = json
return event
}
}

0 comments on commit dc7c8c1

Please sign in to comment.