-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadText.js
More file actions
154 lines (144 loc) · 4.45 KB
/
readText.js
File metadata and controls
154 lines (144 loc) · 4.45 KB
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
var fs = require("fs");
function myfind(array, round, team) {
let value = null;
array.forEach(x => {
if (x.round == round && (x.player1 == team) | (x.player2 == team))
value = x;
else return false;
});
return value;
}
function getTeams(games){
let teams = [];
games.forEach((element) => {
if(!teams.includes(element.player1)){
teams.push(element.player1);
}
if (!teams.includes(element.player2)) {
teams.push(element.player2);
}
});
return teams;
}
function getGames(year) {
let games = [];
let content = fs.readFileSync(`data/${year}.txt`, "utf8");
lines = content.split("\n").forEach(function(line) {
let elements = line.split(" ");
games.push({
player1: elements[3],
player2: elements[7],
p1Score: elements[4],
p2Score: elements[6],
round: elements[0]
});
});
return games;
}
function getParams(round, teamA, teamB, games, range) {
const tAStats = {
outGoals: 0,
homeGoals: 0,
goalDif: 0,
goalsTaken: 0,
goalsTotal: 0
};
const tBStats = {
outGoals: 0,
homeGoals: 0,
goalDif: 0,
goalsTaken: 0,
goalsTotal: 0
};
const match = {
teamA:'',
teamB:'',
stadiumOwner: "",
matchGoalsTA: 0,
matchGoalsTB: 0
};
if (round - range < 1) return "Range too low";
//Team 1
for (i = 1; i <= range; i++) {
let roundReal = "" + (round - i) + "ª";
let game = myfind(games, roundReal, teamA);
if (teamA == game.player1) {
tAStats.homeGoals += parseInt(game.p1Score);
tAStats.goalsTaken += parseInt(game.p2Score);
tAStats.goalDif += parseInt(game.p1Score) - parseInt(game.p2Score);
tAStats.goalsTotal += parseInt(game.p1Score);
} else {
tAStats.outGoals += parseInt(game.p2Score);
tAStats.goalsTaken += parseInt(game.p1Score);
tAStats.goalDif += parseInt(game.p2Score) - parseInt(game.p1Score);
tAStats.goalsTotal += parseInt(game.p2Score);
}
}
//Team 2
for (i = 1; i < range; i++) {
roundReal = "" + (round - i) + "ª";
game = myfind(games, roundReal, teamB);
if (teamB == game.player1) {
tBStats.homeGoals += parseInt(game.p1Score);
tBStats.goalsTaken += parseInt(game.p2Score);
tBStats.goalDif += parseInt(game.p1Score) - parseInt(game.p2Score);
tBStats.goalsTotal += parseInt(game.p1Score);
} else {
tBStats.outGoals += parseInt(game.p2Score);
tBStats.goalsTaken += parseInt(game.p1Score);
tBStats.goalDif += parseInt(game.p2Score) - parseInt(game.p1Score);
tBStats.goalsTotal += parseInt(game.p2Score);
}
}
roundReal = "" + round + "ª";
game = myfind(games, roundReal, teamA);
match.teamA = teamA;
match.teamB = teamB;
if (game.player1 == teamA) {
match.stadiumOwner = teamA;
match.matchGoalsTA = parseInt(game.p1Score);
match.matchGoalsTB = parseInt(game.p2Score);
} else {
match.stadiumOwner = teamB;
match.matchGoalsTA = parseInt(game.p2Score);
match.matchGoalsTB = parseInt(game.p1Score);
}
const params = [tAStats, tBStats, match];
return params;
}
module.exports = function getFullData2(year, team){
let teamScores = [];
let opponentScores = [];
let games = getGames(year);
for(let i = 0; i <games.length; i++){
if(team == games[i].player1){
teamScores.push( parseInt(games[i].p1Score) );
opponentScores.push( parseInt(games[i].p2Score) );
}
if(team == games[i].player2){
teamScores.push( parseInt(games[i].p2Score) );
opponentScores.push(parseInt(games[i].p1Score));
}
}
return {ts: teamScores, os: opponentScores};
}
function getFullData(years){
let totalParams = [];
years.forEach( (element) => {
let games = getGames(element);
for(let i =0; i < games.length; i++){
let round = games[i].round;
let roundNum = parseInt(round.slice(0, -1));
if(roundNum > 4){
let params = getParams(roundNum, games[i].player1, games[i].player2, games, 3);
totalParams.push(params);
}
}
});
return totalParams;
}
//let years = [2016, 2017];
//let fullData = getFullData(years);
//console.log(fullData);
//let myGames = getGames('2013');
//let params = getParams(22, 'Cruzeiro', 'Botafogo', 2013, 5);