Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Hacksaar
Haxotel
Commits
89e0760c
Commit
89e0760c
authored
Aug 01, 2014
by
Ralf
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
more logging
parent
44bbf786
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
30 additions
and
3 deletions
+30
-3
game.py
game.py
+2
-1
labyrinth.py
labyrinth.py
+18
-0
main.py
main.py
+3
-1
player.py
player.py
+7
-1
No files found.
game.py
View file @
89e0760c
...
...
@@ -5,7 +5,7 @@ import labyrinth
import
random
class
Game
:
def
__init__
(
self
,
labyrinthFileName
,
logFileName
=
None
,
port
=
1234
):
def
__init__
(
self
,
labyrinthFileName
,
adminPW
,
logFileName
=
None
,
port
=
1234
):
self
.
players
=
[]
with
open
(
labyrinthFileName
,
'r'
)
as
f
:
self
.
labyrinth
=
labyrinth
.
Labyrinth
(
stream
=
f
)
...
...
@@ -15,6 +15,7 @@ class Game:
self
.
sel
=
selectors
.
DefaultSelector
()
self
.
sel
.
register
(
self
.
sock
,
selectors
.
EVENT_READ
,
self
.
accept
)
self
.
logFile
=
None
if
logFileName
is
None
else
open
(
logFileName
,
'a'
)
self
.
adminPW
=
adminPW
# low-level functions
def
accept
(
self
,
sock
,
mask
):
...
...
labyrinth.py
View file @
89e0760c
...
...
@@ -2,6 +2,7 @@
from
enum
import
Enum
from
functools
import
reduce
from
player
import
Player
directions
=
{
"north"
:
(
-
1
,
0
),
"east"
:
(
0
,
1
),
"west"
:
(
0
,
-
1
),
"south"
:
(
1
,
0
)}
...
...
@@ -66,6 +67,23 @@ class Labyrinth:
tiles
.
append
(
row
)
return
tiles
def
writelab
(
self
,
stream
):
for
row
in
self
.
tiles
:
for
field
in
row
:
if
field
.
groundtype
==
Labyrinth
.
GroundType
.
FLOOR
:
print
(
"#"
,
file
=
stream
)
else
:
if
not
field
.
things
:
print
(
"."
,
file
=
stream
)
elif
len
(
field
.
things
)
==
1
:
if
isinstance
(
field
.
things
[
0
],
Player
):
print
(
"P"
,
file
=
stream
)
else
:
print
(
"?"
,
file
=
stram
)
else
:
print
(
"+"
,
file
=
stream
)
print
(
"
\n
"
,
file
=
stream
)
def
getWidth
(
self
):
return
len
(
self
.
tiles
[
0
])
...
...
main.py
View file @
89e0760c
...
...
@@ -10,9 +10,11 @@ parser.add_argument("-l", "--labyrinth-file", required=True, dest="labyrinthFile
help
=
"Name of labyrinth file"
)
parser
.
add_argument
(
"--log-file"
,
dest
=
"logFileName"
,
help
=
"Name of log file"
)
parser
.
add_argument
(
"--admin-password"
,
dest
=
"adminPW"
,
help
=
"Password for the admin"
)
args
=
parser
.
parse_args
()
game
=
Game
(
labyrinthFileName
=
args
.
labyrinthFileName
,
port
=
args
.
port
,
logFileName
=
args
.
logFileName
)
game
=
Game
(
labyrinthFileName
=
args
.
labyrinthFileName
,
port
=
args
.
port
,
logFileName
=
args
.
logFileName
,
adminPW
=
adminPW
)
try
:
game
.
run
()
...
...
player.py
View file @
89e0760c
...
...
@@ -6,12 +6,17 @@ goVerbs = ['go', 'walk']
sayVerbs
=
[
'say'
,
'talk'
,
'scream'
]
leaveVerbs
=
[
'leave'
,
'quit'
,
'exit'
,
'suicide'
]
playerUID
=
0
class
Player
(
Thing
):
def
__init__
(
self
,
game
,
conn
):
super
().
__init__
()
self
.
buffer
=
b
""
self
.
game
=
game
self
.
conn
=
conn
self
.
uid
=
playerUID
playerUID
+=
1
log_stdout
(
"New player: {0}"
.
format
(
self
.
uid
))
# low-level functions
def
send
(
self
,
data
,
end
=
'
\n
'
):
...
...
@@ -20,6 +25,7 @@ class Player(Thing):
def
close
(
self
):
# only to be called by Game - it has to remove us from its list
self
.
conn
.
close
()
log_stdout
(
"Player left: {0}"
.
format
(
self
.
uid
))
return
self
.
conn
def
read
(
self
,
conn
,
mask
):
...
...
@@ -45,7 +51,7 @@ class Player(Thing):
# high(er)-level functions
def
readCmd
(
self
,
cmd
):
self
.
game
.
log
(
"Someone wrote '{0}'"
.
format
(
cmd
))
self
.
game
.
log
(
"Someone
[{1}]
wrote '{0}'"
.
format
(
cmd
,
self
.
uid
))
words
=
cmd
.
lower
().
split
()
if
not
words
:
self
.
send
(
"What did you mean?"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment