Navigation Menu

Skip to content

Commit

Permalink
Black Hole ~ Green Sun
Browse files Browse the repository at this point in the history
  • Loading branch information
leo60228 committed Oct 24, 2020
1 parent 9b3a79b commit 7750873
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
17 changes: 17 additions & 0 deletions idol_api/src/models.rs
Expand Up @@ -5,6 +5,8 @@ pub struct PitchingStats {
pub player_id: String,
#[serde(with = "serde_with::rust::display_fromstr")]
pub k_per_9: f64,
#[serde(with = "serde_with::rust::display_fromstr")]
pub games: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -58,6 +60,7 @@ pub struct Game {
pub away_odds: f64,
pub home_odds: f64,
pub inning: usize,
pub season: usize,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
Expand All @@ -72,6 +75,7 @@ pub struct Simulation {
#[serde(rename_all = "camelCase")]
pub struct Games {
pub sim: Simulation,
pub schedule: Vec<Game>,
pub tomorrow_schedule: Vec<Game>,
}

Expand Down Expand Up @@ -102,3 +106,16 @@ pub struct Team {
pub struct Idol {
pub player_id: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GameUpdates {
pub next_page: String,
pub data: Vec<GameUpdate>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GameUpdate {
pub data: Game,
}
24 changes: 19 additions & 5 deletions idol_api/src/state.rs
@@ -1,5 +1,6 @@
use super::models::{
AtBatLeader, Event, Game, Idol, PitchingStats, Position, StrikeoutLeader, Team,
AtBatLeader, Event, Game, GameUpdate, GameUpdates, Idol, PitchingStats, Position,
StrikeoutLeader, Team,
};
use anyhow::Result;
use log::*;
Expand All @@ -14,14 +15,19 @@ pub struct State {
pub players: Vec<Position>,
pub games: Vec<Game>,
pub idols: Vec<Idol>,
pub black_hole_sun_2: Vec<GameUpdate>,
pub season: usize,
}

impl State {
pub fn from_event(data: &Event) -> Result<Self> {
Self::from_games_and_season(
data.value.games.tomorrow_schedule.clone(),
data.value.games.sim.season,
)
let games = if data.value.games.tomorrow_schedule.len() == 0 {
warn!("No games scheduled, checking current games");
data.value.games.schedule.clone()
} else {
data.value.games.tomorrow_schedule.clone()
};
Self::from_games_and_season(games, data.value.games.sim.season)
}

pub fn from_games_and_season(games: Vec<Game>, season: usize) -> Result<Self> {
Expand Down Expand Up @@ -68,6 +74,12 @@ impl State {
.send()?
.json::<Positions>()?
.data;
debug!("Getting Sun 2 and Black Hole events");
let black_hole_sun_2 = client
.get("https://api.sibr.dev/chronicler/v1/games/updates?search=%22Sun%202%22%20or%20%22Black%20Hole%22&count=1000&order=desc")
.send()?
.json::<GameUpdates>()?
.data;
let idols = client
.get("https://www.blaseball.com/api/getIdols")
.send()?
Expand All @@ -80,6 +92,8 @@ impl State {
players,
games,
idols,
black_hole_sun_2,
season,
})
}
}
7 changes: 7 additions & 0 deletions idol_api/src/team_pair.rs
Expand Up @@ -65,6 +65,13 @@ impl<T> TeamPair<T> {
}
}

pub fn any<F>(self, mut func: F) -> bool
where
F: FnMut(T) -> bool,
{
func(self.home) || func(self.away)
}

pub fn as_ref(&self) -> TeamPair<&T> {
TeamPair {
home: &self.home,
Expand Down
5 changes: 4 additions & 1 deletion idol_historical/src/main.rs
Expand Up @@ -89,6 +89,7 @@ impl StatState {
.map(|x| PitchingStats {
player_id: x.0.clone(),
k_per_9: (x.1.strikeouts * 9) as f64 / x.1.innings_pitched as f64,
games: 0,
})
.collect();
let mut games = Vec::new();
Expand All @@ -110,7 +111,9 @@ impl StatState {
teams,
players,
games,
idols: vec![], // TODO
idols: vec![], // TODO
black_hole_sun_2: vec![], // TODO
season: 0, // TODO
})
}
}
Expand Down
16 changes: 16 additions & 0 deletions idol_predictor/src/algorithms.rs
Expand Up @@ -170,6 +170,21 @@ algorithm!(NAME_LENGTH, "name length", [], Unforbidden, |x| {
x.player.name.len() as f64
});

algorithm!(GAMES_PER_GAME, "games per game", [], Unforbidden, |x| {
let normal_games = x.stats?.games;
let extra = x
.state
.black_hole_sun_2
.iter()
.map(|y| &y.data)
.take_while(|y| y.season == x.state.season)
.filter_map(|y| y.pitcher_ids())
.filter(|y| y.any(|z| z == x.id))
.count();
let games = normal_games + extra;
games as f64 / normal_games as f64
});

pub const ALGORITHMS: &[Algorithm] = &[SO9, RUTHLESSNESS, STAT_RATIO];

pub const JOKE_ALGORITHMS: &[Algorithm] = &[
Expand All @@ -180,4 +195,5 @@ pub const JOKE_ALGORITHMS: &[Algorithm] = &[
IDOLS,
BATTING_STARS,
NAME_LENGTH,
GAMES_PER_GAME,
];

0 comments on commit 7750873

Please sign in to comment.