FBSim Core
A library for american football simulation
Rust Docs: fbsim-core Rust crate documentation
TypeDocs: fbsim-core TypeScript & JavaScript WASM module documentation
Contributing: CONTRIBUTING.md
Overview
Provides utilities for simulating american football games and leagues. The core library is written in Rust; it also compiles into a WebAssembly module for use in JavaScript and TypeScript projects. It is based on various statistical models derived in repositories
Example
Below is a quick example of running a play-by-play football simulation using fbsim-core. It includes equivalent examples, one in Rust using the fbsim-core Rust crate, the other in JavaScript using the fbsim-core NPM package.
Rust
Here is a quick example of simulating a play-by-play game between two teams in Rust.
use GameContext;
use GameSimulator;
use FootballTeam;
// Instantiate the home and away teams, game context
let home_team = new;
let away_team = new;
let context = new;
// Instantiate the rng, simulator, and simulate the game
let mut rng = thread_rng;
let sim = new;
let = sim.sim.unwrap;
// Print the game log
println!;
println!;
JavaScript & TypeScript
The same play-by-play simulation is available in JavaScript and TypeScript via WebAssembly.
import init, {
FootballTeam,
Game,
GameSimulator,
Rng,
createGameContext,
} from "@whatsacomputertho/fbsim-core";
// Initialize the WASM module
await init();
// Create teams with overall offensive & defensive ratings
const home = new FootballTeam();
const away = new FootballTeam();
// Set up game state
const rng = new Rng();
const simulator = new GameSimulator();
const game = new Game();
let ctx = createGameContext();
// Simulate play-by-play and print the game log
while (!game.complete) {
ctx = simulator.simPlay(home, away, ctx, game, rng);
const play = game.getLatestPlay();
console.log(play.description);
}
console.log("Game Over");
Installing
Rust
JavaScript & TypeScript