Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement initiative ranking command #7

Merged
merged 5 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from abc import ABC, abstractmethod
from textwrap import dedent
from npc import NPC, NPCList, findList

from src.npc import NPC, NPCList, findList


class Command(ABC):
Expand Down Expand Up @@ -707,6 +708,40 @@ def execute(self, args=[]) -> None:
self.usage()


class rank(Command):
def __init__(self, encounter: NPCList):
super().__init__()
self.names = ['rank', 'initiative']
self.encounter = encounter
self.description = "Orders NPCs by value."
self.details = dedent("""\
NPCs order within the encounter will be determined by their rank.
NPCs with a higher value will appear higher in the list.
This command can also be called with the alias "initiative".\
""").strip().replace('\n', ' ').replace('\r', '')
self.usageStr = "rank <encounter_index,...> <rank>"

def execute(self, args=[]) -> None:
if (len(self.encounter) < 1):
self.encounterEmpty()
return

if len(args) == 2:
if not isValidInt(args[0], self.encounter) or not isInt(args[1]):
self.usage()
return

rank = int(args[1])
npc = self.encounter.data[int(args[0]) - 1]
if npc.currentHP > 0:
npc.currentRank = rank
npc.maxRank = rank

self.encounter.data.sort(reverse = True)
else:
self.usage()


if __name__ == "__main__":
print("Something seems wrong, this file is not meant to be executed.")
print("Did you mean to run encounter instead?")
31 changes: 14 additions & 17 deletions src/encounter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import commands as cmd
from npc import NPCList
import src.commands as cmd
from src.npc import NPCList


def initialize_commands() -> list[cmd.Command]:
Expand All @@ -23,6 +23,7 @@ def initialize_commands() -> list[cmd.Command]:
commands.append(cmd.name(encounter))
commands.append(cmd.mark(encounter))
commands.append(cmd.unmark(encounter))
commands.append(cmd.rank(encounter))
commands.append(cmd.displayHelp(commands))
return commands

Expand All @@ -39,27 +40,23 @@ def main():
print("Type help or ? to get a list of availible commands.")

while True:
usrRequest = input(prompt).split(" ")
prompt = "\ncmd: "
userInput = input(prompt).split(" ")
userInput = [token for token in userInput if not token.isspace() and not token == '']

action = None
if not len(userInput) > 0:
prompt = "\nType a command: "
continue
else:
prompt = "\ncmd: "

if usrRequest != ['']:
action = usrRequest[0].lower()

if action in ['quit', 'q', 'exit']:
userCommand = userInput.pop(0).lower()
if userCommand in ['quit', 'q', 'exit']:
break

args = []

if (len(usrRequest) > 1):
for index in range(1, len(usrRequest)):
args.append(usrRequest[index])

found = False
for command in commands:
if action in command.names:
command.execute(args)
if userCommand in command.names:
command.execute(userInput)
found = True
break

Expand Down
16 changes: 15 additions & 1 deletion src/npc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from typing import Optional


class NPC:
REQUIRED_PARAMETERS = 3

Expand Down Expand Up @@ -34,6 +37,7 @@ def __init__(self, name: str, maxHP: int, ac: int, nick: str | None = None):
self.nick = nick
self.maxHP = self.currentHP = maxHP
self.ac = int(ac)
self.maxRank = self.currentRank = 0

def __str__(self):
output = ""
Expand All @@ -47,10 +51,16 @@ def __str__(self):

if self.currentHP <= 0:
output += " [X]"
else:
if self.currentRank > 0:
output = "(" + str(self.currentRank) + ") " + output

return output

def equals(self, other):
def __lt__(self, other):
return self.currentRank < other.currentRank

def equals(self: "NPC", other: Optional["NPC"]) -> bool:
if self == other:
return True
if other is None:
Expand All @@ -67,6 +77,10 @@ def equals(self, other):
return False
if self.note != other.note:
return False
if self.maxRank != other.maxRank:
return False
if self.currentRank != other.currentRank:
return False
return True

def combatStatus(self) -> str:
Expand Down
Loading