diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-08-03 22:18:31 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-08-03 22:18:31 +0900 |
| commit | 7a4171d021a709e0af1e0e13830514c67ec97702 (patch) | |
| tree | bdef0421d29b9ee737e869677166d4b3ebdadb0f | |
| parent | f70d6eed9f2c519aed030c9dbda99ed0435991a0 (diff) | |
| download | iosdc-japan-2024-albatross-7a4171d021a709e0af1e0e13830514c67ec97702.tar.gz iosdc-japan-2024-albatross-7a4171d021a709e0af1e0e13830514c67ec97702.tar.zst iosdc-japan-2024-albatross-7a4171d021a709e0af1e0e13830514c67ec97702.zip | |
feat: partially implement submit action
| -rw-r--r-- | backend/api/generated.go | 90 | ||||
| -rw-r--r-- | backend/game/hub.go | 7 | ||||
| -rw-r--r-- | backend/game/message.go | 12 | ||||
| -rw-r--r-- | frontend/app/.server/api/schema.d.ts | 11 | ||||
| -rw-r--r-- | frontend/app/components/GolfPlayApp.client.tsx | 9 | ||||
| -rw-r--r-- | frontend/app/components/GolfPlayApps/GolfPlayAppGaming.tsx | 27 | ||||
| -rw-r--r-- | openapi.yaml | 20 |
7 files changed, 145 insertions, 31 deletions
diff --git a/backend/api/generated.go b/backend/api/generated.go index bb9b624..f7da9ee 100644 --- a/backend/api/generated.go +++ b/backend/api/generated.go @@ -103,6 +103,17 @@ type GamePlayerMessageC2SReady struct { Type string `json:"type"` } +// GamePlayerMessageC2SSubmit defines model for GamePlayerMessageC2SSubmit. +type GamePlayerMessageC2SSubmit struct { + Data GamePlayerMessageC2SSubmitPayload `json:"data"` + Type string `json:"type"` +} + +// GamePlayerMessageC2SSubmitPayload defines model for GamePlayerMessageC2SSubmitPayload. +type GamePlayerMessageC2SSubmitPayload struct { + Code string `json:"code"` +} + // GamePlayerMessageS2C defines model for GamePlayerMessageS2C. type GamePlayerMessageS2C struct { union json.RawMessage @@ -429,6 +440,32 @@ func (t *GamePlayerMessageC2S) MergeGamePlayerMessageC2SCode(v GamePlayerMessage return err } +// AsGamePlayerMessageC2SSubmit returns the union data inside the GamePlayerMessageC2S as a GamePlayerMessageC2SSubmit +func (t GamePlayerMessageC2S) AsGamePlayerMessageC2SSubmit() (GamePlayerMessageC2SSubmit, error) { + var body GamePlayerMessageC2SSubmit + err := json.Unmarshal(t.union, &body) + return body, err +} + +// FromGamePlayerMessageC2SSubmit overwrites any union data inside the GamePlayerMessageC2S as the provided GamePlayerMessageC2SSubmit +func (t *GamePlayerMessageC2S) FromGamePlayerMessageC2SSubmit(v GamePlayerMessageC2SSubmit) error { + b, err := json.Marshal(v) + t.union = b + return err +} + +// MergeGamePlayerMessageC2SSubmit performs a merge with any union data inside the GamePlayerMessageC2S, using the provided GamePlayerMessageC2SSubmit +func (t *GamePlayerMessageC2S) MergeGamePlayerMessageC2SSubmit(v GamePlayerMessageC2SSubmit) error { + b, err := json.Marshal(v) + if err != nil { + return err + } + + merged, err := runtime.JSONMerge(t.union, b) + t.union = merged + return err +} + func (t GamePlayerMessageC2S) MarshalJSON() ([]byte, error) { b, err := t.union.MarshalJSON() return b, err @@ -1549,32 +1586,33 @@ func (sh *strictHandler) GetToken(ctx echo.Context, params GetTokenParams) error // Base64 encoded, gzipped, json marshaled Swagger object var swaggerSpec = []string{ - "H4sIAAAAAAAC/+xZX2/bNhD/Kho3oBugxY4TFJ3f0qzNOnSd0TTYQxEYtHS2mUmkSlJNvELffSCpP6ZF", - "W7SjpunWPAS2yLv78e53vPPpE4pYmjEKVAo0/oQyzHEKErj+tgQcA5/iXC4ZJ/9gSRhVzwlF43IRhYji", - "FNAYnVm7QsThQ044xGgseQ4hEtESUqzE5SpTAkJyQheoKEKUYbmcLnAKUxLXBtTDRn216qGYUAkL4KhQ", - "qjmIjFEB+kDPcfwWPuQgpPoWMSqB6o84yxISaeiDG2FO2ej9gcMcjdH3g8ZZA7MqBi84Z6WpGETESWa8", - "pGwFvDRWhOgl4zMSx0A/v+XGVBGiN0y+ZDmNP7/ZN0wGc22qCNEVr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DI4cXlULW3uQkFp9efKypqSxkyv", + "wt30rVJH6IzpYYGJKzpLplhyJkSgIHKKk+AGpsHZ6A0K0Wfgwoyg+0fHR32FnmVAcUbQEJ0c9Y/6yNxu", + "6BD1tN3eHKcmZHPQh0JFUU8Z38TKnlpzAfJCrwqtK5ktvVG9pOe8simuNu5BBv3+XkN5m2kVfiIhFT6J", + "q85NCHOOl85BrNgSEHvU/5YIGbBZYCSKEJ32j7dBqPbcsy8IlNBJu9DaPYqqKXmaYr5cQSjtF6EV1d6X", + "cqRceMW3o/CGrXLWBds90MGPBI6ge8X8THv7wYKtJE7bJaqbNZsdFyADXAHO8m0UGOWPTgF9yfWSmVHp", + "gdF/qAut7SXZq/FsubHya16fyA2W47zYd+JF43yfNnoiNDZt9ixPkmWQZzGWq9PSb+f+2kX613EqL/UG", + "q4NZp23VJ7QX40u96ikW4wq/VzHWrWNbMTYq9ynGRuLxijFOkhUGFdndDda33uor6q18u6pvDdX/oqFS", + "lEjY3PzozJhwMGHEhHyrl3TV4mRYiBvG442xZvn0eHDi6nHu+COWrshcmr46qOzfhYWS/Q0bs5Nb9Xe0", + "9r99nKSV+FDSakcU3YBKBXXFuL1papd/ATwwxNEcqja3LZl80AueYoX4T8XFnG2xYFz+nJDPEAdYmwsM", + "wKIoin8DAAD//5oSt8+jKgAA", } // GetSwagger returns the content of the embedded swagger specification file diff --git a/backend/game/hub.go b/backend/game/hub.go index c73009b..239f5da 100644 --- a/backend/game/hub.go +++ b/backend/game/hub.go @@ -170,8 +170,13 @@ func (hub *gameHub) run() { }, } } + case *playerMessageC2SSubmit: + // TODO: assert game state is gaming + log.Printf("submit: %v", message.message) + // code := msg.Data.Code + // TODO default: - log.Fatalf("unexpected message type: %T", message.message) + log.Printf("unexpected message type: %T", message.message) } case <-ticker.C: if hub.game.state == gameStateStarting { diff --git a/backend/game/message.go b/backend/game/message.go index 0b413d6..d4007a3 100644 --- a/backend/game/message.go +++ b/backend/game/message.go @@ -14,6 +14,7 @@ const ( playerMessageTypeC2SEntry = "player:c2s:entry" playerMessageTypeC2SReady = "player:c2s:ready" playerMessageTypeC2SCode = "player:c2s:code" + playerMessageTypeC2SSubmit = "player:c2s:submit" ) type playerMessageC2SWithClient struct { @@ -36,6 +37,8 @@ type playerMessageC2SEntry = api.GamePlayerMessageC2SEntry type playerMessageC2SReady = api.GamePlayerMessageC2SReady type playerMessageC2SCode = api.GamePlayerMessageC2SCode type playerMessageC2SCodePayload = api.GamePlayerMessageC2SCodePayload +type playerMessageC2SSubmit = api.GamePlayerMessageC2SSubmit +type playerMessageC2SSubmitPayload = api.GamePlayerMessageC2SSubmitPayload func asPlayerMessageC2S(raw map[string]json.RawMessage) (playerMessageC2S, error) { var typ string @@ -61,6 +64,15 @@ func asPlayerMessageC2S(raw map[string]json.RawMessage) (playerMessageC2S, error Type: playerMessageTypeC2SCode, Data: payload, }, nil + case playerMessageTypeC2SSubmit: + var payload playerMessageC2SSubmitPayload + if err := json.Unmarshal(raw["data"], &payload); err != nil { + return nil, err + } + return &playerMessageC2SSubmit{ + Type: playerMessageTypeC2SSubmit, + Data: payload, + }, nil default: return nil, fmt.Errorf("unknown message type: %s", typ) } diff --git a/frontend/app/.server/api/schema.d.ts b/frontend/app/.server/api/schema.d.ts index 705380d..1c8cead 100644 --- a/frontend/app/.server/api/schema.d.ts +++ b/frontend/app/.server/api/schema.d.ts @@ -201,7 +201,7 @@ export interface components { /** @example 100 */ score: number | null; }; - GamePlayerMessageC2S: components["schemas"]["GamePlayerMessageC2SEntry"] | components["schemas"]["GamePlayerMessageC2SReady"] | components["schemas"]["GamePlayerMessageC2SCode"]; + GamePlayerMessageC2S: components["schemas"]["GamePlayerMessageC2SEntry"] | components["schemas"]["GamePlayerMessageC2SReady"] | components["schemas"]["GamePlayerMessageC2SCode"] | components["schemas"]["GamePlayerMessageC2SSubmit"]; GamePlayerMessageC2SEntry: { /** @constant */ type: "player:c2s:entry"; @@ -219,6 +219,15 @@ export interface components { /** @example print('Hello, world!') */ code: string; }; + GamePlayerMessageC2SSubmit: { + /** @constant */ + type: "player:c2s:submit"; + data: components["schemas"]["GamePlayerMessageC2SSubmitPayload"]; + }; + GamePlayerMessageC2SSubmitPayload: { + /** @example print('Hello, world!') */ + code: string; + }; GameWatcherMessage: components["schemas"]["GameWatcherMessageS2C"]; GameWatcherMessageS2C: components["schemas"]["GameWatcherMessageS2CStart"] | components["schemas"]["GameWatcherMessageS2CCode"] | components["schemas"]["GameWatcherMessageS2CExecResult"]; GameWatcherMessageS2CStart: { diff --git a/frontend/app/components/GolfPlayApp.client.tsx b/frontend/app/components/GolfPlayApp.client.tsx index ace0710..80e7182 100644 --- a/frontend/app/components/GolfPlayApp.client.tsx +++ b/frontend/app/components/GolfPlayApp.client.tsx @@ -81,6 +81,14 @@ export default function GolfPlayApp({ }); }, 1000); + const onCodeSubmit = useDebouncedCallback((code: string) => { + console.log("player:c2s:submit"); + sendJsonMessage({ + type: "player:c2s:submit", + data: { code }, + }); + }, 1000); + if (readyState === ReadyState.UNINSTANTIATED) { throw new Error("WebSocket is not connected"); } @@ -140,6 +148,7 @@ export default function GolfPlayApp({ <GolfPlayAppGaming problem={problem!.description} onCodeChange={onCodeChange} + onCodeSubmit={onCodeSubmit} currentScore={currentScore} /> ); diff --git a/frontend/app/components/GolfPlayApps/GolfPlayAppGaming.tsx b/frontend/app/components/GolfPlayApps/GolfPlayAppGaming.tsx index 75ab18e..9fddb01 100644 --- a/frontend/app/components/GolfPlayApps/GolfPlayAppGaming.tsx +++ b/frontend/app/components/GolfPlayApps/GolfPlayAppGaming.tsx @@ -1,18 +1,30 @@ +import React, { useRef } from "react"; + type Props = { problem: string; onCodeChange: (code: string) => void; + onCodeSubmit: (code: string) => void; currentScore: number | null; }; export default function GolfPlayAppGaming({ problem, onCodeChange, + onCodeSubmit, currentScore, }: Props) { + const textareaRef = useRef<HTMLTextAreaElement>(null); + const handleTextChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => { onCodeChange(e.target.value); }; + const handleSubmitButtonClick = () => { + if (textareaRef.current) { + onCodeSubmit(textareaRef.current.value); + } + }; + return ( <div className="min-h-screen flex"> <div className="mx-auto flex min-h-full flex-grow"> @@ -22,16 +34,25 @@ export default function GolfPlayAppGaming({ <div className="text-gray-700">{problem}</div> </div> <div className="mb-4 mt-auto"> - <div className="font-semibold text-green-500"> - Score: {currentScore == null ? "-" : `${currentScore}`} + <div className="mb-2"> + <div className="font-semibold text-green-500"> + Score: {currentScore == null ? "-" : `${currentScore}`} + </div> </div> + <button + onClick={handleSubmitButtonClick} + className="focus:shadow-outline rounded bg-blue-500 px-4 py-2 font-bold text-white hover:bg-blue-700 focus:outline-none" + > + Submit + </button> </div> </div> <div className="w-1/2 p-4 flex"> <div className="flex-grow"> <textarea - className="h-full w-full rounded-lg border border-gray-300 p-2 focus:outline-none focus:ring-2 focus:ring-blue-500" + ref={textareaRef} onChange={handleTextChange} + className="h-full w-full rounded-lg border border-gray-300 p-2 focus:outline-none focus:ring-2 focus:ring-blue-500" ></textarea> </div> </div> diff --git a/openapi.yaml b/openapi.yaml index 683fadf..d04951d 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -427,6 +427,7 @@ components: - $ref: '#/components/schemas/GamePlayerMessageC2SEntry' - $ref: '#/components/schemas/GamePlayerMessageC2SReady' - $ref: '#/components/schemas/GamePlayerMessageC2SCode' + - $ref: '#/components/schemas/GamePlayerMessageC2SSubmit' GamePlayerMessageC2SEntry: type: object properties: @@ -462,6 +463,25 @@ components: example: "print('Hello, world!')" required: - code + GamePlayerMessageC2SSubmit: + type: object + properties: + type: + type: string + const: "player:c2s:submit" + data: + $ref: '#/components/schemas/GamePlayerMessageC2SSubmitPayload' + required: + - type + - data + GamePlayerMessageC2SSubmitPayload: + type: object + properties: + code: + type: string + example: "print('Hello, world!')" + required: + - code GameWatcherMessage: oneOf: - $ref: '#/components/schemas/GameWatcherMessageS2C' |
