-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
127 lines (95 loc) · 2.84 KB
/
player.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
121
122
123
124
125
126
127
# -*- coding: utf8 -*-
import sys
import getpass
import utils
import gender
import species
import character
from Model import Model
import datetime
class player(character.character):
def __init__(self, login, password):
self._login = login
self._password = password
self._model = None
def isConnected(self):
return self._model is not None
#~ Connect the player by asking him to enter his login and his password
def loadPlayerFromStdIn(self):
if self._login is None or self._password is None:
self._readLoginAndPassword(False, False)
#~ Method to connect the player
def connect(self):
self._model = model.loadByLoginAndPassword(self._login, self._password)
if len(self._model) == 0:
self._login = None
self._password = None
raise exception("Invalid login or password")
return True
#~ Read the login and the password from stdin
def _readLoginAndPassword(self, checkLogin, confirmPassword):
while self._login is None or self._login == '':
self._login = utils.read("Login: ")
if checkLogin and len(model.loadBy({'login': self._login})):
print('This login is already used')
self._login = None
confirmPassword = ''
while (self._password is None or self._password == ''):
self._password = getpass.getpass("Password: ")
if confirmPassword:
confirmPassword = getpass.getpass("Confirm password: ")
else:
confirmPassword = self._password
if self._password != confirmPassword:
print('The passwords do not match')
self._password = None
def createNewPlayerFromStdIn(self):
self._readLoginAndPassword(True, True)
genders = gender.model.loadAll()
nbGenders = len(genders)
for k, v in enumerate(genders):
print(v['name'] + " (" + str(k) + ")")
g = -1
while g < 0 or g >= nbGenders:
g = utils.read("Character gender: ")
try:
g = int(g)
except:
g = -1
genderId = genders[g]['id_gender']
sps = species.model.getSpecies(genders[g]['name'])
nbSpecies = len(sps)
for k, v in enumerate(sps):
print(v['name'] + " (" + str(k) + ")")
print(v['description'])
sp = -1
while sp < 0 or sp >= nbSpecies:
sp = utils.read("Character species: ")
try:
sp = int(sp)
except:
sp = -1
speciesId = sps[sp]['id_species']
self._model = {
'login': self._login,
'name': self._login,
'password': self._password,
'id_species': speciesId,
'id_gender': genderId,
'id_area': 1
}
self._model['id_character'] = character.model.insert(self._model)
model.insert(self._model)
class model(character.model):
fields = ['id_player', 'login', 'password', 'id_character']
@staticmethod
def loadByLoginAndPassword(login, password):
pm = model.loadBy({'login': login, 'password': password})
if len(pm) == 0:
return dict()
pm = pm[0]
cm = character.model.loadById(pm['id_character'])
cm.update(pm)
return cm
class exception(BaseException):
pass