Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panel#takeAndPlaceをStageに移動 #223

Merged
merged 7 commits into from
Jan 9, 2017
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 23 additions & 82 deletions lib/panel.js
Original file line number Diff line number Diff line change
@@ -1,107 +1,51 @@
const $ = require('jquery');
const assert = require('assert');

class Panel {
constructor(stage) {
this.stage = stage;
constructor(partsLimit, $panel) {
this.parts = new Map();
Object.keys(stage.config.parts).forEach((name, index) => {
const count = stage.config.parts[name];
Object.keys(partsLimit).forEach((name, index) => {
const count = partsLimit[name];
this.parts.set(name, count);
if (index === 0) {
this.selected = name;
}
});
this.$panel = this.stage.$stage.find('.panel');
this.$panel = $panel;
this.update();
}

/**
* panel上にblockを追加する
* @param {string} itemName - 追加するblock名
* @param {number} count - 追加する量
* @return {bool} 処理が成功したかどうか
* @param {string} blockName - 追加するblock名
* @return {undefined}
*/
addItem(itemName, count) {
if (!this.parts.has(itemName)) {
this.parts.set(itemName, count);
return true;
push(blockName) {
let currentCount = 0;
if (this.parts.has(blockName)) {
currentCount = this.parts.get(blockName);
}
const currentCount = this.parts.get(itemName);
if (currentCount === null) { // null means infinity
return true;
if (currentCount !== null) { // null means infinity
this.parts.set(blockName, currentCount + 1);
}
this.parts.set(itemName, currentCount + count);
return true;
this.update();
}

/**
* panel上のblockを減らす
* @param {string} itemName - 消費するblock名
* @param {number} count - 消費する量
* @return {bool} 処理が成功したかどうか
* @param {string} blockName - 消費するblock名
* @return {undefined}
*/
removeItem(itemName, count) {
if (!this.parts.has(itemName)) {
return false;
}
const currentCount = this.parts.get(itemName);
if (currentCount === null) { // null means infinity
return true;
}
if (currentCount - count > 0) {
this.parts.set(itemName, currentCount - count);
return true;
}
if (currentCount - count < 0) { // if panel doesn't have enough item
return false;
}
this.parts.delete(itemName);
return true;
}

takeAndPlace(x, y, blockName) {
const oldBlock = this.stage.board.getBlock(x, y);
assert(oldBlock, 'oldBlock is invalid');
if (blockName === 'empty') { // just take the block
if (oldBlock.config.name !== 'empty') {
// take the block from the board
this.addItem(oldBlock.config.name, 1);
this.stage.boardElement.placeBlock({x, y, type: blockName, rotate: 0});
this.update();
}
return;
}

if (this.parts.has(blockName)) {
let rotate = 0;

if (oldBlock.config.name === 'empty') {
// just place the block
this.removeItem(blockName, 1);
} else if (oldBlock.config.name === blockName && oldBlock.config.rotatable) {
rotate = (oldBlock.rotate + 1) % 4;
} else {
// take and place
this.removeItem(blockName, 1);
this.addItem(oldBlock.config.name, 1);
}

if (!this.parts.has(blockName)) {
take(blockName) {
const currentCount = this.parts.get(blockName);
if (currentCount !== null) { // null means infinity
if (currentCount - 1 === 0) { // take the last block
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currentCountが0のときに return false する必要がありそう

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

多分今のところ、currentCountが0という状況は発生し得ないのですが(そうなりそうな場合partsからkeyごと消される)、対応したほうがいいもんですかね

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

それを言うなら39行目の !this.parts.has(blockName) も発生し得ないような気が⋯⋯。それなら「正しくblockを減らせたかどうか」をreturnする仕様を消して、アサーションに置き換えたほうがよさそう

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

45行目を見てください。(これはもとからの仕様です)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あー、selectedがundifinedにならなくなったのか⋯⋯。それならむしろここでselectedにundefinedをセットして、takeAndPlaceにundefinedなら回転する仕様を追加して return false を撲滅するほうがよさそう。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(いずれにせよreturnされた値は使ってないんだし)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あ、39行目なんて通らないんじゃないかの意味がようやく分かった。(だーめ。)
undefinedにする案よいかもしれませんね。直前に操作した以外のブロックも回転するようになりますが、まあ。

this.selected = null;
this.parts.delete(blockName);
} else {
this.parts.set(blockName, currentCount - 1);
}

this.stage.boardElement.placeBlock({x, y, type: blockName, rotate});
this.update();
} else if (
!this.selected &&
oldBlock.config.name !== 'empty' &&
oldBlock.config.rotatable
) {
const rotate = (oldBlock.rotate + 1) % 4;
this.stage.boardElement.placeBlock({x, y, type: oldBlock.config.name, rotate});
this.update();
}
this.update();
}

update() {
Expand All @@ -119,16 +63,13 @@ class Panel {
})));
});

this.selected = this.$panel.find('.block[selected]').first().data('type');

this.$panel.find('.block').click((event) => {
const $block = $(event.target);

this.$panel.find('.block').attr('selected', false);
$block.attr('selected', true);

this.selected = $block.data('type');
this.stage.$selectedBlock = $block;
});
}
}
Expand Down
39 changes: 31 additions & 8 deletions lib/stage.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Stage {

this.board = new Board(this.config, this.blockSize);
this.boardElement = new BoardElement(this, this.board);
this.panel = new Panel(this);
this.panel = new Panel(this.config.parts, this.$stage.find('.panel'));

this.$ranking = this.$stage.siblings('.result-layer').find('.ranking');
this.$result = this.$stage.siblings('.result-layer').find('.result');
Expand All @@ -35,20 +35,18 @@ class Stage {

this.$stage.find('.statement').text(config.statement);

this.$selectedBlock = this.$stage.find('.panel .block[selected]').first();

this.$stage.find('.board .block').click((event) => {
const $block = $(event.target);
if (this.board.status === 'stop') {
const type = this.$selectedBlock.data('type');
this.panel.takeAndPlace($block.data('x'), $block.data('y'), type);
const type = this.panel.selected;
this.takeAndPlace($block.data('x'), $block.data('y'), type);
}
});

this.$stage.find('.board .block').bind('contextmenu', (event) => {
const $block = $(event.target);
if (this.board.status === 'stop') {
this.panel.takeAndPlace($block.data('x'), $block.data('y'), 'empty');
this.takeAndPlace($block.data('x'), $block.data('y'), 'empty');
}
return false;
});
Expand Down Expand Up @@ -376,14 +374,15 @@ class Stage {
clearBoard() {
this.board.blocks.forEach((row, x) => {
row.forEach((block, y) => {
this.panel.takeAndPlace(x, y, 'empty');
this.takeAndPlace(x, y, 'empty');
});
});
}

makeBoard(board) {
board.forEach((block) => {
for (let i = 0; i < block.rotate + 1; i++) {
this.panel.takeAndPlace(block.x, block.y, block.type);
this.takeAndPlace(block.x, block.y, block.type);
}
});
}
Expand All @@ -396,6 +395,30 @@ class Stage {
$register.removeClass('success error');
$register.attr('disabled', false);
}

takeAndPlace(x, y, blockName) {
const oldBlock = this.board.getBlock(x, y);
assert(oldBlock, 'oldBlock is invalid');

if (!blockName || blockName === oldBlock.config.name) { // rotate the block
if (oldBlock.config.rotatable) {
const rotate = (oldBlock.config.rotate + 1) % 4;
this.boardElement.placeBlock({x, y, type: oldBlock.config.name, rotate});
}
} else { // replace the block
// take the block from panel
if (blockName !== 'empty') {
this.panel.take(blockName);
}

// push the old block into panel
if (oldBlock.config.name !== 'empty') {
this.panel.push(oldBlock.config.name);
}

this.boardElement.placeBlock({x, y, type: blockName, rotate: 0});
}
}
}

module.exports = Stage;