about summary refs log tree commit diff stats
path: root/client/Archipelago/client.gd
blob: 4c34e919be8c7cbfca9b353ab122d8502fa19e36 (plain) (blame)
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
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
name: "Tree Entrance"
panel_display_name: "Outside Pumpkin"
panels {
  name: "RAT"
  path: "Panels/Hexes/lime_1"
  clue: "rat"
  answer: "rated"
  symbols: SPARKLES
}
panels {
  name: "DIFFERENCE"
  path: "Panels/Hexes/lime_2"
  clue: "difference"
  answer: "some"
  symbols: SUN
  symbols: ZERO
}
panels {
  name: "LEANS"
  path: "Panels/Hexes/lime_3"
  clue: "leans"
  answer: "aliens"
  symbols: SPARKLES
  symbols: ZERO
}
panels {
  name: "QUESTION"
  path: "Panels/Hexes/lime_4"
  clue: "question"
  answer: "what"
  symbols: EXAMPLE
}
panels {
  name: "WHERE"
  path: "Panels/Hexes/lime_5"
  clue: "where"
  answer: "are"
  symbols: SPARKLES
  symbols: AGE
}
panels {
  name: "SUNDER"
  path: "Panels/Hexes/lime_6"
  clue: "sunder"
  answer: "over"
  symbols: SUN
  symbols: SPARKLES
}
ports {
  name: "TREE"
  path: "Components/Warps/Worldports/worldport12"
}
a id='n275' href='#n275'>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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
extends Node

const ap_version = {"major": 0, "minor": 6, "build": 3, "class": "Version"}

var SCRIPT_uuid

var _ws = WebSocketPeer.new()
var _should_process = false
var _initiated_disconnect = false
var _try_wss = false
var _has_connected = false

var _datapackages = {}
var _pending_packages = []
var _item_id_to_name = {}  # All games
var _location_id_to_name = {}  # All games
var _item_name_to_id = {}  # Lingo 2 only
var _location_name_to_id = {}  # Lingo 2 only

var _remote_version = {"major": 0, "minor": 0, "build": 0}
var _gen_version = {"major": 0, "minor": 0, "build": 0}

var ap_server = ""
var ap_user = ""
var ap_pass = ""

var _authenticated = false
var _seed = ""
var _team = 0
var _slot = 0
var _players = []
var _player_name_by_slot = {}
var _game_by_player = {}
var _checked_locations = []
var _received_items = []
var _slot_data = {}

signal could_not_connect
signal connect_status
signal client_connected
signal item_received(item_id, index, player, flags)
signal message_received(message)


func _init():
	global._print("Instantiated APClient")

	# Read AP datapackages from file, if there are any
	if FileAccess.file_exists("user://ap_datapackages"):
		var file = FileAccess.open("user://ap_datapackages", FileAccess.READ)
		var data = file.get_var(true)
		file.close()

		if typeof(data) != TYPE_DICTIONARY:
			global._print("AP datapackages file is corrupted")
			data = {}

		_datapackages = data

		processDatapackages()


func _ready():
	pass
	#_ws.connect("connection_closed", _closed)
	#_ws.connect("connection_failed", _closed)
	#_ws.connect("server_disconnected", _closed)
	#_ws.connect("connection_error", _errored)
	#_ws.connect("connection_established", _connected)


func _reset_state():
	_should_process = false
	_authenticated = false
	_try_wss = false
	_has_connected = false


func _errored():
	if _try_wss:
		global._print("Could not connect to AP with ws://, now trying wss://")
		connectToServer(ap_server, ap_user, ap_pass)
	else:
		global._print("AP connection failed")
		_reset_state()

		emit_signal(
			"could_not_connect",
			"Could not connect to Archipelago. Check that your server and port are correct. See the error log for more information."
		)


func _closed(_was_clean = true):
	global._print("Connection closed")
	_reset_state()

	if not _initiated_disconnect:
		emit_signal("could_not_connect", "Disconnected from Archipelago")

	_initiated_disconnect = false


func _connected(_proto = ""):
	global._print("Connected!")
	_try_wss = false


func disconnect_from_ap():
	_initiated_disconnect = true
	_ws.close()


func _process(_delta):
	if _should_process:
		_ws.poll()

		var state = _ws.get_ready_state()
		if state == WebSocketPeer.STATE_OPEN:
			if not _has_connected:
				_has_connected = true

				_connected()

			while _ws.get_available_packet_count():
				var packet = _ws.get_packet()
				global._print("Got data from server: " + packet.get_string_from_utf8())
				var json = JSON.new()
				var jserror = json.parse(packet.get_string_from_utf8())
				if jserror != OK:
					global._print("Error parsing packet from AP: " + jserror.error_string)
					return

				for message in json.data:
					var cmd = message["cmd"]
					global._print("Received command: " + cmd)

					if cmd == "RoomInfo":
						_seed = message["seed_name"]
						_remote_version = message["version"]
						_gen_version = message["generator_version"]

						var needed_games = []
						for game in message["datapackage_checksums"].keys():
							if (
								!_datapackages.has(game)
								or (
									_datapackages[game]["checksum"]
									!= message["datapackage_checksums"][game]
								)
							):
								needed_games.append(game)

						if !needed_games.is_empty():
							_pending_packages = needed_games
							var cur_needed = _pending_packages.pop_front()
							requestDatapackages([cur_needed])
						else:
							connectToRoom()

					elif cmd == "DataPackage":
						for game in message["data"]["games"].keys():
							_datapackages[game] = message["data"]["games"][game]
						saveDatapackages()

						if !_pending_packages.is_empty():
							var cur_needed = _pending_packages.pop_front()
							requestDatapackages([cur_needed])
						else:
							processDatapackages()
							connectToRoom()

					elif cmd == "Connected":
						_authenticated = true
						_team = message["team"]
						_slot = message["slot"]
						_players = message["players"]
						_checked_locations = message["checked_locations"]
						_slot_data = message["slot_data"]

						for player in _players:
							_player_name_by_slot[player["slot"]] = player["alias"]
							_game_by_player[player["slot"]] = message["slot_info"][str(
								player["slot"]
							)]["game"]

						emit_signal("client_connected")

					elif cmd == "ConnectionRefused":
						var error_message = ""
						for error in message["errors"]:
							var submsg = ""
							if error == "InvalidSlot":
								submsg = "Invalid player name."
							elif error == "InvalidGame":
								submsg = "The specified player is not playing Lingo."
							elif error == "IncompatibleVersion":
								submsg = (
									"The Archipelago server is not the correct version for this client. Expected v%d.%d.%d. Found v%d.%d.%d."
									% [
										ap_version["major"],
										ap_version["minor"],
										ap_version["build"],
										_remote_version["major"],
										_remote_version["minor"],
										_remote_version["build"]
									]
								)
							elif error == "InvalidPassword":
								submsg = "Incorrect password."
							elif error == "InvalidItemsHandling":
								submsg = "Invalid item handling flag. This is a bug with the client."

							if submsg != "":
								if error_message != "":
									error_message += " "
								error_message += submsg

						if error_message == "":
							error_message = "Unknown error."

						_initiated_disconnect = true
						_ws.disconnect_from_host()

						emit_signal("could_not_connect", error_message)
						global._print("Connection to AP refused")
						global._print(message)

					elif cmd == "ReceivedItems":
						var i = 0
						for item in message["items"]:
							if not _received_items.has(int(item["item"])):
								_received_items.append(int(item["item"]))

							emit_signal(
								"item_received",
								int(item["item"]),
								int(message["index"]) + i,
								int(item["player"]),
								int(item["flags"])
							)
							i += 1

					elif cmd == "PrintJSON":
						emit_signal("message_received", message)

		elif state == WebSocketPeer.STATE_CLOSED:
			if _has_connected:
				_closed()
			else:
				_errored()


func saveDatapackages():
	# Save the AP datapackages to disk.
	var file = FileAccess.open("user://ap_datapackages", FileAccess.WRITE)
	file.store_var(_datapackages, true)
	file.close()


func connectToServer(server, un, pw):
	ap_server = server
	ap_user = un
	ap_pass = pw

	_initiated_disconnect = false

	var url = ""
	if ap_server.begins_with("ws://") or ap_server.begins_with("wss://"):
		url = ap_server
		_try_wss = false
	elif _try_wss:
		url = "wss://" + ap_server
		_try_wss = false
	else:
		url = "ws://" + ap_server
		_try_wss = true

	var err = _ws.connect_to_url(url)
	if err != OK:
		emit_signal(
			"could_not_connect",
			(
				"Could not connect to Archipelago. Check that your server and port are correct. See the error log for more information. Error code: %d."
				% err
			)
		)
		global._print("Could not connect to AP: " + err)
		return
	_should_process = true

	emit_signal("connect_status", "Connecting...")


func sendMessage(msg):
	var payload = JSON.stringify(msg)
	_ws.send_text(payload)


func requestDatapackages(games):
	emit_signal("connect_status", "Downloading %s data package..." % games[0])

	sendMessage([{"cmd": "GetDataPackage", "games": games}])


func processDatapackages():
	_item_id_to_name = {}
	_location_id_to_name = {}
	for game in _datapackages.keys():
		var package = _datapackages[game]

		_item_id_to_name[game] = {}
		for item_name in package["item_name_to_id"].keys():
			_item_id_to_name[game][int(package["item_name_to_id"][item_name])] = item_name

		_location_id_to_name[game] = {}
		for location_name in package["location_name_to_id"].keys():
			_location_id_to_name[game][int(package["location_name_to_id"][location_name])] = location_name

	if _datapackages.has("Lingo 2"):
		_item_name_to_id = _datapackages["Lingo 2"]["item_name_to_id"]
		_location_name_to_id = _datapackages["Lingo 2"]["location_name_to_id"]


func connectToRoom():
	emit_signal("connect_status", "Authenticating...")

	sendMessage(
		[
			{
				"cmd": "Connect",
				"password": ap_pass,
				"game": "Lingo 2",
				"name": ap_user,
				"uuid": SCRIPT_uuid.v4(),
				"version": ap_version,
				"items_handling": 0b111,  # always receive our items
				"tags": [],
				"slot_data": true
			}
		]
	)


func sendConnectUpdate(tags):
	sendMessage([{"cmd": "ConnectUpdate", "tags": tags}])


func requestSync():
	sendMessage([{"cmd": "Sync"}])


func sendLocation(loc_id):
	sendMessage([{"cmd": "LocationChecks", "locations": [loc_id]}])


func setValue(key, value, operation = "replace"):
	sendMessage(
		[
			{
				"cmd": "Set",
				"key": "Lingo2_%d_%s" % [_slot, key],
				"want_reply": false,
				"operations": [{"operation": operation, "value": value}]
			}
		]
	)


func say(textdata):
	sendMessage([{"cmd": "Say", "text": textdata}])


func completedGoal():
	sendMessage([{"cmd": "StatusUpdate", "status": 30}])  # CLIENT_GOAL


func hasItem(item_id):
	return _received_items.has(item_id)