-
Notifications
You must be signed in to change notification settings - Fork 2
/
utilities.R
117 lines (108 loc) · 3.57 KB
/
utilities.R
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
parse.weeks = function(raw_data){
nteams = 2*(which(raw_data=="3\U00AA Giornata lega")-which(raw_data=="1\U00AA Giornata lega")-1)
offset.col = 6
offset.row = nteams/2+1
offset.beginning = 4
week.height = nteams/2-1
week.width = 4
weeks = list()
for(i in 0:((nrow(raw_data)-2)/((nteams/2)+1)-1)){
week = raw_data[(offset.beginning+offset.row*i):(offset.beginning+offset.row*i+week.height),1:week.width]
names(week) = c("team1","pts1","pts2","team2")
week$pts1 = as.numeric(week$pts1)
week$pts2 = as.numeric(week$pts2)
weeks = append(weeks,list(week))
week = raw_data[(offset.beginning+offset.row*i):(offset.beginning+offset.row*i+week.height),(1+offset.col):(week.width+offset.col)]
names(week) = c("team1","pts1","pts2","team2")
week$pts1 = as.numeric(week$pts1)
week$pts2 = as.numeric(week$pts2)
weeks = append(weeks,list(week))
}
return(weeks)
}
team.names = function(weeks){
return(c(weeks[[1]]$team1,weeks[[1]]$team2))
}
calculateGoals = function(weeks){
scores = matrix(0,nrow = length((weeks)),ncol = length(team.names(weeks)))
colnames(scores) = team.names(weeks)
for(i in 1:length(weeks)){
week = weeks[[i]]
weekResults = lapply(getMatches(week), calculateResults)
scores[i,] = updateScores(scores[i,],weekResults)
}
return(scores)
}
calculateResults = function(match){
goal1 = floor((as.numeric(match["pts1"])-60)/6)
goal2 = floor((as.numeric(match["pts2"])-60)/6)
if(goal1<0) goal1 = 0
if(goal2<0) goal2 = 0
results = c(goal1,goal2)
names(results) = c(match["team1"],match["team2"])
return(results)
}
updateScores = function(scores, results){
res = unlist(results)
names(res) = unlist(lapply(names(unlist(results)),function(x){substring(x, 3)}))
for(r in results){
scores[names(r)]=scores[names(r)]+r[names(r)]
}
return(scores)
}
getMatches = function(week){
return(suppressWarnings(split(week,row(week))))
}
getRounds = function(teamNames,nrounds){
teams <- teamNames
n = length(teams)
rounds <- list()
for( i in 1:nrounds){
round <-
data.frame(
round = i,
team1 = teams[1:(n/2)],
team2 = rev(teams)[1:(n/2)])
rounds[[i]] <- round
teams <- c( teams[1], last(teams), head(teams[-1],-1) )
}
return(suppressWarnings(bind_rows(rounds)))
}
calculateFinal = function(goals,rounds){
nrounds = nrow(goals)
finals = array(0,dim = ncol(goals))
names(finals)=colnames(goals)
for(round in 1:nrounds){
week = rounds[which(rounds$round==round),]
for(i in 1:nrow(week)) {
g1 = goals[round,week[i,"team1"]]
g2 = goals[round,week[i,"team2"]]
if(g1>g2) finals[week[i,"team1"]] = finals[week[i,"team1"]]+3
else if (g1<g2) finals[week[i,"team2"]] = finals[week[i,"team2"]]+3
else{
finals[week[i,"team2"]] = finals[week[i,"team2"]]+1
finals[week[i,"team1"]] = finals[week[i,"team1"]]+1
}
}
}
return(finals)
}
getPerms <- function(x) {
incProgress(0.75, detail = paste("Calcolo permutazioni..."))
Sys.sleep(1)
incProgress(0.15, detail = paste(factorial(length(x))/1000,"mila permutazioni da calcolare...potrebbe volerci un po'!"))
p = permutations(n=length(x),r=length(x),v=x)
incProgress(0.10, detail = paste("Fine!"))
return(p)
}
getActualRounds <- function(weeks){
rounds = data.frame()
for(i in 1:length(weeks)){
week = weeks[[i]]
round = data.frame(cbind(rep(i,length(week$team1)),week$team1,week$team2),stringsAsFactors = FALSE)
names(round) = c("round","team1","team2")
round$round = as.integer(round$round)
rounds <- rbind(rounds,round)
}
return(rounds)
}