/
course.go
398 lines (332 loc) · 8.54 KB
/
course.go
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package model
import (
"context"
"fmt"
"strings"
"sync"
"time"
"github.com/isucon/isucon11-final/benchmarker/api"
"github.com/isucon/isucon11-final/benchmarker/util"
)
type ReservationResult int
const (
Succeeded ReservationResult = iota
NotAvailable
)
const (
// ClassCountPerCourse は科目あたりの講義数 -> used in model/course.go
ClassCountPerCourse = 5
)
type CourseParam struct {
Code string
Type string
Name string
Description string
Credit int
Teacher string
Period int
DayOfWeek int
Keywords string
}
type Course struct {
*CourseParam
ID string
teacher *Teacher
registeredStudents map[string]*Student
reservations int
capacity int // 登録学生上限
capacityCounter *CapacityCounter
classes []*Class
status api.CourseStatus
closer chan struct{}
zeroReservationCond *sync.Cond
once sync.Once
rmu sync.RWMutex
}
type SearchCourseParam struct {
Type string
Credit int
Teacher string
Period int
DayOfWeek int
Keywords []string
Status string
}
func NewCourse(param *CourseParam, id string, teacher *Teacher, capacity int, capacityCounter *CapacityCounter) *Course {
c := &Course{
CourseParam: param,
ID: id,
teacher: teacher,
registeredStudents: make(map[string]*Student, capacity),
capacity: capacity,
capacityCounter: capacityCounter,
classes: make([]*Class, 0, ClassCountPerCourse),
status: api.StatusRegistration,
closer: make(chan struct{}, 0),
}
c.zeroReservationCond = sync.NewCond(&c.rmu)
return c
}
func (c *Course) AddClass(class *Class) {
c.rmu.Lock()
defer c.rmu.Unlock()
c.classes = append(c.classes, class)
}
func (c *Course) Status() api.CourseStatus {
c.rmu.RLock()
defer c.rmu.RUnlock()
return c.status
}
func (c *Course) SetStatusToInProgress() {
c.rmu.Lock()
defer c.rmu.Unlock()
c.status = api.StatusInProgress
}
func (c *Course) SetStatusToClosed() {
c.rmu.Lock()
defer c.rmu.Unlock()
c.status = api.StatusClosed
}
func (c *Course) Wait(ctx context.Context, cancel context.CancelFunc, addCourseFunc func()) <-chan struct{} {
go func() {
select {
case <-c.closer:
// 科目の履修を締め切ったときに、次の科目を追加する
addCourseFunc()
case <-ctx.Done():
}
c.zeroReservationCond.L.Lock()
for c.reservations > 0 {
c.zeroReservationCond.Wait()
}
c.zeroReservationCond.L.Unlock()
cancel()
}()
return ctx.Done()
}
func (c *Course) Teacher() *Teacher {
c.rmu.RLock()
defer c.rmu.RUnlock()
return c.teacher
}
func (c *Course) Students() map[string]*Student {
c.rmu.RLock()
defer c.rmu.RUnlock()
s := make(map[string]*Student, len(c.registeredStudents))
for userCode, user := range c.registeredStudents {
s[userCode] = user
}
return s
}
func (c *Course) Classes() []*Class {
c.rmu.RLock()
defer c.rmu.RUnlock()
cs := make([]*Class, len(c.classes))
copy(cs, c.classes[:])
return cs
}
// ReserveIfAvailable は履修受付中なら1枠確保する
func (c *Course) ReserveIfAvailable() ReservationResult {
c.rmu.Lock()
defer c.rmu.Unlock()
select {
case <-c.closer:
return NotAvailable
default:
}
if len(c.registeredStudents)+c.reservations >= c.capacity {
return NotAvailable
}
c.reservations++
return Succeeded
}
func (c *Course) CommitReservation(s *Student) {
c.rmu.Lock()
defer c.rmu.Unlock()
c.registeredStudents[s.Code] = s
c.reservations--
if c.reservations == 0 {
c.zeroReservationCond.Broadcast()
}
// 満員が確定した時、履修を締め切る
if len(c.registeredStudents) == c.capacity {
select {
case <-c.closer:
// close済み
default:
close(c.closer)
}
}
// 仮登録者数がゼロで履修する可能性のある学生がいない時、履修を締め切る
if c.reservations == 0 && c.capacityCounter.Get(c.DayOfWeek, c.Period) == 0 {
select {
case <-c.closer:
// close済み
default:
close(c.closer)
}
}
}
func (c *Course) RollbackReservation() {
c.rmu.Lock()
defer c.rmu.Unlock()
c.reservations--
if c.reservations == 0 {
c.zeroReservationCond.Broadcast()
}
// 仮登録者数がゼロで履修する可能性のある学生がいない時、履修を締め切る
if c.reservations == 0 && c.capacityCounter.Get(c.DayOfWeek, c.Period) == 0 {
select {
case <-c.closer:
// close済み
default:
close(c.closer)
}
}
}
func (c *Course) StartTimer(duration time.Duration) {
c.once.Do(func() {
go func() {
time.Sleep(duration)
c.rmu.Lock()
defer c.rmu.Unlock()
select {
case <-c.closer:
// close済み
default:
close(c.closer)
}
}()
})
}
// for prepare
func (c *Course) AddStudent(student *Student) {
c.rmu.Lock()
defer c.rmu.Unlock()
c.registeredStudents[student.Code] = student
}
func (c *Course) BroadCastAnnouncement(a *Announcement) {
c.rmu.Lock()
defer c.rmu.Unlock()
for _, s := range c.registeredStudents {
s.AddAnnouncement(a)
}
}
func (c *Course) CollectSimpleClassScores(userCode string) []*SimpleClassScore {
c.rmu.RLock()
defer c.rmu.RUnlock()
res := make([]*SimpleClassScore, 0, len(c.classes))
for _, class := range c.classes {
res = append(res, class.IntoSimpleClassScore(userCode))
}
return res
}
func (c *Course) CollectClassScores(userCode string) []*ClassScore {
c.rmu.RLock()
defer c.rmu.RUnlock()
res := make([]*ClassScore, 0, len(c.classes))
for _, class := range c.classes {
res = append(res, class.IntoClassScore(userCode))
}
return res
}
func (c *Course) CalcCourseResultByStudentCode(code string) *CourseResult {
c.rmu.RLock()
defer c.rmu.RUnlock()
if _, ok := c.registeredStudents[code]; !ok {
panic("unreachable! user is not found. userCode: " + code)
}
totalScores := c.calcTotalScores()
totalScoresArr := make([]int, 0, len(totalScores))
for _, totalScore := range totalScores {
totalScoresArr = append(totalScoresArr, totalScore)
}
totalAvg := util.AverageInt(totalScoresArr, 0)
totalMax := util.MaxInt(totalScoresArr, 0)
totalMin := util.MinInt(totalScoresArr, 0)
// totalscore は registeredStudents に対して 0 点になるのでここは大丈夫
totalScore, ok := totalScores[code]
if !ok {
panic("unreachable! userCode: " + code)
}
totalTScore := util.TScoreInt(totalScore, totalScoresArr)
classScores := c.CollectClassScores(code)
return &CourseResult{
Name: c.Name,
Code: c.Code,
TotalScore: totalScore,
TotalScoreTScore: totalTScore,
TotalScoreAvg: totalAvg,
TotalScoreMax: totalMax,
TotalScoreMin: totalMin,
ClassScores: classScores,
}
}
func (c *Course) GetTotalScoreByStudentCode(code string) int {
c.rmu.RLock()
defer c.rmu.RUnlock()
score := 0
for _, class := range c.classes {
submission := class.GetSubmissionByStudentCode(code)
if submission != nil && submission.score != nil {
score += *submission.score
}
}
return score
}
func (c *Course) calcTotalScores() map[string]int {
c.rmu.RLock()
defer c.rmu.RUnlock()
res := make(map[string]int, len(c.registeredStudents))
for userCode := range c.registeredStudents {
res[userCode] = 0
}
for _, class := range c.classes {
for userCode, submission := range class.Submissions() {
if submission != nil && submission.score != nil {
res[userCode] += *submission.score
}
}
}
return res
}
func NewCourseParam() *SearchCourseParam {
return &SearchCourseParam{
Type: "",
Credit: 0,
Teacher: "",
Period: -1, // 0-5, -1で指定なし
DayOfWeek: -1, // 0-4, -1で指定なし
Keywords: []string{},
Status: "",
}
}
func (p *SearchCourseParam) GetParamString() string {
paramStrings := make([]string, 0)
if p.Type != "" {
paramStrings = append(paramStrings, fmt.Sprintf("type = %s", p.Type))
}
if p.Credit != 0 {
paramStrings = append(paramStrings, fmt.Sprintf("credit = %d", p.Credit))
}
if p.Teacher != "" {
paramStrings = append(paramStrings, fmt.Sprintf("teacher = %s", p.Teacher))
}
if p.Period != -1 {
paramStrings = append(paramStrings, fmt.Sprintf("period = %d", p.Period+1))
}
if p.DayOfWeek != -1 {
paramStrings = append(paramStrings, fmt.Sprintf("day_of_week = %s", api.DayOfWeekTable[p.DayOfWeek]))
}
if len(p.Keywords) != 0 {
paramStrings = append(paramStrings, fmt.Sprintf("keywords = %s", strings.Join(p.Keywords, " ")))
}
if p.Status != "" {
paramStrings = append(paramStrings, fmt.Sprintf("status = %s", p.Status))
}
if len(paramStrings) == 0 {
return "指定なし"
} else {
return strings.Join(paramStrings, ", ")
}
}