UNPKG

modaq

Version:

Quiz Bowl Reader using TypeScript, React, and MobX

246 lines 12.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); require("src/state/Cycle"); const TeamState_1 = require("src/state/TeamState"); const GameState_1 = require("src/state/GameState"); const PacketState_1 = require("src/state/PacketState"); const firstTeamPlayer = new TeamState_1.Player("Alice", "A", /* isStarter */ true); const secondTeamPlayer = new TeamState_1.Player("Bob", "B", /* isStarter */ true); const players = [firstTeamPlayer, secondTeamPlayer]; const defaultPacket = new PacketState_1.PacketState(); defaultPacket.setTossups([ new PacketState_1.Tossup("first q", "first a"), new PacketState_1.Tossup("second q", "second a"), new PacketState_1.Tossup("third q", "third a"), new PacketState_1.Tossup("fourth q", "fourth a"), ]); defaultPacket.setBonuses([ new PacketState_1.Bonus("first leadin", [{ question: "first q", answer: "first a", value: 10 }]), new PacketState_1.Bonus("second leadin", [{ question: "second q", answer: "second a", value: 10 }]), new PacketState_1.Bonus("third leadin", [{ question: "third q", answer: "third a", value: 10 }]), new PacketState_1.Bonus("fourth leadin", [{ question: "fourth q", answer: "fourth a", value: 10 }]), ]); describe("GameStateTests", () => { describe("getBonusIndex", () => { it("No buzzes, same bonus", () => { const game = createDefaultGame(); for (let i = 0; i < game.cycles.length; i++) { chai_1.expect(game.getBonusIndex(i)).to.equal(0); } }); it("No buzzes, paired bonus", () => { const game = createDefaultGame(/* pairTossupsBonuses */ true); for (let i = 0; i < game.cycles.length; i++) { chai_1.expect(game.getBonusIndex(i)).to.equal(i); } }); it("No correct buzzes, same bonus", () => { const game = createDefaultGame(); const buzzCycleIndex = 1; game.cycles[buzzCycleIndex].addWrongBuzz({ player: firstTeamPlayer, position: 1, points: -5, }, 0, game.gameFormat); for (let i = 0; i < game.cycles.length; i++) { chai_1.expect(game.getBonusIndex(i)).to.equal(0); } }); it("No correct buzzes, paired bonus", () => { const game = createDefaultGame(/* pairTossupsBonuses */ true); const buzzCycleIndex = 1; game.cycles[buzzCycleIndex].addWrongBuzz({ player: firstTeamPlayer, position: 1, points: -5, }, 0, game.gameFormat); for (let i = 0; i < game.cycles.length; i++) { chai_1.expect(game.getBonusIndex(i)).to.equal(i); } }); it("Correct buzz changes next cycle's bonus", () => { const game = createDefaultGame(); const buzzCycleIndex = 1; game.cycles[buzzCycleIndex].addCorrectBuzz({ player: firstTeamPlayer, position: 1, points: 10, }, 0, game.gameFormat, 0, defaultPacket.bonuses[0].parts.length); chai_1.expect(game.getBonusIndex(0)).to.equal(0); chai_1.expect(game.getBonusIndex(buzzCycleIndex)).to.equal(0); chai_1.expect(game.getBonusIndex(2)).to.equal(1); }); it("Correct buzz with paired bonuses changes nothing", () => { const game = createDefaultGame(/* pairTossupsBonuses */ true); const buzzCycleIndex = 1; game.cycles[buzzCycleIndex].addCorrectBuzz({ player: firstTeamPlayer, position: 1, points: 10, }, 0, game.gameFormat, 0, defaultPacket.bonuses[0].parts.length); for (let i = 0; i < game.cycles.length; i++) { chai_1.expect(game.getBonusIndex(i)).to.equal(i); } }); it("Thrown out bonus changes this cycle's bonus", () => { const game = createDefaultGame(); const buzzCycleIndex = 1; game.cycles[buzzCycleIndex].addCorrectBuzz({ player: firstTeamPlayer, position: 1, points: 10, }, 0, game.gameFormat, 0, defaultPacket.bonuses[0].parts.length); game.cycles[buzzCycleIndex].addThrownOutBonus(0); chai_1.expect(game.getBonusIndex(0)).to.equal(0); chai_1.expect(game.getBonusIndex(buzzCycleIndex)).to.equal(1); chai_1.expect(game.getBonusIndex(2)).to.equal(2); }); it("Thrown out bonuses changes this cycle's bonus", () => { const game = createDefaultGame(); const buzzCycleIndex = 1; const cycle = game.cycles[buzzCycleIndex]; cycle.addCorrectBuzz({ player: firstTeamPlayer, position: 1, points: 10, }, 0, game.gameFormat, 0, defaultPacket.bonuses[0].parts.length); cycle.addThrownOutBonus(0); cycle.addThrownOutBonus(1); chai_1.expect(game.getBonusIndex(0)).to.equal(0); chai_1.expect(game.getBonusIndex(buzzCycleIndex)).to.equal(2); chai_1.expect(game.getBonusIndex(2)).to.equal(3); }); it("Thrown out bonus changes paired cycle's bonus", () => { const game = createDefaultGame(/* pairTossupsBonuses */ true); const buzzCycleIndex = 1; game.cycles[buzzCycleIndex].addCorrectBuzz({ player: firstTeamPlayer, position: 1, points: 10, }, 0, game.gameFormat, 0, defaultPacket.bonuses[0].parts.length); game.cycles[buzzCycleIndex].addThrownOutBonus(1); chai_1.expect(game.getBonusIndex(0)).to.equal(0); for (let i = 1; i < game.cycles.length - 1; i++) { chai_1.expect(game.getBonusIndex(i)).to.equal(i + 1); } // Last cycle doesn't have a bonus chai_1.expect(game.getBonusIndex(game.cycles.length - 1)).to.equal(-1); }); it("Thrown out bonuses changes this cycle's bonus", () => { const game = createDefaultGame(); const buzzCycleIndex = 1; const cycle = game.cycles[buzzCycleIndex]; cycle.addCorrectBuzz({ player: firstTeamPlayer, position: 1, points: 10, }, 0, game.gameFormat, 0, defaultPacket.bonuses[0].parts.length); cycle.addThrownOutBonus(0); cycle.addThrownOutBonus(1); chai_1.expect(game.getBonusIndex(0)).to.equal(0); chai_1.expect(game.getBonusIndex(buzzCycleIndex)).to.equal(2); chai_1.expect(game.getBonusIndex(2)).to.equal(3); }); it("Thrown out bonuses changes paired cycle's bonus", () => { const game = createDefaultGame(/* pairTossupsBonuses */ true); const buzzCycleIndex = 1; const cycle = game.cycles[buzzCycleIndex]; cycle.addCorrectBuzz({ player: firstTeamPlayer, position: 1, points: 10, }, 0, game.gameFormat, 0, defaultPacket.bonuses[0].parts.length); cycle.addThrownOutBonus(0); cycle.addThrownOutBonus(1); chai_1.expect(game.getBonusIndex(0)).to.equal(0); for (let i = 1; i < game.cycles.length - 2; i++) { chai_1.expect(game.getBonusIndex(i)).to.equal(i + 2); } // Second to last cycle doesn't have a bonus index chai_1.expect(game.getBonusIndex(game.cycles.length - 2)).to.equal(-1); }); it("Correct buzzes changes multiple cycle's bonus", () => { const game = createDefaultGame(); const buzzCycleIndex = 0; const secondBuzzCycleIndex = 1; game.cycles[buzzCycleIndex].addCorrectBuzz({ player: firstTeamPlayer, position: 1, points: 10, }, 0, game.gameFormat, 0, defaultPacket.bonuses[0].parts.length); game.cycles[secondBuzzCycleIndex].addCorrectBuzz({ player: secondTeamPlayer, position: 2, points: 10, }, 1, game.gameFormat, 1, defaultPacket.bonuses[1].parts.length); chai_1.expect(game.getBonusIndex(buzzCycleIndex)).to.equal(0); chai_1.expect(game.getBonusIndex(secondBuzzCycleIndex)).to.equal(1); chai_1.expect(game.getBonusIndex(secondBuzzCycleIndex + 1)).to.equal(2); }); it("Not enough bonuses", () => { const game = createDefaultGame(); const buzzCycleIndex = 0; const buzzCycle = game.cycles[buzzCycleIndex]; buzzCycle.addCorrectBuzz({ player: firstTeamPlayer, position: 1, points: 10, }, 0, game.gameFormat, 0, defaultPacket.bonuses[0].parts.length); // Throw out all but the last two bonuses. The next cycle will use the last bonus, then we can verify that // throwing it out returns -1 for getBonusIndex for (let i = 0; i < game.packet.bonuses.length - 2; i++) { buzzCycle.addThrownOutBonus(i); } const nextBuzzCycleIndex = 1; const nextBuzzCycle = game.cycles[nextBuzzCycleIndex]; nextBuzzCycle.addCorrectBuzz({ player: firstTeamPlayer, position: 2, points: 10, }, 1, game.gameFormat, game.packet.bonuses.length - 1, defaultPacket.bonuses[game.packet.bonuses.length - 1].parts.length); chai_1.expect(game.getBonusIndex(buzzCycleIndex)).to.equal(game.packet.bonuses.length - 2); chai_1.expect(game.getBonusIndex(nextBuzzCycleIndex)).to.equal(game.packet.bonuses.length - 1); nextBuzzCycle.addThrownOutBonus(game.packet.bonuses.length - 1); chai_1.expect(game.getBonusIndex(nextBuzzCycleIndex)).to.equal(-1); chai_1.expect(game.getBonusIndex(nextBuzzCycleIndex + 1)).to.equal(-1); }); it("Not enough bonuses with paired bonuses", () => { const game = createDefaultGame(/* pairTossupsBonuses */ true); const buzzCycleIndex = 0; const buzzCycle = game.cycles[buzzCycleIndex]; buzzCycle.addCorrectBuzz({ player: firstTeamPlayer, position: 1, points: 10, }, 0, game.gameFormat, 0, defaultPacket.bonuses[0].parts.length); // Throw out all but the last two bonuses. The next cycle will use the last bonus, then we can verify that // throwing it out returns -1 for getBonusIndex for (let i = 0; i < game.packet.bonuses.length - 2; i++) { buzzCycle.addThrownOutBonus(i); } const nextBuzzCycleIndex = 1; const nextBuzzCycle = game.cycles[nextBuzzCycleIndex]; nextBuzzCycle.addCorrectBuzz({ player: firstTeamPlayer, position: 2, points: 10, }, 1, game.gameFormat, game.packet.bonuses.length - 1, defaultPacket.bonuses[game.packet.bonuses.length - 1].parts.length); chai_1.expect(game.getBonusIndex(buzzCycleIndex)).to.equal(game.packet.bonuses.length - 2); chai_1.expect(game.getBonusIndex(nextBuzzCycleIndex)).to.equal(game.packet.bonuses.length - 1); nextBuzzCycle.addThrownOutBonus(game.packet.bonuses.length - 1); chai_1.expect(game.getBonusIndex(nextBuzzCycleIndex)).to.equal(-1); chai_1.expect(game.getBonusIndex(nextBuzzCycleIndex + 1)).to.equal(-1); }); }); }); function createDefaultGame(pairTossupsBonuses) { const game = new GameState_1.GameState(); game.addNewPlayers(players); game.loadPacket(defaultPacket); if (pairTossupsBonuses) { game.setGameFormat(Object.assign(Object.assign({}, game.gameFormat), { pairTossupsBonuses: true })); } return game; } //# sourceMappingURL=GetBonusIndexTests.js.map