The Battle for Wesnoth  1.19.0-dev
mp_ui_alerts.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2024
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 / notifications
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 const std::string _player_joins = "player_joins";
53 const std::string _player_leaves = "player_leaves";
54 const std::string _private_message = "private_message";
55 const std::string _friend_message = "friend_message";
56 const std::string _public_message = "public_message";
57 const std::string _server_message = "server_message";
58 const std::string _ready_for_start = "ready_for_start";
59 const std::string _game_has_begun = "game_has_begun";
60 const std::string _turn_changed = "turn_changed";
61 const std::string _game_created = "game_created";
62 } // end anonymous namespace
63 
64 const std::vector<std::string> items{
65  _player_joins,
66  _player_leaves,
67  _private_message,
68  _friend_message,
69  _public_message,
70  _server_message,
71  _ready_for_start,
72  _game_has_begun,
73  _turn_changed,
74  _game_created,
75 };
76 
77 void game_created(const std::string& scenario, const std::string& name)
78 {
79  if(!lobby_pref(_game_created)) {
80  return;
81  }
82 
83  if(sound_pref(_game_created)) {
85  }
86 
87  if(notif_pref(_game_created)) {
88  const std::string message = VGETTEXT("A game ($name|, $scenario|) has been created", {{"name", name}, {"scenario", scenario}});
90  }
91 }
92 
93 void player_joins(bool is_lobby)
94 {
95  if(is_lobby && !lobby_pref(_player_joins)) {
96  return;
97  }
98 
99  if(sound_pref(_player_joins)) {
101  }
102 
103  if(notif_pref(_player_joins)) {
104  desktop::notifications::send(_("Wesnoth"), _("A player has joined"), desktop::notifications::OTHER);
105  }
106 }
107 
108 void player_leaves(bool is_lobby)
109 {
110  if(is_lobby && !lobby_pref(_player_leaves)) {
111  return;
112  }
113 
114  if(sound_pref(_player_leaves)) {
116  }
117 
118  if(notif_pref(_player_leaves)) {
119  desktop::notifications::send(_("Wesnoth"), _("A player has left"), desktop::notifications::OTHER);
120  }
121 }
122 
123 void public_message(bool is_lobby, const std::string& sender, const std::string& message)
124 {
125  if(is_lobby && !lobby_pref(_public_message)) {
126  return;
127  }
128 
129  if(sound_pref(_public_message)) {
131  }
132 
133  if(notif_pref(_public_message)) {
135  }
136 }
137 
138 void friend_message(bool is_lobby, const std::string& sender, const std::string& message)
139 {
140  if(is_lobby && !lobby_pref(_friend_message)) {
141  return;
142  }
143 
144  if(sound_pref(_friend_message)) {
146  }
147 
148  if(notif_pref(_friend_message)) {
150  }
151 }
152 
153 void private_message(bool is_lobby, const std::string& sender, const std::string& message)
154 {
155  if(is_lobby && !lobby_pref(_private_message)) {
156  return;
157  }
158 
159  if(sound_pref(_private_message)) {
161  }
162 
163  if(notif_pref(_private_message)) {
165  }
166 }
167 
168 void server_message(bool is_lobby, const std::string& sender, const std::string& message)
169 {
170  if(is_lobby && !lobby_pref(_server_message)) {
171  return;
172  }
173 
174  if(sound_pref(_server_message)) {
176  }
177 
178  if(notif_pref(_server_message)) {
180  }
181 }
182 
184 {
185  if(sound_pref(_ready_for_start)) {
187  // this is play_bell instead of play_UI_sound to economize on sound channels. UI only has two
188  // sounds, and turn bell has a dedicated channel.
190  }
191  }
192 
193  if(notif_pref(_ready_for_start)) {
194  desktop::notifications::send(_("Wesnoth"), _("Ready to start!"), desktop::notifications::OTHER);
195  }
196 }
197 
199 {
200  if(sound_pref(_game_has_begun)) {
202  }
203 
204  if(notif_pref(_game_has_begun)) {
205  desktop::notifications::send(_("Wesnoth"), _("Game has begun!"), desktop::notifications::OTHER);
206  }
207 }
208 
209 void turn_changed(const std::string& player_name)
210 {
211  if(notif_pref(_turn_changed)) {
212  utils::string_map player;
213  player["name"] = player_name;
214  desktop::notifications::send(_("Turn changed"), VGETTEXT("$name has taken control", player), desktop::notifications::TURN_CHANGED);
215  }
216 }
217 
218 bool get_def_pref_sound(const std::string& id)
219 {
220  return (id != _public_message && id != _friend_message);
221 }
222 
223 bool get_def_pref_notif(const std::string& id)
224 {
225  return (desktop::notifications::available() && (
226  id == _private_message ||
227  id == _ready_for_start ||
228  id == _game_has_begun ||
229  id == _turn_changed ||
230  id == _game_created
231  ));
232 }
233 
234 bool get_def_pref_lobby(const std::string& id)
235 {
236  return (id == _private_message || id == _server_message || id == _game_created);
237 }
238 
239 } // end namespace mp_ui_alerts
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
static std::string _(const char *str)
Definition: gettext.hpp:93
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 public_message
std::string private_message
std::string player_leaves
std::string server_message
std::string friend_message
std::string ready_for_start
std::string game_has_begun
std::string game_created
std::string player_joins
This namespace provides handlers which play the sounds / notifications for various mp server events,...
void turn_changed(const std::string &player_name)
void public_message(bool is_lobby, const std::string &sender, const std::string &message)
const std::vector< std::string > items
bool get_def_pref_lobby(const std::string &id)
void game_created(const std::string &scenario, const std::string &name)
void private_message(bool is_lobby, const std::string &sender, const std::string &message)
void player_joins(bool is_lobby)
void game_has_begun()
void friend_message(bool is_lobby, const std::string &sender, const std::string &message)
void ready_for_start()
bool get_def_pref_sound(const std::string &id)
bool get_def_pref_notif(const std::string &id)
void server_message(bool is_lobby, const std::string &sender, const std::string &message)
void player_leaves(bool is_lobby)
bool UI_sound_on()
Definition: general.cpp:699
std::string get(const std::string &key)
Definition: general.cpp:213
void play_UI_sound(const std::string &files)
Definition: sound.cpp:1064
void play_bell(const std::string &files)
Definition: sound.cpp:1048
std::map< std::string, t_string > string_map