rust/hedgewars-server/src/handlers/common.rs
author alfadur
Tue, 14 Jan 2020 01:40:23 +0300
changeset 15544 77551d5e0a3b
parent 15543 02f648f7cbe1
permissions -rw-r--r--
send 🦔 on round end
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13524
diff changeset
     1
use crate::{
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
     2
    core::{
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
     3
        client::HwClient,
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
     4
        room::HwRoom,
15525
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
     5
        server::{
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
     6
            EndGameResult, HwRoomControl, HwServer, JoinRoomError, LeaveRoomResult, StartGameError,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
     7
            VoteError, VoteResult,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
     8
        },
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
     9
        types::{ClientId, GameCfg, RoomId, TeamInfo, Vote, VoteType, MAX_HEDGEHOGS_PER_TEAM},
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    10
    },
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13524
diff changeset
    11
    protocol::messages::{
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    12
        add_flags, remove_flags, server_chat,
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    13
        HwProtocolMessage::{self, Rnd},
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    14
        HwServerMessage::{self, *},
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
    15
        ProtocolFlags as Flags,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    16
    },
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
    17
    utils::to_engine_msg,
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    18
};
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
    19
15543
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
    20
use super::{
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
    21
    actions::{Destination, DestinationGroup},
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
    22
    Response,
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
    23
};
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
    24
15074
c5a6e8566425 shuffle server files
alfadur
parents: 14891
diff changeset
    25
use crate::core::types::RoomConfig;
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14696
diff changeset
    26
use rand::{self, seq::SliceRandom, thread_rng, Rng};
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
    27
use std::{iter::once, mem::replace};
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    28
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    29
pub fn rnd_reply(options: &[String]) -> HwServerMessage {
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    30
    let mut rng = thread_rng();
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14696
diff changeset
    31
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    32
    let reply = if options.is_empty() {
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14696
diff changeset
    33
        (*&["heads", "tails"].choose(&mut rng).unwrap()).to_string()
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
    34
    } else {
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14696
diff changeset
    35
        options.choose(&mut rng).unwrap().clone()
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    36
    };
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
    37
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    38
    ChatMsg {
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14696
diff changeset
    39
        nick: "[random]".to_string(),
f64e21f164a5 fix some warnings
alfadur
parents: 14696
diff changeset
    40
        msg: reply,
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    41
    }
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    42
}
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    43
15441
61a0bd0bb021 refactor root handler
alfadur
parents: 15439
diff changeset
    44
pub fn get_lobby_join_data(server: &HwServer, response: &mut Response) {
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
    45
    let client_id = response.client_id();
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
    46
15441
61a0bd0bb021 refactor root handler
alfadur
parents: 15439
diff changeset
    47
    let client = server.client(client_id);
14791
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    48
    let nick = vec![client.nick.clone()];
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    49
    let mut flags = vec![];
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    50
    if client.is_registered() {
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    51
        flags.push(Flags::Registered)
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    52
    }
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    53
    if client.is_admin() {
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    54
        flags.push(Flags::Admin)
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    55
    }
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    56
    if client.is_contributor() {
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    57
        flags.push(Flags::Contributor)
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    58
    }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
    59
14791
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    60
    let all_nicks: Vec<_> = server.collect_nicks(|_| true);
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
    61
14791
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    62
    let mut flag_selectors = [
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    63
        (
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    64
            Flags::Registered,
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    65
            server.collect_nicks(|(_, c)| c.is_registered()),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    66
        ),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    67
        (Flags::Admin, server.collect_nicks(|(_, c)| c.is_admin())),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    68
        (
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    69
            Flags::Contributor,
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    70
            server.collect_nicks(|(_, c)| c.is_contributor()),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    71
        ),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    72
        (
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    73
            Flags::InRoom,
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    74
            server.collect_nicks(|(_, c)| c.room_id.is_some()),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    75
        ),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    76
    ];
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    77
15441
61a0bd0bb021 refactor root handler
alfadur
parents: 15439
diff changeset
    78
    let server_msg = ServerMessage(server.get_greetings(client).to_string());
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
    79
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14697
diff changeset
    80
    let rooms_msg = Rooms(
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
    81
        server
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
    82
            .iter_rooms()
15522
4a0b06b03199 make rooms field private
alfadur <mail@none>
parents: 15519
diff changeset
    83
            .filter(|r| r.protocol_number == client.protocol_number)
4a0b06b03199 make rooms field private
alfadur <mail@none>
parents: 15519
diff changeset
    84
            .flat_map(|r| r.info(r.master_id.map(|id| server.client(id))))
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14697
diff changeset
    85
            .collect(),
01f8ab45f806 fix lobby joining
alfadur
parents: 14697
diff changeset
    86
    );
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
    87
14791
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    88
    response.add(LobbyJoined(nick).send_all().but_self());
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    89
    response.add(
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    90
        ClientFlags(add_flags(&flags), all_nicks.clone())
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    91
            .send_all()
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    92
            .but_self(),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    93
    );
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    94
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    95
    response.add(LobbyJoined(all_nicks).send_self());
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    96
    for (flag, nicks) in &mut flag_selectors {
14891
e72d8c2299dd fix broken messages on lobby join
alfadur
parents: 14801
diff changeset
    97
        if !nicks.is_empty() {
e72d8c2299dd fix broken messages on lobby join
alfadur
parents: 14801
diff changeset
    98
            response.add(ClientFlags(add_flags(&[*flag]), replace(nicks, vec![])).send_self());
e72d8c2299dd fix broken messages on lobby join
alfadur
parents: 14801
diff changeset
    99
        }
14791
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
   100
    }
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
   101
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14697
diff changeset
   102
    response.add(server_msg.send_self());
01f8ab45f806 fix lobby joining
alfadur
parents: 14697
diff changeset
   103
    response.add(rooms_msg.send_self());
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
   104
}
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
   105
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   106
pub fn get_room_join_data<'a, I: Iterator<Item = &'a HwClient> + Clone>(
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   107
    client: &HwClient,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   108
    room: &HwRoom,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   109
    room_clients: I,
14787
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   110
    response: &mut Response,
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   111
) {
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   112
    #[inline]
15534
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   113
    fn partition_nicks<'a, I, F>(clients: I, f: F) -> (Vec<String>, Vec<String>)
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   114
    where
15534
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   115
        I: Iterator<Item = &'a HwClient> + Clone,
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   116
        F: Fn(&&'a HwClient) -> bool,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   117
    {
15534
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   118
        (
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   119
            clients
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   120
                .clone()
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   121
                .filter(|c| f(c))
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   122
                .map(|c| &c.nick)
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   123
                .cloned()
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   124
                .collect(),
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   125
            clients
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   126
                .filter(|c| !f(c))
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   127
                .map(|c| &c.nick)
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   128
                .cloned()
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   129
                .collect(),
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   130
        )
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   131
    }
14787
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   132
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   133
    let nick = client.nick.clone();
15534
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   134
    response.add(
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   135
        RoomJoined(vec![nick.clone()])
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   136
            .send_all()
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   137
            .in_room(room.id)
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   138
            .but_self(),
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   139
    );
15535
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   140
    response.add(ClientFlags(add_flags(&[Flags::InRoom]), vec![nick.clone()]).send_all());
15534
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   141
    let nicks = room_clients.clone().map(|c| c.nick.clone()).collect();
14787
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   142
    response.add(RoomJoined(nicks).send_self());
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   143
14799
e41131607167 restore setup sending on room join
alfadur
parents: 14798
diff changeset
   144
    let mut flag_selectors = [
e41131607167 restore setup sending on room join
alfadur
parents: 14798
diff changeset
   145
        (
e41131607167 restore setup sending on room join
alfadur
parents: 14798
diff changeset
   146
            Flags::RoomMaster,
15534
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   147
            partition_nicks(room_clients.clone(), |c| c.is_master()),
14799
e41131607167 restore setup sending on room join
alfadur
parents: 14798
diff changeset
   148
        ),
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   149
        (
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   150
            Flags::Ready,
15534
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   151
            partition_nicks(room_clients.clone(), |c| c.is_ready()),
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   152
        ),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   153
        (
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   154
            Flags::InGame,
15534
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   155
            partition_nicks(room_clients.clone(), |c| c.is_in_game()),
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   156
        ),
14799
e41131607167 restore setup sending on room join
alfadur
parents: 14798
diff changeset
   157
    ];
e41131607167 restore setup sending on room join
alfadur
parents: 14798
diff changeset
   158
15534
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   159
    for (flag, (set_nicks, cleared_nicks)) in &mut flag_selectors {
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   160
        if !set_nicks.is_empty() {
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   161
            response.add(ClientFlags(add_flags(&[*flag]), replace(set_nicks, vec![])).send_self());
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   162
        }
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   163
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   164
        if !cleared_nicks.is_empty() {
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   165
            response.add(
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   166
                ClientFlags(remove_flags(&[*flag]), replace(cleared_nicks, vec![])).send_self(),
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   167
            );
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   168
        }
14799
e41131607167 restore setup sending on room join
alfadur
parents: 14798
diff changeset
   169
    }
14787
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   170
15543
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   171
    get_room_teams(room, Destination::ToSelf, response);
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   172
    get_room_config(room, Destination::ToSelf, response);
15535
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   173
14787
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   174
    if !room.greeting.is_empty() {
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   175
        response.add(
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   176
            ChatMsg {
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   177
                nick: "[greeting]".to_string(),
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   178
                msg: room.greeting.clone(),
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   179
            }
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   180
            .send_self(),
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   181
        );
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   182
    }
15535
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   183
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   184
    if let Some(info) = &room.game_info {
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   185
        response.add(
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   186
            ClientFlags(add_flags(&[Flags::Ready, Flags::InGame]), vec![nick])
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   187
                .send_all()
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   188
                .in_room(room.id),
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   189
        );
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   190
        response.add(RunGame.send_self());
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   191
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   192
        response.add(
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   193
            ForwardEngineMessage(
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   194
                once(to_engine_msg("e$spectate 1".bytes()))
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   195
                    .chain(info.msg_log.iter().cloned())
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   196
                    .collect(),
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   197
            )
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   198
            .send_self(),
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   199
        );
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   200
15540
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15536
diff changeset
   201
        for team in info.client_teams(client.id) {
15535
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   202
            response.add(
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   203
                ForwardEngineMessage(vec![to_engine_msg(once(b'G').chain(team.name.bytes()))])
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   204
                    .send_all()
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   205
                    .in_room(room.id),
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   206
            );
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   207
        }
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   208
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   209
        if info.is_paused {
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   210
            response.add(ForwardEngineMessage(vec![to_engine_msg(once(b'I'))]).send_self());
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   211
        }
3f6a7a867040 add back em response on joining a game in progress
alfadur <mail@none>
parents: 15534
diff changeset
   212
    }
14787
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   213
}
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   214
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   215
pub fn get_room_join_error(error: JoinRoomError, response: &mut Response) {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   216
    use super::strings::*;
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   217
    match error {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   218
        JoinRoomError::DoesntExist => response.warn(NO_ROOM),
15534
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   219
        JoinRoomError::WrongProtocol => response.warn(INCOMPATIBLE_ROOM_PROTOCOL),
15533
0606f89698e7 check the room passwords
alfadur <mail@none>
parents: 15531
diff changeset
   220
        JoinRoomError::WrongPassword => {
0606f89698e7 check the room passwords
alfadur <mail@none>
parents: 15531
diff changeset
   221
            response.add(Notice("WrongPassword".to_string()).send_self())
0606f89698e7 check the room passwords
alfadur <mail@none>
parents: 15531
diff changeset
   222
        }
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   223
        JoinRoomError::Full => response.warn(ROOM_FULL),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   224
        JoinRoomError::Restricted => response.warn(ROOM_JOIN_RESTRICTED),
15534
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15533
diff changeset
   225
        JoinRoomError::RegistrationRequired => response.warn(ROOM_REGISTRATION_REQUIRED),
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   226
    }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   227
}
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   228
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   229
pub fn get_remove_teams_data(
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   230
    room_id: RoomId,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   231
    was_in_game: bool,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   232
    removed_teams: Vec<String>,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   233
    response: &mut Response,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   234
) {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   235
    if was_in_game {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   236
        for team_name in &removed_teams {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   237
            let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes()));
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   238
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   239
            response.add(
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   240
                ForwardEngineMessage(vec![remove_msg])
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   241
                    .send_all()
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   242
                    .in_room(room_id)
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   243
                    .but_self(),
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   244
            );
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   245
        }
15541
d122b65bdf6f reapply some simplifications
alfadur <mail@none>
parents: 15540
diff changeset
   246
    } else {
d122b65bdf6f reapply some simplifications
alfadur <mail@none>
parents: 15540
diff changeset
   247
        for team_name in removed_teams {
d122b65bdf6f reapply some simplifications
alfadur <mail@none>
parents: 15540
diff changeset
   248
            response.add(TeamRemove(team_name).send_all().in_room(room_id));
d122b65bdf6f reapply some simplifications
alfadur <mail@none>
parents: 15540
diff changeset
   249
        }
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   250
    }
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   251
}
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   252
15492
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   253
pub fn get_room_leave_result(
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   254
    server: &HwServer,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   255
    room: &HwRoom,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   256
    leave_message: &str,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   257
    result: LeaveRoomResult,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   258
    response: &mut Response,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   259
) {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   260
    let client = server.client(response.client_id);
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   261
    response.add(ClientFlags(remove_flags(&[Flags::InRoom]), vec![client.nick.clone()]).send_all());
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   262
15516
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   263
    match result {
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   264
        LeaveRoomResult::RoomRemoved => {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   265
            response.add(
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   266
                RoomRemove(room.name.clone())
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   267
                    .send_all()
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   268
                    .with_protocol(room.protocol_number),
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   269
            );
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   270
        }
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   271
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   272
        LeaveRoomResult::RoomRemains {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   273
            is_empty,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   274
            was_master,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   275
            new_master,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   276
            was_in_game,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   277
            removed_teams,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   278
        } => {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   279
            if !is_empty {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   280
                response.add(
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   281
                    RoomLeft(client.nick.clone(), leave_message.to_string())
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   282
                        .send_all()
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   283
                        .in_room(room.id)
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   284
                        .but_self(),
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   285
                );
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   286
            }
14798
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   287
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   288
            if was_master {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   289
                response.add(
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   290
                    ClientFlags(
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   291
                        remove_flags(&[Flags::RoomMaster]),
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   292
                        vec![client.nick.clone()],
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   293
                    )
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   294
                    .send_all()
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   295
                    .in_room(room.id),
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   296
                );
14798
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   297
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   298
                if let Some(new_master_id) = new_master {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   299
                    let new_master_nick = server.client(new_master_id).nick.clone();
14798
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   300
                    response.add(
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   301
                        ClientFlags(add_flags(&[Flags::RoomMaster]), vec![new_master_nick])
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   302
                            .send_all()
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   303
                            .in_room(room.id),
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   304
                    );
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14691
diff changeset
   305
                }
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   306
            }
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   307
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   308
            get_remove_teams_data(room.id, was_in_game, removed_teams, response);
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   309
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   310
            response.add(
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   311
                RoomUpdated(room.name.clone(), room.info(Some(&client)))
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   312
                    .send_all()
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   313
                    .with_protocol(room.protocol_number),
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   314
            );
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   315
        }
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   316
    }
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   317
}
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   318
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   319
pub fn remove_client(server: &mut HwServer, response: &mut Response, msg: String) {
14674
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   320
    let client_id = response.client_id();
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   321
    let client = server.client(client_id);
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14696
diff changeset
   322
    let nick = client.nick.clone();
14674
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   323
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   324
    if let Some(mut room_control) = server.get_room_control(client_id) {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   325
        let room_id = room_control.room().id;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   326
        let result = room_control.leave_room();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   327
        get_room_leave_result(server, server.room(room_id), &msg, result, response);
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   328
    }
14674
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   329
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   330
    server.remove_client(client_id);
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   331
15441
61a0bd0bb021 refactor root handler
alfadur
parents: 15439
diff changeset
   332
    response.add(LobbyLeft(nick, msg.clone()).send_all());
61a0bd0bb021 refactor root handler
alfadur
parents: 15439
diff changeset
   333
    response.add(Bye(msg).send_self());
14696
8a45c90f4580 fix client removal
alfadur
parents: 14694
diff changeset
   334
    response.remove_client(client_id);
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14457
diff changeset
   335
}
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14457
diff changeset
   336
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   337
pub fn get_room_update(
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   338
    room_name: Option<String>,
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   339
    room: &HwRoom,
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   340
    master: Option<&HwClient>,
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   341
    response: &mut Response,
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   342
) {
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   343
    let update_msg = RoomUpdated(room_name.unwrap_or(room.name.clone()), room.info(master));
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   344
    response.add(update_msg.send_all().with_protocol(room.protocol_number));
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   345
}
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   346
15543
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   347
pub fn get_room_config_impl(
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   348
    config: &RoomConfig,
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   349
    destination: Destination,
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   350
    response: &mut Response,
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   351
) {
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   352
    response.add(
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   353
        ConfigEntry("FULLMAPCONFIG".to_string(), config.to_map_config())
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   354
            .send_to_destination(destination.clone()),
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   355
    );
14785
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   356
    for cfg in config.to_game_config() {
15543
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   357
        response.add(cfg.to_server_msg().send_to_destination(destination.clone()));
14785
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   358
    }
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   359
}
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   360
15543
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   361
pub fn get_room_config(room: &HwRoom, destination: Destination, response: &mut Response) {
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   362
    get_room_config_impl(room.active_config(), destination, response);
14785
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   363
}
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   364
15543
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   365
pub fn get_teams<'a, I>(teams: I, destination: Destination, response: &mut Response)
14785
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   366
where
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   367
    I: Iterator<Item = &'a TeamInfo>,
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   368
{
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   369
    for team in teams {
15543
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   370
        response.add(TeamAdd(team.to_protocol()).send_to_destination(destination.clone()));
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   371
        response
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   372
            .add(TeamColor(team.name.clone(), team.color).send_to_destination(destination.clone()));
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   373
        response.add(
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   374
            HedgehogsNumber(team.name.clone(), team.hedgehogs_number)
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   375
                .send_to_destination(destination.clone()),
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   376
        );
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   377
    }
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   378
}
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   379
15543
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   380
pub fn get_room_teams(room: &HwRoom, destination: Destination, response: &mut Response) {
15540
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15536
diff changeset
   381
    let current_teams = match room.game_info {
15541
d122b65bdf6f reapply some simplifications
alfadur <mail@none>
parents: 15540
diff changeset
   382
        Some(ref info) => &info.original_teams,
15540
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15536
diff changeset
   383
        None => &room.teams,
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15536
diff changeset
   384
    };
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15536
diff changeset
   385
15543
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   386
    get_teams(current_teams.iter().map(|(_, t)| t), destination, response);
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   387
}
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   388
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   389
pub fn get_room_flags(
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   390
    server: &HwServer,
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   391
    room_id: RoomId,
15543
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   392
    destination: Destination,
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   393
    response: &mut Response,
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   394
) {
15522
4a0b06b03199 make rooms field private
alfadur <mail@none>
parents: 15519
diff changeset
   395
    let room = server.room(room_id);
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   396
    if let Some(id) = room.master_id {
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   397
        response.add(
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
   398
            ClientFlags(
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
   399
                add_flags(&[Flags::RoomMaster]),
15492
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   400
                vec![server.client(id).nick.clone()],
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
   401
            )
15543
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   402
            .send_to_destination(destination.clone()),
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   403
        );
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   404
    }
15492
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   405
    let nicks = server.collect_nicks(|(_, c)| c.room_id == Some(room_id) && c.is_ready());
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   406
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   407
    if !nicks.is_empty() {
15543
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   408
        response
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   409
            .add(ClientFlags(add_flags(&[Flags::Ready]), nicks).send_to_destination(destination));
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   410
    }
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   411
}
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   412
15525
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   413
pub fn check_vote(
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   414
    server: &HwServer,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   415
    room: &HwRoom,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   416
    kind: &VoteType,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   417
    response: &mut Response,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   418
) -> bool {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   419
    let error = match &kind {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   420
        VoteType::Kick(nick) => {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   421
            if server
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   422
                .find_client(&nick)
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   423
                .filter(|c| c.room_id == Some(room.id))
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   424
                .is_some()
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   425
            {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   426
                None
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   427
            } else {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   428
                Some("/callvote kick: No such user!".to_string())
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   429
            }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   430
        }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   431
        VoteType::Map(None) => {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   432
            let names: Vec<_> = room.saves.keys().cloned().collect();
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   433
            if names.is_empty() {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   434
                Some("/callvote map: No maps saved in this room!".to_string())
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   435
            } else {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   436
                Some(format!("Available maps: {}", names.join(", ")))
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   437
            }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   438
        }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   439
        VoteType::Map(Some(name)) => {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   440
            if room.saves.get(&name[..]).is_some() {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   441
                None
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   442
            } else {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   443
                Some("/callvote map: No such map!".to_string())
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   444
            }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   445
        }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   446
        VoteType::Pause => {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   447
            if room.game_info.is_some() {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   448
                None
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   449
            } else {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   450
                Some("/callvote pause: No game in progress!".to_string())
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   451
            }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   452
        }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   453
        VoteType::NewSeed => None,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   454
        VoteType::HedgehogsPerTeam(number) => match number {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   455
            1..=MAX_HEDGEHOGS_PER_TEAM => None,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   456
            _ => Some("/callvote hedgehogs: Specify number from 1 to 8.".to_string()),
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   457
        },
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   458
    };
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   459
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   460
    match error {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   461
        None => true,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   462
        Some(msg) => {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   463
            response.add(server_chat(msg).send_self());
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   464
            false
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   465
        }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   466
    }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   467
}
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   468
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   469
pub fn get_vote_data(
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   470
    room_id: RoomId,
15525
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   471
    result: &Result<VoteResult, VoteError>,
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   472
    response: &mut Response,
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   473
) {
15525
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   474
    match result {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   475
        Ok(VoteResult::Submitted) => {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   476
            response.add(server_chat("Your vote has been counted.".to_string()).send_self())
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   477
        }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   478
        Ok(VoteResult::Succeeded(_)) | Ok(VoteResult::Failed) => response.add(
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   479
            server_chat("Voting closed.".to_string())
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   480
                .send_all()
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   481
                .in_room(room_id),
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   482
        ),
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   483
        Err(VoteError::NoVoting) => {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   484
            response.add(server_chat("There's no voting going on.".to_string()).send_self())
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   485
        }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   486
        Err(VoteError::AlreadyVoted) => {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   487
            response.add(server_chat("You already have voted.".to_string()).send_self())
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   488
        }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   489
    }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   490
}
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   491
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   492
pub fn handle_vote(
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   493
    mut room_control: HwRoomControl,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   494
    result: Result<VoteResult, VoteError>,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   495
    response: &mut super::Response,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   496
) {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   497
    let room_id = room_control.room().id;
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   498
    super::common::get_vote_data(room_control.room().id, &result, response);
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   499
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   500
    if let Ok(VoteResult::Succeeded(kind)) = result {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   501
        match kind {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   502
            VoteType::Kick(nick) => {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   503
                if let Some(kicked_client) = room_control.server().find_client(&nick) {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   504
                    let kicked_id = kicked_client.id;
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   505
                    if let Some(mut room_control) = room_control.change_client(kicked_id) {
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   506
                        response.add(Kicked.send(kicked_id));
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   507
                        let result = room_control.leave_room();
15525
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   508
                        super::common::get_room_leave_result(
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   509
                            room_control.server(),
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   510
                            room_control.room(),
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   511
                            "kicked",
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   512
                            result,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   513
                            response,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   514
                        );
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   515
                    }
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   516
                }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   517
            }
15525
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   518
            VoteType::Map(None) => (),
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   519
            VoteType::Map(Some(name)) => {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   520
                if let Some(location) = room_control.load_config(&name) {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   521
                    let msg = server_chat(location.to_string());
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   522
                    let room = room_control.room();
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   523
                    response.add(msg.send_all().in_room(room.id));
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   524
15525
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   525
                    let room_master = room.master_id.map(|id| room_control.server().client(id));
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   526
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   527
                    super::common::get_room_update(None, room, room_master, response);
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   528
15543
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   529
                    let room_destination = Destination::ToAll {
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   530
                        group: DestinationGroup::Room(room.id),
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   531
                        skip_self: false,
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   532
                    };
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15541
diff changeset
   533
                    super::common::get_room_config(room, room_destination, response);
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   534
                }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   535
            }
15525
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   536
            VoteType::Pause => {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   537
                if room_control.toggle_pause() {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   538
                    response.add(
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   539
                        server_chat("Pause toggled.".to_string())
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   540
                            .send_all()
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   541
                            .in_room(room_id),
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   542
                    );
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   543
                    response.add(
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   544
                        ForwardEngineMessage(vec![to_engine_msg(once(b'I'))])
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   545
                            .send_all()
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   546
                            .in_room(room_id),
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   547
                    );
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   548
                }
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   549
            }
15525
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   550
            VoteType::NewSeed => {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   551
                let seed = thread_rng().gen_range(0, 1_000_000_000).to_string();
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   552
                let cfg = GameCfg::Seed(seed);
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   553
                response.add(cfg.to_server_msg().send_all().in_room(room_id));
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   554
                room_control.set_config(cfg);
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   555
            }
15525
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   556
            VoteType::HedgehogsPerTeam(number) => {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   557
                let nicks = room_control.set_hedgehogs_number(number);
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   558
                response.extend(
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   559
                    nicks
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   560
                        .into_iter()
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   561
                        .map(|n| HedgehogsNumber(n, number).send_all().in_room(room_id)),
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   562
                );
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   563
            }
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   564
        }
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   565
    }
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   566
}
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   567
15492
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   568
pub fn get_start_game_data(
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   569
    server: &HwServer,
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   570
    room_id: RoomId,
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   571
    result: Result<Vec<String>, StartGameError>,
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   572
    response: &mut Response,
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   573
) {
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   574
    match result {
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   575
        Ok(room_nicks) => {
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   576
            let room = server.room(room_id);
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   577
            response.add(RunGame.send_all().in_room(room.id));
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   578
            response.add(
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   579
                ClientFlags(add_flags(&[Flags::InGame]), room_nicks)
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   580
                    .send_all()
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   581
                    .in_room(room.id),
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   582
            );
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   583
15492
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   584
            let room_master = room.master_id.map(|id| server.client(id));
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   585
            get_room_update(None, room, room_master, response);
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   586
        }
15492
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   587
        Err(StartGameError::NotEnoughClans) => {
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   588
            response.warn("The game can't be started with less than two clans!")
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   589
        }
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   590
        Err(StartGameError::NotReady) => response.warn("Not all players are ready"),
15516
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   591
        Err(StartGameError::AlreadyInGame) => response.warn("The game is already in progress"),
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   592
    }
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   593
}
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   594
15516
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   595
pub fn get_end_game_result(
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   596
    server: &HwServer,
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   597
    room_id: RoomId,
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   598
    result: EndGameResult,
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   599
    response: &mut Response,
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   600
) {
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   601
    let room = server.room(room_id);
15525
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   602
    let room_master = room.master_id.map(|id| server.client(id));
15516
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   603
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   604
    get_room_update(None, room, room_master, response);
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   605
    response.add(RoundFinished.send_all().in_room(room_id));
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   606
15541
d122b65bdf6f reapply some simplifications
alfadur <mail@none>
parents: 15540
diff changeset
   607
    response.extend(
d122b65bdf6f reapply some simplifications
alfadur <mail@none>
parents: 15540
diff changeset
   608
        result
d122b65bdf6f reapply some simplifications
alfadur <mail@none>
parents: 15540
diff changeset
   609
            .left_teams
d122b65bdf6f reapply some simplifications
alfadur <mail@none>
parents: 15540
diff changeset
   610
            .iter()
d122b65bdf6f reapply some simplifications
alfadur <mail@none>
parents: 15540
diff changeset
   611
            .map(|name| TeamRemove(name.clone()).send_all().in_room(room.id)),
d122b65bdf6f reapply some simplifications
alfadur <mail@none>
parents: 15540
diff changeset
   612
    );
d122b65bdf6f reapply some simplifications
alfadur <mail@none>
parents: 15540
diff changeset
   613
15544
77551d5e0a3b send 🦔 on round end
alfadur
parents: 15543
diff changeset
   614
    let midgame_destination = Destination::ToIds(result.joined_mid_game_clients);
77551d5e0a3b send 🦔 on round end
alfadur
parents: 15543
diff changeset
   615
    for (_, team) in &room.teams {
77551d5e0a3b send 🦔 on round end
alfadur
parents: 15543
diff changeset
   616
        response.add(
77551d5e0a3b send 🦔 on round end
alfadur
parents: 15543
diff changeset
   617
            HedgehogsNumber(team.name.clone(), team.hedgehogs_number)
77551d5e0a3b send 🦔 on round end
alfadur
parents: 15543
diff changeset
   618
                .send_to_destination(midgame_destination.clone()),
77551d5e0a3b send 🦔 on round end
alfadur
parents: 15543
diff changeset
   619
        );
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   620
    }
15544
77551d5e0a3b send 🦔 on round end
alfadur
parents: 15543
diff changeset
   621
    super::common::get_room_config(room, midgame_destination.clone(), response);
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   622
15516
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   623
    if !result.unreadied_nicks.is_empty() {
15541
d122b65bdf6f reapply some simplifications
alfadur <mail@none>
parents: 15540
diff changeset
   624
        response.add(
15516
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   625
            ClientFlags(remove_flags(&[Flags::Ready]), result.unreadied_nicks)
15541
d122b65bdf6f reapply some simplifications
alfadur <mail@none>
parents: 15540
diff changeset
   626
                .send_all()
d122b65bdf6f reapply some simplifications
alfadur <mail@none>
parents: 15540
diff changeset
   627
                .in_room(room_id),
d122b65bdf6f reapply some simplifications
alfadur <mail@none>
parents: 15540
diff changeset
   628
        );
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   629
    }
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   630
}
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   631
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   632
#[cfg(test)]
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   633
mod tests {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   634
    use super::*;
15111
1e45db229f9f make greeting argument optional
alfadur
parents: 15075
diff changeset
   635
    use crate::handlers::actions::PendingMessage;
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   636
    use crate::protocol::messages::HwServerMessage::ChatMsg;
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   637
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   638
    fn reply2string(r: HwServerMessage) -> String {
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   639
        match r {
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   640
            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
   641
            _ => panic!("expected a ChatMsg"),
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   642
        }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   643
    }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   644
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   645
    fn run_handle_test(opts: Vec<String>) {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   646
        let opts2 = opts.clone();
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   647
        for opt in opts {
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
   648
            while reply2string(rnd_reply(&opts2)) != opt {}
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   649
        }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   650
    }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   651
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   652
    /// This test terminates almost surely.
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   653
    #[test]
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   654
    fn test_handle_rnd_empty() {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   655
        run_handle_test(vec![])
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   656
    }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   657
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   658
    /// This test terminates almost surely.
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   659
    #[test]
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   660
    fn test_handle_rnd_nonempty() {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   661
        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
   662
    }
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   663
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   664
    /// 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
   665
    #[test]
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   666
    fn test_distribution() {
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   667
        let eps = 0.000001;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   668
        let lim = 0.5;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   669
        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
   670
        let mut ones = 0;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   671
        let mut tries = 0;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   672
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   673
        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
   674
            tries += 1;
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
   675
            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
   676
                ones += 1;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   677
            }
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   678
        }
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   679
    }
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   680
}