chore: cleaned the code for readability according to the python-lsp-server

This commit is contained in:
Marlow Alfonso 2023-11-20 22:06:06 +00:00
parent fee6f735ed
commit 13c2d9b149
2 changed files with 63 additions and 33 deletions

View File

@ -1,39 +1,43 @@
from mudserver import MudServer as mud
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 +57,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]))

56
run.py
View File

@ -1,23 +1,27 @@
#!/usr/bin/env python
import time
import json
# import json
from mudserver import MudServer
import commands as cmd
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 = {}
waitlist = {}
characters = {
"Dummy1": {
"password": "Dummy1",
@ -65,11 +69,11 @@ 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,
@ -79,6 +83,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,18 +105,19 @@ 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():
if id in waitlist:
if command == "help":
if params:
pass
else:
pass
else:
mud.send_message(id, '''
You can see a full list of available commands typing:
help commands
@ -132,29 +138,34 @@ You can log in to your character typing:
continue
for pid, pl in players.items():
if name in pl["name"]:
mud.send_message(id, "That character is already connected")
mud.send_message(
id, "That character is already connected")
break
else:
add_player(id, name, pl["species"], pl["description"])
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]))
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"]))
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
@ -175,12 +186,15 @@ You can log in to your character typing:
elif command == "go":
cmd.go(mud, id, players, rooms, params)
# 'open' command
elif command == "open":
cmd.open(mud, id, players, rooms, params)
else:
mud.send_message(id, "Unknown command '{}'".format(command))
# Game loop
while True:
update()
handle_input()