Skip to content

Commit

Permalink
Added rasberry server (use npm run bootpi -- --host)
Browse files Browse the repository at this point in the history
Added rasberry server (use npm run bootpi -- --host) (#135)
  • Loading branch information
OfekShaltiel-UTD authored Dec 7, 2024
1 parent 13c0457 commit ca23689
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 3 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"preview": "nuxt preview",
"postinstall": "nuxt prepare",
"bootgame": "tsx server/Game_Manager/src/index.ts",
"bootcontrol": "tsx server/Controller/src/index.ts"
"bootcontrol": "tsx server/Controller/src/index.ts",
"bootpi": "tsx server/Rasberri/src/index.ts"

},
"dependencies": {
"axios": "^1.7.7",
Expand Down
34 changes: 34 additions & 0 deletions prisma/migrations/20241206020508_init_expo/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- CreateTable
CREATE TABLE "Player" (
"user_id" TEXT NOT NULL PRIMARY KEY,
"username" TEXT NOT NULL,
"email" TEXT NOT NULL,
"wins" INTEGER NOT NULL DEFAULT 0,
"goals" INTEGER NOT NULL DEFAULT 0,
"games" INTEGER NOT NULL DEFAULT 0,
"losses" INTEGER NOT NULL DEFAULT 0,
"ratio" REAL
);

-- CreateTable
CREATE TABLE "Match" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"datetime" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- CreateTable
CREATE TABLE "PlayersInMatches" (
"playerID" TEXT NOT NULL,
"playerScore" INTEGER NOT NULL,
"matchID" INTEGER NOT NULL,

PRIMARY KEY ("playerID", "matchID"),
CONSTRAINT "PlayersInMatches_playerID_fkey" FOREIGN KEY ("playerID") REFERENCES "Player" ("user_id") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "PlayersInMatches_matchID_fkey" FOREIGN KEY ("matchID") REFERENCES "Match" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateIndex
CREATE UNIQUE INDEX "Player_username_key" ON "Player"("username");

-- CreateIndex
CREATE UNIQUE INDEX "Player_email_key" ON "Player"("email");
5 changes: 3 additions & 2 deletions server/Game_Manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ let score1: number = 8
let score2: number = 5
enum GAME_STATE { NOT_PLAYING, SEND_CONFIRM, PLAYING, RESETTING }
let game_state: GAME_STATE = GAME_STATE.NOT_PLAYING

let robots_ready: boolean = true
let numPlayers: number = 1

Expand Down Expand Up @@ -164,7 +165,7 @@ const gameCycle = setInterval( async () => {
else if(game_state == GAME_STATE.PLAYING){
// Check when timer reaches 0
console.log(`TIMER: ${timer} | ${players[0]["username"]} vs ${players[1]["username"]}`)
timer--;
//timer--;
if(timer == 0){
game_state = GAME_STATE.RESETTING
}
Expand Down Expand Up @@ -293,7 +294,7 @@ const gameCycle = setInterval( async () => {
}

players.splice(0, 2)
robots_ready = true
robots_ready = false
timer = 0
score1 = score1 - 3
score2 = score2
Expand Down
94 changes: 94 additions & 0 deletions server/Rasberri/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { WebSocketServer } from "ws"
import dotenv from "dotenv"

// Environment variables
dotenv.config({ path: "./.env" })
const LOCALHOST: string = process.env.LOCALHOST ?? "localhost"
const PORT_GM_RASPBERRY: number = parseInt(`${process.env.PORT_GM_RASPBERRY}`)
const PORT_WSS_CONTROLLER_RASPBERRY: number = parseInt(`${process.env.PORT_WSS_CONTROLLER_RASPBERRY}`)

// Shared variables
let ready: boolean = true
let timer: number = 0
let score1: number = 10
let score2: number = 2

// FOR GAME MANAGER
const wss_gm = new WebSocketServer({ port: PORT_GM_RASPBERRY})

wss_gm.on("listening", () => {
console.log(`WebSocket wss_gm is running on ws://${LOCALHOST}:${PORT_GM_RASPBERRY}`)
})

wss_gm.on("error", (error) => {
console.log("WebSocket wss_gm error: " + error)
})

wss_gm.on("close", () => {
console.log("WebSocket wss_gm closed")
})

wss_gm.on("connection", (ws: any, request) => {
ws.on("message", (data: any) => {
const { type, payload } = JSON.parse(data)
if(type === "CHECK_READY"){ // should already be in
console.log(`Telling GM: ready is ${ready}`)
ws.send(JSON.stringify({
"type": "IS_READY",
"payload": ready
}))
}
else if(type === "GAME_START"){
timer = payload["timer"]
const broadcastTimer = setInterval(() => {
if(timer === 0){
clearInterval(broadcastTimer)
clearInterval(broadcastScore)
ws.send(JSON.stringify({
"type": "GAME_END",
"payload": {"timer": timer, "score1": score1, "score2": score2}
}))
}
else{
ws.send(JSON.stringify({
"type": "TIMER_UPDATE",
"payload": {"timer": timer}
}))
timer--
}
}, 1000)
const broadcastScore = setInterval(() => {
ws.send(JSON.stringify({
"type": "SCORE_UPDATE",
"payload": {"score1": score1, "score2": score2}
}))
}, 1000)
}
})
})

// FOR CONTROLLER
const wss_control = new WebSocketServer({ port: PORT_WSS_CONTROLLER_RASPBERRY})

wss_control.on("listening", () => {
console.log(`WebSocket wss_control is running on ws://${LOCALHOST}:${PORT_WSS_CONTROLLER_RASPBERRY}`)
})

wss_control.on("error", (error) => {
console.log("WebSocket wss_control error: " + error)
})

wss_control.on("close", () => {
console.log("WebSocket wss_control closed")
})

wss_control.on("connection", (ws: any, request) => {
ws.on("message", (data: any) => {
const { type, payload } = JSON.parse(data)

if(type === "KEY_INPUT"){ // should already be in
const { keys, playernumber }: {keys: string, playernumber: number} = payload
console.log(`Player ${playernumber} pressed ${keys}`)
}
})
})

0 comments on commit ca23689

Please sign in to comment.