-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsvWork.js
225 lines (193 loc) · 9.85 KB
/
csvWork.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
`use strict`
const csv = require(`fast-csv`);
const fs = require(`fs`);
const _ = require(`lodash`);
module.exports = callback => {
let keyStream = fs.createReadStream(`./key.csv`);
let stream = fs.createReadStream("./test.csv");
let Master;
let Rest = [];
let Key = [];
csv.fromStream(keyStream, { headers: true })
.on(`data`, (data) => {
Key.push(data)
})
.on(`end`, () => {
_.forEach(Key, (value, key) => {
if (value.Rank.length === 1) {
value.Rank = `0${value.Rank}`
}
});
let p = _.map(Key, (value, key) => {
_.forEach(value, (val, ky) => {
if (ky !== `Rank`) {
// Do Nothing
value[`${ky.charAt(0)}${value.Rank}`] = val
delete value[ky]
} else {
// delete value[ky]
// console.log(ky)
}
});
return value
});
});
csv
.fromStream(stream, { headers: true })
.on("data", function (data) {
// console.log(data);
if (data.Name === `Master`) {
Master = data
} else {
Rest.push(data)
}
})
.on("end", function () {
// Create an object of finished games, this will be used for calculating the Pts Remaining, ADVANCED
// let finishedGamesPerRound = {};
// _.forEach(Master, (value, key) => {
// let val = value.replace(/---/g, ``)
// if (key === `Name`) {
// //Do Nothing
// } else {
// if (val === ``) {
// //Do Nothing
// } else {
// finishedGamesPerRound[key] = val
// }
// }
// });
// Get the last finished round from Master; this will be compared against future rounds in the users to calculate Pts Remaining
let lastFinishedRound = _.filter(Master, x => {
return (_.includes(x, `---`) == false)
}).pop().match(/.{1,3}/g);
// Create the final objects for each user that will be used for HighCharts
let FinalObjs = _.map(Rest, x => {
_.forEach(x, (value, key) => {
if (key === `Name`) {
//Do Nothing
} else {
// Get the strings for each round in Master
let userString = value;
let masterString = Master[key];
// console.log(userString)
// console.log(masterString)
// Divide the strings up into all of the games that were chosen
let userPicks = userString.match(/.{1,3}/g);
let masterPicks = masterString.match(/.{1,3}/g);
if (key === `Elite8`) {
// console.log(key)
// console.log(userPicks)
let arr = _.map(userPicks, (val, ky) => {
let obs = _.find(Key, o => {
return o.hasOwnProperty(val)
});
return obs[val]
});
x[`Final Four`] = arr
} else if (key === `Final4`) {
let arr = _.map(userPicks, (val, ky) => {
let obs = _.find(Key, o => {
return o.hasOwnProperty(val)
});
return obs[val]
});
x[`Top Two`] = arr
} else if (key === `CHAMPIONSHIP`) {
let arr = _.map(userPicks, (val, ky) => {
let obs = _.find(Key, o => {
return o.hasOwnProperty(val)
});
return obs[val]
});
x[`Champion`] = arr
}
// console.log(masterPicks)
// Work with partially finished or completely unfinished rounds for Pts Remaining calculations
if (_.includes(masterPicks, "---")) {
// Amount of games in an unfinished round that the user has gotten right
let NumCorrectChoicesInUnfinishedRound = _.filter(userPicks, x => {
return _.includes(masterPicks, x)
}).length
// All of the games in unfinished rounds that does not match the master
let NonMatches = _.filter(userPicks, x => {
return (_.includes(masterPicks, x) == false)
});
// console.log(NonMatches)
let gamesPossibleInThisRound = _.intersection(NonMatches, lastFinishedRound).length
// Start do the calculations for each
if (_.includes(key, '64')) {
x[`${key}PtsSoFar`] = NumCorrectChoicesInUnfinishedRound * 1
x[`${key}PtsRemain`] = gamesPossibleInThisRound * 1
} else if (_.includes(key, '32')) {
x[`${key}PtsSoFar`] = NumCorrectChoicesInUnfinishedRound * 2
x[`${key}PtsRemain`] = gamesPossibleInThisRound * 2
} else if (_.includes(key, '16')) {
x[`${key}PtsSoFar`] = NumCorrectChoicesInUnfinishedRound * 4
x[`${key}PtsRemain`] = gamesPossibleInThisRound * 4
} else if (_.includes(key, '8')) {
x[`${key}PtsSoFar`] = NumCorrectChoicesInUnfinishedRound * 8
x[`${key}PtsRemain`] = gamesPossibleInThisRound * 8
} else if (_.includes(key, '4')) {
x[`${key}PtsSoFar`] = NumCorrectChoicesInUnfinishedRound * 16
x[`${key}PtsRemain`] = gamesPossibleInThisRound * 16
} else if (_.includes(key.toLowerCase(), 'championship')) {
// x[`${key}PtsSoFar`] = NumCorrectChoicesInUnfinishedRound * 32
x[`${key}PtsRemain`] = gamesPossibleInThisRound * 32
} else {
//Do Nothing
}
} else { // All Rounds that have finished
// Return all the matches that the user got right
let userMatches = _.filter(userPicks, x => {
return _.includes(masterPicks, x)
});
// How many matches?
x[`${key}NumberCorrect`] = userMatches.length;
let NumberCorrect = x[`${key}NumberCorrect`]
// Create the points for each round
if (_.includes(key, '64')) {
x[`${key}Points`] = NumberCorrect * 1;
} else if (_.includes(key, '32')) {
x[`${key}Points`] = NumberCorrect * 2;
} else if (_.includes(key, '16')) {
x[`${key}Points`] = NumberCorrect * 4;
} else if (_.includes(key, '8')) {
x[`${key}Points`] = NumberCorrect * 8;
} else if (_.includes(key, '4')) {
x[`${key}Points`] = NumberCorrect * 16;
} else if (_.includes(key.toLowerCase(), 'championship')) {
x[`${key}Points`] = NumberCorrect * 32;
} else {
//Do Nothing
}
}
}
// console.log(x)
});
// console.log(x)
TotalCorrect = null;
TotalPoints = null;
PtsRemain = null;
// Get the totals
_.forEach(x, (value, key) => {
if (_.includes(key.toLowerCase(), "points")) {
TotalPoints += x[key]
} else if (_.includes(key.toLowerCase(), "correct")) {
TotalCorrect += x[key]
} else if (_.includes(key.toLowerCase(), "remain")) {
PtsRemain += x[key]
}
});
x.TotalCorrect = TotalCorrect;
x.TotalPoints = TotalPoints;
x.PtsRemain = PtsRemain
// console.log(x)
return _.pick(x, [`Name`, `Round64Points`, `Round64PtsSoFar`, `Round32Points`, `Round32PtsSoFar`,
`Sweet16Points`, `Sweet16PtsSoFar`, `Elite8Points`, `Elite8PtsSoFar`, `Final4Points`, `Final4PtsSoFar`, `Final Four`, `Top Two`, `Champion`, `CHAMPIONSHIPPoints`, `TotalCorrect`, `TotalPoints`, `PtsRemain`])
});
let orderObjs = _.orderBy(FinalObjs, [`TotalPoints`], [`desc`])
// console.log(orderObjs)
callback(orderObjs)
});
}