-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwsmsg_choose.go
More file actions
256 lines (237 loc) · 5.05 KB
/
wsmsg_choose.go
File metadata and controls
256 lines (237 loc) · 5.05 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
/*
* Handle the "Y" message for choosing a course
*
* Copyright (C) 2024 Runxi Yu <https://runxiyu.org>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package main
import (
"context"
"errors"
"log/slog"
"strconv"
"sync/atomic"
"time"
"github.com/coder/websocket"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
func messageChooseCourse(
ctx context.Context,
c *websocket.Conn,
mar []string,
userID string,
yeargroup string,
legalSex string,
userCourseGroups *userCourseGroupsT,
userCourseTypes *userCourseTypesT,
) error {
_state, ok := states[yeargroup]
if !ok {
return errNoSuchYearGroup
}
if atomic.LoadUint32(_state) != 2 {
err := writeText(ctx, c, "E :Course selections are not open")
if err != nil {
return wrapError(
errCannotSend,
err,
)
}
return nil
}
select {
case <-ctx.Done():
return wrapError(
errWsHandlerContextCanceled,
ctx.Err(),
)
default:
}
if len(mar) != 2 {
return errBadNumberOfArguments
}
_courseID, err := strconv.ParseInt(mar[1], 10, strconv.IntSize)
if err != nil {
return errNoSuchCourse
}
courseID := int(_courseID)
_course, ok := courses.Load(courseID)
if !ok {
return errNoSuchCourse
}
course, ok := _course.(*courseT)
if !ok {
return errType
}
if course == nil {
return errNoSuchCourse
}
if course.LegalSexReq != "" && course.LegalSexReq != legalSex {
err := writeText(ctx, c, "R "+mar[1]+" :Unmatching legal sex")
if err != nil {
return wrapError(
errCannotSend,
err,
)
}
return nil
}
if course.Forced {
err := writeText(ctx, c, "R "+mar[1]+" :Cannot manually select")
if err != nil {
return wrapError(
errCannotSend,
err,
)
}
return nil
}
if course.YearGroups&yearGroupsNumberBits[yeargroup] == 0 {
return errNotForYourYearGroup
}
if _, ok := (*userCourseGroups)[course.Group]; ok {
err := writeText(ctx, c, "R "+mar[1]+" :Group conflict")
if err != nil {
return wrapError(
errCannotSend,
err,
)
}
return nil
}
// oppositeGroup := ""
// for _, r := range course.Group {
// switch r {
// case '2':
// oppositeGroup += "3"
// case '3':
// oppositeGroup += "2"
// default:
// oppositeGroup += string(r)
// }
// }
// if _, ok := (*userCourseGroups)[oppositeGroup]; ok {
// err := writeText(ctx, c, "R "+mar[1]+" :You cannot choose both CCA2 and CCA3 on the same day")
// if err != nil {
// return wrapError(
// errCannotSend,
// err,
// )
// }
// return nil
// }
err = func() (returnedError error) {
tx, err := db.Begin(ctx)
if err != nil {
return wrapError(errors.New("unexpected database error 34"), err)
}
defer func() {
err := tx.Rollback(ctx)
if err != nil && (!errors.Is(err, pgx.ErrTxClosed)) {
returnedError = wrapError(errors.New("unexpected database error 35"), err)
return
}
}()
_, err = tx.Exec(
ctx,
"INSERT INTO choices (seltime, userid, courseid, forced) VALUES ($1, $2, $3, false)",
time.Now().UnixMicro(),
userID,
courseID,
)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) &&
pgErr.Code == pgErrUniqueViolation {
err2 := writeText(ctx, c, "Y "+mar[1])
if err2 != nil {
return wrapError(
err2,
wrapError(errors.New("unexpected database error 36"), err),
)
}
return nil
}
return wrapError(errors.New("unexpected database error 37"), err)
}
ok := func() bool {
course.SelectedLock.Lock()
defer course.SelectedLock.Unlock()
/*
* The read here doesn't have to be atomic because the
* lock guarantees that no other goroutine is writing to
* it.
*/
if course.Selected < course.Max {
atomic.AddUint32(&course.Selected, 1)
/*
* This write must be atomic because there
* could be other atomic readers.
*/
return true
}
return false
}()
if ok {
go func() {
defer func() {
if e := recover(); e != nil {
slog.Error("panic", "arg", e)
}
}()
propagateSelectedUpdate(course)
}()
err := tx.Commit(ctx)
if err != nil {
err := course.decrementSelectedAndPropagate(ctx, c)
if err != nil {
return wrapError(
errCannotSend,
err,
)
}
return wrapError(errors.New("unexpected database error 38"), err)
}
/*
* This would race if message handlers could run
* concurrently for one connection.
*/
(*userCourseGroups)[course.Group] = struct{}{}
(*userCourseTypes)[course.Type]++
err = writeText(ctx, c, "Y "+mar[1])
if err != nil {
return wrapError(
errCannotSend,
err,
)
}
if config.Perf.PropagateImmediate {
err = sendSelectedUpdate(ctx, c, courseID)
if err != nil {
return wrapError(
errCannotSend,
err,
)
}
}
} else {
err := tx.Rollback(ctx)
if err != nil {
return wrapError(errors.New("unexpected database error 39"), err)
}
err = writeText(ctx, c, "R "+mar[1]+" :Full")
if err != nil {
return wrapError(
errCannotSend,
err,
)
}
}
return nil
}()
if err != nil {
return err
}
return nil
}