about summary refs log tree commit diff stats
path: root/data/maps/the_darkroom/rooms/S Room.txtpb
blob: 9960b86230550e921dbffdadd5d32a0e7d019817 (plain) (blame)
1
2
3
4
5
name: "S Room"
letters {
  key: "s"
  path: "Components/Collectables/collectable"
}
id='n200' href='#n200'>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 291 292
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
var _latched_doors = []

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 door_latched(id)
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"]
			)

		elif cmd == "UpdateLatches":
			for id in message["latches"]:
				var iid = int(id)
				if not _latched_doors.has(iid):
					_latched_doors.append(iid)

				door_latched.emit(iid)


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 latchDoor(id):
	if not _latched_doors.has(id):
		_latched_doors.append(id)

		sendMessage([{"cmd": "LatchDoor", "door": 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)