Compare commits

...

2 Commits

Author SHA1 Message Date
Marlow Alfonso fc7051a9af refactor: reduced handle_input() cyclomatic complexity
Reformated the handle_input()'s elif chain with a dict lookup
2023-11-21 23:01:52 +00:00
Marlow Alfonso 13c2d9b149 chore: cleaned the code for readability according to the python-lsp-server 2023-11-20 22:06:06 +00:00
2 changed files with 143 additions and 94 deletions

View File

@ -1,39 +1,48 @@
from mudserver import MudServer as mud
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")
def say(mud, id, players, params):
for pid, pl in players.items():
if players[pid]["room"] == players[id]["room"]:
mud.send_message(pid, "{} says: {}".format(players[id]["name"], params))
mud.send_message(pid, "{} says: {}"
.format(players[id]["name"], params))
def go(mud, id, players, rooms, params):
old_room = players[id]["room"]
params_list = params.split()
if len(params_list) != 1:
mud.send_message(id, "Invalid params")
mud.send_message(id, "Invalid params.")
return
exit = params_list[0]
room = rooms[players[id]["room"]]
if exit in room["exits"]:
if exit in room["exits"]:
for pid, pl in players.items():
if pl["room"] == players[id]["room"] and pid != id:
mud.send_message(pid, "{} left via exit '{}'".format(players[id]["name"], exit))
mud.send_message(pid, "{} left via exit '{}'."
.format(players[id]["name"], exit))
players[id]["room"] = room["exits"][exit]
mud.send_message(id, "You arrive at '{}'".format(players[id]["room"]))
mud.send_message(id, "You arrive at '{}'.".format(players[id]["room"]))
for pid, pl in players.items():
if pl["room"] == players[id]["room"] and pid != id:
mud.send_message(pid, "{} arrived from '{}' via exit '{}'".format(players[id]["name"], old_room, exit))
else:
mud.send_message(pid, "{} arrived from '{}' via exit '{}'."
.format(players[id]["name"], old_room, exit))
else:
mud.send_message(id, "Unknown exit '{}'".format(exit))
def look(mud, id, players, rooms, params):
params_list = params.split()
if len(params_list) > 1:
mud.send_message(id, "Invalid params")
mud.send_message(id, "Invalid params.")
return
if len(params_list) == 1:
for pid, pl in players.items():
@ -53,6 +62,18 @@ def look(mud, id, players, rooms, params):
continue
room_players.append(pl["name"])
mud.send_message(id, "Players here: {}".format(", ".join(room_players)))
mud.send_message(id, "Exits are: {}".format(", ".join(room["exits"])))
mud.send_message(id, "Players here: {}.".format(", ".join(room_players)))
mud.send_message(id, "Exits are: {}.".format(", ".join(room["exits"])))
def open(mud, id, players, rooms, params):
params_list = params.split()
if len(params_list) != 2:
mud.send_message(id, "Invalid params.")
return
if not players[id]["name"] in rooms[players[id]["room"]]["builders"]:
mud.send_message(id, "You don't have the rights of building here.")
return
rooms[players[id]["room"]]["exits"][params_list[0]] = params_list[1]
mud.send_message(id, "You open the exit '{}' to '{}'."
.format(params_list[0], params_list[1]))

194
run.py
View File

@ -1,23 +1,112 @@
#!/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",
"exits": {"north": "Bar"}
"exits": {"north": "Bar"},
"owner": "Dummy1",
"builders": ["Dummy1"]
},
"Bar": {
"description": "You are in the Bar",
"exits": {"outside": "Lobby"}
"exits": {"outside": "Lobby"},
"owner": "Dummy1",
"builders": ["Dummy1"]
}
}
players = {}
characters = {
"Dummy1": {
"password": "Dummy1",
@ -65,13 +154,12 @@ characters = {
"description": "A practice dummy"
}
}
waitlist = {}
mud = MudServer()
def add_player(id, name, species, description, room = "Lobby"):
def add_player(id, name, species, description, room="Lobby"):
players[id] = {
"logged": False,
"name": name,
"species": species,
"description": description,
@ -79,6 +167,7 @@ def add_player(id, name, species, description, room = "Lobby"):
}
print("{} connected as '{}'".format(id, name))
def update():
# Update the server
time.sleep(0.2)
@ -100,87 +189,26 @@ Type help for a list of commands
print("{}('{}') disconnected".format(id, players[id]["name"]))
for pid, pl in players.items():
mud.send_message(pid, "{} quit the game".format(players[id]["name"]))
mud.send_message(pid, "{} quit the game"
.format(players[id]["name"]))
del players[id]
del(players[id])
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)
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()