Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
tailuge committed Nov 19, 2023
1 parent 7dae401 commit c18bd4b
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 22 deletions.
2 changes: 1 addition & 1 deletion 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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@testing-library/dom": "^9.3.3",
"@types/chai": "^4.3.10",
"@types/jest": "^29.5.8",
"@types/node": "^20.9.1",
"@types/node": "^20.9.2",
"@types/three": "^0.158.2",
"chai": "^4.3.10",
"ini": "^4.1.1",
Expand Down
35 changes: 23 additions & 12 deletions src/controller/rules/snooker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,27 +111,37 @@ export class Snooker implements Rules {
// exactly one non red potted

const id = Outcome.pots(outcome)[0].id
if (!this.previousPotRed) {
const lesserBallOnTable =
SnookerUtils.coloursOnTable(this.container.table).filter(
(b) => b.id < id
).length > 0

if (!info.legalFirstCollision || lesserBallOnTable) {
this.foulPoints = this.foulCalculation(outcome, info)
this.respot(outcome)
return this.switchPlayer()
}
console.log(info)
if (id !== info.firstCollision.ballB.id) {
return this.foul(outcome, info)
}

if (this.previousPotRed) {
this.respot(outcome)
this.currentBreak += id + 1
this.previousPotRed = false
return this.continueBreak()
}

const lesserBallOnTable =
SnookerUtils.coloursOnTable(this.container.table).filter((b) => b.id < id)
.length > 0

if (lesserBallOnTable) {
return this.foul(outcome, info)
}

this.currentBreak += id + 1
this.previousPotRed = false
this.container.hud.updateBreak(this.currentBreak)
return this.continueBreak()
}

foul(outcome, info) {
this.foulPoints = this.foulCalculation(outcome, info)
this.respot(outcome)
return this.switchPlayer()
}

foulCalculation(outcome, info) {
const potted = Outcome.pots(outcome)
.map((b) => b.id)
Expand Down Expand Up @@ -235,6 +245,7 @@ export class Snooker implements Rules {
}

continueBreak() {
this.container.hud.updateBreak(this.currentBreak)
const table = this.container.table
this.container.sound.playSuccess(table.inPockets())
if (Outcome.isClearTable(table)) {
Expand Down
4 changes: 2 additions & 2 deletions src/controller/rules/snookerutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ export class SnookerUtils {
return false
}
const id = firstCollision.ballB!.id
const lesserBallOnTable =
SnookerUtils.coloursOnTable(table).filter((b) => b.id < id).length > 0
if (targetIsRed) {
const isRed = id >= 7
return isRed
}
const lesserBallOnTable =
SnookerUtils.coloursOnTable(table).filter((b) => b.id < id).length > 0
return !lesserBallOnTable
}

Expand Down
15 changes: 15 additions & 0 deletions test/rules/snooker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,21 @@ describe("Snooker", () => {
done()
})

it("hit black but pot pink is foul", (done) => {
snooker.targetIsRed = false
snooker.previousPotRed = true
const outcome: Outcome[] = []
outcome.push(Outcome.hit(table.cueball, 1))
outcome.push(Outcome.collision(table.cueball, table.balls[6], 1))
outcome.push(Outcome.collision(table.cueball, table.balls[5], 1))
outcome.push(Outcome.pot(table.balls[5], 1))
table.balls[5].state = State.InPocket
snooker.update(outcome)
expect(snooker.targetIsRed).to.be.true
expect(snooker.foulPoints).to.be.equal(7)
done()
})

it("target is red but hit pink is foul", (done) => {
const outcome: Outcome[] = []
outcome.push(Outcome.hit(table.cueball, 1))
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -936,10 +936,10 @@
dependencies:
"@types/node" "*"

"@types/node@*", "@types/node@^20.9.1":
version "20.9.1"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.9.1.tgz#9d578c610ce1e984adda087f685ace940954fe19"
integrity sha512-HhmzZh5LSJNS5O8jQKpJ/3ZcrrlG6L70hpGqMIAoM9YVD0YBRNWYsfwcXq8VnSjlNpCpgLzMXdiPo+dxcvSmiA==
"@types/node@*", "@types/node@^20.9.2":
version "20.9.2"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.9.2.tgz#002815c8e87fe0c9369121c78b52e800fadc0ac6"
integrity sha512-WHZXKFCEyIUJzAwh3NyyTHYSR35SevJ6mZ1nWwJafKtiQbqRTIKSRcw3Ma3acqgsent3RRDqeVwpHntMk+9irg==
dependencies:
undici-types "~5.26.4"

Expand Down

0 comments on commit c18bd4b

Please sign in to comment.