Use format strings where it makes sense

This commit is contained in:
Hamcha 2020-07-23 11:12:07 +02:00
parent f57e1c433b
commit 4d1d37a169
Signed by: hamcha
GPG key ID: 41467804B19A3315
8 changed files with 25 additions and 27 deletions

View file

@ -5,9 +5,9 @@ onready var scene = $"/root/scene" as GameInstance
func _physics_process(_delta): func _physics_process(_delta):
if not visible: if not visible:
return return
var speed_str = str(round(scene.world.map.current_ship_speed * 10000)) + " u/s" var speed_str = "%.0f u/s" % (scene.world.map.current_ship_speed * 10000)
$Margin/Container/VelocityBox/HBoxContainer3/CurrentSpeed.text = speed_str $Margin/Container/VelocityBox/HBoxContainer3/CurrentSpeed.text = speed_str
var dir_str = str(round(rad2deg(scene.world.map.current_ship_direction))) + " deg" var dir_str = "%.0f deg" % rad2deg(scene.world.map.current_ship_direction)
$Margin/Container/VelocityBox/HBoxContainer2/CurrentDirection.text = dir_str $Margin/Container/VelocityBox/HBoxContainer2/CurrentDirection.text = dir_str
var current_position_str = Coordinates.as_string(scene.world.map.current_ship_position + scene.world.map.current_ship_subpos, true) var current_position_str = Coordinates.as_string(scene.world.map.current_ship_position + scene.world.map.current_ship_subpos, true)
$Margin/Container/VelocityBox/HBoxContainer4/CurrentPosition.text = current_position_str $Margin/Container/VelocityBox/HBoxContainer4/CurrentPosition.text = current_position_str

View file

@ -15,8 +15,8 @@ func _physics_process(_delta):
total_sink += network.total_usage total_sink += network.total_usage
total_source += network.total_source total_source += network.total_source
total_alerts += network.unpowered total_alerts += network.unpowered
$Margin/Container/EnergyBox/CurrentSource/Value.text = str(round(total_source)) + " kW" $Margin/Container/EnergyBox/CurrentSource/Value.text = "%.0f kW" % total_source
$Margin/Container/EnergyBox/CurrentSink/Value.text = str(round(total_sink)) + " kW" $Margin/Container/EnergyBox/CurrentSink/Value.text = "%.0f kW" % total_sink
$Margin/Container/EnergyBox/CurrentAlerts/Value.text = str(total_alerts) $Margin/Container/EnergyBox/CurrentAlerts/Value.text = str(total_alerts)
func _map_button_pressed(): func _map_button_pressed():

View file

@ -2,8 +2,8 @@ extends Control
onready var scene = $"/root/scene" as GameInstance onready var scene = $"/root/scene" as GameInstance
func set_current_charge(val): func set_current_charge(val: float):
$Container/CurrentBox/HBoxContainer/CurrentChargeLabel.text = "(" + str(round(val/10)/100) + "MJ)" $Container/CurrentBox/HBoxContainer/CurrentChargeLabel.text = "(%.2fMJ)" % (val / 1000.0)
$Container/CurrentBox/ProgressBar.value = val $Container/CurrentBox/ProgressBar.value = val
func set_max_charge(val: float): func set_max_charge(val: float):

View file

@ -9,9 +9,7 @@ static func as_string(coord: Vector2, include_subcoord: bool = false) -> String:
else: else:
sector_name += str(int(y)) sector_name += str(int(y))
if include_subcoord: if include_subcoord:
var x_sub = floor((coord.x - x) * 100) sector_name += ".%02d/%02d" % [(coord.x - x) * 100, (coord.y - y) * 100]
var y_sub = floor((coord.y - y) * 100)
sector_name += "." + str(int(x_sub)).pad_zeros(2) + "/" + str(int(y_sub)).pad_zeros(2)
return sector_name return sector_name
static func to_letter(num: int) -> String: static func to_letter(num: int) -> String:

View file

@ -30,7 +30,7 @@ master func send_map() -> void:
var id = get_tree().get_rpc_sender_id() var id = get_tree().get_rpc_sender_id()
var map_data = world.map.serialize() var map_data = world.map.serialize()
var systems_data = systems.serialize() var systems_data = systems.serialize()
print("sending map data to ", id) print("sending map data to %d" % id)
rpc_id(id, "receive_data", { rpc_id(id, "receive_data", {
"map": map_data, "map": map_data,
"systems": systems_data "systems": systems_data
@ -44,7 +44,7 @@ puppet func receive_data(data: Dictionary) -> void:
master func ready_to_spawn() -> void: master func ready_to_spawn() -> void:
var id = get_tree().get_rpc_sender_id() var id = get_tree().get_rpc_sender_id()
print(id, " is ready to spawn players") print("%d is ready to spawn players" % id)
# Tell him everyone to spawn # Tell him everyone to spawn
var players = $world/players.get_children() var players = $world/players.get_children()

View file

@ -54,7 +54,7 @@ func punch_nat():
yield(get_tree().create_timer(.3), "timeout") yield(get_tree().create_timer(.3), "timeout")
if socketUDP.get_available_packet_count() < 1: if socketUDP.get_available_packet_count() < 1:
failed += 1 failed += 1
print("no reply (attempt #", failed, ")") print("no reply (attempt #%d)" % failed)
continue continue
port = socketUDP.get_var(false) port = socketUDP.get_var(false)
print("Received ", port) print("Received ", port)
@ -66,7 +66,7 @@ func discover_upnp():
upnp.discover(2000, 2, "InternetGatewayDevice") upnp.discover(2000, 2, "InternetGatewayDevice")
return upnp.add_port_mapping(SERVER_PORT) return upnp.add_port_mapping(SERVER_PORT)
func host(): func host(map_name: String = "odyssey"):
scene_manager.enter_loader() scene_manager.enter_loader()
scene_manager.loading_text = "Starting server" scene_manager.loading_text = "Starting server"
@ -81,9 +81,9 @@ func host():
push_warning("UPNP magicks fail, punching NAT in the face") push_warning("UPNP magicks fail, punching NAT in the face")
yield(punch_nat(), "completed") yield(punch_nat(), "completed")
server_name = player_name + "'s server" server_name = "%s's server" % player_name
player_info[1] = { "name": player_name } player_info[1] = { "name": player_name }
round_info = { "map": "odyssey" } round_info = { "map": map_name }
bind_events() bind_events()
var peer = NetworkedMultiplayerENet.new() var peer = NetworkedMultiplayerENet.new()
@ -103,7 +103,7 @@ func host():
scene_manager.loading_text = null scene_manager.loading_text = null
scene_manager.load_scene("res://Scenes/Game.tscn", [ scene_manager.load_scene("res://Scenes/Game.tscn", [
"res://Scenes/Maps/odyssey.tscn" "res://Scenes/Maps/%s.tscn" % map_name
]) ])
# Add to master server after hosting # Add to master server after hosting
@ -124,7 +124,7 @@ func join(server):
peer.create_client(addr, SERVER_PORT) peer.create_client(addr, SERVER_PORT)
get_tree().network_peer = peer get_tree().network_peer = peer
print("Connecting to ", addr) print("Connecting to %s" % addr)
func leave(): func leave():
var peer = get_tree().network_peer var peer = get_tree().network_peer
@ -145,7 +145,7 @@ func _player_connected(id):
rpc_id(id, "_handshake", { "round": round_info, "players": player_info }) rpc_id(id, "_handshake", { "round": round_info, "players": player_info })
func _player_disconnected(id): func _player_disconnected(id):
print(player_info[id].name, " (", id, ") disconnected") print("%s (%d) disconnected" % [player_info[id].name, id])
player_info.erase(id) player_info.erase(id)
func _connected_ok(): func _connected_ok():
@ -162,7 +162,7 @@ func _connected_fail():
remote func register_player(username: String): remote func register_player(username: String):
var id = get_tree().get_rpc_sender_id() var id = get_tree().get_rpc_sender_id()
player_info[id] = { name=username } player_info[id] = { name=username }
print(player_info[id].name, " (", id, ") connected") print("%s (%d) connected" % [player_info[id].name, id])
remote func _handshake(infos): remote func _handshake(infos):
round_info = infos["round"] round_info = infos["round"]
@ -172,9 +172,9 @@ func _ms_request(endpoint: String, data):
var http_request = HTTPRequest.new() var http_request = HTTPRequest.new()
add_child(http_request) add_child(http_request)
http_request.connect("request_completed", self, "_ms_response", [endpoint]) http_request.connect("request_completed", self, "_ms_response", [endpoint])
print_debug("Telling ms to " + endpoint) print_debug("Telling ms to %s" % endpoint)
var error = http_request.request( var error = http_request.request(
"https://" + MASTER_SERVER_ADDR + "/" + endpoint, "https://%s/%s" % [MASTER_SERVER_ADDR, endpoint],
["Content-Type: application/json"], ["Content-Type: application/json"],
true, HTTPClient.METHOD_POST, JSON.print(data)) true, HTTPClient.METHOD_POST, JSON.print(data))
if error != OK: if error != OK:
@ -185,7 +185,7 @@ func ms_get_entries():
add_child(http_request) add_child(http_request)
http_request.connect("request_completed", self, "_ms_response", ["list_games"]) http_request.connect("request_completed", self, "_ms_response", ["list_games"])
var error = http_request.request( var error = http_request.request(
"https://" + MASTER_SERVER_ADDR + "/" + MS_GAME_CODE, "https://%s/%s" % [MASTER_SERVER_ADDR, MS_GAME_CODE],
["Content-Type: application/json"], ["Content-Type: application/json"],
true, HTTPClient.METHOD_GET) true, HTTPClient.METHOD_GET)
if error != OK: if error != OK:
@ -222,9 +222,9 @@ func _process(delta):
time_left = 30 time_left = 30
func _ms_response(_result: int, response_code: int, _headers: PoolStringArray, body: PoolByteArray, action: String): func _ms_response(_result: int, response_code: int, _headers: PoolStringArray, body: PoolByteArray, action: String):
print_debug("MS said " + str(response_code)) print_debug("MS said %s" % response_code)
if response_code > 299: if response_code > 299:
push_error("ms action \"" + action + "\" returned error: " + str(response_code) + " - " + body.get_string_from_utf8()) push_error("ms action '%s' returned error: %s - %s" % [action, response_code, body.get_string_from_utf8()])
return return
var json = JSON.parse(body.get_string_from_utf8()) var json = JSON.parse(body.get_string_from_utf8())
match action: match action:

View file

@ -46,4 +46,4 @@ func get_progress() -> String:
var current = 0 var current = 0
for path in queue.pending: for path in queue.pending:
current += queue.get_progress(path) current += queue.get_progress(path)
return "Loading scene (" + str(round(current/count*100)) + "%)" return "Loading scene (%.0f%%)" % (current/count*100)

View file

@ -34,8 +34,8 @@ func _physics_process(delta):
radar_next_remaining = RADAR_EFFECT_DELAY radar_next_remaining = RADAR_EFFECT_DELAY
if not visible: if not visible:
return return
speed_text.text = str(round(scene.world.map.current_ship_speed * 10000)) + " u/s" speed_text.text = "%.0f u/s" % (scene.world.map.current_ship_speed * 10000)
dir_text.text = str(round(rad2deg(scene.world.map.current_ship_direction))) + " deg" dir_text.text = "%.0f deg" % rad2deg(scene.world.map.current_ship_direction)
update() update()
func _draw(): func _draw():