nimble_sample_game/
lib.rs

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
/*
 * 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 app_version::{Version, VersionProvider};
use flood_rs::prelude::{InOctetStream, OutOctetStream};
use flood_rs::{BufferDeserializer, Deserialize, ReadOctetStream, Serialize, WriteOctetStream};
use log::info;
use nimble_assent::AssentCallback;
use nimble_rectify::RectifyCallback;
use nimble_seer::SeerCallback;
use std::io;

pub use nimble_sample_step::SampleStep;
use nimble_step::Step;
use nimble_step_map::StepMap;

#[derive(Default, Clone, Debug, Eq, PartialEq)]
pub struct SampleGameState {
    pub x: i32,
    pub y: i32,
}

impl SampleGameState {
    pub fn update(&mut self, step: &StepMap<Step<SampleStep>>) {
        for (participant_id, step) in step {
            match &step {
                Step::Custom(custom) => match custom {
                    SampleStep::MoveLeft(amount) => self.x -= *amount as i32,
                    SampleStep::MoveRight(amount) => self.x += *amount as i32,
                    SampleStep::Jump => self.y += 1,
                    SampleStep::Nothing => {}
                },
                Step::Forced => self.y += 1,
                Step::WaitingForReconnect => info!("waiting for reconnect"),
                Step::Joined(data) => info!(
                    "participant {} joined at time {}",
                    participant_id, data.tick_id
                ),
                Step::Left => info!("participant {} left", participant_id),
            }
        }
    }

    pub fn to_octets(&self) -> io::Result<Vec<u8>> {
        let mut out = OutOctetStream::new();
        out.write_i32(self.x)?;
        out.write_i32(self.y)?;
        Ok(out.octets())
    }

    #[allow(unused)]
    pub fn from_octets(payload: &[u8]) -> io::Result<(Self, usize)> {
        let mut in_stream = InOctetStream::new(payload);
        Ok((
            Self {
                x: in_stream.read_i32()?,
                y: in_stream.read_i32()?,
            },
            in_stream.cursor.position() as usize,
        ))
    }
}

impl BufferDeserializer for SampleGameState {
    fn deserialize(buf: &[u8]) -> io::Result<(Self, usize)> {
        let mut in_stream = InOctetStream::new(buf);
        let s = <SampleGameState as Deserialize>::deserialize(&mut in_stream)?;
        Ok((s, in_stream.cursor.position() as usize))
    }
}

impl Serialize for SampleGameState {
    fn serialize(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
        stream.write_i32(self.x)?;
        stream.write_i32(self.y)
    }
}

impl Deserialize for SampleGameState {
    fn deserialize(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
        Ok(Self {
            x: stream.read_i32()?,
            y: stream.read_i32()?,
        })
    }
}

#[derive(Default, Clone, Debug, Eq, PartialEq)]
pub struct SampleGame {
    pub predicted: SampleGameState,
    pub authoritative: SampleGameState,
}

impl SampleGame {
    pub fn authoritative_octets(&self) -> io::Result<Vec<u8>> {
        self.authoritative.to_octets()
    }
}

impl BufferDeserializer for SampleGame {
    fn deserialize(octets: &[u8]) -> io::Result<(Self, usize)> {
        let (sample_state, size) = SampleGameState::from_octets(octets)?;
        Ok((
            Self {
                predicted: sample_state.clone(),
                authoritative: sample_state,
            },
            size,
        ))
    }
}

impl VersionProvider for SampleGame {
    fn version() -> Version {
        Version::new(0, 0, 5)
    }
}

impl Serialize for SampleGame {
    fn serialize(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
        self.authoritative.serialize(stream)
    }
}

impl Deserialize for SampleGame {
    fn deserialize(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
        let state = <SampleGameState as Deserialize>::deserialize(stream)?;
        Ok(Self {
            authoritative: state.clone(),
            predicted: state,
        })
    }
}

impl SeerCallback<StepMap<Step<SampleStep>>> for SampleGame {
    fn on_pre_ticks(&mut self) {}
    fn on_tick(&mut self, step: &StepMap<Step<SampleStep>>) {
        self.predicted.update(step);
    }
    fn on_post_ticks(&mut self) {}
}

impl AssentCallback<StepMap<Step<SampleStep>>> for SampleGame {
    fn on_pre_ticks(&mut self) {}
    fn on_tick(&mut self, step: &StepMap<Step<SampleStep>>) {
        self.authoritative.update(step);
    }
    fn on_post_ticks(&mut self) {}
}

impl RectifyCallback for SampleGame {
    fn on_copy_from_authoritative(&mut self) {
        self.predicted = self.authoritative.clone();
    }
}