diff options
Diffstat (limited to 'apworld/client/client.gd')
| -rw-r--r-- | apworld/client/client.gd | 318 |
1 files changed, 318 insertions, 0 deletions
| diff --git a/apworld/client/client.gd b/apworld/client/client.gd new file mode 100644 index 0000000..c149482 --- /dev/null +++ b/apworld/client/client.gd | |||
| @@ -0,0 +1,318 @@ | |||
| 1 | extends Node | ||
| 2 | |||
| 3 | const ap_version = {"major": 0, "minor": 6, "build": 3, "class": "Version"} | ||
| 4 | |||
| 5 | var SCRIPT_websocketserver | ||
| 6 | |||
| 7 | var _server | ||
| 8 | var _should_process = false | ||
| 9 | |||
| 10 | var _remote_version = {"major": 0, "minor": 0, "build": 0} | ||
| 11 | var _gen_version = {"major": 0, "minor": 0, "build": 0} | ||
| 12 | |||
| 13 | var ap_server = "" | ||
| 14 | var ap_user = "" | ||
| 15 | var ap_pass = "" | ||
| 16 | |||
| 17 | var _seed = "" | ||
| 18 | var _team = 0 | ||
| 19 | var _slot = 0 | ||
| 20 | var _checked_locations = [] | ||
| 21 | var _checked_worldports = [] | ||
| 22 | var _received_indexes = [] | ||
| 23 | var _received_items = {} | ||
| 24 | var _slot_data = {} | ||
| 25 | var _accessible_locations = [] | ||
| 26 | var _accessible_worldports = [] | ||
| 27 | var _goal_accessible = false | ||
| 28 | var _latched_doors = [] | ||
| 29 | var _hinted_locations = [] | ||
| 30 | |||
| 31 | signal could_not_connect | ||
| 32 | signal connect_status | ||
| 33 | signal client_connected(slot_data) | ||
| 34 | signal item_received(item, amount) | ||
| 35 | signal location_scout_received(location_id, item_name, player_name, flags, for_self) | ||
| 36 | signal text_message_received(message) | ||
| 37 | signal item_sent_notification(message) | ||
| 38 | signal hint_received(message) | ||
| 39 | signal door_latched(id) | ||
| 40 | signal accessible_locations_updated | ||
| 41 | signal checked_locations_updated | ||
| 42 | signal ignored_locations_updated(locations) | ||
| 43 | signal checked_worldports_updated | ||
| 44 | signal keyboard_update_received | ||
| 45 | signal hinted_locations_updated | ||
| 46 | |||
| 47 | |||
| 48 | func _init(): | ||
| 49 | set_process_mode(Node.PROCESS_MODE_ALWAYS) | ||
| 50 | |||
| 51 | global._print("Instantiated APClient") | ||
| 52 | |||
| 53 | |||
| 54 | func _ready(): | ||
| 55 | _server = SCRIPT_websocketserver.new() | ||
| 56 | _server.client_connected.connect(_on_web_socket_server_client_connected) | ||
| 57 | _server.client_disconnected.connect(_on_web_socket_server_client_disconnected) | ||
| 58 | _server.message_received.connect(_on_web_socket_server_message_received) | ||
| 59 | add_child(_server) | ||
| 60 | _server.listen(43182) | ||
| 61 | |||
| 62 | |||
| 63 | func _reset_state(): | ||
| 64 | _should_process = false | ||
| 65 | _received_items = {} | ||
| 66 | _received_indexes = [] | ||
| 67 | _checked_worldports = [] | ||
| 68 | _accessible_locations = [] | ||
| 69 | _accessible_worldports = [] | ||
| 70 | _goal_accessible = false | ||
| 71 | |||
| 72 | |||
| 73 | func disconnect_from_ap(): | ||
| 74 | sendMessage([{"cmd": "Disconnect"}]) | ||
| 75 | |||
| 76 | |||
| 77 | func _on_web_socket_server_client_connected(peer_id: int) -> void: | ||
| 78 | var peer: WebSocketPeer = _server.peers[peer_id] | ||
| 79 | print("Remote client connected: %d. Protocol: %s" % [peer_id, peer.get_selected_protocol()]) | ||
| 80 | _server.send(-peer_id, "[%d] connected" % peer_id) | ||
| 81 | |||
| 82 | |||
| 83 | func _on_web_socket_server_client_disconnected(peer_id: int) -> void: | ||
| 84 | var peer: WebSocketPeer = _server.peers[peer_id] | ||
| 85 | print( | ||
| 86 | ( | ||
| 87 | "Remote client disconnected: %d. Code: %d, Reason: %s" | ||
| 88 | % [peer_id, peer.get_close_code(), peer.get_close_reason()] | ||
| 89 | ) | ||
| 90 | ) | ||
| 91 | _server.send(-peer_id, "[%d] disconnected" % peer_id) | ||
| 92 | |||
| 93 | |||
| 94 | func _on_web_socket_server_message_received(_peer_id: int, packet: String) -> void: | ||
| 95 | global._print("Got data from server: " + packet) | ||
| 96 | var json = JSON.new() | ||
| 97 | var jserror = json.parse(packet) | ||
| 98 | if jserror != OK: | ||
| 99 | global._print("Error parsing packet from AP: " + jserror.error_string) | ||
| 100 | return | ||
| 101 | |||
| 102 | for message in json.data: | ||
| 103 | var cmd = message["cmd"] | ||
| 104 | global._print("Received command: " + cmd) | ||
| 105 | |||
| 106 | if cmd == "Connected": | ||
| 107 | _seed = message["seed_name"] | ||
| 108 | _remote_version = message["version"] | ||
| 109 | _gen_version = message["generator_version"] | ||
| 110 | _team = message["team"] | ||
| 111 | _slot = message["slot"] | ||
| 112 | _slot_data = message["slot_data"] | ||
| 113 | |||
| 114 | _checked_locations = [] | ||
| 115 | for location in message["checked_locations"]: | ||
| 116 | _checked_locations.append(int(location)) | ||
| 117 | |||
| 118 | client_connected.emit(_slot_data) | ||
| 119 | |||
| 120 | elif cmd == "ConnectionRefused": | ||
| 121 | could_not_connect.emit(message["text"]) | ||
| 122 | global._print("Connection to AP refused") | ||
| 123 | |||
| 124 | elif cmd == "UpdateLocations": | ||
| 125 | for location in message["locations"]: | ||
| 126 | var lint = int(location) | ||
| 127 | if not _checked_locations.has(lint): | ||
| 128 | _checked_locations.append(lint) | ||
| 129 | |||
| 130 | checked_locations_updated.emit() | ||
| 131 | |||
| 132 | elif cmd == "UpdateWorldports": | ||
| 133 | for port_id in message["worldports"]: | ||
| 134 | var lint = int(port_id) | ||
| 135 | if not _checked_worldports.has(lint): | ||
| 136 | _checked_worldports.append(lint) | ||
| 137 | |||
| 138 | checked_worldports_updated.emit() | ||
| 139 | |||
| 140 | elif cmd == "ItemReceived": | ||
| 141 | for item in message["items"]: | ||
| 142 | var index = int(item["index"]) | ||
| 143 | if _received_indexes.has(index): | ||
| 144 | # Do not re-process items. | ||
| 145 | continue | ||
| 146 | |||
| 147 | _received_indexes.append(index) | ||
| 148 | |||
| 149 | var item_id = int(item["id"]) | ||
| 150 | _received_items[item_id] = _received_items.get(item_id, 0) + 1 | ||
| 151 | |||
| 152 | item_received.emit(item, _received_items[item_id]) | ||
| 153 | |||
| 154 | elif cmd == "TextMessage": | ||
| 155 | text_message_received.emit(message["data"]) | ||
| 156 | |||
| 157 | elif cmd == "ItemSentNotif": | ||
| 158 | item_sent_notification.emit(message) | ||
| 159 | |||
| 160 | elif cmd == "HintReceived": | ||
| 161 | hint_received.emit(message) | ||
| 162 | |||
| 163 | elif cmd == "LocationInfo": | ||
| 164 | for loc in message["locations"]: | ||
| 165 | location_scout_received.emit( | ||
| 166 | int(loc["id"]), loc["item"], loc["player"], int(loc["flags"]), int(loc["self"]) | ||
| 167 | ) | ||
| 168 | |||
| 169 | elif cmd == "AccessibleLocations": | ||
| 170 | _accessible_locations.clear() | ||
| 171 | _accessible_worldports.clear() | ||
| 172 | |||
| 173 | for loc in message["locations"]: | ||
| 174 | _accessible_locations.append(int(loc)) | ||
| 175 | |||
| 176 | if "worldports" in message: | ||
| 177 | for port_id in message["worldports"]: | ||
| 178 | _accessible_worldports.append(int(port_id)) | ||
| 179 | |||
| 180 | _goal_accessible = bool(message.get("goal", false)) | ||
| 181 | |||
| 182 | accessible_locations_updated.emit() | ||
| 183 | |||
| 184 | elif cmd == "UpdateKeyboard": | ||
| 185 | var updates = {} | ||
| 186 | for k in message["updates"]: | ||
| 187 | updates[k] = int(message["updates"][k]) | ||
| 188 | |||
| 189 | keyboard_update_received.emit(updates) | ||
| 190 | |||
| 191 | elif cmd == "PathReply": | ||
| 192 | var textclient = global.get_node("Textclient") | ||
| 193 | textclient.display_logical_path( | ||
| 194 | message["type"], int(message.get("id", null)), message["path"] | ||
| 195 | ) | ||
| 196 | |||
| 197 | elif cmd == "UpdateLatches": | ||
| 198 | for id in message["latches"]: | ||
| 199 | var iid = int(id) | ||
| 200 | if not _latched_doors.has(iid): | ||
| 201 | _latched_doors.append(iid) | ||
| 202 | |||
| 203 | door_latched.emit(iid) | ||
| 204 | |||
| 205 | elif cmd == "SetIgnoredLocations": | ||
| 206 | var locs = [] | ||
| 207 | for id in message["locations"]: | ||
| 208 | locs.append(int(id)) | ||
| 209 | |||
| 210 | ignored_locations_updated.emit(locs) | ||
| 211 | |||
| 212 | elif cmd == "UpdateHintedLocations": | ||
| 213 | for id in message["locations"]: | ||
| 214 | var iid = int(id) | ||
| 215 | if !_hinted_locations.has(iid): | ||
| 216 | _hinted_locations.append(iid) | ||
| 217 | |||
| 218 | hinted_locations_updated.emit() | ||
| 219 | |||
| 220 | |||
| 221 | func connectToServer(server, un, pw): | ||
| 222 | sendMessage([{"cmd": "Connect", "server": server, "player": un, "password": pw}]) | ||
| 223 | |||
| 224 | ap_server = server | ||
| 225 | ap_user = un | ||
| 226 | ap_pass = pw | ||
| 227 | |||
| 228 | _should_process = true | ||
| 229 | |||
| 230 | connect_status.emit("Connecting...") | ||
| 231 | |||
| 232 | |||
| 233 | func sendMessage(msg): | ||
| 234 | var payload = JSON.stringify(msg) | ||
| 235 | _server.send(0, payload) | ||
| 236 | |||
| 237 | |||
| 238 | func connectToRoom(): | ||
| 239 | connect_status.emit("Authenticating...") | ||
| 240 | |||
| 241 | sendMessage( | ||
| 242 | [ | ||
| 243 | { | ||
| 244 | "cmd": "Connect", | ||
| 245 | "password": ap_pass, | ||
| 246 | "game": "Lingo 2", | ||
| 247 | "name": ap_user, | ||
| 248 | } | ||
| 249 | ] | ||
| 250 | ) | ||
| 251 | |||
| 252 | |||
| 253 | func requestSync(): | ||
| 254 | sendMessage([{"cmd": "Sync"}]) | ||
| 255 | |||
| 256 | |||
| 257 | func sendLocation(loc_id): | ||
| 258 | sendMessage([{"cmd": "LocationChecks", "locations": [loc_id]}]) | ||
| 259 | |||
| 260 | |||
| 261 | func sendLocations(loc_ids): | ||
| 262 | sendMessage([{"cmd": "LocationChecks", "locations": loc_ids}]) | ||
| 263 | |||
| 264 | |||
| 265 | func say(textdata): | ||
| 266 | sendMessage([{"cmd": "Say", "text": textdata}]) | ||
| 267 | |||
| 268 | |||
| 269 | func completedGoal(): | ||
| 270 | sendMessage([{"cmd": "StatusUpdate", "status": 30}]) # CLIENT_GOAL | ||
| 271 | |||
| 272 | |||
| 273 | func scoutLocations(loc_ids): | ||
| 274 | sendMessage([{"cmd": "LocationScouts", "locations": loc_ids}]) | ||
| 275 | |||
| 276 | |||
| 277 | func updateKeyboard(updates): | ||
| 278 | sendMessage([{"cmd": "UpdateKeyboard", "keyboard": updates}]) | ||
| 279 | |||
| 280 | |||
| 281 | func checkWorldport(port_id): | ||
| 282 | if not _checked_worldports.has(port_id): | ||
| 283 | sendMessage([{"cmd": "CheckWorldport", "port_id": port_id}]) | ||
| 284 | |||
| 285 | |||
| 286 | func latchDoor(id): | ||
| 287 | if not _latched_doors.has(id): | ||
| 288 | _latched_doors.append(id) | ||
| 289 | |||
| 290 | sendMessage([{"cmd": "LatchDoor", "door": id}]) | ||
| 291 | |||
| 292 | |||
| 293 | func getLogicalPath(object_type, object_id): | ||
| 294 | var msg = {"cmd": "GetPath", "type": object_type} | ||
| 295 | if object_id != null: | ||
| 296 | msg["id"] = object_id | ||
| 297 | |||
| 298 | sendMessage([msg]) | ||
| 299 | |||
| 300 | |||
| 301 | func addIgnoredLocation(loc_id): | ||
| 302 | sendMessage([{"cmd": "IgnoreLocation", "id": loc_id}]) | ||
| 303 | |||
| 304 | |||
| 305 | func removeIgnoredLocation(loc_id): | ||
| 306 | sendMessage([{"cmd": "UnignoreLocation", "id": loc_id}]) | ||
| 307 | |||
| 308 | |||
| 309 | func sendQuit(): | ||
| 310 | sendMessage([{"cmd": "Quit"}]) | ||
| 311 | |||
| 312 | |||
| 313 | func hasItem(item_id): | ||
| 314 | return _received_items.has(item_id) | ||
| 315 | |||
| 316 | |||
| 317 | func getItemAmount(item_id): | ||
| 318 | return _received_items.get(item_id, 0) | ||
