complete newseed voting
authoralfadur
Mon, 09 Jul 2018 19:41:26 +0300
changeset 13460 fb37745c5bca
parent 13459 38f9097b6bbc
child 13461 8697b235f236
complete newseed voting
gameServer2/src/server/actions.rs
gameServer2/src/server/handlers/inroom.rs
--- a/gameServer2/src/server/actions.rs	Mon Jul 09 19:39:15 2018 +0300
+++ b/gameServer2/src/server/actions.rs	Mon Jul 09 19:41:26 2018 +0300
@@ -7,7 +7,7 @@
     server::HWServer,
     room::{GameInfo},
     client::HWClient,
-    coretypes::{ClientId, RoomId, VoteType},
+    coretypes::{ClientId, RoomId, GameCfg, VoteType},
     room::HWRoom,
     handlers
 };
@@ -18,6 +18,7 @@
     server_chat
 };
 use utils::to_engine_msg;
+use rand::{thread_rng, Rng, distributions::Uniform};
 
 pub enum Destination {
     ToId(ClientId),
@@ -381,7 +382,7 @@
                     unimplemented!();
                 },
                 VoteType::Pause => {
-                    if let Some(ref mut info) = server.room(client_id).unwrap().game_info {
+                    if let Some(ref mut info) = server.rooms[room_id].game_info {
                         info.is_paused = !info.is_paused;
                         actions.push(server_chat("Pause toggled.")
                             .send_all().in_room(room_id).action());
@@ -390,7 +391,10 @@
                     }
                 },
                 VoteType::NewSeed => {
-                    unimplemented!();
+                    let seed = thread_rng().gen_range(0, 1_000_000_000).to_string();
+                    let cfg = GameCfg::Seed(seed);
+                    actions.push(cfg.to_server_msg().send_all().in_room(room_id).action());
+                    server.rooms[room_id].set_config(cfg);
                 },
                 VoteType::HedgehogsPerTeam(number) => {
                     let r = &mut server.rooms[room_id];
--- a/gameServer2/src/server/handlers/inroom.rs	Mon Jul 09 19:39:15 2018 +0300
+++ b/gameServer2/src/server/handlers/inroom.rs	Mon Jul 09 19:41:26 2018 +0300
@@ -71,6 +71,16 @@
     msg.get(1).filter(|t| !NON_TIMED_MESSAGES.contains(t)).is_some()
 }
 
+fn voting_description(kind: &VoteType) -> String {
+    format!("New voting started: {}", match kind {
+        VoteType::Kick(nick) => format!("kick {}", nick),
+        VoteType::Map(name) => format!("map {}", name.as_ref().unwrap()),
+        VoteType::Pause => "pause".to_string(),
+        VoteType::NewSeed => "new seed".to_string(),
+        VoteType::HedgehogsPerTeam(number) => format!("hedgehogs per team: {}", number)
+    })
+}
+
 pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
     use protocol::messages::HWProtocolMessage::*;
     match message {
@@ -304,11 +314,11 @@
             };
             match error {
                 None => {
+                    let msg = voting_description(&kind);
                     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(),
+                        server_chat(&msg).send_all().in_room(room_id).action(),
                         AddVote{ vote: true, is_forced: false}]);
                 }
                 Some(msg) => {