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