dkmud/commands/builder.py

286 lines
8.7 KiB
Python
Raw Normal View History

2022-01-10 14:42:13 +01:00
from evennia import default_cmds, create_object, search_tag
from evennia.utils import inherits_from
from evennia.utils.eveditor import EvEditor
from commands.command import Command
from typeclasses.exits import BaseDoor
from typeclasses.rooms import Room, Zone
def _descdoor_load(caller):
return caller.db.evmenu_target.db.desc or ""
def _descdoor_save(caller, buf):
"""
Save line buffer to the desc prop. This should
return True if successful and also report its status to the user.
"""
caller.db.evmenu_target.setdesc(buf)
caller.msg("Saved.")
return True
def _descdoor_quit(caller):
caller.attributes.remove("evmenu_target")
caller.msg("Exited editor.")
class CmdDescDoor(Command):
"""
describe a BaseDoor in the current room.
Usage:
descdoor <obj> = <description>
Switches:
edit - Open up a line editor for more advanced editing.
Sets the "desc" attribute on an object. If an object is not given,
describe the current room.
"""
key = "descdoor"
aliases = ["descd"]
switch_options = ("edit",)
locks = "cmd:perm(descdoor) or perm(Builder)"
help_category = "Building"
def edit_handler(self):
if self.rhs:
self.msg("|rYou may specify a value, or use the edit switch, " "but not both.|n")
return
if self.args:
obj = self.caller.search(self.args)
else:
obj = self.caller.location or self.msg("|rYou can't describe oblivion.|n")
if not obj:
return
if not (obj.access(self.caller, "control") or obj.access(self.caller, "edit")):
self.caller.msg("You don't have permission to edit the description of %s." % obj.key)
self.caller.db.evmenu_target = obj
# launch the editor
EvEditor(
self.caller,
loadfunc=_descdoor_load,
savefunc=_descdoor_save,
quitfunc=_descdoor_quit,
key="desc",
persistent=True,
)
return
def func(self):
"""Define command"""
caller = self.caller
if not self.args or ("=" in self.args and "edit" in self.switches):
caller.msg("Usage: descdoor <obj> = <description>")
return
if "edit" in self.switches:
self.edit_handler()
return
# We have an =
obj = caller.search(self.lhs)
if not obj:
return
desc = self.rhs or ""
if inherits_from(obj, BaseDoor):
if obj.access(self.caller, "control") or obj.access(self.caller, "edit"):
obj.setdesc(desc)
caller.msg("The description was set on %s." % obj.get_display_name(caller))
else:
caller.msg("You don't have permission to edit the description of %s." % obj.key)
else:
self.msg("|rYou can't describe oblivion.|n")
class CmdOpen(default_cmds.CmdOpen):
__doc__ = default_cmds.CmdOpen.__doc__
# overloading parts of the default CmdOpen command to support doors.
def create_exit(self, exit_name, location, destination, exit_aliases=None, typeclass=None):
"""
Simple wrapper for the default CmdOpen.create_exit
"""
# create a new exit as normal
new_exit = super().create_exit(
exit_name, location, destination, exit_aliases=exit_aliases, typeclass=typeclass
)
if hasattr(self, "return_exit_already_created"):
# we don't create a return exit if it was already created (because
# we created a door)
del self.return_exit_already_created
return new_exit
if inherits_from(new_exit, BaseDoor):
# a door - create its counterpart and make sure to turn off the default
# return-exit creation of CmdOpen
self.caller.msg(
"Note: A door-type exit was created - ignored eventual custom return-exit type."
)
self.return_exit_already_created = True
back_exit = self.create_exit(
exit_name, destination, location, exit_aliases=exit_aliases, typeclass=typeclass
)
new_exit.db.return_exit = back_exit
back_exit.db.return_exit = new_exit
return new_exit
class CmdUpdateLightState(Command):
"""
update room light state.
Usage:
update_light [targetroom]
"""
key = "update_light"
aliases = ["up_l"]
locks = "cmd:perm(update_light) or perm(Builder)"
help_category = "Building"
arg_regex = r"(?:^(?:\s+|\/).*$)|^$"
def func(self):
caller = self.caller
if not self.args:
target = caller.location
if not target:
caller.msg("You have no location to update!")
return
else:
target = caller.search(self.args)
if not (target.attributes.has("is_lit") and hasattr(target, "check_light_state")):
caller.msg(
"You cannot update lights on {}!".format(target.key))
return
target.check_light_state()
caller.msg("Performed update on {}!".format(target.key))
class CmdZone(Command):
"""
creates, deletes or lists zones
Usage:
zone[/list||/del||/addroom] [zonename] [= room]
Creates a new zone.
"""
key = "zone"
locks = "cmd:perm(zone) or perm(Builders)"
help_category = "Building"
def func(self):
"""
Creates the zone.
"""
caller = self.caller
if "list" in self.switches:
string = "";
zones = search_tag(key="zone", category="general")
for zone in zones:
string += "|c{}|n ({})\n".format(zone.name, zone.dbref)
rooms = search_tag(key=zone.name, category="zoneId")
for room in rooms:
string += "- {} ({})\n".format(room.name, room.dbref)
caller.msg("Zones found: \n" + string)
return
if not self.args:
string = "Usage: zone[/list||/del||/addroom] [zonename] [= room]"
caller.msg(string)
return
if "del" in self.switches:
self.delete_zone(self.args)
elif "addroom" in self.switches:
self.add_room_to_zone(self.lhs, self.rhs)
else:
zone = create_object(Zone, key=self.args)
caller.msg("Created zone |w{}|n.".format(zone.name))
def delete_zone(self, zone_string):
caller = self.caller
zone = caller.search(zone_string, global_search=True, exact=True)
if not zone:
return
if not inherits_from(zone, Zone):
caller.msg("{} is not a valid zone.",format(zone.name))
return
key = zone.name
zone.delete()
caller.msg("Zone {} deleted.".format(key))
def add_room_to_zone(self, zone_string, room_string):
caller = self.caller
zone = caller.search(zone_string, global_search=True, exact=True)
if not zone:
return
if not inherits_from(zone, Zone):
caller.msg("{} is not a valid zone.",format(zone.name))
return
room = caller.search(room_string, global_search=True)
if not room:
return
if not inherits_from(room, Room):
caller.msg("{} is not a valid room.",format(room.name))
return
zone.add_room(room)
caller.msg("{} added to zone {}.".format(room.name, zone.name))
class CmdAddToZone(Command):
"""
add a room to a zone
Usage:
@addtozone obj = zone
Adds a room to an existing zone.
"""
key = "@addtozone"
locks = "cmd:perm(zone) or perm(Builders)"
help_category = "Building"
def func(self):
"""
Adds a room to an existing zone.
"""
caller = self.caller
if self.rhs:
# We have an =
zone = caller.search(self.rhs, global_search=True, exact=True)
if not zone:
self.msg("Zone %s doesn't exist." % self.rhs)
return
if not utils.inherits_from(zone, Zone):
self.msg("{r%s is not a valid zone.{n" % zone.name)
return
room = caller.search(self.lhs)
if not room:
self.msg("{rRoom %s doesn't exist.{n" % self.lhs)
return
if not utils.inherits_from(room, BaseRoom):
self.msg("{r%s is not a valid room.{n" % room.name)
return
room.add_to_zone(zone.name)
self.msg("Room %s (%s) added to zone %s (%s)." % (room.name, room.dbref, zone.name, zone.dbref))
else:
self.msg("{rUsage: @addtozone obj = zone{n")
return