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 = []
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 = []
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(message["checked_locations"]))
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["for_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))
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)
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 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)