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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*----------------------------------------------------------------------------------------------------------
 *  Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/nimble-rust/steps
 *  Licensed under the MIT License. See LICENSE in the project root for license information.
 *--------------------------------------------------------------------------------------------------------*/
use std::collections::VecDeque;

use tick_id::TickId;

pub mod pending_steps;

#[derive(Debug, PartialEq, Eq)]
pub enum Step<T> {
    Forced,
    WaitingForReconnect,
    Custom(T),
}

pub struct ParticipantStep<T> {
    pub participant_id: u8,
    pub step: Step<T>,
}

pub struct ParticipantSteps<T> {
    pub steps: Vec<ParticipantStep<T>>,
}

impl<T> Default for ParticipantSteps<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T> ParticipantStep<T> {
    pub fn new(participant_id: u8, step: Step<T>) -> Self {
        Self {
            participant_id,
            step,
        }
    }
}

impl<T> ParticipantSteps<T> {
    pub fn new() -> Self {
        Self { steps: Vec::new() }
    }

    pub fn push(&mut self, participant_id: u8, step: Step<T>) {
        self.steps.push(ParticipantStep::new(participant_id, step));
    }

    pub fn len(&self) -> usize {
        self.steps.len()
    }

    pub fn is_empty(&self) -> bool {
        self.steps.is_empty()
    }
}

pub struct StepInfo<T> {
    pub step: Step<T>,
    pub tick_id: TickId,
}

pub struct Steps<T> {
    steps: VecDeque<StepInfo<T>>,
    expected_read_id: TickId,
    expected_write_id: TickId,
}

impl<T> Default for Steps<T> {
    fn default() -> Self {
        Self::new()
    }
}

const TICK_ID_MAX: u32 = u32::MAX;

impl<T> Steps<T> {
    pub fn new() -> Self {
        Self {
            steps: VecDeque::new(),
            expected_read_id: TickId::new(TICK_ID_MAX),
            expected_write_id: TickId::new(TICK_ID_MAX),
        }
    }
    pub fn new_with_initial_tick(initial_tick_id: TickId) -> Self {
        Self {
            steps: VecDeque::new(),
            expected_read_id: initial_tick_id,
            expected_write_id: initial_tick_id,
        }
    }

    pub fn push(&mut self, step: Step<T>) {
        let info = StepInfo {
            step,
            tick_id: self.expected_write_id,
        };
        self.steps.push_back(info);
        self.expected_write_id += 1;
    }

    pub fn pop(&mut self) -> Option<StepInfo<T>> {
        let info = self.steps.pop_front();
        if let Some(ref step_info) = info {
            assert_eq!(step_info.tick_id, self.expected_read_id);
            self.expected_read_id += 1;
        }
        info
    }

    pub fn pop_up_to(&mut self, tick_id: TickId) {
        while let Some(info) = self.steps.front() {
            if info.tick_id >= tick_id {
                break;
            }

            self.steps.pop_front();
        }
    }

    pub fn pop_count(&mut self, count: usize) {
        if count >= self.steps.len() {
            self.steps.clear();
        } else {
            self.steps.drain(..count);
        }
    }

    pub fn front_tick_id(&self) -> Option<TickId> {
        self.steps.front().map(|step_info| step_info.tick_id)
    }

    pub fn back_tick_id(&self) -> Option<TickId> {
        self.steps.back().map(|step_info| step_info.tick_id)
    }

    pub fn len(&self) -> usize {
        self.steps.len()
    }

    pub fn is_empty(&self) -> bool {
        self.steps.is_empty()
    }
}

#[cfg(test)]
mod tests {
    use crate::Step::Custom;

    use super::*;

    #[derive(Debug, PartialEq, Eq)]
    enum GameInput {
        Jumping(bool),
        MoveHorizontal(i32),
    }

    #[test]
    fn add_step() {
        let mut steps = Steps::<GameInput>::new_with_initial_tick(TickId(23));
        steps.push(Custom(GameInput::MoveHorizontal(-2)));
        assert_eq!(steps.len(), 1);
        assert_eq!(steps.front_tick_id().unwrap().value(), 23)
    }

    #[test]
    fn push_and_pop_step() {
        let mut steps = Steps::<GameInput>::new_with_initial_tick(TickId(23));
        steps.push(Custom(GameInput::Jumping(true)));
        steps.push(Custom(GameInput::MoveHorizontal(42)));
        assert_eq!(steps.len(), 2);
        assert_eq!(steps.front_tick_id().unwrap().value(), 23);
        assert_eq!(steps.pop().unwrap().step, Custom(GameInput::Jumping(true)));
        assert_eq!(steps.front_tick_id().unwrap().value(), 24);
    }

    #[test]
    fn push_and_pop_count() {
        let mut steps = Steps::<GameInput>::new_with_initial_tick(TickId(23));
        steps.push(Custom(GameInput::Jumping(true)));
        steps.push(Custom(GameInput::MoveHorizontal(42)));
        assert_eq!(steps.len(), 2);
        steps.pop_count(8);
        assert_eq!(steps.len(), 0);
    }

    #[test]
    fn push_and_pop_up_to_lower() {
        let mut steps = Steps::<GameInput>::new_with_initial_tick(TickId(23));
        steps.push(Custom(GameInput::Jumping(true)));
        steps.push(Custom(GameInput::MoveHorizontal(42)));
        assert_eq!(steps.len(), 2);
        steps.pop_up_to(TickId(1));
        assert_eq!(steps.len(), 2);
    }

    #[test]
    fn push_and_pop_up_to_equal() {
        let mut steps = Steps::<GameInput>::new_with_initial_tick(TickId(23));
        steps.push(Custom(GameInput::Jumping(true)));
        steps.push(Custom(GameInput::MoveHorizontal(42)));
        assert_eq!(steps.len(), 2);
        steps.pop_up_to(TickId::new(24));
        assert_eq!(steps.len(), 1);
    }
}