-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscore.js
143 lines (116 loc) · 3.88 KB
/
score.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**********************************
* 各種モジュールの読み込み
**********************************/
const fs = require('fs');
const moment = require('moment');
/**********************************
* メイン処理
**********************************/
/**
* セレクト情報ファイルを読み込む
*/
const loadScoreInfo = function () {
return new Promise(function (resolve, reject) {
let fileName = moment().format('YYYYMMDD') + '.json';
let fileList = fs.readdirSync(__dirname);
let scoreList = [];
if(fileList.indexOf(fileName) !== -1) {
scoreList = fs.readFileSync(__dirname + '/' + fileName, 'utf-8');
scoreList = JSON.parse(scoreList);
} else {
// ファイル保存
fs.writeFileSync(__dirname + '/' + fileName, '');
}
resolve(scoreList);
});
};
/**
* オッズ情報ファイルを読み込む
*/
const loadOddsInfo = function (scoreList) {
return new Promise(function (resolve, reject) {
let oddsList = fs.readFileSync(__dirname + '/oddsinfo.json', 'utf-8');
oddsList = JSON.parse(oddsList);
if(scoreList.length === 0) {
oddsList.forEach(oddsMap => {
let raceList = oddsMap['race_data'];
let subScoreList = [];
raceList.forEach(raceMap => {
let scoreMap = {
"number": raceMap['number'],
"name": raceMap['name'],
"score": 0
}
subScoreList.push(scoreMap);
});
subScoreList.sort(function(a, b){
if(a.number < b.number) return -1;
if(a.number > b.number) return 1;
return 0;
});
scoreList.push({
"race_id": oddsMap['race_id'],
"race_data": subScoreList
})
});
}
resolve(scoreList);
});
};
/**
* ツイート情報ファイルを読み込む
*/
const loadSelectInfo = function (scoreList) {
return new Promise(function (resolve, reject) {
let selectList = fs.readFileSync(__dirname + '/select.json', 'utf-8');
selectList = JSON.parse(selectList);
selectList.forEach(selectMap => {
let selectRaceId = selectMap['race_id'];
scoreList.some((scoreMap, i) => {
let scoreRaceId = scoreMap['race_id'];
if(selectRaceId == scoreRaceId) {
scoreMap['race_data'].some((raceMap, j) => {
if(selectMap['number'] == raceMap['number']) {
scoreList[i]['race_data'][j]['score']++;
return true;
}
});
return true;
}
});
});
resolve(scoreList);
});
};
/**
* メイン処理の実行順を定義する
*/
const main = function () {
return new Promise(function (resolve, reject) {
Promise.resolve()
.then(function () {
// ファイル読み込み
return loadScoreInfo();
})
.then(function (results) {
// ファイル読み込み
return loadOddsInfo(results);
})
.then(function (results) {
// ファイル読み込み
return loadSelectInfo(results);
})
.then(function (results) {
// ファイル保存
fs.writeFileSync(__dirname + '/' + moment().format('YYYYMMDD') + '.json', JSON.stringify(results, null, ' '));
// 完了
resolve();
})
.catch(function (err) {
// エラー通知
reject(err);
});
});
};
// メイン処理を実行
main();