ManaPlus
killstats.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2009 The Mana World Development Team
4  * Copyright (C) 2011-2019 The ManaPlus Developers
5  * Copyright (C) 2009-2021 Andrei Karas
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include "gui/windows/killstats.h"
24 
25 #include "gui/widgets/button.h"
26 #include "gui/widgets/label.h"
27 #include "gui/widgets/layoutcell.h"
28 
30 
31 #include "client.h"
32 #include "game.h"
33 
34 #include "being/localplayer.h"
35 #include "being/playerinfo.h"
36 
37 #include "utils/gettext.h"
38 
39 #ifdef WIN32
40 #include <sys/time.h>
41 #endif // WIN32
42 
43 #include "debug.h"
44 
45 KillStats *killStats = nullptr;
46 
48  // TRANSLATORS: kill stats window name
49  Window(_("Kill stats"), Modal_false, nullptr, "killstats.xml"),
52  mKillTimer(0),
53  // TRANSLATORS: kill stats window button
54  mResetButton(new Button(this, _("Reset stats"), "reset",
55  BUTTON_SKIN, this)),
56  // TRANSLATORS: kill stats window button
57  mTimerButton(new Button(this, _("Reset timer"), "timer",
58  BUTTON_SKIN, this)),
59  mLine1(nullptr),
60  mLine2(nullptr),
61  mLine3(nullptr),
62  // TRANSLATORS: kill stats window label
63  mLine4(new Label(this, strprintf(_("Kills: %s, total exp: %s"),
64  "?", "?"))),
65  // TRANSLATORS: kill stats window label
66  mLine5(new Label(this, strprintf(_("Avg Exp: %s"), "?"))),
67  // TRANSLATORS: kill stats window label
68  mLine6(new Label(this, strprintf(_("No. of avg mob to next level: %s"),
69  "?"))),
70  // TRANSLATORS: kill stats window label
71  mLine7(new Label(this, strprintf(_("Kills/Min: %s, Exp/Min: %s"),
72  "?", "?"))),
73  mExpSpeed1Label(new Label(this, strprintf(ngettext(
74  // TRANSLATORS: kill stats window label
75  "Exp speed per %d min: %s", "Exp speed per %d min: %s", 1), 1, "?"))),
76  mExpTime1Label(new Label(this, strprintf(ngettext(
77  "Time for next level per %d min: %s",
78  "Time for next level per %d min: %s", 1), 1, "?"))),
79  mExpSpeed5Label(new Label(this, strprintf(ngettext(
80  "Exp speed per %d min: %s", "Exp speed per %d min: %s", 5), 5, "?"))),
81  mExpTime5Label(new Label(this, strprintf(ngettext(
82  "Time for next level per %d min: %s",
83  "Time for next level per %d min: %s", 5), 5, "?"))),
84  mExpSpeed15Label(new Label(this, strprintf(ngettext(
85  "Exp speed per %d min: %s", "Exp speed per %d min: %s", 15),
86  15, "?"))),
87  mExpTime15Label(new Label(this, strprintf(ngettext(
88  "Time for next level per %d min: %s",
89  "Time for next level per %d min: %s", 15), 15, "?"))),
90  // TRANSLATORS: kill stats window label
91  mLastKillExpLabel(new Label(this, strprintf("%s ?", _("Last kill exp:")))),
92  mKillCounter(0),
93  mExpCounter(0),
94  mKillTCounter(0),
95  mExpTCounter(0),
96  m1minExpTime(0),
97  m1minExpNum(0),
98  m1minSpeed(0),
99  m5minExpTime(0),
100  m5minExpNum(0),
101  m5minSpeed(0),
102  m15minExpTime(0),
103  m15minExpNum(0),
104  m15minSpeed(0)
105 {
106  setWindowName("Kill stats");
107  setCloseButton(true);
108  setResizable(true);
109  setSaveVisible(true);
110  setStickyButtonLock(true);
111  setDefaultSize(250, 250, 350, 300);
112 
113  if (setupWindow != nullptr)
115 
117  int64_t xpNextLevel(PlayerInfo::getAttribute64(
119 
120  if (xpNextLevel == 0)
121  xpNextLevel = 1;
122 
123  // TRANSLATORS: kill stats window label
124  mLine1 = new Label(this, strprintf(_("Level: %d at %f%%"),
125  localPlayer->getLevel(), static_cast<double>(xp)
126  / static_cast<double>(xpNextLevel) * 100.0));
127 
128  const std::string strXp = toString(CAST_U64(xp));
129  const std::string strXpNextLevel = toString(CAST_U64(xpNextLevel));
130  const std::string strXpLeft = toString(CAST_U64(xpNextLevel - xp));
131  const std::string strXpPercent = toString(CAST_U64(xpNextLevel / 100));
132  // TRANSLATORS: kill stats window label
133  mLine2 = new Label(this, strprintf(_("Exp: %s/%s Left: %s"),
134  strXp.c_str(),
135  strXpNextLevel.c_str(),
136  strXpLeft.c_str()));
137 
138  // TRANSLATORS: kill stats window label
139  mLine3 = new Label(this, strprintf(_("1%% = %s exp, avg mob for 1%%: %s"),
140  strXpPercent.c_str(),
141  "?"));
142 
143  place(0, 0, mLine1, 6, 1).setPadding(0);
144  place(0, 1, mLine2, 6, 1).setPadding(0);
145  place(0, 2, mLine3, 6, 1).setPadding(0);
146  place(0, 3, mLine4, 6, 1).setPadding(0);
147  place(0, 4, mLine5, 6, 1).setPadding(0);
148  place(0, 5, mLine6, 6, 1).setPadding(0);
149  place(0, 6, mLine7, 6, 1).setPadding(0);
150 
151  place(0, 7, mLastKillExpLabel, 6, 1).setPadding(0);
152  place(0, 8, mExpSpeed1Label, 6, 1).setPadding(0);
153  place(0, 9, mExpTime1Label, 6, 1).setPadding(0);
154  place(0, 10, mExpSpeed5Label, 6, 1).setPadding(0);
155  place(0, 11, mExpTime5Label, 6, 1).setPadding(0);
156  place(0, 12, mExpSpeed15Label, 6, 1).setPadding(0);
157  place(0, 13, mExpTime15Label, 6, 1).setPadding(0);
158 
159  place(5, 12, mTimerButton, 1, 1).setPadding(0);
160  place(5, 13, mResetButton, 1, 1).setPadding(0);
161 
162  loadWindowState();
163  enableVisibleSound(true);
164 }
165 
166 void KillStats::action(const ActionEvent &event)
167 {
168  const std::string &eventId = event.getId();
169  if (eventId == "reset")
170  {
171  mKillCounter = 0;
172  mExpCounter = 0;
173  const std::string strXpPercent = toString(CAST_U64(
175  mLine3->setCaption(strprintf("1%% = %s exp, avg mob for 1%%: %s",
176  strXpPercent.c_str(),
177  "?"));
178  // TRANSLATORS: kill stats window label
179  mLine4->setCaption(strprintf(_("Kills: %s, total exp: %s"), "?", "?"));
180  // TRANSLATORS: kill stats window label
181  mLine5->setCaption(strprintf(_("Avg Exp: %s"), "?"));
183  // TRANSLATORS: kill stats window label
184  _("No. of avg mob to next level: %s"), "?"));
185 
186  resetTimes();
187  }
188  else if (eventId == "timer")
189  {
190  mKillTimer = 0;
191  mKillTCounter = 0;
192  mExpTCounter = 0;
194  // TRANSLATORS: kill stats window label
195  _("Kills/Min: %s, Exp/Min: %s"), "?", "?"));
196 
197  resetTimes();
198  }
199 }
200 
202 {
203  m1minExpTime = 0;
204  m1minExpNum = 0;
205  m1minSpeed = 0;
206  m5minExpTime = 0;
207  m5minExpNum = 0;
208  m5minSpeed = 0;
209  m15minExpTime = 0;
210  m15minExpNum = 0;
211  m15minSpeed = 0;
212 }
213 
214 void KillStats::gainXp(int64_t xp)
215 {
216  const int64_t expNeed = PlayerInfo::getAttribute64(
218  if (xp == expNeed)
219  xp = 0;
220  else if (xp == 0)
221  return;
222 
223  mKillCounter++;
224  mKillTCounter++;
225 
226  mExpCounter = mExpCounter + xp;
227  mExpTCounter = mExpTCounter + xp;
228  if (mKillCounter == 0)
229  mKillCounter = 1;
230 
231  const float AvgExp = static_cast<float>(mExpCounter)
232  / static_cast<float>(mKillCounter);
233  int64_t xpNextLevel(expNeed);
234 
235  if (mKillTimer == 0)
237 
238  if (xpNextLevel == 0)
239  xpNextLevel = 1;
240 
241  double timeDiff = difftime(cur_time, mKillTimer) / 60;
242 
243  if (timeDiff <= 0.01)
244  timeDiff = 1;
245 
247  // TRANSLATORS: kill stats window label
248  mLine1->setCaption(strprintf(_("Level: %d at %f%%"),
249  localPlayer->getLevel(), static_cast<double>(exp)
250  / static_cast<double>(xpNextLevel) * 100.0));
251 
252  const std::string strXp = toString(CAST_U64(exp));
253  const std::string strXpNextLevel = toString(CAST_U64(xpNextLevel));
254  const std::string strXpLeft = toString(CAST_U64(xpNextLevel - exp));
255  const std::string strXpPercent = toString(CAST_U64(xpNextLevel / 100));
256  // TRANSLATORS: kill stats window label
257  mLine2->setCaption(strprintf(_("Exp: %s/%s Left: %s"),
258  strXp.c_str(),
259  strXpNextLevel.c_str(),
260  strXpLeft.c_str()));
261 
262  if (AvgExp >= -0.001F && AvgExp <= 0.001F)
263  {
264  // TRANSLATORS: kill stats window label
265  mLine3->setCaption(strprintf(_("1%% = %s exp, avg mob for 1%%: %s"),
266  strXpPercent.c_str(),
267  "?"));
268 
269  // TRANSLATORS: kill stats window label
270  mLine5->setCaption(strprintf(_("Avg Exp: %s"),
271  toString(AvgExp).c_str()));
272 
274  // TRANSLATORS: kill stats window label
275  _("No. of avg mob to next level: %s"), "?"));
276  }
277  else
278  {
279  // TRANSLATORS: kill stats window label
280  mLine3->setCaption(strprintf(_("1%% = %s exp, avg mob for 1%%: %s"),
281  strXpPercent.c_str(), toString((static_cast<float>(
282  xpNextLevel) / 100) / AvgExp).c_str()));
283 
284  // TRANSLATORS: kill stats window label
285  mLine5->setCaption(strprintf(_("Avg Exp: %s"),
286  toString(AvgExp).c_str()));
287 
288  // TRANSLATORS: kill stats window label
289  mLine6->setCaption(strprintf(_("No. of avg mob to next level: %s"),
290  toString(static_cast<float>(xpNextLevel - exp) / AvgExp).c_str()));
291  }
292  // TRANSLATORS: kill stats window label
293  mLine4->setCaption(strprintf(_("Kills: %s, total exp: %s"),
294  toString(mKillCounter).c_str(),
295  toString(CAST_U64(mExpCounter)).c_str()));
296 
297  // TRANSLATORS: kill stats window label
298  mLine7->setCaption(strprintf(_("Kills/Min: %s, Exp/Min: %s"),
299  toString(mKillTCounter / timeDiff).c_str(),
300  toString(CAST_U64(mExpTCounter / timeDiff)).c_str()));
301 
303  // TRANSLATORS: kill stats window label
304  _("Last kill exp:"),
305  strXp.c_str()));
306 
307  recalcStats();
308  update();
309 }
310 
312 {
313  BLOCK_START("KillStats::recalcStats")
314  const time_t curTime = cur_time;
315 
316  // Need Update Exp Counter
317  if (curTime - m1minExpTime > 60)
318  {
319  const int64_t newExp = PlayerInfo::getAttribute(
321  if (m1minExpTime != 0)
322  m1minSpeed = CAST_S32(newExp - m1minExpNum);
323  else
324  m1minSpeed = 0;
325  m1minExpTime = curTime;
326  m1minExpNum = newExp;
327  }
328 
329  if (curTime != 0 && mLastHost == 0xFF6B66 && cur_time > 1)
330  {
331  const int newExp = PlayerInfo::getAttribute(
333  if (m1minExpTime != 0)
334  m1minSpeed = CAST_S32(newExp - m1minExpNum);
335  mStatsReUpdated = true;
336  m1minExpNum = newExp;
337  }
338 
339  if (curTime - m5minExpTime > 60*5)
340  {
341  const int64_t newExp = PlayerInfo::getAttribute(
343  if (m5minExpTime != 0)
344  m5minSpeed = CAST_S32(newExp - m5minExpNum);
345  else
346  m5minSpeed = 0;
347  m5minExpTime = curTime;
348  m5minExpNum = newExp;
349  }
350 
351  if (curTime - m15minExpTime > 60*15)
352  {
353  const int64_t newExp = PlayerInfo::getAttribute(
355  if (m15minExpTime != 0)
356  m15minSpeed = CAST_S32(newExp - m15minExpNum);
357  else
358  m15minSpeed = 0;
359  m15minExpTime = curTime;
360  m15minExpNum = newExp;
361  }
362  BLOCK_END("KillStats::recalcStats")
363 }
364 
366 {
367  BLOCK_START("KillStats::update")
368 
369  mExpSpeed1Label->setCaption(strprintf(ngettext("Exp speed per %d min: %s",
370  "Exp speed per %d min: %s", 1),
371  1,
372  toString(m1minSpeed).c_str()));
373 
374  if (m1minSpeed != 0)
375  {
376  // TRANSLATORS: kill stats window label
377  mExpTime1Label->setCaption(strprintf(_(" Time for next level: %s"),
378  toString(static_cast<float>((PlayerInfo::getAttribute(
381  static_cast<float>(m1minSpeed))).c_str()));
382  }
383  else
384  {
386  // TRANSLATORS: kill stats window label
387  _(" Time for next level: %s"), "?"));
388  }
390 
391  mExpSpeed5Label->setCaption(strprintf(ngettext("Exp speed per %d min: %s",
392  "Exp speed per %d min: %s", 5),
393  5,
394  toString(m5minSpeed / 5).c_str()));
396 
397  if (m5minSpeed != 0)
398  {
399  // TRANSLATORS: kill stats window label
400  mExpTime5Label->setCaption(strprintf(_(" Time for next level: %s"),
401  toString(static_cast<float>((PlayerInfo::getAttribute(
403  Attributes::PLAYER_EXP)) / m5minSpeed * 5)).c_str()));
404  }
405  else
406  {
408  // TRANSLATORS: kill stats window label
409  _(" Time for next level: %s"), "?"));
410  }
412 
413 
414  mExpSpeed15Label->setCaption(strprintf(ngettext("Exp speed per %d min: %s",
415  "Exp speed per %d min: %s", 15), 15, toString(
416  m15minSpeed / 15).c_str()));
418 
419  if (m15minSpeed != 0)
420  {
421  // TRANSLATORS: kill stats window label
422  mExpTime15Label->setCaption(strprintf(_(" Time for next level: %s"),
423  toString(static_cast<float>((PlayerInfo::getAttribute(
425  Attributes::PLAYER_EXP)) / m15minSpeed * 15)).c_str()));
426  }
427  else
428  {
430  // TRANSLATORS: kill stats window label
431  _(" Time for next level: %s"), "?"));
432  }
433 
434  BLOCK_END("KillStats::update")
435 }
436 
438  const int64_t oldVal,
439  const int64_t newVal)
440 {
441  PRAGMA45(GCC diagnostic push)
442  PRAGMA45(GCC diagnostic ignored "-Wswitch-enum")
443  switch (id)
444  {
447  gainXp(newVal - oldVal);
448  break;
450  {
451  const std::string strXpPercent = toString(CAST_U64(
454  mKillCounter = 0;
455  mKillTCounter = 0;
456  mExpCounter = 0;
457  mExpTCounter = 0;
458  mLine3->setCaption(strprintf("1%% = %s exp, avg mob for 1%%: %s",
459  strXpPercent.c_str(),
460  "?"));
462  // TRANSLATORS: kill stats window label
463  _("Kills: %s, total exp: %s"), "?", "?"));
464  // TRANSLATORS: kill stats window label
465  mLine5->setCaption(strprintf(_("Avg Exp: %s"), "?"));
467  // TRANSLATORS: kill stats window label
468  _("No. of avg mob to next level: %s"), "?"));
470  // TRANSLATORS: kill stats window label
471  _("Kills/Min: %s, Exp/Min: %s"), "?", "?"));
472 
473  resetTimes();
474  update();
475  break;
476  }
477  default:
478  break;
479  }
480  PRAGMA45(GCC diagnostic pop)
481 }
Attributes ::T AttributesT
Definition: attributes.h:118
volatile time_t cur_time
Definition: timer.cpp:58
const std::string BUTTON_SKIN
Definition: button.h:89
#define CAST_S32
Definition: cast.h:30
#define CAST_U64
Definition: cast.h:33
Definition: button.h:102
Label * mLine2
Definition: killstats.h:74
Label * mLine1
Definition: killstats.h:73
Label * mExpTime15Label
Definition: killstats.h:86
void resetTimes()
Definition: killstats.cpp:201
void update()
Definition: killstats.cpp:365
time_t mKillTimer
Definition: killstats.h:70
int m5minSpeed
Definition: killstats.h:101
void gainXp(int64_t Xp)
Definition: killstats.cpp:214
Label * mLine6
Definition: killstats.h:78
int64_t mExpCounter
Definition: killstats.h:91
Label * mExpTime5Label
Definition: killstats.h:84
Button * mResetButton
Definition: killstats.h:71
void attributeChanged(const AttributesT id, const int64_t oldVal, const int64_t newVal)
Definition: killstats.cpp:437
int mKillTCounter
Definition: killstats.h:92
int64_t m5minExpNum
Definition: killstats.h:100
int m1minSpeed
Definition: killstats.h:97
time_t m15minExpTime
Definition: killstats.h:103
time_t m5minExpTime
Definition: killstats.h:99
Label * mLastKillExpLabel
Definition: killstats.h:88
time_t m1minExpTime
Definition: killstats.h:95
Button * mTimerButton
Definition: killstats.h:72
Label * mExpSpeed15Label
Definition: killstats.h:85
void action(const ActionEvent &event)
Definition: killstats.cpp:166
Label * mLine4
Definition: killstats.h:76
Label * mLine3
Definition: killstats.h:75
int64_t mExpTCounter
Definition: killstats.h:93
void recalcStats()
Definition: killstats.cpp:311
Label * mExpSpeed5Label
Definition: killstats.h:83
Label * mLine7
Definition: killstats.h:79
Label * mLine5
Definition: killstats.h:77
int64_t m15minExpNum
Definition: killstats.h:104
int64_t m1minExpNum
Definition: killstats.h:96
Label * mExpTime1Label
Definition: killstats.h:82
int mKillCounter
Definition: killstats.h:90
Label * mExpSpeed1Label
Definition: killstats.h:81
int m15minSpeed
Definition: killstats.h:105
Definition: label.h:91
void adjustSize()
Definition: label.cpp:200
void setCaption(const std::string &caption)
Definition: label.cpp:264
LayoutCell & setPadding(int p)
Definition: layoutcell.h:60
int getLevel() const
void registerWindowForReset(Window *const window)
Definition: window.h:102
void setSaveVisible(const bool save)
Definition: window.h:300
void setResizable(const bool resize)
Definition: window.cpp:627
void setWindowName(const std::string &name)
Definition: window.h:355
void enableVisibleSound(bool b)
Definition: window.h:481
void setCloseButton(const bool flag)
Definition: window.cpp:749
void setStickyButtonLock(const bool sticky)
Definition: window.cpp:772
LayoutCell & place(const int x, const int y, Widget *const wg, const int w, const int h)
Definition: window.cpp:1384
void setDefaultSize()
Definition: window.cpp:1198
void loadWindowState()
Definition: window.cpp:1087
#define new
Definition: debug_new.h:147
unsigned int mLastHost
Definition: client.cpp:136
bool mStatsReUpdated
Definition: game.cpp:161
#define _(s)
Definition: gettext.h:35
PRAGMA45(GCC diagnostic push) PRAGMA45(GCC diagnostic ignored "-Wredundant-decls") PRAGMA45(GCC diagnostic pop) class TestMain
KillStats * killStats
Definition: killstats.cpp:45
#define nullptr
Definition: localconsts.h:45
LocalPlayer * localPlayer
const bool Modal_false
Definition: modal.h:30
@ PLAYER_EXP_NEEDED
Definition: attributes.h:37
@ PLAYER_BASE_LEVEL
Definition: attributes.h:31
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
int64_t getAttribute64(const AttributesT id)
Definition: playerinfo.cpp:94
int32_t getAttribute(const AttributesT id)
Definition: playerinfo.cpp:102
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79
SetupWindow * setupWindow
Definition: setupwindow.cpp:64
std::string strprintf(const char *const format,...)