Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reversed score cycle function for season 7 #542

Merged
merged 3 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Unreleased
- Change inner u8 of `RoomCoordinate` to private
- Use `f64` instead of `u64` to work around bindgen expecting `BigInt` return values

### Additions:

- Add `s7_score_cycle_at_tick` seasonal constant function to reflect the reversed score cycle in
season 7

0.22.0 (2024-08-27)
===================

Expand Down
45 changes: 43 additions & 2 deletions src/constants/seasonal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ pub mod season_1 {
}
}

/// Calculates the state of the score cycle for season 1
pub const fn score_cycle_at_tick(tick: u32) -> ScoreCycleState {
match tick % SCORE_CYCLE_DURATION {
// the bonus/crisis periods are exclusive of their boundaries
Expand All @@ -120,12 +121,29 @@ pub mod season_1 {
}
}

/// Calculates the state of the score cycle for season 7, which reverses the
/// crisis/bonus order
pub const fn s7_score_cycle_at_tick(tick: u32) -> ScoreCycleState {
match tick % SCORE_CYCLE_DURATION {
// the bonus/crisis periods are exclusive of their boundaries
// https://github.com/screeps/mod-season1/blob/7ca3c7ddb47bf9dfbdfb4e72b666a3159fde8780/src/scoreContainer.roomObject.js#L77-L81
// match on those exact values first
SCORE_CYCLE_CRISIS_START => ScoreCycleState::Normal,
SCORE_CYCLE_BONUS_START => ScoreCycleState::Normal,
// then on the remaining ranges - these are flipped from normal in s7, which is
// not currently open sourced
SCORE_CYCLE_CRISIS_START..SCORE_CYCLE_CRISIS_END => ScoreCycleState::Bonus,
SCORE_CYCLE_BONUS_START..SCORE_CYCLE_BONUS_END => ScoreCycleState::Crisis,
_ => ScoreCycleState::Normal,
}
}

#[cfg(test)]
mod test {
use super::{score_cycle_at_tick, ScoreCycleState};
use super::{s7_score_cycle_at_tick, score_cycle_at_tick, ScoreCycleState};

#[test]
fn score_cycle() {
fn s1_score_cycle() {
assert_eq!(score_cycle_at_tick(0), ScoreCycleState::Normal);
assert_eq!(score_cycle_at_tick(10_000), ScoreCycleState::Normal);
assert_eq!(score_cycle_at_tick(10_001), ScoreCycleState::Crisis);
Expand All @@ -146,6 +164,29 @@ pub mod season_1 {
assert_eq!(score_cycle_at_tick(249_999), ScoreCycleState::Bonus);
assert_eq!(score_cycle_at_tick(250_000), ScoreCycleState::Normal);
}

#[test]
fn s7_score_cycle() {
assert_eq!(s7_score_cycle_at_tick(0), ScoreCycleState::Normal);
assert_eq!(s7_score_cycle_at_tick(10_000), ScoreCycleState::Normal);
assert_eq!(s7_score_cycle_at_tick(10_001), ScoreCycleState::Bonus);
assert_eq!(s7_score_cycle_at_tick(14_999), ScoreCycleState::Bonus);
assert_eq!(s7_score_cycle_at_tick(15_000), ScoreCycleState::Normal);
assert_eq!(s7_score_cycle_at_tick(45_000), ScoreCycleState::Normal);
assert_eq!(s7_score_cycle_at_tick(45_001), ScoreCycleState::Crisis);
assert_eq!(s7_score_cycle_at_tick(49_999), ScoreCycleState::Crisis);
assert_eq!(s7_score_cycle_at_tick(50_000), ScoreCycleState::Normal);

assert_eq!(s7_score_cycle_at_tick(200_000), ScoreCycleState::Normal);
assert_eq!(s7_score_cycle_at_tick(210_000), ScoreCycleState::Normal);
assert_eq!(s7_score_cycle_at_tick(210_001), ScoreCycleState::Bonus);
assert_eq!(s7_score_cycle_at_tick(214_999), ScoreCycleState::Bonus);
assert_eq!(s7_score_cycle_at_tick(215_000), ScoreCycleState::Normal);
assert_eq!(s7_score_cycle_at_tick(245_000), ScoreCycleState::Normal);
assert_eq!(s7_score_cycle_at_tick(245_001), ScoreCycleState::Crisis);
assert_eq!(s7_score_cycle_at_tick(249_999), ScoreCycleState::Crisis);
assert_eq!(s7_score_cycle_at_tick(250_000), ScoreCycleState::Normal);
}
}
}

Expand Down