98 } |
98 } |
99 pub fn send_all(self) -> PendingMessage { |
99 pub fn send_all(self) -> PendingMessage { |
100 PendingMessage::send_all(self) |
100 PendingMessage::send_all(self) |
101 } |
101 } |
102 } |
102 } |
103 |
|
104 pub enum Action { |
|
105 ChangeMaster(RoomId, Option<ClientId>), |
|
106 } |
|
107 |
|
108 use self::Action::*; |
|
109 |
|
110 pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) { |
|
111 match action { |
|
112 ChangeMaster(room_id, new_id) => { |
|
113 let room_client_ids = server.room_clients(room_id); |
|
114 let new_id = if server |
|
115 .room(client_id) |
|
116 .map(|r| r.is_fixed()) |
|
117 .unwrap_or(false) |
|
118 { |
|
119 new_id |
|
120 } else { |
|
121 new_id.or_else(|| room_client_ids.iter().find(|id| **id != client_id).cloned()) |
|
122 }; |
|
123 let new_nick = new_id.map(|id| server.clients[id].nick.clone()); |
|
124 |
|
125 if let (c, Some(r)) = server.client_and_room(client_id) { |
|
126 match r.master_id { |
|
127 Some(id) if id == c.id => { |
|
128 c.set_is_master(false); |
|
129 r.master_id = None; |
|
130 /*actions.push( |
|
131 ClientFlags("-h".to_string(), vec![c.nick.clone()]) |
|
132 .send_all() |
|
133 .in_room(r.id) |
|
134 .action(), |
|
135 );*/ |
|
136 } |
|
137 Some(_) => unreachable!(), |
|
138 None => {} |
|
139 } |
|
140 r.master_id = new_id; |
|
141 if !r.is_fixed() && c.protocol_number < 42 { |
|
142 r.name |
|
143 .replace_range(.., new_nick.as_ref().map_or("[]", String::as_str)); |
|
144 } |
|
145 r.set_join_restriction(false); |
|
146 r.set_team_add_restriction(false); |
|
147 let is_fixed = r.is_fixed(); |
|
148 r.set_unregistered_players_restriction(is_fixed); |
|
149 if let Some(nick) = new_nick { |
|
150 /*actions.push( |
|
151 ClientFlags("+h".to_string(), vec![nick]) |
|
152 .send_all() |
|
153 .in_room(r.id) |
|
154 .action(), |
|
155 );*/ |
|
156 } |
|
157 } |
|
158 if let Some(id) = new_id { |
|
159 server.clients[id].set_is_master(true) |
|
160 } |
|
161 } |
|
162 } |
|
163 } |
|