1 /*
2 * XPilot NG, a multiplayer space war game.
3 *
4 * Copyright (C) TODO
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include "xpserver.h"
22
23 static char teamcup_score_file_name[1024];
24 static FILE *teamcup_score_file = NULL;
25
26 static void teamcup_open_score_file(void);
27 static void teamcup_close_score_file(void);
28
teamcup_check_options(void)29 static void teamcup_check_options(void)
30 {
31 bool ok = true;
32
33 if (!options.teamcup)
34 return;
35
36 if (strlen(options.teamcupName) == 0) {
37 ok = false;
38 warn("Option teamcupName is not set.");
39 }
40 if (strlen(options.teamcupMailAddress) == 0) {
41 ok = false;
42 warn("Option teamcupMailAddress is not set.");
43 }
44 if (strlen(options.teamcupScoreFileNamePrefix) == 0) {
45 ok = false;
46 warn("Option teamcupScoreFileNamePrefix is not set.");
47 }
48 if (!ok) {
49 warn("Teamcup related options must be set in the map file.");
50 exit(1);
51 }
52 }
53
teamcup_init(void)54 void teamcup_init(void)
55 {
56 teamcup_check_options();
57 }
58
teamcup_open_score_file(void)59 static void teamcup_open_score_file(void)
60 {
61 char msg[MSG_LEN];
62 player_t *pl;
63 int i;
64
65
66 if (!options.teamcup)
67 return;
68
69 /* if (teamcup_score_file != NULL) {
70 error("teamcup_score_file != NULL");
71 End_game();
72 */
73
74 snprintf(teamcup_score_file_name, sizeof(teamcup_score_file_name), "%s%d",
75 options.teamcupScoreFileNamePrefix, options.teamcupMatchNumber);
76
77
78 teamcup_score_file = fopen(teamcup_score_file_name, "w");
79 if (teamcup_score_file == NULL) {
80 error("fopen() failed, could not create score file");
81 End_game();
82 }
83
84 snprintf(msg, sizeof(msg),
85 "Score file \"%s\" opened.", teamcup_score_file_name);
86 Set_message_f("%s [*Server notice*]", msg);
87
88 warn("%s\n", msg);
89
90 teamcup_log("1) Fill the names of the teams below.\n"
91 "2) Fill the team number of total winner only if "
92 "this was the second match.\n"
93 "3) Send this file to %s with subject %s-RESULT.\n"
94 "4) Copy this file in a safe place. "
95 "Do not delete it after sending.\n"
96 "\nTeam 2 name: \n"
97 "Team 4 name: \n"
98 "Match: %d\n"
99 "Total winner (team number): \n"
100 "\nDO NOT CHANGE ANYTHING AFTER THIS LINE\n\n",
101 options.teamcupMailAddress, options.teamcupName,
102 options.teamcupMatchNumber
103 );
104
105 teamcup_log("Player present:\n");
106 for (i = 0; i < NumPlayers; i++) {
107 pl = Player_by_index(i);
108 teamcup_log("Team %d: %s\n",pl->team,pl->name);
109 }
110
111 }
112
teamcup_close_score_file(void)113 static void teamcup_close_score_file(void)
114 {
115 char msg[MSG_LEN];
116
117
118 fclose(teamcup_score_file);
119 teamcup_score_file = NULL;
120
121 snprintf(msg, sizeof(msg),
122 "Score file \"%s\" closed.", teamcup_score_file_name);
123 Set_message_f("%s [*Server notice*]", msg);
124 warn("%s\n", msg);
125
126 strcpy(teamcup_score_file_name, "");
127 }
128
teamcup_game_start(void)129 void teamcup_game_start(void)
130 {
131 teamcup_open_score_file();
132 teamcup_round_start();
133 }
134
teamcup_game_over(void)135 void teamcup_game_over(void)
136 {
137
138 if (!options.teamcup || teamcup_score_file == NULL)
139 return;
140
141 teamcup_close_score_file();
142 }
143
teamcup_log(const char * fmt,...)144 void teamcup_log(const char *fmt, ...)
145 {
146 va_list ap;
147
148 if (!options.teamcup)
149 return;
150
151 if (teamcup_score_file == NULL)
152 return;
153
154 va_start(ap, fmt);
155 vfprintf(teamcup_score_file, fmt, ap);
156 va_end(ap);
157 }
158
teamcup_round_start(void)159 void teamcup_round_start(void)
160 {
161 if (!options.teamcup)
162 return;
163
164 teamcup_log("\nRound %d\n", roundsPlayed + 1);
165 }
166
teamcup_round_end(int winning_team)167 void teamcup_round_end(int winning_team)
168 {
169 int i, j, *list, best, team_players[MAX_TEAMS], best_team;
170 double team_score[MAX_TEAMS];
171 double best_score = -FLT_MAX;
172 double best_team_score = -FLT_MAX;
173 double double_max = FLT_MAX;
174 player_t *pl;
175
176 if (!options.teamcup)
177 return;
178
179 list = XMALLOC(int, NumPlayers);
180 if (list == NULL) {
181 error("Can't allocate memory for list");
182 End_game();
183 }
184
185 for (i = 0; i < NumPlayers; i++)
186 list[i] = i;
187
188 for (i = 0; i < MAX_TEAMS; i++)
189 team_score[i] = double_max;
190
191 for (i = 0; i < MAX_TEAMS; i++)
192 team_players[i] = 0;
193
194 for (i = 0; i < NumPlayers; i++) {
195 best = NumPlayers;
196 for (j = 0; j < NumPlayers; j++) {
197 if (list[j] == NumPlayers)
198 continue;
199
200 pl = Player_by_index(j);
201 if (best == NumPlayers || Get_Score(pl) > best_score) {
202 best_score = Get_Score(pl);
203 best = j;
204 }
205 }
206
207 list[best] = NumPlayers;
208 pl = Player_by_index(best);
209 teamcup_log("%d\t%.0f\t%2d/%d\t%s\n", pl->team, Get_Score(pl),
210 pl->kills, pl->deaths, pl->name);
211
212 if (team_score[pl->team] == double_max)
213 team_score[pl->team] = 0.0;
214 team_score[pl->team] += Get_Score(pl);
215 team_players[pl->team]++;
216 }
217
218 for (i = 0; i < MAX_TEAMS; i++) {
219 if (team_score[i] != double_max){
220 teamcup_log("Team %d\t%d\n", i, (int)(team_score[i]));
221 if(team_score[i]>best_team_score){
222 best_team_score=team_score[i];
223 best_team=i;
224 }
225 }
226 }
227 teamcup_log("Advantage Team %d\n",best_team);
228 if (teamcup_score_file != NULL)
229 fflush(teamcup_score_file);
230
231 free(list);
232 }
233