-
Notifications
You must be signed in to change notification settings - Fork 8
/
cmdclient.py
48 lines (41 loc) · 1.35 KB
/
cmdclient.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
import sys,traceback
from game import Game
from command import do_cmdstr,find_cmd
import pprint
from cfg import Config
class Client(object):
def __init__(self, srv_addr):
self.game = Game(srv_addr)
def docmd(self, tokens):
cmdname = tokens[0]
ok,similars = find_cmd(cmdname)
if not ok:
print "cmd not found:", cmdname
print "Did you mean this?"
print "\t", ", ".join(similars)
return
result = do_cmdstr(self.game, cmdname, tokens[1] if len(tokens) > 1 else "")
if result != None:
pprint.pprint(result)
def interact(self):
if "uid" in Config:
self.docmd(["login", Config["uid"]])
while True:
try:
sys.stdout.write(Config["client_prompt"])
sys.stdout.flush()
l = sys.stdin.read(1)
if not l: # eof
print "exit on EOF"
exit(0)
if l == '\n':
continue
l += sys.stdin.readline()
l.strip()
tokens = l.split(None, 1)
if not tokens:
continue
self.docmd(tokens)
except Exception as e:
print "error occured", e, traceback.format_exc()
continue