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
}
}
+18 -47
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));
loadHistoryRecords: function() {
this.historyRecordManager.clear();
return array;
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>
+17
View File
@@ -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>
+32
View File
@@ -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);
});
+11
View File
@@ -4,3 +4,14 @@ QUnit.test( "NumberWithCommas", function( assert ) {
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);
});