The Battle for Wesnoth  1.15.2+dev
mp_ui_alerts.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by Chris Beck <render787@gmail.com>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * This namespace provides handlers which play the sounds / notificaitons
17  * for various mp server events, depending on the preference configuration.
18  */
19 
20 #include "mp_ui_alerts.hpp"
21 
23 #include "formula/string_utils.hpp"
24 #include "game_config.hpp"
25 #include "gettext.hpp"
26 #include "preferences/general.hpp"
27 #include "sound.hpp"
28 
29 #include <string>
30 #include <vector>
31 
32 namespace mp_ui_alerts {
33 
34 namespace {
35 
36 bool lobby_pref(const std::string& id)
37 {
38  return preferences::get(id + "_lobby", get_def_pref_lobby(id));
39 }
40 
41 bool sound_pref(const std::string& id)
42 {
43  return preferences::get(id + "_sound", get_def_pref_sound(id));
44 }
45 
46 bool notif_pref(const std::string& id)
47 {
48  return preferences::get(id + "_notif", get_def_pref_notif(id));
49 }
50 
51 } // end anonymous namespace
52 
53 const std::vector<std::string> items {"player_joins", "player_leaves", "private_message", "friend_message", "public_message", "server_message", "ready_for_start", "game_has_begun", "turn_changed", "game_created"};
54 
55 void game_created(const std::string & scenario, const std::string & name)
56 {
57  std::string id = "game_created";
58  if (!lobby_pref(id)) {
59  return ;
60  }
61 
62  if (sound_pref(id)) {
64  }
65 
66  if (notif_pref(id)) {
67  const std::string message = VGETTEXT("A game ($name|, $scenario|) has been created", {{"name", name}, {"scenario", scenario}});
69  }
70 }
71 
72 void player_joins(bool is_lobby)
73 {
74  std::string id = "player_enters";
75  if (is_lobby && !lobby_pref(id)) {
76  return ;
77  }
78  if (sound_pref(id)) {
80  }
81  if (notif_pref(id)) {
82  desktop::notifications::send(_("Wesnoth"), _("A player has joined"), desktop::notifications::OTHER);
83  }
84 }
85 
86 void player_leaves(bool is_lobby)
87 {
88  std::string id = "player_leaves";
89  if (is_lobby && !lobby_pref(id)) {
90  return ;
91  }
92  if (sound_pref(id)) {
94  }
95  if (notif_pref(id)) {
96  desktop::notifications::send(_("Wesnoth"), _("A player has left"), desktop::notifications::OTHER);
97  }
98 }
99 
100 void public_message(bool is_lobby, const std::string & sender, const std::string & message)
101 {
102  std::string id = "public_message";
103  if (is_lobby && !lobby_pref(id)) {
104  return ;
105  }
106  if (sound_pref(id)) {
108  }
109  if (notif_pref(id)) {
111  }
112 }
113 
114 void friend_message(bool is_lobby, const std::string & sender, const std::string & message)
115 {
116  std::string id = "friend_message";
117  if (is_lobby && !lobby_pref(id)) {
118  return ;
119  }
120  if (sound_pref(id)) {
122  }
123  if (notif_pref(id)) {
125  }
126 }
127 
128 void private_message(bool is_lobby, const std::string & sender, const std::string & message)
129 {
130  std::string id = "private_message";
131  if (is_lobby && !lobby_pref(id)) {
132  return ;
133  }
134  if (sound_pref(id)) {
136  }
137  if (notif_pref(id)) {
139  }
140 }
141 
142 void server_message(bool is_lobby, const std::string & sender, const std::string & message)
143 {
144  std::string id = "server_message";
145  if (is_lobby && !lobby_pref(id)) {
146  return ;
147  }
148  if (sound_pref(id)) {
150  }
151  if (notif_pref(id)) {
153  }
154 }
155 
157 {
158  std::string id = "ready_for_start";
159  if (sound_pref(id)) {
160  if (preferences::UI_sound_on()) {
161  sound::play_bell(game_config::sounds::ready_for_start); //this is play_bell instead of play_UI_sound to economize on sound channels. UI only has two sounds, and turn bell has a dedicated channel.
162  }
163  }
164  if (notif_pref(id)) {
165  desktop::notifications::send(_("Wesnoth"), _("Ready to start!"), desktop::notifications::OTHER);
166  }
167 }
168 
170 {
171  std::string id = "game_has_begun";
172  if (sound_pref(id)) {
174  }
175  if (notif_pref(id)) {
176  desktop::notifications::send(_("Wesnoth"), _ ("Game has begun!"), desktop::notifications::OTHER);
177  }
178 }
179 
180 void turn_changed(const std::string & player_name)
181 {
182  std::string id = "turn_changed";
183  if (notif_pref(id)) {
184  utils::string_map player;
185  player["name"] = player_name;
186  desktop::notifications::send(_("Turn changed"), VGETTEXT("$name has taken control", player), desktop::notifications::TURN_CHANGED);
187  }
188 }
189 
190 bool get_def_pref_sound(const std::string & id) {
191  return (id != "public_message" && id != "friend_message");
192 }
193 
194 bool get_def_pref_notif(const std::string & id) {
195  return (desktop::notifications::available() && (id == "private_message" || id == "ready_for_start" || id == "game_has_begun" || id == "turn_changed" || id == "game_created"));
196 }
197 
198 bool get_def_pref_lobby(const std::string & id) {
199  return (id == "private_message" || id == "server_message" || id == "game_created");
200 }
201 
202 
203 } // end namespace mp_ui_alerts
void ready_for_start()
void turn_changed(const std::string &player_name)
std::map< std::string, t_string > string_map
void game_has_begun()
std::string private_message
This namespace provides handlers which play the sounds / notificaitons for various mp server events...
std::string friend_message
const std::vector< std::string > items
std::string player_joins
void friend_message(bool is_lobby, const std::string &sender, const std::string &message)
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:91
std::string get(const std::string &key)
Definition: general.cpp:228
void private_message(bool is_lobby, const std::string &sender, const std::string &message)
std::string game_created
std::string ready_for_start
bool UI_sound_on()
Definition: general.cpp:639
bool available()
Returns whether we were compiled with support for desktop notifications.
void send(const std::string &, const std::string &, type)
Displays a desktop notification message, from owner, of type t.
std::string server_message
std::string public_message
void public_message(bool is_lobby, const std::string &sender, const std::string &message)
void server_message(bool is_lobby, const std::string &sender, const std::string &message)
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
bool get_def_pref_sound(const std::string &id)
void player_leaves(bool is_lobby)
std::string player_leaves
void play_bell(const std::string &files)
Definition: sound.cpp:1003
void game_created(const std::string &scenario, const std::string &name)
void player_joins(bool is_lobby)
void play_UI_sound(const std::string &files)
Definition: sound.cpp:1019
bool get_def_pref_lobby(const std::string &id)
std::string game_has_begun
bool get_def_pref_notif(const std::string &id)