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