refactor: reduced handle_input() cyclomatic complexity

Reformated the handle_input()'s elif chain with a dict lookup
This commit is contained in:
Marlow Alfonso 2023-11-21 23:01:52 +00:00
parent 13c2d9b149
commit fc7051a9af
2 changed files with 101 additions and 82 deletions

View File

@ -1,3 +1,8 @@
def unknown(mud, command, id):
mud.send_message(id, "Unknown command '{}'"
.format(command))
def help(mud, id):
mud.send_message(id, "Here should be a list with commands")

178
run.py
View File

@ -1,9 +1,97 @@
#!/usr/bin/env python
import time
# import json
from mudserver import MudServer
import commands as cmd
players = {}
waitlist = {}
def cmd_help(id, _params):
cmd.help(mud, id)
def cmd_go(id, params):
cmd.go(mud, id, players, rooms, params)
def cmd_say(id, params):
cmd.say(mud, id, players, params)
def cmd_look(id, params):
cmd.look(mud, id, players, rooms, params)
def cmd_open(id, params):
cmd.open(mud, id, players, rooms, params)
def cmd_whelp(id, params):
if params:
pass
else:
mud.send_message(id, '''
You can see a full list of available commands typing:
help commands
You can log in to your character typing:
connect <name> <password>
''')
def cmd_wconnect(id, params):
params_list = params.split()
if len(params_list) != 2:
mud.send_message(id, "Invalid params")
return
if len(params_list) != 2:
mud.send_message(id, "Invalid params")
for name, pl in characters.items():
if params_list[0] != name:
continue
if params_list[1] != pl["password"]:
mud.send_message(id, "Wrong password")
continue
for pid, pl in players.items():
if name in pl["name"]:
mud.send_message(id, "That character is already connected")
break
else:
add_player(
id, name, pl["species"], pl["description"])
waitlist.pop(id)
mud.send_message(id, "Connected as {} the {}."
.format(players[id]["name"],
players[id]["species"]))
mud.send_message(id, rooms[players[id]["room"]]["description"])
break
else:
found = False
for name, pl in characters.items():
if params_list[0] == name:
found = True
if not found:
mud.send_message(
id, "Character '{}' not found"
.format(params_list[0]))
def cmd_unknown(id, command):
cmd.unknown(mud, command, id)
commands = {
"help": cmd_help,
"look": cmd_look,
"go": cmd_go,
"say": cmd_say,
"open": cmd_open
}
waitlist_commands = {
"help": cmd_whelp,
"connect": cmd_wconnect
}
rooms = {
"Lobby": {
"description": "You are in the Lobby",
@ -19,9 +107,6 @@ rooms = {
}
}
players = {}
waitlist = {}
characters = {
"Dummy1": {
"password": "Dummy1",
@ -75,7 +160,6 @@ mud = MudServer()
def add_player(id, name, species, description, room="Lobby"):
players[id] = {
"logged": False,
"name": name,
"species": species,
"description": description,
@ -113,88 +197,18 @@ Type help for a list of commands
def handle_input():
for id, command, params in mud.get_commands():
execute = None
if id in waitlist:
if command == "help":
if params:
pass
else:
mud.send_message(id, '''
You can see a full list of available commands typing:
help commands
You can log in to your character typing:
connect <name> <password>
''')
if command == "connect":
if params:
params_list = params.split()
if len(params_list) != 2:
mud.send_message(id, "Invalid params")
continue
for name, pl in characters.items():
if params_list[0] != name:
continue
if params_list[1] != pl["password"]:
mud.send_message(id, "Wrong password")
continue
for pid, pl in players.items():
if name in pl["name"]:
mud.send_message(
id, "That character is already connected")
break
else:
add_player(
id, name, pl["species"], pl["description"])
waitlist.pop(id)
break
else:
found = False
for name, pl in characters.items():
if params_list[0] == name:
found = True
if not found:
mud.send_message(
id, "Character '{}' not found"
.format(params_list[0]))
else:
mud.send_message(id, "Unknown command " + command)
if id not in players:
continue
# login message
if command == "connect" and not players[id]["logged"]:
mud.send_message(id, "Logged in as {} the {}".format(
players[id]["name"], players[id]["species"]))
rm = rooms[players[id]["room"]]
mud.send_message(id, rm["description"])
players[id]["logged"] = True
# 'help' command
elif command == "help":
cmd.help(mud, id)
# 'say' command
elif command == "say":
cmd.say(mud, id, players, params)
# 'look' command
elif command == "look":
cmd.look(mud, id, players, rooms, params)
# 'go' command
elif command == "go":
cmd.go(mud, id, players, rooms, params)
# 'open' command
elif command == "open":
cmd.open(mud, id, players, rooms, params)
execute = waitlist_commands.get(command, None)
elif id in players:
execute = commands.get(command, None)
if execute:
execute(id, params)
else:
mud.send_message(id, "Unknown command '{}'".format(command))
cmd_unknown(id, command)
# Game loop
while True:
update()
handle_input()