-
Notifications
You must be signed in to change notification settings - Fork 0
/
soccerParser.js
73 lines (59 loc) · 3.34 KB
/
soccerParser.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
function getAllPlayersXYAllFrames(matchString) { // generates a data structure for the whole soccer match
let matchArray = splitOnNewLine(matchString); // the array contains one line for each timestamp
let matchPosArray = [];
// Since the each string in the array can start with one or more spaces, it is necessary to uniform this notation
// In particular, the beginning of the string should be the only place where there is more than one space
// At the end of the procedure all the strings have to start with one space followed by a non-space char
// If there is not a space as first char, therefore it has to be inserted
// Then, if two or more spaces are found, they are replaced by a single space
for(let k=0; k<matchArray.length; ++k) {
if(matchArray[k][0]!=" ") { // if it dows not start with a space
matchArray[k] = " " + matchArray[k]; // add a space at the beginning
}
if(matchArray[k].length > 10) {
// Now when two spaces or more spaces are found, they are replaced by a single space => replace(/ +/g, ' ')
// Then the string is split into arrays when a space is found
matchPosArray.push(getAllPlayersXYFrame(matchArray[k].replace(/ +/g, ' ').split(" ")));
}
}
return matchPosArray;
// This array contains 2 arrays, each containing the positions of n players
}
function getAllPlayersXYFrame(frameArray) { // returns two arrays, one for the first team and th other for the second team
let teamPosList = [];
teamPosList.push(getTeamPlayersXYFrame(frameArray, 1)); // this is the first team
teamPosList.push(getTeamPlayersXYFrame(frameArray, 2)); // this is the second team
return teamPosList;
}
function getTeamPlayersXYFrame(frameArray, team) { // returns the list of the players for a given team
let posList = [];
let startingPosition = 1; // number of empty elements at the beginning of the array
if(frameArray[frameArray.length-1]=="") {
frameArray.pop(); // removes the last (useless) element
}
let teamSize = (frameArray.length - startingPosition - 1)/4;
// teamSize => the number of players for a team (-startingPosition because the array always contains an empty element at the beginning,
// -1 because the first element is a timestamp, and -endSpaces since the last element can be an empty string)
// Since a position has two coordinates, the team size is the number of actual coordinates divided by 4 (2 because there are 2 teams
// and 2 because of the two coordinates)
if(teamSize > 10) { // Removes lines without a readable content
for(let j=1+(teamSize-1)*(team-1); j<teamSize+(teamSize-1)*(team-1); ++j) { // for every player in the team
posList.push(getNthPlayerXY(frameArray, j)); // put the coordinates of a player in the team array
}
}
return posList;
}
function getNthPlayerXY(frameArray, num) { // gets the position (x, y) of the n-th player
return [getNthPlayerX(frameArray, num), getNthPlayerY(frameArray, num)];
}
function getNthPlayerX(frameArray, num) { // gets the n-th player x position
return frameArray[2*num];
}
function getNthPlayerY(frameArray, num) { // gets the n-th player y position
return frameArray[2*num+1];
}
function fromFrameToPosition(frameNr, playerNr, matchArray) {
let x = matchArray[frameNr][Math.floor(playerNr/15)][playerNr][0];
let y = matchArray[frameNr][Math.floor(playerNr/15)][playerNr][1];
return [x, y];
}