summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--wsmsg_choose.go (renamed from wsm.go)156
-rw-r--r--wsmsg_hello.go78
-rw-r--r--wsmsg_unchoose.go136
3 files changed, 215 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
-}
diff --git a/wsmsg_hello.go b/wsmsg_hello.go
new file mode 100644
index 0000000..b92dde9
--- /dev/null
+++ b/wsmsg_hello.go
@@ -0,0 +1,78 @@
+/*
+ * Handle the "HELLO" message
+ *
+ * Copyright (C) 2024 Runxi Yu <https://runxiyu.org>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package main
+
+import (
+ "context"
+ "fmt"
+ "strings"
+ "sync/atomic"
+
+ "github.com/coder/websocket"
+ "github.com/jackc/pgx/v5"
+)
+
+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
+}
diff --git a/wsmsg_unchoose.go b/wsmsg_unchoose.go
new file mode 100644
index 0000000..07cb18b
--- /dev/null
+++ b/wsmsg_unchoose.go
@@ -0,0 +1,136 @@
+/*
+ * Handle the "N" message for unchoosing a course
+ *
+ * Copyright (C) 2024 Runxi Yu <https://runxiyu.org>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package main
+
+import (
+ "context"
+ "fmt"
+ "strconv"
+ "sync/atomic"
+
+ "github.com/coder/websocket"
+)
+
+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
+}