Auto-reconnect retries with the session's player name
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -90,9 +90,9 @@ impl Game<'_> {
|
||||
self.extras.fps.clear();
|
||||
}
|
||||
}
|
||||
/// Auto-reconnect: a connection to `address` starts.
|
||||
pub(super) fn extras_connecting(&mut self, address: &str) {
|
||||
self.extras.reconnect.connecting(address);
|
||||
/// Auto-reconnect: a connection to `address` as `name` starts.
|
||||
pub(super) fn extras_connecting(&mut self, address: &str, name: &str) {
|
||||
self.extras.reconnect.connecting(address, name);
|
||||
}
|
||||
/// Auto-reconnect: the server let the player in.
|
||||
pub(super) fn extras_joined(&mut self) {
|
||||
@@ -131,9 +131,8 @@ impl Game<'_> {
|
||||
}
|
||||
/// Auto-reconnect: connect again once the countdown is over.
|
||||
pub(super) fn extras_retry(&mut self) -> horizon_client::Result<()> {
|
||||
if let Some(address) = self.title.as_mut().and_then(|t| t.due_retry()) {
|
||||
let name = self.options.player_name.clone();
|
||||
self.connect_server(&address, &name)?;
|
||||
if let Some(retry) = self.title.as_mut().and_then(|t| t.due_retry()) {
|
||||
self.connect_server(&retry.address, &retry.name)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -162,7 +162,7 @@ impl Game<'_> {
|
||||
let auth = horizon_client::auth::current();
|
||||
let name = auth.as_ref().map_or(name, |a| a.name.as_str());
|
||||
let session = Session::connect(address, name, auth.clone(), view, lang.clone());
|
||||
self.extras_connecting(address);
|
||||
self.extras_connecting(address, name);
|
||||
let player_type = super::perspective::player_type(&root);
|
||||
let mut streaming = Streaming::new(
|
||||
Arc::new(NoTerrain),
|
||||
|
||||
@@ -599,14 +599,14 @@ impl Screen {
|
||||
}
|
||||
}
|
||||
/// Auto-reconnect's attempt, once its countdown is over.
|
||||
pub fn due_retry(&mut self) -> Option<String> {
|
||||
pub fn due_retry(&mut self) -> Option<crate::reconnect::Retry> {
|
||||
let Self::Disconnected { retry, .. } = self else {
|
||||
return None;
|
||||
};
|
||||
if !retry.as_ref()?.due(std::time::Instant::now()) {
|
||||
return None;
|
||||
}
|
||||
retry.take().map(|r| r.address)
|
||||
retry.take()
|
||||
}
|
||||
/// The focused text field, its id and maximum length (copy and paste).
|
||||
pub fn focused_text(&mut self) -> Option<(crate::clipboard::FieldId, &mut String, usize)> {
|
||||
|
||||
@@ -47,6 +47,8 @@ pub fn allowed(reason: &str, from_server: bool) -> bool {
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Tracker {
|
||||
address: Option<String>,
|
||||
/// The player name the connection used.
|
||||
name: String,
|
||||
/// Attempts already made since the last stable connection.
|
||||
attempts: usize,
|
||||
joined: Option<Instant>,
|
||||
@@ -55,6 +57,7 @@ pub struct Tracker {
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Retry {
|
||||
pub address: String,
|
||||
pub name: String,
|
||||
pub at: Instant,
|
||||
/// 1-based, of `DELAYS.len()`.
|
||||
pub attempt: usize,
|
||||
@@ -78,12 +81,14 @@ impl Retry {
|
||||
}
|
||||
}
|
||||
impl Tracker {
|
||||
/// A connection to `address` starts: a new address starts counting anew.
|
||||
pub fn connecting(&mut self, address: &str) {
|
||||
/// A connection to `address` as `name` starts: a new address starts
|
||||
/// counting anew.
|
||||
pub fn connecting(&mut self, address: &str, name: &str) {
|
||||
if self.address.as_deref() != Some(address) {
|
||||
self.address = Some(address.to_owned());
|
||||
self.attempts = 0;
|
||||
}
|
||||
self.name = name.to_owned();
|
||||
self.joined = None;
|
||||
}
|
||||
/// The server let the player in.
|
||||
@@ -113,6 +118,7 @@ impl Tracker {
|
||||
self.attempts += 1;
|
||||
Some(Retry {
|
||||
address,
|
||||
name: self.name.clone(),
|
||||
at: now + delay,
|
||||
attempt: self.attempts,
|
||||
})
|
||||
@@ -155,13 +161,16 @@ mod tests {
|
||||
fn attempts_back_off_and_stop() {
|
||||
let mut t = Tracker::default();
|
||||
let now = Instant::now();
|
||||
t.connecting("localhost:25565");
|
||||
t.connecting("localhost:25565", "Tester");
|
||||
let mut delays = Vec::new();
|
||||
while let Some(r) = t.ended("Connection refused", false, now) {
|
||||
assert_eq!(r.address, "localhost:25565");
|
||||
assert_eq!(
|
||||
(r.address.as_str(), r.name.as_str()),
|
||||
("localhost:25565", "Tester")
|
||||
);
|
||||
assert_eq!(r.attempt, delays.len() + 1);
|
||||
delays.push(r.at.duration_since(now).as_secs());
|
||||
t.connecting(&r.address);
|
||||
t.connecting(&r.address, &r.name);
|
||||
}
|
||||
assert_eq!(delays, DELAYS);
|
||||
// Out of attempts, the tracker forgets the address.
|
||||
@@ -172,26 +181,26 @@ mod tests {
|
||||
fn a_short_stay_does_not_reset_the_count() {
|
||||
let mut t = Tracker::default();
|
||||
let now = Instant::now();
|
||||
t.connecting("a");
|
||||
t.connecting("a", "P");
|
||||
for _ in 0..DELAYS.len() {
|
||||
t.connecting("a");
|
||||
t.connecting("a", "P");
|
||||
t.joined(now);
|
||||
assert!(
|
||||
t.ended("Server closed", true, now + Duration::from_secs(5))
|
||||
.is_some()
|
||||
);
|
||||
}
|
||||
t.connecting("a");
|
||||
t.connecting("a", "P");
|
||||
t.joined(now);
|
||||
assert_eq!(
|
||||
t.ended("Server closed", true, now + Duration::from_secs(5)),
|
||||
None
|
||||
);
|
||||
// A minute connected starts over.
|
||||
t.connecting("a");
|
||||
t.connecting("a", "P");
|
||||
for _ in 0..3 {
|
||||
t.ended("Timed out", true, now);
|
||||
t.connecting("a");
|
||||
t.connecting("a", "P");
|
||||
}
|
||||
t.joined(now);
|
||||
let r = t.ended("Timed out", true, now + STABLE).unwrap();
|
||||
@@ -202,7 +211,7 @@ mod tests {
|
||||
fn kicks_and_leaving_reset_and_new_addresses_start_over() {
|
||||
let mut t = Tracker::default();
|
||||
let now = Instant::now();
|
||||
t.connecting("a");
|
||||
t.connecting("a", "P");
|
||||
assert!(t.ended("Server closed", true, now).is_some());
|
||||
assert_eq!(t.ended("Kicked by an operator", true, now), None);
|
||||
assert_eq!(
|
||||
@@ -210,9 +219,9 @@ mod tests {
|
||||
None,
|
||||
"forgotten after a kick"
|
||||
);
|
||||
t.connecting("a");
|
||||
t.connecting("a", "P");
|
||||
t.ended("Server closed", true, now);
|
||||
t.connecting("b");
|
||||
t.connecting("b", "P");
|
||||
assert_eq!(t.ended("Server closed", true, now).unwrap().attempt, 1);
|
||||
let mut t = Tracker::default();
|
||||
assert_eq!(t.ended("Server closed", true, now), None, "never connected");
|
||||
@@ -223,6 +232,7 @@ mod tests {
|
||||
let now = Instant::now();
|
||||
let r = Retry {
|
||||
address: "a".into(),
|
||||
name: "P".into(),
|
||||
at: now + Duration::from_millis(4200),
|
||||
attempt: 2,
|
||||
};
|
||||
|
||||
@@ -594,7 +594,7 @@ impl Title {
|
||||
}
|
||||
}
|
||||
/// The server auto-reconnect retries now, once its countdown is over.
|
||||
pub fn due_retry(&mut self) -> Option<String> {
|
||||
pub fn due_retry(&mut self) -> Option<crate::reconnect::Retry> {
|
||||
match &mut self.mode {
|
||||
Mode::Multiplayer(screen) => screen.due_retry(),
|
||||
_ => None,
|
||||
|
||||
Reference in New Issue
Block a user