Skip to content

Commit

Permalink
LNX-164 Equally synchronise Mati suggestion players relative to server (
Browse files Browse the repository at this point in the history
#29)

* LNX-164 Add better choosing time to get state

* LNX-164 Added acceptable delay

* LNX-164 After testing I found out that this sollution is most consistent. Max range of gets is about 0.15s. Gets on scene host appear after 0.02s. All in all, lags still show up, but more rarely than before this bugfix implementation

* LNX-164 Removed junk script

* lnx-164 Delay based on previous delays, so we can send get request a little bit before this time

* Remove junk file

* Cleaned after junk file

* LNX164 Fixes suggested by

* Fixed typo

* LNX-164 removed variable which wasnt used

* LNX-164 Starting with -1 tick not 0
  • Loading branch information
EasyJB authored Aug 6, 2023
1 parent fc8279f commit 3e3d199
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
6 changes: 6 additions & 0 deletions config.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extends Node

const DEFAULT_GET_STATE_WAIT_TIME: float = 1.0
const THRESHOLD_OF_SECOND_ROUNDING: float = 0.85
const DELAYS_LIST_SIZE: int = 10
const TICKS_WITHOUT_SYNCHRONIZATION: int = 3
1 change: 1 addition & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ config/icon="res://icon.svg"
[autoload]

Globals="*res://globals.gd"
Config="*res://config.gd"

[display]

Expand Down
38 changes: 35 additions & 3 deletions scene/world_updater/StateGetter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ extends Node

var json = JSON.new()
var current_tick_number: int = -1
var delays: Array = []
var last_delay: float = 0.0

func _calculate_average_delay_time():
var sum := 0.0
if delays.size() == 0:
return 0.0
return delays.reduce(func(accum, number): return accum + number) / delays.size()

func _normalize_second(lower_time, higher_time):
var difference_in_ms = float(int(higher_time - lower_time) % 1000) / 1000
var difference_in_ms_normalized = difference_in_ms
if difference_in_ms_normalized >= Config.THRESHOLD_OF_SECOND_ROUNDING:
difference_in_ms_normalized = difference_in_ms - Config.DEFAULT_GET_STATE_WAIT_TIME
return difference_in_ms_normalized

func _speed_up_actions():
for object in Globals.WORLD_UPDATER.objects_container.get_children():
Expand All @@ -26,20 +41,37 @@ func _update_state(response):
scene_wiper.wipe()
scene_recreator.recreate_scene(json.get_data())
elif "deltas" in response.keys():
if delays.size() == Config.DELAYS_LIST_SIZE:
delays.pop_front()
var difference_in_ms_normalized = _normalize_second(response["send_time_ms"], Time.get_unix_time_from_system() * 1000)
delays.push_back(difference_in_ms_normalized + last_delay)
last_delay = _calculate_average_delay_time()
deltas_applier.apply_deltas(response["deltas"])

_handle_lag(current_tick_number, response["tick_number"])
current_tick_number = response["tick_number"]


if get_owner().objects_container.get_child_count() == 0:
current_tick_number = -1
else:
current_tick_number = response["tick_number"]

func _on_get_state_http_request_request_completed(_result, response_code, _headers, body):
if response_code == 200:
json.parse(body.get_string_from_utf8())
var response = json.get_data()
_update_state(response)




# send get state requests every timer timeout (1s)
func _on_get_state_timer_timeout():
if get_state_http_request.get_http_client_status() not in Globals.BUSY_HTTP_STATUSES:
var result = get_state_http_request.request(Globals.SERVER_ADDRESS + "?tick_number=" + str(current_tick_number))
if result != OK:
push_error("[ERROR] Could not GET state: " + str(result))

var delay_time_normalized = _normalize_second(0, Time.get_unix_time_from_system() * 1000)
if current_tick_number % Config.TICKS_WITHOUT_SYNCHRONIZATION == 0:
get_node("GetStateTimer").wait_time = Config.DEFAULT_GET_STATE_WAIT_TIME - delay_time_normalized - _calculate_average_delay_time()
else:
get_node("GetStateTimer").wait_time = Config.DEFAULT_GET_STATE_WAIT_TIME

0 comments on commit 3e3d199

Please sign in to comment.