summaryrefslogtreecommitdiff
path: root/client/src/logic/players.ts
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--client/src/logic/players.ts79
1 files changed, 79 insertions, 0 deletions
diff --git a/client/src/logic/players.ts b/client/src/logic/players.ts
new file mode 100644
index 0000000..ebe469f
--- /dev/null
+++ b/client/src/logic/players.ts
@@ -0,0 +1,79 @@
+import { GameState, Input, Key, Rotation } from "../types.js"
+
+const canPlayerJoin = (data: GameState) => {
+
+ // lobby has already started
+ if (data.started) {
+ return false
+ }
+
+ // lobby full
+ if (Object.keys(data.players).length >= 4) {
+ return false
+ }
+
+ return true
+
+}
+
+export const updatePlayers = (data: GameState, input: Input) => {
+
+ let startPressed = false;
+
+ for(const added of input.added || []) {
+
+ if (!canPlayerJoin(data)) {
+ continue
+ }
+
+ console.log("added", added);
+
+ data.input[added] = Key.NOTHING
+
+ data.players[added] ||= {
+ pos: {x: 1, y: 1},
+ inputRotation: Rotation.EAST,
+ moveRotation: Rotation.EAST,
+ moving: false,
+ };
+
+ }
+
+ for(const id in input.players) {
+
+ if(!input.players[id]) {
+ continue;
+ }
+
+ if(id in data.players && input.players[id].name !== undefined) {
+
+ let name = input.players[id].name;
+ name = name.substring(0, 16);
+
+ data.players[id] = {
+ ...data.players[id],
+ name,
+ };
+
+ }
+
+ startPressed ||= input.players[id].start;
+ if (input.players[id].key)
+ data.input[id] = input.players[id].key
+
+ }
+
+ for(const removed of input.removed || []) {
+ console.log("removed", removed);
+ delete data.input[removed];
+ delete data.players[removed];
+
+ let element_id = 'span' + removed
+ let element = document.getElementById(element_id)
+ if (element !== null && element !== undefined) element.remove()
+ }
+
+ return startPressed
+
+}
+