feat: completed the main framework with some testing characters

This commit is contained in:
Marlow Alfonso 2023-11-17 03:11:03 +00:00
parent f18350242d
commit 58fb1280b1
2 changed files with 242 additions and 0 deletions

60
commands.py Normal file
View File

@ -0,0 +1,60 @@
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))
def go(mud, id, players, rooms, params):
params_list = params.split()
if len(params_list) != 1:
mud.send_message(id, "Invalid params")
return
exit = params_list[0]
room = rooms[players[id]["room"]]
if exit in room["exits"]:
for pid, pl in players.items():
if players[pid]["room"] == players[id]["room"] \
and pid != id:
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 " + players[id]["room"])
for pid, pl in players.items():
if players[pid]["room"] == players[id]["room"] \
and pid != id:
mud.send_message(pid,
"{} arrived via exit '{}'".format(players[id]["name"], exit))
else:
mud.send_message(id, "Unknown exit " + exit)
def look(mud, id, players, rooms, params):
params_list = params.split()
if len(params_list) > 1:
mud.send_message(id, "Invalid params")
return
if len(params_list) == 1:
for pid, pl in players.items():
if params_list[0] == "me":
mud.send_message(id, players[id]["description"])
break
elif params_list[0] == pl["name"]:
mud.send_message(id, pl["description"])
return
room = rooms[players[id]["room"]]
mud.send_message(id, room["description"])
room_players = []
for pid, pl in players.items():
if pl["room"] != players[id]["room"]:
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"])))

182
run.py Executable file
View File

@ -0,0 +1,182 @@
#!/usr/bin/env python
import time
import json
from mudserver import MudServer
import commands as cmd
rooms = {
"Lobby": {
"description": "You are in the Lobby",
"exits": {"north": "Bar"}
},
"Bar": {
"description": "You are in the Bar",
"exits": {"outside": "Lobby"}
}
}
players = {}
characters = {
"Dummy1": {
"password": "Dummy1",
"species": "dummy",
"room": "Lobby",
"description": "A practice dummy"
},
"Dummy2": {
"password": "Dummy2",
"species": "dummy",
"room": "Lobby",
"description": "A practice dummy"
},
"Dummy3": {
"password": "Dummy3",
"species": "dummy",
"room": "Lobby",
"description": "A practice dummy"
},
"Dummy4": {
"password": "Dummy4",
"species": "dummy",
"room": "Lobby",
"description": "A practice dummy"
},
"Dummy5": {
"password": "Dummy5",
"species": "dummy",
"room": "Lobby",
"description": "A practice dummy"
},
"Dummy6": {
"password": "Dummy6",
"species": "dummy",
"room": "Lobby",
"description": "A practice dummy"
},
"Dummy7": {
"password": "Dummy7",
"species": "dummy",
"room": "Lobby",
"description": "A practice dummy"
},
"Dummy8": {
"password": "Dummy8",
"species": "dummy",
"room": "Lobby",
"description": "A practice dummy"
},
"Dummy9": {
"password": "Dummy9",
"species": "dummy",
"room": "Lobby",
"description": "A practice dummy"
}
}
waitlist = {}
mud = MudServer()
def add_player(id, name, species, description, room = "Lobby"):
players[id] = {
"logged": False,
"name": name,
"species": species,
"description": description,
"room": room
}
def update():
# Update the server
time.sleep(0.2)
mud.update()
# Add new connected players to the waitlist
for id in mud.get_new_players():
waitlist[id] = id
mud.send_message(id, '''
Welcome to the UNIX.dog MUCK!!
Type help for a list of commands
''')
# Remove disconected players from the list
for id in mud.get_disconnected_players():
if id not in players:
continue
for pid, pl in players.items():
mud.send_message(pid, "{} quit the game".format(players[id]["name"]))
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:
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
add_player(id, name, pl["species"], pl["description"], pl["room"])
waitlist.pop(id)
break
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)
else:
mud.send_message(id, "Unknown command '{}'".format(command))
# Game loop
while True:
update()
handle_input()