From 5465ff6d3e23edd4fdeb632ba6dad9310f57de38 Mon Sep 17 00:00:00 2001 From: dclxxxvi Date: Sat, 23 Oct 2021 18:16:56 +0500 Subject: [PATCH 1/4] 1-5 done --- src/TaskQueue.js | 63 ++++++++++++++++++------------------ src/index.js | 83 ++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 101 insertions(+), 45 deletions(-) diff --git a/src/TaskQueue.js b/src/TaskQueue.js index f3a9c6a..a2ecec7 100644 --- a/src/TaskQueue.js +++ b/src/TaskQueue.js @@ -1,10 +1,35 @@ -const TaskQueue = function() { - function TaskQueue() { +function runNextTask(taskQueue) { + if (taskQueue.running || taskQueue.tasks.length === 0) { + return; + } + taskQueue.running = true; + const task = taskQueue.tasks.shift(); + + if (task.runAndContinue) { + setTimeout(() => { + task.runAndContinue(() => { + task.dispose && task.dispose(); + taskQueue.running = false; + + setTimeout(() => { + runNextTask(taskQueue); + }); + }); + }, 0); + } + else { + runNextTask(taskQueue); + } +} + +class TaskQueue { + + constructor() { this.tasks = []; this.running = false; } - TaskQueue.prototype.push = function(run, dispose, duration) { + push(run, dispose, duration) { if (duration === undefined || duration === null) { this.tasks.push({runAndContinue: run, dispose}); } else { @@ -19,37 +44,11 @@ const TaskQueue = function() { }); } runNextTask(this); - }; + } - TaskQueue.prototype.continueWith = function(action) { + continueWith(action) { this.push(action, null, 0); - }; - - function runNextTask(taskQueue) { - if (taskQueue.running || taskQueue.tasks.length === 0) { - return; - } - taskQueue.running = true; - const task = taskQueue.tasks.shift(); - - if (task.runAndContinue) { - setTimeout(() => { - task.runAndContinue(() => { - task.dispose && task.dispose(); - taskQueue.running = false; - - setTimeout(() => { - runNextTask(taskQueue); - }); - }); - }, 0); - } - else { - runNextTask(taskQueue); - } } - - return TaskQueue; -}(); +} export default TaskQueue; diff --git a/src/index.js b/src/index.js index a01f912..f360300 100644 --- a/src/index.js +++ b/src/index.js @@ -27,33 +27,90 @@ function getCreatureDescription(card) { return 'Существо'; } +class Creature extends Card { + constructor(name, maxPower) { + super(name, maxPower); + this._currentPower = maxPower; + } + + getDescriptions() { + return [getCreatureDescription(this), super.getDescriptions()]; + } +} + +class Duck extends Creature { + constructor(name = 'Мирная утка', maxPower = 2) { + super(name, maxPower); + } + quacks() { + console.log('quack'); + }; -// Основа для утки. -function Duck() { - this.quacks = function () { console.log('quack') }; - this.swims = function () { console.log('float: both;') }; + swims() { + console.log('float: both;'); + }; } +class Dog extends Creature { + constructor(name = 'Пес-бандит', maxPower = 3) { + super(name, maxPower); + } +} -// Основа для собаки. -function Dog() { +class Trasher extends Dog { + constructor(name = 'Громила', maxPower = 5) { + super(name, maxPower); + } + + modifyTakenDamage(value, fromCard, gameContext, continuation) { + this.view.signalAbility(() => + super.modifyTakenDamage(value - 1, fromCard, gameContext, continuation)); + } + + getDescriptions() { + return ['Получает меньше урона на 1', super.getDescriptions()]; + } } +class Gatling extends Creature { + constructor(name = 'Гатлинг', maxPower = 6) { + super(name, maxPower); + } + + attack(gameContext, continuation) { + const taskQueue = new TaskQueue(); + + const {currentPlayer, oppositePlayer, position, updateView} = gameContext; + taskQueue.push(onDone => this.view.showAttack(onDone)); + oppositePlayer.table.forEach(card => { + taskQueue.push(onDone => { + if (card) { + this.dealDamageToCreature(2, card, gameContext, onDone); + } + }); + }); + taskQueue.continueWith(continuation); + }; + + getDescriptions() { + return ['Наносит 2 урона всем противникам', super.getDescriptions()]; + } +} // Колода Шерифа, нижнего игрока. const seriffStartDeck = [ - new Card('Мирный житель', 2), - new Card('Мирный житель', 2), - new Card('Мирный житель', 2), + new Duck(), + new Duck(), + new Duck(), + new Gatling(), ]; - -// Колода Бандита, верхнего игрока. const banditStartDeck = [ - new Card('Бандит', 3), + new Trasher(), + new Dog(), + new Dog(), ]; - // Создание игры. const game = new Game(seriffStartDeck, banditStartDeck); From 5851b4196b2e097ef9c315d4a05e57e8518fe4d7 Mon Sep 17 00:00:00 2001 From: dclxxxvi Date: Sat, 23 Oct 2021 19:17:56 +0500 Subject: [PATCH 2/4] 1-7 done --- package-lock.json | 6 ++-- src/TaskQueue.js | 4 +-- src/index.js | 80 ++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 77 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index 62879f0..b5765b2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -89,9 +89,9 @@ } }, "lodash": { - "version": "4.17.20", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz", - "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==" + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" }, "mime": { "version": "1.6.0", diff --git a/src/TaskQueue.js b/src/TaskQueue.js index a2ecec7..37ea76a 100644 --- a/src/TaskQueue.js +++ b/src/TaskQueue.js @@ -22,7 +22,7 @@ function runNextTask(taskQueue) { } } -class TaskQueue { +export default class TaskQueue { constructor() { this.tasks = []; @@ -50,5 +50,3 @@ class TaskQueue { this.push(action, null, 0); } } - -export default TaskQueue; diff --git a/src/index.js b/src/index.js index f360300..e7ff874 100644 --- a/src/index.js +++ b/src/index.js @@ -69,7 +69,9 @@ class Trasher extends Dog { } getDescriptions() { - return ['Получает меньше урона на 1', super.getDescriptions()]; + let descriptions = super.getDescriptions(); + descriptions.push('Получает меньше урона на 1'); + return descriptions; } } @@ -94,7 +96,71 @@ class Gatling extends Creature { }; getDescriptions() { - return ['Наносит 2 урона всем противникам', super.getDescriptions()]; + let descriptions = super.getDescriptions(); + descriptions.push('Наносит 2 урона всем противникам'); + return descriptions; + } +} + +class Lad extends Dog { + static getInGameCount() { return this.inGameCount || 0; } + static setInGameCount(value) { this.inGameCount = value; } + static getBonus() { return this.getInGameCount() * (this.getInGameCount() + 1) / 2; } + constructor(name = 'Браток', maxPower = 2) { + super(name, maxPower); + } + + doAfterComingIntoPlay(gameContext, continuation) { + Lad.setInGameCount(Lad.getInGameCount() + 1); + continuation(); + } + + doBeforeRemoving(continuation) { + Lad.setInGameCount(Lad.getInGameCount() - 1); + continuation(); + } + + modifyDealedDamageToCreature(value, toCard, gameContext, continuation) { + continuation(value + Lad.getBonus()); + } + + modifyTakenDamage(value, fromCard, gameContext, continuation) { + continuation(value - Lad.getBonus()); + } + + getDescriptions() { + let descriptions = super.getDescriptions(); + if (Lad.prototype.hasOwnProperty("modifyDealedDamageToCreature") && Lad.prototype.hasOwnProperty("modifyTakenDamage")) { + descriptions.push("Чем их больше, тем они сильнее"); + } + return descriptions; + } +} + +class Rogue extends Creature { + static properties = [ + 'modifyDealedDamageToCreature', + 'modifyDealedDamageToPlayer', + 'modifyTakenDamage', + ]; + + constructor(name = 'Изгой', maxPower = 2) { + super(name, maxPower); + } + + doBeforeAttack(gameContext, continuation) { + const {currentPlayer, oppositePlayer, position, updateView} = gameContext; + const oppositeCard = oppositePlayer.table[position]; + const obj = Object.getPrototypeOf(oppositeCard); + + Rogue.properties.forEach(property => { + if (obj.hasOwnProperty(property)) { + this[property] = obj[property]; + delete obj[property]; + } + }); + continuation(); + updateView(); } } @@ -103,19 +169,19 @@ const seriffStartDeck = [ new Duck(), new Duck(), new Duck(), - new Gatling(), + new Rogue(), ]; const banditStartDeck = [ - new Trasher(), - new Dog(), - new Dog(), + new Lad(), + new Lad(), + new Lad(), ]; // Создание игры. const game = new Game(seriffStartDeck, banditStartDeck); // Глобальный объект, позволяющий управлять скоростью всех анимаций. -SpeedRate.set(1); +SpeedRate.set(2); // Запуск игры. game.play(false, (winner) => { From 9dc000f5e01bd671b5a38ac12b035d51836e650a Mon Sep 17 00:00:00 2001 From: dclxxxvi Date: Fri, 29 Oct 2021 04:14:23 +0500 Subject: [PATCH 3/4] 1-10 done --- src/index.js | 79 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 8 deletions(-) diff --git a/src/index.js b/src/index.js index e7ff874..ba16d22 100644 --- a/src/index.js +++ b/src/index.js @@ -33,6 +33,14 @@ class Creature extends Card { this._currentPower = maxPower; } + get currentPower() { + return this._currentPower; + } + + set currentPower(value) { + this._currentPower = value > this.maxPower ? this.maxPower : value; + } + getDescriptions() { return [getCreatureDescription(this), super.getDescriptions()]; } @@ -162,26 +170,81 @@ class Rogue extends Creature { continuation(); updateView(); } + + getDescriptions() { + let descriptions = super.getDescriptions(); + descriptions.push('Крадет способности'); + return descriptions; + } +} + +class Brewer extends Duck { + constructor(name = "Пивовар", maxPower = 2) { + super(name, maxPower); + } + + doBeforeAttack(gameContext, continuation) { + let taskQueue = new TaskQueue(); + const cards = gameContext.currentPlayer.table.concat(gameContext.oppositePlayer.table); + + for(let card of cards.filter(card => isDuck(card))) { + card.view.signalHeal(); + card.maxPower += 1; + card.currentPower += 2; + card.updateView(); + } + + taskQueue.continueWith(continuation); + + } + + getDescriptions() { + let descriptions = super.getDescriptions(); + descriptions.push('Раздает живительное пиво'); + return descriptions; + } +} + +class PseudoDuck extends Dog { + constructor() { + super("Псевдоутка", 3); + } + swims() {} + quacks() {} +} + +class Nemo extends Creature { + constructor(name = 'Немо', maxPower = 4) { + super(name, maxPower); + } + + modifyDealedDamageToCreature(value, toCard, gameContext, continuation) { + Object.setPrototypeOf(this, Object.getPrototypeOf(toCard)); + this.doBeforeAttack(gameContext, continuation); + gameContext.updateView(); + } + + getDescriptions() { + let descriptions = super.getDescriptions(); + descriptions.push('The one without a name without an honest heart as compass'); + return descriptions; + } } // Колода Шерифа, нижнего игрока. const seriffStartDeck = [ - new Duck(), - new Duck(), - new Duck(), - new Rogue(), + new Nemo(), ]; const banditStartDeck = [ - new Lad(), - new Lad(), - new Lad(), + new Brewer(), + new Brewer(), ]; // Создание игры. const game = new Game(seriffStartDeck, banditStartDeck); // Глобальный объект, позволяющий управлять скоростью всех анимаций. -SpeedRate.set(2); +SpeedRate.set(3); // Запуск игры. game.play(false, (winner) => { From 9246624ea4e34decfd28bc9f3cf4febd361916db Mon Sep 17 00:00:00 2001 From: dclxxxvi Date: Fri, 29 Oct 2021 04:15:28 +0500 Subject: [PATCH 4/4] 1-10 done --- src/index.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/index.js b/src/index.js index ba16d22..bd1f33b 100644 --- a/src/index.js +++ b/src/index.js @@ -3,17 +3,14 @@ import Game from './Game.js'; import TaskQueue from './TaskQueue.js'; import SpeedRate from './SpeedRate.js'; -// Отвечает является ли карта уткой. function isDuck(card) { return card && card.quacks && card.swims; } -// Отвечает является ли карта собакой. function isDog(card) { return card instanceof Dog; } -// Дает описание существа по схожести с утками и собаками function getCreatureDescription(card) { if (isDuck(card) && isDog(card)) { return 'Утка-Собака'; @@ -231,7 +228,6 @@ class Nemo extends Creature { } } -// Колода Шерифа, нижнего игрока. const seriffStartDeck = [ new Nemo(), ]; @@ -240,13 +236,10 @@ const banditStartDeck = [ new Brewer(), ]; -// Создание игры. const game = new Game(seriffStartDeck, banditStartDeck); -// Глобальный объект, позволяющий управлять скоростью всех анимаций. SpeedRate.set(3); -// Запуск игры. game.play(false, (winner) => { alert('Победил ' + winner.name); });