1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/nimble-rust/nimble
 * Licensed under the MIT License. See LICENSE in the project root for license information.
 */
pub mod prelude;

use log::trace;
use std::marker::PhantomData;

use nimble_steps::Steps;
use tick_id::TickId;

pub trait SeerCallback<CombinedStepT> {
    fn on_pre_ticks(&mut self) {}

    fn on_tick(&mut self, step: &CombinedStepT);

    fn on_post_ticks(&mut self) {}
}

// Define the Assent struct
impl<Callback, CombinedStepT> Default for Seer<Callback, CombinedStepT>
where
    Callback: SeerCallback<CombinedStepT>,
{
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Debug)]
pub struct Seer<Callback, CombinedStepT>
where
    Callback: SeerCallback<CombinedStepT>,
{
    combined_steps: Steps<CombinedStepT>,
    authoritative_has_changed: bool,
    phantom: PhantomData<Callback>,
}

impl<Callback, CombinedStepT> Seer<Callback, CombinedStepT>
where
    Callback: SeerCallback<CombinedStepT>,
{
    pub fn new() -> Self {
        Seer {
            combined_steps: Steps::new(),
            phantom: PhantomData,
            authoritative_has_changed: false,
        }
    }

    pub fn predicted_steps(&self) -> &Steps<CombinedStepT> {
        &self.combined_steps
    }

    pub fn update(&mut self, callback: &mut Callback) {
        callback.on_pre_ticks();

        trace!("combined steps len:{}", self.combined_steps.len());
        for combined_step_info in self.combined_steps.iter() {
            callback.on_tick(&combined_step_info.step);
        }

        callback.on_post_ticks();
        self.authoritative_has_changed = false;
    }

    pub fn received_authoritative(&mut self, tick: TickId) {
        self.combined_steps.pop_up_to(tick + 1);
    }

    pub fn authoritative_has_changed(&mut self) {
        self.authoritative_has_changed = true;
    }

    pub fn push(&mut self, combined_step: CombinedStepT) {
        self.combined_steps.push(combined_step);
    }
}