-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsimplemud.py
executable file
·103 lines (80 loc) · 3.33 KB
/
simplemud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
"""A simple Multi-User Dungeon (MUD) game. Players can talk to each
other, examine their surroundings and move between rooms.
Some ideas for things to try adding:
* More rooms to explore
* An 'emote' command e.g. 'emote laughs out loud' -> 'Mark laughs
out loud'
* A 'whisper' command for talking to individual players
* A 'shout' command for yelling to players in all rooms
* Items to look at in rooms e.g. 'look fireplace' -> 'You see a
roaring, glowing fire'
* Items to pick up e.g. 'take rock' -> 'You pick up the rock'
* Monsters to fight
* Loot to collect
* Saving players accounts between sessions
* A password login
* A shop from which to buy items
author: Mark Frimston - [email protected]
"""
import logging
import sys
import time
from game_data import rooms
from mudserver import MudServer
from lib.constants import DEFAULT_START_LOCATION
from lib.command import Commands
from lib.models.game_state import GameState
from lib.models.player import Player
game = GameState(MudServer())
commands = Commands(game)
log = logging.getLogger()
def init_logging():
formatter = logging.Formatter("%(asctime)s [%(name)s] %(levelname)s: "\
"%(message)s")
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(formatter)
log.addHandler(consoleHandler)
def handle_new_player(player: Player):
# Any new command event will become the player's name.
player.name = command.capitalize()
game.broadcast(f"{player.name} entered the game.")
player.message(f"Welcome to the game, {player.name}.")
player.message("Type 'help' for a list of commands. Have fun!")
player.move(DEFAULT_START_LOCATION)
if __name__ == '__main__':
init_logging()
log.setLevel(logging.DEBUG)
log.info("Starting up...")
try:
# main game loop. We loop forever (i.e. until the program is terminated)
while True:
# pause for 1/5 of a second on each loop, so that we don't constantly
# use 100% CPU time
time.sleep(0.2)
# 'update' must be called in the loop to keep the game running and give
# us up-to-date information
game.update()
# go through any newly connected players
new_players = game.handle_player_join()
# go through any recently disconnected players
game.handle_player_leave()
# go through any new commands sent from players
for event in game.server.get_commands():
client = event.client
command = event.command
params = event.params
player = game.find_player_by_client_id(client.uuid)
# if for any reason the player isn't in the player map, skip them and
# move on to the next one
if not player:
continue
if player.name is None:
handle_new_player(player)
continue
# each of the possible commands is handled below. Try adding new
# commands to the game!
commands.execute_command(player, command, params)
except KeyboardInterrupt:
log.info("Terminated by user. Exiting gracefully.")
sys.exit(1)