author | alfadur <mail@none> |
Tue, 05 Feb 2019 17:46:43 +0300 | |
changeset 14683 | 932ff7683653 |
parent 14675 | dfe652c53470 |
child 14686 | 9f98086de1b6 |
permissions | -rw-r--r-- |
14674
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
1 |
use crate::server::client::HWClient; |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
2 |
use crate::server::coretypes::ClientId; |
14674
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
3 |
use crate::server::room::HWRoom; |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
4 |
use crate::utils::to_engine_msg; |
13666 | 5 |
use crate::{ |
6 |
protocol::messages::{ |
|
14457 | 7 |
HWProtocolMessage::{self, Rnd}, |
14674
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
8 |
HWServerMessage::{ |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
9 |
self, Bye, ChatMsg, ClientFlags, ForwardEngineMessage, LobbyJoined, LobbyLeft, Notice, |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
10 |
RoomLeft, RoomRemove, RoomUpdated, Rooms, ServerMessage, TeamRemove, |
14674
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
11 |
}, |
14457 | 12 |
}, |
13 |
server::{actions::Action, core::HWServer}, |
|
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
14 |
}; |
14457 | 15 |
use rand::{self, thread_rng, Rng}; |
14674
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
16 |
use std::iter::once; |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
17 |
|
13524 | 18 |
pub fn rnd_reply(options: &[String]) -> HWServerMessage { |
13521 | 19 |
let mut rng = thread_rng(); |
20 |
let reply = if options.is_empty() { |
|
21 |
(*rng.choose(&["heads", "tails"]).unwrap()).to_owned() |
|
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
22 |
} else { |
13521 | 23 |
rng.choose(&options).unwrap().clone() |
24 |
}; |
|
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
25 |
|
13521 | 26 |
ChatMsg { |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
27 |
nick: "[random]".to_owned(), |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
28 |
msg: reply.clone(), |
13521 | 29 |
} |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
30 |
} |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
31 |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
32 |
pub fn process_login(server: &mut HWServer, response: &mut super::Response) { |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
33 |
let client_id = response.client_id(); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
34 |
let nick = server.clients[client_id].nick.clone(); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
35 |
|
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
36 |
let has_nick_clash = server |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
37 |
.clients |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
38 |
.iter() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
39 |
.any(|(id, c)| id != client_id && c.nick == nick); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
40 |
|
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
41 |
let client = &mut server.clients[client_id]; |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
42 |
|
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
43 |
if !client.is_checker() && has_nick_clash { |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
44 |
if client.protocol_number < 38 { |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
45 |
remove_client(server, response, "Nickname is already in use".to_string()); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
46 |
} else { |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
47 |
client.nick.clear(); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
48 |
response.add(Notice("NickAlreadyInUse".to_string()).send_self()); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
49 |
} |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
50 |
} else { |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
51 |
server.clients[client_id].room_id = Some(server.lobby_id); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
52 |
|
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
53 |
let lobby_nicks: Vec<_> = server |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
54 |
.clients |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
55 |
.iter() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
56 |
.filter_map(|(_, c)| c.room_id.and(Some(c.nick.clone()))) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
57 |
.collect(); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
58 |
let joined_msg = LobbyJoined(lobby_nicks); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
59 |
|
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
60 |
let everyone_msg = LobbyJoined(vec![server.clients[client_id].nick.clone()]); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
61 |
let flags_msg = ClientFlags( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
62 |
"+i".to_string(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
63 |
server |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
64 |
.clients |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
65 |
.iter() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
66 |
.filter(|(_, c)| c.room_id.is_some()) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
67 |
.map(|(_, c)| c.nick.clone()) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
68 |
.collect(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
69 |
); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
70 |
let server_msg = ServerMessage("\u{1f994} is watching".to_string()); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
71 |
|
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
72 |
let rooms_msg = Rooms( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
73 |
server |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
74 |
.rooms |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
75 |
.iter() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
76 |
.filter(|(id, _)| *id != server.lobby_id) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
77 |
.flat_map(|(_, r)| r.info(r.master_id.map(|id| &server.clients[id]))) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
78 |
.collect(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
79 |
); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
80 |
|
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
81 |
response.add(everyone_msg.send_all().but_self()); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
82 |
response.add(joined_msg.send_self()); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
83 |
response.add(flags_msg.send_self()); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
84 |
response.add(server_msg.send_self()); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
85 |
response.add(rooms_msg.send_self()); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
86 |
} |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
87 |
} |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
88 |
|
14675
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
89 |
pub fn remove_teams( |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
90 |
room: &mut HWRoom, |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
91 |
team_names: Vec<String>, |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
92 |
is_in_game: bool, |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
93 |
response: &mut super::Response, |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
94 |
) { |
14674
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
95 |
if let Some(ref mut info) = room.game_info { |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
96 |
for team_name in &team_names { |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
97 |
info.left_teams.push(team_name.clone()); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
98 |
|
14675
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
99 |
if is_in_game { |
14674
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
100 |
let msg = once(b'F').chain(team_name.bytes()); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
101 |
response.add( |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
102 |
ForwardEngineMessage(vec![to_engine_msg(msg)]) |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
103 |
.send_all() |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
104 |
.in_room(room.id) |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
105 |
.but_self(), |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
106 |
); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
107 |
|
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
108 |
info.teams_in_game -= 1; |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
109 |
if info.teams_in_game == 0 { |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
110 |
//FinishRoomGame(room.id) |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
111 |
} |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
112 |
|
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
113 |
let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes())); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
114 |
if let Some(m) = &info.sync_msg { |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
115 |
info.msg_log.push(m.clone()); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
116 |
info.sync_msg = None |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
117 |
} |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
118 |
info.msg_log.push(remove_msg.clone()); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
119 |
|
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
120 |
response.add( |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
121 |
ForwardEngineMessage(vec![remove_msg]) |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
122 |
.send_all() |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
123 |
.in_room(room.id) |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
124 |
.but_self(), |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
125 |
); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
126 |
} |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
127 |
} |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
128 |
} |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
129 |
|
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
130 |
for team_name in team_names { |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
131 |
room.remove_team(&team_name); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
132 |
response.add(TeamRemove(team_name).send_all().in_room(room.id)); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
133 |
} |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
134 |
} |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
135 |
|
14675
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
136 |
pub fn exit_room(client: &HWClient, room: &mut HWRoom, response: &mut super::Response, msg: &str) { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
137 |
if room.players_number > 1 || room.is_fixed() { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
138 |
room.players_number -= 1; |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
139 |
if client.is_ready() && room.ready_players_number > 0 { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
140 |
room.ready_players_number -= 1; |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
141 |
} |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
142 |
|
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
143 |
let team_names: Vec<_> = room |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
144 |
.client_teams(client.id) |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
145 |
.map(|t| t.name.clone()) |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
146 |
.collect(); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
147 |
remove_teams(room, team_names, client.is_in_game(), response); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
148 |
|
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
149 |
if room.players_number > 0 { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
150 |
response.add( |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
151 |
RoomLeft(client.nick.clone(), msg.to_string()) |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
152 |
.send_all() |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
153 |
.in_room(room.id) |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
154 |
.but_self(), |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
155 |
); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
156 |
} |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
157 |
|
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
158 |
//ChangeMaster(room.id, None)); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
159 |
} |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
160 |
|
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
161 |
let update_msg = if room.players_number == 0 && !room.is_fixed() { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
162 |
RoomRemove(room.name.clone()) |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
163 |
} else { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
164 |
RoomUpdated(room.name.clone(), room.info(Some(&client))) |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
165 |
}; |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
166 |
response.add(update_msg.send_all().with_protocol(room.protocol_number)); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
167 |
|
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
168 |
response.add(ClientFlags("-i".to_string(), vec![client.nick.clone()]).send_all()); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
169 |
} |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
170 |
|
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
171 |
pub fn remove_client(server: &mut HWServer, response: &mut super::Response, msg: String) { |
14674
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
172 |
let client_id = response.client_id(); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
173 |
let lobby_id = server.lobby_id; |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
174 |
let client = &mut server.clients[client_id]; |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
175 |
let (nick, room_id) = (client.nick.clone(), client.room_id); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
176 |
|
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
177 |
if let Some(room_id) = room_id { |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
178 |
let room = &mut server.rooms[room_id]; |
14675
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
179 |
exit_room(client, room, response, &msg); |
14674
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
180 |
client.room_id = Some(lobby_id); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
181 |
} |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
182 |
|
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
183 |
server.remove_client(client_id); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
184 |
|
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
185 |
response.add(LobbyLeft(nick, msg.to_string()).send_all()); |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
186 |
response.add(Bye("User quit: ".to_string() + &msg).send_self()); |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
187 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
188 |
|
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
189 |
#[cfg(test)] |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
190 |
mod tests { |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
191 |
use super::*; |
13796 | 192 |
use crate::protocol::messages::HWServerMessage::ChatMsg; |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
193 |
use crate::server::actions::PendingMessage; |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
194 |
|
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
195 |
fn reply2string(r: HWServerMessage) -> String { |
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
196 |
match r { |
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
197 |
ChatMsg { msg: p, .. } => String::from(p), |
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
198 |
_ => panic!("expected a ChatMsg"), |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
199 |
} |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
200 |
} |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
201 |
|
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
202 |
fn run_handle_test(opts: Vec<String>) { |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
203 |
let opts2 = opts.clone(); |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
204 |
for opt in opts { |
13521 | 205 |
while reply2string(rnd_reply(&opts2)) != opt {} |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
206 |
} |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
207 |
} |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
208 |
|
13446
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
209 |
/// This test terminates almost surely. |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
210 |
#[test] |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
211 |
fn test_handle_rnd_empty() { |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
212 |
run_handle_test(vec![]) |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
213 |
} |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
214 |
|
13446
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
215 |
/// This test terminates almost surely. |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
216 |
#[test] |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
217 |
fn test_handle_rnd_nonempty() { |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
218 |
run_handle_test(vec!["A".to_owned(), "B".to_owned(), "C".to_owned()]) |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
219 |
} |
13446
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
220 |
|
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
221 |
/// This test terminates almost surely (strong law of large numbers) |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
222 |
#[test] |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
223 |
fn test_distribution() { |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
224 |
let eps = 0.000001; |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
225 |
let lim = 0.5; |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
226 |
let opts = vec![0.to_string(), 1.to_string()]; |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
227 |
let mut ones = 0; |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
228 |
let mut tries = 0; |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
229 |
|
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
230 |
while tries < 1000 || ((ones as f64 / tries as f64) - lim).abs() >= eps { |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
231 |
tries += 1; |
13521 | 232 |
if reply2string(rnd_reply(&opts)) == 1.to_string() { |
13446
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
233 |
ones += 1; |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
234 |
} |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
235 |
} |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
236 |
} |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
237 |
} |