Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding list comprehensions #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 4 additions & 26 deletions mudserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,29 +135,14 @@ def get_new_players(self):
entered the game since the last call to 'update'. Each item in
the list is a player id number.
"""
retval = []
# go through all the events in the main list
for ev in self._events:
# if the event is a new player occurence, add the info to the list
if ev[0] == self._EVENT_NEW_PLAYER:
retval.append(ev[1])
# return the info list
return retval
return [ev[1] for ev in self._events if ev[0] == self._EVENT_NEW_PLAYER]

def get_disconnected_players(self):
"""Returns a list containing info on any players that have left
the game since the last call to 'update'. Each item in the list
is a player id number.
"""
retval = []
# go through all the events in the main list
for ev in self._events:
# if the event is a player disconnect occurence, add the info to
# the list
if ev[0] == self._EVENT_PLAYER_LEFT:
retval.append(ev[1])
# return the info list
return retval
return [ev[1] for ev in self._events if ev[0] == self._EVENT_PLAYER_LEFT]

def get_commands(self):
"""Returns a list containing any commands sent from players
Expand All @@ -167,14 +152,7 @@ def get_commands(self):
they typed), and another string containing the text after the
command
"""
retval = []
# go through all the events in the main list
for ev in self._events:
# if the event is a command occurence, add the info to the list
if ev[0] == self._EVENT_COMMAND:
retval.append((ev[1], ev[2], ev[3]))
# return the info list
return retval
return [(ev[1], ev[2], ev[3]) for ev in self._events if ev[0] == self._EVENT_COMMAND]

def send_message(self, to, message):
"""Sends the text in the 'message' parameter to the player with
Expand All @@ -183,7 +161,7 @@ def send_message(self, to, message):
"""
# we make sure to put a newline on the end so the client receives the
# message on its own line
self._attempt_send(to, message+"\n\r")
self._attempt_send(to, message + "\n\r")

def shutdown(self):
"""Closes down the server, disconnecting all clients and
Expand Down
11 changes: 2 additions & 9 deletions simplemud.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,8 @@
# send the player back the description of their current room
mud.send_message(id, rm["description"])

playershere = []
# go through every player in the game
for pid, pl in players.items():
# if they're in the same room as the player
if players[pid]["room"] == players[id]["room"]:
# ... and they have a name to be shown
if players[pid]["name"] is not None:
# add their name to the list
playershere.append(players[pid]["name"])
# make list of every player with a name that's in the same room as the current player
playershere = [players[pid]["name"] for pid, pl in players.items() if players[pid]["room"] == players[id]["room"] and players[pid]["name"] is not None]

# send player a message containing the list of players in the room
mud.send_message(id, "Players here: {}".format(
Expand Down