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
use std::error::Error;

use alkahest::{Schema, Unpacked};
use scoped_arena::Scope;

use crate::channel::Channel;

use super::*;

#[derive(Debug, thiserror::Error)]
pub enum ClientError<E: Error + 'static> {
    #[error("Client channel error: {source}")]
    ChannelError {
        #[from]
        source: E,
    },

    #[error("Unexpected server message")]
    UnexpectedMessage,
}
pub struct Updates<'a, U: Schema> {
    pub server_step: u64,
    pub updates: Unpacked<'a, U>,
}

pub struct ClientSession<C> {
    channel: C,
    current_step: u64,
    next_update_step: u64,
}

impl<C> ClientSession<C>
where
    C: Channel,
{
    /// Create new client session via specified channel.
    pub async fn new(channel: C, scope: &Scope<'_>) -> Result<Self, ClientError<C::Error>> {
        let mut channel = channel;
        channel
            .send_reliable::<ClientMessage, _>(ClientMessageConnectPack { token: "evoke" }, scope)
            .await?;

        loop {
            match channel.recv::<ServerMessage>(&scope) {
                Ok(Some(ServerMessageUnpacked::Connected { step })) => {
                    return Ok(ClientSession {
                        channel,
                        current_step: step,
                        next_update_step: 0,
                    });
                }
                Ok(Some(ServerMessageUnpacked::Updates { .. })) => {}
                Ok(Some(ServerMessageUnpacked::PlayerJoined { .. })) => {
                    return Err(ClientError::UnexpectedMessage)
                }
                Ok(None) => {
                    channel.recv_ready().await?;
                }
                Err(err) => return Err(err.into()),
            }
        }
    }

    pub fn current_step(&self) -> u64 {
        self.current_step
    }

    /// Adds new player to the session.
    pub async fn add_player<'a, P, J, K>(
        &mut self,
        player: K,
        scope: &'a Scope<'_>,
    ) -> Result<Unpacked<'a, J>, ClientError<C::Error>>
    where
        P: Schema,
        J: Schema,
        K: Pack<P>,
    {
        self.channel
            .send_reliable::<ClientMessage<P>, _>(ClientMessageAddPlayerPack { player }, scope)
            .await?;

        loop {
            match self.channel.recv::<ServerMessage<J>>(scope) {
                Ok(Some(ServerMessageUnpacked::PlayerJoined { info })) => return Ok(info),
                Ok(Some(ServerMessageUnpacked::Connected { .. })) => {
                    return Err(ClientError::UnexpectedMessage)
                }
                Ok(Some(ServerMessageUnpacked::Updates { .. })) => {}
                Ok(None) => {
                    self.channel.recv_ready().await?;
                }
                Err(err) => return Err(err.into()),
            }
        }
    }

    /// Sends input from players to the server.
    pub async fn send_inputs<T, K, I>(
        &mut self,
        inputs: I,
        scope: &Scope<'_>,
    ) -> Result<(), C::Error>
    where
        T: Schema,
        K: Pack<T>,
        I: IntoIterator,
        I::IntoIter: ExactSizeIterator<Item = (PlayerId, K)>,
    {
        self.channel
            .send_reliable::<ClientMessage<(), T>, _>(
                ClientMessageInputsPack {
                    step: self.current_step,
                    next_update_step: self.next_update_step,
                    inputs: inputs.into_iter(),
                },
                scope,
            )
            .await?;

        Ok(())
    }

    /// Advances client-side simulation by one step.
    pub fn advance<'a, U>(
        &mut self,
        scope: &'a Scope<'_>,
    ) -> Result<Option<Updates<'a, U>>, ClientError<C::Error>>
    where
        U: Schema,
    {
        let updates = match self.channel.recv::<ServerMessage<(), U>>(scope) {
            Ok(Some(ServerMessageUnpacked::Updates {
                server_step,
                updates,
            })) => {
                self.next_update_step = server_step + 1;
                Some(Updates {
                    server_step,
                    updates,
                })
            }
            Ok(Some(_)) => return Err(ClientError::UnexpectedMessage),
            Ok(None) => None,
            Err(err) => return Err(err.into()),
        };

        self.current_step += 1;
        Ok(updates)
    }
}