-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRpg.py
120 lines (97 loc) · 2.73 KB
/
Rpg.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# -*- coding: utf8 -*-
from player import player
import command
import utils
import readline
class Rpg:
_debug = False
def __init__(self, login, password, action):
#~ if the game is launched with login/password,
#~ the player is directly fetched
if login is not None and password is not None:
self._player = player(login, password)
elif action == []:
#else an empty player is created
self._player = player(None, None)
self._doInteractiveAuth()
self._action = action
self._player.connect()
def setDebug(self, debug):
self._debug = debug
#~ This method asks the player to login or to create a new account
def _doInteractiveAuth(self):
choice = 0
while choice != '1' and choice != '2':
choice = utils.read("new account (1) or login (2) ? ")
if choice == '1':
self._player.createNewPlayerFromStdIn()
elif choice == '2':
self._player.loadPlayerFromStdIn()
#~ Main method of the Rpg Class, will run the action if it is given,
#~ else ask the player to enter a command
def run(self):
if len(self._action) > 0:
self._runAction()
else:
c = ''
result = 0
while 1:
try:
c = self.readCommand()
except KeyboardInterrupt:
print("")
continue
except EOFError:
print("")
continue
if c != "":
self._action = self.parseTypedAction(c)
result = self._runAction()
if result == command.quit:
break
def _runAction(self):
try:
c = command.factory.create(self._player, self._action)
if c != command.quit:
return c.run()
return c
except BaseException as e:
if self._debug:
import traceback
print(traceback.format_exc())
elif not isinstance(e, KeyboardInterrupt):
print(e)
def parseTypedAction(self, action):
inOption = False
commands, sep, option, optionStart = list(), ' ', '', 0
commandLen = len(action)
for k,i in enumerate(action):
# first letter of the option
if i != ' ' and not inOption:
#~ Set the start index of the option
optionStart = k
inOption = True
#~ Set the option delimiter
sep = i if i in ("'", '"') else ' '
if inOption:
#~ If the current char is the option delimiter, but not the
#~ stat one
if i == sep and k > optionStart:
#~ The option is ended
inOption = False
elif i != sep:
option += i
#~ The option is complete, append it in the list
if not inOption or k == commandLen - 1:
commands.append(option)
option = ''
return commands
def readCommand(self):
"""
Method to set the autocompleter and run the prompt, from utils
"""
completer = command.completer()
readline.set_completer(completer.complete)
readline.parse_and_bind('tab: complete')
readline.set_completer_delims('')
return utils.read("Command: ")