Merge pull request #2298 from carlosnunez09/fix/leave-game-statistic

🤖 Agent: Merged by hand by Ludovic's AI agent (orchestrator) after the contribution gate. rdbtCVS's change request is addressed at this head; a re-review was asked, with no new objection since. Proven in wave 5's union run (cg20 union C 5900ab38793aa602315091ab4873615e93dda57d (main b6f18dc8f + 57 PRs) Mac mini 122/122 delta harness).
This commit is contained in:
Ludovic Marie
2026-10-27 12:16:09 +01:00
committed by GitHub
5 changed files with 74 additions and 7 deletions
@@ -420,6 +420,8 @@ impl Game<'_> {
s.flags.published.store(0, Ordering::Relaxed);
}
if let Some(mut host) = self.lan.take() {
// PlayerList.removeAll: the owner and every guest leave the game.
host.award_leave_game_to_all();
for g in &host.hub.guests {
self.save_guest(&host, g);
}
+31
View File
@@ -92,6 +92,33 @@ pub(super) struct LanHost {
states: lan::entity::States,
}
impl LanHost {
/// `PlayerList.remove`: `player.awardStat(Stats.LEAVE_GAME)` before the
/// player is saved. Once per guest, however often they are saved.
pub(super) fn award_leave_game(&mut self, conn: u32) {
if let Some(play) = self.play.get_mut(&conn)
&& !play.left_game
{
play.left_game = true;
play.stats.custom("leave_game", 1);
}
}
/// `PlayerList.removeAll`: every player who joined leaves with the world.
pub(super) fn award_leave_game_to_all(&mut self) {
let conns: Vec<u32> = self
.hub
.guests
.iter()
.filter(|g| g.placed)
.map(|g| g.conn)
.collect();
for conn in conns {
self.award_leave_game(conn);
}
}
}
/// The player who owns an integrated server's world: a guest whose state is
/// the world's own player (`players/data/<singleplayer_uuid>.dat`), as in
/// single player.
@@ -731,6 +758,7 @@ impl Game<'_> {
let Some(mut host) = self.lan.take() else {
return;
};
host.award_leave_game_to_all();
for g in &host.hub.guests {
self.save_guest(&host, g);
}
@@ -1150,6 +1178,9 @@ impl Game<'_> {
}
}
self.lan_crafting_left(&mut host, &mut guest);
if guest.placed {
host.award_leave_game(guest.conn);
}
self.save_guest(&host, &guest);
host.models.remove(&guest.conn);
host.play.remove(&guest.conn);
@@ -74,6 +74,9 @@ pub(super) struct GuestPlay {
potions: super::potions::BodyPotions,
/// `ServerPlayer.getStats` and the inventory `inventory_changed` last saw.
pub(super) stats: super::stats::Stats,
/// `Stats.LEAVE_GAME` is counted: a guest is saved more than once on the
/// way out, the statistic is awarded once (`LanHost::award_leave_game`).
pub(super) left_game: bool,
last_inventory: u64,
/// `Entity.portalProcess` and `portalCooldown` (game/server_travel.rs).
pub(super) portal: horizon_client::nether_portal::PortalState,
@@ -576,16 +576,18 @@ impl Game<'_> {
/// A guest left: what their crafting grids and workstation held goes back into their
/// inventory before it is saved (dropped where they stood if full).
pub(super) fn lan_crafting_left(&mut self, host: &mut LanHost, g: &mut Guest) {
let play = host.play.remove(&g.conn);
// The guest's play state stays: their statistics are awarded and saved
// after this (`HostEvent::Left`), which removes it.
let play = host.play.get_mut(&g.conn);
let mut grid: Vec<ItemStack> = g.craft_grid.iter_mut().filter_map(Option::take).collect();
g.craft_result = None;
if let Some(play) = play {
if let Some((_, _, menu, _)) = play.crafting.table {
if let Some((_, _, menu, _)) = play.crafting.table.take() {
grid.extend((1..10).filter_map(|s| menu.slot(s).cloned()));
}
// A workstation's inputs too.
if let Some(station) = &play.station {
grid.extend(super::lan_stations::station_inputs(station));
if let Some(station) = play.station.take() {
grid.extend(super::lan_stations::station_inputs(&station));
}
}
if grid.is_empty() {
+32 -3
View File
@@ -24,7 +24,7 @@ address. The script then checks, from both sides, what a guest's game used to ge
- pvp: the guest and the host hit each other with a sword; with the pvp game rule off, or on one
team without friendly fire, nothing;
- kills: a pig the guest kills counts in the guest's killed statistic, not the host's (#484);
- leave: a guest who leaves has leave_game in their saved statistics, and again after the next leave;
- arrows: the guest and the host shoot each other with a bow; with the pvp game rule off, nothing (#484);
- looks: mobs look on both sides as the server has them (their synched data): a calf is a
@@ -301,7 +301,7 @@ GUEST_NAME = 'Guest'
# Match normal single player; --in-frame is the comparison path.
SERVER_THREAD = True
CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'spawned', 'sign', 'damage', 'mobs', 'hazards', 'pvp',
'arrows', 'kills', 'hurt', 'looks', 'leads', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'styles',
'arrows', 'kills', 'leave', 'hurt', 'looks', 'leads', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'styles',
'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing',
'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push',
'piston-push', 'piston-slime', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'thrown', 'sparks', 'hud', 'sidebar', 'border', 'particles',
@@ -1196,6 +1196,35 @@ def check_pvp(host, guest, out):
return hits
def leave_game_total(host):
"""The leave_game statistic summed over the stats files the host's world holds."""
total = 0
for path in (host.work / 'saves' / 'Lan' / 'players' / 'stats').glob('*.json'):
custom = json.loads(path.read_text()).get('stats', {}).get('minecraft:custom', {})
total += custom.get('minecraft:leave_game', 0)
return total
def check_leave(host, guest, out):
# PlayerList.remove awards Stats.LEAVE_GAME before it saves the player: a guest who leaves has it in
# their saved statistics, and it counts again the next time they leave.
port = host.raw('lan', 'status')['port']
before = leave_game_total(host)
counts = []
for _ in range(2):
guest.raw('disconnect')
for _ in range(20):
time.sleep(0.5)
if leave_game_total(host) > (counts[-1] if counts else before):
break
counts.append(leave_game_total(host))
guest.raw('connect', f'127.0.0.1:{port}', GUEST_NAME)
guest.raw('wait-for', 'loaded', '--timeout', 300)
guest.wait(20)
assert counts == [before + 1, before + 2], f'leave_game was {before}, then {counts} after two leaves'
return {'leave_game': counts}
def check_kills(host, guest, out):
# A mob the guest kills is the guest's kill (#484): LivingEntity.die credits lastHurtByPlayer, so the
# killed statistic (and its scoreboard criterion) counts for the guest, not the host.
@@ -4938,7 +4967,7 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour,
'light': check_light, 'plates': check_plates, 'swings': check_swings,
'cracks': check_cracks, 'time': check_time, 'growth': check_growth, 'doors': check_doors,
'flight': check_flight, 'fireball': check_fireball, 'deflect': check_deflect,
'barter': check_barter, 'worn': check_worn, 'kills': check_kills,
'barter': check_barter, 'worn': check_worn, 'kills': check_kills, 'leave': check_leave,
'arrows': check_arrows, 'predict': check_predict, 'ladder': check_ladder,
'boats': check_boats, 'minecarts': check_minecarts, 'frames': check_frames, 'hands': check_hands,
'lightning': check_lightning, 'riding': check_riding, 'occupied_horse': check_occupied_horse, 'mounts': check_mounts,