Skip to content

Commit

Permalink
fix a few buggs connected with dynamic handling of rank table
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonSys committed Sep 14, 2019
1 parent fc940d4 commit 46557ee
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ class Controller {
if (document.getElementById('summary-popup')) this.interface.removeGameSummary();

this.addListener(this.grid.root, function (e) {
if ((this.game.gameState === "ongoing" || this.game.gameState === 'win') && e.target.className === "game-square") {
if ((this.game.gameState === "ongoing" || this.game.gameState === 'win' || this.game.gameState === 'failed') && e.target.className === "game-square") {
this.interface.actionsCounter = this.game.stats.actionsCounter;
}

if (this.game.gameState === 'win') {
this.game.stopGame(this.grid);

const result = new Result(
this.game.stats.timerValue,
this.game.stats._getTimerResoult(),
this.game.stats.actionsCounter,
this.game.stats.difficulty,
Expand Down
2 changes: 1 addition & 1 deletion Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ class Game {
}

checkIfWin() {
return this.numberOfMatchedSqueres === 2 ? true : false;
return this.numberOfMatchedSqueres === this.numberOfSquares ? true : false;
// return this.numberOfMatchedSqueres === 2 ? true : false;
}
}
4 changes: 2 additions & 2 deletions Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Grid {
elem.dataset.isActive = square.isActive.toString();
elem.dataset.isMatched = square.isMatched.toString();
elem.style.backgroundColor = square.color;
// elem.textContent = square.value;
elem.textContent = square.value;

wrapper.appendChild(elem);
}
Expand All @@ -42,4 +42,4 @@ class Grid {

this.root.appendChild(wrapper);
}
}
}
12 changes: 10 additions & 2 deletions Result.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class Result {
constructor(time = null, actions = null, difficulty = null, playerName = '') {
constructor(timeValue = null, time = null, actions = null, difficulty = null, playerName = '') {
if (!(time || actions))
throw new Error("You have to set time and actions properties");

this._time = time;
this._actions = actions;
this._playerName = playerName ? playerName : 'anonim';
this._difficulty = difficulty;
this._timeValue = timeValue;
}

get time() {
Expand Down Expand Up @@ -49,4 +49,12 @@ class Result {

return this._difficulty = difficulty;
}

get timeValue() {
return this._timeValue
}

set timeValue(value) {
return this._timeValue = value;
}
}
23 changes: 14 additions & 9 deletions Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Table {
playerName: "result-name",
time: "result-time",
actions: "result-counter",
timeValue: "timeValue",
difficulty: "result-difficulty"
},
style: {
Expand Down Expand Up @@ -117,7 +118,7 @@ class Table {

}

sortTable(tab = this.rankTable, by = 'time', order = 'ASC') {
sortTable(tab = this.rankTable, by = 'timeValue', order = 'ASC') {
tab.sort((a, b) => {
if (!(a instanceof Result && b instanceof Result))
throw new Error("Element of Array has not require type");
Expand All @@ -135,7 +136,7 @@ class Table {

loadFromStorage() {
if (this.hasStorage()) {
return JSON.parse(localStorage.getItem('rankTable')).map(result => new Result(result._time, result._actions, result._difficulty, result._playerName));
return JSON.parse(localStorage.getItem('rankTable')).map(result => new Result(result._timeValue, result._time, result._actions, result._difficulty, result._playerName));
} else return [];
}

Expand Down Expand Up @@ -167,7 +168,10 @@ class Table {
result.style[prop] = this.resultElementProps.style[prop];
}

// result.dataset.timeValue = result.timeValue;

for (const id in this.resultElementProps.typeId) {
if (id === 'timeValue') continue;
result.dataset[id] = id === 'number' ? i + 1 : this.rankTable[i][id];
result.innerHTML += `<div id="${this.resultElementProps.typeId[id]}" style = "${this.resultElementProps.childWidth}" class="${this.resultElementProps.typeClass}">
<span>${id === 'number' ? i+1 : this.rankTable[i][id]}</span>
Expand All @@ -179,7 +183,7 @@ class Table {
this.htmlTable.appendChild(docFragment);
}

addResultToView(result, by = 'time') {
addResultToView(result, by = 'timeValue') {

function addNewNode(i) {
const newNode = document.createElement('div');
Expand All @@ -191,7 +195,10 @@ class Table {
newNode.style[prop] = this.resultElementProps.style[prop];
}

newNode.dataset.timeValue = result.timeValue;

for (const id in this.resultElementProps.typeId) {
if (id === "timeValue") continue;
newNode.dataset[id] = id === 'number' ? +i : result[id];
newNode.innerHTML += `<div id="${this.resultElementProps.typeId[id]}" style="${this.resultElementProps.childWidth}" class="${this.resultElementProps.typeClass}">
<span>${id === 'number' ? +i: result[id]}</span>
Expand All @@ -212,17 +219,17 @@ class Table {
for (const node of [...this.htmlTable.childNodes]) {
index++;

if (!node.previousSibling && result.time <= node.dataset.time) {
if (!node.previousSibling && result.timeValue <= node.dataset.timeValue) {
this.htmlTable.insertBefore(addNewNode.call(this, node.dataset.number), node);

break;

} else if (!node.nextSibling && result.time >= node.dataset.time) {
} else if (!node.nextSibling && result.timeValue >= node.dataset.timeValue) {
this.htmlTable.appendChild(addNewNode.call(this, node.dataset.number));

break;

} else if (result.time > node.dataset.time && result.time <= node.nextSibling.dataset.time) {
} else if (result.timeValue > node.dataset.timeValue && result.timeValue <= node.nextSibling.dataset.timeValue) {
this.htmlTable.insertBefore(addNewNode.call(this, node.dataset.number), node.nextSibling);

break;
Expand All @@ -232,8 +239,6 @@ class Table {

const htmlNodes = [...this.htmlTable.childNodes];

console.log(index)

for (let i = index; i <= htmlNodes.length - 1; i++) {
htmlNodes[i].dataset.number++;
htmlNodes[i].firstChild.innerHTML = `<span>${htmlNodes[i].dataset.number}</span>`;
Expand All @@ -259,7 +264,7 @@ class Table {
return -1;
}

_add = (result, table, isSorted, by = 'time', order = 'ASC') => {
_add = (result, table, isSorted, by = 'timeValue', order = 'ASC') => {
if (result[by] === undefined)
throw new Error("set correct type of sorting");

Expand Down

0 comments on commit 46557ee

Please sign in to comment.