var mongojs = require("mongojs");
var db = mongojs("localhost:27017/ThePixelWorld", ["account","progress"]);
var express = require("express");
var app = express();
var serv = require("http").Server(app);
app.get('/', function(req,res) {
res.sendFile(__dirname + '/client/index.html');
});
app.use('/client', express.static(__dirname + '/client'));
serv.listen(2000);
console.log("Server started.");
var SOCKET_LIST = {};
const servermsg = "Server: ";
const Entity = require('./server/Entities/Entity')
const Player = require('./server/Entities/Player')
const Bullet = require('./server/Entities/Bullet')
var isValidPassword = function(data, cb){
db.account.find({username:data.username, password:data.password},function(err,res){
if(res.length > 0)
cb(true);
else
cb(false);
});
}
var isUsernameTaken = function(data, cb){
db.account.find({username:data.username},function(err,res){
if(res.length > 0)
cb(true);
else
cb(false);
});
}
var addUser = function(data, cb){
db.account.insert({username:data.username, password:data.password},function(err){
cb();
});
}
var updateGMLevel = function(username, lvl, cb){
db.account.update({username:username},{$set:{gmlvl:lvl}},function(err){
cb();
});
}
var hasPermission = function(username, req, cb){
db.account.find({username:username},function(err,res){
if (res[0].gmlvl >= req)
cb(true);
else
cb(false);
});
}
var io = require('socket.io')(serv, {});
io.sockets.on('connection', function(socket){
socket.id = Math.random();
SOCKET_LIST[socket.id] = socket;
socket.on('signIn', function(data){
isValidPassword(data, function(res){
if (res){
Player.Player.onConnect(socket, data.username);
socket.playerName = data.username;
socket.emit('signInResponse', {success:true});
} else {
socket.emit('signInResponse', {success:false});
}
});
});
socket.on('signUp', function(data){
isUsernameTaken(data, function(res){
if (res){
socket.emit('signUpResponse', {success:false});
} else {
addUser(data, function(){
socket.emit('signUpResponse', {success:true});
});
}
});
});
socket.on('disconnect', function(){
delete SOCKET_LIST[socket.id];
Player.Player.onDisconnect(socket);
});
socket.on('sendMsgToServer', function(data){
if (data)
for (var i in SOCKET_LIST){
SOCKET_LIST[i].emit('addToChat', {ctimestamp: getTimeStamp(0), content: socket.playerName + ': ' + escapeHTML(chatEmote(data)), tab: "world"});
}
});
socket.on('command', function(data){
modifyer = data[0];
command = data.slice(1).split(' ');
//staff cmd
if(modifyer === '@'){
hasPermission(socket.playerName, 1, function(res){
if (res)
switch (command[0].toLowerCase()) {
// @bc (args: <message>)
case "bc":
case "broadcast":
res = joinArgs(command, 1, 0);
for (var i in SOCKET_LIST){
SOCKET_LIST[i].emit('addToChat', {ctimestamp: getTimeStamp(0), content: "<font color='red' weight='bold'>GlobalAnnounce: " + escapeHTML(res) + "</font>", tab: "world"});
}
break;
// @lbc (args: <message>)
case "lbc":
case "localbroadcast":
res = joinArgs(command, 1, 0);
for (var i in Player.Player.list){
if (Player.Player.list[i].map == Player.Player.list[socket.id].map)
SOCKET_LIST[i].emit('addToChat', {ctimestamp: getTimeStamp(0), content: "<font color='red' weight='bold'>GlobalAnnounce: " + escapeHTML(res) + "</font>", tab: "world"});
}
break;
// @who (args: none)
case "who":
res = servermsg;
for (var i in Player.Player.list){
res+=Player.Player.list[i].playerName + ", ";
}
socket.emit('addToChat', {ctimestamp: getTimeStamp(0), content: res, tab:"any"});
break;
// @where (args:[playername])
case "where":
found = false;
if(command.length <= 1)
command[1] = socket.playerName;
for (var i in Player.Player.list){
if (Player.Player.list[i].playerName.includes(command[1])){
socket.emit('addToChat', {ctimestamp: getTimeStamp(0), content: servermsg + Player.Player.list[i].playerName + ": " + Player.Player.list[i].map + " [" + Player.Player.list[i].x + ", " + Player.Player.list[i].y + "]",tab: "any"});
found = true;
}
}
if (!found)
socket.emit('addToChat', {ctimestamp: getTimeStamp(0), content: servermsg + "Player not found", tab: "any"});
break;
// @goto (args:<playername>)
case "goto":
found = false;
if(command.length <= 1)
command[1] = socket.playerName;
for (var i in Player.Player.list){
if (Player.Player.list[i].playerName.includes(command[1])){
Player.Player.list[socket.id].map = Player.Player.list[i].map;
Player.Player.list[socket.id].x = Player.Player.list[i].x;
Player.Player.list[socket.id].y = Player.Player.list[i].y;
socket.emit('addToChat', {ctimestamp: getTimeStamp(0), content: servermsg + "Warped to " + Player.Player.list[i].playerName, tab: "any"});
found = true;
break;
}
}
if (!found)
socket.emit('addToChat', {ctimestamp: getTimeStamp(0), content: servermsg + "Player not found", tab:"any"});
break;
// @recall (args:<playername>)
case "recall":
found = false;
if(command.length <= 1)
command[1] = socket.playerName;
for (var i in Player.Player.list){
if (Player.Player.list[i].playerName.includes(command[1])){
Player.Player.list[i].map = Player.Player.list[socket.id].map;
Player.Player.list[i].x = Player.Player.list[socket.id].x;
Player.Player.list[i].y = Player.Player.list[socket.id].y;
socket.emit('addToChat', {ctimestamp: getTimeStamp(0), content: servermsg + "Recalled " + Player.Player.list[i].playerName + " to you.", tab: "any"});
SOCKET_LIST[Player.Player.list[i].id].emit('addToChat', {ctimestamp: getTimeStamp(0), content: servermsg + "You were Recalled from " + socket.playerName, tab: "any"});
found = true;
break;
}
}
if (!found)
socket.emit('addToChat', {ctimestamp: getTimeStamp(0), content: servermsg + "Player not found", tab: "any"});
break;
// @warp/@tele/@teleport (args:[map] [<x> <y>])
case "warp":
case "tele":
case "teleport":
hasPermission(socket.playerName, 60, function(res)
{
if(res)
{
if(command.length < 2 || command.length > 4){
socket.emit('addToChat', {ctimestamp: getTimeStamp(0), content: servermsg + escapeHTML("Wrong syntax: @warp [map] [<x> <y>]"), tab: "any"});
return;
}
else if(command.length == 2){
Player.Player.list[socket.id].map = command[1];
var new_x = Math.random()*500; // TODO: map size?!
var new_y = Math.random()*500;
Player.Player.list[socket.id].x = new_x;
Player.Player.list[socket.id].y = new_y;
socket.emit('addToChat', {ctimestamp: getTimeStamp(0), content: servermsg + "Warped to map:" + command[1] + " x:" + new_x + " y:" + new_y, tab: "any"});
}
else if(command.length == 3){ // only cords
// TODO: check if map exists
Player.Player.list[socket.id].x = parseInt(command[1]);
Player.Player.list[socket.id].y = parseInt(command[2]);
socket.emit('addToChat', {ctimestamp: getTimeStamp(0), content: servermsg + "Warped to x:" + command[1] + " y:" + command[2], tab: "any"});
}
else if(command.length == 4){ // map x y
// TODO: check if map exists
Player.Player.list[socket.id].map = command[1];
Player.Player.list[socket.id].x = parseInt(command[2]);
Player.Player.list[socket.id].y = parseInt(command[3]);
socket.emit('addToChat', {ctimestamp: getTimeStamp(0), content: servermsg + "Warped to map:" + command[1] + " x:" + command[2] + " y:" + command[3], tab: "any"});
}
}
else
{
socket.emit('addToChat', {ctimestamp: getTimeStamp(0), content: servermsg + "you dont have permission to use this command!", tab: "any"});
}
});
break;
// @hide
case "hide":
Player.Player.list[socket.id].ignorePlayerAttack = ! Player.Player.list[socket.id].ignorePlayerAttack;
break;
// @setgm (args:<playername> <lvl>)
case "setgm":
hasPermission(socket.playerName, 99, function(res){
if (res)
updateGMLevel(command[1],parseInt(command[2]), function(){
socket.emit('addToChat', {ctimestamp: getTimeStamp(0), content: servermsg + "GM level set.", tab:"any"});
});
else
socket.emit('addToChat', {ctimestamp: getTimeStamp(0), content: servermsg + "you dont have permission to use this command!", tab: "any"});
});
break;
default:
socket.emit('addToChat', {ctimestamp: getTimeStamp(0), content: servermsg + "Command not found.", tab: "any"});
break;
}
else{
socket.emit('addToChat', {ctimestamp: getTimeStamp(0), content: servermsg + "you dont have permission to use this command!", tab: "any"});
}
});
}
// player cmd
else if(modifyer === '/'){
switch (command[0]) {
// /who (args: none)
case "who":
socket.emit('addToChat', {ctimestamp: getTimeStamp(0), content: servermsg + Object.keys(Player.Player.list).length + " Player" + ((Object.keys(Player.Player.list).length > 1)? "s" : "") + " Online.", tab: "any"});
break;
// /where (args: none)
case "where":
socket.emit('addToChat', {ctimestamp: getTimeStamp(0), content: servermsg + Player.Player.list[socket.id].playerName + ": " +
Player.Player.list[socket.id].map + " [" + Player.Player.list[socket.id].x + ", " + Player.Player.list[socket.id].y + "]", tab: "any"});
break;
// /whisper (args: <playername> <message>)
case "w":
case "whisper":
case "tell":
case "msg":
found = false;
for (var i in Player.Player.list){
if (Player.Player.list[i].playerName == command[1]){
res = escapeHTML(joinArgs(command, 2, 0));
socket.emit('addToChat', {ctimestamp: getTimeStamp(0), content: socket.playerName + ": " + res, tab: Player.Player.list[i].playerName});
SOCKET_LIST[Player.Player.list[i].id].emit('addToChat', {ctimestamp: getTimeStamp(0), content: socket.playerName + ": " + res, tab: socket.playerName});
found = true;
break;
}
}
if (!found)
socket.emit('addToChat', {ctimestamp: getTimeStamp(0), content: servermsg + "Player not found", tab: "any"});
break;
default:
socket.emit('addToChat', {ctimestamp: getTimeStamp(0), content: servermsg + "Command not found.", tab: "any"});
break;
}
}
// guild commands
else if(modifyer === '#'){
}
// party commands
else if(modifyer === '.'){
}
// help commands
else if(modifyer === '?'){
// ?
}
else{
// this should never happen...
}
});
socket.on('evalServer', function(data){
hasPermission(socket.playerName, 80, function(res){
if (res)
socket.emit('evalAnswer', eval(data));
else
socket.emit('addToChat', {ctimestamp: getTimeStamp(0), content: servermsg + "you dont have permission to use this command!", tab: "any"});
});
});
});
// erm... the game loop
setInterval(function(){
var pack = {
player: Player.Player.update(),
bullet: Bullet.Bullet.update()
}
for(var i in SOCKET_LIST){
var socket = SOCKET_LIST[i];
socket.emit('init', Entity.initPack);
socket.emit('update', pack);
socket.emit('remove', Entity.removePack);
}
Entity.initPack.player = [];
Entity.initPack.bullet = [];
Entity.removePack.player = [];
Entity.removePack.bullet = [];
}, 1000/25);
//removes (number)remArgsBefore and (number)remArgsAfter from argument array/list
joinArgs = function(argsList, remArgsBefore, remArgsAfter){
var res = "";
for(var i = 0; i<argsList.length; i++){
if(i < remArgsBefore)
continue;
if(argsList.length - i <= remArgsAfter)
continue;
res +=argsList[i] + " ";
}
return res;
}
// gets the current (weird formated) timestamp
getTimeStamp = function(){
var date_ob = new Date();
day = addLeadingZero(date_ob.getDate());
month = addLeadingZero(date_ob.getMonth());
year = date_ob.getFullYear();
hours = addLeadingZero(date_ob.getHours());
minutes = addLeadingZero(date_ob.getMinutes());
seconds = addLeadingZero(date_ob.getSeconds());
return (year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds);
}
// formats number (n) to 2-digit
addLeadingZero = function(n){
if (n <= 9)
return "0"+n;
return n;
}
escapeHTML = function(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
chatEmote = function(msg) {
return msg.replace(":D", "😀")
.replace("O:)", "😇")
.replace("0:)", "😇")
.replace(":)", "🙂")
.replace(":(", "🙁")
.replace(":o", "😮")
.replace(":O", "😲")
.replace(":'(", "😢")
.replace(":')", "🥲")
.replace(":P", "😛");
}