Rename pending match to match config and add match state (#51)
CI / build-and-test (push) Successful in 1m17s
CI / production-build (push) Successful in 9m37s

Reviewed-on: #51
Co-authored-by: Álex Román Núñez <eirexe@eirteam.moe>
This commit was merged in pull request #51.
This commit is contained in:
2026-09-20 11:13:15 +00:00
committed by EIREXE
parent 769d82573a
commit 1276562890
7 changed files with 300 additions and 78 deletions
+126 -33
View File
@@ -8,28 +8,29 @@ use bevy_ggrs::prelude::Session;
use serde::{Deserialize, Serialize};
use crate::{
AppState,
args::Args,
fighter::{
FighterId, FighterRoster,
animation::AnimKind,
attack::{AttackKind, FighterAttackScriptAssets},
baked_animation::{BakedFighterAnimations, BAKED_ANIMATION_FPS},
baked_animation::{BAKED_ANIMATION_FPS, BakedFighterAnimations},
manifest::{FighterManifest, FighterManifestRegistry},
spawn_fighter,
visual::FighterAnimations,
FighterId, FighterRoster,
},
input::{LocalInputAssignments, LocalInputSource},
match_runtime::MatchState,
math::{int::FGi32, vec::FGVec2},
netcode::{
session::{create_session, ConnectionStatus, SessionConfig},
GGRSCfg,
session::{ConnectionStatus, SessionConfig, create_session},
},
scripting::FighterAttackScript,
stage::{
self,
manifest::{StageId, StageManifest, StageManifestRegistry},
},
AppState,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
@@ -38,10 +39,49 @@ pub struct MatchPlayer {
pub fighter: FighterId,
}
#[derive(Resource, Debug, Clone, PartialEq, Eq)]
pub struct PendingMatch {
#[derive(Resource, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MatchConfig {
pub stage: StageId,
pub players: Vec<MatchPlayer>,
pub rules: MatchRules,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum MatchRules {
Stock {
starting_stocks: u8,
time_limit_frames: Option<u32>,
},
Timed {
time_limit_frames: u32,
},
}
impl Default for MatchRules {
fn default() -> Self {
Self::Stock {
starting_stocks: 3,
time_limit_frames: None,
}
}
}
impl MatchRules {
fn validate(&self) -> Result<(), &'static str> {
match self {
Self::Stock {
starting_stocks: 0, ..
} => Err("stock matches must start with at least one stock"),
Self::Stock {
time_limit_frames: Some(0),
..
} => Err("stock match time limits must be greater than zero frames"),
Self::Timed {
time_limit_frames: 0,
} => Err("timed match limits must be greater than zero frames"),
_ => Ok(()),
}
}
}
pub struct SelectedFighterAssets {
@@ -59,7 +99,7 @@ pub struct MatchAssets {
impl MatchAssets {
fn requested(world: &World) -> Self {
let request = world.resource::<PendingMatch>();
let config = world.resource::<MatchConfig>();
let asset_server = world.resource::<AssetServer>();
let registry = world.resource::<FighterManifestRegistry>();
let stage_registry = world.resource::<StageManifestRegistry>();
@@ -67,7 +107,7 @@ impl MatchAssets {
let stage_manifests = world.resource::<Assets<StageManifest>>();
let stage_handle = stage_registry
.get(request.stage)
.get(config.stage)
.expect("Startup validation guarantees every stage has a manifest");
let stage_manifest = stage_manifests
.get(&stage_handle)
@@ -75,7 +115,7 @@ impl MatchAssets {
Self {
stage: asset_server.load(stage_manifest.model_path.clone()),
fighters: request
fighters: config
.players
.iter()
.map(|player| {
@@ -141,10 +181,10 @@ pub struct MatchRoot;
pub fn initiate_match(
commands: &mut Commands,
next_state: &mut NextState<AppState>,
request: PendingMatch,
config: MatchConfig,
) {
commands.remove_resource::<crate::netcode::session::MatchDisconnected>();
commands.insert_resource(request);
commands.insert_resource(config);
next_state.set(AppState::LoadingMatch);
}
@@ -166,12 +206,9 @@ pub fn initiate_default_match(
if let Some(path) = &args.play_replay {
match crate::replay::load(path) {
Ok(replay) => {
let request = PendingMatch {
stage: replay.match_config.stage,
players: replay.match_config.players.clone(),
};
let config = replay.match_config.clone();
commands.insert_resource(crate::replay::LoadedReplay(replay));
initiate_match(&mut commands, &mut next_state, request);
initiate_match(&mut commands, &mut next_state, config);
}
Err(error) => {
error!("Replay load failed: {error}");
@@ -215,16 +252,17 @@ pub fn initiate_default_match(
initiate_match(
&mut commands,
&mut next_state,
PendingMatch {
MatchConfig {
stage: StageId::TestStage,
players,
rules: MatchRules::default(),
},
);
}
pub fn prepare_match(
mut commands: Commands,
request: Res<PendingMatch>,
config: Res<MatchConfig>,
assets: Res<MatchAssets>,
gltfs: Res<Assets<Gltf>>,
baked_animations: Res<Assets<BakedFighterAnimations>>,
@@ -236,7 +274,7 @@ pub fn prepare_match(
args: Res<Args>,
mut next_state: ResMut<NextState<AppState>>,
) {
if !valid_player_handles(&request.players) {
if !valid_player_handles(&config.players) {
fail_match(
&mut next_state,
"player handles must be unique and cover 0..player_count",
@@ -244,7 +282,12 @@ pub fn prepare_match(
return;
}
if assets.fighters.len() != request.players.len() {
if let Err(error) = config.rules.validate() {
fail_match(&mut next_state, error);
return;
}
if assets.fighters.len() != config.players.len() {
fail_match(
&mut next_state,
"loaded character assets do not match the requested player slots",
@@ -396,7 +439,7 @@ pub fn prepare_match(
));
}
let session_config = match SessionConfig::from_args(&args, request.players.len()) {
let session_config = match SessionConfig::from_args(&args, config.players.len()) {
Ok(config) => config,
Err(error) => {
fail_match(
@@ -406,7 +449,7 @@ pub fn prepare_match(
return;
}
};
let session = match create_session(&args, request.players.len()) {
let session = match create_session(&args, config.players.len()) {
Ok(session) => session,
Err(error) => {
fail_match(
@@ -420,7 +463,7 @@ pub fn prepare_match(
let root = commands.spawn((Name::new("Match"), MatchRoot)).id();
let stage_handle = stage_registry
.get(request.stage)
.get(config.stage)
.expect("Initialization check should ensure all stages exist");
let stage_manifest = stage_manifests
.get(&stage_handle)
@@ -433,11 +476,11 @@ pub fn prepare_match(
stage_manifest.camera.clone(),
);
let mut fighter_roster = Vec::with_capacity(request.players.len());
let mut fighter_roster = Vec::with_capacity(config.players.len());
for (index, (player, (scene, animations, attack_scripts, manifest_handle, model_scale))) in
request.players.iter().zip(prepared).enumerate()
config.players.iter().zip(prepared).enumerate()
{
let spawn_x = (index as i32 * 2 + 1 - request.players.len() as i32) * 5;
let spawn_x = (index as i32 * 2 + 1 - config.players.len() as i32) * 5;
fighter_roster.push(spawn_fighter(
&mut commands,
player.handle,
@@ -451,6 +494,7 @@ pub fn prepare_match(
}
commands.insert_resource(FighterRoster(fighter_roster));
commands.insert_resource(MatchState::from_config(&config));
commands.insert_resource(session_config);
commands.insert_resource(ConnectionStatus::default());
commands.insert_resource::<Session<GGRSCfg>>(session);
@@ -487,7 +531,8 @@ pub fn cleanup_match(
commands.remove_resource::<ConnectionStatus>();
commands.remove_resource::<stage::line::StageCollision>();
commands.remove_resource::<MatchAssets>();
commands.remove_resource::<PendingMatch>();
commands.remove_resource::<MatchConfig>();
commands.remove_resource::<MatchState>();
commands.remove_resource::<FighterRoster>();
}
@@ -496,8 +541,8 @@ mod tests {
use super::*;
#[test]
fn match_request_keeps_duplicate_fighter_slots() {
let request = PendingMatch {
fn match_config_keeps_duplicate_fighter_slots() {
let config = MatchConfig {
stage: StageId::TestStage,
players: vec![
MatchPlayer {
@@ -509,12 +554,13 @@ mod tests {
fighter: FighterId::TestFighter,
},
],
rules: MatchRules::default(),
};
assert_eq!(request.players.len(), 2);
assert_eq!(request.players[0].handle, 0);
assert_eq!(request.players[1].handle, 1);
assert!(valid_player_handles(&request.players));
assert_eq!(config.players.len(), 2);
assert_eq!(config.players[0].handle, 0);
assert_eq!(config.players[1].handle, 1);
assert!(valid_player_handles(&config.players));
}
#[test]
@@ -533,6 +579,53 @@ mod tests {
assert!(!valid_player_handles(&players));
}
#[test]
fn match_rules_reject_zero_limits() {
assert!(
MatchRules::Stock {
starting_stocks: 0,
time_limit_frames: None,
}
.validate()
.is_err()
);
assert!(
MatchRules::Stock {
starting_stocks: 3,
time_limit_frames: Some(0),
}
.validate()
.is_err()
);
assert!(
MatchRules::Timed {
time_limit_frames: 0,
}
.validate()
.is_err()
);
}
#[test]
fn match_rules_accept_valid_modes() {
assert!(MatchRules::default().validate().is_ok());
assert!(
MatchRules::Stock {
starting_stocks: 3,
time_limit_frames: Some(60),
}
.validate()
.is_ok()
);
assert!(
MatchRules::Timed {
time_limit_frames: 60,
}
.validate()
.is_ok()
);
}
#[test]
fn match_loading_waits_for_attack_scripts() {
let mut attack_scripts = HashMap::new();