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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * 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.
 */
use flood_rs::prelude::*;
use std::collections::VecDeque;
use std::io;
use tick_id::TickId;

pub mod pending_steps;

#[derive(Debug)]
pub struct GenericOctetStep {
    pub payload: Vec<u8>,
}

impl Serialize for GenericOctetStep {
    fn serialize(&self, stream: &mut impl WriteOctetStream) -> io::Result<()>
    where
        Self: Sized,
    {
        stream.write_u8(self.payload.len() as u8)?;
        stream.write(self.payload.as_slice())
    }
}

impl Deserialize for GenericOctetStep {
    fn deserialize(stream: &mut impl ReadOctetStream) -> io::Result<Self>
    where
        Self: Sized,
    {
        let len = stream.read_u8()? as usize;
        let mut payload = vec![0u8; len];
        stream.read(&mut payload)?;
        Ok(Self { payload })
    }
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct JoinedData {
    pub tick_id: TickId,
}

impl Serialize for JoinedData {
    fn serialize(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
        stream.write_u32(self.tick_id.0)
    }
}

impl Deserialize for JoinedData {
    fn deserialize(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
        Ok(Self {
            tick_id: TickId(stream.read_u32()?),
        })
    }
}

#[derive(Debug, Clone, Eq, PartialEq)] // Clone is needed since it can be in collections (like pending steps queue), Eq and PartialEq is to be able to use in tests, Debug for debug output.
pub enum Step<T> {
    Forced,
    WaitingForReconnect,
    Joined(JoinedData),
    Left,
    Custom(T),
}

impl<T> Step<T> {
    #[must_use]
    pub fn to_octet(&self) -> u8 {
        match self {
            Step::Forced => 0x01,
            Step::WaitingForReconnect => 0x02,
            Step::Joined(_) => 0x03,
            Step::Left => 0x04,
            Step::Custom(_) => 0x05,
        }
    }
}

impl<T: Serialize> Serialize for Step<T> {
    fn serialize(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
        stream.write_u8(self.to_octet())?;
        match self {
            Step::Joined(join) => join.serialize(stream),
            Step::Custom(custom) => custom.serialize(stream),
            _ => Ok(()),
        }
    }
}

impl<T: Deserialize> Deserialize for Step<T> {
    fn deserialize(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
        let step_type = stream.read_u8()?;
        let t = match step_type {
            0x01 => Step::Forced,
            0x02 => Step::WaitingForReconnect,
            0x03 => Step::Joined(JoinedData::deserialize(stream)?),
            0x04 => Step::Left,
            0x05 => Step::Custom(T::deserialize(stream)?),
            _ => Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "invalid input, unknown step type",
            ))?,
        };
        Ok(t)
    }
}

#[derive(Debug, PartialEq, Clone)]
pub struct StepInfo<T> {
    pub step: T,
    pub tick_id: TickId,
}

#[derive(Default, Debug)]
pub struct Steps<T> {
    steps: VecDeque<StepInfo<T>>,
    expected_read_id: TickId,
    expected_write_id: TickId,
}

impl<T> Steps<T> {
    pub fn iter(&self) -> impl Iterator<Item = &StepInfo<T>> {
        self.steps.iter()
    }
}

pub struct FromIndexIterator<'a, T> {
    deque: &'a VecDeque<StepInfo<T>>,
    #[allow(unused)]
    start_index: usize,
    current_index: usize,
}

impl<'a, T> FromIndexIterator<'a, T> {
    pub fn new(deque: &'a VecDeque<StepInfo<T>>, start_index: usize) -> Self {
        Self {
            deque,
            start_index,
            current_index: start_index,
        }
    }
}

impl<StepType: Clone> Iterator for FromIndexIterator<'_, StepType> {
    type Item = StepInfo<StepType>;

    fn next(&mut self) -> Option<Self::Item> {
        let item = self.deque.get(self.current_index)?;
        self.current_index += 1;
        Some(item.clone())
    }
}

pub const TICK_ID_MAX: u32 = u32::MAX;

impl<StepType> Steps<StepType> {
    pub fn new() -> Self {
        Self {
            steps: VecDeque::new(),
            expected_read_id: TickId::new(0),
            expected_write_id: TickId::new(0),
        }
    }

    pub fn clear(&mut self) {
        self.steps.clear();
        self.expected_read_id = TickId(0);
        self.expected_write_id = TickId(0);
    }

    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: StepType) {
        let info = StepInfo {
            step,
            tick_id: self.expected_write_id,
        };
        self.steps.push_back(info);
        self.expected_write_id += 1;
    }

    pub fn debug_get(&self, index: usize) -> Option<&StepInfo<StepType>> {
        self.steps.get(index)
    }

    pub fn pop(&mut self) -> Option<StepInfo<StepType>> {
        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()
    }

    pub fn iter_index(&self, start_index: usize) -> FromIndexIterator<StepType> {
        FromIndexIterator::new(&self.steps, start_index)
    }
}