aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/game/hub.go
blob: 3697b4a13f23215d0ae6c643c5a063c275cd10c0 (plain)
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
package game

import (
	"context"
	"log"
	"regexp"
	"strings"

	"github.com/nsfisis/iosdc-japan-2025-albatross/backend/db"
	"github.com/nsfisis/iosdc-japan-2025-albatross/backend/taskqueue"
)

type Hub struct {
	q          *db.Queries
	ctx        context.Context
	taskQueue  *taskqueue.Queue
	taskWorker *taskqueue.WorkerServer
}

func NewGameHub(q *db.Queries, taskQueue *taskqueue.Queue, taskWorker *taskqueue.WorkerServer) *Hub {
	return &Hub{
		q:          q,
		ctx:        context.Background(),
		taskQueue:  taskQueue,
		taskWorker: taskWorker,
	}
}

func (hub *Hub) Run() {
	go func() {
		if err := hub.taskWorker.Run(); err != nil {
			log.Fatal(err)
		}
	}()

	go hub.processTaskResults()
}

func (hub *Hub) CalcCodeSize(code string) int {
	re := regexp.MustCompile(`\s+`)
	return len(strings.TrimSuffix(strings.TrimPrefix(strings.TrimPrefix(re.ReplaceAllString(code, ""), "<?php"), "<?"), "?>"))
}

func (hub *Hub) EnqueueTestTasks(ctx context.Context, submissionID, gameID, userID int, code string) error {
	rows, err := hub.q.ListTestcasesByGameID(ctx, int32(gameID))
	if err != nil {
		return err
	}
	for _, row := range rows {
		err := hub.taskQueue.EnqueueTaskRunTestcase(
			gameID,
			userID,
			submissionID,
			int(row.TestcaseID),
			code,
			row.Stdin,
			row.Stdout,
		)
		if err != nil {
			return err
		}
	}
	return nil
}

func (hub *Hub) processTaskResults() {
	for taskResult := range hub.taskWorker.Results() {
		switch taskResult := taskResult.(type) {
		case *taskqueue.TaskResultRunTestcase:
			// TODO: error handling
			_ = hub.processTaskResultRunTestcase(taskResult)
			aggregatedStatus, _ := hub.q.AggregateTestcaseResults(hub.ctx, int32(taskResult.TaskPayload.SubmissionID))
			if aggregatedStatus == "running" {
				continue
			}

			// TODO: error handling
			// TODO: transaction
			_ = hub.q.UpdateSubmissionStatus(hub.ctx, db.UpdateSubmissionStatusParams{
				SubmissionID: int32(taskResult.TaskPayload.SubmissionID),
				Status:       aggregatedStatus,
			})
			_ = hub.q.UpdateGameStateStatus(hub.ctx, db.UpdateGameStateStatusParams{
				GameID: int32(taskResult.TaskPayload.GameID),
				UserID: int32(taskResult.TaskPayload.UserID),
				Status: aggregatedStatus,
			})
			if aggregatedStatus != "success" {
				continue
			}
			_ = hub.q.SyncGameStateBestScoreSubmission(hub.ctx, db.SyncGameStateBestScoreSubmissionParams{
				GameID: int32(taskResult.TaskPayload.GameID),
				UserID: int32(taskResult.TaskPayload.UserID),
			})
		default:
			panic("unexpected task result type")
		}
	}
}

func (hub *Hub) processTaskResultRunTestcase(
	taskResult *taskqueue.TaskResultRunTestcase,
) error {
	if taskResult.Err != nil {
		return taskResult.Err
	}

	if taskResult.Status != "success" {
		if err := hub.q.CreateTestcaseResult(hub.ctx, db.CreateTestcaseResultParams{
			SubmissionID: int32(taskResult.TaskPayload.SubmissionID),
			TestcaseID:   int32(taskResult.TaskPayload.TestcaseID),
			Status:       taskResult.Status,
			Stdout:       taskResult.Stdout,
			Stderr:       taskResult.Stderr,
		}); err != nil {
			return err
		}
		return nil
	}

	var status string
	if isTestcaseResultCorrect(taskResult.TaskPayload.Stdout, taskResult.Stdout) {
		status = "success"
	} else {
		status = "wrong_answer"
	}
	if err := hub.q.CreateTestcaseResult(hub.ctx, db.CreateTestcaseResultParams{
		SubmissionID: int32(taskResult.TaskPayload.SubmissionID),
		TestcaseID:   int32(taskResult.TaskPayload.TestcaseID),
		Status:       status,
		Stdout:       taskResult.Stdout,
		Stderr:       taskResult.Stderr,
	}); err != nil {
		return err
	}
	return nil
}

func isTestcaseResultCorrect(expectedStdout, actualStdout string) bool {
	expectedStdout = strings.TrimSpace(expectedStdout)
	actualStdout = strings.TrimSpace(actualStdout)
	return actualStdout == expectedStdout
}