fix: 修复竞态条件、重置逻辑、无匹配终态等关键问题
- 用 Prisma $transaction 实现 atomicUpdateRoom,防止并发写入覆盖 - 新增 POST /api/room/[id]/reset 端点,修复"再来一轮"按钮死循环 - 新增 swipeCounts 字段追踪滑动进度,检测"无人匹配"终态 - 着陆页 handleCreate 增加 res.ok 检查,防止跳转到无效房间 - 匹配或无匹配后停止轮询,减少无效请求
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getRoomData, updateRoomData } from "@/lib/store";
|
||||
import { atomicUpdateRoom } from "@/lib/store";
|
||||
|
||||
export async function POST(
|
||||
req: Request,
|
||||
@@ -8,28 +8,28 @@ export async function POST(
|
||||
const { id } = await params;
|
||||
|
||||
try {
|
||||
const data = await getRoomData(id);
|
||||
const { userId } = await req.json();
|
||||
if (!userId) {
|
||||
return NextResponse.json({ error: "userId required" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (!data) {
|
||||
const updated = await atomicUpdateRoom(id, (data) => {
|
||||
if (!data.users.includes(userId)) {
|
||||
data.users.push(userId);
|
||||
}
|
||||
return data;
|
||||
});
|
||||
|
||||
if (!updated) {
|
||||
return NextResponse.json(
|
||||
{ error: "房间不存在或已过期" },
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
const { userId } = await req.json();
|
||||
if (!userId) {
|
||||
return NextResponse.json({ error: "userId required" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (!data.users.includes(userId)) {
|
||||
data.users.push(userId);
|
||||
await updateRoomData(id, data);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
roomId: id,
|
||||
userCount: data.users.length,
|
||||
userCount: updated.users.length,
|
||||
});
|
||||
} catch (e) {
|
||||
console.error("Failed to join room:", e);
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { atomicUpdateRoom } from "@/lib/store";
|
||||
|
||||
export async function POST(
|
||||
_req: Request,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
const { id } = await params;
|
||||
|
||||
try {
|
||||
const updated = await atomicUpdateRoom(id, (data) => {
|
||||
data.likes = {};
|
||||
data.swipeCounts = {};
|
||||
data.match = null;
|
||||
return data;
|
||||
});
|
||||
|
||||
if (!updated) {
|
||||
return NextResponse.json(
|
||||
{ error: "房间不存在或已过期" },
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch (e) {
|
||||
console.error("Failed to reset room:", e);
|
||||
return NextResponse.json(
|
||||
{ error: "重置失败" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -17,10 +17,17 @@ export async function GET(
|
||||
);
|
||||
}
|
||||
|
||||
const total = data.restaurants.length;
|
||||
const allFinished =
|
||||
data.users.length > 0 &&
|
||||
data.users.every((u) => (data.swipeCounts[u] ?? 0) >= total);
|
||||
const noMatch = allFinished && data.match === null;
|
||||
|
||||
return NextResponse.json({
|
||||
roomId: id,
|
||||
userCount: data.users.length,
|
||||
match: data.match,
|
||||
noMatch,
|
||||
restaurants: data.restaurants,
|
||||
});
|
||||
} catch (e) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getRoomData, updateRoomData } from "@/lib/store";
|
||||
import { atomicUpdateRoom } from "@/lib/store";
|
||||
|
||||
export async function POST(
|
||||
req: Request,
|
||||
@@ -8,15 +8,6 @@ export async function POST(
|
||||
const { id } = await params;
|
||||
|
||||
try {
|
||||
const data = await getRoomData(id);
|
||||
|
||||
if (!data) {
|
||||
return NextResponse.json(
|
||||
{ error: "房间不存在或已过期" },
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
const { userId, restaurantId, action } = await req.json();
|
||||
|
||||
if (!userId || restaurantId == null || !action) {
|
||||
@@ -27,33 +18,39 @@ export async function POST(
|
||||
}
|
||||
|
||||
const rid = String(restaurantId);
|
||||
let dirty = false;
|
||||
|
||||
if (action === "like") {
|
||||
if (!data.likes[rid]) {
|
||||
data.likes[rid] = [];
|
||||
}
|
||||
if (!data.likes[rid].includes(userId)) {
|
||||
data.likes[rid].push(userId);
|
||||
dirty = true;
|
||||
const updated = await atomicUpdateRoom(id, (data) => {
|
||||
if (action === "like") {
|
||||
if (!data.likes[rid]) {
|
||||
data.likes[rid] = [];
|
||||
}
|
||||
if (!data.likes[rid].includes(userId)) {
|
||||
data.likes[rid].push(userId);
|
||||
}
|
||||
|
||||
if (
|
||||
data.users.length > 1 &&
|
||||
data.likes[rid].length === data.users.length
|
||||
) {
|
||||
data.match = rid;
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
data.users.length > 1 &&
|
||||
data.likes[rid].length === data.users.length
|
||||
) {
|
||||
data.match = rid;
|
||||
dirty = true;
|
||||
}
|
||||
}
|
||||
data.swipeCounts[userId] = (data.swipeCounts[userId] ?? 0) + 1;
|
||||
|
||||
if (dirty) {
|
||||
await updateRoomData(id, data);
|
||||
return data;
|
||||
});
|
||||
|
||||
if (!updated) {
|
||||
return NextResponse.json(
|
||||
{ error: "房间不存在或已过期" },
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
match: data.match,
|
||||
likeCount: data.likes[rid]?.length ?? 0,
|
||||
match: updated.match,
|
||||
likeCount: updated.likes[rid]?.length ?? 0,
|
||||
});
|
||||
} catch (e) {
|
||||
console.error("Failed to process swipe:", e);
|
||||
|
||||
@@ -61,7 +61,15 @@ export default function LandingPage() {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(coords),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error("创建房间失败");
|
||||
}
|
||||
|
||||
const { roomId } = await res.json();
|
||||
if (!roomId) {
|
||||
throw new Error("创建房间失败");
|
||||
}
|
||||
|
||||
setLoadingText("正在进入房间...");
|
||||
await joinRoom(roomId);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { useEffect, useState, useCallback } from "react";
|
||||
import { useParams } from "next/navigation";
|
||||
import TopNav from "@/components/TopNav";
|
||||
import SwipeDeck from "@/components/SwipeDeck";
|
||||
@@ -14,7 +14,8 @@ export default function RoomPage() {
|
||||
const [userId, setUserId] = useState("");
|
||||
const [joined, setJoined] = useState(false);
|
||||
|
||||
const { userCount, match, restaurants } = useRoomPolling(roomId);
|
||||
const { userCount, match, noMatch, restaurants, mutate } =
|
||||
useRoomPolling(roomId);
|
||||
|
||||
useEffect(() => {
|
||||
const id = getUserId();
|
||||
@@ -27,6 +28,11 @@ export default function RoomPage() {
|
||||
}).then(() => setJoined(true));
|
||||
}, [roomId]);
|
||||
|
||||
const handleReset = useCallback(async () => {
|
||||
await fetch(`/api/room/${roomId}/reset`, { method: "POST" });
|
||||
await mutate();
|
||||
}, [roomId, mutate]);
|
||||
|
||||
const ready = joined && userId && restaurants.length > 0;
|
||||
|
||||
if (!ready) {
|
||||
@@ -46,6 +52,8 @@ export default function RoomPage() {
|
||||
roomId={roomId}
|
||||
userId={userId}
|
||||
matchedRestaurantId={match}
|
||||
noMatch={noMatch}
|
||||
onReset={handleReset}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user