-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added rasberry server (use npm run bootpi -- --host)
Added rasberry server (use npm run bootpi -- --host) (#135)
- Loading branch information
1 parent
13c0457
commit ca23689
Showing
4 changed files
with
134 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
} | ||
}) | ||
}) |