Skip to content

Commit a1b9147

Browse files
committed
add workspace
1 parent 1ac90b8 commit a1b9147

32 files changed

Lines changed: 9041 additions & 0 deletions

app/workspace/.gitignore

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.*
7+
.yarn/*
8+
!.yarn/patches
9+
!.yarn/plugins
10+
!.yarn/releases
11+
!.yarn/versions
12+
13+
# testing
14+
/coverage
15+
16+
# next.js
17+
/.next/
18+
/out/
19+
20+
# production
21+
/build
22+
23+
# misc
24+
.DS_Store
25+
*.pem
26+
27+
# debug
28+
npm-debug.log*
29+
yarn-debug.log*
30+
yarn-error.log*
31+
.pnpm-debug.log*
32+
33+
# env files (can opt-in for committing if needed)
34+
.env*
35+
36+
# vercel
37+
.vercel
38+
39+
# typescript
40+
*.tsbuildinfo
41+
next-env.d.ts

app/workspace/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app).
2+
3+
## Getting Started
4+
5+
First, run the development server:
6+
7+
```bash
8+
npm run dev
9+
# or
10+
yarn dev
11+
# or
12+
pnpm dev
13+
# or
14+
bun dev
15+
```
16+
17+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
18+
19+
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
20+
21+
This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.
22+
23+
## Learn More
24+
25+
To learn more about Next.js, take a look at the following resources:
26+
27+
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
28+
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
29+
30+
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome!
31+
32+
## Deploy on Vercel
33+
34+
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
35+
36+
Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { type NextRequest, NextResponse } from "next/server"
2+
import dbConnect from "@/lib/mongodb"
3+
import Room from "@/lib/models/Room"
4+
5+
interface Player {
6+
name: string;
7+
uuid: string;
8+
profileImage: string;
9+
}
10+
11+
interface CreateRoomRequest {
12+
room: {
13+
gameSessionUuid: string;
14+
};
15+
players: Player[];
16+
}
17+
18+
export async function POST(request: NextRequest) {
19+
try {
20+
await dbConnect()
21+
22+
const body: CreateRoomRequest = await request.json()
23+
const { room, players } = body
24+
25+
const gameStateId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
26+
27+
const newRoom = new Room({
28+
gameSessionUuid: room.gameSessionUuid,
29+
players: players.map((player: Player) => ({
30+
name: player.name,
31+
uuid: player.uuid,
32+
profileImage: player.profileImage,
33+
ready: false,
34+
winState: "DEFEATED",
35+
})),
36+
currentTurn: players[0].uuid,
37+
})
38+
39+
await newRoom.save()
40+
41+
const baseUrl = process.env.NODE_ENV === "production" ? "https://your-domain.com" : "http://localhost:3000"
42+
43+
const link1 = `${baseUrl}/?gameSessionUuid=${room.gameSessionUuid}&gameStateId=${gameStateId}&uuid=${players[0].uuid}`
44+
const link2 = `${baseUrl}/?gameSessionUuid=${room.gameSessionUuid}&gameStateId=${gameStateId}&uuid=${players[1].uuid}`
45+
46+
return NextResponse.json({
47+
status: true,
48+
message: "success",
49+
payload: {
50+
gameSessionUuid: room.gameSessionUuid,
51+
gameStateId: gameStateId,
52+
createDate: newRoom.createdDate.toISOString(),
53+
link1: link1,
54+
link2: link2,
55+
},
56+
})
57+
} catch (error) {
58+
console.error("Error creating room:", error)
59+
return NextResponse.json(
60+
{
61+
status: false,
62+
message: "Error creating room",
63+
error: error instanceof Error ? error.message : "Unknown error",
64+
},
65+
{ status: 500 },
66+
)
67+
}
68+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { type NextRequest, NextResponse } from "next/server"
2+
import dbConnect from "@/lib/mongodb"
3+
import Room from "@/lib/models/Room"
4+
5+
export async function GET(request: NextRequest) {
6+
try {
7+
await dbConnect()
8+
9+
const { searchParams } = new URL(request.url)
10+
const gameSessionUuid = searchParams.get("gameSessionUuid")
11+
12+
if (!gameSessionUuid) {
13+
return NextResponse.json(
14+
{
15+
status: false,
16+
message: "gameSessionUuid is required",
17+
},
18+
{ status: 400 },
19+
)
20+
}
21+
22+
const room = await Room.findOne({ gameSessionUuid })
23+
24+
if (!room) {
25+
return NextResponse.json(
26+
{
27+
status: false,
28+
message: "Room not found",
29+
},
30+
{ status: 404 },
31+
)
32+
}
33+
34+
return NextResponse.json({
35+
status: true,
36+
message: "success",
37+
payload: room,
38+
})
39+
} catch (error) {
40+
console.error("Error fetching room:", error)
41+
return NextResponse.json(
42+
{
43+
status: false,
44+
message: "Error fetching room",
45+
error: error instanceof Error ? error.message : "Unknown error",
46+
},
47+
{ status: 500 },
48+
)
49+
}
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { type NextRequest, NextResponse } from "next/server"
2+
import dbConnect from "@/lib/mongodb"
3+
import Room from "@/lib/models/Room"
4+
5+
export async function GET(request: NextRequest) {
6+
try {
7+
await dbConnect()
8+
9+
const { searchParams } = new URL(request.url)
10+
const gameSessionUuid = searchParams.get("gameSessionUuid")
11+
12+
if (!gameSessionUuid) {
13+
return NextResponse.json(
14+
{
15+
status: false,
16+
message: "gameSessionUuid is required",
17+
},
18+
{ status: 400 },
19+
)
20+
}
21+
22+
const room = await Room.findOne({ gameSessionUuid })
23+
24+
if (!room) {
25+
return NextResponse.json(
26+
{
27+
status: false,
28+
message: "Room not found",
29+
},
30+
{ status: 404 },
31+
)
32+
}
33+
34+
return NextResponse.json({
35+
status: true,
36+
message: "success",
37+
payload: room,
38+
})
39+
} catch (error) {
40+
console.error("Error fetching room:", error)
41+
return NextResponse.json(
42+
{
43+
status: false,
44+
message: "Error fetching room",
45+
error: error instanceof Error ? error.message : "Unknown error",
46+
},
47+
{ status: 500 },
48+
)
49+
}
50+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { type NextRequest, NextResponse } from "next/server"
2+
import dbConnect from "@/lib/mongodb"
3+
import Room from "@/lib/models/Room"
4+
5+
const GAMEON_BACKEND_URL = process.env.GAMEON_BACKEND_URL || "https://your-backend-url.com"
6+
7+
interface Player {
8+
uuid: string;
9+
name: string;
10+
profileImage: string;
11+
ready: boolean;
12+
winState: string;
13+
}
14+
15+
interface SendWinnerRequest {
16+
gameSessionUuid: string;
17+
winner: string;
18+
}
19+
20+
export async function POST(request: NextRequest) {
21+
try {
22+
await dbConnect()
23+
24+
const body: SendWinnerRequest = await request.json()
25+
const { gameSessionUuid, winner } = body
26+
27+
const room = await Room.findOne({ gameSessionUuid })
28+
29+
if (!room) {
30+
return NextResponse.json(
31+
{
32+
status: false,
33+
message: "Room not found",
34+
},
35+
{ status: 404 },
36+
)
37+
}
38+
39+
room.gameStatus = "FINISHED"
40+
room.winner = winner
41+
room.players = room.players.map((player: Player) => ({
42+
...player,
43+
winState: player.uuid === winner ? "WON" : "DEFEATED",
44+
}))
45+
46+
await room.save()
47+
48+
const payload = {
49+
gameSessionUuid,
50+
gameStatus: room.gameStatus,
51+
players: room.players.map((player: Player) => ({
52+
uuid: player.uuid,
53+
points: player.uuid === winner ? 100 : 0,
54+
userGameSessionStatus: player.winState,
55+
})),
56+
}
57+
58+
try {
59+
const response = await fetch(`${GAMEON_BACKEND_URL}/api/external_game/v1/game_session_finish`, {
60+
method: "POST",
61+
headers: {
62+
"Content-Type": "application/json",
63+
},
64+
body: JSON.stringify(payload),
65+
})
66+
67+
const result = await response.json()
68+
69+
return NextResponse.json({
70+
status: true,
71+
message: "Winner data sent successfully",
72+
externalResponse: result,
73+
})
74+
} catch (externalError) {
75+
console.error("Error sending to external API:", externalError)
76+
return NextResponse.json(
77+
{
78+
status: false,
79+
message: "Error sending winner data to external API",
80+
error: externalError instanceof Error ? externalError.message : "Unknown error",
81+
},
82+
{ status: 500 },
83+
)
84+
}
85+
} catch (error) {
86+
console.error("Error processing winner:", error)
87+
return NextResponse.json(
88+
{
89+
status: false,
90+
message: "Error processing winner",
91+
error: error instanceof Error ? error.message : "Unknown error",
92+
},
93+
{ status: 500 },
94+
)
95+
}
96+
}

0 commit comments

Comments
 (0)