Skip to content
Snippets Groups Projects
Commit 925ba30e authored by Travis Savo's avatar Travis Savo
Browse files

Refactoring bodies and command structures. Still a ways to go on all this.

parent c3126f8e
No related branches found
No related tags found
No related merge requests found
Pipeline #
Loading
Loading
@@ -48,6 +48,7 @@
"mocha": "^3.2.0",
"mocha-circleci-reporter": "0.0.2",
"mocha-junit-reporter": "^1.12.1",
"mock-socket": "^6.0.3",
"telnet-client": "^0.12.3",
"xunit-file": "^1.0.0"
}
Loading
Loading
Loading
Loading
@@ -13,7 +13,7 @@ global.$game.common.moveTo = (to)->
global.$game.common.move(this, to)
 
global.$game.common.question = (socket, prompt, criteria, callback)->
deferred = require("./node_modules/q").defer()
deferred = require("q").defer()
readline = require("readline")
askQuestion = ->
rl = readline.createInterface(socket, socket)
Loading
Loading
@@ -30,7 +30,7 @@ global.$game.common.question = (socket, prompt, criteria, callback)->
return deferred.promise.nodeify(callback)
 
global.$game.common.yesorno = (socket, prompt, callback)->
deferred = require("./node_modules/q").defer()
deferred = require("q").defer()
global.$game.common.question socket, prompt, (criteria)->
return "Please answer yes or no." if not criteria or not criteria.toLowerCase().startsWith("y") and not criteria.toLowerCase().startsWith("n")
.then (answer) ->
Loading
Loading
@@ -38,7 +38,7 @@ global.$game.common.yesorno = (socket, prompt, callback)->
return deferred.promise.nodeify(callback)
 
global.$game.common.choice = (socket, prompt, choices, callback) ->
deferred = require("./node_modules/q").defer()
deferred = require("q").defer()
what = prompt + "\n"
index = 1
map = {}
Loading
Loading
@@ -56,6 +56,9 @@ global.$game.common.choice = (socket, prompt, choices, callback) ->
return deferred.promise.nodeify(callback)
 
global.$game.common.gameTime = ->
moment = require("./node_modules/moment")
moment = require("moment")
now = moment()
now.year(now.year() + 85)
\ No newline at end of file
now.year(now.year() + 85)
global.randomString = (size)->
Array(size+1).join((Math.random().toString(36)+'00000000000000000').slice(2, 18)).slice(0, size)
\ No newline at end of file
Loading
Loading
@@ -62,6 +62,18 @@ body.init = (@owner, @info, @primaryHand = Math.floor(Math.random()*2) ? "right"
stomach:global.$game.common.makeBodyPart "stomach", [], true, false, false
back:global.$game.common.makeBodyPart "back", ["spine"], true, false, false
 
body.getHeight = ->
@info.appearance.height
body.getWeight = ->
@info.appearance.weight
body.getHeightString = ->
global.$game.constants.body.human.formatHeight @info?.appearance?.height or 1.7
body.getWeightString = ->
global.$game.constants.body.human.formatWeight @info?.appearance?.weight or 80, @getHeight()
body.tell = (what)->
@owner.tell(what)
 
Loading
Loading
Loading
Loading
@@ -58,7 +58,8 @@ charGen.start = (socket)->
makePlayerLoop()
 
charGen.cloneNewPlayer = (socket, info)->
player = new global.$game.classes.Player(info.name.alias, socket.user, info)
socket.user.createBody
socket.user.body = new global.$game.classes.Player(info.name.alias, socket.user, info)
socket.tell("Designing bio-specification for clone job.".cyan)
socket.tell("Please wait...".italic)
 
Loading
Loading
require("./src/user")
require("user")
global.$game.$index = {} if not global.$game.$index
global.$game.$index.users = {} if not global.$game.$index.users
 
Loading
Loading
@@ -29,7 +29,7 @@ global.$game.common.login.handleNewConnection = (socket) ->
 
global.$game.common.login.loginLoop = (socket) ->
readline = require('readline')
_ = require("./node_modules/underscore")
_ = require("underscore")
rl = readline.createInterface(socket, socket)
loginPrompt = "Login> "
passwordPrompt = "Password> "
Loading
Loading
@@ -73,13 +73,13 @@ global.$game.common.login.loginLoop = (socket) ->
socket.tell("Successfully authenticated as " + login + ". Welcome back!")
global.$driver.authenticatedUsers[user] = socket
socket.user = user
user.handleConnection(socket)
global.$game.common.login.handleConnection(socket)
return
socket.tell("Successfully authenticated as " + login + ". Welcome back!")
global.$driver.authenticatedUsers[user] = socket
socket.user = user
user.lastLogin = new Date()
user.handleConnection(socket)
global.$game.common.login.handleConnection(socket)
)
 
global.$game.common.login.showWelcomeMessage = (socket, callback) ->
Loading
Loading
@@ -159,6 +159,48 @@ global.$game.common.login.getUserName = (socket, callback) ->
, (err, answer) ->
callback(answer)
 
global.$game.common.login.handleConnection = (socket) ->
socket.question = (prompt, criteria, callback)->
global.$game.common.question socket, prompt, criteria, callback
socket.choice = (prompt, options, callback)->
global.$game.common.choice socket, prompt, options, callback
socket.yesorno = (prompt, callback)->
global.$game.common.yesorno socket, prompt, callback
choices = []
if not socket.user.body
prompt = """
Please make a selection from the following options:
1. Make a new character
2. Quit
"""
else
prompt = """
Please make a selection from the following options:
1. Enter the game as #{socket.user.player.name}
2. Quit
"""
socket.question prompt, (answer) ->
return "Invalid selection." if answer.trim() isnt "1" and answer.trim() isnt "2"
, (err, answer)->
if(answer is "2")
return socket.end()
if(answer is "1")
if(socket.user.player)
x = 3
socket.tell("Now entering the world in 3...")
ready = ->
x--
if(x is 0)
socket.user.player.goIC(socket)
else
socket.tell(x + "...")
setTimeout ready, 1000
return setTimeout ready, 1000
global.$game.common.charGen.start(socket)
global.$game.common.login.repl = (socket) ->
repl = require("repl")
repl.start(
Loading
Loading
require("proportionate")
global.$game.$index = {} if not global.$game.$index
global.$game.$index.players = {} if not global.$game.$index.players
global.$game.constants = {} if not global.$game.constants
global.$game.constants.player = {} if not global.$game.constants.player
if not global.$game.classes.Player
global.$game.classes.Player = class Player
constructor:->
@type = "$game.classes.Player"
this.init.apply(this, arguments)
player = global.$game.classes.Player.prototype
player.init = (@name, @user, @info, @location = global.$game.$index.rooms.$nowhere) ->
throw new Error("Player names must be unique.") if global.$game.$index.players[@name]
throw new Error("Player must be associated with a user.") if not @user
global.$game.$index.players[@name] = this
@salt = require("uuid").v4()
@user.player = this
@description = "Someone who needs a better description."
@doing = ""
@body = new global.$game.classes.HumanBody(this, @info)
player.tell = (what)->
@user.tell(what)
player.walkThrough = (exit) ->
exit.accept(this)
player.getSex = ->
@info.sex or "neuter"
player.getHeight = ->
@info.height or 1.75
player.getWeight = ->
@info.weight or 75
player.getHeightString = ->
return global.$game.constants.player.formatHeight @info?.appearance?.height or 1.7
player.getWeightString = ->
return global.$game.constants.player.formatWeight @info?.appearance?.weight or 80, @getHeight()
player.hold = (what)->
return @tell("That's not something that can be held.") if what.cantBeHeld
return @tell("You'll need both hands free for that.") if what.twoHanded and (@leftHand or @rightHand)
if what.twoHanded
@leftHand = @rightHand = what
hands = "both hands"
if not @rightHand
return @tell("You're now holding the #{what.description()} in your right hand")
player.getSocket = ->
@user.getSocket()
player.isConnected = ->
@user.isConnected()
player.goIC = () ->
@commandLoop()
player.commandLoop = ->
if not @isConnected() then return console.log("Not connected")
command = global.$game.common.question @getSocket(), ""
self = this
command.then (input)->
try
self.getInputHandler()(input)
catch err
console.log(err, err.stack.split("\n"))
self.commandLoop()
command.done()
player.getInputHandler = ->
self = this
return global.$driver.getInputHandler(self) or (input)->
self.handleCommand(input)
player.setInputHandler = (handler) ->
global.$game.$driver.setInputHandler(this, handler)
player.clearInputHandler = ->
global.$game.$driver.clearInputHandler(this)
player.handleCommand = (command)->
self = this
func = @matchCommand(command)
return @tell("I don't understand that.") if not func
_ = require("underscore")
test = _(func.tests).find (test)->
test.regexp.test command
args = [self]
groups = test.regexp.exec(command)
if test.position
if test.position > -1
args.push groups[test.position]
else
test.position.forEach (item)->
args.push groups[item]
func.func.apply(func.source, args)
player.matchCommand = (command)->
_ = require("underscore")
self = this
_(@getCommands(self)).find (options)->
_(options.tests).find (test)->
test.regexp.test command
player.getCommands = (who)->
_ = require("underscore")
commands = [
{
name:"l~ook"
tests:[
regexp:/l(ook)?$/i
]
description:"Describes the room you're presently in."
func:self.look
source:self
},
{
name:"l~ook [at/in] <something>"
tests:[
{
regexp:/^l(ook)?\s(at\s|in\s|the\s)*(.+)$/i
position:3
}
]
description:"Describes a specific item in more detail."
func:self.lookAt
source:self
}
]
commands = commands.concat(@location?.getCommands(who)) if @location.getCommands
_(commands).flatten()
player.resolve = (what) ->
_ = require("underscore")
what = what.trim().toLowerCase()
return this if what is "me"
return @location if what is "here"
contents = @resolveAllContents()
found = _(contents).filter (item)->
regexp.test(item?.name) or _(item?.aliases).find (alias)->
regexp.test(alias)
return found[0] if found and found.length is 1
return found if found.length > 1
groups = /^([\w-]+)\s(.*)$/i.exec what
return undefined if not groups
ordinal = require("ordinal")
position = ordinal(groups[1])
rest = groups[2]
regexp = new RegExp("^(.*)\b(" + rest + ")(.*)$", "i")
found = _(contents).filter (item)->
regexp.test(item?.name or "") or _(item?.aliases or []).find (alias)->
regexp.test(alias)
return found[position-1] if found.length > 0 and position > 0 and found.length > position-1
return undefined if found.length is 0
found
require("src/stimulus")
require("stimulus")
 
global.$game = {} if not global.$game
global.$game.classes = {} if not global.$game.classes
Loading
Loading
Loading
Loading
@@ -13,7 +13,6 @@ if not global.$game.classes.User
 
user = global.$game.classes.User.prototype
 
user.init = (@name, @email, password, @lastIp) ->
throw new Error("Username already in use.") if global.$game.$index.users[@name]
@salt = require("uuid").v4()
Loading
Loading
@@ -29,48 +28,49 @@ user.getSocket = ->
global.$driver.getSocket(this)
 
user.tell = (what) ->
global.$driver.getSocket(this)?.tell(what) if typeof what is "string"
@getSocket()?.tell(what) if typeof what is "string"
 
user.handleConnection = (socket) ->
socket.question = (prompt, criteria, callback)->
global.$game.common.question socket, prompt, criteria, callback
socket.choice = (prompt, options, callback)->
global.$game.common.choice socket, prompt, options, callback
socket.yesorno = (prompt, callback)->
global.$game.common.yesorno socket, prompt, callback
choices = []
if not socket.user.player
prompt = """
Please make a selection from the following options:
1. Make a new character
2. Quit
user.isConnected = ->
console.log("Ah hell")
!!global.$driver.getSocket(this)
 
"""
else
prompt = """
Please make a selection from the following options:
1. Enter the game as #{socket.user.player.name}
2. Quit
user.goIC = () ->
@commandLoop()
user.commandLoop = ->
if not @isConnected() then return console.log("Not connected")
self = this
global.$game.common.question @getSocket(), ""
.then (input)->
try
@handleCommand(input)
catch err
console.log(err, err.stack.split("\n"))
.then(self.commandLoop)
.done()
user.handleCommand = (command)->
self = this
func = @matchCommand(command)
return @tell("I don't understand that.") if not func
_ = require("underscore")
test = _(func.tests).find (test)->
test.regexp.test command
args = [self]
groups = test.regexp.exec(command)
if test.position
if test.position > -1
args.push groups[test.position]
else
test.position.forEach (item)->
args.push groups[item]
func.func.apply(func.source, args)
user.matchCommand = (command)->
_ = require("underscore")
self = this
_(@body.getCommands(self)).find (options)->
_(options.tests).find (test)->
test.regexp.test command
 
"""
socket.question prompt, (answer) ->
return "Invalid selection." if answer.trim() isnt "1" and answer.trim() isnt "2"
, (err, answer)->
if(answer is "2")
return socket.end()
if(answer is "1")
if(socket.user.player)
x = 3
socket.tell("Now entering the world in 3...")
ready = ->
x--
if(x is 0)
socket.user.player.goIC(socket)
else
socket.tell(x + "...")
setTimeout ready, 1000
return setTimeout ready, 1000
global.$game.common.charGen.start(socket)
 
user.isConnected = ->
if global.$driver.getSocket(this) then return true else return false
require('app-module-path').addPath(".")
chai = require "chai"
expect = chai.expect
chai.should()
require('app-module-path').addPath(__dirname + '/../src')
require("../src/room")
 
describe "Room", ->
Loading
Loading
Loading
Loading
@@ -4,7 +4,45 @@ chai.use spies
chai.should()
require('app-module-path').addPath(__dirname + '/../src')
require("user")
net = require "net"
stream = require('stream').Duplex
 
describe "User", ->
harnessUser = ->
new global.$game.classes.User(global.randomString(10))
it "should be constructable", ->
user = new global.$game.classes.User()
\ No newline at end of file
user = harnessUser()
it "should get the current socket from the driver and tell it what it's told", ->
user = harnessUser()
tell = chai.spy()
global.$driver =
getSocket:chai.spy ->
tell:tell
user.tell "what"
global.$driver.getSocket.should.have.been.called.with user
tell.should.have.been.called.with
delete global.$driver
it "should be connected if there is a socket", ->
user = harnessUser()
global.$driver =
getSocket:->
false
user.isConnected().should.be.false
global.$driver =
getSocket:->
true
user.isConnected().should.be.true
delete global.$driver
it "should be able to go IC", ->
global.$driver =
getSocket:->
new stream
read:(size)->
write:(chunk, encoding, callback)->
harnessUser().goIC()
it "should handle a command", ->
#harnessUser().handleCommand("look")
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment