-
Notifications
You must be signed in to change notification settings - Fork 1
/
playerconnections.py
347 lines (319 loc) · 12.6 KB
/
playerconnections.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
__filename__ = "playerconnections.py"
__author__ = "Bob Mottram"
__credits__ = ["Bob Mottram"]
__license__ = "AGPL3+"
__version__ = "1.0.0"
__maintainer__ = "Bob Mottram"
__email__ = "[email protected]"
__status__ = "Production"
__module_group__ = "Core"
import os
import time
from functions import log
from functions import save_state
from functions import load_players_db
from functions import deepcopy
MAXIMUM_PLAYERS = 128
def _run_new_player_connections(mud, players: {}, players_db: {}, fights: {},
config):
# go through any newly connected players
for id in mud.get_new_players():
if len(players) >= MAXIMUM_PLAYERS:
mud.send_message(id, "Player limit reached\n\n")
mud.handle_disconnect(id)
continue
# add the new player to the dictionary, noting that they've not been
# named yet.
# The dictionary key is the player's id number. We set their room to
# None initially until they have entered a name
# Try adding more player stats - level, gold, inventory, etc
players[id] = {
'name': None,
'prefix': None,
'room': None,
'lvl': None,
'exp': None,
'str': None,
'siz': None,
'wei': None,
'per': None,
'endu': None,
'cha': None,
'int': None,
'agi': None,
'luc': None,
'cool': None,
'cred': None,
'pp': None,
'ep': None,
'cp': None,
'sp': None,
'gp': None,
'inv': None,
'authenticated': None,
'speakLanguage': None,
'language': None,
'clo_head': None,
'clo_neck': None,
'clo_larm': None,
'clo_rarm': None,
'clo_lhand': None,
'clo_rhand': None,
'clo_gloves': None,
'clo_lfinger': None,
'clo_rfinger': None,
'clo_waist': None,
'clo_lear': None,
'clo_rear': None,
'clo_lwrist': None,
'clo_rwrist': None,
'clo_chest': None,
'clo_back': None,
'clo_lleg': None,
'clo_rleg': None,
'clo_feet': None,
'imp_head': None,
'imp_neck': None,
'imp_larm': None,
'imp_rarm': None,
'imp_lhand': None,
'imp_rhand': None,
'imp_gloves': None,
'imp_lfinger': None,
'imp_rfinger': None,
'imp_waist': None,
'imp_lear': None,
'imp_rear': None,
'imp_chest': None,
'imp_back': None,
'imp_lleg': None,
'imp_rleg': None,
'imp_feet': None,
'hp': None,
'charge': None,
'isInCombat': None,
'lastCombatAction': None,
'isAttackable': None,
'lastRoom': None,
'corpseTTL': None,
'canSay': None,
'canGo': None,
'canLook': None,
'canAttack': None,
'canDirectMessage': None,
'inDescription': None,
'outDescription': None,
'lookDescription': None,
'idleStart': int(time.time()),
'channels': None,
'permissionLevel': None,
'defaultChannel': None,
'exAttribute0': None,
'exAttribute1': None,
'exAttribute2': None,
'ref': None,
'bodyType': None,
'race': None,
'characterClass': None,
'proficiencies': None,
'fightingStyle': None,
'restRequired': None,
'enemy': None,
'magicShield': 0,
'magicShieldDuration': 0,
'magicShieldStart': 0,
'tempCharm': None,
'tempCharmTarget': None,
'tempCharmDuration': None,
'tempCharmStart': None,
'guild': None,
'guildRole': None,
'archetype': None,
'preparedSpells': None,
'spellSlots': None,
'tempHitPoints': 0,
'tempHitPointsStart': 0,
'tempHitPointsDuration': 0,
'prepareSpell': None,
'prepareSpellProgress': None,
'prepareSpellTime': None,
'frozenDescription': None,
'frozenStart': 0,
'frozenDuration': 0,
'affinity': None,
'familiar': None
}
# Read in the MOTD file and send to the player
filename = str(config.get('System', 'Motd'))
try:
with open(filename, "r",
encoding='utf-8') as motd_file:
motd_lines = motd_file.readlines()
except OSError:
print('EX: _run_new_player_connections 1 ' + filename)
for file_line in motd_lines:
line_str = file_line[:-1]
if line_str.strip().startswith('images/'):
motd_image_file = line_str.strip()
if os.path.isfile(motd_image_file):
try:
with open(motd_image_file, 'r',
encoding='utf-8') as fp_img:
mud.send_image(id, '\n' + fp_img.read(), True)
except OSError:
print('EX: _run_new_player_connections 1 ' +
motd_image_file)
else:
mud.send_message(id, line_str)
if not os.path.isfile(".disableRegistrations"):
mud.send_message_wrap(
id, '<f220>',
'You can create a new Character, or use the guest account, ' +
'username: <f32>Guest<r>, password: <f32>Password<r>')
mud.send_message(
id, "<r>What is your username? " +
"(type <f32>new<r> for new character)\n")
else:
mud.send_message(
id,
'<f0><b220> New account registrations are currently closed')
mud.send_message(id, "\n<r>What is your username?\n\n")
id_str = str(id)
log("Player ID " + id_str + " has connected", "info")
def _run_player_disconnections(mud, players: {}, players_db: {}, fights: {},
config, terminal_mode: {}):
# go through any recently disconnected players
for id in mud.get_disconnected_players():
# if for any reason the player isn't in the player map, skip them and
# move on to the next one
if id not in players:
continue
id_str = str(id)
terminal_mode[id_str] = False
name_str = str(players[id]['name'])
log("Player ID " + id_str + " has disconnected [" +
name_str + "]", "info")
# go through all the players in the game
for pid, plyr in players.items():
# send each player a message to tell them about the diconnected
# player if they are in the same room
if plyr['authenticated'] is None:
continue
if plyr['authenticated'] is not None \
and plyr['room'] == players[id]['room'] \
and plyr['name'] != players[id]['name']:
mud.send_message(
pid, "<f32><u>{}<r>".format(players[id]['name']) +
"'s body has vanished.\n\n")
# Code here to save player to the database after he's disconnected and
# before removing him from players dictionary
if players[id]['authenticated'] is not None:
log("Player disconnected, saving state", "info")
save_state(players[id], players_db, False)
players_db = load_players_db()
# Create a deep copy of fights, iterate through it and remove fights
# disconnected player was taking part in
fights_copy = deepcopy(fights)
for fight, _ in fights_copy.items():
if fights_copy[fight]['s1'] == players[id]['name'] or \
fights_copy[fight]['s2'] == players[id]['name']:
del fights[fight]
# remove the player's entry in the player dictionary
del players[id]
def run_player_connections(mud, players: {}, players_db: {},
fights: {}, config, terminal_mode: {}):
_run_new_player_connections(mud, players, players_db, fights, config)
_run_player_disconnections(mud, players, players_db, fights,
config, terminal_mode)
def disconnect_idle_players(mud, players: {}, allowed_player_idle: int,
players_db: {}) -> bool:
# Evaluate player idle time and disconnect if required
authenticated_players_disconnected = False
now = int(time.time())
players_copy = deepcopy(players)
for plyr_id, plyr in players_copy.items():
if now - plyr['idleStart'] <= allowed_player_idle:
continue
if players[plyr_id]['authenticated'] is not None:
mud.send_message_wrap(
plyr_id, "<f232><b11>",
"<f232><b11>Your body starts tingling. " +
"You instinctively hold your hand up to " +
"your face and notice you slowly begin to " +
"vanish. You are being disconnected due " +
"to inactivity...****DISCONNECT****\n")
save_state(players[plyr_id], players_db, False)
authenticated_players_disconnected = True
else:
mud.send_message(
plyr_id, "<f232><b11>You are being disconnected " +
"due to inactivity. Bye!****DISCONNECT****\n")
name_str = str(players[plyr_id]['name'])
if str(plyr_id).isdigit():
name_str += ' ID ' + str(plyr_id)
log("Character " + name_str +
" is being disconnected due to inactivity.", "warning")
p_str = str(plyr_id)
log("Disconnecting client " + p_str, "warning")
del players[plyr_id]
mud.handle_disconnect(plyr_id)
return authenticated_players_disconnected
def player_in_game(id: int, username: str, players: {}) -> bool:
""" is the given player already logged in?
"""
for plyr_id, plyr in players.items():
if username is not None and \
plyr['name'] is not None and \
username == plyr['name'] and \
plyr_id != id:
return True
return False
def initial_setup_after_login(mud, id: int, players: {},
loaded_json: {}) -> None:
"""Sets up a player after login
"""
players[id] = loaded_json.copy()
plyr = players[id]
plyr['authenticated'] = True
plyr['prefix'] = "None"
plyr['isInCombat'] = 0
plyr['lastCombatAction'] = int(time.time())
plyr['isAttackable'] = 1
plyr['corpseTTL'] = 60
plyr['idleStart'] = int(time.time())
id_str = str(id)
log("Player ID " + id_str +
" has successfully authenticated user " +
plyr['name'], "info")
# print(plyr)
# go through all the players in the game
for pid, plyr in players.items():
# send each player a message to tell them about the new
# player
if plyr['authenticated'] is not None \
and plyr['room'] == plyr['room'] \
and plyr['name'] != plyr['name']:
mud.send_message(
pid, '{} '.format(plyr['name']) +
'has materialised out of thin air nearby.\n\n')
# send the new player a welcome message
mud.send_message_wrap(id, '<f255>',
'****CLEAR****<f220>Welcome to AberMUSH!, ' +
'{}. '.format(plyr['name']))
mud.send_message_wrap(id, '<f255>',
'<f255>Hello there traveller! ' +
'You have connected to the ' +
'server. You can move around the ' +
'rooms along with other players, ' +
'attack each other (including NPCs), ' +
'pick up and drop items and chat. ' +
'Make sure to visit the repo for ' +
'further info. Thanks for your ' +
'interest in AberMUSH.')
mud.send_message_wrap(id, '<f255>',
"<f255>Type '<r><f220>help<r><f255>' " +
"for a list of all currently implemented " +
"commands.")
mud.send_message_wrap(id, '<f255>',
"<f255>Type '<r><f220>look<r><f255>' " +
"to see what's around you.\n\n")