stuff
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
{
|
{
|
||||||
"tabWidth": 4
|
"tabWidth": 4,
|
||||||
|
"printWidth": 170
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,103 +4,7 @@
|
|||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<title>WebSocket MUD</title>
|
<title>WebSocket MUD</title>
|
||||||
<style>
|
<link rel="stylesheet" href="style.css">
|
||||||
body {
|
|
||||||
font-family: "Courier New", monospace;
|
|
||||||
background-color: #1a1a1a;
|
|
||||||
color: #00ff00;
|
|
||||||
margin: 0;
|
|
||||||
padding: 0;
|
|
||||||
height: 100vh;
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
}
|
|
||||||
|
|
||||||
#container {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
height: 100vh;
|
|
||||||
max-width: 1200px;
|
|
||||||
margin: 0 auto;
|
|
||||||
padding: 10px;
|
|
||||||
}
|
|
||||||
|
|
||||||
#output {
|
|
||||||
flex: 1;
|
|
||||||
background-color: #000;
|
|
||||||
border: 2px solid #333;
|
|
||||||
padding: 15px;
|
|
||||||
overflow-y: auto;
|
|
||||||
white-space: pre-wrap;
|
|
||||||
font-size: 14px;
|
|
||||||
line-height: 1.4;
|
|
||||||
margin-bottom: 10px;
|
|
||||||
}
|
|
||||||
|
|
||||||
#input-container {
|
|
||||||
display: flex;
|
|
||||||
gap: 10px;
|
|
||||||
}
|
|
||||||
|
|
||||||
#input {
|
|
||||||
flex: 1;
|
|
||||||
background-color: #222;
|
|
||||||
border: 2px solid #333;
|
|
||||||
color: #00ff00;
|
|
||||||
padding: 10px;
|
|
||||||
font-family: "Courier New", monospace;
|
|
||||||
font-size: 14px;
|
|
||||||
}
|
|
||||||
|
|
||||||
#input:focus {
|
|
||||||
outline: none;
|
|
||||||
border-color: #00ff00;
|
|
||||||
}
|
|
||||||
|
|
||||||
#send {
|
|
||||||
background-color: #333;
|
|
||||||
border: 2px solid #555;
|
|
||||||
color: #00ff00;
|
|
||||||
padding: 10px 20px;
|
|
||||||
font-family: "Courier New", monospace;
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
|
||||||
|
|
||||||
#send:hover {
|
|
||||||
background-color: #444;
|
|
||||||
}
|
|
||||||
|
|
||||||
#status {
|
|
||||||
background-color: #333;
|
|
||||||
padding: 5px 15px;
|
|
||||||
margin-bottom: 10px;
|
|
||||||
border-radius: 3px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.connected {
|
|
||||||
color: #00ff00;
|
|
||||||
}
|
|
||||||
|
|
||||||
.disconnected {
|
|
||||||
color: #ff4444;
|
|
||||||
}
|
|
||||||
|
|
||||||
.connecting {
|
|
||||||
color: #ffaa00;
|
|
||||||
}
|
|
||||||
|
|
||||||
.error {
|
|
||||||
color: #ff4444;
|
|
||||||
}
|
|
||||||
|
|
||||||
.system {
|
|
||||||
color: #aaaaaa;
|
|
||||||
}
|
|
||||||
|
|
||||||
.prompt {
|
|
||||||
color: #00ccff;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="container">
|
<div id="container">
|
||||||
@@ -138,7 +42,7 @@
|
|||||||
this.updateStatus("Connecting...", "connecting");
|
this.updateStatus("Connecting...", "connecting");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.s = new WebSocket(wsUrl);
|
this.ws = new WebSocket(wsUrl);
|
||||||
|
|
||||||
this.ws.onopen = () => {
|
this.ws.onopen = () => {
|
||||||
this.updateStatus("Connected", "connected");
|
this.updateStatus("Connected", "connected");
|
||||||
@@ -148,6 +52,7 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
this.ws.onmessage = (event) => {
|
this.ws.onmessage = (event) => {
|
||||||
|
console.log(event);
|
||||||
const data = JSON.parse(event.data);
|
const data = JSON.parse(event.data);
|
||||||
this.handleMessage(data);
|
this.handleMessage(data);
|
||||||
};
|
};
|
||||||
@@ -169,6 +74,7 @@
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
this.updateStatus("Connection Failed", "error");
|
this.updateStatus("Connection Failed", "error");
|
||||||
setTimeout(() => this.connect(), 3000);
|
setTimeout(() => this.connect(), 3000);
|
||||||
}
|
}
|
||||||
@@ -254,18 +160,16 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
handleMessage(data) {
|
handleMessage(data) {
|
||||||
switch (data.type) {
|
console.log(data);
|
||||||
case "message":
|
switch (data[0]) {
|
||||||
this.appendOutput(data.content);
|
|
||||||
break;
|
|
||||||
case "error":
|
case "error":
|
||||||
this.appendOutput(data.content, "error");
|
this.appendOutput(data[1], "error");
|
||||||
break;
|
break;
|
||||||
case "system":
|
case "system":
|
||||||
this.appendOutput(data.content, "system");
|
this.appendOutput(data[1], "system");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
this.appendOutput(data.content);
|
this.appendOutput(data[1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
95
server/public/style.css
Normal file
95
server/public/style.css
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
body {
|
||||||
|
font-family: "Courier New", monospace;
|
||||||
|
background-color: #1a1a1a;
|
||||||
|
color: #00ff00;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
height: 100vh;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
#container {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
height: 100vh;
|
||||||
|
max-width: 1200px;
|
||||||
|
margin: 0 auto;
|
||||||
|
padding: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#output {
|
||||||
|
flex: 1;
|
||||||
|
background-color: #000;
|
||||||
|
border: 2px solid #333;
|
||||||
|
padding: 15px;
|
||||||
|
overflow-y: auto;
|
||||||
|
white-space: pre-wrap;
|
||||||
|
font-size: 14px;
|
||||||
|
line-height: 1.4;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#input-container {
|
||||||
|
display: flex;
|
||||||
|
gap: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#input {
|
||||||
|
flex: 1;
|
||||||
|
background-color: #222;
|
||||||
|
border: 2px solid #333;
|
||||||
|
color: #00ff00;
|
||||||
|
padding: 10px;
|
||||||
|
font-family: "Courier New", monospace;
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#input:focus {
|
||||||
|
outline: none;
|
||||||
|
border-color: #00ff00;
|
||||||
|
}
|
||||||
|
|
||||||
|
#send {
|
||||||
|
background-color: #333;
|
||||||
|
border: 2px solid #555;
|
||||||
|
color: #00ff00;
|
||||||
|
padding: 10px 20px;
|
||||||
|
font-family: "Courier New", monospace;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
#send:hover {
|
||||||
|
background-color: #444;
|
||||||
|
}
|
||||||
|
|
||||||
|
#status {
|
||||||
|
background-color: #333;
|
||||||
|
padding: 5px 15px;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
border-radius: 3px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.connected {
|
||||||
|
color: #00ff00;
|
||||||
|
}
|
||||||
|
|
||||||
|
.disconnected {
|
||||||
|
color: #ff4444;
|
||||||
|
}
|
||||||
|
|
||||||
|
.connecting {
|
||||||
|
color: #ffaa00;
|
||||||
|
}
|
||||||
|
|
||||||
|
.error {
|
||||||
|
color: #ff4444;
|
||||||
|
}
|
||||||
|
|
||||||
|
.system {
|
||||||
|
color: #aaaaaa;
|
||||||
|
}
|
||||||
|
|
||||||
|
.prompt {
|
||||||
|
color: #00ccff;
|
||||||
|
}
|
||||||
102
server/server.js
102
server/server.js
@@ -104,14 +104,8 @@ class MudServer {
|
|||||||
// We haven"t gotten a username yet, so we expect one.
|
// We haven"t gotten a username yet, so we expect one.
|
||||||
//----------------------------------------------------
|
//----------------------------------------------------
|
||||||
if (!message.hasUsername()) {
|
if (!message.hasUsername()) {
|
||||||
console.error(
|
console.error("User should have sent a “username” message, but sent something else instead");
|
||||||
"User should have sent a “username” message, but sent something else instead",
|
this.send(websocket, MSG_CALAMITY, "I expected you to send me a username, but you sent me something else instead. You bad! Goodbye...");
|
||||||
);
|
|
||||||
this.send(
|
|
||||||
websocket,
|
|
||||||
MSG_CALAMITY,
|
|
||||||
"I expected you to send me a username, but you sent me something else instead. You bad! Goodbye...",
|
|
||||||
);
|
|
||||||
|
|
||||||
// for now, just close the socket.
|
// for now, just close the socket.
|
||||||
websocket.close();
|
websocket.close();
|
||||||
@@ -120,7 +114,9 @@ class MudServer {
|
|||||||
const player = this.game.players.get(message.username);
|
const player = this.game.players.get(message.username);
|
||||||
|
|
||||||
if (!player) {
|
if (!player) {
|
||||||
// player not found - for now, just close the connection - make a better
|
//----------------------------------------------------
|
||||||
|
// Invalid Username.
|
||||||
|
//----------------------------------------------------
|
||||||
console.log("Invalid username sent during login: %s", username);
|
console.log("Invalid username sent during login: %s", username);
|
||||||
this.send(websocket, MSG_ERROR, "Invalid username");
|
this.send(websocket, MSG_ERROR, "Invalid username");
|
||||||
this.send(
|
this.send(
|
||||||
@@ -186,86 +182,6 @@ class MudServer {
|
|||||||
this.showRoom(player);
|
this.showRoom(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param {Player} player
|
|
||||||
* @param {string} input
|
|
||||||
*/
|
|
||||||
processCommand(player, input) {
|
|
||||||
const args = input.toLowerCase().split(" ");
|
|
||||||
const command = args[0];
|
|
||||||
|
|
||||||
switch (command) {
|
|
||||||
case "look":
|
|
||||||
case "l":
|
|
||||||
this.showRoom(player);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "go":
|
|
||||||
case "move":
|
|
||||||
if (args[1]) {
|
|
||||||
this.movePlayer(player, args[1]);
|
|
||||||
} else {
|
|
||||||
player.sendMessage("Go where?");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "north":
|
|
||||||
case "n":
|
|
||||||
this.movePlayer(player, "north");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "south":
|
|
||||||
case "s":
|
|
||||||
this.movePlayer(player, "south");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "east":
|
|
||||||
case "e":
|
|
||||||
this.movePlayer(player, "east");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "west":
|
|
||||||
case "w":
|
|
||||||
this.movePlayer(player, "west");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "say":
|
|
||||||
if (args.length > 1) {
|
|
||||||
const message = args.slice(1).join(" ");
|
|
||||||
this.sayToRoom(player, message);
|
|
||||||
} else {
|
|
||||||
player.sendMessage("Say what?");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "who":
|
|
||||||
this.showOnlinePlayers(player);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "inventory":
|
|
||||||
case "inv":
|
|
||||||
this.showInventory(player);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "help":
|
|
||||||
this.showHelp(player);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "quit":
|
|
||||||
player.sendMessage("Goodbye!");
|
|
||||||
player.websocket.close();
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
player.sendMessage(
|
|
||||||
`Unknown command: ${command}. Type "help" for available commands.`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
player.sendPrompt();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when a websocket connection is closing.
|
* Called when a websocket connection is closing.
|
||||||
*
|
*
|
||||||
@@ -289,7 +205,7 @@ class MudServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create HTTP server for serving the client
|
// Create HTTP server for serving the client
|
||||||
const server = http.createServer((req, res) => {
|
const httpServer = http.createServer((req, res) => {
|
||||||
// let filePath = path.join(__dirname, "public", req.url === "/" ? "index.html" : req.url);
|
// let filePath = path.join(__dirname, "public", req.url === "/" ? "index.html" : req.url);
|
||||||
let filePath = path.join(
|
let filePath = path.join(
|
||||||
"public",
|
"public",
|
||||||
@@ -324,15 +240,17 @@ const server = http.createServer((req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Create WebSocket server
|
// Create WebSocket server
|
||||||
const websocketServer = new WebSocketServer({ server });
|
const websocketServer = new WebSocketServer({ server: httpServer });
|
||||||
const mudServer = new MudServer();
|
const mudServer = new MudServer();
|
||||||
|
|
||||||
websocketServer.on("connection", (ws) => {
|
websocketServer.on("connection", (ws) => {
|
||||||
mudServer.onConnectionEstabished(ws);
|
mudServer.onConnectionEstabished(ws);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// websocketServer.on("connection", mudServer.onConnectionEstabished);
|
||||||
|
|
||||||
const PORT = process.env.PORT || 3000;
|
const PORT = process.env.PORT || 3000;
|
||||||
server.listen(PORT, () => {
|
httpServer.listen(PORT, () => {
|
||||||
console.log(`MUD server running on port ${PORT}`);
|
console.log(`MUD server running on port ${PORT}`);
|
||||||
console.log(`WebSocket server ready for connections`);
|
console.log(`WebSocket server ready for connections`);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user