Add: HistoryRecordManager

This commit is contained in:
2018-05-10 18:12:04 +09:00
parent 7fc6364fc0
commit b524614220
8 changed files with 175 additions and 57 deletions
+82
View File
@@ -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
View File
@@ -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
}
}
+19 -48
View File
@@ -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" };
+1
View File
@@ -16,6 +16,7 @@
<!-- library source files -->
<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/round_rect_button.js"></script>
<script src="../../game/lib/back_button.js"></script>