From 70b233a3741ba5adb5e8aa72f34030d61eb59fd1 Mon Sep 17 00:00:00 2001 From: Enessetere Date: Fri, 29 Jan 2021 22:26:00 +0100 Subject: [PATCH 01/22] test: add test to Trade class --- test/Trade.spec.ts | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 test/Trade.spec.ts diff --git a/test/Trade.spec.ts b/test/Trade.spec.ts new file mode 100644 index 0000000..b40a556 --- /dev/null +++ b/test/Trade.spec.ts @@ -0,0 +1,59 @@ +import { Player } from '../src/Player'; +import { AnimalNames } from '../src/Enums/AnimalNamesEnum'; +import { Trade } from '../src/app/Trade'; + +describe('Trade class test.', () => { + const bank = new Player('bank'); + bank.theHerd.addAnimals(AnimalNames.RABBIT, 60); + bank.theHerd.addAnimals(AnimalNames.SHEEP, 24); + bank.theHerd.addAnimals(AnimalNames.PIG, 20); + bank.theHerd.addAnimals(AnimalNames.COW, 12); + bank.theHerd.addAnimals(AnimalNames.HORSE, 4); + bank.theHerd.addAnimals(AnimalNames.SMALL_DOG, 4); + bank.theHerd.addAnimals(AnimalNames.BIG_DOG, 2); + const trade = new Trade(bank); + const player = new Player('player'); + + it('Should process trade with correct ammount', () => { + player.theHerd.addAnimals(AnimalNames.RABBIT, 6); + const offer: [AnimalNames, number] = [AnimalNames.RABBIT, 6]; + const target: [AnimalNames, number] = [AnimalNames.SHEEP, 1]; + const result = trade.processOffer(offer, player, target); + expect(result).toBe(true); + expect(player.theHerd.getAnimalNumber(AnimalNames.SHEEP)).toBe(1); + }); + + it('Should process trade with reducing players offer', () => { + player.theHerd.addAnimals(AnimalNames.COW, 3); + const offer: [AnimalNames, number] = [AnimalNames.COW, 3]; + const target: [AnimalNames, number] = [AnimalNames.PIG, 3]; + const result = trade.processOffer(offer, player, target); + expect(result).toBe(true); + expect(player.theHerd.getAnimalNumber(AnimalNames.PIG)).toBe(3); + expect(player.theHerd.getAnimalNumber(AnimalNames.COW)).toBe(2); + }); + + it('Should not process due to low offer', () => { + player.theHerd.addAnimals(AnimalNames.HORSE, 1); + const offer: [AnimalNames, number] = [AnimalNames.HORSE, 1]; + const target: [AnimalNames, number] = [AnimalNames.BIG_DOG, 1]; + const result = trade.processOffer(offer, player, target); + expect(result).toBe(false); + expect(player.theHerd.getAnimalNumber(AnimalNames.HORSE)).toBe(1); + expect(player.theHerd.getAnimalNumber(AnimalNames.BIG_DOG)).toBe( + 0, + ); + }); + + it("Should not process if player don't have enougn resources", () => { + const player2 = new Player('player2'); + const offer: [AnimalNames, number] = [AnimalNames.BIG_DOG, 1]; + const target: [AnimalNames, number] = [AnimalNames.COW, 1]; + const result = trade.processOffer(offer, player2, target); + expect(result).toBe(false); + expect(player2.theHerd.getAnimalNumber(AnimalNames.BIG_DOG)).toBe( + 0, + ); + expect(player2.theHerd.getAnimalNumber(AnimalNames.COW)).toBe(0); + }); +}); From a84d26d9c23629defdbd03dce780c431cf7f03ea Mon Sep 17 00:00:00 2001 From: Enessetere Date: Fri, 29 Jan 2021 22:27:34 +0100 Subject: [PATCH 02/22] feat: add trade class with implemented functionalities --- src/app/Trade.ts | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/app/Trade.ts diff --git a/src/app/Trade.ts b/src/app/Trade.ts new file mode 100644 index 0000000..8bb6d39 --- /dev/null +++ b/src/app/Trade.ts @@ -0,0 +1,70 @@ +import { AnimalNames } from '../Enums/AnimalNamesEnum'; +import { ConvertToAnimalObject } from './utils/ConvertToAnimalObject'; +import _ from 'lodash'; +import { Player } from '../Player'; +import { Herd } from './logic/Herd'; + +export class Trade { + constructor(private bank: Player) {} + /** + * Gets an offer from player and returns true or false if transaction can be made processed and process it if possible + * @param offer accepts tuple with offer containing animal name and quantity to be sold + * @param player accepts instance of Player class, which wants to sell his animals + * @param target accepts tuple with desired animal(s) containing desired animal name and quantity to buy + * @returns true if transaction will be processed, and false otherwise + */ + processOffer( + offer: [AnimalNames, number], + { theHerd: playerHerd }: Player, + target: [AnimalNames, number], + ): boolean { + if ( + playerHerd.getAnimalNumber(offer[0]) < offer[1] || + this.bank.theHerd.getAnimalNumber(target[0]) < target[1] + ) { + return false; + } + let value = this.calculateValue(offer); + const price = this.calculateValue(target); + if (price < value) { + this.adjustOffer(offer, target); + value = this.calculateValue(offer); + } + return price > value + ? false + : this.disposeResult(offer, playerHerd, target); + } + + private calculateValue(offer: [AnimalNames, number]): number { + return _.multiply( + ConvertToAnimalObject.convertToAnimalObject(offer[0]).theValue, + offer[1], + ); + } + + private adjustOffer( + offer: [AnimalNames, number], + target: [AnimalNames, number], + ): void { + offer[1] -= 1; + if (this.calculateValue(offer) <= this.calculateValue(target)) { + return; + } + this.adjustOffer(offer, target); + } + + /** + * updates players and banks herd + */ + private disposeResult( + [animalSold, quantitySold]: [AnimalNames, number], + playerHerd: Herd, + [animalBought, quantityBought]: [AnimalNames, number], + ): boolean { + playerHerd.addAnimals(animalSold, -quantitySold); + playerHerd.addAnimals(animalBought, quantityBought); + this.bank.theHerd.addAnimals(animalSold, quantitySold); + this.bank.theHerd.addAnimals(animalBought, -quantityBought); + return true; + } +} From 6ddf62e765e4c8dcf6f8612f96459f072fd410f1 Mon Sep 17 00:00:00 2001 From: Enessetere Date: Fri, 29 Jan 2021 22:28:31 +0100 Subject: [PATCH 03/22] feat: add trade demo class with static method to display demo --- src/app/TradeDemo.ts | 95 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/app/TradeDemo.ts diff --git a/src/app/TradeDemo.ts b/src/app/TradeDemo.ts new file mode 100644 index 0000000..41bbfc7 --- /dev/null +++ b/src/app/TradeDemo.ts @@ -0,0 +1,95 @@ +import { AnimalNames } from '~src/Enums/AnimalNamesEnum'; +import { Player } from '~src/Player'; +import { Trade } from './Trade'; + +export class TradeDemo { + static playDemo(): void { + const bank = new Player('bank'); + bank.theHerd.addAnimals(AnimalNames.RABBIT, 60); + bank.theHerd.addAnimals(AnimalNames.SHEEP, 24); + bank.theHerd.addAnimals(AnimalNames.PIG, 20); + bank.theHerd.addAnimals(AnimalNames.COW, 12); + bank.theHerd.addAnimals(AnimalNames.HORSE, 4); + bank.theHerd.addAnimals(AnimalNames.SMALL_DOG, 4); + bank.theHerd.addAnimals(AnimalNames.BIG_DOG, 2); + const trade = new Trade(bank); + const player = new Player('player'); + player.theHerd.addAnimals(AnimalNames.SHEEP, 5); + player.theHerd.addAnimals(AnimalNames.PIG, 5); + player.theHerd.addAnimals(AnimalNames.COW, 2); + const offer1: [AnimalNames, number] = [AnimalNames.SHEEP, 1]; + const offer2: [AnimalNames, number] = [AnimalNames.SHEEP, 2]; + const offer3: [AnimalNames, number] = [AnimalNames.COW, 1]; + const offer4: [AnimalNames, number] = [AnimalNames.COW, 2]; + const offer5: [AnimalNames, number] = [AnimalNames.PIG, 2]; + const target1: [AnimalNames, number] = [AnimalNames.RABBIT, 6]; + const target2: [AnimalNames, number] = [AnimalNames.RABBIT, 7]; + const target3: [AnimalNames, number] = [AnimalNames.RABBIT, 5]; + const target4: [AnimalNames, number] = [AnimalNames.HORSE, 1]; + const target5: [AnimalNames, number] = [AnimalNames.RABBIT, 12]; + + console.log( + `OFFER: ${offer1}; TARGET: ${target1}; SUCCES: ${trade.processOffer( + offer1, + player, + target1, + )}; PLAYER_HERD: ${JSON.stringify( + player.theHerd, + )}; BANK_HERD: ${JSON.stringify(bank.theHerd)};`, + ); + console.log( + `OFFER: ${offer1}; TARGET: ${target2}; SUCCES: ${trade.processOffer( + offer1, + player, + target2, + )}; PLAYER_HERD: ${JSON.stringify( + player.theHerd, + )}; BANK_HERD: ${JSON.stringify(bank.theHerd)};`, + ); + console.log( + `OFFER: ${offer1}; TARGET: ${target3}; SUCCES: ${trade.processOffer( + offer1, + player, + target3, + )}; PLAYER_HERD: ${JSON.stringify( + player.theHerd, + )}; BANK_HERD: ${JSON.stringify(bank.theHerd)};`, + ); + console.log( + `OFFER: ${offer2}; TARGET: ${target1}; SUCCES: ${trade.processOffer( + offer2, + player, + target1, + )}; PLAYER_HERD: ${JSON.stringify( + player.theHerd, + )}; BANK_HERD: ${JSON.stringify(bank.theHerd)};`, + ); + console.log( + `OFFER: ${offer3}; TARGET: ${target4}; SUCCES: ${trade.processOffer( + offer3, + player, + target4, + )}; PLAYER_HERD: ${JSON.stringify( + player.theHerd, + )}; BANK_HERD: ${JSON.stringify(bank.theHerd)};`, + ); + console.log( + `OFFER: ${offer4}; TARGET: ${target4}; SUCCES: ${trade.processOffer( + offer4, + player, + target4, + )}; PLAYER_HERD: ${JSON.stringify( + player.theHerd, + )}; BANK_HERD: ${JSON.stringify(bank.theHerd)};`, + ); + console.log( + `OFFER: ${offer5}; TARGET: ${target5}; SUCCES: ${trade.processOffer( + offer5, + player, + target5, + )}; PLAYER_HERD: ${JSON.stringify( + player.theHerd, + )}; BANK_HERD: ${JSON.stringify(bank.theHerd)};`, + ); + } +} From a2297af2ef98b4877d9cac52a8d423bbbef8b94f Mon Sep 17 00:00:00 2001 From: Enessetere Date: Fri, 29 Jan 2021 22:29:38 +0100 Subject: [PATCH 04/22] feat: add convert class to create Animal object from AnimalName --- src/app/utils/ConvertToAnimalObject.ts | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/app/utils/ConvertToAnimalObject.ts diff --git a/src/app/utils/ConvertToAnimalObject.ts b/src/app/utils/ConvertToAnimalObject.ts new file mode 100644 index 0000000..f32786c --- /dev/null +++ b/src/app/utils/ConvertToAnimalObject.ts @@ -0,0 +1,37 @@ +import { Animal } from '../../Animals/Animal'; +import { BigDog } from '../../Animals/BigDog'; +import { Cow } from '../../Animals/Cow'; +import { Horse } from '../../Animals/Horse'; +import { Pig } from '../../Animals/Pig'; +import { Rabbit } from '../../Animals/Rabbit'; +import { Sheep } from '../../Animals/Sheep'; +import { SmallDog } from '../../Animals/SmallDog'; +import { AnimalNames } from '../../Enums/AnimalNamesEnum'; + +export class ConvertToAnimalObject { + /** + * Method that creates animal object based on animal name + * @param animal takes AnimalName + * @returns Animal object with corresponding instace + */ + static convertToAnimalObject(animal: AnimalNames): Animal { + switch (animal) { + case AnimalNames.RABBIT: + return new Rabbit(); + case AnimalNames.SHEEP: + return new Sheep(); + case AnimalNames.PIG: + return new Pig(); + case AnimalNames.COW: + return new Cow(); + case AnimalNames.HORSE: + return new Horse(); + case AnimalNames.SMALL_DOG: + return new SmallDog(); + case AnimalNames.BIG_DOG: + return new BigDog(); + default: + throw Error(`Animal name is unknown: ${animal}`); + } + } +} From 0787e82cc73eb60279913468fe699d74c08e4582 Mon Sep 17 00:00:00 2001 From: Enessetere Date: Fri, 29 Jan 2021 23:37:49 +0100 Subject: [PATCH 05/22] refactor: refactor class and extract repetable parts to methods --- src/app/BreedProcessor.ts | 143 +++++++++++++++++++++----------------- 1 file changed, 81 insertions(+), 62 deletions(-) diff --git a/src/app/BreedProcessor.ts b/src/app/BreedProcessor.ts index 6b2f221..4811a0b 100644 --- a/src/app/BreedProcessor.ts +++ b/src/app/BreedProcessor.ts @@ -6,12 +6,15 @@ import { AnimalNames } from '../Enums/AnimalNamesEnum'; import { Fox } from '../Animals/Fox'; import { Wolf } from '../Animals/Wolf'; import { Herd } from './logic/Herd'; +import { ConvertToAnimalObject } from './utils/ConvertToAnimalObject'; +import { add, divide, floor, min } from 'lodash'; +import { AnimalRoles } from '~src/Enums/AnimalRolesEnum'; export class BreedProcessor { randomResultInterfaceWolf: GetRandomValue; randomResultInterfaceFox: GetRandomValue; - constructor() { + constructor(private bank: Player) { this.randomResultInterfaceWolf = new SecondDice(); this.randomResultInterfaceFox = new FirstDice(); } @@ -19,78 +22,94 @@ export class BreedProcessor { processBreedPhase({ theHerd }: Player): [AnimalNames, AnimalNames] { const wolfDiceResult = this.randomResultInterfaceWolf.getRandomValue(); const foxDiceResult = this.randomResultInterfaceFox.getRandomValue(); - if (foxDiceResult !== AnimalNames.FOX) { - if (wolfDiceResult !== AnimalNames.WOLF) { - this.breedAnimals(foxDiceResult, wolfDiceResult, theHerd); - } else { - this.breedAnimals(foxDiceResult, undefined, theHerd); - theHerd.cullAnimals( - this.getRandomResult(wolfDiceResult) as Wolf, - ); - } + const equalResult = foxDiceResult === wolfDiceResult; + if (equalResult) { + this.breedAnimals(foxDiceResult, theHerd, true); + return [wolfDiceResult, foxDiceResult]; + } + if (foxDiceResult === AnimalNames.FOX) { + const fox: Fox = ConvertToAnimalObject.convertToAnimalObject( + foxDiceResult, + ) as Fox; + this.returnToBank(fox, theHerd); + theHerd.cullAnimals(fox); } else { - if (wolfDiceResult !== AnimalNames.WOLF) { - this.breedAnimals(undefined, wolfDiceResult, theHerd); - theHerd.cullAnimals( - this.getRandomResult(foxDiceResult) as Fox, - ); - } else { - theHerd.cullAnimals( - this.getRandomResult(foxDiceResult) as Fox, - ); - theHerd.cullAnimals( - this.getRandomResult(wolfDiceResult) as Wolf, - ); - } + this.breedAnimals(foxDiceResult, theHerd); } - return [wolfDiceResult, foxDiceResult]; - } - - private getRandomResult( - animalName: AnimalNames, - ): Wolf | Fox | AnimalNames { - switch (animalName) { - case AnimalNames.FOX: - return new Fox(); - case AnimalNames.WOLF: - return new Wolf(); - default: - return animalName; + if (wolfDiceResult === AnimalNames.WOLF) { + const wolf = ConvertToAnimalObject.convertToAnimalObject( + wolfDiceResult, + ) as Wolf; + this.returnToBank(wolf, theHerd); + theHerd.cullAnimals(wolf); + } else { + this.breedAnimals(wolfDiceResult, theHerd); } + return [wolfDiceResult, foxDiceResult]; } private breedAnimals( - foxDiceAnimalName: AnimalNames | undefined, - wolfDiceAnimalName: AnimalNames | undefined, + diceResult: AnimalNames, herd: Herd, - ) { - if ( - foxDiceAnimalName && - wolfDiceAnimalName && - foxDiceAnimalName == wolfDiceAnimalName - ) { - const numberOfAnimals = herd.getAnimalNumber(foxDiceAnimalName); - herd.addAnimals( - foxDiceAnimalName, - Math.floor((numberOfAnimals + 2) / 2), + isDoubled?: boolean, + ): void { + if (isDoubled) { + const herdGrow = this.calculateHerdGrow( + diceResult, + herd, + isDoubled, ); + this.bank.theHerd.addAnimals(diceResult, -herdGrow); + herd.addAnimals(diceResult, herdGrow); return; } - if (foxDiceAnimalName) { - const numberOfAnimals = herd.getAnimalNumber(foxDiceAnimalName); - herd.addAnimals( - foxDiceAnimalName, - Math.floor((numberOfAnimals + 1) / 2), - ); + const herdGrow = this.calculateHerdGrow(diceResult, herd); + this.bank.theHerd.addAnimals(diceResult, -herdGrow); + herd.addAnimals(diceResult, herdGrow); + } + + private returnToBank(predator: Wolf | Fox, herd: Herd): void { + if (predator instanceof Fox) { + if (herd.getAnimalNumber(AnimalNames.SMALL_DOG) > 0) { + this.bank.theHerd.addAnimals(AnimalNames.SMALL_DOG, 1); + return; + } + const quantity = herd.getAnimalNumber(AnimalNames.RABBIT); + this.bank.theHerd.addAnimals(AnimalNames.RABBIT, quantity); + return; } - if (wolfDiceAnimalName) { - const numberOfAnimals = herd.getAnimalNumber( - wolfDiceAnimalName, - ); - herd.addAnimals( - wolfDiceAnimalName, - Math.floor((numberOfAnimals + 1) / 2), - ); + if (herd.getAnimalNumber(AnimalNames.BIG_DOG) > 0) { + this.bank.theHerd.addAnimals(AnimalNames.BIG_DOG, 1); + return; } + herd.theAnimals + .filter( + (tuple) => + tuple[0].hasRole(AnimalRoles.LIVESTOCK) && + tuple[0].theName !== AnimalNames.HORSE, + ) + .forEach((tuple) => + this.bank.theHerd.addAnimals( + tuple[0].theName as AnimalNames, + tuple[1], + ), + ); + } + + private calculateHerdGrow( + diceResult: AnimalNames, + herd: Herd, + isDoubled?: boolean, + ): number { + const herdMaxGrow = floor( + divide( + add(herd.getAnimalNumber(diceResult), isDoubled ? 2 : 1), + 2, + ), + ); + const bankContains = this.bank.theHerd.getAnimalNumber( + diceResult, + ); + return min([herdMaxGrow, bankContains]) as number; } } From 921efbc33e0e42fc1e782ec0a1c38a6444682f09 Mon Sep 17 00:00:00 2001 From: Enessetere Date: Fri, 29 Jan 2021 23:38:38 +0100 Subject: [PATCH 06/22] feat: add wolf and fox generator --- src/app/utils/ConvertToAnimalObject.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/app/utils/ConvertToAnimalObject.ts b/src/app/utils/ConvertToAnimalObject.ts index f32786c..9adea52 100644 --- a/src/app/utils/ConvertToAnimalObject.ts +++ b/src/app/utils/ConvertToAnimalObject.ts @@ -1,3 +1,5 @@ +import { Fox } from '~src/Animals/Fox'; +import { Wolf } from '~src/Animals/Wolf'; import { Animal } from '../../Animals/Animal'; import { BigDog } from '../../Animals/BigDog'; import { Cow } from '../../Animals/Cow'; @@ -30,6 +32,10 @@ export class ConvertToAnimalObject { return new SmallDog(); case AnimalNames.BIG_DOG: return new BigDog(); + case AnimalNames.FOX: + return new Fox(); + case AnimalNames.WOLF: + return new Wolf(); default: throw Error(`Animal name is unknown: ${animal}`); } From 84c3689e4633d11662395a328b31e1c23191b8d7 Mon Sep 17 00:00:00 2001 From: Enessetere Date: Fri, 29 Jan 2021 23:40:03 +0100 Subject: [PATCH 07/22] chore: moved trade demo class to correct directory --- src/app/{ => manuals}/TradeDemo.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) rename src/app/{ => manuals}/TradeDemo.ts (94%) diff --git a/src/app/TradeDemo.ts b/src/app/manuals/TradeDemo.ts similarity index 94% rename from src/app/TradeDemo.ts rename to src/app/manuals/TradeDemo.ts index 41bbfc7..7ec2d6a 100644 --- a/src/app/TradeDemo.ts +++ b/src/app/manuals/TradeDemo.ts @@ -1,6 +1,8 @@ -import { AnimalNames } from '~src/Enums/AnimalNamesEnum'; -import { Player } from '~src/Player'; -import { Trade } from './Trade'; +import { AnimalNames } from '../../Enums/AnimalNamesEnum'; +import { Player } from '../../Player'; +import { Trade } from '../Trade'; + +// To run this demo add TradeDemo.playDemo() in App.ts export class TradeDemo { static playDemo(): void { From d954ac0d0f72550d6e1f88cd996b3b5139b37d2f Mon Sep 17 00:00:00 2001 From: Enessetere Date: Fri, 29 Jan 2021 23:41:57 +0100 Subject: [PATCH 08/22] feat: Add breed process demo --- src/app/manuals/BreedPhaseDemo.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/app/manuals/BreedPhaseDemo.ts diff --git a/src/app/manuals/BreedPhaseDemo.ts b/src/app/manuals/BreedPhaseDemo.ts new file mode 100644 index 0000000..2a552ba --- /dev/null +++ b/src/app/manuals/BreedPhaseDemo.ts @@ -0,0 +1,23 @@ +import { Player } from '../../Player'; +import { AnimalNames } from '../../Enums/AnimalNamesEnum'; +import { BreedProcessor } from '../BreedProcessor'; + +export class BreedPhaseDemo { + static playDemo(): void { + const bank = new Player('bank'); + bank.theHerd.addAnimals(AnimalNames.RABBIT, 10); + bank.theHerd.addAnimals(AnimalNames.SHEEP, 10); + bank.theHerd.addAnimals(AnimalNames.PIG, 10); + bank.theHerd.addAnimals(AnimalNames.COW, 12); + bank.theHerd.addAnimals(AnimalNames.HORSE, 4); + bank.theHerd.addAnimals(AnimalNames.SMALL_DOG, 4); + bank.theHerd.addAnimals(AnimalNames.BIG_DOG, 2); + const bp = new BreedProcessor(bank); + const player = new Player('player'); + for (let i = 0; i < 10; i++) { + bp.processBreedPhase(player); + } + console.log(bank.theHerd); + console.log(player.theHerd); + } +} From 2b4c140cc604e91b993dfe6c2b1b118b8302fc5f Mon Sep 17 00:00:00 2001 From: Enessetere Date: Fri, 29 Jan 2021 23:46:25 +0100 Subject: [PATCH 09/22] fix: change import path in object converter --- src/app/utils/ConvertToAnimalObject.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app/utils/ConvertToAnimalObject.ts b/src/app/utils/ConvertToAnimalObject.ts index 9adea52..98b6a00 100644 --- a/src/app/utils/ConvertToAnimalObject.ts +++ b/src/app/utils/ConvertToAnimalObject.ts @@ -1,5 +1,5 @@ -import { Fox } from '~src/Animals/Fox'; -import { Wolf } from '~src/Animals/Wolf'; +import { Fox } from '../../Animals/Fox'; +import { Wolf } from '../../Animals/Wolf'; import { Animal } from '../../Animals/Animal'; import { BigDog } from '../../Animals/BigDog'; import { Cow } from '../../Animals/Cow'; From 03ded1c96b9af2d14e045a1c5b0e2633e63f688a Mon Sep 17 00:00:00 2001 From: Enessetere Date: Fri, 29 Jan 2021 23:52:17 +0100 Subject: [PATCH 10/22] fix: add missing dependency to BreedProcessor --- src/app/GameController.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/app/GameController.ts b/src/app/GameController.ts index 21f1873..2aea8ac 100644 --- a/src/app/GameController.ts +++ b/src/app/GameController.ts @@ -1,4 +1,3 @@ -import { time } from 'console'; import { AnimalNames } from '../Enums/AnimalNamesEnum'; import { Player } from '../Player'; import { BreedProcessor } from './BreedProcessor'; @@ -12,7 +11,7 @@ export class GameController { private breedProcessor: BreedProcessor; constructor(private view: View) { this.timer = new Timer(); - this.breedProcessor = new BreedProcessor(); + this.breedProcessor = new BreedProcessor(new Player('bank')); } get theCurrentPlayer(): Player { From b66a03ae27bb044f41a079851f1d7477f7ee47d4 Mon Sep 17 00:00:00 2001 From: Enessetere Date: Sat, 30 Jan 2021 14:33:38 +0100 Subject: [PATCH 11/22] refactor: implemented changes in to latest develop --- src/app/BreedProcessor.ts | 17 +++++++++-------- src/app/GameController.ts | 3 ++- src/app/Trade.ts | 4 ++-- src/app/manuals/TradeDemo.ts | 10 ++-------- 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/app/BreedProcessor.ts b/src/app/BreedProcessor.ts index 4811a0b..f082748 100644 --- a/src/app/BreedProcessor.ts +++ b/src/app/BreedProcessor.ts @@ -8,13 +8,14 @@ import { Wolf } from '../Animals/Wolf'; import { Herd } from './logic/Herd'; import { ConvertToAnimalObject } from './utils/ConvertToAnimalObject'; import { add, divide, floor, min } from 'lodash'; -import { AnimalRoles } from '~src/Enums/AnimalRolesEnum'; +import { AnimalRoles } from '../Enums/AnimalRolesEnum'; +import { Bank } from './logic/Bank'; export class BreedProcessor { randomResultInterfaceWolf: GetRandomValue; randomResultInterfaceFox: GetRandomValue; - constructor(private bank: Player) { + constructor(private bank: Bank) { this.randomResultInterfaceWolf = new SecondDice(); this.randomResultInterfaceFox = new FirstDice(); } @@ -84,14 +85,14 @@ export class BreedProcessor { } herd.theAnimals .filter( - (tuple) => - tuple[0].hasRole(AnimalRoles.LIVESTOCK) && - tuple[0].theName !== AnimalNames.HORSE, + ([animal]) => + animal.hasRole(AnimalRoles.LIVESTOCK) && + animal.theName !== AnimalNames.HORSE, ) - .forEach((tuple) => + .forEach(([animal, count]) => this.bank.theHerd.addAnimals( - tuple[0].theName as AnimalNames, - tuple[1], + animal.theName as AnimalNames, + count, ), ); } diff --git a/src/app/GameController.ts b/src/app/GameController.ts index 2aea8ac..3cd93de 100644 --- a/src/app/GameController.ts +++ b/src/app/GameController.ts @@ -1,6 +1,7 @@ import { AnimalNames } from '../Enums/AnimalNamesEnum'; import { Player } from '../Player'; import { BreedProcessor } from './BreedProcessor'; +import { Bank } from './logic/Bank'; import { Timer } from './Timer'; import { View } from './View'; @@ -11,7 +12,7 @@ export class GameController { private breedProcessor: BreedProcessor; constructor(private view: View) { this.timer = new Timer(); - this.breedProcessor = new BreedProcessor(new Player('bank')); + this.breedProcessor = new BreedProcessor(new Bank()); } get theCurrentPlayer(): Player { diff --git a/src/app/Trade.ts b/src/app/Trade.ts index 8bb6d39..e231ace 100644 --- a/src/app/Trade.ts +++ b/src/app/Trade.ts @@ -1,6 +1,6 @@ import { AnimalNames } from '../Enums/AnimalNamesEnum'; import { ConvertToAnimalObject } from './utils/ConvertToAnimalObject'; -import _ from 'lodash'; +import { multiply } from 'lodash'; import { Player } from '../Player'; import { Herd } from './logic/Herd'; @@ -36,7 +36,7 @@ export class Trade { } private calculateValue(offer: [AnimalNames, number]): number { - return _.multiply( + return multiply( ConvertToAnimalObject.convertToAnimalObject(offer[0]).theValue, offer[1], ); diff --git a/src/app/manuals/TradeDemo.ts b/src/app/manuals/TradeDemo.ts index 7ec2d6a..92d53cf 100644 --- a/src/app/manuals/TradeDemo.ts +++ b/src/app/manuals/TradeDemo.ts @@ -1,19 +1,13 @@ import { AnimalNames } from '../../Enums/AnimalNamesEnum'; import { Player } from '../../Player'; +import { Bank } from '../logic/Bank'; import { Trade } from '../Trade'; // To run this demo add TradeDemo.playDemo() in App.ts export class TradeDemo { static playDemo(): void { - const bank = new Player('bank'); - bank.theHerd.addAnimals(AnimalNames.RABBIT, 60); - bank.theHerd.addAnimals(AnimalNames.SHEEP, 24); - bank.theHerd.addAnimals(AnimalNames.PIG, 20); - bank.theHerd.addAnimals(AnimalNames.COW, 12); - bank.theHerd.addAnimals(AnimalNames.HORSE, 4); - bank.theHerd.addAnimals(AnimalNames.SMALL_DOG, 4); - bank.theHerd.addAnimals(AnimalNames.BIG_DOG, 2); + const bank = new Bank(); const trade = new Trade(bank); const player = new Player('player'); player.theHerd.addAnimals(AnimalNames.SHEEP, 5); From 0fd92aad283df566813d532ee1d1fb441c6df2ed Mon Sep 17 00:00:00 2001 From: VieraBoschkova Date: Sat, 30 Jan 2021 19:50:28 +0100 Subject: [PATCH 12/22] fix: analize needed changes --- src/app/logic/Herd.ts | 48 +++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/app/logic/Herd.ts b/src/app/logic/Herd.ts index 21e145a..7fdb51c 100644 --- a/src/app/logic/Herd.ts +++ b/src/app/logic/Herd.ts @@ -12,26 +12,27 @@ import { AnimalNames } from '../../Enums/AnimalNamesEnum'; export class Herd { protected animals: [Animal, number][]; - constructor( - numberOfRabbits = 0, - numberOfSheep = 0, - numberOfPigs = 0, - numberOfCows = 0, - numberOfHorses = 0, - numberOfSmallDogs = 0, - numberOfBigDogs = 0, - ) { - this.animals = [ - [new Rabbit(), numberOfRabbits], - [new Sheep(), numberOfSheep], - [new Pig(), numberOfPigs], - [new Cow(), numberOfCows], - [new Horse(), numberOfHorses], - [new SmallDog(), numberOfSmallDogs], - [new BigDog(), numberOfBigDogs], - ]; + constructor() { + // numberOfBigDogs = 0, // numberOfSmallDogs = 0, // numberOfHorses = 0, // numberOfCows = 0, // numberOfPigs = 0, // numberOfSheep = 0, // numberOfRabbits = 0, // herdConfig: Animal[] = defaultHerdConfig; + // this.animals = herdConfig.map(animal => { + // return [new Animal(animal.name), animal.farmStock ] + // }) + // this.animals = [ + // [new Rabbit(), numberOfRabbits], + // [new Sheep(), numberOfSheep], + // [new Pig(), numberOfPigs], + // [new Cow(), numberOfCows], + // [new Horse(), numberOfHorses], + // [new SmallDog(), numberOfSmallDogs], + // [new BigDog(), numberOfBigDogs], + // ]; + this.animals = []; } - + findAnimal(animalName: AnimalNames) { + return; + // return indexOfAnimal; + } + // updateNumberOfAnimals(index: number, newNumberOfAnimals: number) {} addAnimals(animalName: AnimalNames, numberToAdd: number): Herd { this.animals = this.animals.map((animalData) => { const name = animalData[0].theName; @@ -48,11 +49,10 @@ export class Herd { } getAnimalNumber(animalName: AnimalNames): number { - const animalToCheck = this.animals.find((animalData) => { - return animalData[0].theName === animalName; - }); - if (!animalToCheck) return 0; - return animalToCheck[1]; + const animalToCheck = this.animals.find( + (animalData) => animalData[0].theName === animalName, + ); + return animalToCheck ? animalToCheck[1] : 0; } private cullAllAnimalsOfOneType(animalName: AnimalNames): void { From 26f09b4df0accacad4b58592fd6bb3a24db2f0a0 Mon Sep 17 00:00:00 2001 From: Enessetere Date: Sat, 30 Jan 2021 21:11:19 +0100 Subject: [PATCH 13/22] feat: add WinModal functionality and change buttons in basic modal --- src/app/components/ModalBasic.ts | 16 +++++++++------- src/app/components/WinModal.ts | 28 ++++++++++++++++++++++++++++ src/app/manuals/WinModalDemo.ts | 16 ++++++++++++++++ 3 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 src/app/components/WinModal.ts create mode 100644 src/app/manuals/WinModalDemo.ts diff --git a/src/app/components/ModalBasic.ts b/src/app/components/ModalBasic.ts index 453a499..ce3060d 100644 --- a/src/app/components/ModalBasic.ts +++ b/src/app/components/ModalBasic.ts @@ -69,24 +69,26 @@ export class ModalBasic { createAndAppendButtonsRow( leftButtonText: string, leftButtonAction: () => void, - rightButtonText: string, - rightButtonAction: () => void, + rightButtonText?: string, + rightButtonAction?: () => void, ): ModalBasic { const leftButton: HTMLElement = new Button().create( leftButtonText, ); - const rightButton: HTMLElement = new Button().create( - rightButtonText, - ); + const rightButton: HTMLElement | null = rightButtonText + ? new Button().create(rightButtonText) + : null; + if (rightButton && rightButtonAction) { + rightButton.addEventListener('click', rightButtonAction); + } leftButton.addEventListener('click', leftButtonAction); - rightButton.addEventListener('click', rightButtonAction); const buttonsRow = Render.elementFactory( 'div', { className: 'modal__buttons', }, leftButton, - rightButton, + rightButton ? rightButton : '', ); Render.childrenInjector(this.modalContainer, buttonsRow); return this; diff --git a/src/app/components/WinModal.ts b/src/app/components/WinModal.ts new file mode 100644 index 0000000..d366501 --- /dev/null +++ b/src/app/components/WinModal.ts @@ -0,0 +1,28 @@ +import { Player } from '~src/Player'; +import { Render } from '../utils/Render'; +import { ModalBasic } from './ModalBasic'; + +export class WinModal extends ModalBasic { + create(player: Player): void { + this.renderBasicModal( + 'CONGRATULATIONS!', + `${player.theName} has won!`, + this.createImage(player), + ) as WinModal; + } + + addButton(): void { + const handleEnd = () => Render.removeElement('.modal'); + this.createAndAppendButtonsRow( + 'End game - back to menu', + handleEnd, + ); + } + + private createImage({ theName, theAvatar }: Player): HTMLElement { + return Render.elementFactory('img', { + src: theAvatar, + alt: `${theName}-avatar`, + }); + } +} \ No newline at end of file diff --git a/src/app/manuals/WinModalDemo.ts b/src/app/manuals/WinModalDemo.ts new file mode 100644 index 0000000..44d858c --- /dev/null +++ b/src/app/manuals/WinModalDemo.ts @@ -0,0 +1,16 @@ +import { Player } from '../../Player'; +import { WinModal } from '../components/WinModal'; +import { Render } from '../utils/Render'; + +export class WinModalDemo { + static playDemo(): void { + const modal = new WinModal(); + const player = new Player( + 'player', + './static/images/avatars/dog.png', + ); + modal.create(player); + modal.addButton(); + Render.render('#sf-app', modal.modal); + } +} From a661d47cbb42e050360a1ffd23114b1619676c2d Mon Sep 17 00:00:00 2001 From: VieraBoschkova Date: Sun, 31 Jan 2021 01:06:38 +0100 Subject: [PATCH 14/22] feat: add separete methods to processing data refactor: use herdConfig to create new herd --- src/Animals/Animal.ts | 5 +- src/Enums/GameModeEnums.ts | 4 + src/Interfaces/GameConfigInterface.ts | 28 +++++ src/app/App.ts | 2 + src/app/BreedProcessor.ts | 6 +- src/app/logic/Bank.ts | 6 +- src/app/logic/Herd.ts | 144 ++++++++++++++++--------- src/app/logic/mockGameConfiguration.ts | 107 ++++++++++++++++++ src/app/logic/mockHerdConfig.ts | 63 +++++++++++ src/app/manuals/herdManual.ts | 74 +++++++++++++ test/herd.spec.ts | 24 +++-- 11 files changed, 399 insertions(+), 64 deletions(-) create mode 100644 src/Enums/GameModeEnums.ts create mode 100644 src/Interfaces/GameConfigInterface.ts create mode 100644 src/app/logic/mockGameConfiguration.ts create mode 100644 src/app/logic/mockHerdConfig.ts create mode 100644 src/app/manuals/herdManual.ts diff --git a/src/Animals/Animal.ts b/src/Animals/Animal.ts index c9d4150..8b561ca 100644 --- a/src/Animals/Animal.ts +++ b/src/Animals/Animal.ts @@ -2,9 +2,10 @@ import { AnimalNames } from '../Enums/AnimalNamesEnum'; import { AnimalRoles } from '../Enums/AnimalRolesEnum'; import { pull } from 'lodash'; -type Value = 1 | 6 | 12 | 36 | 72; +export type Value = 1 | 6 | 12 | 36 | 72; -export abstract class Animal { +// export abstract class Animal { +export class Animal { protected name: AnimalNames; protected imagePath: string; protected roles: AnimalRoles[]; diff --git a/src/Enums/GameModeEnums.ts b/src/Enums/GameModeEnums.ts new file mode 100644 index 0000000..6fb1dc6 --- /dev/null +++ b/src/Enums/GameModeEnums.ts @@ -0,0 +1,4 @@ +export enum GameModes { + STATIC = 'static', + DYNAMIC = 'dynamic', +} diff --git a/src/Interfaces/GameConfigInterface.ts b/src/Interfaces/GameConfigInterface.ts new file mode 100644 index 0000000..69038f0 --- /dev/null +++ b/src/Interfaces/GameConfigInterface.ts @@ -0,0 +1,28 @@ +import { GameModes } from '../Enums/GameModeEnums'; +import { AnimalNames } from '../Enums/AnimalNamesEnum'; +import { AnimalRoles } from '../Enums/AnimalRolesEnum'; + +export interface GameConfigInterface { + mode: GameModes; + roundTimeInSeconds: number; + totalGameTimeInSeconds?: number; + playersConfig: { name: string; path: string }[]; + herdConfig: herdConfigInterface[]; + predatorAnimalsConfig: predatorAnimalInterface[]; +} + +export interface herdConfigInterface { + name: AnimalNames; + tradeValue: number; + role: AnimalRoles; + playersInitialStock: number; + bankInitialStock: number; + dice?: { diceNumber: number; probability: number }[]; +} + +export interface predatorAnimalInterface { + name: AnimalNames; + kills: AnimalNames[]; + isChasedAwayBy: [AnimalNames]; + dice?: { diceNumber: number; probability: number }[]; +} \ No newline at end of file diff --git a/src/app/App.ts b/src/app/App.ts index 3b63de4..c53ce9b 100644 --- a/src/app/App.ts +++ b/src/app/App.ts @@ -1,5 +1,7 @@ +import { herdDemo } from '../app/manuals/herdManual'; export class App { init(): string { + herdDemo(); return 'hello world'; } } diff --git a/src/app/BreedProcessor.ts b/src/app/BreedProcessor.ts index 6b2f221..85eec6b 100644 --- a/src/app/BreedProcessor.ts +++ b/src/app/BreedProcessor.ts @@ -70,7 +70,7 @@ export class BreedProcessor { foxDiceAnimalName == wolfDiceAnimalName ) { const numberOfAnimals = herd.getAnimalNumber(foxDiceAnimalName); - herd.addAnimals( + herd.addAnimalsToHerd( foxDiceAnimalName, Math.floor((numberOfAnimals + 2) / 2), ); @@ -78,7 +78,7 @@ export class BreedProcessor { } if (foxDiceAnimalName) { const numberOfAnimals = herd.getAnimalNumber(foxDiceAnimalName); - herd.addAnimals( + herd.addAnimalsToHerd( foxDiceAnimalName, Math.floor((numberOfAnimals + 1) / 2), ); @@ -87,7 +87,7 @@ export class BreedProcessor { const numberOfAnimals = herd.getAnimalNumber( wolfDiceAnimalName, ); - herd.addAnimals( + herd.addAnimalsToHerd( wolfDiceAnimalName, Math.floor((numberOfAnimals + 1) / 2), ); diff --git a/src/app/logic/Bank.ts b/src/app/logic/Bank.ts index 031d398..f6ad6c8 100644 --- a/src/app/logic/Bank.ts +++ b/src/app/logic/Bank.ts @@ -1,9 +1,9 @@ -import { Player } from '~src/Player'; +import { Player } from '../../Player'; import { Herd } from './Herd'; export class Bank extends Player { constructor() { - super('Bank'); - this.herd = new Herd(60, 24, 20, 12, 4, 4, 2); + super('bank'); + // this.herd = new Herd(60, 24, 20, 12, 4, 4, 2); } } diff --git a/src/app/logic/Herd.ts b/src/app/logic/Herd.ts index 7fdb51c..7f239f9 100644 --- a/src/app/logic/Herd.ts +++ b/src/app/logic/Herd.ts @@ -1,65 +1,109 @@ -import { Animal } from '../../Animals/Animal'; +import _ from 'lodash'; +import { AnimalRoles } from '~src/Enums/AnimalRolesEnum'; +import { Animal, Value } from '../../Animals/Animal'; import { BigDog } from '../../Animals/BigDog'; -import { Cow } from '../../Animals/Cow'; +// import { Cow } from '../../Animals/Cow'; import { Fox } from '../../Animals/Fox'; -import { Horse } from '../../Animals/Horse'; -import { Pig } from '../../Animals/Pig'; -import { Rabbit } from '../../Animals/Rabbit'; -import { Sheep } from '../../Animals/Sheep'; +// import { Horse } from '../../Animals/Horse'; +// import { Pig } from '../../Animals/Pig'; +// import { Rabbit } from '../../Animals/Rabbit'; +// import { Sheep } from '../../Animals/Sheep'; import { SmallDog } from '../../Animals/SmallDog'; import { Wolf } from '../../Animals/Wolf'; import { AnimalNames } from '../../Enums/AnimalNamesEnum'; export class Herd { protected animals: [Animal, number][]; - constructor() { - // numberOfBigDogs = 0, // numberOfSmallDogs = 0, // numberOfHorses = 0, // numberOfCows = 0, // numberOfPigs = 0, // numberOfSheep = 0, // numberOfRabbits = 0, // herdConfig: Animal[] = defaultHerdConfig; - // this.animals = herdConfig.map(animal => { - // return [new Animal(animal.name), animal.farmStock ] - // }) - // this.animals = [ - // [new Rabbit(), numberOfRabbits], - // [new Sheep(), numberOfSheep], - // [new Pig(), numberOfPigs], - // [new Cow(), numberOfCows], - // [new Horse(), numberOfHorses], - // [new SmallDog(), numberOfSmallDogs], - // [new BigDog(), numberOfBigDogs], - // ]; - this.animals = []; - } - findAnimal(animalName: AnimalNames) { - return; - // return indexOfAnimal; + constructor( + playersHerdConfig: { + name: AnimalNames; + tradeValue: Value; + role: AnimalRoles; + path: string; + initialStock: number; + }[], + ) { + this.animals = playersHerdConfig.map( + ({ name, tradeValue, role, path, initialStock }) => { + // TODO: ADD PATH TO ANIMAL IMAGE, VALUE, ROLE IN CONFIG -> CHANGE IN GAME + const newAnimal = new Animal(name, path, tradeValue, role); + return [newAnimal, initialStock]; + }, + ); } - // updateNumberOfAnimals(index: number, newNumberOfAnimals: number) {} - addAnimals(animalName: AnimalNames, numberToAdd: number): Herd { - this.animals = this.animals.map((animalData) => { - const name = animalData[0].theName; - if (name === animalName) { - animalData[1] += numberToAdd; - } - return animalData; + // TODO: ADD METHOD DESCRIPTION + private findAnimalIndex(animalName: AnimalNames): number { + const indexOfAnimal = this.animals.findIndex((animal) => { + if (animal[0].theName === animalName) return true; }); - return this; + if (indexOfAnimal === -1) + throw new Error(`Animal: ${animalName} not found`); + return indexOfAnimal; } - + // TODO: ADD METHOD DESCRIPTION + private addAnimalNumbers( + currentNumber: number, + numberToAdd: number, + ): number { + return _.add(currentNumber, numberToAdd); + } + // TODO: ADD METHOD DESCRIPTION + private substractAnimalNumbers( + currentNumber: number, + numberToSubstract: number, + ): number { + return _.subtract(currentNumber, numberToSubstract); + } + // TODO: ADD METHOD DESCRIPTION + private updateNumberOfAnimals( + index: number, + newNumberOfAnimals: number, + ): void { + this.animals[index][1] = newNumberOfAnimals; + } + // TODO: ADD METHOD DESCRIPTION + addAnimalsToHerd( + animalName: AnimalNames, + numberToAdd: number, + ): void { + const animalIndex = this.findAnimalIndex(animalName); + const animalTuple = this.animals[animalIndex]; + const newNumber = this.addAnimalNumbers( + animalTuple[1], + numberToAdd, + ); + this.updateNumberOfAnimals(animalIndex, newNumber); + } + // TODO: ADD METHOD DESCRIPTION + removeAnimalsFromHerd( + animalName: AnimalNames, + numberToSubstract: number, + ): void { + const animalIndex = this.findAnimalIndex(animalName); + const animalTuple = this.animals[animalIndex]; + const newNumber = this.substractAnimalNumbers( + animalTuple[1], + numberToSubstract, + ); + this.updateNumberOfAnimals(animalIndex, newNumber); + } + // TODO: ADD METHOD DESCRIPTION get theAnimals(): [Animal, number][] { return this.animals; } - + // TODO: ADD METHOD DESCRIPTION getAnimalNumber(animalName: AnimalNames): number { - const animalToCheck = this.animals.find( - (animalData) => animalData[0].theName === animalName, - ); - return animalToCheck ? animalToCheck[1] : 0; + const animalToCheck = this.animals[ + this.findAnimalIndex(animalName) + ]; + return animalToCheck[1]; } - + // TODO: ADD METHOD DESCRIPTION private cullAllAnimalsOfOneType(animalName: AnimalNames): void { - const currentNumberOfAnimals = this.getAnimalNumber(animalName); - this.addAnimals(animalName, -currentNumberOfAnimals); + const indexOfAnimalTuple = this.findAnimalIndex(animalName); + this.animals[indexOfAnimalTuple][1] = 0; } - + // TODO: ADD METHOD DESCRIPTION private cullAllAnimalsOfGivenTypes( animalNames: AnimalNames[], ): void { @@ -67,7 +111,9 @@ export class Herd { this.cullAllAnimalsOfOneType(animal); }); } - + // TODO: ADD METHOD DESCRIPTION + // TODO: Check parameteres type + // TODO: Modify to use config cullAnimals(attackingAnimal: Fox | Wolf): void { switch (attackingAnimal.theName) { case AnimalNames.FOX: { @@ -78,8 +124,8 @@ export class Herd { this.cullAllAnimalsOfOneType(AnimalNames.RABBIT); return; } - this.addAnimals(AnimalNames.SMALL_DOG, -1); - (this.animals[5][0] as SmallDog).protectHerd(); + this.removeAnimalsFromHerd(AnimalNames.SMALL_DOG, 1); + // (this.animals[5][0] as SmallDog).protectHerd(); break; } case AnimalNames.WOLF: { @@ -95,8 +141,8 @@ export class Herd { ]); return; } - this.addAnimals(AnimalNames.BIG_DOG, -1); - (this.animals[6][0] as BigDog).protectHerd(); + this.removeAnimalsFromHerd(AnimalNames.BIG_DOG, 1); + // (this.animals[6][0] as BigDog).protectHerd(); } } } diff --git a/src/app/logic/mockGameConfiguration.ts b/src/app/logic/mockGameConfiguration.ts new file mode 100644 index 0000000..5fd9400 --- /dev/null +++ b/src/app/logic/mockGameConfiguration.ts @@ -0,0 +1,107 @@ +import { AnimalNames } from '~src/Enums/AnimalNamesEnum'; +import { AnimalRoles } from '~src/Enums/AnimalRolesEnum'; +import { GameModes } from '~src/Enums/GameModeEnums'; +import { GameConfigInterface } from '~src/Interfaces/GameConfigInterface'; + +export const defaultGameConfiguration: GameConfigInterface = { + mode: GameModes.STATIC, + roundTimeInSeconds: 15, + playersConfig: [ + { + name: 'Carlos Santanos', + path: '../../static/images/avatars/dog.png', + }, + { + name: 'Pablo Escofarmo', + path: '../../static/images/avatars/cow.png', + }, + ], + + herdConfig: [ + { + name: AnimalNames.RABBIT, + tradeValue: 1, + role: AnimalRoles.LIVESTOCK, + playersInitialStock: 0, + bankInitialStock: 60, + dice: [ + { diceNumber: 1, probability: 6 }, + { diceNumber: 2, probability: 6 }, + ], + }, + { + name: AnimalNames.SHEEP, + tradeValue: 6, + role: AnimalRoles.LIVESTOCK, + playersInitialStock: 0, + bankInitialStock: 24, + dice: [ + { diceNumber: 1, probability: 2 }, + { diceNumber: 2, probability: 3 }, + ], + }, + { + name: AnimalNames.PIG, + tradeValue: 12, + role: AnimalRoles.LIVESTOCK, + playersInitialStock: 0, + bankInitialStock: 20, + dice: [ + { diceNumber: 1, probability: 2 }, + { diceNumber: 2, probability: 1 }, + ], + }, + { + name: AnimalNames.COW, + tradeValue: 36, + role: AnimalRoles.LIVESTOCK, + playersInitialStock: 0, + bankInitialStock: 12, + dice: [{ diceNumber: 2, probability: 1 }], + }, + { + name: AnimalNames.HORSE, + tradeValue: 72, + role: AnimalRoles.LIVESTOCK, + playersInitialStock: 0, + bankInitialStock: 4, + dice: [{ diceNumber: 1, probability: 1 }], + }, + ], + predatorAnimalsConfig: [ + { + name: AnimalNames.FOX, + kills: [AnimalNames.RABBIT], + isChasedAwayBy: [AnimalNames.SMALL_DOG], + dice: [{ diceNumber: 1, probability: 1 }], + }, + { + name: AnimalNames.WOLF, + kills: [ + AnimalNames.RABBIT, + AnimalNames.SHEEP, + AnimalNames.PIG, + AnimalNames.COW, + ], + isChasedAwayBy: [AnimalNames.BIG_DOG], + dice: [{ diceNumber: 2, probability: 1 }], + }, + ], +}; + +// diceConfig: [ +// [ +// { name: AnimalNames.RABBIT, probability: 6 }, +// { name: AnimalNames.SHEEP, probability: 2 }, +// { name: AnimalNames.PIG, probability: 2 }, +// { name: AnimalNames.HORSE, probability: 1 }, +// { name: AnimalNames.FOX, probability: 1 }, +// ], +// [ +// { name: AnimalNames.RABBIT, probability: 6 }, +// { name: AnimalNames.SHEEP, probability: 3 }, +// { name: AnimalNames.PIG, probability: 1 }, +// { name: AnimalNames.COW, probability: 1 }, +// { name: AnimalNames.FOX, probability: 1 }, +// ], +// ], diff --git a/src/app/logic/mockHerdConfig.ts b/src/app/logic/mockHerdConfig.ts new file mode 100644 index 0000000..5d2358c --- /dev/null +++ b/src/app/logic/mockHerdConfig.ts @@ -0,0 +1,63 @@ +import { Value } from '../../Animals/Animal'; +import { AnimalNames } from '../../Enums/AnimalNamesEnum'; +import { AnimalRoles } from '../../Enums/AnimalRolesEnum'; + +interface playersHerdConfigInterface { + name: AnimalNames; + tradeValue: Value; + role: AnimalRoles; + path: string; + initialStock: number; +} + +export const mockHerdConfig: playersHerdConfigInterface[] = [ + { + name: AnimalNames.RABBIT, + tradeValue: 1, + path: '/static/images/avatars/rabbit.png', + role: AnimalRoles.LIVESTOCK, + initialStock: 0, + }, + { + name: AnimalNames.SHEEP, + tradeValue: 6, + path: '/static/images/avatars/sheep.png', + role: AnimalRoles.LIVESTOCK, + initialStock: 0, + }, + { + name: AnimalNames.PIG, + tradeValue: 12, + path: '/static/images/avatars/pig.png', + role: AnimalRoles.LIVESTOCK, + initialStock: 0, + }, + { + name: AnimalNames.COW, + tradeValue: 36, + path: '/static/images/avatars/cow.png', + role: AnimalRoles.LIVESTOCK, + initialStock: 0, + }, + { + name: AnimalNames.HORSE, + tradeValue: 72, + path: '/static/images/avatars/horse.png', + role: AnimalRoles.LIVESTOCK, + initialStock: 0, + }, + { + name: AnimalNames.SMALL_DOG, + tradeValue: 6, + path: '/static/images/avatars/dog.png', + role: AnimalRoles.GUARDIAN, + initialStock: 0, + }, + { + name: AnimalNames.BIG_DOG, + tradeValue: 36, + path: '/static/images/avatars/dog.png', + role: AnimalRoles.GUARDIAN, + initialStock: 0, + }, +]; diff --git a/src/app/manuals/herdManual.ts b/src/app/manuals/herdManual.ts new file mode 100644 index 0000000..731d881 --- /dev/null +++ b/src/app/manuals/herdManual.ts @@ -0,0 +1,74 @@ +import { Value } from '../../Animals/Animal'; +import { AnimalNames } from '../../Enums/AnimalNamesEnum'; +import { AnimalRoles } from '../../Enums/AnimalRolesEnum'; +import { Herd } from '../logic/Herd'; + +export function herdDemo(): void { + interface playersHerdConfigInterface { + name: AnimalNames; + tradeValue: Value; + role: AnimalRoles; + path: string; + initialStock: number; + } + + const mockHerdConfig: playersHerdConfigInterface[] = [ + { + name: AnimalNames.RABBIT, + tradeValue: 1, + path: '/static/images/avatars/rabbit.png', + role: AnimalRoles.LIVESTOCK, + initialStock: 0, + }, + { + name: AnimalNames.SHEEP, + tradeValue: 6, + path: '/static/images/avatars/sheep.png', + role: AnimalRoles.LIVESTOCK, + initialStock: 0, + }, + { + name: AnimalNames.PIG, + tradeValue: 12, + path: '/static/images/avatars/pig.png', + role: AnimalRoles.LIVESTOCK, + initialStock: 0, + }, + { + name: AnimalNames.COW, + tradeValue: 36, + path: '/static/images/avatars/cow.png', + role: AnimalRoles.LIVESTOCK, + initialStock: 0, + }, + { + name: AnimalNames.HORSE, + tradeValue: 72, + path: '/static/images/avatars/horse.png', + role: AnimalRoles.LIVESTOCK, + initialStock: 0, + }, + { + name: AnimalNames.SMALL_DOG, + tradeValue: 6, + path: '/static/images/avatars/dog.png', + role: AnimalRoles.GUARDIAN, + initialStock: 0, + }, + { + name: AnimalNames.BIG_DOG, + tradeValue: 36, + path: '/static/images/avatars/dog.png', + role: AnimalRoles.GUARDIAN, + initialStock: 0, + }, + ]; + + const newHerd = new Herd(mockHerdConfig); + + console.log(JSON.parse(JSON.stringify(newHerd.theAnimals))); + + console.log(newHerd.getAnimalNumber(AnimalNames.COW)); + newHerd.addAnimalsToHerd(AnimalNames.RABBIT, 10); + console.log(JSON.parse(JSON.stringify(newHerd.theAnimals))); +} diff --git a/test/herd.spec.ts b/test/herd.spec.ts index 5ba47b8..e573abb 100644 --- a/test/herd.spec.ts +++ b/test/herd.spec.ts @@ -1,4 +1,4 @@ -import { Animal } from '../src/Animals/Animal'; +import { Animal, Value } from '../src/Animals/Animal'; import { BigDog } from '../src/Animals/BigDog'; import { Cow } from '../src/Animals/Cow'; import { Fox } from '../src/Animals/Fox'; @@ -10,18 +10,22 @@ import { SmallDog } from '../src/Animals/SmallDog'; import { Wolf } from '../src/Animals/Wolf'; import { AnimalNames } from '../src/Enums/AnimalNamesEnum'; import { Herd } from '../src/app/logic/Herd'; +import { AnimalRoles } from '../src/Enums/AnimalRolesEnum'; +import { mockHerdConfig } from '../src/app/logic/mockHerdConfig'; +// const newHerd = new Herd(mockHerdConfig); describe('Herds method', () => { describe('addAnimal, given mock data', () => { - const testedHerd = new Herd(); - testedHerd.addAnimals(AnimalNames.SHEEP, 2); + const testedHerd = new Herd(mockHerdConfig); + testedHerd.addAnimalsToHerd(AnimalNames.SHEEP, 2); it('should increment the number of specific animal', () => { expect(testedHerd.theAnimals[1][1]).toBe(2); }); }); describe('cullAnimals, given mock data', () => { - const testedHerd = new Herd(10, 10, 10, 10, 10); + const testedHerd = new Herd(mockHerdConfig); + testedHerd.addAnimalsToHerd(AnimalNames.RABBIT, 10); testedHerd.cullAnimals(new Fox()); it('should modify the number of specific animal', () => { expect(testedHerd.theAnimals[0][1]).toBe(0); @@ -29,7 +33,9 @@ describe('Herds method', () => { }); describe('cullAnimals, given mock data', () => { - const testedHerd = new Herd(10, 10, 10, 10, 10, 1, 1); + const testedHerd = new Herd(mockHerdConfig); + testedHerd.addAnimalsToHerd(AnimalNames.SMALL_DOG, 1); + testedHerd.addAnimalsToHerd(AnimalNames.RABBIT, 10); testedHerd.cullAnimals(new Fox()); it('should modify the number of specific animal', () => { expect(testedHerd.theAnimals[5][1]).toBe(0); @@ -38,7 +44,9 @@ describe('Herds method', () => { }); describe('cullAnimals, given mock data', () => { - const testedHerd = new Herd(10, 10, 10, 10, 10, 1, 1); + const testedHerd = new Herd(mockHerdConfig); + testedHerd.addAnimalsToHerd(AnimalNames.BIG_DOG, 1); + testedHerd.addAnimalsToHerd(AnimalNames.SHEEP, 10); testedHerd.cullAnimals(new Wolf()); it('should modify the number of specific animal', () => { expect(testedHerd.theAnimals[6][1]).toBe(0); @@ -47,7 +55,9 @@ describe('Herds method', () => { }); describe('getAnimalNumber, given mock data', () => { - const testedHerd = new Herd(10, 10, 10, 10, 10, 1, 1); + const testedHerd = new Herd(mockHerdConfig); + testedHerd.addAnimalsToHerd(AnimalNames.BIG_DOG, 1); + testedHerd.addAnimalsToHerd(AnimalNames.RABBIT, 10); it('should modify the number of specific animal', () => { expect(testedHerd.getAnimalNumber(AnimalNames.RABBIT)).toBe(10); expect(testedHerd.getAnimalNumber(AnimalNames.BIG_DOG)).toBe(1); From f50b2568aa9325fe41182c90f4870c22f00eb8fb Mon Sep 17 00:00:00 2001 From: VieraBoschkova Date: Sun, 31 Jan 2021 01:08:40 +0100 Subject: [PATCH 15/22] format --- src/Interfaces/GameConfigInterface.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Interfaces/GameConfigInterface.ts b/src/Interfaces/GameConfigInterface.ts index 69038f0..d8a6faf 100644 --- a/src/Interfaces/GameConfigInterface.ts +++ b/src/Interfaces/GameConfigInterface.ts @@ -25,4 +25,4 @@ export interface predatorAnimalInterface { kills: AnimalNames[]; isChasedAwayBy: [AnimalNames]; dice?: { diceNumber: number; probability: number }[]; -} \ No newline at end of file +} From 596daea8a4df422854a90343bf50c013af3d2bb3 Mon Sep 17 00:00:00 2001 From: VieraBoschkova Date: Sun, 31 Jan 2021 13:57:16 +0100 Subject: [PATCH 16/22] refactor: how herd is invoked in other files --- src/Player.ts | 17 +++- src/app/App.ts | 2 - src/app/logic/Bank.ts | 18 +++- src/app/logic/Herd.ts | 117 +++++++++++++++++-------- src/app/logic/mockGameConfiguration.ts | 8 +- src/app/manuals/basicModalManual.ts | 2 +- src/app/manuals/menuWindowManual.ts | 2 +- test/herd.spec.ts | 18 ++-- 8 files changed, 127 insertions(+), 57 deletions(-) diff --git a/src/Player.ts b/src/Player.ts index 15325bc..2dad784 100644 --- a/src/Player.ts +++ b/src/Player.ts @@ -1,4 +1,7 @@ +import { Value } from './Animals/Animal'; import { Herd } from './app/logic/Herd'; +import { AnimalNames } from './Enums/AnimalNamesEnum'; +import { AnimalRoles } from './Enums/AnimalRolesEnum'; export class Player { protected name: string; @@ -6,9 +9,19 @@ export class Player { protected herd: Herd; // TODO: set path to default avatar when it's available - constructor(name: string, avatar = 'path to default avatar') { + constructor( + name: string, + avatar = 'path to default avatar', + playersHerdConfig: { + name: AnimalNames; + tradeValue: Value; + role: AnimalRoles; + path: string; + initialStock: number; + }[], + ) { this.name = name; - this.herd = new Herd(); + this.herd = new Herd(playersHerdConfig); this.avatar = avatar; } diff --git a/src/app/App.ts b/src/app/App.ts index c53ce9b..3b63de4 100644 --- a/src/app/App.ts +++ b/src/app/App.ts @@ -1,7 +1,5 @@ -import { herdDemo } from '../app/manuals/herdManual'; export class App { init(): string { - herdDemo(); return 'hello world'; } } diff --git a/src/app/logic/Bank.ts b/src/app/logic/Bank.ts index f6ad6c8..2808315 100644 --- a/src/app/logic/Bank.ts +++ b/src/app/logic/Bank.ts @@ -1,9 +1,21 @@ +import { Value } from '../../Animals/Animal'; +import { AnimalNames } from '../../Enums/AnimalNamesEnum'; +import { AnimalRoles } from '../../Enums/AnimalRolesEnum'; import { Player } from '../../Player'; -import { Herd } from './Herd'; +// import { Herd } from './Herd'; export class Bank extends Player { - constructor() { - super('bank'); + constructor( + banksHerdConfig: { + name: AnimalNames; + tradeValue: Value; + role: AnimalRoles; + path: string; + initialStock: number; + }[], + ) { + super('bank', '', banksHerdConfig); + // TO BE DIFNED BY GAME'S CONFIG: // this.herd = new Herd(60, 24, 20, 12, 4, 4, 2); } } diff --git a/src/app/logic/Herd.ts b/src/app/logic/Herd.ts index 7f239f9..364bb5f 100644 --- a/src/app/logic/Herd.ts +++ b/src/app/logic/Herd.ts @@ -1,14 +1,7 @@ import _ from 'lodash'; -import { AnimalRoles } from '~src/Enums/AnimalRolesEnum'; +import { AnimalRoles } from '../../Enums/AnimalRolesEnum'; import { Animal, Value } from '../../Animals/Animal'; -import { BigDog } from '../../Animals/BigDog'; -// import { Cow } from '../../Animals/Cow'; import { Fox } from '../../Animals/Fox'; -// import { Horse } from '../../Animals/Horse'; -// import { Pig } from '../../Animals/Pig'; -// import { Rabbit } from '../../Animals/Rabbit'; -// import { Sheep } from '../../Animals/Sheep'; -import { SmallDog } from '../../Animals/SmallDog'; import { Wolf } from '../../Animals/Wolf'; import { AnimalNames } from '../../Enums/AnimalNamesEnum'; @@ -31,8 +24,12 @@ export class Herd { }, ); } - // TODO: ADD METHOD DESCRIPTION - private findAnimalIndex(animalName: AnimalNames): number { + /** + * Finds the index of the tuple with given animal's name. + * @param {AnimalNames} animalName The animal name to search in animals tuples array. + * @return {number} The index of the tuple with searched animal. + */ + private findAnimalTupleIndex(animalName: AnimalNames): number { const indexOfAnimal = this.animals.findIndex((animal) => { if (animal[0].theName === animalName) return true; }); @@ -40,70 +37,115 @@ export class Herd { throw new Error(`Animal: ${animalName} not found`); return indexOfAnimal; } - // TODO: ADD METHOD DESCRIPTION - private addAnimalNumbers( + + /** + * Adds two numbers. + * @param {number} currentNumber Current number of animals in the herd. + * @param {number} numberToAdd Number to add to the current number of animals in the herd. + * @return {number} New number of animals in the herd. + */ + private addNumbers( currentNumber: number, numberToAdd: number, ): number { return _.add(currentNumber, numberToAdd); } - // TODO: ADD METHOD DESCRIPTION - private substractAnimalNumbers( + + /** + * Substracts two numbers. + * @param {number} currentNumber Current number of animals in the herd. + * @param {number} numberToSubstract Number to substract from the current number of animals in the herd. + * @return {number} New number of animals in the herd. + */ + private substractNumbers( currentNumber: number, numberToSubstract: number, ): number { return _.subtract(currentNumber, numberToSubstract); } - // TODO: ADD METHOD DESCRIPTION + + /** + * Updates the number of animals in the herd. + * @param {number} indexOfTupleAnimal Current number of animals in the herd. + * @param {number} newNumberOfAnimals Number to be the new number of animals in the herd. + */ private updateNumberOfAnimals( - index: number, + indexOfTupleAnimal: number, newNumberOfAnimals: number, ): void { - this.animals[index][1] = newNumberOfAnimals; + this.animals[indexOfTupleAnimal][1] = newNumberOfAnimals; } - // TODO: ADD METHOD DESCRIPTION + + /** + * Finds the index of the chosen animals tuple, adds the number of the animals in the herd + * to the incoming number of animals, updates the number in animals tuple array. + * updates the number of animals in the herd. + * @param {AnimalNames} animalName Name of the animal to be updated. + * @param {number} numberToAdd Number to add to the number of animals in the herd. + */ addAnimalsToHerd( animalName: AnimalNames, numberToAdd: number, ): void { - const animalIndex = this.findAnimalIndex(animalName); + const animalIndex = this.findAnimalTupleIndex(animalName); const animalTuple = this.animals[animalIndex]; - const newNumber = this.addAnimalNumbers( - animalTuple[1], - numberToAdd, - ); + const newNumber = this.addNumbers(animalTuple[1], numberToAdd); this.updateNumberOfAnimals(animalIndex, newNumber); } - // TODO: ADD METHOD DESCRIPTION + + /** + * Finds the index of the chosen animals tuple, substracts the given number of the animals from + * the number of animals in the herd, updates the number in animals tuple array. + * updates the number of animals in the herd. + * @param {number} animalName Name of the animal to be updated. + * @param {number} numberToSubstract Number to substract from the number of animals in the herd. + */ removeAnimalsFromHerd( animalName: AnimalNames, numberToSubstract: number, ): void { - const animalIndex = this.findAnimalIndex(animalName); + const animalIndex = this.findAnimalTupleIndex(animalName); const animalTuple = this.animals[animalIndex]; - const newNumber = this.substractAnimalNumbers( + const newNumber = this.substractNumbers( animalTuple[1], numberToSubstract, ); this.updateNumberOfAnimals(animalIndex, newNumber); } - // TODO: ADD METHOD DESCRIPTION + + /** + * Returns the object's array of animals tuples. + * @return {[Animal, number][]} Array of animals tuples in herd. + */ get theAnimals(): [Animal, number][] { return this.animals; } - // TODO: ADD METHOD DESCRIPTION + + /** + * Gets the number of the animals in the herd with given name. + * @param {AnimalNames} animalName The animal name to search. + * @return {number} The current number of animals of this name in the herd. + */ getAnimalNumber(animalName: AnimalNames): number { const animalToCheck = this.animals[ - this.findAnimalIndex(animalName) + this.findAnimalTupleIndex(animalName) ]; return animalToCheck[1]; } - // TODO: ADD METHOD DESCRIPTION + + /** + * Reduces to zero the number of the animals in the herd with given name. + * @param {AnimalNames} animalName The animal name to search and to be culled. + */ private cullAllAnimalsOfOneType(animalName: AnimalNames): void { - const indexOfAnimalTuple = this.findAnimalIndex(animalName); + const indexOfAnimalTuple = this.findAnimalTupleIndex(animalName); this.animals[indexOfAnimalTuple][1] = 0; } - // TODO: ADD METHOD DESCRIPTION + + /** + * Reduces to zero the number of the animals in the herd with given names. + * @param {AnimalNames[]} animalName The animal's names array to search and to be culled. + */ private cullAllAnimalsOfGivenTypes( animalNames: AnimalNames[], ): void { @@ -111,9 +153,14 @@ export class Herd { this.cullAllAnimalsOfOneType(animal); }); } - // TODO: ADD METHOD DESCRIPTION - // TODO: Check parameteres type - // TODO: Modify to use config + + /** + * Depending on the attacking animal, it checks if there is a herd protector for the given type of attacker, + * then reduces to zero the number of the animals in the herd or removes the protector, as is defined by game configuration. + * @param { Fox | Wolf } attackingAnimal The animal that is attacking the herd. + */ + // TODO: Check parameteres type. Create classes for protectors and predators if needed. + // TODO: Modify to use config? Define at refactor cullAnimals(attackingAnimal: Fox | Wolf): void { switch (attackingAnimal.theName) { case AnimalNames.FOX: { diff --git a/src/app/logic/mockGameConfiguration.ts b/src/app/logic/mockGameConfiguration.ts index 5fd9400..f31e99f 100644 --- a/src/app/logic/mockGameConfiguration.ts +++ b/src/app/logic/mockGameConfiguration.ts @@ -1,7 +1,7 @@ -import { AnimalNames } from '~src/Enums/AnimalNamesEnum'; -import { AnimalRoles } from '~src/Enums/AnimalRolesEnum'; -import { GameModes } from '~src/Enums/GameModeEnums'; -import { GameConfigInterface } from '~src/Interfaces/GameConfigInterface'; +import { AnimalNames } from '../../Enums/AnimalNamesEnum'; +import { AnimalRoles } from '../../Enums/AnimalRolesEnum'; +import { GameModes } from '../../Enums/GameModeEnums'; +import { GameConfigInterface } from '../../Interfaces/GameConfigInterface'; export const defaultGameConfiguration: GameConfigInterface = { mode: GameModes.STATIC, diff --git a/src/app/manuals/basicModalManual.ts b/src/app/manuals/basicModalManual.ts index 53ec56e..1806c6b 100644 --- a/src/app/manuals/basicModalManual.ts +++ b/src/app/manuals/basicModalManual.ts @@ -4,7 +4,7 @@ import { Button } from '../components/Button'; import { ModalBasic } from '../components/ModalBasic'; import { Render } from '../utils/Render'; -export function basicModalDemo() { +export function basicModalDemo(): void { // MOCK FOR MODAL const handleModalClose = () => { //TODO: WRITE LOGIC diff --git a/src/app/manuals/menuWindowManual.ts b/src/app/manuals/menuWindowManual.ts index 344a70e..707637a 100644 --- a/src/app/manuals/menuWindowManual.ts +++ b/src/app/manuals/menuWindowManual.ts @@ -1,7 +1,7 @@ import { MenuWindow } from '../components/MenuWindow'; import { Render } from '../utils/Render'; -export function menuWindowManual() { +export function menuWindowManual(): void { const menuWindowView = new MenuWindow().renderMenuWindow(); Render.render('#sf-app', menuWindowView); } diff --git a/test/herd.spec.ts b/test/herd.spec.ts index e573abb..3e562f8 100644 --- a/test/herd.spec.ts +++ b/test/herd.spec.ts @@ -1,16 +1,16 @@ -import { Animal, Value } from '../src/Animals/Animal'; -import { BigDog } from '../src/Animals/BigDog'; -import { Cow } from '../src/Animals/Cow'; +// import { Animal, Value } from '../src/Animals/Animal'; +// import { BigDog } from '../src/Animals/BigDog'; +// import { Cow } from '../src/Animals/Cow'; import { Fox } from '../src/Animals/Fox'; -import { Horse } from '../src/Animals/Horse'; -import { Pig } from '../src/Animals/Pig'; -import { Rabbit } from '../src/Animals/Rabbit'; -import { Sheep } from '../src/Animals/Sheep'; -import { SmallDog } from '../src/Animals/SmallDog'; +// import { Horse } from '../src/Animals/Horse'; +// import { Pig } from '../src/Animals/Pig'; +// import { Rabbit } from '../src/Animals/Rabbit'; +// import { Sheep } from '../src/Animals/Sheep'; +// import { SmallDog } from '../src/Animals/SmallDog'; import { Wolf } from '../src/Animals/Wolf'; import { AnimalNames } from '../src/Enums/AnimalNamesEnum'; import { Herd } from '../src/app/logic/Herd'; -import { AnimalRoles } from '../src/Enums/AnimalRolesEnum'; +// import { AnimalRoles } from '../src/Enums/AnimalRolesEnum'; import { mockHerdConfig } from '../src/app/logic/mockHerdConfig'; // const newHerd = new Herd(mockHerdConfig); From de4a0daf9443f289ad8501c7697cfeb6a40e2a93 Mon Sep 17 00:00:00 2001 From: VieraBoschkova Date: Sun, 31 Jan 2021 14:02:49 +0100 Subject: [PATCH 17/22] refactor: remove player init from game controller --- src/app/GameController.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/app/GameController.ts b/src/app/GameController.ts index 21f1873..d1cfd75 100644 --- a/src/app/GameController.ts +++ b/src/app/GameController.ts @@ -23,12 +23,6 @@ export class GameController { return this.timer; } - initializePlayer(name: string, imgPath: string): Player { - this.player = new Player(name, imgPath); - this.currentPlayer = this.player; - return this.player; - } - startTurn(): void { this.timer.countdown(); const turnTimer = setInterval(() => { From bc836461301e247e161bf26ca9ad67c47b957603 Mon Sep 17 00:00:00 2001 From: VieraBoschkova Date: Sun, 31 Jan 2021 14:06:05 +0100 Subject: [PATCH 18/22] refactor: remove players intialization from view --- src/app/View.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/app/View.ts b/src/app/View.ts index 6fb020f..9ecdb85 100644 --- a/src/app/View.ts +++ b/src/app/View.ts @@ -79,10 +79,10 @@ export class View { src: playersChosenAvatarPath, }); const gameController = new GameController(this); - gameController.initializePlayer( - playersChosenName, - playersChosenAvatarPath, - ); + // gameController.initializePlayer( + // playersChosenName, + // playersChosenAvatarPath, + // ); const remainingTime = Render.elementFactory('div', { id: 'time-remaining', className: 'remainig-time__counter', From 9400e06daeb03c9e397b1d0746b3ca3bb4cbd7cc Mon Sep 17 00:00:00 2001 From: VieraBoschkova Date: Mon, 1 Feb 2021 18:28:15 +0100 Subject: [PATCH 19/22] refactor: create separete interfaces refactor: remove unused imports --- src/Interfaces/GameConfigInterface.ts | 28 ++++++--------------- src/Interfaces/HerdConfigInterface.ts | 12 +++++++++ src/Interfaces/LivestockConfigInterface.ts | 11 ++++++++ src/Interfaces/PredatorsConfigInterface.ts | 8 ++++++ src/Interfaces/ProtectorsConfigInterface.ts | 7 ++++++ src/Player.ts | 17 +++++-------- src/app/View.ts | 4 --- src/app/logic/Bank.ts | 17 +++---------- src/app/logic/Game.ts | 0 src/app/logic/Herd.ts | 22 ++++++---------- src/app/logic/mockHerdConfig.ts | 28 ++++++++------------- test/herd.spec.ts | 10 -------- 12 files changed, 72 insertions(+), 92 deletions(-) create mode 100644 src/Interfaces/HerdConfigInterface.ts create mode 100644 src/Interfaces/LivestockConfigInterface.ts create mode 100644 src/Interfaces/PredatorsConfigInterface.ts create mode 100644 src/Interfaces/ProtectorsConfigInterface.ts create mode 100644 src/app/logic/Game.ts diff --git a/src/Interfaces/GameConfigInterface.ts b/src/Interfaces/GameConfigInterface.ts index d8a6faf..f124104 100644 --- a/src/Interfaces/GameConfigInterface.ts +++ b/src/Interfaces/GameConfigInterface.ts @@ -1,28 +1,14 @@ import { GameModes } from '../Enums/GameModeEnums'; -import { AnimalNames } from '../Enums/AnimalNamesEnum'; -import { AnimalRoles } from '../Enums/AnimalRolesEnum'; +import { LivestockConfigInterface } from './LivestockConfigInterface'; +import { PredatorsConfigInterface } from './PredatorsConfigInterface'; +import { ProtectorsConfigInterface } from './ProtectorsConfigInterface'; export interface GameConfigInterface { mode: GameModes; roundTimeInSeconds: number; totalGameTimeInSeconds?: number; - playersConfig: { name: string; path: string }[]; - herdConfig: herdConfigInterface[]; - predatorAnimalsConfig: predatorAnimalInterface[]; -} - -export interface herdConfigInterface { - name: AnimalNames; - tradeValue: number; - role: AnimalRoles; - playersInitialStock: number; - bankInitialStock: number; - dice?: { diceNumber: number; probability: number }[]; -} - -export interface predatorAnimalInterface { - name: AnimalNames; - kills: AnimalNames[]; - isChasedAwayBy: [AnimalNames]; - dice?: { diceNumber: number; probability: number }[]; + playersConfig: { name: string; path: string; color: string }[]; + livestockConfig: LivestockConfigInterface[]; + protectorsConfig: ProtectorsConfigInterface[]; + predatorsConfig: PredatorsConfigInterface[]; } diff --git a/src/Interfaces/HerdConfigInterface.ts b/src/Interfaces/HerdConfigInterface.ts new file mode 100644 index 0000000..4939e12 --- /dev/null +++ b/src/Interfaces/HerdConfigInterface.ts @@ -0,0 +1,12 @@ +import { Value } from '../Animals/Animal'; +import { AnimalNames } from '../Enums/AnimalNamesEnum'; +import { AnimalRoles } from '../Enums/AnimalRolesEnum'; + +export interface HerdConfigInterface { + name: AnimalNames; + tradeValue: Value; + role: AnimalRoles; + path: string; + inStock: number; + chasesAway?: AnimalNames; +} diff --git a/src/Interfaces/LivestockConfigInterface.ts b/src/Interfaces/LivestockConfigInterface.ts new file mode 100644 index 0000000..812c1fa --- /dev/null +++ b/src/Interfaces/LivestockConfigInterface.ts @@ -0,0 +1,11 @@ +import { AnimalNames } from '../Enums/AnimalNamesEnum'; +import { AnimalRoles } from '../Enums/AnimalRolesEnum'; + +export interface LivestockConfigInterface { + name: AnimalNames; + tradeValue: number; + roles: AnimalRoles; + playersInitialStock?: number; + bankInitialStock?: number; + dice?: { diceNumber: number; probability: number }[]; +} diff --git a/src/Interfaces/PredatorsConfigInterface.ts b/src/Interfaces/PredatorsConfigInterface.ts new file mode 100644 index 0000000..c9bd359 --- /dev/null +++ b/src/Interfaces/PredatorsConfigInterface.ts @@ -0,0 +1,8 @@ +import { AnimalNames } from '../Enums/AnimalNamesEnum'; + +export interface PredatorsConfigInterface { + name: AnimalNames; + kills: AnimalNames[]; + isChasedAwayBy: AnimalNames[]; + dice?: { diceNumber: number; probability: number }[]; +} diff --git a/src/Interfaces/ProtectorsConfigInterface.ts b/src/Interfaces/ProtectorsConfigInterface.ts new file mode 100644 index 0000000..118a938 --- /dev/null +++ b/src/Interfaces/ProtectorsConfigInterface.ts @@ -0,0 +1,7 @@ +import { AnimalNames } from '../Enums/AnimalNamesEnum'; +import { LivestockConfigInterface } from './LivestockConfigInterface'; + +export interface ProtectorsConfigInterface + extends LivestockConfigInterface { + chasesAway?: AnimalNames[]; +} diff --git a/src/Player.ts b/src/Player.ts index 2dad784..c5e8b98 100644 --- a/src/Player.ts +++ b/src/Player.ts @@ -1,28 +1,23 @@ -import { Value } from './Animals/Animal'; import { Herd } from './app/logic/Herd'; -import { AnimalNames } from './Enums/AnimalNamesEnum'; -import { AnimalRoles } from './Enums/AnimalRolesEnum'; +import { HerdConfigInterface } from './Interfaces/HerdConfigInterface'; export class Player { protected name: string; protected avatar: string; protected herd: Herd; + protected color: string; // TODO: set path to default avatar when it's available constructor( name: string, avatar = 'path to default avatar', - playersHerdConfig: { - name: AnimalNames; - tradeValue: Value; - role: AnimalRoles; - path: string; - initialStock: number; - }[], + color: string, + herdConfig: HerdConfigInterface[], ) { this.name = name; - this.herd = new Herd(playersHerdConfig); + this.herd = new Herd(herdConfig); this.avatar = avatar; + this.color = color; } get theName(): string { diff --git a/src/app/View.ts b/src/app/View.ts index 9ecdb85..70d8ac1 100644 --- a/src/app/View.ts +++ b/src/app/View.ts @@ -79,10 +79,6 @@ export class View { src: playersChosenAvatarPath, }); const gameController = new GameController(this); - // gameController.initializePlayer( - // playersChosenName, - // playersChosenAvatarPath, - // ); const remainingTime = Render.elementFactory('div', { id: 'time-remaining', className: 'remainig-time__counter', diff --git a/src/app/logic/Bank.ts b/src/app/logic/Bank.ts index 2808315..d4ed174 100644 --- a/src/app/logic/Bank.ts +++ b/src/app/logic/Bank.ts @@ -1,20 +1,9 @@ -import { Value } from '../../Animals/Animal'; -import { AnimalNames } from '../../Enums/AnimalNamesEnum'; -import { AnimalRoles } from '../../Enums/AnimalRolesEnum'; +import { HerdConfigInterface } from '~src/Interfaces/HerdConfigInterface'; import { Player } from '../../Player'; -// import { Herd } from './Herd'; export class Bank extends Player { - constructor( - banksHerdConfig: { - name: AnimalNames; - tradeValue: Value; - role: AnimalRoles; - path: string; - initialStock: number; - }[], - ) { - super('bank', '', banksHerdConfig); + constructor(banksHerdConfig: HerdConfigInterface[]) { + super('bank', '', '', banksHerdConfig); // TO BE DIFNED BY GAME'S CONFIG: // this.herd = new Herd(60, 24, 20, 12, 4, 4, 2); } diff --git a/src/app/logic/Game.ts b/src/app/logic/Game.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/app/logic/Herd.ts b/src/app/logic/Herd.ts index 364bb5f..11e0568 100644 --- a/src/app/logic/Herd.ts +++ b/src/app/logic/Herd.ts @@ -1,26 +1,18 @@ import _ from 'lodash'; -import { AnimalRoles } from '../../Enums/AnimalRolesEnum'; -import { Animal, Value } from '../../Animals/Animal'; +import { Animal } from '../../Animals/Animal'; import { Fox } from '../../Animals/Fox'; import { Wolf } from '../../Animals/Wolf'; import { AnimalNames } from '../../Enums/AnimalNamesEnum'; +import { HerdConfigInterface } from '../../Interfaces/HerdConfigInterface'; export class Herd { protected animals: [Animal, number][]; - constructor( - playersHerdConfig: { - name: AnimalNames; - tradeValue: Value; - role: AnimalRoles; - path: string; - initialStock: number; - }[], - ) { + constructor(playersHerdConfig: HerdConfigInterface[]) { this.animals = playersHerdConfig.map( - ({ name, tradeValue, role, path, initialStock }) => { + ({ name, tradeValue, role, path, inStock }) => { // TODO: ADD PATH TO ANIMAL IMAGE, VALUE, ROLE IN CONFIG -> CHANGE IN GAME const newAnimal = new Animal(name, path, tradeValue, role); - return [newAnimal, initialStock]; + return [newAnimal, inStock]; }, ); } @@ -30,8 +22,8 @@ export class Herd { * @return {number} The index of the tuple with searched animal. */ private findAnimalTupleIndex(animalName: AnimalNames): number { - const indexOfAnimal = this.animals.findIndex((animal) => { - if (animal[0].theName === animalName) return true; + const indexOfAnimal = this.animals.findIndex(([animal]) => { + if (animal.theName === animalName) return true; }); if (indexOfAnimal === -1) throw new Error(`Animal: ${animalName} not found`); diff --git a/src/app/logic/mockHerdConfig.ts b/src/app/logic/mockHerdConfig.ts index 5d2358c..1a9913c 100644 --- a/src/app/logic/mockHerdConfig.ts +++ b/src/app/logic/mockHerdConfig.ts @@ -1,63 +1,57 @@ -import { Value } from '../../Animals/Animal'; +import { HerdConfigInterface } from '../../Interfaces/HerdConfigInterface'; import { AnimalNames } from '../../Enums/AnimalNamesEnum'; import { AnimalRoles } from '../../Enums/AnimalRolesEnum'; -interface playersHerdConfigInterface { - name: AnimalNames; - tradeValue: Value; - role: AnimalRoles; - path: string; - initialStock: number; -} - -export const mockHerdConfig: playersHerdConfigInterface[] = [ +export const mockHerdConfig: HerdConfigInterface[] = [ { name: AnimalNames.RABBIT, tradeValue: 1, path: '/static/images/avatars/rabbit.png', role: AnimalRoles.LIVESTOCK, - initialStock: 0, + inStock: 0, }, { name: AnimalNames.SHEEP, tradeValue: 6, path: '/static/images/avatars/sheep.png', role: AnimalRoles.LIVESTOCK, - initialStock: 0, + inStock: 0, }, { name: AnimalNames.PIG, tradeValue: 12, path: '/static/images/avatars/pig.png', role: AnimalRoles.LIVESTOCK, - initialStock: 0, + inStock: 0, }, { name: AnimalNames.COW, tradeValue: 36, path: '/static/images/avatars/cow.png', role: AnimalRoles.LIVESTOCK, - initialStock: 0, + inStock: 0, }, { name: AnimalNames.HORSE, tradeValue: 72, path: '/static/images/avatars/horse.png', role: AnimalRoles.LIVESTOCK, - initialStock: 0, + inStock: 0, }, { name: AnimalNames.SMALL_DOG, tradeValue: 6, path: '/static/images/avatars/dog.png', role: AnimalRoles.GUARDIAN, - initialStock: 0, + inStock: 0, + chasesAway: AnimalNames.FOX, }, { name: AnimalNames.BIG_DOG, tradeValue: 36, path: '/static/images/avatars/dog.png', role: AnimalRoles.GUARDIAN, - initialStock: 0, + inStock: 0, + chasesAway: AnimalNames.WOLF, }, ]; diff --git a/test/herd.spec.ts b/test/herd.spec.ts index 3e562f8..1575265 100644 --- a/test/herd.spec.ts +++ b/test/herd.spec.ts @@ -1,19 +1,9 @@ -// import { Animal, Value } from '../src/Animals/Animal'; -// import { BigDog } from '../src/Animals/BigDog'; -// import { Cow } from '../src/Animals/Cow'; import { Fox } from '../src/Animals/Fox'; -// import { Horse } from '../src/Animals/Horse'; -// import { Pig } from '../src/Animals/Pig'; -// import { Rabbit } from '../src/Animals/Rabbit'; -// import { Sheep } from '../src/Animals/Sheep'; -// import { SmallDog } from '../src/Animals/SmallDog'; import { Wolf } from '../src/Animals/Wolf'; import { AnimalNames } from '../src/Enums/AnimalNamesEnum'; import { Herd } from '../src/app/logic/Herd'; -// import { AnimalRoles } from '../src/Enums/AnimalRolesEnum'; import { mockHerdConfig } from '../src/app/logic/mockHerdConfig'; -// const newHerd = new Herd(mockHerdConfig); describe('Herds method', () => { describe('addAnimal, given mock data', () => { const testedHerd = new Herd(mockHerdConfig); From 2a9d9073b81407ead35927a9644756271e4be735 Mon Sep 17 00:00:00 2001 From: VieraBoschkova Date: Mon, 1 Feb 2021 18:48:06 +0100 Subject: [PATCH 20/22] refactor: animal role --- src/Interfaces/LivestockConfigInterface.ts | 2 +- src/app/logic/mockGameConfiguration.ts | 22 +++++++++++++++-- src/app/manuals/herdManual.ts | 28 +++++++++------------- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/Interfaces/LivestockConfigInterface.ts b/src/Interfaces/LivestockConfigInterface.ts index 812c1fa..71f38f0 100644 --- a/src/Interfaces/LivestockConfigInterface.ts +++ b/src/Interfaces/LivestockConfigInterface.ts @@ -4,7 +4,7 @@ import { AnimalRoles } from '../Enums/AnimalRolesEnum'; export interface LivestockConfigInterface { name: AnimalNames; tradeValue: number; - roles: AnimalRoles; + role: AnimalRoles; playersInitialStock?: number; bankInitialStock?: number; dice?: { diceNumber: number; probability: number }[]; diff --git a/src/app/logic/mockGameConfiguration.ts b/src/app/logic/mockGameConfiguration.ts index f31e99f..5d167bf 100644 --- a/src/app/logic/mockGameConfiguration.ts +++ b/src/app/logic/mockGameConfiguration.ts @@ -10,14 +10,16 @@ export const defaultGameConfiguration: GameConfigInterface = { { name: 'Carlos Santanos', path: '../../static/images/avatars/dog.png', + color: 'blue', }, { name: 'Pablo Escofarmo', path: '../../static/images/avatars/cow.png', + color: 'green', }, ], - herdConfig: [ + livestockConfig: [ { name: AnimalNames.RABBIT, tradeValue: 1, @@ -68,7 +70,23 @@ export const defaultGameConfiguration: GameConfigInterface = { dice: [{ diceNumber: 1, probability: 1 }], }, ], - predatorAnimalsConfig: [ + protectorsConfig: [ + { + name: AnimalNames.SMALL_DOG, + tradeValue: 1, + role: AnimalRoles.GUARDIAN, + playersInitialStock: 0, + bankInitialStock: 60, + }, + { + name: AnimalNames.SHEEP, + tradeValue: 6, + role: AnimalRoles.GUARDIAN, + playersInitialStock: 0, + bankInitialStock: 24, + }, + ], + predatorsConfig: [ { name: AnimalNames.FOX, kills: [AnimalNames.RABBIT], diff --git a/src/app/manuals/herdManual.ts b/src/app/manuals/herdManual.ts index 731d881..db2a569 100644 --- a/src/app/manuals/herdManual.ts +++ b/src/app/manuals/herdManual.ts @@ -1,66 +1,60 @@ -import { Value } from '../../Animals/Animal'; +import { HerdConfigInterface } from '../../Interfaces/HerdConfigInterface'; import { AnimalNames } from '../../Enums/AnimalNamesEnum'; import { AnimalRoles } from '../../Enums/AnimalRolesEnum'; import { Herd } from '../logic/Herd'; export function herdDemo(): void { - interface playersHerdConfigInterface { - name: AnimalNames; - tradeValue: Value; - role: AnimalRoles; - path: string; - initialStock: number; - } - - const mockHerdConfig: playersHerdConfigInterface[] = [ + const mockHerdConfig: HerdConfigInterface[] = [ { name: AnimalNames.RABBIT, tradeValue: 1, path: '/static/images/avatars/rabbit.png', role: AnimalRoles.LIVESTOCK, - initialStock: 0, + inStock: 0, }, { name: AnimalNames.SHEEP, tradeValue: 6, path: '/static/images/avatars/sheep.png', role: AnimalRoles.LIVESTOCK, - initialStock: 0, + inStock: 0, }, { name: AnimalNames.PIG, tradeValue: 12, path: '/static/images/avatars/pig.png', role: AnimalRoles.LIVESTOCK, - initialStock: 0, + inStock: 0, }, { name: AnimalNames.COW, tradeValue: 36, path: '/static/images/avatars/cow.png', role: AnimalRoles.LIVESTOCK, - initialStock: 0, + inStock: 0, }, { name: AnimalNames.HORSE, tradeValue: 72, path: '/static/images/avatars/horse.png', role: AnimalRoles.LIVESTOCK, - initialStock: 0, + inStock: 0, }, { name: AnimalNames.SMALL_DOG, tradeValue: 6, path: '/static/images/avatars/dog.png', role: AnimalRoles.GUARDIAN, - initialStock: 0, + inStock: 0, + chasesAway: AnimalNames.FOX, }, { name: AnimalNames.BIG_DOG, tradeValue: 36, path: '/static/images/avatars/dog.png', role: AnimalRoles.GUARDIAN, - initialStock: 0, + inStock: 0, + chasesAway: AnimalNames.WOLF, }, ]; From d12828292728ef6f9e9d3ca336b33a2afc767711 Mon Sep 17 00:00:00 2001 From: VieraBoschkova Date: Mon, 1 Feb 2021 21:25:36 +0100 Subject: [PATCH 21/22] refactor: update herds methods --- src/Player.ts | 5 +++-- src/app/Trade.ts | 8 ++++---- src/app/logic/Herd.ts | 5 ++++- test/Trade.spec.ts | 20 ++++++++++---------- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/Player.ts b/src/Player.ts index c5e8b98..ded47b7 100644 --- a/src/Player.ts +++ b/src/Player.ts @@ -1,4 +1,5 @@ import { Herd } from './app/logic/Herd'; +import { mockHerdConfig } from './app/logic/mockHerdConfig'; import { HerdConfigInterface } from './Interfaces/HerdConfigInterface'; export class Player { @@ -11,8 +12,8 @@ export class Player { constructor( name: string, avatar = 'path to default avatar', - color: string, - herdConfig: HerdConfigInterface[], + color = 'green', + herdConfig: HerdConfigInterface[] = mockHerdConfig, ) { this.name = name; this.herd = new Herd(herdConfig); diff --git a/src/app/Trade.ts b/src/app/Trade.ts index e231ace..3a4551a 100644 --- a/src/app/Trade.ts +++ b/src/app/Trade.ts @@ -61,10 +61,10 @@ export class Trade { playerHerd: Herd, [animalBought, quantityBought]: [AnimalNames, number], ): boolean { - playerHerd.addAnimals(animalSold, -quantitySold); - playerHerd.addAnimals(animalBought, quantityBought); - this.bank.theHerd.addAnimals(animalSold, quantitySold); - this.bank.theHerd.addAnimals(animalBought, -quantityBought); + playerHerd.addAnimalsToHerd(animalSold, -quantitySold); + playerHerd.addAnimalsToHerd(animalBought, quantityBought); + this.bank.theHerd.addAnimalsToHerd(animalSold, quantitySold); + this.bank.theHerd.addAnimalsToHerd(animalBought, -quantityBought); return true; } } diff --git a/src/app/logic/Herd.ts b/src/app/logic/Herd.ts index 11e0568..77ec501 100644 --- a/src/app/logic/Herd.ts +++ b/src/app/logic/Herd.ts @@ -4,10 +4,13 @@ import { Fox } from '../../Animals/Fox'; import { Wolf } from '../../Animals/Wolf'; import { AnimalNames } from '../../Enums/AnimalNamesEnum'; import { HerdConfigInterface } from '../../Interfaces/HerdConfigInterface'; +import { mockHerdConfig } from './mockHerdConfig'; export class Herd { protected animals: [Animal, number][]; - constructor(playersHerdConfig: HerdConfigInterface[]) { + constructor( + playersHerdConfig: HerdConfigInterface[] = mockHerdConfig, + ) { this.animals = playersHerdConfig.map( ({ name, tradeValue, role, path, inStock }) => { // TODO: ADD PATH TO ANIMAL IMAGE, VALUE, ROLE IN CONFIG -> CHANGE IN GAME diff --git a/test/Trade.spec.ts b/test/Trade.spec.ts index b40a556..0794ad6 100644 --- a/test/Trade.spec.ts +++ b/test/Trade.spec.ts @@ -4,18 +4,18 @@ import { Trade } from '../src/app/Trade'; describe('Trade class test.', () => { const bank = new Player('bank'); - bank.theHerd.addAnimals(AnimalNames.RABBIT, 60); - bank.theHerd.addAnimals(AnimalNames.SHEEP, 24); - bank.theHerd.addAnimals(AnimalNames.PIG, 20); - bank.theHerd.addAnimals(AnimalNames.COW, 12); - bank.theHerd.addAnimals(AnimalNames.HORSE, 4); - bank.theHerd.addAnimals(AnimalNames.SMALL_DOG, 4); - bank.theHerd.addAnimals(AnimalNames.BIG_DOG, 2); + bank.theHerd.addAnimalsToHerd(AnimalNames.RABBIT, 60); + bank.theHerd.addAnimalsToHerd(AnimalNames.SHEEP, 24); + bank.theHerd.addAnimalsToHerd(AnimalNames.PIG, 20); + bank.theHerd.addAnimalsToHerd(AnimalNames.COW, 12); + bank.theHerd.addAnimalsToHerd(AnimalNames.HORSE, 4); + bank.theHerd.addAnimalsToHerd(AnimalNames.SMALL_DOG, 4); + bank.theHerd.addAnimalsToHerd(AnimalNames.BIG_DOG, 2); const trade = new Trade(bank); const player = new Player('player'); it('Should process trade with correct ammount', () => { - player.theHerd.addAnimals(AnimalNames.RABBIT, 6); + player.theHerd.addAnimalsToHerd(AnimalNames.RABBIT, 6); const offer: [AnimalNames, number] = [AnimalNames.RABBIT, 6]; const target: [AnimalNames, number] = [AnimalNames.SHEEP, 1]; const result = trade.processOffer(offer, player, target); @@ -24,7 +24,7 @@ describe('Trade class test.', () => { }); it('Should process trade with reducing players offer', () => { - player.theHerd.addAnimals(AnimalNames.COW, 3); + player.theHerd.addAnimalsToHerd(AnimalNames.COW, 3); const offer: [AnimalNames, number] = [AnimalNames.COW, 3]; const target: [AnimalNames, number] = [AnimalNames.PIG, 3]; const result = trade.processOffer(offer, player, target); @@ -34,7 +34,7 @@ describe('Trade class test.', () => { }); it('Should not process due to low offer', () => { - player.theHerd.addAnimals(AnimalNames.HORSE, 1); + player.theHerd.addAnimalsToHerd(AnimalNames.HORSE, 1); const offer: [AnimalNames, number] = [AnimalNames.HORSE, 1]; const target: [AnimalNames, number] = [AnimalNames.BIG_DOG, 1]; const result = trade.processOffer(offer, player, target); From 65557686bedc4f28809ad7fe05d0c3d3d04e02e9 Mon Sep 17 00:00:00 2001 From: VieraBoschkova Date: Mon, 1 Feb 2021 21:32:47 +0100 Subject: [PATCH 22/22] refactor: update herd's methods create banks default config --- src/app/logic/Bank.ts | 5 ++- src/app/logic/defaultBankConfig.ts | 57 ++++++++++++++++++++++++++++++ src/app/manuals/BreedPhaseDemo.ts | 14 ++++---- src/app/manuals/TradeDemo.ts | 6 ++-- 4 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 src/app/logic/defaultBankConfig.ts diff --git a/src/app/logic/Bank.ts b/src/app/logic/Bank.ts index d4ed174..df5e5f0 100644 --- a/src/app/logic/Bank.ts +++ b/src/app/logic/Bank.ts @@ -1,8 +1,11 @@ import { HerdConfigInterface } from '~src/Interfaces/HerdConfigInterface'; import { Player } from '../../Player'; +import { defaultBankConfig } from './defaultBankConfig'; export class Bank extends Player { - constructor(banksHerdConfig: HerdConfigInterface[]) { + constructor( + banksHerdConfig: HerdConfigInterface[] = defaultBankConfig, + ) { super('bank', '', '', banksHerdConfig); // TO BE DIFNED BY GAME'S CONFIG: // this.herd = new Herd(60, 24, 20, 12, 4, 4, 2); diff --git a/src/app/logic/defaultBankConfig.ts b/src/app/logic/defaultBankConfig.ts new file mode 100644 index 0000000..7045c4f --- /dev/null +++ b/src/app/logic/defaultBankConfig.ts @@ -0,0 +1,57 @@ +import { HerdConfigInterface } from '../../Interfaces/HerdConfigInterface'; +import { AnimalNames } from '../../Enums/AnimalNamesEnum'; +import { AnimalRoles } from '../../Enums/AnimalRolesEnum'; + +export const defaultBankConfig: HerdConfigInterface[] = [ + { + name: AnimalNames.RABBIT, + tradeValue: 1, + path: '/static/images/avatars/rabbit.png', + role: AnimalRoles.LIVESTOCK, + inStock: 60, + }, + { + name: AnimalNames.SHEEP, + tradeValue: 6, + path: '/static/images/avatars/sheep.png', + role: AnimalRoles.LIVESTOCK, + inStock: 24, + }, + { + name: AnimalNames.PIG, + tradeValue: 12, + path: '/static/images/avatars/pig.png', + role: AnimalRoles.LIVESTOCK, + inStock: 20, + }, + { + name: AnimalNames.COW, + tradeValue: 36, + path: '/static/images/avatars/cow.png', + role: AnimalRoles.LIVESTOCK, + inStock: 12, + }, + { + name: AnimalNames.HORSE, + tradeValue: 72, + path: '/static/images/avatars/horse.png', + role: AnimalRoles.LIVESTOCK, + inStock: 4, + }, + { + name: AnimalNames.SMALL_DOG, + tradeValue: 6, + path: '/static/images/avatars/dog.png', + role: AnimalRoles.GUARDIAN, + inStock: 4, + chasesAway: AnimalNames.FOX, + }, + { + name: AnimalNames.BIG_DOG, + tradeValue: 36, + path: '/static/images/avatars/dog.png', + role: AnimalRoles.GUARDIAN, + inStock: 2, + chasesAway: AnimalNames.WOLF, + }, +]; diff --git a/src/app/manuals/BreedPhaseDemo.ts b/src/app/manuals/BreedPhaseDemo.ts index 2a552ba..d13eaa3 100644 --- a/src/app/manuals/BreedPhaseDemo.ts +++ b/src/app/manuals/BreedPhaseDemo.ts @@ -5,13 +5,13 @@ import { BreedProcessor } from '../BreedProcessor'; export class BreedPhaseDemo { static playDemo(): void { const bank = new Player('bank'); - bank.theHerd.addAnimals(AnimalNames.RABBIT, 10); - bank.theHerd.addAnimals(AnimalNames.SHEEP, 10); - bank.theHerd.addAnimals(AnimalNames.PIG, 10); - bank.theHerd.addAnimals(AnimalNames.COW, 12); - bank.theHerd.addAnimals(AnimalNames.HORSE, 4); - bank.theHerd.addAnimals(AnimalNames.SMALL_DOG, 4); - bank.theHerd.addAnimals(AnimalNames.BIG_DOG, 2); + bank.theHerd.addAnimalsToHerd(AnimalNames.RABBIT, 10); + bank.theHerd.addAnimalsToHerd(AnimalNames.SHEEP, 10); + bank.theHerd.addAnimalsToHerd(AnimalNames.PIG, 10); + bank.theHerd.addAnimalsToHerd(AnimalNames.COW, 12); + bank.theHerd.addAnimalsToHerd(AnimalNames.HORSE, 4); + bank.theHerd.addAnimalsToHerd(AnimalNames.SMALL_DOG, 4); + bank.theHerd.addAnimalsToHerd(AnimalNames.BIG_DOG, 2); const bp = new BreedProcessor(bank); const player = new Player('player'); for (let i = 0; i < 10; i++) { diff --git a/src/app/manuals/TradeDemo.ts b/src/app/manuals/TradeDemo.ts index 92d53cf..f0f4e1e 100644 --- a/src/app/manuals/TradeDemo.ts +++ b/src/app/manuals/TradeDemo.ts @@ -10,9 +10,9 @@ export class TradeDemo { const bank = new Bank(); const trade = new Trade(bank); const player = new Player('player'); - player.theHerd.addAnimals(AnimalNames.SHEEP, 5); - player.theHerd.addAnimals(AnimalNames.PIG, 5); - player.theHerd.addAnimals(AnimalNames.COW, 2); + player.theHerd.addAnimalsToHerd(AnimalNames.SHEEP, 5); + player.theHerd.addAnimalsToHerd(AnimalNames.PIG, 5); + player.theHerd.addAnimalsToHerd(AnimalNames.COW, 2); const offer1: [AnimalNames, number] = [AnimalNames.SHEEP, 1]; const offer2: [AnimalNames, number] = [AnimalNames.SHEEP, 2]; const offer3: [AnimalNames, number] = [AnimalNames.COW, 1];