msg.rb
znmud-0.0.1/lib/engine/msg.rb · 3.0 KB
From the archive znmud-minimal-0.0.1.tar.gz
Download the archive « Back to the archive contents
# Author: Craig Smith
#
# This source code copyright (C) 2009 Craig Smith
# All rights reserved.
#
# Released under the terms of the TeensyMUD Public License
# See LICENSE file for additional information.
#
require 'gettext'
# Special message parsing string class
class Msg < String
include GetText
bindtextdomain("engine")
logger "DEBUG"
attr_accessor :p1, :p2, :o1, :o2, :s1, :s2, :broadcast, :system, :cansee,
:is_invisible, :cansee_invisible, :no_parse,
:ch1, :ch2, :obj1, :obj2,
:m1, :m2, :n1, :n2
def initialize(str=nil)
self.ch1 = nil # Character 1 object
self.ch2 = nil # Character 2 object
self.obj1 = nil # Object 1 object
self.obj2 = nil # Object 2 object
self.p1 = nil # Character 1 spot
self.p2 = nil # Character 2 spot
self.o1 = nil # Object 1 Spot
self.o2 = nil # Object 2 Spot
self.s1 = "" # String 1 Spot
self.s2 = "" # String 2 Spot
self.m1 = nil # Character 1 pronoun
self.m2 = nil # Character 2 pronoun
self.n1 = nil # Character 1 pos. pronoun
self.n2 = nil # Character 2 pos. pronoun
self.broadcast = false
self.system = false
self.cansee = true
self.is_invisible = false
self.cansee_invisible = false
self.no_parse = false
super(str)
end
# Replaces variables based on special string chars that match ^p1 == p1, etc.
def to_s
return nil if is_invisible and not cansee_invisible
str = self.dup
return str if no_parse
if cansee
# Auto-populate
if ch1
self.p1 = ch1.shortname if not p1
self.m1 = ch1.pronoun if not m1
self.n1 = ch1.pos_pronoun if not n1
end
if ch2
self.p2 = ch2.shortname if not p2
self.m2 = ch2.pronoun if not m2
self.n1 = ch2.pos_pronoun if not n1
end
if obj1
self.o1 = obj1.shortname if not o1
end
if obj2
self.o2 = obj2.shortname if not o2
end
# Do substitute
if str=~/\^p1/
if p1
str.gsub!("^p1", p1)
else
log.error("^p1 not set for #{str}")
end
end
if str=~/\^p2/
if p2
str.gsub!("^p2", p2)
else
log.error("^p2 not set for #{str}")
end
end
if str=~/\^o1/
if o1
str.gsub!("^o1", o1)
else
log.error("^o1 not set for #{str}")
end
end
if str=~/\^o2/
if o2
str.gsub!("^o2", o2)
else
log.error("^o2 not set for #{str}")
end
end
if str=~/\^m1/
if m1
str.gsub!("^m1", m1)
else
log.error("^m1 not set for #{str}")
end
end
if str=~/\^m2/
if m2
str.gsub!("^m2", m2)
else
log.error("^m2 not set for #{str}")
end
end
if str=~/\^n1/
if n1
str.gsub!("^n1", n1)
else
log.error("^n1 not set for #{str}")
end
end
if str=~/\^n2/
if n2
str.gsub!("^n2", n2)
else
log.error("^n2 not set for #{str}")
end
end
else
str.gsub!("^p1", _("someone"))
str.gsub!("^p2", _("someone"))
str.gsub!("^o1", _("something"))
str.gsub!("^o2", _("something"))
str.gsub!("^m1", _("it"))
str.gsub!("^m2", _("it"))
str.gsub!("^n1", _("its"))
str.gsub!("^n2", _("its"))
end
str.gsub!("^s1",s1)
str.gsub!("^s2",s2)
str.ucfirst
end
end