This repository has been archived on 2020-09-30. You can view files and clone it, but cannot push or open issues or pull requests.
odyssey-old/Scenes/Global/Multiplayer.gd

243 lines
6.5 KiB
GDScript3
Raw Normal View History

2020-07-10 13:37:13 +00:00
extends Node
2020-07-12 15:26:40 +00:00
signal ms_updated(action, data)
signal left()
# Hosting info
2020-07-10 13:37:13 +00:00
const SERVER_PORT = 5513
const MAX_PLAYERS = 30
2020-07-12 15:26:40 +00:00
var port = SERVER_PORT
# Throttle network updates of often-changed variables by this many physics frames
const SYSTEMS_UPDATE_INTERVAL = 10
2020-07-12 15:26:40 +00:00
# Master server data
const MASTER_SERVER_ADDR = "fgms.zyg.ovh"
const MASTER_SERVER_UDP_PORT = 9434
const MS_GAME_CODE = "odyssey-0-a1"
# Master server entry
var ms_active = false
var ms_key = ""
var server_name = ""
2020-07-10 13:37:13 +00:00
var hosting = false
2020-07-10 13:37:13 +00:00
export var player_name = ""
2020-07-12 15:26:40 +00:00
2020-07-10 13:37:13 +00:00
var player_info = {}
var round_info = {}
2020-07-10 13:37:13 +00:00
onready var scene_manager = $"/root/SceneManager"
2020-07-10 13:37:13 +00:00
func _ready():
player_name = "tider-" + str(randi() % 1000)
2020-07-12 15:26:40 +00:00
func bind_events():
2020-07-10 13:37:13 +00:00
get_tree().connect("network_peer_connected", self, "_player_connected")
get_tree().connect("network_peer_disconnected", self, "_player_disconnected")
get_tree().connect("connected_to_server", self, "_connected_ok")
get_tree().connect("connection_failed", self, "_connected_fail")
get_tree().connect("server_disconnected", self, "_server_disconnected")
2020-07-12 15:26:40 +00:00
func punch_nat():
var socketUDP = PacketPeerUDP.new()
if socketUDP.listen(SERVER_PORT) != OK:
push_error("error listening on port: " + str(SERVER_PORT))
socketUDP.set_dest_address(MASTER_SERVER_ADDR, MASTER_SERVER_UDP_PORT)
socketUDP.put_packet("hi server!".to_ascii())
# Poll for answer from server, since godot doesn't have events for UDP packets (ノへ ̄、)
var failed = 0
while failed < 5:
yield(get_tree().create_timer(.3), "timeout")
if socketUDP.get_available_packet_count() < 1:
failed += 1
print("no reply (attempt #", failed, ")")
continue
port = socketUDP.get_var(false)
print("Received ", port)
break
socketUDP.close()
func discover_upnp():
var upnp = UPNP.new()
upnp.discover(2000, 2, "InternetGatewayDevice")
return upnp.add_port_mapping(SERVER_PORT)
2020-07-10 13:37:13 +00:00
func host():
scene_manager.enter_loader()
scene_manager.loading_text = "Starting server"
# Wait just a sec to draw
yield(get_tree().create_timer(0.3), "timeout")
2020-07-12 15:26:40 +00:00
print("Running UPNP magicks")
if discover_upnp() == UPNP.UPNP_RESULT_SUCCESS:
print("UPNP mapping added")
else:
push_warning("UPNP magicks fail, punching NAT in the face")
yield(punch_nat(), "completed")
bind_events()
2020-07-10 13:37:13 +00:00
var peer = NetworkedMultiplayerENet.new()
2020-07-12 15:26:40 +00:00
var server_res = peer.create_server(port, MAX_PLAYERS)
if server_res != OK:
match server_res:
ERR_CANT_CREATE:
push_error("Can't create server")
ERR_ALREADY_IN_USE:
push_error("Already in use")
return
get_tree().network_peer = peer
2020-07-10 13:37:13 +00:00
print("Hosting")
hosting = true
2020-07-12 15:26:40 +00:00
server_name = player_name + "'s server"
player_info[1] = { "name": player_name }
round_info = { "map": "odyssey" }
scene_manager.loading_text = null
2020-07-12 15:26:40 +00:00
scene_manager.load_scene("res://Scenes/Game.tscn", [
"res://Scenes/Maps/odyssey.tscn"
])
2020-07-12 15:26:40 +00:00
# Add to master server
create_ms_entry()
2020-07-10 13:37:13 +00:00
func join(addr: String):
scene_manager.enter_loader()
scene_manager.loading_text = "Joining server " + str(addr)
# Wait just a sec to draw
yield(get_tree().create_timer(0.3), "timeout")
2020-07-12 15:26:40 +00:00
bind_events()
2020-07-10 13:37:13 +00:00
var peer = NetworkedMultiplayerENet.new()
peer.create_client(addr, SERVER_PORT)
get_tree().network_peer = peer
2020-07-12 15:26:40 +00:00
print("Connecting to ", addr)
2020-07-10 13:37:13 +00:00
func leave():
var peer = get_tree().network_peer
if get_tree().is_network_server():
2020-07-12 15:26:40 +00:00
# Tell MS we're leaving
if ms_active:
_ms_request("remove", { "key": ms_key })
yield(self, "ms_updated")
2020-07-10 13:37:13 +00:00
elif peer != null:
#TODO Send leave message
pass
get_tree().network_peer = null
2020-07-12 15:26:40 +00:00
emit_signal("left")
2020-07-10 13:37:13 +00:00
func _player_connected(id):
rpc_id(id, "register_player", player_name)
if get_tree().is_network_server():
rpc_id(id, "_handshake", { "round": round_info, "players": player_info })
2020-07-10 13:37:13 +00:00
func _player_disconnected(id):
print(player_info[id].name, " (", id, ") disconnected")
player_info.erase(id)
func _connected_ok():
print("Connected to server")
scene_manager.loading_text = null
scene_manager.load_scene("res://Scenes/Game.tscn", [])
2020-07-10 13:37:13 +00:00
func _server_disconnected():
print("Disconnected from server")
func _connected_fail():
2020-07-12 15:26:40 +00:00
push_warning("Connection failed")
2020-07-10 13:37:13 +00:00
remote func register_player(username: String):
var id = get_tree().get_rpc_sender_id()
player_info[id] = { name=username }
print(player_info[id].name, " (", id, ") connected")
remote func _handshake(infos):
round_info = infos["round"]
player_info = infos["players"]
2020-07-12 15:26:40 +00:00
func _ms_request(endpoint: String, data):
var http_request = HTTPRequest.new()
add_child(http_request)
http_request.connect("request_completed", self, "_ms_response", [endpoint])
print_debug("Telling ms to " + endpoint)
var error = http_request.request(
"https://" + MASTER_SERVER_ADDR + "/" + endpoint,
["Content-Type: application/json"],
true, HTTPClient.METHOD_POST, JSON.print(data))
if error != OK:
push_error("An error occurred in the HTTP request.")
2020-07-13 23:59:45 +00:00
func ms_get_entries():
var http_request = HTTPRequest.new()
add_child(http_request)
http_request.connect("request_completed", self, "_ms_response", ["list_games"])
var error = http_request.request(
"https://" + MASTER_SERVER_ADDR + "/" + MS_GAME_CODE,
["Content-Type: application/json"],
true, HTTPClient.METHOD_GET)
if error != OK:
push_error("An error occurred in the HTTP request.")
2020-07-12 15:26:40 +00:00
func get_game_data():
return {
"name": server_name,
"port": port,
"max_players": MAX_PLAYERS,
"players": player_info.size()
}
func create_ms_entry():
_ms_request("new", {
"game_id": MS_GAME_CODE,
"data": get_game_data()
})
update_ms_entry()
func update_ms_entry():
if ms_active:
_ms_request("update", {
"key": ms_key,
"data": get_game_data()
})
2020-07-13 23:59:45 +00:00
var time_left = 30
func _process(delta):
if not ms_active:
return
time_left -= delta
if time_left < 0:
update_ms_entry()
time_left = 30
2020-07-12 15:26:40 +00:00
func _ms_response(_result: int, response_code: int, _headers: PoolStringArray, body: PoolByteArray, action: String):
print_debug("MS said " + str(response_code))
if response_code > 299:
push_error("ms action \"" + action + "\" returned error: " + str(response_code) + " - " + body.get_string_from_utf8())
return
var json = JSON.parse(body.get_string_from_utf8())
match action:
"new":
if json.result.ok:
ms_active = true
ms_key = json.result.key
emit_signal("ms_updated", action, json.result)
func _notification(what):
# Are we quittin son?
if what == MainLoop.NOTIFICATION_WM_QUIT_REQUEST:
leave()
yield(self, "left")
func get_current_map():
match round_info.map:
"odyssey":
return GameWorld.Map.ODYSSEY
"runtime":
return GameWorld.Map.RUNTIME
_:
return GameWorld.Map.EMPTY