-
Notifications
You must be signed in to change notification settings - Fork 0
/
mud_gmcp.py
193 lines (162 loc) · 7.32 KB
/
mud_gmcp.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
import queue
import json
from mud_shared import log_error, log_info
from mud_comms import handle_disconnection
from mud_consts import Exits, RoomSectorType, DISCORD_APPLICATION_ID, DISCORD_URL
# Telnet constants - see https://tools.ietf.org/html/rfc854
TELNET_IAC = b'\xff'
TELNET_WILL = b'\xfb'
TELNET_WONT = b'\xfc'
TELNET_DO = b'\xfd'
TELNET_DONT = b'\xfe'
TELNET_SB = b'\xfa'
TELNET_SE = b'\xf0'
TELNET_GMCP = b'\xc9'
# Telnet commands, simplified to improvide code readability
TELNET_WILL_SUPPORT = TELNET_IAC + TELNET_WILL
TELNET_WONT_SUPPORT = TELNET_IAC + TELNET_WONT
TELNET_GMCP_ASK_SUPPORTED = TELNET_IAC + TELNET_DO + TELNET_GMCP
TELNET_GMCP_MSG_START = TELNET_IAC + TELNET_SB + TELNET_GMCP
TELNET_GMCP_MSG_END = TELNET_IAC + TELNET_SE
class PlayerGMCP:
def __init__(self, player):
self.player = player
self.output_queue = queue.Queue()
self.gmcp = True
def queue_message(self, package, message, data):
try:
data_str = json.dumps(data)
gmcp_msg = f"{package}.{message} {data_str}"
gmcp_msg_bytes = gmcp_msg.encode('utf-8')
telnet_cmd = TELNET_GMCP_MSG_START + gmcp_msg_bytes + TELNET_GMCP_MSG_END
self.output_queue.put(telnet_cmd)
except UnicodeEncodeError:
log_error(f"Failed to encode message for player {self.fd}")
except Exception as e: # This will catch any other types of exceptions
log_error(f"Unexpected error while adding GMCP message to output queue: {e}")
def update_status(self):
if self.player.loggedin is False:
return
status = {
'ac': self.player.character.get_AC(),
'alignment': self.player.character.alignment,
'character_name': self.player.name,
'class': "",
'con': self.player.character.con,
'dex': self.player.character.dex,
'experience_tnl': self.player.character.tnl - self.player.character.xp,
'experience_tnl_max': self.player.character.tnl,
'gold': self.player.character.gold,
'health': self.player.character.current_hitpoints,
'health_max': self.player.character.max_hitpoints,
'hitroll': self.player.character.get_hitroll(),
'int': self.player.character.int,
'level': self.player.character.level,
'mana': self.player.character.current_mana,
'mana_max': self.player.character.max_mana,
# 'opponent_name'
'race': self.player.character.race,
# 'room_exits': self.current_room.get_exit_names(),
'room_name': self.player.current_room.name,
'str': self.player.character.str,
'wis': self.player.character.wis
}
self.queue_message("Char", "Status", status)
vitals = {
'hp': self.player.character.current_hitpoints,
'maxhp': self.player.character.max_hitpoints,
'mp': self.player.character.current_mana,
'maxmp': self.player.character.max_mana,
'stamina': self.player.character.current_stamina,
'maxstamina': self.player.character.max_stamina,
'tnl': self.player.character.tnl - self.player.character.xp,
'tnlmax': self.player.character.tnl
}
self.queue_message("Char", "Vitals", vitals)
def get_room_info(self):
room = self.player.current_room
if room is None:
log_error(f"PlayerGMCP: Player {self.player.name} has no current room!")
return {}
door_status = {}
for door in room.door_list:
door_status[Exits.get_name_by_value(door.door_number)] = "O" if door.locks == 0 else "C"
room = {
'details': {},
'environment': RoomSectorType.get_name_by_value(room.sector_type),
'exits': door_status,
'name': room.name,
'zone': room.area_number
}
return room
def update_room(self):
if self.player.loggedin is False:
return
self.queue_message("Room", "Info", self.get_room_info())
def handle_gmcp_negotiation(data, player):
# print("handle_telnet", str(data[:3]), TELNET_IAC + TELNET_WILL)
if data[:3] == TELNET_WILL_SUPPORT + TELNET_GMCP:
player.gmcp = PlayerGMCP(player)
log_info(f"Player {player.fd} supports GMCP")
player.gmcp.queue_message("External", "Discord.Info", {'inviteurl': DISCORD_URL, 'applicationid': DISCORD_APPLICATION_ID})
elif data[:3] == TELNET_WONT_SUPPORT + TELNET_GMCP:
player.gmcp = None
log_info(f"Player {player.fd} does not support GMCP")
# optional - check for out telnet options here
def handle_gmcp_message(data, player):
# Remove the IAC SB GMCP IAC SE bytes from the start and end of the message
gmcp_msg = data[3:-2]
# Decode the message from bytes to a string
gmcp_msg_str = gmcp_msg.decode('utf-8')
# Split the GMCP message into parts
parts = gmcp_msg_str.split(' ', 2)
try:
# Check if the first part contains a '.'
if '.' in parts[0]:
# Split the first part into package and message parts
package, message = parts[0].split('.', 1)
except ValueError:
package = None
message = None
return
# If the message contains data, parse it
if len(parts) == 2:
data = json.loads(parts[1])
process_gmcp_message(player, package, message, data)
log_info(f"Received GMCP message from player {player.fd}: {package} {message} {data}")
def process_gmcp_message(player, package, message, data):
if package == "Core":
if message == "Ping":
player.gmcp.queue_message("Core", "Ping", {})
elif message == "Hello":
if data is not None:
if "client" in data:
client = data["client"]
if "version" in data:
version = data["version"]
log_info(f"Player {player.fd} is using {client} version {version}")
elif package == "Char":
if message == "Status" or message == "Vitals":
player.gmcp.update_status()
elif package == "Room":
player.gmcp.update_room()
elif package == "External":
log_info("External message received")
process_gmcp_discord_message(player, message, data)
def process_gmcp_discord_message(player, message, data):
if message == "Discord.Hello":
log_info("Discord.Hello received")
player.queue_message("External", "Discord.Info", {'inviteurl': DISCORD_URL, 'applicationid': DISCORD_APPLICATION_ID})
def send_gmcp_messages(players):
for player in players:
if player.gmcp:
while not player.gmcp.output_queue.empty():
try:
# Get the next message from the queue
msg = player.gmcp.output_queue.get()
# Send the message
player.socket.send(msg)
except (BrokenPipeError, OSError):
handle_disconnection(player)
except Exception as e: # This will catch any other types of exceptions
log_error(f"Unexpected error while sending GMCP message: {e}")