-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/#56-banks-board
- Loading branch information
Showing
29 changed files
with
957 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum GameModes { | ||
STATIC = 'static', | ||
DYNAMIC = 'dynamic', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { GameModes } from '../Enums/GameModeEnums'; | ||
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; color: string }[]; | ||
livestockConfig: LivestockConfigInterface[]; | ||
protectorsConfig: ProtectorsConfigInterface[]; | ||
predatorsConfig: PredatorsConfigInterface[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { AnimalNames } from '../Enums/AnimalNamesEnum'; | ||
import { AnimalRoles } from '../Enums/AnimalRolesEnum'; | ||
|
||
export interface LivestockConfigInterface { | ||
name: AnimalNames; | ||
tradeValue: number; | ||
role: AnimalRoles; | ||
playersInitialStock?: number; | ||
bankInitialStock?: number; | ||
dice?: { diceNumber: number; probability: number }[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { AnimalNames } from '../Enums/AnimalNamesEnum'; | ||
|
||
export interface PredatorsConfigInterface { | ||
name: AnimalNames; | ||
kills: AnimalNames[]; | ||
isChasedAwayBy: AnimalNames[]; | ||
dice?: { diceNumber: number; probability: number }[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { AnimalNames } from '../Enums/AnimalNamesEnum'; | ||
import { LivestockConfigInterface } from './LivestockConfigInterface'; | ||
|
||
export interface ProtectorsConfigInterface | ||
extends LivestockConfigInterface { | ||
chasesAway?: AnimalNames[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { AnimalNames } from '../Enums/AnimalNamesEnum'; | ||
import { ConvertToAnimalObject } from './utils/ConvertToAnimalObject'; | ||
import { multiply } 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.addAnimalsToHerd(animalSold, -quantitySold); | ||
playerHerd.addAnimalsToHerd(animalBought, quantityBought); | ||
this.bank.theHerd.addAnimalsToHerd(animalSold, quantitySold); | ||
this.bank.theHerd.addAnimalsToHerd(animalBought, -quantityBought); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.