HighScoresWidget.C 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (C) 2011 Emweb bvba, Heverlee, Belgium
  3. *
  4. * See the LICENSE file for terms of use.
  5. */
  6. #include <boost/lexical_cast.hpp>
  7. #include <Wt/WText>
  8. #include <Wt/WTable>
  9. #include <Wt/Dbo/Dbo>
  10. #include "HighScoresWidget.h"
  11. #include "Session.h"
  12. using namespace Wt;
  13. HighScoresWidget::HighScoresWidget(Session *session, WContainerWidget *parent):
  14. WContainerWidget(parent),
  15. session_(session)
  16. {
  17. setContentAlignment(AlignCenter);
  18. setStyleClass("highscores");
  19. }
  20. void HighScoresWidget::update()
  21. {
  22. clear();
  23. new WText("<h2>Hall of fame</h2>", this);
  24. int ranking = session_->findRanking();
  25. std::string yourScore;
  26. if (ranking == 1)
  27. yourScore = "Congratulations! You are currently leading the pack.";
  28. else {
  29. yourScore = "You are currently ranked number "
  30. + boost::lexical_cast<std::string>(ranking)
  31. + ". Almost there !";
  32. }
  33. WText *score = new WText("<p>" + yourScore + "</p>", this);
  34. score->addStyleClass("score");
  35. std::vector<User> top = session_->topUsers(20);
  36. WTable *table = new WTable(this);
  37. new WText("Rank", table->elementAt(0, 0));
  38. new WText("User", table->elementAt(0, 1));
  39. new WText("Games", table->elementAt(0, 2));
  40. new WText("Score", table->elementAt(0, 3));
  41. new WText("Last game", table->elementAt(0, 4));
  42. table->setHeaderCount(1);
  43. int formerScore = -1;
  44. int rank = 0;
  45. for (unsigned i = 0; i < top.size(); i++) {
  46. User u = top[i];
  47. if (u.score != formerScore) {
  48. formerScore = u.score;
  49. ++rank;
  50. }
  51. int row = table->rowCount();
  52. new WText(boost::lexical_cast<std::string>(rank),
  53. table->elementAt(row, 0));
  54. new WText(u.name, table->elementAt(row, 1));
  55. new WText(boost::lexical_cast<std::string>(u.gamesPlayed),
  56. table->elementAt(row, 2));
  57. new WText(boost::lexical_cast<std::string>(u.score),
  58. table->elementAt(row, 3));
  59. if (!u.lastGame.isNull())
  60. new WText(u.lastGame.timeTo(WDateTime::currentDateTime())
  61. + " ago", table->elementAt(row, 4));
  62. else
  63. new WText("---", table->elementAt(row, 4));
  64. if (session_->login().loggedIn() && session_->userName() == u.name)
  65. table->rowAt(row)->setId("self");
  66. }
  67. WText *fineprint = new WText(tr("highscore.info"), this);
  68. fineprint->addStyleClass("fineprint");
  69. }