use crate::{Frame, NULL_FRAME};
#[derive(Debug, Clone)]
pub(crate) struct GameState<S> {
pub frame: Frame,
pub data: Option<S>,
pub checksum: Option<u128>,
}
impl<S> Default for GameState<S> {
fn default() -> Self {
Self {
frame: NULL_FRAME,
data: None,
checksum: None,
}
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub(crate) struct PlayerInput<I>
where
I: Copy + Clone + PartialEq,
{
pub frame: Frame,
pub input: I,
}
impl<I: Copy + Clone + PartialEq + Default> PlayerInput<I> {
pub(crate) fn new(frame: Frame, input: I) -> Self {
Self { frame, input }
}
pub(crate) fn blank_input(frame: Frame) -> Self {
Self {
frame,
input: I::default(),
}
}
pub(crate) fn input_matches(&self, other: &Self) -> bool {
self.input == other.input
}
#[cfg(test)]
pub(crate) fn matches_full(&self, other: &Self) -> bool {
self.frame == other.frame && self.input == other.input
}
}
#[cfg(test)]
mod game_input_tests {
use super::*;
#[repr(C)]
#[derive(Copy, Clone, PartialEq, Default)]
struct TestInput {
inp: u8,
}
#[test]
fn test_input_equality() {
let input1 = PlayerInput::new(0, TestInput { inp: 5 });
let input2 = PlayerInput::new(0, TestInput { inp: 5 });
assert!(input1.matches_full(&input2));
}
#[test]
fn test_input_equality_input_only() {
let input1 = PlayerInput::new(0, TestInput { inp: 5 });
let input2 = PlayerInput::new(5, TestInput { inp: 5 });
assert!(input1.input_matches(&input2)); }
#[test]
fn test_input_equality_fail() {
let input1 = PlayerInput::new(0, TestInput { inp: 5 });
let input2 = PlayerInput::new(0, TestInput { inp: 7 });
assert!(!input1.matches_full(&input2)); }
#[test]
fn test_input_equality_frame_mismatch_not_input_only() {
let input1 = PlayerInput::new(0, TestInput { inp: 5 });
let input2 = PlayerInput::new(1, TestInput { inp: 5 });
assert!(!input1.matches_full(&input2));
}
#[test]
fn test_blank_input() {
let input = PlayerInput::<TestInput>::blank_input(7);
assert_eq!(input.frame, 7);
assert_eq!(input.input.inp, 0);
}
#[test]
fn test_new_stores_frame_and_input() {
let input = PlayerInput::new(42, TestInput { inp: 99 });
assert_eq!(input.frame, 42);
assert_eq!(input.input.inp, 99);
}
}