Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
notificationsystem.c
Go to the documentation of this file.
1 const static float NOTIFICATION_FADE_TIME = 3.0;
2 
4 {
11  //Please add types before this item
13 }
14 
16 {
18 
21  string m_DetailText;
22 
23  void NotificationRuntimeData(float time, NotificationData data, string detail_text)
24  {
25  m_NotificationTime = time;
26  m_TimeRemaining = time;
27  m_StaticData = data;
28 
29  if (detail_text != "")
30  m_DetailText = detail_text;
31  else
32  m_DetailText = m_StaticData.m_DescriptionText;
33  }
34 
35  float GetTime()
36  {
37  return m_NotificationTime;
38  }
39 
41  {
42  return m_TimeRemaining;
43  }
44 
45  string GetIcon()
46  {
47  return m_StaticData.m_Icon;
48  }
49 
50  string GetTitleText()
51  {
52  return m_StaticData.m_TitleText;
53  }
54 
55  string GetDetailText()
56  {
57  return m_DetailText;
58  }
59 
60  void UpdateRemainingTime(float updateInterval)
61  {
62  m_TimeRemaining -= updateInterval;
63  }
64 
66  void SetTime(float time);
67 }
68 
70 {
71  const int DEFAULT_TIME_DISPLAYED = 10;
72  const float NOTIFICATION_FADE_TIME = 3.0;
73 
74  protected static const string JSON_FILE_PATH = "scripts/data/notifications.json";
75  protected static const int MAX_NOTIFICATIONS = 5;
76 
77  private static const float UPDATE_INTERVAL_THRESHOLD = 1.0;
78 
79  protected static ref NotificationSystem m_Instance;
80 
81  protected ref array<ref NotificationRuntimeData> m_TimeArray;
82  protected ref array<ref NotificationRuntimeData> m_DeferredArray;
83  protected ref map<NotificationType, ref NotificationData> m_DataArray;
84 
85  protected float m_TimeElapsed;
86 
87  ref ScriptInvoker m_OnNotificationAdded = new ScriptInvoker();
88  ref ScriptInvoker m_OnNotificationRemoved = new ScriptInvoker();
89 
90  static void InitInstance()
91  {
92  if (!m_Instance)
93  {
94  m_Instance = new NotificationSystem();
95  m_Instance.LoadNotificationData();
96  }
97  }
98 
99  static void CleanupInstance()
100  {
101  m_Instance = null;
102  }
103 
104  static NotificationSystem GetInstance()
105  {
106  return m_Instance;
107  }
108 
109  void NotificationSystem()
110  {
111  m_TimeElapsed = 0.0;
112 
113  m_TimeArray = new array<ref NotificationRuntimeData>();
114  m_DeferredArray = new array<ref NotificationRuntimeData>();
115  }
116 
125  static void SendNotificationToPlayerExtended(Man player, float show_time, string title_text, string detail_text = "", string icon = "")
126  {
127  if (player)
128  {
129  SendNotificationToPlayerIdentityExtended(player.GetIdentity(), show_time, title_text, detail_text, icon);
130  }
131  }
132 
141  static void SendNotificationToPlayerIdentityExtended(PlayerIdentity player, float show_time, string title_text, string detail_text = "", string icon = "")
142  {
143  ScriptRPC rpc = new ScriptRPC();
144 
145  rpc.Write(show_time);
146  rpc.Write(title_text);
147  rpc.Write(detail_text);
148  rpc.Write(icon);
149 
150  rpc.Send(null, ERPCs.RPC_SEND_NOTIFICATION_EXTENDED, true, player);
151  }
152 
160  static void SendNotificationToPlayer(Man player, NotificationType type, float show_time, string detail_text = "")
161  {
162  if (player)
163  {
164  SendNotificationToPlayerIdentity(player.GetIdentity(), type, show_time, detail_text);
165  }
166  }
167 
175  static void SendNotificationToPlayerIdentity( PlayerIdentity player, NotificationType type, float show_time, string detail_text = "" )
176  {
177  ScriptRPC rpc = new ScriptRPC();
178 
179  rpc.Write(type);
180  rpc.Write(show_time);
181  rpc.Write(detail_text);
182 
183  rpc.Send(null, ERPCs.RPC_SEND_NOTIFICATION, true, player);
184  }
185 
192  static void AddNotification(NotificationType type, float show_time, string detail_text = "")
193  {
194  if (m_Instance.m_TimeArray.Count() < MAX_NOTIFICATIONS)
195  {
196  NotificationRuntimeData data = new NotificationRuntimeData(show_time, m_Instance.GetNotificationData(type), detail_text);
197 
198  m_Instance.m_TimeArray.Insert(data);
199  m_Instance.m_OnNotificationAdded.Invoke(data);
200  }
201  else
202  {
203  NotificationRuntimeData dataDeferred = new NotificationRuntimeData(show_time, m_Instance.GetNotificationData(type), detail_text);
204  m_Instance.m_DeferredArray.Insert(dataDeferred);
205  }
206  }
207 
215  static void AddNotificationExtended(float show_time, string title_text, string detail_text = "", string icon = "")
216  {
217  if (m_Instance.m_TimeArray.Count() < MAX_NOTIFICATIONS)
218  {
219  NotificationData tempData = new NotificationData(icon, title_text);
220  NotificationRuntimeData data = new NotificationRuntimeData(show_time, tempData, detail_text);
221 
222  m_Instance.m_TimeArray.Insert(data);
223  m_Instance.m_OnNotificationAdded.Invoke(data);
224  }
225  else
226  {
227  NotificationData tempDataDeferred = new NotificationData(icon, title_text);
228  NotificationRuntimeData dataDeferred = new NotificationRuntimeData(show_time, tempDataDeferred, detail_text);
229  m_Instance.m_DeferredArray.Insert(dataDeferred);
230  }
231  }
232 
233  static void Update(float timeslice)
234  {
235  if (m_Instance)
236  {
237  if (g_Game.GetGameState() != DayZGameState.IN_GAME && g_Game.GetGameState() != DayZGameState.MAIN_MENU)
238  return;
239 
240  m_Instance.m_TimeElapsed += timeslice;
241  if (m_Instance.m_TimeElapsed >= UPDATE_INTERVAL_THRESHOLD)
242  {
244  foreach (NotificationRuntimeData visibleNotificationData : m_Instance.m_TimeArray)
245  {
246  if (visibleNotificationData.GetRemainingTime() <= 0.0)
247  expiredNotifications.Insert(visibleNotificationData);
248  else
249  visibleNotificationData.UpdateRemainingTime(UPDATE_INTERVAL_THRESHOLD);
250  }
251 
252  foreach (NotificationRuntimeData expiredNotificationData : expiredNotifications)
253  {
254  m_Instance.m_OnNotificationRemoved.Invoke(expiredNotificationData);
255  m_Instance.m_TimeArray.RemoveItem(expiredNotificationData);
256 
257  if (m_Instance.m_DeferredArray.Count() > 0)
258  {
259  int count = m_Instance.m_DeferredArray.Count();
260  NotificationRuntimeData deferredNotificationData = m_Instance.m_DeferredArray.Get(count - 1);
261  m_Instance.m_TimeArray.Insert(deferredNotificationData);
262  m_Instance.m_OnNotificationAdded.Invoke(deferredNotificationData);
263  m_Instance.m_DeferredArray.Remove(count - 1);
264  }
265  }
266 
267  m_Instance.m_TimeElapsed = 0;
268  }
269  }
270  }
271 
272  protected NotificationData GetNotificationData(NotificationType type)
273  {
274  if (m_DataArray.Contains(type))
275  return m_DataArray.Get(type);
276 
277  return null;
278  }
279 
280  static void LoadNotificationData()
281  {
283  m_Instance.m_DataArray = new map<NotificationType, ref NotificationData>();
284  string errorMessage;
285  if (!JsonFileLoader<map<NotificationType, NotificationData>>.LoadFile(JSON_FILE_PATH, dataArray, errorMessage))
286  ErrorEx(errorMessage);
287 
288  m_Instance.m_DataArray.Copy(dataArray);
289 
290  array<int> notificationTypes = new array<int>();
291  NotificationType notificationType = 0;
292  while (notificationType != NotificationType.NOTIFICATIONS_END)
293  {
294  notificationTypes.Insert(notificationType);
295  notificationType++;
296  }
297 
298  for (int i = 0; i < m_Instance.m_DataArray.Count(); ++i)
299  {
300  notificationTypes.RemoveItem(m_Instance.m_DataArray.GetKey(i));
301  }
302 
303  if (notificationTypes.Count() > 0)
304  {
305  foreach (NotificationType notificationType2 : notificationTypes)
306  {
307  NotificationData notificationData = new NotificationData(
308  "please_add_an_icon",
309  "please_add_a_title",
310  "optional_description",
311  );
312 
313  m_Instance.m_DataArray.Insert(notificationType2, notificationData);
314  }
315 
316  if (!JsonFileLoader<map<NotificationType, ref NotificationData>>.SaveFile(JSON_FILE_PATH, m_Instance.m_DataArray, errorMessage))
317  ErrorEx(errorMessage);
318  }
319  }
320 }
GENERIC_ERROR
@ GENERIC_ERROR
Definition: notificationsystem.c:10
m_NotificationTime
float m_NotificationTime
Definition: notificationsystem.c:19
m_DetailText
string m_DetailText
Definition: notificationsystem.c:21
NotificationSystem
Definition: notificationsystem.c:69
m_StaticData
enum NotificationType m_StaticData
NOTIFICATIONS_END
@ NOTIFICATIONS_END
Definition: notificationsystem.c:12
UpdateRemainingTime
void UpdateRemainingTime(float updateInterval)
Definition: notificationsystem.c:60
NotificationData
Definition: notificationdata.c:1
INVITE_FAIL_SAME_SERVER
@ INVITE_FAIL_SAME_SERVER
Definition: notificationsystem.c:6
ErrorEx
enum ShapeType ErrorEx
PlayerIdentity
The class that will be instanced (moddable)
Definition: gameplay.c:377
NotificationRuntimeData
void NotificationRuntimeData(float time, NotificationData data, string detail_text)
Definition: notificationsystem.c:23
map
map
Definition: controlsxboxnew.c:3
m_TimeRemaining
float m_TimeRemaining
Definition: notificationsystem.c:20
ScriptRPC
Definition: gameplay.c:104
g_Game
DayZGame g_Game
Definition: dayzgame.c:3727
FRIEND_CONNECTED
@ FRIEND_CONNECTED
Definition: notificationsystem.c:5
GetRemainingTime
float GetRemainingTime()
Definition: notificationsystem.c:40
array< ref NotificationRuntimeData >
JOIN_FAIL_GET_SESSION
@ JOIN_FAIL_GET_SESSION
Definition: notificationsystem.c:7
GetIcon
string GetIcon()
Definition: notificationsystem.c:45
NotificationType
NotificationType
DEPRECATED (moved into NotificationSystem)
Definition: notificationsystem.c:3
ERPCs
ERPCs
Definition: erpcs.c:1
GetTime
float GetTime()
Definition: notificationsystem.c:35
GetDetailText
string GetDetailText()
Definition: notificationsystem.c:55
SetTime
void SetTime(float time)
DEPRECATED.
GetTitleText
string GetTitleText()
Definition: notificationsystem.c:50
CONNECT_FAIL_GENERIC
@ CONNECT_FAIL_GENERIC
Definition: notificationsystem.c:8
DISCONNECTED
@ DISCONNECTED
Definition: notificationsystem.c:9
ScriptInvoker
ScriptInvoker Class provide list of callbacks usage:
Definition: tools.c:115