From 390c96c86855d086fee45e924f3dd514fae9363c Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sat, 25 Apr 2015 20:14:54 +0200 Subject: Add Naampje --- chatserver.js | 15 ++++++++++----- package.json | 1 + 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/chatserver.js b/chatserver.js index ec0e839..11443d8 100755 --- a/chatserver.js +++ b/chatserver.js @@ -4,10 +4,14 @@ var fs=require("fs"), util=require("util"), WebSocketServer=require("ws").Server, CircularBuffer=require("circular-buffer"), - Persist=require("node-persist"); + Persist=require("node-persist"), + Naampje=require("naampje").name; var HTTPPORT=81,WSPORT=8000; +var nickdeleteregex=/[\x00-\x20\x7F\x80-\xFF]/g; + + ////////// WEBSOCKETS ////////// function uniqid(){ @@ -49,9 +53,10 @@ if(!lobbies){ new WebSocketServer({port:WSPORT}).on("connection",function(ws){ var uobj; (function(){ - var nick="_user"+uniqid(); - while(fnick(nick)!=-1)nick="_user"+uniqid(); - uobj=new UserObject(nick,ws); + //var nick="_user"+uniqid(); + var nick=Naampje(),suffix=""; + if(fnick(nick)!=-1)for(suffix=1;fnick(nick+suffix)!=-1;suffix++); + uobj=new UserObject((nick+suffix).replace(nickdeleteregex,""),ws); })(); users.push(uobj); ws.send("#yournick "+uobj.nick); @@ -72,7 +77,7 @@ new WebSocketServer({port:WSPORT}).on("connection",function(ws){ if(uobj.lob==null)ws.send("#mylobby"); else ws.send("#mylobby "+uobj.lob); } else if(msg.match(/^#nick /)){ - var choice=msg.slice(6).replace(/[\x00-\x20\x7F\x80-\xFF]/g,""); //all shitty characters including space (' ') + var choice=msg.slice(6).replace(nickdeleteregex,""); //all shitty characters including space (' ') var i; if(choice.length==0||choice.length>8){ ws.send("#error Invalid nick"); diff --git a/package.json b/package.json index d359a03..1fd53cb 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "main": "chatserver.js", "dependencies": { "circular-buffer": "0.0.6", + "naampje": "^1.0.1", "node-persist": "0.0.3", "ws": "^0.6.5" }, -- cgit v1.2.3-70-g09d2