-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsubstrata_chatbot_demo.py
More file actions
290 lines (215 loc) · 9.8 KB
/
substrata_chatbot_demo.py
File metadata and controls
290 lines (215 loc) · 9.8 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
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
#
# substrata_chatbot_demo.py
# -------------------------
#
# Creates a user avatar, logs in, walks in circles, and says "Beep Boop" every 5 seconds.
# You could extend this example to make the bot chat to a user, follow a user around or whatever.
# See https://www.youtube.com/watch?v=j-ja0_GcB4s for a video of it.
#
#
# Copyright Glare Technologies 2025 -
#
# NOTE: will need to have requests module installed.
import socket, ssl, pprint, time, select, struct, math, configparser, requests, datetime
from lib.BufferIn import BufferIn
from lib.BufferOut import BufferOut
from lib.BasicTypes import Vec3d, Vec3f, Colour3f, Matrix2f, TimeStamp, readColour3fFromStream, readMatrix2fFromStream, readVec3fFromStream, readVec3dFromStream, readTimeStampFromStream
from lib.WorldMaterial import WorldMaterial
from lib.WorldObject import WorldObject
from lib.Avatar import Avatar
from lib.Protocol import Protocol
#------------------------- Define some misc. functions -------------------------
def writeUInt32ToSocket(socket, x):
b = x.to_bytes(4, byteorder='little')
socket.sendall(b)
def writeStringLengthFirst(socket, str):
b = bytes(str, 'UTF-8')
writeUInt32ToSocket(socket, len(b))
socket.sendall(b)
def readNBytesFromSocket(socket, n):
remaining = n
b = bytearray()
while(remaining > 0):
chunk = socket.recv(remaining)
if(len(chunk) == 0):
raise Exception("Socket was closed gracefully by remote host.")
b.extend(chunk)
remaining -= len(chunk)
return b
def readUInt32FromSocket(socket):
b = readNBytesFromSocket(socket, 4)
return int.from_bytes(b, byteorder='little', signed=False)
def readUInt64FromSocket(socket):
b = readNBytesFromSocket(socket, 8)
return int.from_bytes(b, byteorder='little', signed=False)
def readUID(socket):
return readUInt64FromSocket(socket)
def writeUID(out_stream, uid):
out_stream.writeUInt64(uid)
def socketReadable(socket, timeout_s):
socket_list = [socket]
read_sockets, write_sockets, error_sockets = select.select(socket_list, [], socket_list, timeout_s)
if(len(error_sockets) == 1):
raise Exception("Socket excep")
return len(read_sockets) == 1
#------------------------- Read username and password from disk -------------------------
config = configparser.ConfigParser()
config.read('config.txt')
username = config['credentials']['username']
password = config['credentials']['password']
server_hostname = config['connection']['server_hostname']
if username is None:
raise Exception("Could not find 'username' in config file.")
if password is None:
raise Exception("Could not find 'password' in config file.")
print("Using username '" + username + "'")
#------------------------- Connect to server -------------------------
plain_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
plain_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True)
print("Connecting to server '" + server_hostname + "'...")
# (pre-python 3.7)
#conn = ssl.wrap_socket(plain_socket)
#conn.connect((server_hostname, 7600))
# ========================
# https://stackoverflow.com/questions/4818280/ssl-wrap-socket-attributeerror-module-object-has-no-attribute-wrap-socket
sslSettings = ssl.SSLContext(ssl.PROTOCOL_TLS)
conn = sslSettings.wrap_socket(plain_socket)
conn.connect((server_hostname,7600))
print("Connected to server '" + server_hostname + "'.")
#------------------------- Do the Substrata protocol with server -------------------------
writeUInt32ToSocket(conn, Protocol.CyberspaceHello)
writeUInt32ToSocket(conn, Protocol.CyberspaceProtocolVersion)
writeUInt32ToSocket(conn, Protocol.ConnectionTypeUpdates)
writeStringLengthFirst(conn, "") # Write world name
hello = readUInt32FromSocket(conn)
print('Received hello: ', str(hello))
protocol_response = readUInt32FromSocket(conn)
if(protocol_response == Protocol.ClientProtocolTooOld):
raise Exception("Client protcol is too old")
elif(protocol_response == Protocol.ClientProtocolTooNew):
raise Exception("Client protcol is too new")
elif(protocol_response == Protocol.ClientProtocolOK):
print("ClientProtocolOK")
else:
raise Exception("Invalid protocol version response from server: " + protocol_response);
# Read server protocol version
server_protocol_version = readUInt32FromSocket(conn)
print('server_protocol_version: ', str(server_protocol_version))
client_avatar_UID = readUID(conn)
print("Received client_avatar_UID: " + str(client_avatar_UID))
#------------------------- Send login message -------------------------
login_buf = BufferOut()
login_buf.writeUInt32(Protocol.LogInMessage)
login_buf.writeUInt32(0) # will be updated with length of message
login_buf.writeStringLengthFirst(username)
login_buf.writeStringLengthFirst(password)
login_buf.updateLengthField()
login_buf.writeToSocket(conn)
avatar = Avatar()
avatar.uid = client_avatar_UID
avatar.pos = Vec3d(5, 5, 2)
#------------------------- Send create avatar message -------------------------
av_buf = BufferOut()
av_buf.writeUInt32(Protocol.CreateAvatar)
av_buf.writeUInt32(0) # will be updated with length of message
avatar.writeToStream(av_buf)
av_buf.updateLengthField()
av_buf.writeToSocket(conn)
#------------------------- Start main loop -------------------------
last_send_time = -1000.0
last_chat_time = -1000.0
initial_time = time.monotonic()
world_obs = {} # Dict from UID to object
while(1):
#------------------------- Check for any incoming messages from server -------------------------
while(socketReadable(conn, 0.01)): # timeout = 0.01 s
# print("Socket readable!")
# Read and handle message(s)
msg_type = readUInt32FromSocket(conn)
msg_len = readUInt32FromSocket(conn)
#print("Received msg, type: " + str(msg_type) + ", len: " + str(msg_len))
if(msg_len < 8):
raise Exception("Invalid msg len: " + str(msg_len))
# Read rest of message
msg_body = readNBytesFromSocket(conn, msg_len - 8) # We have already read 8 bytes of the message, read the rest.
buffer_in = BufferIn(msg_body)
if(msg_type == Protocol.TimeSyncMessage):
global_time = buffer_in.readDouble()
#print("Received TimeSyncMessage: global_time: " + str(global_time))
elif(msg_type == Protocol.LoggedInMessageID):
print("Received LoggedInMessageID")
logged_in_user_id = buffer_in.readUInt32()
logged_in_username = buffer_in.readStringLengthFirst()
avatar.avatar_settings.readFromStream(buffer_in) # Read avatar settings (which are stored on the server for each user)
logged_in_user_flags = buffer_in.readUInt32()
avatar.name = logged_in_username
print("Sending AvatarFullUpdate...")
# Send AvatarFullUpdate message, to change the nametag and model on our avatar.
buffer_out = BufferOut()
buffer_out.writeUInt32(Protocol.AvatarFullUpdate)
buffer_out.writeUInt32(0) # will be updated with length of message
avatar.writeToStream(buffer_out)
buffer_out.updateLengthField()
buffer_out.writeToSocket(conn) # Send it
elif(msg_type == Protocol.ChatMessageID):
name = buffer_in.readStringLengthFirst()
msg = buffer_in.readStringLengthFirst()
#print("Received ChatMessage: '" + name + "': '" + msg +"'")
elif(msg_type == Protocol.AvatarTransformUpdate):
#print("Received AvatarTransformUpdate")
pass
elif(msg_type == Protocol.ParcelCreated):
#print("Received ParcelCreated")
pass
elif(msg_type == Protocol.InfoMessageID):
msg = buffer_in.readStringLengthFirst()
#print("Received InfoMessage: " + msg)
elif(msg_type == Protocol.ErrorMessageID):
msg = buffer_in.readStringLengthFirst()
print("Received ErrorMessage: " + msg)
elif(msg_type == Protocol.ObjectInitialSend):
print("Received ObjectInitialSend")
world_ob = WorldObject()
world_ob.readFromStream(buffer_in)
world_obs[world_ob.uid] = world_ob
print("num object: " + str(len(world_obs)))
elif(msg_type == Protocol.ObjectContentChanged):
#print("Received ObjectContentChanged")
object_uid = buffer_in.readUInt64()
new_content = buffer_in.readStringLengthFirst()
#print("object_uid: " + str(object_uid) + ", new_content: '" + new_content + "'")
else:
print("Received unknown/unhandled msg type: " + str(msg_type) + ", ignoring.")
UPDATE_PERIOD = 0.1 # How often we send an object update message to the server, in seconds
if(time.monotonic() - last_send_time > UPDATE_PERIOD):
# Send an AvatarTransformUpdate message to the server
# Make the avatar walk in a circle
t = time.monotonic() - initial_time
phase = t * 0.6
avatar.pos.x = math.cos(phase) * 4
avatar.pos.y = math.sin(phase) * 4
avatar.pos.z = 1.67 # The avatar position is considered to be at the head at eye height, which is 1.67m.
# Compute the avatar rotation so that it faces forwards
avatar.rotation = Vec3f(0.0, math.pi / 2, phase + math.pi / 2) # (roll, pitch, heading)
# Send the AvatarTransformUpdate message
buffer_out = BufferOut()
buffer_out.writeUInt32(Protocol.AvatarTransformUpdate)
buffer_out.writeUInt32(0) # will be updated with length of message
writeUID(buffer_out, client_avatar_UID)
avatar.pos.writeToStream(buffer_out)
avatar.rotation.writeToStream(buffer_out)
buffer_out.writeUInt32(0) # Write anim_state.
buffer_out.updateLengthField()
buffer_out.writeToSocket(conn)
last_send_time = time.monotonic()
CHAT_PERIOD = 5.0
# Send a chat message occasionally
if(time.monotonic() - last_chat_time > CHAT_PERIOD):
# Send the ChatMesssage to server
buffer_out = BufferOut()
buffer_out.writeUInt32(Protocol.ChatMessageID)
buffer_out.writeUInt32(0) # will be updated with length of message
buffer_out.writeStringLengthFirst("Beep boop") # message
buffer_out.updateLengthField()
buffer_out.writeToSocket(conn)
last_chat_time = time.monotonic()