about summary refs log tree commit diff stats
path: root/apworld/client/run_from_apworld.tscn
blob: 11373e069f180bf970680548cc7f3e9c6ce079fc (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
[gd_scene load_steps=11 format=2]

[sub_resource id=2 type="GDScript"]
script/source = "extends Node2D


func _ready():
	var args = OS.get_cmdline_user_args()
	var apworld_path = args[0]

	var zip_reader = ZIPReader.new()
	zip_reader.open(apworld_path)
	
	var runtime_script = GDScript.new()
	runtime_script.source_code = zip_reader.read_file(\"lingo2/client/apworld_runtime.gd\").get_string_from_utf8()
	runtime_script.reload()

	zip_reader.close()

	var runtime = runtime_script.new(apworld_path)
	runtime.name = \"Runtime\"

	global.add_child(runtime)

	runtime.load_script_as_scene.call_deferred(\"settings_screen.gd\", \"settings_screen\")

"

[node name="loader" type="Node2D"]
script = SubResource( 2 )
2 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
extends Node

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

var SCRIPT_websocketserver

var _server
var _should_process = false

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 _seed = ""
var _team = 0
var _slot = 0
var _checked_locations = []
var _checked_worldports = []
var _received_indexes = []
var _received_items = {}
var _slot_data = {}
var _accessible_locations = []
var _accessible_worldports = []
var _goal_accessible = false

signal could_not_connect
signal connect_status
signal client_connected(slot_data)
signal item_received(item, amount)
signal location_scout_received(location_id, item_name, player_name, flags, for_self)
signal text_message_received(message)
signal item_sent_notification(message)
signal hint_received(message)
signal accessible_locations_updated
signal checked_locations_updated
signal checked_worldports_updated
signal keyboard_update_received


func _init():
	set_process_mode(Node.PROCESS_MODE_ALWAYS)

	global._print("Instantiated APClient")


func _ready():
	_server = SCRIPT_websocketserver.new()
	_server.client_connected.connect(_on_web_socket_server_client_connected)
	_server.client_disconnected.connect(_on_web_socket_server_client_disconnected)
	_server.message_received.connect(_on_web_socket_server_message_received)
	add_child(_server)
	_server.listen(43182)


func _reset_state():
	_should_process = false
	_received_items = {}
	_received_indexes = []
	_checked_worldports = []
	_accessible_locations = []
	_accessible_worldports = []
	_goal_accessible = false


func disconnect_from_ap():
	sendMessage([{"cmd": "Disconnect"}])


func _on_web_socket_server_client_connected(peer_id: int) -> void:
	var peer: WebSocketPeer = _server.peers[peer_id]
	print("Remote client connected: %d. Protocol: %s" % [peer_id, peer.get_selected_protocol()])
	_server.send(-peer_id, "[%d] connected" % peer_id)


func _on_web_socket_server_client_disconnected(peer_id: int) -> void:
	var peer: WebSocketPeer = _server.peers[peer_id]
	print(
		(
			"Remote client disconnected: %d. Code: %d, Reason: %s"
			% [peer_id, peer.get_close_code(), peer.get_close_reason()]
		)
	)
	_server.send(-peer_id, "[%d] disconnected" % peer_id)


func _on_web_socket_server_message_received(_peer_id: int, packet: String) -> void:
	global._print("Got data from server: " + packet)
	var json = JSON.new()
	var jserror = json.parse(packet)
	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 == "Connected":
			_seed = message["seed_name"]
			_remote_version = message["version"]
			_gen_version = message["generator_version"]
			_team = message["team"]
			_slot = message["slot"]
			_slot_data = message["slot_data"]

			_checked_locations = []
			for location in message["checked_locations"]:
				_checked_locations.append(int(location))

			client_connected.emit(_slot_data)

		elif cmd == "ConnectionRefused":
			could_not_connect.emit(message["text"])
			global._print("Connection to AP refused")

		elif cmd == "UpdateLocations":
			for location in message["locations"]:
				var lint = int(location)
				if not _checked_locations.has(lint):
					_checked_locations.append(lint)

			checked_locations_updated.emit()

		elif cmd == "UpdateWorldports":
			for port_id in message["worldports"]:
				var lint = int(port_id)
				if not _checked_worldports.has(lint):
					_checked_worldports.append(lint)

			checked_worldports_updated.emit()

		elif cmd == "ItemReceived":
			for item in message["items"]:
				var index = int(item["index"])
				if _received_indexes.has(index):
					# Do not re-process items.
					continue

				_received_indexes.append(index)

				var item_id = int(item["id"])
				_received_items[item_id] = _received_items.get(item_id, 0) + 1

				item_received.emit(item, _received_items[item_id])

		elif cmd == "TextMessage":
			text_message_received.emit(message["data"])

		elif cmd == "ItemSentNotif":
			item_sent_notification.emit(message)

		elif cmd == "HintReceived":
			hint_received.emit(message)

		elif cmd == "LocationInfo":
			for loc in message["locations"]:
				location_scout_received.emit(
					int(loc["id"]), loc["item"], loc["player"], int(loc["flags"]), int(loc["self"])
				)

		elif cmd == "AccessibleLocations":
			_accessible_locations.clear()
			_accessible_worldports.clear()

			for loc in message["locations"]:
				_accessible_locations.append(int(loc))

			if "worldports" in message:
				for port_id in message["worldports"]:
					_accessible_worldports.append(int(port_id))

			_goal_accessible = bool(message.get("goal", false))

			accessible_locations_updated.emit()

		elif cmd == "UpdateKeyboard":
			var updates = {}
			for k in message["updates"]:
				updates[k] = int(message["updates"][k])

			keyboard_update_received.emit(updates)

		elif cmd == "PathReply":
			var textclient = global.get_node("Textclient")
			textclient.display_logical_path(
				message["type"], int(message.get("id", null)), message["path"]
			)


func connectToServer(server, un, pw):
	sendMessage([{"cmd": "Connect", "server": server, "player": un, "password": pw}])

	ap_server = server
	ap_user = un
	ap_pass = pw

	_should_process = true

	connect_status.emit("Connecting...")


func sendMessage(msg):
	var payload = JSON.stringify(msg)
	_server.send(0, payload)


func connectToRoom():
	connect_status.emit("Authenticating...")

	sendMessage(
		[
			{
				"cmd": "Connect",
				"password": ap_pass,
				"game": "Lingo 2",
				"name": ap_user,
			}
		]
	)


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


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


func sendLocations(loc_ids):
	sendMessage([{"cmd": "LocationChecks", "locations": loc_ids}])


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


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


func scoutLocations(loc_ids):
	sendMessage([{"cmd": "LocationScouts", "locations": loc_ids}])


func updateKeyboard(updates):
	sendMessage([{"cmd": "UpdateKeyboard", "keyboard": updates}])


func checkWorldport(port_id):
	if not _checked_worldports.has(port_id):
		sendMessage([{"cmd": "CheckWorldport", "port_id": port_id}])


func getLogicalPath(object_type, object_id):
	var msg = {"cmd": "GetPath", "type": object_type}
	if object_id != null:
		msg["id"] = object_id

	sendMessage([msg])


func sendQuit():
	sendMessage([{"cmd": "Quit"}])


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


func getItemAmount(item_id):
	return _received_items.get(item_id, 0)