1
0
mirror of https://github.com/JohnBreaux/Boat-Battle.git synced 2024-11-15 05:25:57 +00:00
Boat-Battle/godot_ship/script/network/Net.gd

163 lines
4.9 KiB
GDScript3
Raw Normal View History

2021-11-18 15:00:15 +00:00
extends Node
2021-11-24 04:44:53 +00:00
# Constants
# DEFAULT_PORT: The port GodotShip will listen on/connect to by default
2021-11-18 15:00:15 +00:00
const DEFAULT_PORT = 35879
2021-11-24 04:44:53 +00:00
# LOCALHOST: loopback address
2021-11-18 15:00:15 +00:00
const LOCALHOST = "127.0.0.1"
2021-11-24 04:44:53 +00:00
# Enums, used for mail types
# Mail types:
# 1: REQUEST: Message is a request for information
# 0: REPLY: Message is a reply
enum {REPLY, REQUEST}
2021-11-24 04:44:53 +00:00
# Signals
# incoming(mail): Sent when there's an incoming message
signal incoming
# peers_updated(): Sent when the peer list is updated
signal peers_updated
#
2021-11-18 15:00:15 +00:00
2021-11-24 04:44:53 +00:00
# Variables
# inbox: Array: Message history
var inbox = []
# connected: Boolean: True when in the Connected state
2021-11-18 15:00:15 +00:00
var connected = false
2021-11-24 04:44:53 +00:00
# hosting: Boolean: True when in the Hosting state
var hosting = false
2021-11-24 04:44:53 +00:00
# peer_info: Dictionary: Store peer info in a dictionary, by player ID
var peer_info = {}
# local_info: Dictionary: Store this player's info
var local_info = {"name": ""}
2021-11-18 15:00:15 +00:00
# Network -- handles server and client setup, and facilitates communication between the two
2021-11-24 04:44:53 +00:00
# receive: Receive a message (called by sender's `send` function)
# item: The message received from the sender (implicitly JSON-decoded by JSONRPC)
2021-11-23 05:17:27 +00:00
sync func receive(mail):
# Get the sender's ID and force letter to be properly addressed
2021-11-23 05:17:27 +00:00
mail[0] = get_tree().get_rpc_sender_id()
# Add the mail to the inbox (so it can be read back later if necessary
inbox.append(mail)
# Sent it off to anything that expects mail
emit_signal("incoming", mail)
2021-11-24 04:44:53 +00:00
# send: Send a message
# id: Peer ID of the recipient
# mail: Variant of a json-encodable type (non-Object) to send
2021-11-23 05:17:27 +00:00
func send(id, mail):
# Make the recipient receive the mail
2021-11-23 05:17:27 +00:00
rpc_id(id, "receive", to_json(mail))
# start_host: Host the game
2021-11-18 15:00:15 +00:00
# port: TCP port
# max_players: Largest number of players allowed to connect at a time
func start_host(port = DEFAULT_PORT, max_players = 2):
2021-11-18 15:00:15 +00:00
get_hostname()
peer_info[1] = local_info
2021-11-24 04:44:53 +00:00
# Notify that peer list has updated
emit_signal("peers_updated")
# Create a new NetworkedMultiplayerENet (handles multiplayer communication through ENet)
2021-11-18 15:00:15 +00:00
var peer = NetworkedMultiplayerENet.new()
2021-11-24 04:44:53 +00:00
# Create a server
2021-11-18 15:00:15 +00:00
peer.create_server(port, max_players)
2021-11-24 04:44:53 +00:00
# Add the server to the scene tree
2021-11-18 15:00:15 +00:00
get_tree().network_peer = peer
2021-11-24 04:44:53 +00:00
# Update state
2021-11-18 15:00:15 +00:00
connected = true
hosting = true
2021-11-18 15:00:15 +00:00
# connect_host: Connect to a host
func connect_host(ip = LOCALHOST, port = DEFAULT_PORT):
2021-11-18 15:00:15 +00:00
get_hostname()
var peer = NetworkedMultiplayerENet.new()
peer.create_client(ip, port)
get_tree().network_peer = peer
# disconnect_host
func disconnect_host():
2021-11-24 04:44:53 +00:00
# Send intent to disconnect
rpc("unregister_peer", get_network_id())
# Set state to disconnected
2021-11-18 15:00:15 +00:00
connected = false
hosting = false
# Attempt disconnection
if get_tree().network_peer:
get_tree().network_peer.close_connection()
# Disconnect
get_tree().network_peer = null
# Clear peer info
peer_info = {}
2021-11-24 04:44:53 +00:00
# Notify that peer list has updated
emit_signal("peers_updated")
# change_name: Change the local name, and re-register with all peers (including self)
func change_name(name):
# Change name locally
local_info["name"] = name
# Send updated info info to all peers
rpc("register_peer", local_info)
pass
2021-11-18 15:00:15 +00:00
2021-11-24 04:44:53 +00:00
# get_hostname: Asks the host machine to provide its hostname,
# and if the peer name isn't set, set it to the hostname
2021-11-18 15:00:15 +00:00
func get_hostname():
2021-11-24 04:44:53 +00:00
var hostname = []
# Execute the `hostname` command
var _ret = OS.execute("hostname", [], true, hostname)
# If there's no name set, set it to the hostname
if local_info["name"] == "":
local_info["name"] = hostname[0].split("\n")[0]
return hostname[0].split("\n")[0]
2021-11-18 15:00:15 +00:00
func get_network_id():
return get_tree().get_network_unique_id()
func get_ip():
2021-11-24 04:44:53 +00:00
return IP.resolve_hostname(get_hostname(), IP.TYPE_IPV4)
2021-11-18 15:00:15 +00:00
pass
func _ready():
var _trash
_trash = get_tree().connect("network_peer_connected", self, "_peer_connected" )
2021-11-18 15:00:15 +00:00
_trash = get_tree().connect("network_peer_disconnected", self, "_peer_disconnected")
_trash = get_tree().connect("connected_to_server", self, "_host_connected" )
_trash = get_tree().connect("server_disconnected", self, "_host_disconnected")
_trash = get_tree().connect("connection_failed", self, "_connection_fail" )
2021-11-18 15:00:15 +00:00
func _peer_connected(id):
2021-11-24 04:44:53 +00:00
# Send peer info to remote peer
2021-11-18 15:00:15 +00:00
rpc_id(id, "register_peer", local_info)
pass
func _peer_disconnected(id):
2021-11-24 04:44:53 +00:00
# Unregister the peer locally
unregister_peer(id)
2021-11-18 15:00:15 +00:00
pass
func _host_connected():
2021-11-18 15:00:15 +00:00
# On connection to the server, you get a global network id
# Save your info at this id
peer_info[get_network_id()] = local_info
# Set state to connected
2021-11-18 15:00:15 +00:00
connected = true
func _host_disconnected():
# Ensure host is disconnected
disconnect_host()
2021-11-18 15:00:15 +00:00
func _connection_fail():
# Ensure Net state is clear
disconnect_host()
2021-11-18 15:00:15 +00:00
2021-11-24 04:44:53 +00:00
sync func register_peer(info):
2021-11-18 15:00:15 +00:00
# Save player information under the sender id's peer info
peer_info[get_tree().get_rpc_sender_id()] = info
2021-11-24 04:44:53 +00:00
emit_signal("peers_updated")
sync func unregister_peer(id):
peer_info.erase(id)
emit_signal("peers_updated")