nimble_host_logic/
session.rs1use crate::combine::HostCombinator;
7use freelist_rs::FreeList;
8use nimble_participant::ParticipantId;
9use std::cell::RefCell;
10use std::collections::HashMap;
11use std::rc::Rc;
12use tick_id::TickId;
13
14#[derive(Copy, Clone, Debug)]
15pub struct Participant {
16 pub id: ParticipantId,
17 pub client_local_index: u8,
18}
19
20#[allow(clippy::module_name_repetitions)] pub struct GameSession<StepT: Clone + std::fmt::Display> {
22 pub participants: HashMap<ParticipantId, Rc<RefCell<Participant>>>,
23 pub participant_ids: FreeList<u8>,
24 pub(crate) combinator: HostCombinator<StepT>,
25}
26
27impl<StepT: Clone + std::fmt::Display> Default for GameSession<StepT> {
28 fn default() -> Self {
29 Self::new(TickId(0))
30 }
31}
32
33impl<StepT: Clone + std::fmt::Display> GameSession<StepT> {
34 #[must_use]
35 pub fn new(tick_id: TickId) -> Self {
36 Self {
37 participants: HashMap::new(),
38 participant_ids: FreeList::new(0xff),
39 combinator: HostCombinator::<StepT>::new(tick_id),
40 }
41 }
42
43 pub fn create_participants(
44 &mut self,
45 client_local_indices: &[u8],
46 ) -> Option<Vec<Rc<RefCell<Participant>>>> {
47 let mut participants: Vec<Rc<RefCell<Participant>>> = vec![];
48
49 let ids = self
50 .participant_ids
51 .allocate_count(client_local_indices.len())?;
52 for (index, id_value) in ids.iter().enumerate() {
53 let participant_id = ParticipantId(*id_value);
54 let participant = Rc::new(RefCell::new(Participant {
55 client_local_index: client_local_indices[index],
56 id: participant_id,
57 }));
58
59 participants.push(participant.clone());
60
61 self.participants
62 .insert(participant_id, participant.clone());
63 }
64
65 Some(participants)
66 }
67}