1 /*
2  * file dat_rating.c - level and game statistics
3  *
4  * $Id: dat_rating.c,v 1.5 2006/02/09 21:21:23 fzago Exp $
5  *
6  * Program XBLAST
7  * (C) by Oliver Vogel (e-mail: m.vogel@ndh.net)
8  * Added by Koen De Raedt for central support
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published
12  * by the Free Software Foundation; either version 2; or (at your option)
13  * any later version
14  *
15  * This program is distributed in the hope that it will be entertaining,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILTY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
18  * Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.
22  * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24 
25 #include "xblast.h"
26 
27 /*
28  * local variables
29  */
30 
31 /*
32  * compare stat-data by percent
33  */
34 static int
CompareCentralDataByScore(const void * a,const void * b)35 CompareCentralDataByScore (const void *a, const void *b)
36 {
37 	const XBCentralData *pA = a;
38 	const XBCentralData *pB = b;
39 
40 	assert (pA != NULL);
41 	assert (pB != NULL);
42 	if (pA->score == pB->score) {
43 		return pB->numTotal - pA->numTotal;
44 	}
45 	else if (pA->score > pB->score) {
46 		return -1;
47 	}
48 	else {
49 		return +1;
50 	}
51 }								/* CompareStatDataByScore */
52 
53 XBCentralData *
CreateCentralStat(size_t * num)54 CreateCentralStat (size_t * num)
55 {
56 	XBAtom atom;
57 	CFGPlayerEx player;
58 	XBCentralData *table;
59 	size_t i, j;
60 
61 	assert (NULL != num);
62 
63 	/* alloc data for table */
64 	*num = GetNumPlayerConfigs (CT_Central);
65 	table = calloc (*num, sizeof (XBCentralData));
66 	assert (NULL != table);
67 	/* copy data */
68 	for (i = 0, j = 0; i < *num; i++) {
69 		atom = GetPlayerAtom (CT_Central, i);
70 		if (RetrievePlayerEx (CT_Central, atom, &player)) {
71 			(table + j)->atom = atom;
72 			(table + j)->name = player.name;
73 			(table + j)->score = player.rating.rating;
74 			(table + j)->numTotal = player.rating.gamesPlayed;
75 			(table + j)->numWon = player.rating.realWins;
76 			if (player.rating.gamesPlayed > 0) {
77 				(table + j)->percent = player.rating.realWins;
78 				(table + j)->percent /= player.rating.gamesPlayed;
79 				(table + j)->percent *= 100.0;
80 			}
81 			else {
82 				(table + j)->percent = 0;
83 			}
84 			j++;
85 		}
86 	}
87 	*num = j;
88 	if (*num == 0) {
89 		free (table);
90 		return NULL;
91 	}
92 	/* sort by score */
93 	qsort (table, *num, sizeof (XBCentralData), CompareCentralDataByScore);
94 	for (i = 0; i < *num; i++) {
95 		(table + i)->rank = i + 1;
96 	}
97 	/* that's all */
98 	return table;
99 }
100 
101 XBCentralInfo *
CreateCentralInfo(XBAtom atom,XBCentralData data)102 CreateCentralInfo (XBAtom atom, XBCentralData data)	// TODO pass *centralData (for rating, rank, ..)
103 {
104 	XBCentralInfo *table;
105 	CFGPlayerEx player;
106 	char d[MAX_MENU_INFO][256];
107 	int i;
108 
109 	table = calloc (MAX_MENU_INFO, sizeof (XBCentralInfo));
110 	for (i = 0; i < MAX_MENU_INFO; i++) {
111 		(table + i)->name = "";
112 		(table + i)->value = "";
113 	}
114 	if (RetrievePlayerEx (CT_Central, atom, &player)) {
115 		(table + 0)->name = "Name";
116 		(table + 0)->value = player.name;
117 		sprintf (d[0], "%d", data.rank);
118 		(table + 1)->name = "Rank";
119 		(table + 1)->value = d[0];
120 		sprintf (d[1], "%0.1f", data.score);
121 		(table + 2)->name = "Rating";
122 		(table + 2)->value = d[1];
123 
124 		(table + 3)->name = "Winnings";
125 		if (data.numTotal > 0) {
126 			sprintf (d[2], "%d won of %d (%0.1f%%)", data.numWon, data.numTotal, data.percent);
127 			(table + 3)->value = d[2];
128 		}
129 		else {
130 			(table + 3)->value = "No games played yet";
131 		}
132 
133 		sprintf (d[3], "%d", player.rating.relativeWins);
134 		(table + 4)->name = "Total number of levels won";
135 		(table + 4)->value = d[3];
136 
137 		(table + 6)->name = "Registration date";
138 		sprintf (d[4], "%s", DateString (player.rating.timeRegister));
139 		(table + 6)->value = d[4];
140 		(table + 7)->name = "Last game played";
141 		if (player.rating.timeUpdate > 0) {
142 			sprintf (d[5], "%s", DateString (player.rating.timeUpdate));
143 			(table + 7)->value = d[5];
144 		}
145 		else {
146 			(table + 7)->value = "No games played yet";
147 		}
148 
149 		(table + 9)->name = "Win game message";
150 		(table + 9)->value = player.messages.msgWinGame;
151 		(table + 10)->name = "Win level message";
152 		(table + 10)->value = player.messages.msgWinLevel;
153 		(table + 11)->name = "Lose level message";
154 		(table + 11)->value = player.messages.msgLoseLevel;
155 		(table + 12)->name = "Lose life message";
156 		(table + 12)->value = player.messages.msgLoseLife;
157 		(table + 13)->name = "Welcome message";
158 		(table + 13)->value = player.messages.msgWelcome;
159 		(table + 14)->name = "Gloat";
160 		(table + 14)->value = player.messages.msgGloat;
161 		for (i = 9; i < 15; i++) {
162 			if ((table + i)->value == NULL) {
163 				(table + i)->value = "[None]";
164 			}
165 		}
166 	}
167 	return table;
168 }
169