gameServer2/src/server/handlers/inroom.rs
changeset 13483 d79795acaa73
parent 13482 f748a72432f2
child 13485 fb37745c5bca
--- a/gameServer2/src/server/handlers/inroom.rs	Fri Jul 06 21:03:03 2018 +0300
+++ b/gameServer2/src/server/handlers/inroom.rs	Sat Jul 07 20:22:31 2018 +0300
@@ -2,11 +2,12 @@
 
 use protocol::messages::{
     HWProtocolMessage,
-    HWServerMessage::*
+    HWServerMessage::*,
+    server_chat
 };
 use server::{
+    coretypes::{ClientId, Voting, VoteType},
     server::HWServer,
-    client::ClientId,
     room::HWRoom,
     actions::{Action, Action::*}
 };
@@ -262,6 +263,76 @@
             };
             server.react(client_id, actions);
         }
+        CallVote(None) => {
+            server.react(client_id, vec![
+                server_chat("Available callvote commands: kick <nickname>, map <name>, pause, newseed, hedgehogs <number>")
+                    .send_self().action()])
+        }
+        CallVote(Some(kind)) => {
+            let (room_id, is_in_game) = server.room(client_id)
+                .map(|r| (r.id, r.game_info.is_some())).unwrap();
+            let error = match &kind {
+                VoteType::Kick(nick) => {
+                    if server.find_client(&nick).filter(|c| c.room_id == Some(room_id)).is_some() {
+                        None
+                    } else {
+                        Some("/callvote kick: No such user!")
+                    }
+                },
+                VoteType::Map(None) => {
+                    Some("/callvote map: Not implemented")
+                },
+                VoteType::Map(Some(name)) => {
+                    Some("/callvote map: Not implemented")
+                },
+                VoteType::Pause => {
+                    if is_in_game {
+                        None
+                    } else {
+                        Some("/callvote pause: No game in progress!")
+                    }
+                },
+                VoteType::NewSeed => {
+                    None
+                },
+                VoteType::HedgehogsPerTeam(number) => {
+                    match number {
+                        1...8 => None,
+                        _ => Some("/callvote hedgehogs: Specify number from 1 to 8.")
+                    }
+                },
+            };
+            match error {
+                None => {
+                    let voting = Voting::new(kind, server.room_clients(client_id));
+                    server.room(client_id).unwrap().voting = Some(voting);
+                    server.react(client_id, vec![
+                        server_chat("New voting started: ")
+                            .send_all().in_room(room_id).action(),
+                        AddVote{ vote: true, is_forced: false}]);
+                }
+                Some(msg) => {
+                    server.react(client_id, vec![
+                        server_chat(msg).send_self().action()])
+                }
+            }
+        }
+        Vote(vote) => {
+            let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
+                vec![AddVote{ vote, is_forced: false }]
+            } else {
+                Vec::new()
+            };
+            server.react(client_id, actions);
+        }
+        ForceVote(vote) => {
+            let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
+                vec![AddVote{ vote, is_forced: c.is_admin} ]
+            } else {
+                Vec::new()
+            };
+            server.react(client_id, actions);
+        }
         StartGame => {
             let actions = if let (_, Some(r)) = server.client_and_room(client_id) {
                 vec![StartRoomGame(r.id)]