Skip to content

Commit

Permalink
Finally finished tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneWWolf committed Jan 9, 2024
1 parent 0ee8d8e commit 1379c33
Show file tree
Hide file tree
Showing 35 changed files with 940 additions and 84 deletions.
12 changes: 9 additions & 3 deletions rpgsaga/saga/src/bina.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { IStatusEffect } from './spell_system/statusEffect/IStatusEffect';
export class Bina {
private _message: Message;

get message() {
return this._message;
}

public receiveMessage(message: Message) {
this._message = message;
}
Expand All @@ -18,7 +22,7 @@ export class Bina {
);
}

public performSpell() {
public performSpell(): boolean {
const isSuccessful = this._message.spell.execute(this._message.target);

if (isSuccessful) {
Expand All @@ -29,14 +33,16 @@ export class Bina {
);

if (this._message.spell.hasStatusEffect()) {
this.sendStatusEffect(this._message.target, this._message.spell.sendStatusEffect());
this.sendStatusEffect(this._message.target, this._message.spell.getStatusEffect());
}
return true;
} else {
console.log(`${this._message.attackerInfo} has failed to cast a spell!`);
return false;
}
}

public sendStatusEffect(target: Character, statusEffect: IStatusEffect) {
private sendStatusEffect(target: Character, statusEffect: IStatusEffect) {
if (target.statusEffects.contains(statusEffect)) {
const node: DoublyLinkedListNode = target.statusEffects.head;
while (node !== null) {
Expand Down
14 changes: 10 additions & 4 deletions rpgsaga/saga/src/characterFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@ export class CharacterFactory {
}
}

public getCharacter(characterClass: CharacterClass): Character {
public getCharacter(
characterClass: CharacterClass,
randomNumberGenerator: (min: number, max: number) => number,
): Character {
this.set(characterClass);
return this._factory.createCharacter();
return this._factory.createCharacter(randomNumberGenerator);
}

public generatePlayers(quantityOfPlayers: number): Character[] {
public generatePlayers(
quantityOfPlayers: number,
randomNumberGenerator: (min: number, max: number) => number,
): Character[] {
const players: Character[] = [];

for (let i = 0; i < quantityOfPlayers; i++) {
const character = this.getCharacter(getRandomEnumValue(CharacterClass));
const character = this.getCharacter(getRandomEnumValue(CharacterClass), randomNumberGenerator);
players.push(character);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Character } from '../character';

export interface ICharacterGenerator {
createCharacter(): Character;
createCharacter(randomNumberGenerator: (min: number, max: number) => number): Character;
}
7 changes: 3 additions & 4 deletions rpgsaga/saga/src/characterGenerators/archerGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { CharacterClass } from '../characterClasses';
import { Character } from '../character';
import { randomIntFromInterval } from '../randomMath';
import { FireArrow } from '../spell_system/activeEffects/fireArrow';
import { Spell } from '../spell_system/spell/spell';
import { FireArrowEffect } from '../spell_system/statusEffect/fireArrowEffect';
Expand Down Expand Up @@ -50,12 +49,12 @@ export class ArcherGenerator implements ICharacterGenerator {
this._maxHealthPoints = 110;
}

public createCharacter(): Character {
public createCharacter(randomNumberGenerator: (min: number, max: number) => number): Character {
const helper = new CharacterGeneratorHelper();
return new Character(
helper.getRandomNameAndSurname(this._nameList, this._surnameList),
helper.getRandomNameAndSurname(this._nameList, this._surnameList, randomNumberGenerator),
CharacterClass.archer,
randomIntFromInterval(this._minHealthPoints, this._maxHealthPoints),
randomNumberGenerator(this._minHealthPoints, this._maxHealthPoints),
new Spell('Fire Arrow', new FireArrow(2, 7), new FireArrowEffect('Burning', 3)),
);
}
Expand Down
12 changes: 7 additions & 5 deletions rpgsaga/saga/src/characterGenerators/characterGeneratorHelper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { getRandomArrayIndex } from '../randomMath';

export class CharacterGeneratorHelper {
public getRandomNameAndSurname(nameList: string[], surnameList: string[]): string {
const name: string = nameList[getRandomArrayIndex(nameList.length)];
const surname: string = surnameList[getRandomArrayIndex(surnameList.length)];
public getRandomNameAndSurname(
nameList: string[],
surnameList: string[],
randomNumberGenerator: (min: number, max: number) => number,
): string {
const name: string = nameList[randomNumberGenerator(0, nameList.length)];
const surname: string = surnameList[randomNumberGenerator(0, surnameList.length)];

return `${name} ${surname}`;
}
Expand Down
7 changes: 3 additions & 4 deletions rpgsaga/saga/src/characterGenerators/knightGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { CharacterClass } from '../characterClasses';
import { Character } from '../character';
import { randomIntFromInterval } from '../randomMath';
import { KnightAttack } from '../spell_system/activeEffects/knightAttack';
import { Spell } from '../spell_system/spell/spell';

Expand Down Expand Up @@ -49,12 +48,12 @@ export class KnightGenerator implements ICharacterGenerator {
this._maxHealthPoints = 120;
}

public createCharacter(): Character {
public createCharacter(randomNumberGenerator: (min: number, max: number) => number): Character {
const helper = new CharacterGeneratorHelper();
return new Character(
helper.getRandomNameAndSurname(this._nameList, this._surnameList),
helper.getRandomNameAndSurname(this._nameList, this._surnameList, randomNumberGenerator),
CharacterClass.knight,
randomIntFromInterval(this._minHealthPoints, this._maxHealthPoints),
randomNumberGenerator(this._minHealthPoints, this._maxHealthPoints),
new Spell('Mighty Slash', new KnightAttack(2, 5), null),
);
}
Expand Down
7 changes: 3 additions & 4 deletions rpgsaga/saga/src/characterGenerators/mageGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { CharacterClass } from '../characterClasses';
import { Character } from '../character';
import { randomIntFromInterval } from '../randomMath';
import { Stun } from '../spell_system/activeEffects/stun';
import { Spell } from '../spell_system/spell/spell';
import { StunEffect } from '../spell_system/statusEffect/stunEffect';
Expand Down Expand Up @@ -50,12 +49,12 @@ export class MageGenerator implements ICharacterGenerator {
this._maxHealthPoints = 90;
}

public createCharacter(): Character {
public createCharacter(randomNumberGenerator: (min: number, max: number) => number): Character {
const helper = new CharacterGeneratorHelper();
return new Character(
helper.getRandomNameAndSurname(this._nameList, this._surnameList),
helper.getRandomNameAndSurname(this._nameList, this._surnameList, randomNumberGenerator),
CharacterClass.mage,
randomIntFromInterval(this._minHealthPoints, this._maxHealthPoints),
randomNumberGenerator(this._minHealthPoints, this._maxHealthPoints),
new Spell('Freeze', new Stun(2, 0), new StunEffect('Stun', 1)),
);
}
Expand Down
8 changes: 5 additions & 3 deletions rpgsaga/saga/src/doublyLinkedList/doublyLinkedList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ export class DoublyLinkedList {
this._size += 1;
}

public remove(value: IStatusEffect) {
public remove(value: IStatusEffect): boolean {
if (this.isEmpty()) {
return;
return false;
}

let tmp = this._head;
Expand All @@ -102,10 +102,12 @@ export class DoublyLinkedList {

this._size--;

return;
return true;
}

tmp = tmp.next;
}

return false;
}
}
25 changes: 19 additions & 6 deletions rpgsaga/saga/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Message } from './message';
import { AttackType } from './attackType';
import { StatusEffectManager } from './spell_system/statusEffectManager';
import { GameUtility } from './gameUtility';
import { randomIntFromInterval } from './randomMath';

export class Game {
private _players: Character[] = [];
Expand All @@ -16,16 +17,26 @@ export class Game {
private _quantityOfPlayers: number;

private _utility = new GameUtility();
private _randomNumberGenerator: (min: number, max: number) => number;

constructor(quantityOfPlayers: number) {
if (quantityOfPlayers === 1) {
public winner: Character;

constructor(quantityOfPlayers: number, randomNumberGenerator?: (min: number, max: number) => number) {
if (quantityOfPlayers <= 1) {
throw Error('Quantity of players should be greater than 1');
}

this._quantityOfPlayers = quantityOfPlayers;
this._newPlayerIndex = 2;

if (typeof randomNumberGenerator !== 'undefined') {
this._randomNumberGenerator = randomNumberGenerator;
} else {
this._randomNumberGenerator = randomIntFromInterval;
}

const characterFactory: CharacterFactory = new CharacterFactory();
this._players = characterFactory.generatePlayers(this._quantityOfPlayers);
this._players = characterFactory.generatePlayers(this._quantityOfPlayers, this._randomNumberGenerator);
}

private swapCurrentPlayers(): void {
Expand Down Expand Up @@ -56,7 +67,9 @@ export class Game {
bina.receiveMessage(message);

if (!attacker.isStunned) {
if (this._utility.chooseAnAttackType(attacker.spell.canExecute()) === AttackType.attack) {
if (
this._utility.chooseAnAttackType(attacker.spell.canExecute(), this._randomNumberGenerator) === AttackType.attack
) {
bina.performAttack();
} else {
bina.performSpell();
Expand Down Expand Up @@ -131,7 +144,7 @@ export class Game {
console.log('\n');
}

const winner: Character = this._utility.findWinner(this._currentPlayers);
this.announceWinner(winner);
this.winner = this._utility.findWinner(this._currentPlayers);
this.announceWinner(this.winner);
}
}
13 changes: 9 additions & 4 deletions rpgsaga/saga/src/gameUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { cloneDeep } from 'lodash';
import { ArrayItem } from './arrayItem';
import { Character } from './character';
import { AttackType } from './attackType';
import { randomIntFromInterval } from './randomMath';

export class GameUtility {
private announceDeadBody(deadBody: Character): void {
Expand Down Expand Up @@ -46,7 +45,10 @@ export class GameUtility {
const deadPlayerIndex: number = deadPlayerIndexes[i];

if (this.isPlayerDead(currentPlayers[deadPlayerIndex].player)) {
currentPlayers[deadPlayerIndex] = new ArrayItem(cloneDeep(players[newPlayerIndex]), newPlayerIndex);
currentPlayers[deadPlayerIndex] = new ArrayItem(
cloneDeep(players[updatedNewPlayerIndex]),
updatedNewPlayerIndex,
);

updatedNewPlayerIndex += 1;
}
Expand Down Expand Up @@ -86,12 +88,15 @@ export class GameUtility {
return null;
}

public chooseAnAttackType(isSpellAvailable: boolean): AttackType {
public chooseAnAttackType(
isSpellAvailable: boolean,
randomNumberGenerator: (min: number, max: number) => number,
): AttackType {
if (!isSpellAvailable) {
return AttackType.attack;
}

const randomNumber = randomIntFromInterval(1, 10);
const randomNumber = randomNumberGenerator(1, 10);
if (randomNumber <= 3) {
return AttackType.spell;
} else {
Expand Down
9 changes: 0 additions & 9 deletions rpgsaga/saga/src/spell_system/activeEffects/castHelper.ts

This file was deleted.

4 changes: 1 addition & 3 deletions rpgsaga/saga/src/spell_system/activeEffects/fireArrow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Character } from '../../character';

import { CastHelper } from './castHelper';
import { IActiveEffect } from './IActiveEffect';

export class FireArrow implements IActiveEffect {
Expand All @@ -13,8 +12,7 @@ export class FireArrow implements IActiveEffect {
}

public cast(target: Character): boolean {
const castHelper = new CastHelper();
if (castHelper.isCastable(this._castsRemaining)) {
if (this.canCast()) {
target.receiveDamage(this._damagePoints);
this._castsRemaining -= 1;
return true;
Expand Down
4 changes: 1 addition & 3 deletions rpgsaga/saga/src/spell_system/activeEffects/knightAttack.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Character } from '../../character';

import { CastHelper } from './castHelper';
import { IActiveEffect } from './IActiveEffect';

export class KnightAttack implements IActiveEffect {
Expand All @@ -13,8 +12,7 @@ export class KnightAttack implements IActiveEffect {
}

public cast(target: Character): boolean {
const castHelper = new CastHelper();
if (castHelper.isCastable(this._castsRemaining)) {
if (this.canCast()) {
target.receiveDamage(this._damagePoints * 2.0);
this._castsRemaining -= 1;
return true;
Expand Down
4 changes: 1 addition & 3 deletions rpgsaga/saga/src/spell_system/activeEffects/stun.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Character } from '../../character';

import { CastHelper } from './castHelper';
import { IActiveEffect } from './IActiveEffect';

export class Stun implements IActiveEffect {
Expand All @@ -13,8 +12,7 @@ export class Stun implements IActiveEffect {
}

public cast(target: Character): boolean {
const castHelper = new CastHelper();
if (castHelper.isCastable(this._castsRemaining)) {
if (this.canCast()) {
target.receiveDamage(this._damagePoints);
this._castsRemaining -= 1;
return true;
Expand Down
2 changes: 1 addition & 1 deletion rpgsaga/saga/src/spell_system/spell/ISpell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export interface ISpell {
canExecute(): boolean;
execute(target: Character): boolean;
hasStatusEffect(): boolean;
sendStatusEffect(): IStatusEffect;
getStatusEffect(): IStatusEffect;
toString(): string;
}
10 changes: 3 additions & 7 deletions rpgsaga/saga/src/spell_system/spell/spell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ export class Spell implements ISpell {
return this._name;
}

get cast() {
return this._activeEffect;
}

get statusEffect() {
return this._statusEffect;
}
Expand All @@ -25,9 +21,9 @@ export class Spell implements ISpell {
return this._activeEffect;
}

constructor(name: string, cast: IActiveEffect, statusEffect?: IStatusEffect) {
constructor(name: string, activeEffect: IActiveEffect, statusEffect?: IStatusEffect) {
this._name = name;
this._activeEffect = cast;
this._activeEffect = activeEffect;
this._statusEffect = null;

if (typeof statusEffect !== undefined) {
Expand Down Expand Up @@ -60,7 +56,7 @@ export class Spell implements ISpell {
return false;
}

public sendStatusEffect(): IStatusEffect {
public getStatusEffect(): IStatusEffect {
if (this.hasStatusEffect()) {
return this._statusEffect;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Character } from '../../character';

export interface IStatusEffect {
apply(target: Character);
apply(target: Character): boolean;
remove(target: Character);
refresh();
canApply(): boolean;
Expand Down
Loading

0 comments on commit 1379c33

Please sign in to comment.