Skip to content

Commit

Permalink
"fixed nav bar layout for now and added rasberry server (use npm run …
Browse files Browse the repository at this point in the history
…bootpi -- --host"
  • Loading branch information
OfekShaltiel-UTD committed Dec 6, 2024
1 parent 4de0f80 commit 50f1826
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 73 deletions.
3 changes: 3 additions & 0 deletions components/BottomNavBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
</div>
<div>
<p class="cursor-pointer hover:text-gray-300 transition font-semibold duration-300 text-white text-xl" @click="openLeaderboard"> Leaderboard </p>
</div>
<div>
<p class="cursor-pointer hover:text-gray-300 transition font-semibold duration-300 text-white text-xl" v-if="isLoggedIn" @click="openChangeUsername"> Change Username </p>
</div>

</div>

<AboutUsOverlay v-if="showAboutUs" @closeAboutUsOverlay="closeAboutUs"></AboutUsOverlay>
Expand Down
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
18 changes: 0 additions & 18 deletions prisma/migrations/20241017205008_player_stats/migration.sql

This file was deleted.

29 changes: 0 additions & 29 deletions prisma/migrations/20241017205926_initial/migration.sql

This file was deleted.

20 changes: 0 additions & 20 deletions prisma/migrations/20241107231241_player_update/migration.sql

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
CREATE TABLE "Player" (
"user_id" TEXT NOT NULL PRIMARY KEY,
"username" TEXT NOT NULL,
"email" 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
Expand Down
8 changes: 4 additions & 4 deletions server/Game_Manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ let score1: number = 10
let score2: number = 2
enum GAME_STATE { NOT_PLAYING, SEND_CONFIRM, PLAYING, RESETTING }
let game_state: GAME_STATE = GAME_STATE.NOT_PLAYING
let robots_ready: boolean = true
let robots_ready: boolean = false

// SECTION: GAME CYCLES
const gameCycle = setInterval( async () => {
Expand Down Expand Up @@ -152,7 +152,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 @@ -260,10 +260,10 @@ const gameCycle = setInterval( async () => {


players.splice(0, 2)
robots_ready = true
robots_ready = false
timer = 0
score1 = 0
score2 = 0
score2 = 3
game_state = GAME_STATE.NOT_PLAYING
}
}, 1000)
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 50f1826

Please sign in to comment.