Skip to content

Commit 1c29782

Browse files
authored
Merge pull request #34 from ut-code/feature/connect-board-to-code
Feature/connect board to code
2 parents f29eebf + 2c4ecca commit 1c29782

File tree

10 files changed

+130
-23
lines changed

10 files changed

+130
-23
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
Warnings:
3+
4+
- Added the required column `codeData` to the `Board` table without a default value. This is not possible if the table is not empty.
5+
- Added the required column `isEdited` to the `Board` table without a default value. This is not possible if the table is not empty.
6+
7+
*/
8+
-- AlterTable
9+
ALTER TABLE "Board" ADD COLUMN "codeData" JSONB NOT NULL,
10+
ADD COLUMN "isEdited" BOOLEAN NOT NULL;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to drop the column `isEdited` on the `Board` table. All the data in the column will be lost.
5+
6+
*/
7+
-- AlterTable
8+
ALTER TABLE "Board" DROP COLUMN "isEdited";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to drop the column `codeData` on the `Board` table. All the data in the column will be lost.
5+
- You are about to drop the column `data` on the `Board` table. All the data in the column will be lost.
6+
- Added the required column `board` to the `Board` table without a default value. This is not possible if the table is not empty.
7+
- Added the required column `code` to the `Board` table without a default value. This is not possible if the table is not empty.
8+
9+
*/
10+
-- AlterTable
11+
ALTER TABLE "Board" DROP COLUMN "codeData",
12+
DROP COLUMN "data",
13+
ADD COLUMN "board" JSONB NOT NULL,
14+
ADD COLUMN "code" JSONB NOT NULL;

prisma/schema.prisma

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ datasource db {
1818
model Board {
1919
id Int @id @default(autoincrement())
2020
createdAt DateTime @default(now())
21-
data Json
21+
board Json
2222
name String
2323
preview Json
24+
code Json
2425
}
2526

2627
model Code {

src/iframe/life-game.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
let timer = "stop";
44
let generationFigure = 0;
55
let isDragging = false;
6-
let dragMode = false; // true: 黒にする, false: 白にする
6+
let dragMode = 0; // 1: 黒にする, 0: 白にする
77
let isPlacingTemplate = false;
88
let patternShape = [];
99
let patternHeight = 0;
@@ -70,7 +70,7 @@ function renderBoard() {
7070
for (let c = 0; c < patternWidth; c++) {
7171
const boardRow = i + r;
7272
const boardCol = j + c;
73-
board[boardRow][boardCol] = patternShape[r][c] === 1;
73+
board[boardRow][boardCol] = patternShape[r][c];
7474
}
7575
}
7676
rerender();
@@ -239,15 +239,15 @@ on.pause = () => {
239239

240240
on.board_reset = () => {
241241
//すべて白にBoardを変更
242-
board = Array.from({ length: boardSize }, () => Array.from({ length: boardSize }, () => false));
242+
board = Array.from({ length: boardSize }, () => Array.from({ length: boardSize }, () => 0));
243243
renderBoard();
244244
generationChange(0);
245245
};
246246

247247
on.board_randomize = () => {
248248
//白黒ランダムにBoardを変更
249249
board = Array.from({ length: boardSize }, () =>
250-
Array.from({ length: boardSize }, () => Math.random() > 0.5),
250+
Array.from({ length: boardSize }, () => (Math.random() > 0.5 ? 1 : 0)),
251251
);
252252
renderBoard();
253253
generationChange(0);

src/lib/api/board.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { toast } from "$lib/models/ToastStore.svelte";
22

3-
export async function saveBoard(data: { board: number[][]; name: string }, isJapanese: boolean) {
3+
export async function saveBoard(
4+
data: { board: number[][]; name: string; code: string },
5+
isJapanese: boolean,
6+
) {
47
try {
58
const response = await fetch("/api/board", {
69
method: "POST",
@@ -61,10 +64,15 @@ export async function fetchBoardList(isJapanese: boolean): Promise<BoardListItem
6164
}
6265
}
6366

67+
export type LoadedBoardData = {
68+
board: number[][];
69+
code: string;
70+
};
71+
6472
export async function loadBoardById(
6573
id: number,
6674
isJapanese: boolean,
67-
): Promise<number[][] | undefined> {
75+
): Promise<LoadedBoardData | undefined> {
6876
try {
6977
const response = await fetch(`/api/board?id=${id}`);
7078

@@ -78,7 +86,7 @@ export async function loadBoardById(
7886

7987
const loadedBoard = await response.json();
8088

81-
return loadedBoard as number[][];
89+
return loadedBoard as LoadedBoardData;
8290
} catch (err) {
8391
console.error("Load error", err);
8492
if (isJapanese) {

src/lib/components/BoardModals.svelte

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@
1010
isJapanese: boolean;
1111
onSelect: (id: number) => void;
1212
} = $props();
13+
14+
let showConfirmation = $state(false);
15+
let selectedBoardId = $state<number | null>(null);
16+
17+
function handleLoadClick(id: number) {
18+
selectedBoardId = id;
19+
showConfirmation = true;
20+
}
21+
22+
function handleConfirmLoad() {
23+
if (selectedBoardId !== null) {
24+
onSelect(selectedBoardId);
25+
}
26+
showConfirmation = false;
27+
selectedBoardId = null;
28+
}
1329
</script>
1430

1531
<dialog class="modal" open={manager.saveState.saving}>
@@ -105,7 +121,7 @@
105121
<td class="text-right">
106122
<button
107123
class="btn btn-sm btn-success text-black"
108-
onclick={() => onSelect(item.id)}
124+
onclick={() => handleLoadClick(item.id)}
109125
>
110126
{isJapanese ? "ロード" : "Load"}
111127
</button>
@@ -125,6 +141,27 @@
125141
</div>
126142
</dialog>
127143

144+
<dialog class="modal modal-middle" open={showConfirmation}>
145+
<div class="modal-box">
146+
<h3 class="font-bold text-lg">
147+
{isJapanese ? "警告" : "Caution"}
148+
</h3>
149+
<p class="py-4">
150+
{isJapanese
151+
? "盤面に加え、コードも上書きされます。よろしいですか?"
152+
: "The current board and **CODE** will be overwritten. Are you sure?"}
153+
</p>
154+
<div class="modal-action">
155+
<button class="btn btn-error" onclick={() => (showConfirmation = false)}>
156+
{isJapanese ? "いいえ" : "No"}
157+
</button>
158+
<button class="btn btn-success text-black" onclick={handleConfirmLoad}>
159+
{isJapanese ? "はい" : "Yes"}
160+
</button>
161+
</div>
162+
</div>
163+
</dialog>
164+
128165
<style>
129166
.board-preview {
130167
display: grid;

src/lib/models/BoardManager.svelte.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
import { createBoardPreview } from "$lib/board-preview";
2-
import { saveBoard, fetchBoardList, loadBoardById, type BoardListItem } from "$lib/api/board";
2+
import {
3+
saveBoard,
4+
fetchBoardList,
5+
loadBoardById,
6+
type BoardListItem,
7+
type LoadedBoardData,
8+
} from "$lib/api/board";
39

410
type SaveState =
511
| { saving: false }
6-
| { saving: true; data: number[][]; name: string; preview: number[][] };
12+
| {
13+
saving: true;
14+
board: number[][];
15+
name: string;
16+
preview: number[][];
17+
code: string;
18+
};
719

820
type LoadState =
921
| { state: "closed" }
@@ -16,9 +28,15 @@ export class BoardManager {
1628

1729
constructor() {}
1830

19-
openSaveModal(board: number[][]) {
31+
openSaveModal(board: number[][], code: string) {
2032
const preview = createBoardPreview(board);
21-
this.saveState = { saving: true, data: board, name: "", preview: preview };
33+
this.saveState = {
34+
saving: true,
35+
board: board,
36+
name: "",
37+
preview: preview,
38+
code: code,
39+
};
2240
}
2341

2442
closeSaveModal() {
@@ -28,7 +46,14 @@ export class BoardManager {
2846
async save(isJapanese: boolean) {
2947
if (!this.saveState.saving) return;
3048
const name = this.saveState.name.trim() === "" ? "Unnamed Board" : this.saveState.name.trim();
31-
await saveBoard({ board: this.saveState.data, name }, isJapanese);
49+
await saveBoard(
50+
{
51+
board: this.saveState.board,
52+
name: name,
53+
code: this.saveState.code,
54+
},
55+
isJapanese,
56+
);
3257
this.closeSaveModal();
3358
}
3459

@@ -46,7 +71,7 @@ export class BoardManager {
4671
this.loadState = { state: "closed" };
4772
}
4873

49-
async load(id: number, isJapanese: boolean): Promise<number[][] | undefined> {
74+
async load(id: number, isJapanese: boolean): Promise<LoadedBoardData | undefined> {
5075
this.closeLoadModal();
5176
return await loadBoardById(id, isJapanese);
5277
}

src/routes/+page.svelte

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
break;
8989
}
9090
case "save_board": {
91-
boardManager.openSaveModal(event.data.data as number[][]);
91+
boardManager.openSaveModal(event.data.data as number[][], appliedCode as string);
9292
break;
9393
}
9494
default: {
@@ -110,9 +110,11 @@
110110
}
111111
112112
async function onBoardSelect(id: number) {
113-
const board = await boardManager.load(id, isJapanese);
114-
if (board) {
115-
sendEvent("apply_board", board);
113+
const data = await boardManager.load(id, isJapanese);
114+
if (data) {
115+
editingCode = data.code;
116+
appliedCode = data.code;
117+
sendEvent("apply_board", data.board);
116118
}
117119
}
118120

src/routes/api/board/+server.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as v from "valibot";
66
const BoardSchema = v.object({
77
board: v.array(v.array(v.number())),
88
name: v.pipe(v.string(), v.minLength(1, "盤面名は必須です。")),
9+
code: v.string(),
910
});
1011

1112
export async function POST({ request }) {
@@ -23,14 +24,15 @@ export async function POST({ request }) {
2324
return json({ message: "無効なリクエストデータです。" }, { status: 400 });
2425
}
2526

26-
const { board, name } = result.output;
27+
const { board, name, code } = result.output;
2728
const preview = createBoardPreview(board);
2829

2930
const newState = await prisma.board.create({
3031
data: {
31-
data: board,
32+
board: board,
3233
name: name,
3334
preview: preview,
35+
code: code,
3436
},
3537
});
3638

@@ -49,14 +51,14 @@ export async function GET({ url }) {
4951

5052
const state = await prisma.board.findUnique({
5153
where: { id: id },
52-
select: { data: true },
54+
select: { board: true, code: true },
5355
});
5456

5557
if (!state) {
5658
return json({ message: `ID: ${id} の盤面は見つかりません。` }, { status: 404 });
5759
}
5860

59-
return json(state.data);
61+
return json(state);
6062
} else {
6163
//IDが指定されなかった場合、全ての盤面のリストを返す
6264
const allStates = await prisma.board.findMany({

0 commit comments

Comments
 (0)