var crypto = require('crypto'); class Admin { help(room, mobile, input) { if(input.join(" ").trim().length == 0) { libs.Output.toMobile(mobile.id, "Admin commands:"); libs.Output.toMobile(mobile.id, "reload, hash, time, permission"); return false; } var topic = input.join(" ").trim(); switch(topic) { case "reload": libs.Output.toMobile(mobile.id, "* reload world - reloads the core processing"); libs.Output.toMobile(mobile.id, "* reload builder - reloads the building tools"); libs.Output.toMobile(mobile.id, "* reload libs - reloads common utilities like output"); libs.Output.toMobile(mobile.id, "* reload admin - reloads the administration tools"); libs.Output.toMobile(mobile.id, "* reload commands - reloads the collection of player input commands"); return false; case "hash": libs.Output.toMobile(mobile.id, "hash [input to hash] - will generate a sha256-base64 hash"); return true; case "time": libs.Output.toMobile(mobile.id, "time - displays the current server time tick (0-640)"); libs.Output.toMobile(mobile.id, "time [value] - sets the server to this time tick"); return true; case "permission": libs.Output.toMobile(mobile.id, "permission see [playerName] - lists the permissions of the player"); libs.Output.toMobile(mobile.id, "permission add [playerName] [admin|builder] - adds the permission to a player"); libs.Output.toMobile(mobile.id, "permission remove [playerName] [admin|builder] - removes a permission from a player"); return true; } } reload(room, mobile, input) { switch(input[0]) { case "world": return this.reloadWorld(room, mobile, input); case "builder": return this.reloadBuilder(room, mobile, input); case "libs": return this.reloadLibs(room, mobile, input); case "admin": return this.reloadAdmin(room, mobile, input); case "commands": return this.reloadCommands(room, mobile, input); //default: //return this.help(room, mobile, ["reload"]); } } reloadWorld(room, mobile, input) { var tempTime = world.time; var tempSession = session; libs.Output.toMobile(mobile.id,"Preparing to reload world."); try { delete require.cache[require.resolve('./world')]; world = require('./world'); world.time = tempTime; session = tempSession; libs.Output.toMobile(mobile.id,"World reloaded"); libs.Output.toServer("World reloaded.\n"); } catch (error) { libs.Output.toMobile(mobile.id, "Error when attempting to reload world."); libs.Output.toMobile(mobile.id, error.code + " " + error.stack +"\n"); } return true; } reloadBuilder(room, mobile, input) { libs.Output.toMobile(mobile.id,"Preparing to reload build tools."); try { delete require.cache[require.resolve('./builder')]; builder = require('./builder'); libs.Output.toMobile(mobile.id,"Build tools reloaded"); libs.Output.toServer("Build tools reloaded.\n"); } catch (error) { libs.Output.toMobile(mobile.id, "Error when attempting to reload build tools."); libs.Output.toMobile(mobile.id, error.code + " " + error.stack +"\n"); } return true; } reloadLibs(room, mobile, input) { libs.Output.toMobile(mobile.id,"Preparing to reload common libraries."); try { delete require.cache[require.resolve('./libs')]; libs = require('./libs'); for(var library in libs){ libs.Output.toMobile(mobile.id, "\t" + library); delete require.cache[require.resolve('./libs/'+library.toLowerCase()+".js")]; libs[library] = require('./libs/' + library.toLowerCase() + ".js"); } libs.Output.toMobile(mobile.id,"Common libraries reloaded"); libs.Output.toServer("Common libraries reloaded.\n"); } catch (error) { libs.Output.toMobile(mobile.id, "Error when attempting to reload common libraries."); libs.Output.toMobile(mobile.id, error.code + " " + error.stack +"\n"); } return true; } reloadCommands(room, mobile, input) { libs.Output.toMobile(mobile.id,"Preparing to reload commands."); try { delete require.cache[require.resolve('./commands')]; commands = require('./commands'); for(var commandClass in commands){ libs.Output.toMobile(mobile.id, "\t" + commandClass); delete require.cache[require.resolve('./commands/'+commandClass.toLowerCase()+".js")]; commands[commandClass] = require('./commands/' + commandClass.toLowerCase() + ".js"); } libs.Output.toMobile(mobile.id,"Commands reloaded"); libs.Output.toServer("Commands reloaded.\n"); } catch (error) { libs.Output.toMobile(mobile.id, "Error when attempting to reload commands."); libs.Output.toMobile(mobile.id, error.code + " " + error.stack +"\n"); } return true; } reloadAdmin(room, mobile, input) { libs.Output.toMobile(mobile.id,"Preparing to reload admin tools."); try { delete require.cache[require.resolve('./admin')]; admin = require('./admin'); libs.Output.toMobile(mobile.id,"Admin tools reloaded."); libs.Output.toServer("Admin tools reloaded.\n"); } catch (error) { libs.Output.toMobile(mobile.id, "Error when attempting to reload admin tools."); libs.Output.toMobile(mobile.id, error.code + " " + error.stack +"\n"); } return true; } hash(room, mobile, input) { var hash = world.mud_generateHash(input.join(" ")); libs.Output.toMobile(mobile.id,"hash: " + hash); return true; } time(room, mobile, input) { if(input.join(" ") == null || input.join(" ") == "") { libs.Output.toMobile(mobile.id,"The time is " + world.time); return true; } else { var newTime = parseInt(input.join(" ")); world.time = newTime; libs.Output.toMobile(mobile.id,"The time is " + world.time); return true; } } permission(room, mobile, input) { switch(input.shift()) { case "add": this.mud_addPermission(room, mobile, input); return true; case "remove": this.mud_removePermission(room, mobile, input); return true; case "see": this.mud_seePermission(room, mobile, input); return true; default: return this.help(room, mobile, ["permission"]); } } mud_addPermission(room, mobile, input) { var playerIds = []; for(var index in session.players) { var player = session.players[index]; playerIds.push(player.id); } var username = input.join(" ").trim().toLowerCase(); var player = libs.Utilities.getSpecific(username, playerIds, session.mobiles); if(player != null) { var role = input[1].trim(); if(player.roles.indexOf(role) == -1) { player.roles.push(role); libs.Output.toMobile(mobile.id, "Adding permission '"+ role + "' to player '"+player.name+"'"); libs.Output.toMobile(player.id, "You now have '" + role + "' access."); player.save(); world.mud_handleInput(player.roomId, player.id, ["help", role]); } libs.Output.toMobile(mobile.id, "Player '"+player.name+"' roles:"); for(var index in player.roles) { libs.Output.toMobile(mobile.id, "\t"+player.roles[index]); } return true; } libs.Output.toMobile(mobile.id, "That player is not online."); return true; } mud_seePermission(room, mobile, input) { var playerIds = []; for(var index in session.players) { var player = session.players[index]; playerIds.push(player.id); } var username = input.join(" ").trim().toLowerCase(); var player = libs.Utilities.getSpecific(username, playerIds, session.mobiles); if(player != null) { libs.Output.toMobile(mobile.id, "Player '"+player.name+"' roles:"); for(var index in player.roles) { libs.Output.toMobile(mobile.id, "\t"+player.roles[index]); } return true; } libs.Output.toMobile(mobile.id, "That player is not online."); return true; } mud_removePermission(room, mobile, input) { var playerIds = []; for(var index in session.players) { var player = session.players[index]; playerIds.push(player.id); } var username = input.join(" ").trim().toLowerCase(); var player = libs.Utilities.getSpecific(username, playerIds, session.mobiles); if(player != null) { var role = input[1].trim(); if(player.roles.indexOf(role) != -1) { player.roles.splice(player.roles.indexOf(role), 1); libs.Output.toMobile(mobile.id, "Removing permission '"+ role + "' from player '"+player.name+"'"); libs.Output.toMobile(player.id, "You no longer have '" + role + "' access."); player.save(); } libs.Output.toMobile(mobile.id, "Player '"+player.name+"' roles:"); for(var index in player.roles) { libs.Output.toMobile(mobile.id, "\t"+player.roles[index]); } return true; } libs.Output.toMobile(mobile.id, "That player is not online."); return true; } } module.exports = new Admin();