Skip to content

Commit

Permalink
Multi movement
Browse files Browse the repository at this point in the history
  • Loading branch information
miraz12 committed Oct 1, 2023
1 parent 42959e0 commit c00d7fc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
17 changes: 14 additions & 3 deletions code/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ wss.on("connection", (ws) => {
// Add client to room
rooms.set(msg.room_id, new Array());
rooms.get(msg.room_id)!.push(ws);
// Send ACK msg to client
// Send CON msg to client
ws.send(
JSON.stringify({
type: "CRE",
type: "CON",
msg: "OK",
})
);
} else {
ws.send(
JSON.stringify({
type: "CRE",
type: "CON",
msg: "ERR",
})
);
Expand All @@ -53,11 +53,22 @@ wss.on("connection", (ws) => {
console.log("Joining room: " + msg.room_id);
rooms.get(msg.room_id)!.push(ws);
clients.get(ws).room = msg.room_id;
ws.send(
JSON.stringify({
type: "CON",
msg: "OK",
})
);
for (const element of rooms.get(msg.room_id)) {
if (element != ws) {
// Add client to all others in room
element.send(
JSON.stringify({ type: "JOI", id: clients.get(ws).id })
);
// Add all others in room to client
ws.send(
JSON.stringify({ type: "JOI", id: clients.get(element).id })
);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions code/src/Engine/Client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Client {
return false;
}

joinRoom(roomId): boolean {
joinRoom(roomId: string): boolean {
this.send(JSON.stringify({ type: "JOI", room_id: roomId }), 100);
return false;
}
Expand All @@ -40,7 +40,7 @@ export class Client {
console.log(message);
const msg = JSON.parse(message);
switch (msg.type) {
case "CRE":
case "CON":
switch (msg.msg) {
case "OK":
this.connected = true;
Expand Down
2 changes: 1 addition & 1 deletion code/src/Game/PlayerCharacter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export default class PlayerCharacter {
}

const client = Game.getInstanceNoSa().client;
if (true) {
if (client.connected) {
client.send(
JSON.stringify({
type: "MOV",
Expand Down

0 comments on commit c00d7fc

Please sign in to comment.