Skip to content

Instantly share code, notes, and snippets.

@Pinacolada64
Created July 20, 2023 01:56
Show Gist options
  • Save Pinacolada64/4e7c2a4ee8a26355f5634d6d74e6a0b8 to your computer and use it in GitHub Desktop.
Save Pinacolada64/4e7c2a4ee8a26355f5634d6d74e6a0b8 to your computer and use it in GitHub Desktop.
Make command classes for each category of command
# This is based on dot_commands.py
# some interesting ideas here:
# https://colab.research.google.com/github/interactive-fiction-class/interactive-fiction-class.github.io/blob/master/homeworks/text-adventure-game/Text_Adventure_Game.ipynb#scrollTo=8Cug7Bs3Qpdd
from dataclasses import dataclass
from typing import Callable, Any, Union
import re as regex
@dataclass
class ParserCmd(object):
# def __init__(self, cmd_name: dict, cmd_regex: bool, cmd_func: Callable):
# excludes 'cmd_name' since that is the key of the *_cmds dict
"""
the base class of a game command typed at a major prompt,
such as between turns in the dungeon.
"""
# text to type (could be a regex: '[n|north]', in which case cmd_regex = True,
# or a normal string: 'look', in which case cmd_regex = False)
# whether the command is a regex (True) or not (False):
cmd_regex: bool
# the function to call:
cmd_func: Callable[[], Any]
# which game object type(s) the command accepts:
# e.g., "get <Player>" adds Player to list of
cmd_args: Union[Any]
@dataclass
class AdminCmd(ParserCmd):
"""
This is an administrative command which could affect players.
Therefore, we don't want just anyone running it.
Stuff like:
ban boot unban
are in this category.
"""
def admin_flag(self, player_name: Player):
if self.flag["dungeon_master"] is False:
print("Sorry, can't.")
return False
def boot(self, player_name: Player):
pass
def teleport(**kwargs):
"""
Teleport to either a player or a room.
"""
player_name = kwargs["player_name"]
if self.flag(player_name) is False:
pass
for i, v in enumerate(kwargs):
print(i, v)
@dataclass
class ArchitectCmd(ParserCmd):
"""
This is a command which players with the "Architect" flag can use.
Stuff like:
db create edit hide recycle sayedit (for puppet characters)
are in this category.
"""
pass
@dataclass
class GuildCmd(ParserCmd):
"""
This is a Guild command which only players who are members
of a guild may use.
Stuff like:
fl auto duel follow loot stay
are in this category.
"""
def check_flag(self, player: Player):
if player.flag["guild"] is False:
print("Sorry, can't.")
return player.flag["guild"]
def toggle_follow_mode(self):
pass
@dataclass
class UserCmd(ParserCmd):
"""
This is a regular user command which anyone should be able to use.
Stuff like:
drink look more quote etc.
are in this category.
"""
pass
@dataclass
class Player(object):
name: str
hit_points: int
flag: {"dungeon_master": False,
"expert_mode": False,
}
@dataclass
class Room(object):
name: str
exits: dict
objects_here: dict
players_here: dict # {'id': 'name'}
def parser():
cmd_line = input("What now? ").lower().split()
cmd_base = cmd_line[0]
# argument count:
argc = len(cmd_line)
# check if command in list:
# TODO: check for regexes
for cmd in user_cmds:
if cmd["cmd_regex"] is True:
valid = regex.findall(pattern=cmd_base, string=cmd)
# get the type of in-game object(s) the command works with (Player, Item, Room, etc.):
cmd_object = cmd[1]
cmd_target = cmd_line[1]
if cmd_object == type(cmd_target):
print(f'{cmd_object} passed')
pass
# command tables:
# cmd_name, cmd_func, cmd_args (bool)
"""
this syntax works:
DOT_CMD_TABLE = {"a": DotCommand(dot_text="Abort",
dot_func=cmd_abort,
dot_flag="immediate",
dot_range=None,
dot_range_default=None)}
"""
admin_cmds = {"#": AdminCmd(cmd_regex=False,
cmd_func=AdminCmd.teleport,
cmd_args=Union[Room, Player]),
"boot": AdminCmd(cmd_regex=False,
cmd_func=AdminCmd.boot,
cmd_args=Player)}
user_cmds = {"[g|get]", UserCmd(cmd_regex=True,
cmd_func=UserCmd.get,
cmd_args=Item)}
guild_cmds = {"fl": GuildCmd(cmd_regex=False,
cmd_func=GuildCmd.toggle_follow_mode,
cmd_args=None)}
# regex to make ending of command optional: "rea(dy)?"
if __name__ == '__main__':
parser()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment