summaryrefslogtreecommitdiff
path: root/wsmsg_choose.go
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--wsmsg_choose.go (renamed from wsm.go)156
1 files changed, 1 insertions, 155 deletions
diff --git a/wsm.go b/wsmsg_choose.go
index 01cf36a..92f6453 100644
--- a/wsm.go
+++ b/wsmsg_choose.go
@@ -1,5 +1,5 @@
/*
- * WebSocket message handlers
+ * Handle the "Y" message for choosing a course
*
* Copyright (C) 2024 Runxi Yu <https://runxiyu.org>
* SPDX-License-Identifier: AGPL-3.0-or-later
@@ -25,7 +25,6 @@ import (
"errors"
"fmt"
"strconv"
- "strings"
"sync/atomic"
"time"
@@ -34,53 +33,6 @@ import (
"github.com/jackc/pgx/v5/pgconn"
)
-func messageHello(
- ctx context.Context,
- c *websocket.Conn,
- reportError reportErrorT,
- mar []string,
- userID string,
- session string,
-) error {
- _, _ = mar, session
-
- select {
- case <-ctx.Done():
- return fmt.Errorf(
- "%w: %w",
- errContextCancelled,
- ctx.Err(),
- )
- default:
- }
-
- rows, err := db.Query(
- ctx,
- "SELECT courseid FROM choices WHERE userid = $1",
- userID,
- )
- if err != nil {
- return reportError("error fetching choices")
- }
- courseIDs, err := pgx.CollectRows(rows, pgx.RowTo[string])
- if err != nil {
- return reportError("error collecting choices")
- }
-
- if atomic.LoadUint32(&state) == 2 {
- err = writeText(ctx, c, "START")
- if err != nil {
- return fmt.Errorf("%w: %w", errCannotSend, err)
- }
- }
- err = writeText(ctx, c, "HI :"+strings.Join(courseIDs, ","))
- if err != nil {
- return fmt.Errorf("%w: %w", errCannotSend, err)
- }
-
- return nil
-}
-
func messageChooseCourse(
ctx context.Context,
c *websocket.Conn,
@@ -269,109 +221,3 @@ func messageChooseCourse(
}
return nil
}
-
-func messageUnchooseCourse(
- ctx context.Context,
- c *websocket.Conn,
- reportError reportErrorT,
- mar []string,
- userID string,
- session string,
- userCourseGroups *userCourseGroupsT,
-) error {
- _ = session
-
- if atomic.LoadUint32(&state) != 2 {
- err := writeText(ctx, c, "E :Course selections are not open")
- if err != nil {
- return fmt.Errorf(
- "%w: %w",
- errCannotSend,
- err,
- )
- }
- return nil
- }
-
- select {
- case <-ctx.Done():
- return fmt.Errorf(
- "%w: %w",
- errContextCancelled,
- ctx.Err(),
- )
- default:
- }
-
- if len(mar) != 2 {
- return reportError("Invalid number of arguments for N")
- }
- _courseID, err := strconv.ParseInt(mar[1], 10, strconv.IntSize)
- if err != nil {
- return reportError("Course ID must be an integer")
- }
- courseID := int(_courseID)
-
- _course, ok := courses.Load(courseID)
- if !ok {
- return reportError("no such course")
- }
- course, ok := _course.(*courseT)
- if !ok {
- panic("courses map has non-\"*courseT\" items")
- }
- if course == nil {
- return reportError("couse is nil")
- }
-
- ct, err := db.Exec(
- ctx,
- "DELETE FROM choices WHERE userid = $1 AND courseid = $2",
- userID,
- courseID,
- )
- if err != nil {
- return reportError(
- "Database error while deleting course choice",
- )
- }
-
- if ct.RowsAffected() != 0 {
- err := course.decrementSelectedAndPropagate(ctx, c)
- if err != nil {
- return fmt.Errorf(
- "%w: %w",
- errCannotSend,
- err,
- )
- }
-
- _course, ok := courses.Load(courseID)
- if !ok {
- return reportError("no such course")
- }
- course, ok := _course.(*courseT)
- if !ok {
- panic("courses map has non-\"*courseT\" items")
- }
- if course == nil {
- return reportError("couse is nil")
- }
-
- if _, ok := (*userCourseGroups)[course.Group]; !ok {
- return reportError("inconsistent user course groups")
- }
- delete(*userCourseGroups, course.Group)
- }
-
- err = writeText(ctx, c, "N "+mar[1])
- if err != nil {
- return fmt.Errorf(
- "%w: %w",
- errCannotSend,
- err,
- )
- }
-
- return nil
-}