-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewscheduler.py
117 lines (104 loc) · 3.97 KB
/
newscheduler.py
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
import sys
from random import randint
import os.path
from itertools import permutations
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-input')
parser.add_argument('-gap')
args = parser.parse_args()
print(args)
class Match:
def __init__(self, b1, b2, g1, g2):
self.b1 = b1
self.b2 = b2
self.g1 = g1
self.g2 = g2
class Team:
def __init__(self, id, name):
self.id = id
self.name = name
self.canPlay = 0
self.playedWith = []
self.playedAgainst = []
schools = []
def process(line):
values = line.rstrip().split(",")
schools.append(Team(values[0], values[1]))
#input = sys.stdin.readline().rstrip().split(" ")
csvFile = args.input
with open(csvFile) as f:
for line in f:
process(line)
gap = int(args.gap) #Time before school can play again
matches = []
teamsLeft = schools.copy()
totalRounds = (len(schools)*3)//4
for i in range(totalRounds):
matchedTeams = []
if len(teamsLeft) >= 4:
j = 0
while j < 4:
team = teamsLeft[randint(0, len(teamsLeft) - 1)]
if team.canPlay >= 0:
matchedTeams.append(team)
teamsLeft.remove(team)
j += 1
else:
matchedTeams = teamsLeft[:]
restOfTeams = 4 - len(matchedTeams)
teamsLeft = schools.copy()
k = 0
while k < restOfTeams:
team = teamsLeft[randint(0, len(teamsLeft)-1)]
if team.canPlay >= 0 and team not in matchedTeams:
matchedTeams.append(team)
teamsLeft.remove(team)
k += 1
perm = list(permutations(range(4)))
for p in perm:
if matchedTeams[p[0]] not in matchedTeams[p[1]].playedWith and matchedTeams[p[2]] not in matchedTeams[p[3]].playedWith:
matches.append(Match(matchedTeams[p[0]],matchedTeams[p[1]],matchedTeams[p[2]],matchedTeams[p[3]]))
matchedTeams[p[0]].playedWith.append(matchedTeams[p[1]])
matchedTeams[p[1]].playedWith.append(matchedTeams[p[0]])
matchedTeams[p[2]].playedWith.append(matchedTeams[p[3]])
matchedTeams[p[3]].playedWith.append(matchedTeams[p[2]])
for i in range(2):
matchedTeams[p[i]].playedAgainst.append(matchedTeams[p[2]])
matchedTeams[p[i]].playedAgainst.append(matchedTeams[p[3]])
matchedTeams[p[i+2]].playedAgainst.append(matchedTeams[p[0]])
matchedTeams[p[i+2]].playedAgainst.append(matchedTeams[p[1]])
for t in [matchedTeams[p[r]] for r in range(4)]:
t.canPlay = gap*-1 -1
break
for s in schools:
s.canPlay += 1
if teamsLeft:
ghosts = 4 - len(teamsLeft)
for g in range(ghosts):
teamsLeft.append(Team("-1","Spooky Team"))
matchedTeams = teamsLeft
perm = list(permutations(range(4)))
for p in perm:
if matchedTeams[p[0]] not in matchedTeams[p[1]].playedWith and matchedTeams[p[2]] not in matchedTeams[p[3]].playedWith:
matches.append(Match(matchedTeams[p[0]],matchedTeams[p[1]],matchedTeams[p[2]],matchedTeams[p[3]]))
for i in range(2):
matchedTeams[p[i]].playedAgainst.append(matchedTeams[p[2]])
matchedTeams[p[i]].playedAgainst.append(matchedTeams[p[3]])
matchedTeams[p[i+2]].playedAgainst.append(matchedTeams[p[0]])
matchedTeams[p[i+2]].playedAgainst.append(matchedTeams[p[1]])
break
#for i in range(len(schools)):
#print(schools[i].name, [y.name for y in schools[i].playedAgainst])
writeHeader = False
if not os.path.isfile("matches.csv"):
writeHeader = True
f = open("matches.csv", "w+")
f.close()
with open("matches.csv", "a") as f:
f.write("Blue 1 ID, Blue 1 Name, Blue 2 ID, Blue 2 Name, Gold 1 ID, Gold 1 Name, Gold 2 ID, Gold 2 Name\n")
for m in matches:
f.write(m.b1.id + "," + m.b1.name +
"," + m.b2.id + "," + m.b2.name +
"," + m.g1.id + "," + m.g1.name +
"," + m.g2.id + "," + m.g2.name + "\n")