0
0
Fork 0
mirror of https://gitlab.com/Oreolek/salet-module.git synced 2024-07-05 00:04:23 +03:00
salet-module/lib/situation.coffee

88 lines
2.9 KiB
CoffeeScript

###
This file is built on top of Raconteur.
Raconteur is copyright (c) 2015 Bruno Dias
This file is copyright (c) 2016 Alexander Yakovlev
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
###
undum = require('./undum.js')
markdown = require('./markdown.coffee')
###
The prototype RaconteurSituation is the basic spec for situations
created with Raconteur. It should be able to handle any use case for Undum.
This prototype is fairly complex; see the API documentation.
###
RaconteurSituation = (spec) ->
if RaconteurSituation.arguments.length == 0
return
undum.Situation.call(this, spec)
for key, value of spec
this[key] ?= value
@visited = 0
return this
RaconteurSituation.inherits(undum.Situation)
###
Situation.prototype.act() is called by Undum whenever an action link
(Ie, a link that doesn't point at another situation or an external URL) is
clicked.
Raconteur's version of act() is set up to implement commonly used
functionality: "writer" links, "replacer" links, "inserter" links, and
generic "action" links that call functions which access the underlying
Undum API.
###
RaconteurSituation.prototype.act = (character, system, action) ->
actionClass = action.match(/^_(\w+)_(.+)$/)
that = this
responses = {
writer: (ref) ->
content = that.writers[ref].fcall(that, character, system, action)
output = markdown(content)
system.writeInto(output, '#current-situation')
replacer: (ref) ->
content = that.writers[ref].fcall(that, character, system, action)
output = "<span>"+content+"</span>" # <p> tags are usually bad for replacers
system.replaceWith(output, '#'+ref)
inserter: (ref) ->
content = that.writers[ref].fcall(that, character, system, action)
output = markdown(content)
system.writeInto(output, '#'+ref)
}
if (actionClass)
# Matched a special action class
[responder, ref] = [actionClass[1], actionClass[2]]
if(!@writers.hasOwnProperty(actionClass[2]))
throw new Error("Tried to call undefined writer: #{action}");
responses[responder](ref);
else if (@actions.hasOwnProperty(action))
@actions[action].call(this, character, system, action);
else
throw new Error("Tried to call undefined action: #{action}");
RaconteurSituation.prototype.register = () ->
if not @name?
console.error("Situation has no name")
return this
undum.game.situations[@name] = this
return this
module.exports = RaconteurSituation