From 327317328a6128a1c5a19679b622f4053aa80fdd Mon Sep 17 00:00:00 2001 From: Rey1147 Date: Wed, 11 Oct 2023 16:30:06 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B0=D0=BB=D0=B0=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20=D0=B8=20?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BA=20=D0=BD=D0=B5=D0=BC?= =?UTF-8?q?=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rpgsaga/saga/package.json | 3 +- rpgsaga/saga/src/gun.ts | 39 ++++++++++------- rpgsaga/saga/src/index.ts | 5 ++- rpgsaga/saga/tests/gunTest.spec.ts | 69 +++++++++++++++++++++--------- 4 files changed, 75 insertions(+), 41 deletions(-) diff --git a/rpgsaga/saga/package.json b/rpgsaga/saga/package.json index 5152da0..8082796 100644 --- a/rpgsaga/saga/package.json +++ b/rpgsaga/saga/package.json @@ -4,8 +4,7 @@ "description": "", "main": "index.js", "scripts": { - "dev": "ts-node --script-mode src/gun.ts", - "index": "ts-node --script-mode src/index.ts", + "dev": "ts-node --script-mode src/index.ts", "test": "jest", "lint": "eslint src --ext .js --ext .jsx --ext .ts --ext .tsx", "lint:fix": "npm run lint -- --fix" diff --git a/rpgsaga/saga/src/gun.ts b/rpgsaga/saga/src/gun.ts index cb7ced6..162c5a0 100644 --- a/rpgsaga/saga/src/gun.ts +++ b/rpgsaga/saga/src/gun.ts @@ -1,35 +1,42 @@ export class Gun { - readonly serialNumber: string = 's52001'; - caliber: number; + readonly serialNumber: string = 's00001'; + private aMagazine: number; model: string; - magazin = 20; + shotCount = 0; - constructor(modelName: string, public bullets: number, gunCaliber?: number) { + constructor(modelName: string, magazine: number, public bullets?: number) { this.model = modelName; - this.bullets = bullets; - this.caliber = gunCaliber; + this.magazine = magazine; + this.bullets = this.bullets < this.magazine && this.bullets >= 0 ? this.bullets : this.magazine; + } + // инфо :) + info() { + return `You're create gun model: "${this.model}". He has a magazine for ${this.magazine} bullets and ${this.bullets} bullets.`; } // выстрел shot(): string { - return `The Model ${this.model} pistol fired`; + if (this.bullets > 0) { + this.shotCount += 1; + this.bullets -= 1; + return `The gun ${this.model} fired ${this.shotCount} times`; + } else { + return 'You are out of ammo. Recharge'; + } } // перезарядка recharge(): string { + this.bullets = this.magazine; return 'The magazine is being recharged'; } // проверка магазина - fullMagazin(): string { - this.bullets = this.bullets === undefined ? 0 : this.bullets; - if (this.magazin === this.bullets) { - return 'magazine full'; - } else if (this.bullets > this.magazin / 2) { - return `in magazine ${this.bullets} bullets`; - } else { - return 'go recharge'; - } + set magazine(magazine: number) { + this.aMagazine = magazine >= 0 && magazine < 21 ? magazine : this.aMagazine ?? 20; + } + get magazine() { + return this.aMagazine; } } diff --git a/rpgsaga/saga/src/index.ts b/rpgsaga/saga/src/index.ts index 6d5c5ad..8d85b38 100644 --- a/rpgsaga/saga/src/index.ts +++ b/rpgsaga/saga/src/index.ts @@ -1,5 +1,6 @@ import { Gun } from './gun'; -const gun: Gun = new Gun('p-250', 9, 8); +const gun: Gun = new Gun('p-250', 21, 5); -console.log(gun.fullMagazin()); +console.log(gun); +console.log(gun.shot()); diff --git a/rpgsaga/saga/tests/gunTest.spec.ts b/rpgsaga/saga/tests/gunTest.spec.ts index cdddf16..67f21ad 100644 --- a/rpgsaga/saga/tests/gunTest.spec.ts +++ b/rpgsaga/saga/tests/gunTest.spec.ts @@ -1,39 +1,66 @@ import { Gun } from '../src/gun'; describe('Testing gun constructor', () => { - it('gun must be create', () => { - const first = new Gun('Пистолет 1', 15, 5.45); + it('Gun must be create', () => { + const first = new Gun('Пистолет 1', 15, 5); expect(first.model).toEqual('Пистолет 1'); - expect(first.bullets).toEqual(15); - expect(first.caliber).toEqual(5.45); + expect(first.magazine).toEqual(15); + expect(first.bullets).toEqual(5); }); - it('Gun with empty caliber', () => { + + it('Gun with empty string of bullets', () => { const first = new Gun('Пистолет 2', 10); expect(first.model).toEqual('Пистолет 2'); + expect(first.magazine).toEqual(10); + expect(first.bullets).toEqual(10); + }); + it('Number of bullets lower than bound', () => { + const first = new Gun('Пистолет 3', 10, -5); + expect(first.model).toEqual('Пистолет 3'); + expect(first.magazine).toEqual(10); + expect(first.bullets).toEqual(10); + }); + it('Number of bullets higher than bound', () => { + const first = new Gun('Пистолет 4', 10, 15); + expect(first.model).toEqual('Пистолет 4'); + expect(first.magazine).toEqual(10); expect(first.bullets).toEqual(10); - expect(first.caliber).toBeUndefined(); + }); + + it('Magazine lower than bound', () => { + const first = new Gun('Пистолет 5', -5, 15); + expect(first.model).toEqual('Пистолет 5'); + expect(first.magazine).toEqual(20); + expect(first.bullets).toEqual(15); + }); + it('Magazine highed than bound', () => { + const first = new Gun('Пистолет 6', 35, 15); + expect(first.model).toEqual('Пистолет 6'); + expect(first.magazine).toEqual(20); + expect(first.bullets).toEqual(15); }); }); describe('Testing gun methods', () => { - it('Gun set fullMagazine valid value', () => { - const first = new Gun('Пистолет 3', 20, 3.62); - expect(first.fullMagazin()).toEqual('magazine full'); - }); - it('Gun set medium value', () => { - const first = new Gun('Пистолет 4', 13, 4.5); - expect(first.fullMagazin()).toEqual('in magazine 13 bullets'); + it('The gun is firing', () => { + const first = new Gun('Пистолет 7', 20, 20); + expect(first.shot()).toEqual('The gun Пистолет 7 fired 1 times'); }); - it('Gun set lower than valid value', () => { - const first = new Gun('Пистолет 5', -1, 2.5); - expect(first.fullMagazin()).toEqual('go recharge'); + it('The gun fires several times', () => { + const first = new Gun('Пистолет 8', 10, 10); + expect(first.shot()).toEqual('The gun Пистолет 8 fired 1 times'); + expect(first.shot()).toEqual('The gun Пистолет 8 fired 2 times'); + expect(first.shot()).toEqual('The gun Пистолет 8 fired 3 times'); + expect(first.shot()).toEqual('The gun Пистолет 8 fired 4 times'); }); - it('Gun must shot', () => { - const first = new Gun('Пистолет 6', 10, 3.62); - expect(first.shot()).toEqual('The Model Пистолет 6 pistol fired'); + it('The gun ran out of ammo', () => { + const first = new Gun('Пистолет 9', 20, 1); + expect(first.shot()).toEqual('The gun Пистолет 9 fired 1 times'); + expect(first.shot()).toEqual('You are out of ammo. Recharge'); }); - it('Gun must recharge', () => { - const first = new Gun('Пистолет 7', 2, 3.62); + it('Reloading the gun', () => { + const first = new Gun('Пистолет 10', 20, 1); expect(first.recharge()).toEqual('The magazine is being recharged'); + expect(first.bullets).toEqual(20); }); });