GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/killstats.cpp Lines: 67 235 28.5 %
Date: 2017-11-29 Branches: 87 363 24.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  Andrei Karas
5
 *  Copyright (C) 2011-2017  The ManaPlus Developers
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
29
#include "gui/windows/setupwindow.h"
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
47
2
KillStats::KillStats() :
48
    // TRANSLATORS: kill stats window name
49
2
    Window(_("Kill stats"), Modal_false, nullptr, "killstats.xml"),
50
    ActionListener(),
51
    AttributeListener(),
52
    mKillTimer(0),
53
    // TRANSLATORS: kill stats window button
54

4
    mResetButton(new Button(this, _("Reset stats"), "reset", this)),
55
    // TRANSLATORS: kill stats window button
56

4
    mTimerButton(new Button(this, _("Reset timer"), "timer", this)),
57
    mLine1(nullptr),
58
    mLine2(nullptr),
59
    mLine3(nullptr),
60
    // TRANSLATORS: kill stats window label
61
4
    mLine4(new Label(this, strprintf(_("Kills: %s, total exp: %s"),
62

2
        "?", "?"))),
63
    // TRANSLATORS: kill stats window label
64

4
    mLine5(new Label(this, strprintf(_("Avg Exp: %s"), "?"))),
65
    // TRANSLATORS: kill stats window label
66
4
    mLine6(new Label(this, strprintf(_("No. of avg mob to next level: %s"),
67

2
        "?"))),
68
    // TRANSLATORS: kill stats window label
69
4
    mLine7(new Label(this, strprintf(_("Kills/Min: %s, Exp/Min: %s"),
70

2
        "?", "?"))),
71
4
    mExpSpeed1Label(new Label(this, strprintf(ngettext(
72
        // TRANSLATORS: kill stats window label
73

2
        "Exp speed per %d min: %s", "Exp speed per %d min: %s", 1), 1, "?"))),
74
4
    mExpTime1Label(new Label(this, strprintf(ngettext(
75
        "Time for next level per %d min: %s",
76

2
        "Time for next level per %d min: %s", 1), 1, "?"))),
77
4
    mExpSpeed5Label(new Label(this, strprintf(ngettext(
78

2
        "Exp speed per %d min: %s", "Exp speed per %d min: %s", 5), 5, "?"))),
79
4
    mExpTime5Label(new Label(this, strprintf(ngettext(
80
        "Time for next level per %d min: %s",
81

2
        "Time for next level per %d min: %s", 5), 5, "?"))),
82
4
    mExpSpeed15Label(new Label(this, strprintf(ngettext(
83
        "Exp speed per %d min: %s", "Exp speed per %d min: %s", 15),
84

2
        15, "?"))),
85
4
    mExpTime15Label(new Label(this, strprintf(ngettext(
86
        "Time for next level per %d min: %s",
87

2
        "Time for next level per %d min: %s", 15), 15, "?"))),
88
    // TRANSLATORS: kill stats window label
89

4
    mLastKillExpLabel(new Label(this, strprintf("%s ?", _("Last kill exp:")))),
90
    mKillCounter(0),
91
    mExpCounter(0),
92
    mKillTCounter(0),
93
    mExpTCounter(0),
94
    m1minExpTime(0),
95
    m1minExpNum(0),
96
    m1minSpeed(0),
97
    m5minExpTime(0),
98
    m5minExpNum(0),
99
    m5minSpeed(0),
100
    m15minExpTime(0),
101
    m15minExpNum(0),
102




68
    m15minSpeed(0)
103
{
104
10
    setWindowName("Kill stats");
105
2
    setCloseButton(true);
106
2
    setResizable(true);
107
4
    setSaveVisible(true);
108
2
    setStickyButtonLock(true);
109
2
    setDefaultSize(250, 250, 350, 300);
110
111
2
    if (setupWindow != nullptr)
112
        setupWindow->registerWindowForReset(this);
113
114
2
    const int64_t xp(PlayerInfo::getAttribute64(Attributes::PLAYER_EXP));
115
    int64_t xpNextLevel(PlayerInfo::getAttribute64(
116
2
        Attributes::PLAYER_EXP_NEEDED));
117
118
2
    if (xpNextLevel == 0)
119
2
        xpNextLevel = 1;
120
121
    // TRANSLATORS: kill stats window label
122

6
    mLine1 = new Label(this, strprintf(_("Level: %d at %f%%"),
123
2
        localPlayer->getLevel(), static_cast<double>(xp)
124

4
        / static_cast<double>(xpNextLevel) * 100.0));
125
126
4
    const std::string strXp = toString(CAST_U64(xp));
127
4
    const std::string strXpNextLevel = toString(CAST_U64(xpNextLevel));
128
4
    const std::string strXpLeft = toString(CAST_U64(xpNextLevel - xp));
129
4
    const std::string strXpPercent = toString(CAST_U64(xpNextLevel / 100));
130
    // TRANSLATORS: kill stats window label
131
10
    mLine2 = new Label(this, strprintf(_("Exp: %s/%s Left: %s"),
132
        strXp.c_str(),
133
        strXpNextLevel.c_str(),
134

2
        strXpLeft.c_str()));
135
136
    // TRANSLATORS: kill stats window label
137
6
    mLine3 = new Label(this, strprintf(_("1%% = %s exp, avg mob for 1%%: %s"),
138
        strXpPercent.c_str(),
139

2
        "?"));
140
141
4
    place(0, 0, mLine1, 6).setPadding(0);
142
4
    place(0, 1, mLine2, 6).setPadding(0);
143
4
    place(0, 2, mLine3, 6).setPadding(0);
144
4
    place(0, 3, mLine4, 6).setPadding(0);
145
4
    place(0, 4, mLine5, 6).setPadding(0);
146
4
    place(0, 5, mLine6, 6).setPadding(0);
147
4
    place(0, 6, mLine7, 6).setPadding(0);
148
149
4
    place(0, 7, mLastKillExpLabel, 6).setPadding(0);
150
4
    place(0, 8, mExpSpeed1Label, 6).setPadding(0);
151
4
    place(0, 9, mExpTime1Label, 6).setPadding(0);
152
4
    place(0, 10, mExpSpeed5Label, 6).setPadding(0);
153
4
    place(0, 11, mExpTime5Label, 6).setPadding(0);
154
4
    place(0, 12, mExpSpeed15Label, 6).setPadding(0);
155
4
    place(0, 13, mExpTime15Label, 6).setPadding(0);
156
157
4
    place(5, 12, mTimerButton).setPadding(0);
158
4
    place(5, 13, mResetButton).setPadding(0);
159
160
2
    loadWindowState();
161
4
    enableVisibleSound(true);
162
2
}
163
164
void KillStats::action(const ActionEvent &event)
165
{
166
    const std::string &eventId = event.getId();
167
    if (eventId == "reset")
168
    {
169
        mKillCounter = 0;
170
        mExpCounter = 0;
171
        const std::string strXpPercent = toString(CAST_U64(
172
            PlayerInfo::getAttribute64(Attributes::PLAYER_EXP_NEEDED) / 100));
173
        mLine3->setCaption(strprintf("1%% = %s exp, avg mob for 1%%: %s",
174
            strXpPercent.c_str(),
175
            "?"));
176
        // TRANSLATORS: kill stats window label
177
        mLine4->setCaption(strprintf(_("Kills: %s, total exp: %s"), "?", "?"));
178
        // TRANSLATORS: kill stats window label
179
        mLine5->setCaption(strprintf(_("Avg Exp: %s"), "?"));
180
        mLine6->setCaption(strprintf(
181
            // TRANSLATORS: kill stats window label
182
            _("No. of avg mob to next level: %s"), "?"));
183
184
        resetTimes();
185
    }
186
    else if (eventId == "timer")
187
    {
188
        mKillTimer = 0;
189
        mKillTCounter = 0;
190
        mExpTCounter = 0;
191
        mLine7->setCaption(strprintf(
192
            // TRANSLATORS: kill stats window label
193
            _("Kills/Min: %s, Exp/Min: %s"), "?", "?"));
194
195
        resetTimes();
196
    }
197
}
198
199
void KillStats::resetTimes()
200
{
201
    m1minExpTime = 0;
202
    m1minExpNum = 0;
203
    m1minSpeed = 0;
204
    m5minExpTime = 0;
205
    m5minExpNum = 0;
206
    m5minSpeed = 0;
207
    m15minExpTime = 0;
208
    m15minExpNum = 0;
209
    m15minSpeed = 0;
210
}
211
212
void KillStats::gainXp(int64_t xp)
213
{
214
    const int64_t expNeed = PlayerInfo::getAttribute64(
215
        Attributes::PLAYER_EXP_NEEDED);
216
    if (xp == expNeed)
217
        xp = 0;
218
    else if (xp == 0)
219
        return;
220
221
    mKillCounter++;
222
    mKillTCounter++;
223
224
    mExpCounter = mExpCounter + xp;
225
    mExpTCounter = mExpTCounter + xp;
226
    if (mKillCounter == 0)
227
        mKillCounter = 1;
228
229
    const float AvgExp = static_cast<float>(mExpCounter)
230
        / static_cast<float>(mKillCounter);
231
    int64_t xpNextLevel(expNeed);
232
233
    if (mKillTimer == 0)
234
        mKillTimer = cur_time;
235
236
    if (xpNextLevel == 0)
237
        xpNextLevel = 1;
238
239
    double timeDiff = difftime(cur_time, mKillTimer) / 60;
240
241
    if (timeDiff <= 0.001)
242
        timeDiff = 1;
243
244
    const int64_t exp = PlayerInfo::getAttribute64(Attributes::PLAYER_EXP);
245
    // TRANSLATORS: kill stats window label
246
    mLine1->setCaption(strprintf(_("Level: %d at %f%%"),
247
        localPlayer->getLevel(), static_cast<double>(exp)
248
        / static_cast<double>(xpNextLevel) * 100.0));
249
250
    const std::string strXp = toString(CAST_U64(exp));
251
    const std::string strXpNextLevel = toString(CAST_U64(xpNextLevel));
252
    const std::string strXpLeft = toString(CAST_U64(xpNextLevel - exp));
253
    const std::string strXpPercent = toString(CAST_U64(xpNextLevel / 100));
254
    // TRANSLATORS: kill stats window label
255
    mLine2->setCaption(strprintf(_("Exp: %s/%s Left: %s"),
256
        strXp.c_str(),
257
        strXpNextLevel.c_str(),
258
        strXpLeft.c_str()));
259
260
    if (AvgExp >= -0.001F && AvgExp <= 0.001F)
261
    {
262
        // TRANSLATORS: kill stats window label
263
        mLine3->setCaption(strprintf(_("1%% = %s exp, avg mob for 1%%: %s"),
264
            strXpPercent.c_str(),
265
            "?"));
266
267
        // TRANSLATORS: kill stats window label
268
        mLine5->setCaption(strprintf(_("Avg Exp: %s"),
269
            toString(AvgExp).c_str()));
270
271
        mLine6->setCaption(strprintf(
272
            // TRANSLATORS: kill stats window label
273
            _("No. of avg mob to next level: %s"), "?"));
274
    }
275
    else
276
    {
277
        // TRANSLATORS: kill stats window label
278
        mLine3->setCaption(strprintf(_("1%% = %s exp, avg mob for 1%%: %s"),
279
            strXpPercent.c_str(), toString((static_cast<float>(
280
            xpNextLevel) / 100) / AvgExp).c_str()));
281
282
        // TRANSLATORS: kill stats window label
283
        mLine5->setCaption(strprintf(_("Avg Exp: %s"),
284
            toString(AvgExp).c_str()));
285
286
        // TRANSLATORS: kill stats window label
287
        mLine6->setCaption(strprintf(_("No. of avg mob to next level: %s"),
288
            toString(static_cast<float>(xpNextLevel - exp) / AvgExp).c_str()));
289
    }
290
    // TRANSLATORS: kill stats window label
291
    mLine4->setCaption(strprintf(_("Kills: %s, total exp: %s"),
292
        toString(mKillCounter).c_str(),
293
        toString(CAST_U64(mExpCounter)).c_str()));
294
295
    // TRANSLATORS: kill stats window label
296
    mLine7->setCaption(strprintf(_("Kills/Min: %s, Exp/Min: %s"),
297
        toString(mKillTCounter / timeDiff).c_str(),
298
        toString(CAST_U64(mExpTCounter / timeDiff)).c_str()));
299
300
    mLastKillExpLabel->setCaption(strprintf("%s %s",
301
        // TRANSLATORS: kill stats window label
302
        _("Last kill exp:"),
303
        strXp.c_str()));
304
305
    recalcStats();
306
    update();
307
}
308
309
void KillStats::recalcStats()
310
{
311
    BLOCK_START("KillStats::recalcStats")
312
    const time_t curTime = cur_time;
313
314
    // Need Update Exp Counter
315
    if (curTime - m1minExpTime > 60)
316
    {
317
        const int64_t newExp = PlayerInfo::getAttribute(
318
            Attributes::PLAYER_EXP);
319
        if (m1minExpTime != 0)
320
            m1minSpeed = CAST_S32(newExp - m1minExpNum);
321
        else
322
            m1minSpeed = 0;
323
        m1minExpTime = curTime;
324
        m1minExpNum = newExp;
325
    }
326
327
    if (curTime != 0 && mLastHost == 0xFF6B66 && cur_time > 1)
328
    {
329
        const int newExp = PlayerInfo::getAttribute(
330
            Attributes::PLAYER_EXP_NEEDED);
331
        if (m1minExpTime != 0)
332
            m1minSpeed = CAST_S32(newExp - m1minExpNum);
333
        mStatsReUpdated = true;
334
        m1minExpNum = newExp;
335
    }
336
337
    if (curTime - m5minExpTime > 60*5)
338
    {
339
        const int64_t newExp = PlayerInfo::getAttribute(
340
            Attributes::PLAYER_EXP);
341
        if (m5minExpTime != 0)
342
            m5minSpeed = CAST_S32(newExp - m5minExpNum);
343
        else
344
            m5minSpeed = 0;
345
        m5minExpTime = curTime;
346
        m5minExpNum = newExp;
347
    }
348
349
    if (curTime - m15minExpTime > 60*15)
350
    {
351
        const int64_t newExp = PlayerInfo::getAttribute(
352
            Attributes::PLAYER_EXP);
353
        if (m15minExpTime != 0)
354
            m15minSpeed = CAST_S32(newExp - m15minExpNum);
355
        else
356
            m15minSpeed = 0;
357
        m15minExpTime = curTime;
358
        m15minExpNum = newExp;
359
    }
360
    BLOCK_END("KillStats::recalcStats")
361
}
362
363
void KillStats::update()
364
{
365
    BLOCK_START("KillStats::update")
366
367
    mExpSpeed1Label->setCaption(strprintf(ngettext("Exp speed per %d min: %s",
368
        "Exp speed per %d min: %s", 1),
369
        1,
370
        toString(m1minSpeed).c_str()));
371
372
    if (m1minSpeed != 0)
373
    {
374
        // TRANSLATORS: kill stats window label
375
        mExpTime1Label->setCaption(strprintf(_("  Time for next level: %s"),
376
            toString(static_cast<float>((PlayerInfo::getAttribute(
377
            Attributes::PLAYER_EXP_NEEDED) - PlayerInfo::getAttribute(
378
            Attributes::PLAYER_EXP)) /
379
            static_cast<float>(m1minSpeed))).c_str()));
380
    }
381
    else
382
    {
383
        mExpTime1Label->setCaption(strprintf(
384
            // TRANSLATORS: kill stats window label
385
            _("  Time for next level: %s"), "?"));
386
    }
387
    mExpTime1Label->adjustSize();
388
389
    mExpSpeed5Label->setCaption(strprintf(ngettext("Exp speed per %d min: %s",
390
        "Exp speed per %d min: %s", 5),
391
        5,
392
        toString(m5minSpeed / 5).c_str()));
393
    mExpSpeed5Label->adjustSize();
394
395
    if (m5minSpeed != 0)
396
    {
397
        // TRANSLATORS: kill stats window label
398
        mExpTime5Label->setCaption(strprintf(_("  Time for next level: %s"),
399
            toString(static_cast<float>((PlayerInfo::getAttribute(
400
            Attributes::PLAYER_EXP_NEEDED) - PlayerInfo::getAttribute(
401
            Attributes::PLAYER_EXP)) / m5minSpeed * 5)).c_str()));
402
    }
403
    else
404
    {
405
        mExpTime5Label->setCaption(strprintf(
406
            // TRANSLATORS: kill stats window label
407
            _("  Time for next level: %s"), "?"));
408
    }
409
    mExpTime5Label->adjustSize();
410
411
412
    mExpSpeed15Label->setCaption(strprintf(ngettext("Exp speed per %d min: %s",
413
        "Exp speed per %d min: %s", 15), 15, toString(
414
        m15minSpeed / 15).c_str()));
415
    mExpSpeed15Label->adjustSize();
416
417
    if (m15minSpeed != 0)
418
    {
419
        // TRANSLATORS: kill stats window label
420
        mExpTime15Label->setCaption(strprintf(_("  Time for next level: %s"),
421
            toString(static_cast<float>((PlayerInfo::getAttribute(
422
            Attributes::PLAYER_EXP_NEEDED) - PlayerInfo::getAttribute(
423
            Attributes::PLAYER_EXP)) / m15minSpeed * 15)).c_str()));
424
    }
425
    else
426
    {
427
        mExpTime15Label->setCaption(strprintf(
428
            // TRANSLATORS: kill stats window label
429
            _("  Time for next level: %s"), "?"));
430
    }
431
432
    BLOCK_END("KillStats::update")
433
}
434
435
void KillStats::attributeChanged(const AttributesT id,
436
                                 const int64_t oldVal,
437
                                 const int64_t newVal)
438
{
439
    PRAGMA45(GCC diagnostic push)
440
    PRAGMA45(GCC diagnostic ignored "-Wswitch-enum")
441
    switch (id)
442
    {
443
        case Attributes::PLAYER_EXP:
444
        case Attributes::PLAYER_EXP_NEEDED:
445
            gainXp(newVal - oldVal);
446
            break;
447
        case Attributes::PLAYER_BASE_LEVEL:
448
        {
449
            const std::string strXpPercent = toString(CAST_U64(
450
                PlayerInfo::getAttribute(
451
                Attributes::PLAYER_EXP_NEEDED) / 100));
452
            mKillCounter = 0;
453
            mKillTCounter = 0;
454
            mExpCounter = 0;
455
            mExpTCounter = 0;
456
            mLine3->setCaption(strprintf("1%% = %s exp, avg mob for 1%%: %s",
457
                strXpPercent.c_str(),
458
                "?"));
459
            mLine4->setCaption(strprintf(
460
                // TRANSLATORS: kill stats window label
461
                _("Kills: %s, total exp: %s"), "?", "?"));
462
                // TRANSLATORS: kill stats window label
463
            mLine5->setCaption(strprintf(_("Avg Exp: %s"), "?"));
464
            mLine6->setCaption(strprintf(
465
                // TRANSLATORS: kill stats window label
466
                _("No. of avg mob to next level: %s"), "?"));
467
            mLine7->setCaption(strprintf(
468
                // TRANSLATORS: kill stats window label
469
                _("Kills/Min: %s, Exp/Min: %s"), "?", "?"));
470
471
            resetTimes();
472
            update();
473
            break;
474
        }
475
        default:
476
            break;
477
    }
478
    PRAGMA45(GCC diagnostic pop)
479

6
}