]> jfr.im git - irc/gameservirc.git/blob - gameserv/toplist.cpp
Added messages.cpp and find.cpp, which contain functions from gameserv.cpp
[irc/gameservirc.git] / gameserv / toplist.cpp
1 #include "toplist.h"
2 #include "player.h"
3 #include <list>
4
5 using namespace std;
6
7 toplist::toplist()
8 {
9 myList.clear();
10 count = 10;
11 }
12
13 toplist::toplist(int c)
14 {
15 myList.clear();
16 count = c;
17 }
18
19 toplist::~toplist()
20 {
21 myList.clear();
22 count = 10;
23 }
24
25 void toplist::insertPlayer(Player *p)
26 {
27 PlayerWrapper *pw;
28 pw = new PlayerWrapper(p);
29 myList.push_front(*pw);
30 sort();
31
32 // Reverse the list since list::sort() uses the < operator
33 reverse();
34 prune();
35 }
36
37 void toplist::setCount(int c)
38 {
39 count = c;
40 }
41
42 void toplist::sort()
43 {
44 myList.sort();
45 }
46
47 void toplist::reverse()
48 {
49 myList.reverse();
50 }
51 void toplist::prune()
52 {
53 list<PlayerWrapper>::iterator it;
54 it = myList.begin();
55
56 for (int x = 0; it != myList.end(); x++, it++)
57 {
58 if (x > count)
59 {
60 // Delete this player from the top list, because they're over the count
61 myList.erase(it);
62 }
63 }
64 }
65
66 list<PlayerWrapper>::iterator toplist::begin()
67 {
68 return myList.begin();
69 }
70
71
72 list<PlayerWrapper>::iterator toplist::end()
73 {
74 return myList.end();
75 }
76
77 bool toplist::empty()
78 {
79 return myList.empty();
80 }
81
82 bool PlayerWrapper::operator < (PlayerWrapper &right)
83 {
84 return (*p < *right.p);
85 }
86
87 PlayerWrapper::PlayerWrapper()
88 {
89 p = NULL;
90 }
91
92 PlayerWrapper::PlayerWrapper(Player *pl)
93 {
94 p = pl;
95 }
96
97 PlayerWrapper::~PlayerWrapper()
98 {
99 p = NULL;
100 }
101
102 void PlayerWrapper::setPlayer(Player *pl)
103 {
104 p = pl;
105 }
106