rust/hedgewars-server/src/server/actions.rs
author alfadur <mail@none>
Wed, 06 Feb 2019 21:33:22 +0300
changeset 14710 aae29ba56aec
parent 14709 4569d8d50286
child 14711 f61ce544d436
permissions -rw-r--r--
Server action refactoring part C of N
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     1
use super::{
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
     2
    client::HWClient,
14395
e5db279308d7 dispose of server mods
alfadur
parents: 14371
diff changeset
     3
    core::HWServer,
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
     4
    coretypes::{ClientId, GameCfg, RoomId, VoteType},
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
     5
    handlers,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
     6
    room::HWRoom,
13494
8c5dd562c9f7 Add room flags
alfadur
parents: 13486
diff changeset
     7
    room::{GameInfo, RoomFlags},
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     8
};
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13521
diff changeset
     9
use crate::{
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    10
    protocol::messages::{server_chat, HWProtocolMessage, HWServerMessage, HWServerMessage::*},
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    11
    utils::to_engine_msg,
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    12
};
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    13
use rand::{distributions::Uniform, thread_rng, Rng};
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    14
use std::{io, io::Write, iter::once, mem::replace};
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    15
14477
a077aac9df01 Start database interaction implementation
unc0rr
parents: 14436
diff changeset
    16
#[cfg(feature = "official-server")]
a077aac9df01 Start database interaction implementation
unc0rr
parents: 14436
diff changeset
    17
use super::database;
a077aac9df01 Start database interaction implementation
unc0rr
parents: 14436
diff changeset
    18
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    19
pub enum Destination {
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    20
    ToId(ClientId),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    21
    ToSelf,
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    22
    ToAll {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    23
        room_id: Option<RoomId>,
13486
1ee192f13456 Better packing for clients
alfadur
parents: 13460
diff changeset
    24
        protocol: Option<u16>,
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    25
        skip_self: bool,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    26
    },
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    27
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    28
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    29
pub struct PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    30
    pub destination: Destination,
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    31
    pub message: HWServerMessage,
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    32
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    33
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    34
impl PendingMessage {
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    35
    pub fn send(message: HWServerMessage, client_id: ClientId) -> PendingMessage {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    36
        PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    37
            destination: Destination::ToId(client_id),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    38
            message,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    39
        }
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    40
    }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    41
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    42
    pub fn send_self(message: HWServerMessage) -> PendingMessage {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    43
        PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    44
            destination: Destination::ToSelf,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    45
            message,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    46
        }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    47
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    48
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    49
    pub fn send_all(message: HWServerMessage) -> PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    50
        let destination = Destination::ToAll {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    51
            room_id: None,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    52
            protocol: None,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    53
            skip_self: false,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    54
        };
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    55
        PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    56
            destination,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    57
            message,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    58
        }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    59
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    60
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    61
    pub fn in_room(mut self, clients_room_id: RoomId) -> PendingMessage {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    62
        if let Destination::ToAll {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    63
            ref mut room_id, ..
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    64
        } = self.destination
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    65
        {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    66
            *room_id = Some(clients_room_id)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    67
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    68
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    69
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    70
13486
1ee192f13456 Better packing for clients
alfadur
parents: 13460
diff changeset
    71
    pub fn with_protocol(mut self, protocol_number: u16) -> PendingMessage {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    72
        if let Destination::ToAll {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    73
            ref mut protocol, ..
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    74
        } = self.destination
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    75
        {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    76
            *protocol = Some(protocol_number)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    77
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    78
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    79
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    80
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    81
    pub fn but_self(mut self) -> PendingMessage {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    82
        if let Destination::ToAll {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    83
            ref mut skip_self, ..
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    84
        } = self.destination
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    85
        {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    86
            *skip_self = true
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    87
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    88
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    89
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    90
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    91
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    92
impl HWServerMessage {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    93
    pub fn send(self, client_id: ClientId) -> PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    94
        PendingMessage::send(self, client_id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    95
    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    96
    pub fn send_self(self) -> PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    97
        PendingMessage::send_self(self)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    98
    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
    99
    pub fn send_all(self) -> PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
   100
        PendingMessage::send_all(self)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
   101
    }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   102
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   103
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
   104
pub enum Action {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   105
    ChangeMaster(RoomId, Option<ClientId>),
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
   106
}
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   107
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   108
use self::Action::*;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   109
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   110
pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) {
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   111
    match action {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   112
        ChangeMaster(room_id, new_id) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   113
            let room_client_ids = server.room_clients(room_id);
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
   114
            let new_id = if server
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
   115
                .room(client_id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
   116
                .map(|r| r.is_fixed())
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
   117
                .unwrap_or(false)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
   118
            {
13447
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   119
                new_id
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   120
            } else {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
   121
                new_id.or_else(|| room_client_ids.iter().find(|id| **id != client_id).cloned())
13447
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   122
            };
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   123
            let new_nick = new_id.map(|id| server.clients[id].nick.clone());
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   124
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   125
            if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   126
                match r.master_id {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   127
                    Some(id) if id == c.id => {
13486
1ee192f13456 Better packing for clients
alfadur
parents: 13460
diff changeset
   128
                        c.set_is_master(false);
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   129
                        r.master_id = None;
14704
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
   130
                        /*actions.push(
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
   131
                            ClientFlags("-h".to_string(), vec![c.nick.clone()])
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
   132
                                .send_all()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
   133
                                .in_room(r.id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
   134
                                .action(),
14704
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
   135
                        );*/
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   136
                    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   137
                    Some(_) => unreachable!(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   138
                    None => {}
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   139
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   140
                r.master_id = new_id;
13775
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   141
                if !r.is_fixed() && c.protocol_number < 42 {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
   142
                    r.name
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
   143
                        .replace_range(.., new_nick.as_ref().map_or("[]", String::as_str));
13775
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   144
                }
13494
8c5dd562c9f7 Add room flags
alfadur
parents: 13486
diff changeset
   145
                r.set_join_restriction(false);
8c5dd562c9f7 Add room flags
alfadur
parents: 13486
diff changeset
   146
                r.set_team_add_restriction(false);
8c5dd562c9f7 Add room flags
alfadur
parents: 13486
diff changeset
   147
                let is_fixed = r.is_fixed();
8c5dd562c9f7 Add room flags
alfadur
parents: 13486
diff changeset
   148
                r.set_unregistered_players_restriction(is_fixed);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   149
                if let Some(nick) = new_nick {
14704
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
   150
                    /*actions.push(
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
   151
                        ClientFlags("+h".to_string(), vec![nick])
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
   152
                            .send_all()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
   153
                            .in_room(r.id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
   154
                            .action(),
14704
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
   155
                    );*/
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   156
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   157
            }
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13521
diff changeset
   158
            if let Some(id) = new_id {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13521
diff changeset
   159
                server.clients[id].set_is_master(true)
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13521
diff changeset
   160
            }
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14477
diff changeset
   161
        }
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   162
    }
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   163
}