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
use std::convert::TryInto;
use std::sync::mpsc;
use std::thread::JoinHandle;
use crate::backend_connection::TokenResponse;
use crate::error::ServerError;
use crate::games;
use crate::games::{Game, RaskGame};
use log::info;
use ws::Sender;
pub type GroupId = u32;
#[derive(Debug)]
pub struct Group {
pub clients: Vec<Sender>,
pub sender: mpsc::Sender<Message>,
id: GroupId,
group_type: String,
name: String,
capacity: u32,
game_thread: JoinHandle<()>,
}
pub struct SendGroup {
pub receiver: mpsc::Receiver<Message>,
pub id: GroupId,
pub group_type: String,
pub name: String,
pub capacity: u32,
}
#[derive(Debug)]
pub enum Message {
Data((String, Vec<u8>)),
Park,
Kill,
Add(games::User),
Remove(Sender),
}
impl Message {
pub fn is_data(&self) -> bool {
match self {
Message::Data(_) => true,
_ => false,
}
}
}
impl Drop for Group {
fn drop(&mut self) {
info!("dropping group {:?}", &self);
let _ = self.sender.send(Message::Kill);
}
}
impl Group {
pub fn id(&self) -> GroupId {
self.id
}
#[allow(dead_code)]
pub fn group_type(&self) -> &str {
&self.group_type
}
#[allow(dead_code)]
pub fn name(&self) -> &str {
&self.name
}
#[allow(dead_code)]
pub fn park(self) -> Result<(), ServerError> {
Ok(self.sender.send(Message::Park)?)
}
#[allow(dead_code)]
pub fn unpark(&mut self) {
self.game_thread.thread().unpark();
}
pub fn add_client(&mut self, client: Sender) -> Result<mpsc::Sender<Message>, ServerError> {
if self.clients.len() >= self.capacity as usize {
Err(ServerError::Group(format!(
"User limit for {} exceeded",
self.id
)))
} else {
self.clients.push(client.clone());
self.sender
.send(Message::Add(games::User::new("None".to_owned(), client)))
.map_err(Into::into)
.map(|()| self.sender.clone())
}
}
pub fn remove_client(&mut self, client: &Sender) -> Result<(), ServerError> {
if let Some(pos) = self.clients.iter().position(|x| *x == *client) {
self.clients.swap_remove(pos);
}
self.sender
.send(Message::Remove(client.clone()))
.map_err(Into::into)
}
pub fn new(response: TokenResponse) -> Result<Self, ServerError> {
let (sender, receiver) = mpsc::channel();
let (id, name, group_type) = (response.group_id, response.group_name, response.group_type);
let capacity = response.user_max.try_into().unwrap_or(std::usize::MAX) as u32;
info!("Creating Group{} ({}) with game {}", id, name, group_type);
let send_group = SendGroup {
receiver,
id,
name: name.clone(),
group_type: group_type.clone(),
capacity,
};
let game = match group_type.as_str() {
"rask" => RaskGame::new(send_group),
name => {
return Err(ServerError::GroupCreation(format!(
"The game type {} is not implemented",
name
)))
}
};
Ok(Self {
clients: Vec::new(),
sender,
id,
group_type,
name,
capacity,
game_thread: game.run()?,
})
}
}