File: results_menu.cpp

package info (click to toggle)
warmux 1%3A11.04.1%2Brepack2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 126,388 kB
  • sloc: cpp: 186,040; xml: 8,909; sh: 3,358; makefile: 1,052; ansic: 713
file content (498 lines) | stat: -rw-r--r-- 15,364 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/******************************************************************************
 *  Warmux is a convivial mass murder game.
 *  Copyright (C) 2001-2011 Warmux Team.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 ******************************************************************************
 * Results menu
 *****************************************************************************/

#include <algorithm>  //std::sort
#include <time.h>

#include <WARMUX_debug.h>

#include "menu/results_menu.h"

#include "character/character.h"
#include "character/damage_stats.h"
#include "game/game_time.h"
#include "graphic/font.h"
#include "graphic/sprite.h"
#include "graphic/text.h"
#include "graphic/video.h"
#include "gui/box.h"
#include "gui/button.h"
#include "gui/figure_widget.h"
#include "gui/file_list_box.h"
#include "gui/graph_canvas.h"
#include "gui/label.h"
#include "gui/null_widget.h"
#include "gui/question.h"
#include "gui/scroll_box.h"
#include "gui/tabs.h"
#include "gui/talk_box.h"
#include "gui/text_box.h"
#include "include/app.h"
#include "include/action_handler.h"
#include "network/network.h"
#include "replay/replay.h"
#include "sound/jukebox.h"
#include "team/results.h"
#include "team/team.h"
#include "tool/ansi_convert.h"
#include "tool/math_tools.h"
#include "tool/resource_manager.h"
#include "tool/string_tools.h"

#define DEF_BORDER      8
#define DEF_SIZE       32
#define LINE_THICKNESS  2

#define GRAPH_BORDER        20
#define GRAPH_START_Y       400

static const Point2i DefSize(DEF_SIZE, DEF_SIZE);

#define REPLAY_ID "TAB_replay"

class ResultBox : public HBox
{
  Label  *category;
  Label  *character;
  Label  *score;
  Widget *img;
  std::string score_str;

  void SetWidgets(const std::string& type, const char* buffer, const Character* player)
  {
    margin = DEF_BORDER;
    border_size = DEF_BORDER;

    Font::font_size_t font = Font::FONT_SMALL;

    category = new Label(type, W_UNDEF, font, Font::FONT_BOLD);
    AddWidget(category);
    character = new Label((player) ? player->GetName() : _("Nobody!"),
                          W_UNDEF, font, Font::FONT_BOLD);
    AddWidget(character);

    score_str = buffer;
    score = new Label(score_str, 30, font, Font::FONT_BOLD);
    AddWidget(score);

    if (player) {
      PictureWidget *team_picture = new PictureWidget(DefSize);
      team_picture->SetSurface(player->GetTeam().GetFlag());
      AddWidget(team_picture);
      img = team_picture;
    } else {
      img = new NullWidget(DefSize);
      AddWidget(img);
    }
  }

public:
  ResultBox(uint size, const std::string& type)
    : HBox(size, false, false, false)
  {
    SetWidgets(type, "?", NULL);
    Widget::SetBackgroundColor(transparent_color);
  }
  ResultBox(uint size, const std::string& type, uint score, const Character* player)
    : HBox(size, false, false, false)
  {
    char buffer[16];
    snprintf(buffer, 16, "%i", score);
    SetWidgets(type, buffer, player);
  }
  ResultBox(uint size, const std::string& type, float score, const Character* player)
    : HBox(size, false, false, false)
  {
    char buffer[16];
    snprintf(buffer, 16, "%.1f", score);
    SetWidgets(type, buffer, player);
  }
  void Pack()
  {
    int width = size.x - (4*margin + img->GetSizeX() + score->GetSizeX());
    category->SetSizeX(width/2);
    character->SetSizeX(width/2);

    HBox::Pack();
  }
};

class ResultListBox : public ScrollBox
{
public:
  ResultListBox(const TeamResults* res, const Point2i &size)
    : ScrollBox(size)
  {
    ResultBox       *box;
    const Character *player = res->getMostViolent();

    //Most violent
    if (player)
      box = new ResultBox(size.x, _("Most violent"), player->GetDamageStats()->GetMostDamage(), player);
    else
      box = new ResultBox(size.x, _("Most violent"));
    AddWidget(box);

    //Most useful
    player = res->getMostUseful();
    if (player)
      box = new ResultBox(size.x, _("Most useful"), player->GetDamageStats()->GetOthersDamage(), player);
    else
      box = new ResultBox(size.x, _("Most useful"));
    AddWidget(box);

    //Most useless
    player = res->getMostUseless();
    if (player)
      box = new ResultBox(size.x, _("Most useless"), player->GetDamageStats()->GetOthersDamage(), player);
    else
      box = new ResultBox(size.x, _("Most useless"));
    AddWidget(box);

    // Biggest sold-out
    player = res->getBiggestTraitor();
    if (player)
      box = new ResultBox(size.x, _("Most sold-out"), player->GetDamageStats()->GetFriendlyFireDamage(), player);
    else
      box = new ResultBox(size.x, _("Most sold-out"));
    AddWidget(box);

    // Most clumsy
    player = res->getMostClumsy();
    if (player)
      box = new ResultBox(size.x, _("Clumsiest"), player->GetDamageStats()->GetItselfDamage(), player);
    else
      box = new ResultBox(size.x, _("Clumsiest"));
    AddWidget(box);

    // Most accurate
    player = res->getMostAccurate();
    if (player)
      box = new ResultBox(size.x, _("Most accurate"), player->GetDamageStats()->GetAccuracy(), player);
    else
      box = new ResultBox(size.x, _("Most accurate"));
    AddWidget(box);
  }
};


//=========================================================

bool compareTeamResults(const TeamResults* a, const TeamResults* b)
{
  if (a->getTeam() == NULL)
    return false;
  if (b->getTeam() == NULL)
    return true;

  const Team* team_a = a->getTeam();
  const Team* team_b = b->getTeam();

  if (team_a->NbAliveCharacter() < team_b->NbAliveCharacter())
    return false;
  else if (team_a->NbAliveCharacter() > team_b->NbAliveCharacter())
    return true;

  // Same number of alive characters, compare left energy
  if (team_a->ReadEnergy() < team_b->ReadEnergy())
    return false;
  else if (team_a->ReadEnergy() > team_b->ReadEnergy())
    return true;

  // Same energy, probably 0, compare death_time
  return (a->GetDeathTime() > b->GetDeathTime());
}

//=========================================================

static bool IsPodiumSeparate()
{
  return GetMainWindow().GetSize() >= Point2i(640, 480);
}

ResultsMenu::ResultsMenu(std::vector<TeamResults*>& v, bool disconnected)
  : Menu("menu/bg_results", vOkCancel)
  , results(v)
  , first_team(NULL)
  , second_team(NULL)
  , third_team(NULL)
  , msg_box(NULL)
  , winner_box(NULL)
  , save(NULL)
{
  Profile *res  = GetResourceManager().LoadXMLProfile("graphism.xml", false);
  Point2i wsize = GetMainWindow().GetSize();
  bool    small = !IsPodiumSeparate();
  uint x        = wsize.GetX() * 0.02f;
  uint tab_x    = small ? x : 260+16+x;
  uint y        = wsize.GetY() * 0.02f;

  // Not showing it yet
  b_cancel->SetVisible(false);

  if (!disconnected)
    ComputeTeamsOrder();

  // Load the podium img
  podium_img = GetResourceManager().LoadImage(res, "menu/podium");
  podium_img.SetAlpha(0, 0);

  Point2i tab_size = wsize - Point2i(tab_x + x, y+actions_buttons->GetSizeY());

  // Are we in network ? yes, so display a talkbox
  if (Network::IsConnected()) {
    int talk_size = tab_size.y/4;
    msg_box = new TalkBox(Point2i(tab_size.x, talk_size), Font::FONT_SMALL, Font::FONT_BOLD);
    tab_size.y -= talk_size;
  }

  tabs = new MultiTabs(tab_size);

  // Podium, drawn as first tab
  if (second_team)
    DrawTeamOnPodium(*second_team, Point2i(20,30));

  if (third_team)
    DrawTeamOnPodium(*third_team, Point2i(98,52));

  winner_box = new VBox(240, true, true, true);
  if (first_team) {
    Font::font_size_t title = (small) ? Font::FONT_MEDIUM : Font::FONT_BIG;
    Font::font_size_t txt   = (small) ? Font::FONT_SMALL : Font::FONT_MEDIUM;
    JukeBox::GetInstance()->Play("default", "victory");

    DrawTeamOnPodium(*first_team, Point2i(60,20));

    winner_box->AddWidget(new Label(_("Winner"), 240, title, Font::FONT_BOLD,
                                    dark_gray_color, Text::ALIGN_CENTER_TOP));
    PictureWidget* winner_logo = new PictureWidget(Point2i(64, 64)/(small+1));
    winner_logo->SetSurface(first_team->GetFlag());
    winner_box->AddWidget(winner_logo);
    winner_box->AddWidget(new Label(first_team->GetName(), 240, title, Font::FONT_BOLD,
                                    dark_gray_color, Text::ALIGN_CENTER_TOP));

    std::string tmp = _("Controlled by: ") + first_team->GetPlayerName();
    winner_box->AddWidget(new Label(tmp, 240, txt, Font::FONT_BOLD,
                                    dark_gray_color, Text::ALIGN_CENTER_TOP));
  }

  podium_img.SetAlpha(SDL_SRCALPHA, 0);
  podium = new PictureWidget(podium_img);
  winner_box->AddWidget(podium);
  if (IsPodiumSeparate()) {
    winner_box->SetPosition(x, y);
    widgets.AddWidget(winner_box);
    tabs->SetPosition(tab_x, y);
  } else {
    tabs->AddNewTab("TAB_podium", _("Podium"), winner_box);
  }

  // Create tabs for each team result
  stats = new MultiTabs(tab_size - 2*DEF_BORDER);
  stats->SetMaxVisibleTabs(1);
  for (uint i=0; i<v.size(); i++) {
    const Team* team = v[i]->getTeam();
    const std::string name = (team) ? team->GetName() : _("All teams");
    stats->AddNewTab(name, name, new ResultListBox(v[i], tab_size - 4*DEF_BORDER));
  }
  tabs->AddNewTab("TAB_team", _("Team stats"), stats);

  // Convert TeamResults to GraphCanvas::Results
  std::vector<GraphCanvas::Result> team_results;
  for (std::vector<TeamResults*>::const_iterator it = results.begin(); it != results.end(); ++it) {
    const Team* team = (*it)->getTeam();
    if (team) {
      const EnergyList& list = team->energy.energy_list;

      uint duration = list.GetDuration();
      if (duration) {
        GraphCanvas::Result r;
        r.ymax = list.GetMaxValue();
        r.xmax = duration*0.001f;
        r.color = team->GetColor();
        r.item = &team->GetFlag();
        for (uint i=0; i<list.size(); i++)
          r.list.push_back(std::make_pair(list[i]->GetDuration()*0.001f, list[i]->GetValue()));
        team_results.push_back(r);
      }
    }
  }
  tabs->AddNewTab("TAB_canvas", _("Team graphs"),
                  new GraphCanvas(tab_size - 2*DEF_BORDER, _("Time"), _("Energy"), team_results));

  // Save replay tab
  if (Replay::GetInstance()->IsRecording()) {
#define BOX_HEIGHT 24
#define LABEL_SIZE 88
#define BORDER      5
    VBox* vbox  = new VBox(tab_size.x-2*BORDER, false, false); // Keep the 5,5 border
    save = new Button(res, "menu/save", false); save->SetBorder(defaultOptionColorRect, 2);
    HBox* hbox  = new HBox(save->GetSizeY(), false, false); hbox->SetNoBorder();
    hbox->AddWidget(save);

    VBox* vbox2 = new VBox(tab_size.x - 3*BORDER - save->GetSizeX(), false, false); vbox2->SetNoBorder();

    HBox* hbox2 = new HBox(BOX_HEIGHT, false, false); hbox2->SetNoBorder();
    hbox2->AddWidget(new Label(_("Filename:"), LABEL_SIZE, Font::FONT_MEDIUM));
    char buffer[32];
    time_t rawtime;
    struct tm * timeinfo;
    time(&rawtime);
    timeinfo = localtime (&rawtime);
    strftime(buffer, sizeof(buffer), "Record %Y-%m-%d %HH%Mm%S.wrf", timeinfo);
    replay_name = new TextBox(buffer, tab_size.x -6*BORDER -LABEL_SIZE -save->GetSizeX());
    hbox2->AddWidget(replay_name);
    vbox2->AddWidget(hbox2);

    hbox2 = new HBox(BOX_HEIGHT, false, false); hbox2->SetNoBorder();
    hbox2->AddWidget(new Label(_("Comment:"), LABEL_SIZE, Font::FONT_MEDIUM));
    comment = new TextBox(_("I like WarMUX :)"), tab_size.x -6*BORDER -LABEL_SIZE -save->GetSizeX());
    hbox2->AddWidget(comment);
    vbox2->AddWidget(hbox2);
    hbox->AddWidget(vbox2);
    vbox->AddWidget(hbox);
    folders = new FileListBox(Point2i(tab_size.x-2*BORDER,
                              tab_size.y -tabs->GetHeaderHeight() -4*BORDER -hbox->GetSizeY()));
    // Windows may use SFN => uppercase extensions
    folders->AddExtensionFilter("WRF");
    folders->AddExtensionFilter("wrf");
    folders->StartListing();
    vbox->AddWidget(folders);
    tabs->AddNewTab(REPLAY_ID, _("Save replay?"), vbox);
  }

  // Final box
  VBox *vbox = new VBox(tab_size.x, false, false, false);
  vbox->SetNoBorder();
  vbox->AddWidget(tabs);

  if (msg_box) {
    vbox->AddWidget(msg_box);
  }
  vbox->SetPosition(tab_x, y);

  widgets.AddWidget(vbox);
  widgets.Pack();
}

void ResultsMenu::ComputeTeamsOrder()
{
  std::sort(results.begin(), results.end(), compareTeamResults);

  first_team = results.at(0)->getTeam();
  second_team = results.at(1)->getTeam();
  if (results.size() > 3)
    third_team = results.at(2)->getTeam();
  else
    third_team = NULL;
}

void ResultsMenu::DrawTeamOnPodium(const Team& team, const Point2i& relative_position)
{
  Point2i flag_pos = team.GetFlag().GetSize()/2;
  Point2i position = relative_position - flag_pos;

  Surface tmp(team.GetFlag());
  podium_img.MergeSurface(tmp, position);
}

bool ResultsMenu::SaveReplay()
{
  std::string filename = replay_name->GetText();
  if (filename.empty() || filename == "") {
    Question question(Question::WARNING);
    question.Set(_("Invalid filename"), true, 0);
    question.Ask();
    return false;
  }

#  ifdef _WIN32
  std::string name = UTF8ToUTF16(folders->GetCurrentFolder(), filename);
#  else
  std::string name = folders->GetCurrentFolder() + filename;
#endif
  if (!Replay::GetInstance()->SaveReplay(name, comment->GetText().c_str())) {
    Question question(Question::WARNING);
    question.Set(_("Failed to save replay"), true, 0);
    question.Ask();
    return false;
  }

  folders->StartListing(folders->GetCurrentFolder().c_str());
  Question question(Question::NO_TYPE);
  question.Set(_("Replay saved"), true, 0);
  question.Ask();

  return true;
}

bool ResultsMenu::signal_ok()
{
  if (tabs->GetCurrentTabId() == REPLAY_ID)
    return SaveReplay();
  return true;
}

void ResultsMenu::key_ok()
{
  // return was pressed while chat texbox still had focus (player wants to send his msg)
  if (msg_box != NULL && msg_box->TextHasFocus()) {
    msg_box->SendChatMsg();
    return;
  }
  Menu::key_ok();
}

void ResultsMenu::Draw(const Point2i &/*mousePosition*/)
{
  if (Network::IsConnected()) {
    ActionHandler * action_handler = ActionHandler::GetInstance();
    action_handler->ExecFrameLessActions();
  }
}

void ResultsMenu::ReceiveMsgCallback(const std::string& msg, const Color& color)
{
  if (msg_box) {
    msg_box->NewMessage(msg, color);
  }
}

void ResultsMenu::OnClickUp(const Point2i &mousePosition, int button)
{
  Widget *w = widgets.ClickUp(mousePosition, button);

  b_cancel->SetVisible(tabs->GetCurrentTabId() == REPLAY_ID);

  // Are we recording?
  if (save) {
    // Are we requested to save?
    if (w == save) {
      SaveReplay();
    } else if (w == folders) {
      const char* file = folders->GetSelectedName();
      // This is a file, use that filename
      if (file) {
        replay_name->SetText(file);
      }
    }
  }
}