Skip to content

Commit

Permalink
chat message added
Browse files Browse the repository at this point in the history
  • Loading branch information
keshav2010 committed Apr 29, 2024
1 parent 327286d commit 5b6a7d5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
19 changes: 7 additions & 12 deletions gameserver/commands/OnChatBroadcastCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import { Command } from "@colyseus/command";
import { SessionRoom } from "../SessionRoom";
import { CommandPayload } from "./CommandPayloadType";
import { Client } from "colyseus";
import { nanoid } from "nanoid";
import { PacketType } from "../../common/PacketType";
export class OnChatBroadcastCommand extends Command<
SessionRoom,
CommandPayload
Expand All @@ -14,17 +13,13 @@ export class OnChatBroadcastCommand extends Command<
gameManager,
}: CommandPayload<{
sessionId: string;
name: string;
x: number;
y: number;
message: string;
}>) {
const sessionId = client.sessionId;
this.state.addPlayer(
sessionId,
message.name,
message?.x || 0,
message?.y || 0
);
console.log(`[OnChatBroadcastCommand]: Added player. ${message.name}`);
console.log(`[OnChatBroadcastCommand]: Received Chat.`, message);
this.room.broadcast(PacketType.ByServer.NEW_CHAT_MESSAGE, {
sender: sessionId,
message: message.message,
});
}
}
4 changes: 3 additions & 1 deletion gameserver/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { OnSoldierMoveCommand } from "./OnSoldierMoveCommand";
import { OnSoldierAttackCommand } from "./OnSoldierAttackCommand";
import { OnSpawnPointSelectCommand } from "./OnSpawnPointSelectCommand";
import { CommandPayload } from "./CommandPayloadType";
import { OnChatBroadcastCommand } from "./OnChatBroadcastCommand";

export class CommandFactory {
static createCommand(
Expand Down Expand Up @@ -36,7 +37,8 @@ export class CommandFactory {

case PacketType.ByClient.SPAWN_POINT_REQUESTED:
return new OnSpawnPointSelectCommand();

case PacketType.ByClient.CLIENT_SENT_CHAT:
return new OnChatBroadcastCommand();
default:
return null;
}
Expand Down
11 changes: 8 additions & 3 deletions public/NetworkManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Colyseus from "colyseus.js";
import Phaser from "phaser";
import { SessionState } from "../gameserver/schema/SessionState";
import { PacketType } from "../common/PacketType";
const URL = `${window.location.host}`;
export type RoomEventHandlerCallbackType = (
type: "onStateChange" | "onMessage" | "onLeave" | "onError",
Expand All @@ -18,7 +19,7 @@ export class NetworkManager {
phaserGame: Phaser.Game,
phaserRegistry: Phaser.Data.DataManager
) {
this.client = new Colyseus.Client(`wss://${URL}`);
this.client = new Colyseus.Client(`ws://${URL}`);
this.room = null;

this.game = phaserGame;
Expand All @@ -44,7 +45,11 @@ export class NetworkManager {
console.log("init state change", state);
});
this.room.onMessage("*", (type, message) => {
console.log("[room / onMessage] :", { type, message });
if (type === PacketType.ByServer.NEW_CHAT_MESSAGE) {
this.game.scene.getScenes(true).forEach((scene) => {
scene.events.emit(type, message);
});
}
});
this.room.onLeave((code) => {
console.log(`Leaving Room ${this.room?.name}, code: ${code}`);
Expand Down Expand Up @@ -97,7 +102,7 @@ export class NetworkManager {
async hostAndJoinSession(roomName: string) {
try {
console.log(`[hostSession] : room(${roomName}) host requested.`);

await this.disconnectGameServer().catch((err) => {
console.log(err);
});
Expand Down
28 changes: 12 additions & 16 deletions public/scenes/GameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var pointerDownWorldSpace: { x: any; y: any } | null = null;
var cursors;

var networkManager: NetworkManager;
function SendChatMessage() {
const SendChatMessage = () => {
try {
var messageText = $("#chat-message").val();
networkManager.sendEventToServer(PacketType.ByClient.CLIENT_SENT_CHAT, {
Expand All @@ -30,8 +30,8 @@ function SendChatMessage() {
} catch (err) {
console.error(err);
}
}
function addNewChatMessage(msg: string, sender: string) {
};
const addNewChatMessage = (msg: string, sender: string) => {
let msgBlock = `<div>
<div class="d-flex justify-content-between">
<p class="small mb-1">${sender}</p>
Expand All @@ -45,7 +45,7 @@ function addNewChatMessage(msg: string, sender: string) {
</div>
</div>`;
$(".chat-body").append(msgBlock);
}
};
$(() => {
$("#send-chat-btn").on("click", function () {
SendChatMessage();
Expand Down Expand Up @@ -238,9 +238,6 @@ export class GameScene extends BaseScene {
string,
BaseSoldier
>;
const playerSoldiersGameObject = this.data.get(
"playerSoldiersGameObject"
) as Map<PlayerId, soldierIdToPhaserMap>;

if (!selectorGraphics) {
return;
Expand All @@ -267,12 +264,13 @@ export class GameScene extends BaseScene {
} else if (pointer.button === 2) {
//if any soldier selected
if (selectedSoldiersMap.size > 0) {
//If enemy unit in nearby radius, randomly select 1 and send attack signal
let searchAreaSize = 35;
const playerSoldiersGameObject = this.data.get(
"playerSoldiersGameObject"
) as Map<PlayerId, soldierIdToPhaserMap>;
let circle = new Phaser.Geom.Circle(
pointer.worldX,
pointer.worldY,
searchAreaSize * 0.5
16
);
selectorGraphics.strokeCircleShape(circle);

Expand All @@ -286,7 +284,6 @@ export class GameScene extends BaseScene {
(d) => d.playerId !== networkManager.getClientId()
);

console.log(`Enemy Soldiers : ${otherPlayerSoldiers.length}`);
// select atmost 1 target soldier (enemy unit to be attacked)
let targetSoldier = null;
for (let i = 0; i < otherPlayerSoldiers.length; i++) {
Expand Down Expand Up @@ -325,8 +322,6 @@ export class GameScene extends BaseScene {
);
}
}

//this.scene.events.emit(GAMEEVENTS.RIGHT_CLICK, pointer.position);
}
});

Expand Down Expand Up @@ -444,9 +439,10 @@ export class GameScene extends BaseScene {

this.AddSceneEvent(
PacketType.ByServer.NEW_CHAT_MESSAGE,
(data: { message: string; playerId: string }) => {
let { message, playerId } = data;
addNewChatMessage(message, playerId);
(data: { message: string; sender: string }) => {
const { message, sender } = data;
console.log("recived event in scene ", message, sender);
addNewChatMessage(message, sender);
}
);

Expand Down

0 comments on commit 5b6a7d5

Please sign in to comment.