From b5246142200f4888530bd8238d3f450ad201d3ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=A2=E1=84=91=E1=85=B3=E1=86=AF=20=E1=84=82?= =?UTF-8?q?=E1=85=A9=E1=84=90=E1=85=B3=E1=84=87=E1=85=AE=E1=86=A8?= Date: Thu, 10 May 2018 18:12:04 +0900 Subject: [PATCH] Add: HistoryRecordManager --- src/game/lib/history_record_manager.js | 82 ++++++++++++++++++++++++++ src/game/lib/number_util.js | 4 ++ src/game/start/start.js | 67 ++++++--------------- src/web/client/start.html | 1 + test/history_record_manager.html | 17 ++++++ test/number_util.html | 18 +++--- test/test_history_record_manager.js | 32 ++++++++++ test/test_number_util.js | 11 ++++ 8 files changed, 175 insertions(+), 57 deletions(-) create mode 100644 src/game/lib/history_record_manager.js create mode 100644 test/history_record_manager.html create mode 100644 test/test_history_record_manager.js diff --git a/src/game/lib/history_record_manager.js b/src/game/lib/history_record_manager.js new file mode 100644 index 0000000..371f383 --- /dev/null +++ b/src/game/lib/history_record_manager.js @@ -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; + } + + +} \ No newline at end of file diff --git a/src/game/lib/number_util.js b/src/game/lib/number_util.js index 48a56c8..361e16c 100644 --- a/src/game/lib/number_util.js +++ b/src/game/lib/number_util.js @@ -4,4 +4,8 @@ class NumberUtil { 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 + } + } \ No newline at end of file diff --git a/src/game/start/start.js b/src/game/start/start.js index 42ce790..6fda8c5 100644 --- a/src/game/start/start.js +++ b/src/game/start/start.js @@ -1,14 +1,6 @@ ///////////////////////////// // Start -class HistoryRecordData { - - constructor(date, record) { - this.date = date; - this.record = record; - } -} - let Start = { preload: function() { @@ -16,6 +8,8 @@ let Start = { }, create: function() { + this.historyRecordManager = new HistoryRecordManager(); + this.game.stage.backgroundColor = '#4d4d4d'; this.chartGraphics = game.add.graphics(100, game.world.height - 120); @@ -31,8 +25,9 @@ let Start = { // contents this.printHowToPlay(appInfoManager.getHowToPlayText(sessionStorageManager.playingAppName)); this.makeStartButton(); - let historyRecords = this.makeHistoryRecords(); - this.printRecordHistory(historyRecords); + + this.loadHistoryRecords(); + this.printHistoryRecords(); // bottom @@ -62,23 +57,23 @@ let Start = { startButton.move(game.world.centerX, game.world.centerY); }, - makeHistoryRecords: function() { - let array = []; - array.push(new HistoryRecordData("05/10", 15460)); - array.push(new HistoryRecordData("05/11", 13040)); - array.push(new HistoryRecordData("05/12", 16460)); - array.push(new HistoryRecordData("05/13", 15060)); - array.push(new HistoryRecordData("05/14", 19800)); - - return array; + loadHistoryRecords: function() { + this.historyRecordManager.clear(); + + this.historyRecordManager.push(new HistoryRecordData("05/10", 15460)); + this.historyRecordManager.push(new HistoryRecordData("05/11", 13040)); + this.historyRecordManager.push(new HistoryRecordData("05/12", 16460)); + this.historyRecordManager.push(new HistoryRecordData("05/13", 15060)); + this.historyRecordManager.push(new HistoryRecordData("05/14", 19800)); }, - printRecordHistory: function(historyRecords) { - let min = this.minValueOfData(historyRecords); - let max = this.maxValueOfData(historyRecords); + printHistoryRecords: function(historyRecords) { + let underValue = this.historyRecordManager.underValueForGraph(); + let upperValue = this.historyRecordManager.upperValueForGraph(); - for(let i = 0; i < historyRecords.length; i++) { - this.printRecord(i, historyRecords[i].date, historyRecords[i].record, min, max); + for(let i = 0; i < this.historyRecordManager.count; i++) { + let data = this.historyRecordManager.getAt(i); + this.printRecord(i, data.date, data.record, underValue, upperValue); } this.chartGraphics.lineStyle(3, 0xffd900, 1); @@ -86,30 +81,6 @@ let Start = { 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) { const CHART_GAP_PX = 180; const style = { font: "32px Arial", fill: "#fff", align: "left", boundsAlignH: "center", boundsAlignV: "middle" }; diff --git a/src/web/client/start.html b/src/web/client/start.html index bbe89b7..ba7f190 100644 --- a/src/web/client/start.html +++ b/src/web/client/start.html @@ -16,6 +16,7 @@ + diff --git a/test/history_record_manager.html b/test/history_record_manager.html new file mode 100644 index 0000000..413b619 --- /dev/null +++ b/test/history_record_manager.html @@ -0,0 +1,17 @@ + + + + + + QUnit Example + + + +
+
+ + + + + + \ No newline at end of file diff --git a/test/number_util.html b/test/number_util.html index 2b7eba8..dfe056f 100644 --- a/test/number_util.html +++ b/test/number_util.html @@ -1,16 +1,16 @@ - - - QUnit Example - + + + QUnit Example + -
-
- - - +
+
+ + + \ No newline at end of file diff --git a/test/test_history_record_manager.js b/test/test_history_record_manager.js new file mode 100644 index 0000000..b38c06d --- /dev/null +++ b/test/test_history_record_manager.js @@ -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); +}); diff --git a/test/test_number_util.js b/test/test_number_util.js index bdd4766..e5fc80f 100644 --- a/test/test_number_util.js +++ b/test/test_number_util.js @@ -3,4 +3,15 @@ QUnit.test( "NumberWithCommas", function( assert ) { assert.equal(NumberUtil.numberWithCommas(1000), "1,000", "NumberWithCommas"); assert.equal(NumberUtil.numberWithCommas(100000), "100,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); }); \ No newline at end of file