From 312af7ae0e0b382dcbe28d6190bf8e7740a45631 Mon Sep 17 00:00:00 2001 From: tailuge Date: Wed, 18 Oct 2023 13:15:16 +0100 Subject: [PATCH] generate diamond shots --- dist/diagrams/diamond.html | 12 ++--- test/gen/diamon.spec.ts | 90 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 test/gen/diamon.spec.ts diff --git a/dist/diagrams/diamond.html b/dist/diagrams/diamond.html index f956c83..6490433 100644 --- a/dist/diagrams/diamond.html +++ b/dist/diagrams/diamond.html @@ -15,30 +15,24 @@
-
-
-
diff --git a/test/gen/diamon.spec.ts b/test/gen/diamon.spec.ts new file mode 100644 index 0000000..b222e40 --- /dev/null +++ b/test/gen/diamon.spec.ts @@ -0,0 +1,90 @@ +import "mocha" +import { expect } from "chai" +import { HitEvent } from "../../src/controller/controller" +import { Container } from "../../src/container/container" +import { GameEvent } from "../../src/events/gameevent" +import { initDom } from "../view/dom" +import { Init } from "../../src/controller/init" +import { TableGeometry } from "../../src/view/tablegeometry" +import { Vector3 } from "three" +import { R } from "../../src/model/physics/constants" +import { norm, round, roundVec } from "../../src/utils/utils" + +initDom() + +const jestConsole = console + +beforeEach(() => { + global.console = require("console") +}) + +afterEach(() => { + global.console = jestConsole +}) + + +describe("Controller", () => { + let container: Container + let broadcastEvents: GameEvent[] + let stepx: number + let stepy: number + //const replayUrl = "http://localhost:8080/?ruletype=threecushion&state=" + const replayUrl = "?ruletype=threecushion&state=" + beforeEach(function (done) { + container = new Container( + document.getElementById("viewP1"), + (_) => {}, + false, + "threecushion" + ) + broadcastEvents = [] + container.broadcast = (x) => broadcastEvents.push(x) + stepx = TableGeometry.X / 4 + stepy = TableGeometry.Y / 2 + done() + }) + + const toLowerRail = new Vector3(0,-3*R) + const toUpperRail = new Vector3(0,3*R) + + it("initialise ball for diamond system shot", (done) => { + expect(container.controller).to.be.an.instanceof(Init) + container.table.cue.aim.power = 2 + container.table.cue.aim.offset.x = -0.3 + shot(gridPosition(0,0),gridPosition(5,4)) + shot(gridPosition(2,0),gridPosition(6,4)) + shot(gridPosition(4,0),gridPosition(7,4)) + shot(gridPosition(6,0),gridPosition(8,4)) + done() + }) + + function shot(fromDiamond,toDiamond) { + const start = fromDiamond.add(toLowerRail) + const target = toDiamond.add(toUpperRail) + playAlong(start,target) + console.log(getURL()) + } + + function playAlong(start,target) { + const dir = norm(target.clone().sub(start)) + const ballStart = start.clone().addScaledVector(dir,R*7) + container.table.cueball.pos.copy(ballStart) + container.table.cue.aim.angle = round(Math.atan2(dir.y,dir.x)) + } + + function gridPosition(x,y) { + return new Vector3((-4+x) * stepx,(-2+y) * stepy,0) + } + + function getURL() { + roundVec(container.table.cueball.pos) + container.table.cue.moveTo(container.table.cueball.pos) + const event: HitEvent = new HitEvent(container.table.serialise()) + container.recoder.record(event) + container.recoder.updateBreak([]) + + const state = container.recoder.replayLastShot() + const shotUri = `${replayUrl}${encodeURIComponent(JSON.stringify(state))}` + return shotUri + } +})