Skip to content

Commit

Permalink
generate diamond shots
Browse files Browse the repository at this point in the history
  • Loading branch information
tailuge committed Oct 18, 2023
1 parent 7f07ea5 commit 312af7a
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 9 deletions.
12 changes: 3 additions & 9 deletions dist/diagrams/diamond.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,24 @@
<div class="replaydiagram child">
<div
class="topview"
data-state="?ruletype=threecushion&state=%7B%22init%22%3A%5B-1.3861049855898413%2C-0.6663021183415567%2C-1.3528009836432033%2C-0.4691857532050591%2C0.769%2C0%5D%2C%22shots%22%3A%5B%7B%22type%22%3A%22AIM%22%2C%22offset%22%3A%7B%22x%22%3A-0.3%2C%22y%22%3A0.0%2C%22z%22%3A0%7D%2C%22angle%22%3A0.702%2C%22power%22%3A2.0%2C%22pos%22%3A%7B%22x%22%3A-1.3861049855898413%2C%22y%22%3A-0.6663021183415567%2C%22z%22%3A0%7D%2C%22i%22%3A0%7D%5D%7D"
data-state="?ruletype=threecushion&state=%7B%22init%22%3A%5B-1.377%2C-0.717%2C-0.769%2C0%2C0.769%2C0%5D%2C%22shots%22%3A%5B%7B%22type%22%3A%22AIM%22%2C%22offset%22%3A%7B%22x%22%3A-0.3%2C%22y%22%3A0%2C%22z%22%3A0%7D%2C%22angle%22%3A0.731%2C%22power%22%3A2%2C%22pos%22%3A%7B%22x%22%3A-1.377%2C%22y%22%3A-0.717%2C%22z%22%3A0%7D%2C%22i%22%3A0%7D%5D%7D"
></div>
</div>

<div class="replaydiagram child">
<div
class="topview"
data-state="?ruletype=threecushion&state=%7B%22init%22%3A%5B-0.5815437036760774%2C-0.6619233786671217%2C-1.4918488063657103%2C-0.38701043008579117%2C0.769%2C0%5D%2C%22shots%22%3A%5B%7B%22type%22%3A%22AIM%22%2C%22offset%22%3A%7B%22x%22%3A-0.3%2C%22y%22%3A0.0%2C%22z%22%3A0%7D%2C%22angle%22%3A0.831%2C%22power%22%3A2.0%2C%22pos%22%3A%7B%22x%22%3A-0.5815437036760774%2C%22y%22%3A-0.6619233786671217%2C%22z%22%3A0%7D%2C%22i%22%3A0%7D%5D%7D"
data-state="?ruletype=threecushion&state=%7B%22init%22%3A%5B-0.625%2C-0.7%2C-0.769%2C0%2C0.769%2C0%5D%2C%22shots%22%3A%5B%7B%22type%22%3A%22AIM%22%2C%22offset%22%3A%7B%22x%22%3A-0.3%2C%22y%22%3A0%2C%22z%22%3A0%7D%2C%22angle%22%3A0.842%2C%22power%22%3A2%2C%22pos%22%3A%7B%22x%22%3A-0.625%2C%22y%22%3A-0.7%2C%22z%22%3A0%7D%2C%22i%22%3A0%7D%5D%7D"
></div>
</div>

<div class="replaydiagram child">
<div
class="topview"
data-state="?ruletype=threecushion&state=%7B%22init%22%3A%5B0.08637877514555886%2C-0.720797194837692%2C-1.4907664534548462%2C-0.3085631553225074%2C0.769%2C0%5D%2C%22shots%22%3A%5B%7B%22type%22%3A%22AIM%22%2C%22offset%22%3A%7B%22x%22%3A-0.3%2C%22y%22%3A0.0%2C%22z%22%3A0%7D%2C%22angle%22%3A0.97%2C%22power%22%3A2.0%2C%22pos%22%3A%7B%22x%22%3A0.08637877514555886%2C%22y%22%3A-0.720797194837692%2C%22z%22%3A0%7D%2C%22i%22%3A0%7D%5D%7D"
data-state="?ruletype=threecushion&state=%7B%22init%22%3A%5B0.12%2C-0.682%2C-0.769%2C0%2C0.769%2C0%5D%2C%22shots%22%3A%5B%7B%22type%22%3A%22AIM%22%2C%22offset%22%3A%7B%22x%22%3A-0.3%2C%22y%22%3A0%2C%22z%22%3A0%7D%2C%22angle%22%3A0.981%2C%22power%22%3A2%2C%22pos%22%3A%7B%22x%22%3A0.12%2C%22y%22%3A-0.682%2C%22z%22%3A0%7D%2C%22i%22%3A0%7D%5D%7D"
></div>
</div>

<div class="replaydiagram child">
<div
class="topview"
data-state="?ruletype=threecushion&state=%7B%22init%22%3A%5B0.8528801731849832%2C-0.6958489270689311%2C-1.502105336938249%2C-0.21468732812712402%2C0.769%2C0%5D%2C%22shots%22%3A%5B%7B%22type%22%3A%22AIM%22%2C%22offset%22%3A%7B%22x%22%3A-0.3%2C%22y%22%3A0.0%2C%22z%22%3A0%7D%2C%22angle%22%3A1.17%2C%22power%22%3A2.0%2C%22pos%22%3A%7B%22x%22%3A0.8528801731849832%2C%22y%22%3A-0.6958489270689311%2C%22z%22%3A0%7D%2C%22i%22%3A0%7D%5D%7D"
></div>
</div>
</div>

<div class="diagram">
Expand Down
90 changes: 90 additions & 0 deletions test/gen/diamon.spec.ts
Original file line number Diff line number Diff line change
@@ -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
}
})

0 comments on commit 312af7a

Please sign in to comment.