improve chat

This commit is contained in:
Asraelite 2016-03-28 13:16:38 +01:00
parent 2ae3d43354
commit 61ae57a850
14 changed files with 306 additions and 33 deletions

View file

@ -17,6 +17,7 @@
"dependencies": { "dependencies": {
"box2d-html5": "^0.1.230", "box2d-html5": "^0.1.230",
"colors": "^1.1.2", "colors": "^1.1.2",
"escape-html": "^1.0.3",
"express": "^4.13.4", "express": "^4.13.4",
"jade": "^1.11.0", "jade": "^1.11.0",
"poly-decomp": "git://github.com/schteppe/poly-decomp.js.git", "poly-decomp": "git://github.com/schteppe/poly-decomp.js.git",

View file

@ -35,6 +35,15 @@ body {
margin-top: 5px; margin-top: 5px;
} }
#gui #chat b {
font-weight: 100;
color: #8af;
}
#gui #chat span.server {
color: #68c;
}
#gui #chat input { #gui #chat input {
font-family: inherit; font-family: inherit;
font-size: 16px; font-size: 16px;

View file

@ -15,20 +15,28 @@ GUI.prototype.Chat = class {
addMessage(messageData) { addMessage(messageData) {
let message = { let message = {
type: messageData.type, type: messageData.type,
player: messageData.player, source: messageData.source || '?',
text: messageData.message, text: messageData.message,
createTime: Date.now() createTime: Date.now()
}; };
let span = this.gui.createElement(this.messageElement, 'span', { let span;
html: message.text
}); if (message.type == 'server') {
span = this.gui.createElement(this.messageElement, 'span', {
html: `${message.text}`
});
span.classList.add('server');
} else {
span = this.gui.createElement(this.messageElement, 'span', {
html: `<${message.source.bold()}> ${message.text}`
});
}
this.messages.push(message);
setTimeout(_ => { setTimeout(_ => {
this.messageElement.removeChild(span); //this.messageElement.removeChild(span);
this.messages.shift();
}, 15000); }, 15000);
} }

View file

@ -19,6 +19,15 @@ class GUI {
return result.length == 1 ? result[0] : result; return result.length == 1 ? result[0] : result;
} }
format() {
let args = Array.from(arguments);
let text = args.shift();
let element = args.shift();
let string = `<${element} class="${args.join(' ')}">`;
string += `${text}</${element}>`;
return string;
}
tick() { tick() {
this.chat.tick(); this.chat.tick();
} }

View file

@ -13,7 +13,7 @@ class Renderer {
this.effects = new Set(); this.effects = new Set();
pallet.fillScreen(); pallet.fillScreen();
window.addEventListener('resize', pallet.fillScreen); window.addEventListener('resize', _ => pallet.fillScreen(1000, 600));
} }
render(state) { render(state) {

View file

@ -28,7 +28,7 @@ class Physics {
bodyDef.angularVelocity = 0; bodyDef.angularVelocity = 0;
bodyDef.bullet = body.type == 'missile'; bodyDef.bullet = body.type == 'missile';
bodyDef.linearDamping = body.bodyType == 'asteroid' ? 0.003 : 0.01; bodyDef.linearDamping = body.bodyType == 'asteroid' ? 0.003 : 0.01;
bodyDef.angularDamping = body.bodyType == 'asteroid' ? 0.003 : 0.04; bodyDef.angularDamping = body.bodyType == 'asteroid' ? 0.003 : 0.06;
bodyDef.type = body.bodyType == 'structure' ? bodyDef.type = body.bodyType == 'structure' ?
b2Body.b2_staticBody : b2Body.b2_dynamicBody; b2Body.b2_staticBody : b2Body.b2_dynamicBody;
bodyDef.allowSleep = false; bodyDef.allowSleep = false;

View file

@ -16,7 +16,7 @@ class GameServer {
})[0]; })[0];
if (!room || room.full) { if (!room || room.full) {
room = new Room(); room = new Room(this);
this.rooms.set(room.name, room); this.rooms.set(room.name, room);
} }

204
server/game/names.json Normal file
View file

@ -0,0 +1,204 @@
{
"fruit": [
"Apple",
"Apricot",
"Avocado",
"Banana",
"Bilberry",
"Blackberry",
"Blackcurrant",
"Blueberry",
"Boysenberry",
"Cantaloupe",
"Currant",
"Cherry",
"Cherimoya",
"Cloudberry",
"Coconut",
"Cranberry",
"Damson",
"Date",
"Dragonfruit",
"Durian",
"Elderberry",
"Feijoa",
"Fig",
"Goji Berry",
"Gooseberry",
"Grape",
"Raisin",
"Grapefruit",
"Guava",
"Huckleberry",
"Jabuticaba",
"Jackfruit",
"Jambul",
"Jujube",
"Juniper berry",
"Kiwifruit",
"Kumquat",
"Lemon",
"Lime",
"Loquat",
"Lychee",
"Mango",
"Marionberry",
"Melon",
"Honeydew",
"Watermelon",
"Miracle Fruit",
"Mulberry",
"Nectarine",
"Nance",
"Olive",
"Orange",
"Blood Orange",
"Clementine",
"Mandarine",
"Tangerine",
"Papaya",
"Passionfruit",
"Peach",
"Pear",
"Persimmon",
"Physalis",
"Plantain",
"Plum",
"Prune",
"Pineapple",
"Pomegranate",
"Pomelo",
"Quince",
"Raspberry",
"Salmonberry",
"Rambutan",
"Redcurrant",
"Salal Berry",
"Salak",
"Satsuma",
"Star Fruit",
"Strawberry",
"Tamarillo",
"Tamarind",
"Ugli Fruit"
],
"adjectives": {
"a": [
"Awesome",
"Amazing",
"Awkward"
],
"b": [
"Bubbly",
"Brilliant",
"Bizzare",
"Bouncy",
"Blue"
],
"c": [
"Crazy",
"Cool"
],
"d": [
"Dizzy",
"Dangerous"
],
"e": [
"Energetic",
"Easy"
],
"f": [
"Funny",
"Fancy"
],
"g": [
"Giggly",
"Great",
"Golden"
],
"h": [
"Hipster",
"Hot",
"Holy"
],
"i": [
"Interesting",
"Insipid",
"Introverted"
],
"j": [
"Jolly"
],
"k": [
"Crazy",
"Cool"
],
"l": [
"Little",
"Lazy"
],
"m": [
"Magnificent",
"Minty",
"Magical"
],
"n": [
"Nosy",
"Noisy"
],
"o": [
"Oblivious",
"Omnipotent",
"Omnipresent",
"Okay"
],
"p": [
"Pretty",
"Puny",
"Precious",
"Perfect",
"Pitiful",
"Paranoid",
"Pink"
],
"q": [
"Quivering",
"Queer"
],
"r": [
"Radical",
"Racist"
],
"s": [
"Stupid",
"Silly",
"Smart"
],
"t": [
"Terrific",
"Terrible",
"Tiny"
],
"u": [
"Unbelievable",
"Ugly"
],
"v": [
"Voluptuous",
"Vague"
],
"w": [
"Wiggly",
"Weird"
],
"x": [
"Xenophobic"
],
"y": [
"Yellow"
],
"z": [
"Zany"
]
}
}

View file

@ -15,7 +15,7 @@ class Connection {
this.chatCooldown = 0; this.chatCooldown = 0;
socket.on('chat', data => { socket.on('chat', data => {
this.chat(data); this.player.chat(data);
}); });
socket.on('setName', data => { socket.on('setName', data => {
@ -33,20 +33,6 @@ class Connection {
this.server.assignRoom(this.player); this.server.assignRoom(this.player);
} }
chat(data) {
//if(this.chatCooldown > 5 || !this.room) return;
if(!data.msg) return;
wingbase.log(`${this.room}/${this.player.name}: ${data.msg}`);
this.chatCooldown++;
this.io.to(this.room).emit('chat', {
type: 'player',
source: this.player.name,
message: data.msg.slice(0, 100)
});
}
disconnect() { disconnect() {
this.socket.disconnect(); this.socket.disconnect();
this.player.disconnect(); this.player.disconnect();

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const fruit = ['Apple', 'Banana', 'Pear', 'Plum', 'Pineapple', 'Peach', 'Apricot', 'Orange', 'Triangle', 'Kiwi', 'Mango', 'Strawberry', 'Lemon', 'Blueberry', 'Raspberry', 'Grape', 'Dragonfruit', 'Watermelon', 'Honeymelon', 'Pomegranate', 'Cherry', 'Avocado']; const names = require('./names');
class Player { class Player {
constructor(connection) { constructor(connection) {
@ -10,8 +10,10 @@ class Player {
this.kickCount = 0; this.kickCount = 0;
this.lastAction = Date.now(); this.lastAction = Date.now();
this.connection = connection; this.connection = connection;
this.name = `Stupid ${fruit[Math.random() * fruit.length | 0]}`; this.name = this.randomName();
this.delta = {}; this.delta = {};
this.chatCooldown = 0;
} }
disconnect() { disconnect() {
@ -23,6 +25,21 @@ class Player {
this.lastAction = Date.now(); this.lastAction = Date.now();
} }
randomName() {
let fruit = names.fruit[Math.random() * names.fruit.length | 0];
let adjectives = names.adjectives[fruit[0].toLowerCase()];
adjectives = adjectives || ['Weird'];
let adjective = adjectives[Math.random() * adjectives.length | 0];
return adjective + ' ' + fruit;
}
chat(data) {
if(!this.room) return;
if(!data.msg) return;
this.room.chat(this, data.msg.slice(0, 100));
}
send(msg, data) { send(msg, data) {
this.connection.send(msg, data); this.connection.send(msg, data);
} }
@ -36,6 +53,10 @@ class Player {
this.connection.send('update', this.delta); this.connection.send('update', this.delta);
this.delta = {}; this.delta = {};
} }
tick() {
if(this.chatCooldown > 0) this.chatCooldown -= 0.1;
}
} }
module.exports = Player; module.exports = Player;

View file

@ -1,17 +1,22 @@
'use strict'; 'use strict';
const escapeHtml = require('escape-html');
const World = require('./world'); const World = require('./world');
const messages = require('./messages.json'); const messages = require('./messages.json');
class Room { class Room {
constructor() { constructor(gameServer) {
this.players = new Set(); this.players = new Set();
this.teamA = new Set(); this.teamA = new Set();
this.teamB = new Set(); this.teamB = new Set();
this.world = new World(this); this.world = new World(this);
this.name = (Math.random() * 100000 | 0).toString(36); this.name = (Math.random() * 100000 | 0).toString(36);
this.gameServer = gameServer;
this.io = this.gameServer.net.io;
this.start(); this.start();
} }
@ -37,6 +42,8 @@ class Room {
this.stop(); this.stop();
wingbase.gameServer.deleteRoom(this.name); wingbase.gameServer.deleteRoom(this.name);
} }
this.message('roomLeave', player.name);
} }
setTeam(player, team) { setTeam(player, team) {
@ -66,6 +73,17 @@ class Room {
this.players.forEach(player => player.send(msg, data)); this.players.forEach(player => player.send(msg, data));
} }
chat(player, message) {
wingbase.log(`${this.name}/${player.name}: ${message}`);
this.chatCooldown++;
this.io.to(this.name).emit('chat', {
type: 'player',
source: player.name,
message: escapeHtml(message.slice(0, 100))
});
}
message(type, values) { message(type, values) {
if (!(values instanceof Array)) values = [values]; if (!(values instanceof Array)) values = [values];
@ -74,7 +92,7 @@ class Room {
// TODO: format name to class. // TODO: format name to class.
message = message.replace('@', values[0]); message = message.replace('@', `<b>${values[0]}</b>`);
this.broadcast('chat', { this.broadcast('chat', {
type: 'server', type: 'server',

View file

@ -7,7 +7,11 @@ class Mount {
this.ship = ship; this.ship = ship;
this.type = data.type || 'turret'; this.type = data.type || 'turret';
this.fixture = false; // TODO: Create fixture. this.fixture = false;
this.position = {
x: data.pos[0],
y: data.pos[1]
}
this.traversal = data.traversal ? { this.traversal = data.traversal ? {
cw: data.bounds[0], cw: data.bounds[0],
@ -22,7 +26,7 @@ class Mount {
packFull() { packFull() {
return { return {
} }
} }
} }

View file

@ -52,7 +52,7 @@ class Physics {
bodyDef.angularVelocity = body.rvel || 0; bodyDef.angularVelocity = body.rvel || 0;
bodyDef.bullet = body.type == 'missile'; bodyDef.bullet = body.type == 'missile';
bodyDef.linearDamping = body.type == 'asteroid' ? 0.003 : 0.01; bodyDef.linearDamping = body.type == 'asteroid' ? 0.003 : 0.01;
bodyDef.angularDamping = body.type == 'asteroid' ? 0.003 : 0.04; bodyDef.angularDamping = body.type == 'asteroid' ? 0.003 : 0.06;
bodyDef.type = body.type == 'structure' ? bodyDef.type = body.type == 'structure' ?
Box2D.b2BodyType.b2_staticBody : Box2D.b2BodyType.b2_dynamicBody; Box2D.b2BodyType.b2_staticBody : Box2D.b2BodyType.b2_dynamicBody;
if (body.player || true) bodyDef.allowSleep = false; if (body.player || true) bodyDef.allowSleep = false;

View file

@ -5,7 +5,7 @@
"power": { "power": {
"forward": 0.015, "forward": 0.015,
"back": 0, "back": 0,
"rotation": 0.003 "rotation": 0.004
}, },
"frame": [ "frame": [
[ [
@ -21,6 +21,19 @@
{ {
"pos": [18, 4], "pos": [18, 4],
"type": "fixed", "type": "fixed",
"size": 0,
"traversal": false
},
{
"pos": [18, 28],
"type": "fixed",
"size": 0,
"traversal": false
},
{
"pos": [1, 16],
"type": "fixed",
"size": 1,
"traversal": false "traversal": false
} }
] ]