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 @@ + + +
+ + +