Invite friends works with no relay: the code, and what friends can expect
Sharing no longer needs a relay. The panel shows the full invite code in groups (monospace, wrapped) with Copy and Stop sharing, then who can join: friends anywhere when the router opened the port or this computer has a public address; otherwise what to do (UPnP, the port to forward and the LAN address, a relay, a VPN; CGNAT called out) with a link to the troubleshooting docs. A relay set in horizon.txt (friendsRelay) or HORIZON_RELAY_URL adds its short code. horizon.txt keeps other lines when the menu style is saved. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1023,12 +1023,15 @@ pub fn wrap(ui: &Ui, text: &str, width: f32) -> Vec<String> {
|
||||
pub struct FriendsView {
|
||||
/// A singleplayer world (not a server): the button is offered.
|
||||
pub shown: bool,
|
||||
/// A relay is configured in this build.
|
||||
pub available: bool,
|
||||
pub sharing: bool,
|
||||
pub code: Option<String>,
|
||||
/// While there is no code: getting one, or why not.
|
||||
/// Who can join and what to do when friends on the internet can't (or,
|
||||
/// before the code, what the game is doing).
|
||||
pub status: String,
|
||||
/// Friends on the internet can reach the host.
|
||||
pub reachable: bool,
|
||||
/// Where the troubleshooting is, when they can't.
|
||||
pub help: Option<String>,
|
||||
/// The players in the world besides the host.
|
||||
pub guests: Vec<String>,
|
||||
pub notice: Option<String>,
|
||||
@@ -1096,21 +1099,11 @@ pub mod pause {
|
||||
[x + bw + lw + 16.0, y, iw, CONTROL_LARGE],
|
||||
"Invite friends",
|
||||
Kind::Secondary,
|
||||
friends.available && !friends.sharing,
|
||||
!friends.sharing,
|
||||
);
|
||||
}
|
||||
y += CONTROL_LARGE + 32.0;
|
||||
ui.row_index = 0;
|
||||
if friends.shown && !friends.available {
|
||||
ui.text(
|
||||
Face::Regular,
|
||||
SMALL,
|
||||
x,
|
||||
y - 18.0,
|
||||
color::FAINT,
|
||||
"Inviting friends isn't available yet in this build.",
|
||||
);
|
||||
}
|
||||
if friends.sharing {
|
||||
y = invite(ui, x, y, cw, friends);
|
||||
}
|
||||
@@ -1137,6 +1130,20 @@ pub mod pause {
|
||||
}
|
||||
/// The invite section while sharing: the code with Copy and Stop
|
||||
/// sharing, who is here, and what changed. Returns the y below it.
|
||||
/// The code's groups packed into lines of at most `width`.
|
||||
fn code_lines(ui: &Ui, code: &str, width: f32) -> Vec<String> {
|
||||
let mut lines: Vec<String> = Vec::new();
|
||||
for group in code.split('-') {
|
||||
match lines.last_mut() {
|
||||
Some(l) if ui.measure(Face::Mono, BODY, &format!("{l}-{group}")) <= width => {
|
||||
l.push('-');
|
||||
l.push_str(group);
|
||||
}
|
||||
_ => lines.push(group.to_owned()),
|
||||
}
|
||||
}
|
||||
lines
|
||||
}
|
||||
fn invite(ui: &mut Ui, x: f32, mut y: f32, cw: f32, f: &FriendsView) -> f32 {
|
||||
ui.divider(x, y, cw);
|
||||
let top = y + 12.0;
|
||||
@@ -1149,15 +1156,6 @@ pub mod pause {
|
||||
"Invite code",
|
||||
);
|
||||
let vx = x + t::ROW_LABEL + 16.0;
|
||||
match &f.code {
|
||||
Some(code) => {
|
||||
ui.text_line(Face::Display, 22.0, CONTROL, vx, top, color::TEXT, code);
|
||||
}
|
||||
None => {
|
||||
let status = ui.fit(Face::Regular, BODY, &f.status, cw - t::ROW_LABEL - 220.0);
|
||||
ui.text_line(Face::Regular, BODY, CONTROL, vx, top, color::MUTED, &status);
|
||||
}
|
||||
}
|
||||
let stop = "Stop sharing";
|
||||
let sw = ui.button_width(stop, false);
|
||||
ui.button(
|
||||
@@ -1169,14 +1167,51 @@ pub mod pause {
|
||||
);
|
||||
let copy = if f.copied { "Copied" } else { "Copy" };
|
||||
let copy_w = ui.button_width("Copied", false);
|
||||
let buttons = sw + 8.0 + copy_w;
|
||||
ui.button(
|
||||
act(11),
|
||||
[x + cw - sw - 8.0 - copy_w, top, copy_w, CONTROL],
|
||||
[x + cw - buttons, top, copy_w, CONTROL],
|
||||
copy,
|
||||
Kind::Secondary,
|
||||
f.code.is_some(),
|
||||
);
|
||||
y = top + CONTROL + 10.0;
|
||||
let line = (BODY * 1.45).round();
|
||||
let value_w = (cw - t::ROW_LABEL - 16.0 - buttons - 16.0).max(120.0);
|
||||
y = top + 6.0;
|
||||
match &f.code {
|
||||
Some(code) => {
|
||||
for l in code_lines(ui, code, value_w) {
|
||||
ui.text(Face::Mono, BODY, vx, y, color::TEXT, &l);
|
||||
y += line;
|
||||
}
|
||||
}
|
||||
None => {
|
||||
for l in wrap(ui, &f.status, value_w) {
|
||||
ui.text(Face::Regular, BODY, vx, y, color::MUTED, &l);
|
||||
y += line;
|
||||
}
|
||||
}
|
||||
}
|
||||
y = y.max(top + CONTROL) + 10.0;
|
||||
let small = (SMALL * 1.45).round();
|
||||
if f.code.is_some() {
|
||||
// Who can join: plain when friends anywhere can, in the text
|
||||
// colour (it asks for something) when only the LAN and VPNs can.
|
||||
let tint = if f.reachable {
|
||||
color::MUTED
|
||||
} else {
|
||||
color::TEXT
|
||||
};
|
||||
for l in wrap(ui, &f.status, cw) {
|
||||
ui.text(Face::Regular, SMALL, x, y, tint, &l);
|
||||
y += small;
|
||||
}
|
||||
if let Some(help) = &f.help {
|
||||
let l = ui.fit(Face::Regular, SMALL, &format!("Help: {help}"), cw);
|
||||
ui.text(Face::Regular, SMALL, x, y, color::FAINT, &l);
|
||||
y += small;
|
||||
}
|
||||
}
|
||||
let here = if f.guests.is_empty() {
|
||||
"No friends here yet.".to_owned()
|
||||
} else {
|
||||
@@ -1192,7 +1227,7 @@ pub mod pause {
|
||||
for (line, tint) in lines.into_iter().flatten() {
|
||||
let line = ui.fit(Face::Regular, SMALL, &line, cw);
|
||||
ui.text(Face::Regular, SMALL, x, y, tint, &line);
|
||||
y += (SMALL * 1.45).round();
|
||||
y += small;
|
||||
}
|
||||
y + 12.0
|
||||
}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
//! Play with friends, host side: "Invite friends" opens the world to LAN if
|
||||
//! it isn't (with Open to LAN's defaults) and shares that port behind an
|
||||
//! invite code through the relay (horizon-friends). The integrated server is
|
||||
//! the LAN one, unchanged: tunnelled guests are LAN guests that connect from
|
||||
//! the loopback address.
|
||||
//! invite code (horizon-friends). The code holds this computer's addresses
|
||||
//! and the key, so friends join with no server of ours; a relay the player
|
||||
//! configured adds a short code and a fallback. The integrated server is the
|
||||
//! LAN one, unchanged: tunnelled guests are LAN guests that connect from the
|
||||
//! loopback address.
|
||||
use super::{Game, lan::LanSettings};
|
||||
use horizon_client::{Result, front::FriendsView, text::Text};
|
||||
use horizon_friends::{Code, Host, HostConfig, HostEvent, HostState, PathMode};
|
||||
use horizon_friends::{Host, HostConfig, HostEvent, PathMode, host::DEFAULT_PORT, reach, stun};
|
||||
use horizon_protocol::{GAME_VERSION, PROTOCOL_VERSION};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
@@ -13,21 +15,46 @@ pub(super) struct Sharing {
|
||||
host: Host,
|
||||
/// Sharing opened the world to LAN, so stopping closes it again.
|
||||
opened_lan: bool,
|
||||
code: Option<Code>,
|
||||
/// Wrong codes tried against the current code.
|
||||
code: Option<String>,
|
||||
/// Wrong codes tried so far.
|
||||
wrong: u32,
|
||||
/// Sessions carried directly and through the relay so far.
|
||||
paths: (u32, u32),
|
||||
notice: Option<String>,
|
||||
copied: Option<Instant>,
|
||||
/// Sessions carried directly and through the relay so far.
|
||||
paths: (u32, u32),
|
||||
}
|
||||
|
||||
pub const HELP: &[&str] = &[
|
||||
"friends host share the open world behind an invite code (needs HORIZON_RELAY_URL)",
|
||||
"friends host share the open world behind an invite code",
|
||||
"friends stop stop sharing",
|
||||
"friends status the code, the relay state and the guests",
|
||||
"friends status the code, how friends reach this host, the guests",
|
||||
];
|
||||
|
||||
/// The relay the player set (Join a friend's Relay field, `friendsRelay`
|
||||
/// in horizon.txt), else `HORIZON_RELAY_URL`. None by default.
|
||||
pub(super) fn relay_setting() -> Option<String> {
|
||||
let dir = std::env::current_dir().ok()?;
|
||||
horizon_client::ui_theme::preference(&dir, "friendsRelay")
|
||||
.filter(|r| !r.is_empty())
|
||||
.or_else(horizon_friends::relay_url)
|
||||
}
|
||||
|
||||
/// `HORIZON_STUN_SERVERS`, else `friendsStun` in horizon.txt, else the defaults.
|
||||
fn stun_servers() -> Vec<String> {
|
||||
if std::env::var_os("HORIZON_STUN_SERVERS").is_some() {
|
||||
return stun::servers_from_env();
|
||||
}
|
||||
let dir = std::env::current_dir().unwrap_or_default();
|
||||
match horizon_client::ui_theme::preference(&dir, "friendsStun") {
|
||||
Some(list) => stun::parse_servers(&list),
|
||||
None => stun::servers_from_env(),
|
||||
}
|
||||
}
|
||||
|
||||
fn env_flag(name: &str, default: bool) -> bool {
|
||||
std::env::var(name).map_or(default, |v| v != "0")
|
||||
}
|
||||
|
||||
impl Game<'_> {
|
||||
/// Start sharing the open world.
|
||||
pub(super) fn friends_start(&mut self) -> Result<()> {
|
||||
@@ -37,8 +64,6 @@ impl Game<'_> {
|
||||
if self.remote.is_some() || self.title.is_some() {
|
||||
return Err("open a singleplayer world first".into());
|
||||
}
|
||||
let relay = horizon_friends::relay_url()
|
||||
.ok_or("inviting friends is not available yet in this build")?;
|
||||
let opened_lan = self.lan.is_none();
|
||||
if opened_lan {
|
||||
// ShareToLanScreen's defaults, as the world options screen offers them.
|
||||
@@ -50,11 +75,22 @@ impl Game<'_> {
|
||||
}
|
||||
let port = self.lan_port().ok_or("the world isn't open to LAN")?;
|
||||
let host = match Host::start(HostConfig {
|
||||
relay,
|
||||
lan: std::net::SocketAddr::from(([127, 0, 0, 1], port)),
|
||||
protocol: PROTOCOL_VERSION,
|
||||
game: GAME_VERSION.to_owned(),
|
||||
relay: relay_setting(),
|
||||
path: PathMode::from_env(),
|
||||
port: std::env::var("HORIZON_FRIENDS_PORT")
|
||||
.ok()
|
||||
.and_then(|p| p.parse().ok())
|
||||
.unwrap_or(DEFAULT_PORT),
|
||||
reach: reach::Options {
|
||||
// HORIZON_FRIENDS_UPNP=0 leaves the router alone.
|
||||
map_port: env_flag("HORIZON_FRIENDS_UPNP", true),
|
||||
stun: stun_servers(),
|
||||
// Both ends on one machine (tests).
|
||||
loopback: env_flag("HORIZON_FRIENDS_LOOPBACK", false),
|
||||
},
|
||||
}) {
|
||||
Ok(h) => h,
|
||||
Err(e) => {
|
||||
@@ -69,15 +105,15 @@ impl Game<'_> {
|
||||
opened_lan,
|
||||
code: None,
|
||||
wrong: 0,
|
||||
paths: (0, 0),
|
||||
notice: None,
|
||||
copied: None,
|
||||
paths: (0, 0),
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Stop sharing: the code ends; the world closes to LAN again if sharing
|
||||
/// opened it.
|
||||
/// Stop sharing: the code ends (direct port closed, router mapping
|
||||
/// deleted); the world closes to LAN again if sharing opened it.
|
||||
pub(super) fn friends_stop(&mut self) {
|
||||
let Some(mut sharing) = self.friends.take() else {
|
||||
return;
|
||||
@@ -97,44 +133,37 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// New codes and the relay's state, once a tick and while the pause menu
|
||||
/// is drawn.
|
||||
/// New codes and sessions, once a tick and while the pause menu is drawn.
|
||||
pub(super) fn tick_friends(&mut self) {
|
||||
let Some(sharing) = self.friends.as_mut() else {
|
||||
return;
|
||||
};
|
||||
let mut announce = None;
|
||||
let mut announce = false;
|
||||
for event in sharing.host.poll() {
|
||||
match event {
|
||||
HostEvent::Code(code) => {
|
||||
if sharing.code.is_some() {
|
||||
sharing.notice =
|
||||
Some(if sharing.wrong >= horizon_friends::host::MAX_WRONG {
|
||||
"Someone kept trying a wrong code, so the code changed.".to_owned()
|
||||
} else {
|
||||
"The invite service restarted, so the code changed.".to_owned()
|
||||
});
|
||||
if sharing.code.is_some() && sharing.wrong >= horizon_friends::host::MAX_WRONG {
|
||||
sharing.notice = Some(
|
||||
"Someone kept trying a wrong short code, so it changed.".to_owned(),
|
||||
);
|
||||
sharing.wrong = 0;
|
||||
}
|
||||
sharing.wrong = 0;
|
||||
announce = Some(code.to_string());
|
||||
announce = sharing.code.is_none();
|
||||
sharing.code = Some(code);
|
||||
}
|
||||
HostEvent::WrongCode { count } => {
|
||||
sharing.wrong = count;
|
||||
sharing.notice = Some(format!("Someone tried a wrong code ({count} of 5)."));
|
||||
sharing.notice = Some("Someone tried a wrong code.".to_owned());
|
||||
}
|
||||
HostEvent::Connected { direct: true, .. } => sharing.paths.0 += 1,
|
||||
HostEvent::Connected { direct: false, .. } => sharing.paths.1 += 1,
|
||||
HostEvent::Disconnected { .. } => {}
|
||||
}
|
||||
}
|
||||
if let HostState::Retrying(_) = sharing.host.state() {
|
||||
sharing.code = None;
|
||||
}
|
||||
if let Some(code) = announce {
|
||||
self.system_message(Text::literal(format!(
|
||||
"Friends can join with the code {code}"
|
||||
)));
|
||||
if announce {
|
||||
self.system_message(Text::literal(
|
||||
"Your invite code is ready in the pause menu: copy it and send it to your friends.",
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,7 +172,7 @@ impl Game<'_> {
|
||||
return;
|
||||
};
|
||||
use horizon_client::clipboard::Clipboard;
|
||||
self.clipboard.set(&code.to_string());
|
||||
self.clipboard.set(&code);
|
||||
if let Some(s) = self.friends.as_mut() {
|
||||
s.copied = Some(Instant::now());
|
||||
}
|
||||
@@ -159,21 +188,33 @@ impl Game<'_> {
|
||||
let Some(s) = &self.friends else {
|
||||
return FriendsView {
|
||||
shown: self.remote.is_none(),
|
||||
available: horizon_friends::relay_url().is_some(),
|
||||
..FriendsView::default()
|
||||
};
|
||||
};
|
||||
let status = match s.host.state() {
|
||||
HostState::Registering => "Getting a code…".to_owned(),
|
||||
HostState::Retrying(e) => format!("Couldn't reach the invite service; retrying ({e})"),
|
||||
HostState::Sharing(_) | HostState::Stopped => String::new(),
|
||||
let state = s.host.state();
|
||||
let mut status = if s.code.is_none() {
|
||||
"Finding how friends can reach you…".to_owned()
|
||||
} else {
|
||||
state.summary.clone()
|
||||
};
|
||||
if let Some(short) = &state.short {
|
||||
status.push_str(&format!(" Short code for the relay: {short}."));
|
||||
}
|
||||
if let Some(problem) = &state.relay_problem {
|
||||
status.push(' ');
|
||||
status.push_str(problem);
|
||||
}
|
||||
let reachable = matches!(
|
||||
state.verdict,
|
||||
Some(reach::Verdict::Mapped(_) | reach::Verdict::Public)
|
||||
) || state.short.is_some();
|
||||
FriendsView {
|
||||
shown: true,
|
||||
available: true,
|
||||
sharing: true,
|
||||
code: s.code.as_ref().map(Code::to_string),
|
||||
code: s.code.clone(),
|
||||
status,
|
||||
reachable,
|
||||
help: (!reachable && s.code.is_some()).then(|| reach::HELP_URL.to_owned()),
|
||||
guests,
|
||||
notice: s.notice.clone(),
|
||||
copied: s
|
||||
@@ -200,15 +241,15 @@ impl Game<'_> {
|
||||
Some("status") => {
|
||||
self.tick_friends();
|
||||
let view = self.friends_view();
|
||||
let state = self
|
||||
.friends
|
||||
.as_ref()
|
||||
.map(|s| format!("{:?}", s.host.state()));
|
||||
let state = self.friends.as_ref().map(|s| s.host.state());
|
||||
Ok(Some(json!({
|
||||
"available": view.available,
|
||||
"sharing": view.sharing,
|
||||
"code": view.code,
|
||||
"state": state,
|
||||
"short": state.as_ref().and_then(|s| s.short.as_ref().map(ToString::to_string)),
|
||||
"summary": view.status,
|
||||
"reachable": view.reachable,
|
||||
"verdict": state.as_ref().and_then(|s| s.verdict.as_ref().map(|v| format!("{v:?}"))),
|
||||
"direct_port": state.as_ref().map(|s| s.port),
|
||||
"sessions": self.friends.as_ref().map_or(0, |s| s.host.sessions()),
|
||||
"direct": self.friends.as_ref().map_or(0, |s| s.paths.0),
|
||||
"relayed": self.friends.as_ref().map_or(0, |s| s.paths.1),
|
||||
|
||||
@@ -71,7 +71,30 @@ pub fn save_preferences(dir: &Path) {
|
||||
MenuStyle::Horizon => "horizon",
|
||||
MenuStyle::Classic => "classic",
|
||||
};
|
||||
let _ = std::fs::write(dir.join("horizon.txt"), format!("menuStyle:{value}\n"));
|
||||
set_preference(dir, "menuStyle", Some(value));
|
||||
}
|
||||
/// A Horizon-only preference from `horizon.txt` (`key:value` lines).
|
||||
pub fn preference(dir: &Path, key: &str) -> Option<String> {
|
||||
let text = std::fs::read_to_string(dir.join("horizon.txt")).ok()?;
|
||||
text.lines().find_map(|l| {
|
||||
let (k, v) = l.split_once(':')?;
|
||||
(k.trim() == key).then(|| v.trim().to_owned())
|
||||
})
|
||||
}
|
||||
/// Set (or with None remove) one preference, keeping the others.
|
||||
pub fn set_preference(dir: &Path, key: &str, value: Option<&str>) {
|
||||
let text = std::fs::read_to_string(dir.join("horizon.txt")).unwrap_or_default();
|
||||
let mut lines: Vec<String> = text
|
||||
.lines()
|
||||
.filter(|l| l.split_once(':').is_none_or(|(k, _)| k.trim() != key))
|
||||
.map(str::to_owned)
|
||||
.collect();
|
||||
if let Some(v) = value {
|
||||
lines.push(format!("{key}:{v}"));
|
||||
}
|
||||
let mut out = lines.join("\n");
|
||||
out.push('\n');
|
||||
let _ = std::fs::write(dir.join("horizon.txt"), out);
|
||||
}
|
||||
|
||||
/// The dark palette (`horizon_theme::DARK`) in the HUD's colour encoding, plus the
|
||||
|
||||
Reference in New Issue
Block a user