Add: HistoryRecordManager
This commit is contained in:
@@ -0,0 +1,82 @@
|
|||||||
|
class HistoryRecordData {
|
||||||
|
|
||||||
|
constructor(date, record) {
|
||||||
|
this.date = date;
|
||||||
|
this.record = record;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class HistoryRecordManager {
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
push(historyRecordData) {
|
||||||
|
this.historyRecords.push(historyRecordData);
|
||||||
|
|
||||||
|
this.minValueOfRecord = this.minValueOfArray();
|
||||||
|
this.maxValueOfRecord = this.maxValueOfArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
clear() {
|
||||||
|
this.historyRecords = [];
|
||||||
|
|
||||||
|
this.minValueOfRecord = 0;
|
||||||
|
this.maxValueOfRecord = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
get count() {
|
||||||
|
return this.historyRecords.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
getAt(index) {
|
||||||
|
return this.historyRecords[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
getDateAt(index) {
|
||||||
|
return this.historyRecords[index].date;
|
||||||
|
}
|
||||||
|
|
||||||
|
getRecordAt(index) {
|
||||||
|
return this.historyRecords[index].record;
|
||||||
|
}
|
||||||
|
|
||||||
|
minValueOfArray() {
|
||||||
|
let numberArray = this.getRecordArray(this.historyRecords);
|
||||||
|
return Math.min.apply(Math, numberArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
maxValueOfArray() {
|
||||||
|
let numberArray = this.getRecordArray(this.historyRecords);
|
||||||
|
return Math.max.apply(Math, numberArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
underValueForGraph() {
|
||||||
|
let minNumberOfDigits = NumberUtil.numberOfDigits(this.minValueOfRecord);
|
||||||
|
let underMinValue =
|
||||||
|
Math.floor( (this.minValueOfRecord / Math.pow(10, minNumberOfDigits - 2) ) )
|
||||||
|
* Math.pow(10, minNumberOfDigits - 2 );
|
||||||
|
return underMinValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
upperValueForGraph() {
|
||||||
|
let maxNumberOfDigits = NumberUtil.numberOfDigits(this.maxValueOfRecord);
|
||||||
|
let underMaxValue =
|
||||||
|
Math.ceil( (this.maxValueOfRecord / Math.pow(10, maxNumberOfDigits - 2) ) )
|
||||||
|
* Math.pow(10, maxNumberOfDigits - 2);
|
||||||
|
return underMaxValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
getRecordArray() {
|
||||||
|
let numberArray = [];
|
||||||
|
for(let data of this.historyRecords) {
|
||||||
|
numberArray.push(data.record);
|
||||||
|
}
|
||||||
|
return numberArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,4 +4,8 @@ class NumberUtil {
|
|||||||
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static numberOfDigits(number) {
|
||||||
|
return Math.floor(Math.log(number) / Math.LN10 + 1); // bug : 1000 -> expected 4 but 3
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+19
-48
@@ -1,14 +1,6 @@
|
|||||||
/////////////////////////////
|
/////////////////////////////
|
||||||
// Start
|
// Start
|
||||||
|
|
||||||
class HistoryRecordData {
|
|
||||||
|
|
||||||
constructor(date, record) {
|
|
||||||
this.date = date;
|
|
||||||
this.record = record;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let Start = {
|
let Start = {
|
||||||
|
|
||||||
preload: function() {
|
preload: function() {
|
||||||
@@ -16,6 +8,8 @@ let Start = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
create: function() {
|
create: function() {
|
||||||
|
this.historyRecordManager = new HistoryRecordManager();
|
||||||
|
|
||||||
this.game.stage.backgroundColor = '#4d4d4d';
|
this.game.stage.backgroundColor = '#4d4d4d';
|
||||||
this.chartGraphics = game.add.graphics(100, game.world.height - 120);
|
this.chartGraphics = game.add.graphics(100, game.world.height - 120);
|
||||||
|
|
||||||
@@ -31,8 +25,9 @@ let Start = {
|
|||||||
// contents
|
// contents
|
||||||
this.printHowToPlay(appInfoManager.getHowToPlayText(sessionStorageManager.playingAppName));
|
this.printHowToPlay(appInfoManager.getHowToPlayText(sessionStorageManager.playingAppName));
|
||||||
this.makeStartButton();
|
this.makeStartButton();
|
||||||
let historyRecords = this.makeHistoryRecords();
|
|
||||||
this.printRecordHistory(historyRecords);
|
this.loadHistoryRecords();
|
||||||
|
this.printHistoryRecords();
|
||||||
|
|
||||||
|
|
||||||
// bottom
|
// bottom
|
||||||
@@ -62,23 +57,23 @@ let Start = {
|
|||||||
startButton.move(game.world.centerX, game.world.centerY);
|
startButton.move(game.world.centerX, game.world.centerY);
|
||||||
},
|
},
|
||||||
|
|
||||||
makeHistoryRecords: function() {
|
loadHistoryRecords: function() {
|
||||||
let array = [];
|
this.historyRecordManager.clear();
|
||||||
array.push(new HistoryRecordData("05/10", 15460));
|
|
||||||
array.push(new HistoryRecordData("05/11", 13040));
|
this.historyRecordManager.push(new HistoryRecordData("05/10", 15460));
|
||||||
array.push(new HistoryRecordData("05/12", 16460));
|
this.historyRecordManager.push(new HistoryRecordData("05/11", 13040));
|
||||||
array.push(new HistoryRecordData("05/13", 15060));
|
this.historyRecordManager.push(new HistoryRecordData("05/12", 16460));
|
||||||
array.push(new HistoryRecordData("05/14", 19800));
|
this.historyRecordManager.push(new HistoryRecordData("05/13", 15060));
|
||||||
|
this.historyRecordManager.push(new HistoryRecordData("05/14", 19800));
|
||||||
return array;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
printRecordHistory: function(historyRecords) {
|
printHistoryRecords: function(historyRecords) {
|
||||||
let min = this.minValueOfData(historyRecords);
|
let underValue = this.historyRecordManager.underValueForGraph();
|
||||||
let max = this.maxValueOfData(historyRecords);
|
let upperValue = this.historyRecordManager.upperValueForGraph();
|
||||||
|
|
||||||
for(let i = 0; i < historyRecords.length; i++) {
|
for(let i = 0; i < this.historyRecordManager.count; i++) {
|
||||||
this.printRecord(i, historyRecords[i].date, historyRecords[i].record, min, max);
|
let data = this.historyRecordManager.getAt(i);
|
||||||
|
this.printRecord(i, data.date, data.record, underValue, upperValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.chartGraphics.lineStyle(3, 0xffd900, 1);
|
this.chartGraphics.lineStyle(3, 0xffd900, 1);
|
||||||
@@ -86,30 +81,6 @@ let Start = {
|
|||||||
this.chartGraphics.lineTo(game.world.width - 200, 0);
|
this.chartGraphics.lineTo(game.world.width - 200, 0);
|
||||||
},
|
},
|
||||||
|
|
||||||
minValueOfData: function(historyRecords) {
|
|
||||||
let numberArray = this.getRecordArray(historyRecords);
|
|
||||||
let minValue = Math.min.apply(Math, numberArray);
|
|
||||||
let minNumberOfDigits = Math.floor(Math.log(minValue) / Math.LN10 + 1)
|
|
||||||
let underMinValue = Math.floor( (minValue / Math.pow(10, minNumberOfDigits - 2) ) ) * Math.pow(10, minNumberOfDigits - 2);
|
|
||||||
return underMinValue;
|
|
||||||
},
|
|
||||||
|
|
||||||
maxValueOfData: function(historyRecords) {
|
|
||||||
let numberArray = this.getRecordArray(historyRecords);
|
|
||||||
let maxValue = Math.max.apply(Math, numberArray);
|
|
||||||
let maxNumberOfDigits = Math.floor(Math.log(maxValue) / Math.LN10 + 1)
|
|
||||||
let underMaxValue = Math.ceil( (maxValue / Math.pow(10, maxNumberOfDigits - 2) ) ) * Math.pow(10, maxNumberOfDigits - 2);
|
|
||||||
return underMaxValue;
|
|
||||||
},
|
|
||||||
|
|
||||||
getRecordArray: function(historyRecords) {
|
|
||||||
let numberArray = [];
|
|
||||||
for(let i = 0; i < historyRecords.length; i++) {
|
|
||||||
numberArray.push(historyRecords[i].record);
|
|
||||||
}
|
|
||||||
return numberArray;
|
|
||||||
},
|
|
||||||
|
|
||||||
printRecord: function(index, date, record, min, max) {
|
printRecord: function(index, date, record, min, max) {
|
||||||
const CHART_GAP_PX = 180;
|
const CHART_GAP_PX = 180;
|
||||||
const style = { font: "32px Arial", fill: "#fff", align: "left", boundsAlignH: "center", boundsAlignV: "middle" };
|
const style = { font: "32px Arial", fill: "#fff", align: "left", boundsAlignH: "center", boundsAlignV: "middle" };
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
<!-- library source files -->
|
<!-- library source files -->
|
||||||
<script src="../../game/lib/number_util.js"></script>
|
<script src="../../game/lib/number_util.js"></script>
|
||||||
|
<script src="../../game/lib/history_record_manager.js"></script>
|
||||||
<script src="../../game/lib/input_type_text.js"></script>
|
<script src="../../game/lib/input_type_text.js"></script>
|
||||||
<script src="../../game/lib/round_rect_button.js"></script>
|
<script src="../../game/lib/round_rect_button.js"></script>
|
||||||
<script src="../../game/lib/back_button.js"></script>
|
<script src="../../game/lib/back_button.js"></script>
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width">
|
||||||
|
<title>QUnit Example</title>
|
||||||
|
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.6.0.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="qunit"></div>
|
||||||
|
<div id="qunit-fixture"></div>
|
||||||
|
<script src="https://code.jquery.com/qunit/qunit-2.6.0.js"></script>
|
||||||
|
<script src="../src/game/lib/number_util.js"></script>
|
||||||
|
<script src="../src/game/lib/history_record_manager.js"></script>
|
||||||
|
<script src="test_history_record_manager.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -1,16 +1,16 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<meta name="viewport" content="width=device-width">
|
<meta name="viewport" content="width=device-width">
|
||||||
<title>QUnit Example</title>
|
<title>QUnit Example</title>
|
||||||
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.6.0.css">
|
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.6.0.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="qunit"></div>
|
<div id="qunit"></div>
|
||||||
<div id="qunit-fixture"></div>
|
<div id="qunit-fixture"></div>
|
||||||
<script src="https://code.jquery.com/qunit/qunit-2.6.0.js"></script>
|
<script src="https://code.jquery.com/qunit/qunit-2.6.0.js"></script>
|
||||||
<script src="../src/game/lib/number_util.js"></script>
|
<script src="../src/game/lib/number_util.js"></script>
|
||||||
<script src="test_number_util.js"></script>
|
<script src="test_number_util.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
let historyRecordManager = new HistoryRecordManager();
|
||||||
|
|
||||||
|
historyRecordManager.push(new HistoryRecordData("05/10", 1000));
|
||||||
|
historyRecordManager.push(new HistoryRecordData("05/11", 100));
|
||||||
|
historyRecordManager.push(new HistoryRecordData("05/12", 200));
|
||||||
|
historyRecordManager.push(new HistoryRecordData("05/13", 2000));
|
||||||
|
historyRecordManager.push(new HistoryRecordData("05/14", 10000));
|
||||||
|
|
||||||
|
QUnit.test( "push", function( assert ) {
|
||||||
|
assert.equal(historyRecordManager.count, 5);
|
||||||
|
});
|
||||||
|
|
||||||
|
QUnit.test( "getAt", function( assert ) {
|
||||||
|
assert.equal(historyRecordManager.getDateAt(0), "05/10");
|
||||||
|
assert.equal(historyRecordManager.getDateAt(4), "05/14");
|
||||||
|
});
|
||||||
|
|
||||||
|
QUnit.test( "clear", function( assert ) {
|
||||||
|
let historyRecordManagerForClear = new HistoryRecordManager();
|
||||||
|
historyRecordManagerForClear.push(new HistoryRecordData("05/10", 1));
|
||||||
|
historyRecordManagerForClear.clear();
|
||||||
|
|
||||||
|
assert.equal(historyRecordManagerForClear.count, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
QUnit.test( "underValueForGraph", function( assert ) {
|
||||||
|
assert.equal(historyRecordManager.underValueForGraph(), 100);
|
||||||
|
});
|
||||||
|
|
||||||
|
QUnit.test( "upperValueForGraph", function( assert ) {
|
||||||
|
assert.equal(historyRecordManager.upperValueForGraph(), 10000);
|
||||||
|
});
|
||||||
@@ -3,4 +3,15 @@ QUnit.test( "NumberWithCommas", function( assert ) {
|
|||||||
assert.equal(NumberUtil.numberWithCommas(1000), "1,000", "NumberWithCommas");
|
assert.equal(NumberUtil.numberWithCommas(1000), "1,000", "NumberWithCommas");
|
||||||
assert.equal(NumberUtil.numberWithCommas(100000), "100,000", "NumberWithCommas");
|
assert.equal(NumberUtil.numberWithCommas(100000), "100,000", "NumberWithCommas");
|
||||||
assert.equal(NumberUtil.numberWithCommas(1000000), "1,000,000", "NumberWithCommas");
|
assert.equal(NumberUtil.numberWithCommas(1000000), "1,000,000", "NumberWithCommas");
|
||||||
|
});
|
||||||
|
|
||||||
|
QUnit.test( "NumberOfDigits", function( assert ) {
|
||||||
|
assert.equal(NumberUtil.numberOfDigits(1), 1);
|
||||||
|
assert.equal(NumberUtil.numberOfDigits(10), 2);
|
||||||
|
assert.equal(NumberUtil.numberOfDigits(100), 3);
|
||||||
|
assert.equal(NumberUtil.numberOfDigits(999), 3);
|
||||||
|
assert.equal(NumberUtil.numberOfDigits(1000), 4);
|
||||||
|
assert.equal(NumberUtil.numberOfDigits(9999), 4);
|
||||||
|
assert.equal(NumberUtil.numberOfDigits(10000), 5);
|
||||||
|
assert.equal(NumberUtil.numberOfDigits(99999), 5);
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user