-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
256 lines (145 loc) · 4.17 KB
/
server.py
File metadata and controls
256 lines (145 loc) · 4.17 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import os
import json
import pickle
import random
from app import app
from visitor import Visitor, db
from flask import Flask, request, render_template
db.create_all()
db.session.commit()
with open('2017-1.pickle', 'rb') as f:
lectures = pickle.load(f)
def randomLecture(lower, upper, rooms = None):
global lectures
point = 0
selected = list()
if rooms == None:
lec_pool = list(lectures)
else:
lec_pool = list()
for lec in lectures:
for room in lec['room']:
if any(r in room for r in rooms):
lec_pool.append(lec)
if '생명과학관' in room and '동관' in room and '생명과학관 동관' in rooms:
lec_pool.append(lec)
if '생명과학관' in room and '서관' in room and '생명과학관 서관' in rooms:
lec_pool.append(lec)
random.shuffle(lec_pool)
for lec in lec_pool:
if point > lower:
break
else:
temp = set()
for sel in selected:
temp = temp | set(sel['time'])
if len(set(lec['time']) & temp) == 0 and upper >= point + int(lec['hakjum']):
selected.append(lec)
point = point + int(lec['hakjum'])
for time in lec['time']:
if int(time[1:]) > 8 or time[0] == '토':
selected.pop()
point = point - int(lec['hakjum'])
break
else:
pass
return selected, point
def makeDrawLecture(req, trial):
lower = 0
candidate = list()
cand_hakjum = list()
similarity = list()
day = {'월' : 1, '화' : 2, '수' : 3, '목' : 4, '금' : 5}
for key in req:
if int(key[1]) in [1, 2, 5, 6]:
lower += float(req[key]) * 1.5
else:
lower += float(req[key]) * 1
for idx in range(0, trial):
lecture, hakjum = randomLecture(int(lower) - 1, int(lower) + 1)
candidate.append(lecture)
cand_hakjum.append(hakjum)
for cand in candidate:
times = set()
for lec in cand:
point = 0
for t in lec['time']:
times = times | {'c' + t[1] + str(day[t[0]])}
for key in req:
if int(req[key]) == 1:
if key in times:
point += 1
else:
point -= 2
else:
if key not in times:
point += 1
else:
point -= 2
similarity.append(point)
for idx in range(0, trial):
if similarity[idx] == max(similarity):
selected = candidate[idx]
selected_hakjum = cand_hakjum[idx]
break
return selected, selected_hakjum
def makeMapLecture(req):
rooms = list()
for key in req:
if int(req[key]) == 1:
rooms.append(key)
if '생명과학관' in key:
if '동관' in key:
rooms.append('생명과학관 동관')
if '서관' in key:
rooms.append('생명과학관 서관')
else:
pass
lecture, hakjum = randomLecture(17, 22, rooms)
return lecture, hakjum
@app.route('/', methods = ['GET'])
@app.route('/index', methods = ['GET'])
def index():
visitor = Visitor.query.get(1 )
return render_template('index.html', cnt = str(visitor.count))
@app.route('/random', methods = ['GET'])
def randomShow():
visitor = Visitor.query.get(1)
visitor.count += 1
db.session.commit()
result, point = randomLecture(17, 22)
return render_template('random.html', selected = str(result), hakjum = point)
@app.route('/draw', methods = ['GET'])
def draw():
return render_template('draw.html')
@app.route('/map', methods = ['GET'])
def map():
return render_template('map.html')
@app.route('/show', methods = ['GET', 'POST'])
def show():
visitor = Visitor.query.get(1)
visitor.count += 1
db.session.commit()
if request.method == 'POST':
if len(request.form) == 40:
# draw 에서 왔을 때
result, point = makeDrawLecture(request.form, 500)
return render_template('show.html', selected = str(result), hakjum = point)
elif len(request.form) == 28:
# map 에서 왔을 때
result, point = makeMapLecture(request.form)
return render_template('show.html', selected = str(result), hakjum = point)
else:
return render_template('draw.html')
else:
return render_template('draw.html')
@app.route('/about', methods = ['GET'])
def about():
return render_template('about.html')
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host = '0.0.0.0', port = port)
if Visitor.query.count() == 0:
visitor = Visitor(0)
db.session.add(visitor)
db.session.commit()