[go: up one dir, main page]

Menu

[r61]: / mensajero.py  Maximize  Restore  History

Download this file

85 lines (75 with data), 3.1 kB

 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
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
# MUnDoCAAD MUD Engine
# Copyright (C) 2002-2008 José Manuel Ferrer Ortiz
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License version
# 3, as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Affero General Public License version 3 for more details.
#
# You should have received a copy of the GNU Affero General Public
# License version 3 along with this program. If not, see
# <http://www.gnu.org/licenses/>.
import codecs
import cPickle
import os
import archivero
# Convierte un carácter no codificable a una versión plana del mismo
def aplana (unierr):
if unierr.end == unierr.start + 1: # Un carácter de longitud
if unierr.object[unierr.start] == u'\u20ac': # Símbolo del euro
return (u'EUR', unierr.end)
if unierr.object[unierr.start] == u'\u0178': # 'Y' con diéresis
return (u'Y', unierr.end)
print unierr
print 'Inicio:', unierr.start, 'Fin:', unierr.end, '->', unierr.object
return (u'', unierr.end) # Omitimos el error
# Registramos la función aplana como manejadora de errores de codificación
codecs.register_error ('aplana', aplana)
# Envía un mensaje a una lista de personajes
def enviaMensaje (receptores, contenido, nl_inicial = True, prompt = True):
if not contenido or not receptores:
return
for personaje in receptores:
if personaje not in jugadores:
print 'Intento de envío de mensaje a un personaje (' + personaje + \
') no conectado' # Eliminar cuando haya PNJs en el MUD
continue
mensaje = contenido + '\r\n'
if personaje in dCodecs: # Requiere conversión de codificación
mensaje = mensaje.decode ('iso8859-15').encode (dCodecs[personaje],
'aplana')
if personaje not in noprompt:
if nl_inicial:
mensaje = '\r\n' + mensaje
if prompt:
mensaje += '> '
jugadores[personaje].buffer_escr += mensaje
# Inicializa el módulo
def inicializa ():
global jugadores
global dCodecs
global noprompt
jugadores = dict() # Lista de jugadores conectados y sus conexiones
fichero = None
try:
fichero = open (os.path.join (archivero.dir_datos, 'personajes.cfg'), 'r')
dCodecs = cPickle.load (fichero) # Personajes con otras codificaciones
noprompt = cPickle.load (fichero) # Personajes que no quieren prompt
except: # No existe el fichero o está corrupto
# Creamos en su lugar una configuración por defecto
dCodecs = dict()
noprompt = set()
if fichero:
fichero.close()
# Salva a disco la configuración de los personajes
def salvaConfig ():
# Omitimos el close() porque el MUD se cerrará a continuación
fichero = open (os.path.join (archivero.dir_datos, 'personajes.cfg'), 'wb')
cPickle.dump (dCodecs, fichero)
cPickle.dump (noprompt, fichero)