Add Websocket connection and join request to frontend (#6)
This commit is contained in:
1 parent
abf72c8093
commit
89b2554763
4 files changed
+91
-16
No files matched your search
@@ -0,0 +1,72 @@
|
|||||||
|
import * as ex from 'excalibur';
|
||||||
|
import { GameState } from './GameState';
|
||||||
|
|
||||||
|
type Action = "join";
|
||||||
|
|
||||||
|
interface Request {
|
||||||
|
action: Action,
|
||||||
|
}
|
||||||
|
|
||||||
|
interface JoinRequest {
|
||||||
|
action: "join",
|
||||||
|
name: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
type ConnectionEvents = {
|
||||||
|
message: GameMessageEvent;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class GameMessageEvent extends ex.GameEvent<GameState> {
|
||||||
|
constructor(public gameState: GameState) {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ConnectionEvents = {
|
||||||
|
GameMessage: "game-message"
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Connection extends ex.Actor {
|
||||||
|
private _socket: WebSocket | null = null;
|
||||||
|
|
||||||
|
public override events = new ex.EventEmitter<ex.ActorEvents & ConnectionEvents>();
|
||||||
|
|
||||||
|
private get _connected(): boolean {
|
||||||
|
const { _socket } = this;
|
||||||
|
return _socket !== null && !_socket.CLOSING && !_socket?.CLOSED && _socket.OPEN;
|
||||||
|
}
|
||||||
|
|
||||||
|
connect(): void {
|
||||||
|
this._socket = new WebSocket("ws://127.0.0.1:8888");
|
||||||
|
this._socket.onopen = () => console.log("Heureka");
|
||||||
|
this._socket.onmessage = (event) => this._onMessage(event);
|
||||||
|
this._socket.onerror = (event) => console.log(event);
|
||||||
|
this._socket.onclose = (event) => console.log(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
private _onMessage(event: MessageEvent): void {
|
||||||
|
const messageObject = JSON.parse(event.data);
|
||||||
|
const gameState = messageObject.gamestate;
|
||||||
|
|
||||||
|
console.log(messageObject);
|
||||||
|
this.events.emit(ConnectionEvents.GameMessage, new GameMessageEvent(gameState));
|
||||||
|
}
|
||||||
|
|
||||||
|
private _send(request: Request): void {
|
||||||
|
if (!this._connected) {
|
||||||
|
//return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const message = JSON.stringify(request);
|
||||||
|
this._socket!.send(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
join(name: string): void {
|
||||||
|
const joinRequest = {
|
||||||
|
action: "join",
|
||||||
|
name,
|
||||||
|
} as JoinRequest;
|
||||||
|
|
||||||
|
this._send(joinRequest);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,5 +2,5 @@ import * as ex from 'excalibur';
|
|||||||
import { GameState } from '../GameState';
|
import { GameState } from '../GameState';
|
||||||
|
|
||||||
export class GameStateScene extends ex.Scene {
|
export class GameStateScene extends ex.Scene {
|
||||||
public updateFromGameState(_engine: ex.Engine, _gameState: GameState): void { }
|
public updateFromGameState(_gameState: GameState): void { }
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,7 @@ import { TitledListScreenElement } from '../ScreenElements/TitledListScreenEleme
|
|||||||
import { Text } from '../ScreenElements/Text';
|
import { Text } from '../ScreenElements/Text';
|
||||||
import { Button } from '../ScreenElements/Button';
|
import { Button } from '../ScreenElements/Button';
|
||||||
import { PlayerNameInput } from '../GameElements/PlayerNameInput';
|
import { PlayerNameInput } from '../GameElements/PlayerNameInput';
|
||||||
|
import { Connection, GameMessageEvent } from '../Connection';
|
||||||
|
|
||||||
const tableWidth = 400;
|
const tableWidth = 400;
|
||||||
const maxPlayers = 2;
|
const maxPlayers = 2;
|
||||||
@@ -36,6 +37,12 @@ export class LobbyScene extends GameStateScene {
|
|||||||
|
|
||||||
private _screen: "join" | "lobby" = "join";
|
private _screen: "join" | "lobby" = "join";
|
||||||
|
|
||||||
|
constructor(private _connection: Connection) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this._connection.on("game-message", (e: GameMessageEvent) => this.updateFromGameState(e.gameState))
|
||||||
|
}
|
||||||
|
|
||||||
onInitialize(engine: ex.Engine): void {
|
onInitialize(engine: ex.Engine): void {
|
||||||
// Table background
|
// Table background
|
||||||
this._table = new ex.ScreenElement({
|
this._table = new ex.ScreenElement({
|
||||||
@@ -190,7 +197,8 @@ export class LobbyScene extends GameStateScene {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public updateFromGameState(engine: ex.Engine, gameState: GameState): void {
|
public updateFromGameState(gameState: GameState): void {
|
||||||
|
this.setScreen("lobby");
|
||||||
this._playerList.labels = gameState.players;
|
this._playerList.labels = gameState.players;
|
||||||
this._spectatorsList.labels = gameState.spectators;
|
this._spectatorsList.labels = gameState.spectators;
|
||||||
this._updatePositions();
|
this._updatePositions();
|
||||||
@@ -203,8 +211,7 @@ export class LobbyScene extends GameStateScene {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _join(): void {
|
private _join(): void {
|
||||||
this.setScreen("lobby");
|
this._connection.join(this._playerNameInput.playerName);
|
||||||
// Send request
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _joinAsPlayer(): void {
|
private _joinAsPlayer(): void {
|
||||||
|
|||||||
@@ -1,28 +1,24 @@
|
|||||||
import * as ex from 'excalibur';
|
import * as ex from 'excalibur';
|
||||||
import { LobbyScene } from "./Scenes/LobbyScene";
|
import { LobbyScene } from "./Scenes/LobbyScene";
|
||||||
|
import { Connection } from './Connection';
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
|
const connection = new Connection();
|
||||||
|
connection.connect();
|
||||||
|
|
||||||
|
const lobbyScene = new LobbyScene(connection);
|
||||||
|
|
||||||
const game = new ex.Engine({
|
const game = new ex.Engine({
|
||||||
width: 0,
|
width: 0,
|
||||||
height: 0,
|
height: 0,
|
||||||
displayMode: ex.DisplayMode.FillScreen,
|
displayMode: ex.DisplayMode.FillScreen,
|
||||||
scenes: { lobby: LobbyScene},
|
scenes: { lobby: lobbyScene},
|
||||||
backgroundColor: ex.Color.fromHex("23213b"),
|
backgroundColor: ex.Color.fromHex("23213b"),
|
||||||
});
|
});
|
||||||
game.toggleDebug();
|
game.toggleDebug();
|
||||||
|
|
||||||
|
|
||||||
game.start().then(async () => {
|
game.start().then(async () => {
|
||||||
await game.goToScene('lobby');
|
await game.goToScene('lobby');
|
||||||
|
|
||||||
// Test update
|
|
||||||
const gameState = {
|
|
||||||
players: ["LoL"],
|
|
||||||
spectators: [],
|
|
||||||
}
|
|
||||||
|
|
||||||
const lobbyScene = game.currentScene as LobbyScene;
|
|
||||||
setTimeout(() => {
|
|
||||||
lobbyScene.updateFromGameState(game, gameState);
|
|
||||||
}, 2000);
|
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
Reference in new issue
Block a user