| 1 |
/************************************************************************ |
| 2 |
* serverside.c Handles the server side of dopewars * |
| 3 |
* Copyright (C) 1998-2022 Ben Webb * |
| 4 |
* Email: [email protected] * |
| 5 |
* WWW: https://dopewars.sourceforge.io/ * |
| 6 |
* * |
| 7 |
* This program is free software; you can redistribute it and/or * |
| 8 |
* modify it under the terms of the GNU General Public License * |
| 9 |
* as published by the Free Software Foundation; either version 2 * |
| 10 |
* of the License, or (at your option) any later version. * |
| 11 |
* * |
| 12 |
* This program is distributed in the hope that it will be useful, * |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 |
* GNU General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU General Public License * |
| 18 |
* along with this program; if not, write to the Free Software * |
| 19 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, * |
| 20 |
* MA 02111-1307, USA. * |
| 21 |
************************************************************************/ |
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <stdio.h> |
| 28 |
#include <string.h> |
| 29 |
#include <sys/types.h> /* For size_t etc. */ |
| 30 |
#include <sys/stat.h> |
| 31 |
|
| 32 |
#ifdef CYGWIN |
| 33 |
#include <winsock2.h> /* For network functions */ |
| 34 |
#include <windows.h> /* For datatypes such as BOOL */ |
| 35 |
#include <process.h> /* For getpid */ |
| 36 |
#else |
| 37 |
#include <sys/socket.h> /* For struct sockaddr etc. */ |
| 38 |
#include <netinet/in.h> /* For struct sockaddr_in etc. */ |
| 39 |
#include <sys/un.h> /* For struct sockaddr_un */ |
| 40 |
#include <arpa/inet.h> /* For socklen_t */ |
| 41 |
#endif /* CYGWIN */ |
| 42 |
|
| 43 |
#ifdef HAVE_UNISTD_H |
| 44 |
#include <unistd.h> |
| 45 |
#endif |
| 46 |
#include <signal.h> |
| 47 |
#include <errno.h> |
| 48 |
#include <stdlib.h> |
| 49 |
#include <glib.h> |
| 50 |
#include "configfile.h" /* For UpdateConfigFile */ |
| 51 |
#include "dopewars.h" |
| 52 |
#include "log.h" |
| 53 |
#include "message.h" |
| 54 |
#include "network.h" |
| 55 |
#include "nls.h" |
| 56 |
#include "serverside.h" |
| 57 |
#include "tstring.h" |
| 58 |
#include "util.h" |
| 59 |
|
| 60 |
#ifdef GUI_SERVER |
| 61 |
#include "gtkport/gtkport.h" |
| 62 |
#endif |
| 63 |
|
| 64 |
static const price_t MINTRENCHPRICE = 200, MAXTRENCHPRICE = 300; |
| 65 |
|
| 66 |
#define ESCAPE 0 |
| 67 |
#define DEFECT 1 |
| 68 |
#define SHOT 2 |
| 69 |
#define NUMDISCOVER 3 |
| 70 |
char *Discover[NUMDISCOVER] = { |
| 71 |
/* Things that can "happen" to your spies - look for strings containing |
| 72 |
"The spy %s!" to see how these strings are used. */ |
| 73 |
N_("escaped"), N_("defected"), N_("was shot") |
| 74 |
}; |
| 75 |
|
| 76 |
/* The two keys that are valid answers to the Attack/Evade question. If |
| 77 |
you wish to translate them, do so in the same order as they given here. |
| 78 |
You will also need to translate the answers given by the clients. */ |
| 79 |
static char *attackquestiontr = N_("AE"); |
| 80 |
|
| 81 |
/* If we haven't talked to the metaserver for 3 hours, then remind it that |
| 82 |
* we still exist, so we don't get wiped from the list of active servers */ |
| 83 |
#define METAUPDATETIME (10800) |
| 84 |
|
| 85 |
/* Don't report players logging in/out to the metaserver more frequently |
| 86 |
* than once every minute (so as not to overload the metaserver, or slow |
| 87 |
* down our own server). */ |
| 88 |
#define METAMINTIME (60) |
| 89 |
|
| 90 |
int TerminateRequest, ReregisterRequest, RelogRequest; |
| 91 |
|
| 92 |
int MetaUpdateTimeout; |
| 93 |
long MetaMinTimeout; |
| 94 |
gboolean WantQuit = FALSE; |
| 95 |
|
| 96 |
#ifdef CYGWIN |
| 97 |
static SERVICE_STATUS_HANDLE scHandle; |
| 98 |
#endif |
| 99 |
|
| 100 |
#ifdef GUI_SERVER |
| 101 |
static gboolean glib_timeout(gpointer userp); |
| 102 |
static gboolean glib_socket(GIOChannel *ch, GIOCondition condition, |
| 103 |
gpointer data); |
| 104 |
#endif |
| 105 |
|
| 106 |
|
| 107 |
/* Do we want to update the player details on the metaserver when the |
| 108 |
* timeout expires? */ |
| 109 |
gboolean MetaPlayerPending = FALSE; |
| 110 |
|
| 111 |
GSList *FirstServer = NULL; |
| 112 |
|
| 113 |
#ifdef NETWORKING |
| 114 |
/* Data waiting to be sent to/read from the metaserver */ |
| 115 |
static CurlConnection MetaConn; |
| 116 |
|
| 117 |
static GScanner *Scanner; |
| 118 |
|
| 119 |
#endif |
| 120 |
|
| 121 |
/* Handle to the high score file */ |
| 122 |
static FILE *ScoreFP = NULL; |
| 123 |
|
| 124 |
/* Pointer to the filename of a pid file (if non-NULL) */ |
| 125 |
char *PidFile = NULL; |
| 126 |
|
| 127 |
static char HelpText[] = { |
| 128 |
/* Help on various general server commands */ |
| 129 |
N_("dopewars server version %s commands and settings\n\n" |
| 130 |
"help Displays this help screen\n" |
| 131 |
"list Lists all players logged on\n" |
| 132 |
"push <player> Politely asks the named player to leave\n" |
| 133 |
"kill <player> Abruptly breaks the connection with the " |
| 134 |
"named player\n" |
| 135 |
"msg:<mesg> Send message to all players\n" |
| 136 |
"save <file> Save current configuration to the named file\n" |
| 137 |
"quit Gracefully quit, after notifying all players\n" |
| 138 |
"<variable>=<value> Sets the named variable to the given value\n" |
| 139 |
"<variable> Displays the value of the named variable\n" |
| 140 |
"<list>[x].<var>=<value> Sets the named variable in the given list,\n" |
| 141 |
" index x, to the given value\n" |
| 142 |
"<list>[x].<var> Displays the value of the named list variable\n" |
| 143 |
"\nValid variables are listed below:-\n\n") |
| 144 |
}; |
| 145 |
|
| 146 |
typedef enum _OfferForce { |
| 147 |
NOFORCE, FORCECOPS, FORCEBITCH |
| 148 |
} OfferForce; |
| 149 |
|
| 150 |
int SendSingleHighScore(Player *Play, struct HISCORE *Score, |
| 151 |
int ind, gboolean Bold); |
| 152 |
static int SendCopOffer(Player *To, OfferForce Force); |
| 153 |
static int OfferObject(Player *To, gboolean ForceBitch); |
| 154 |
static gboolean HighScoreWrite(FILE *fp, struct HISCORE *MultiScore, |
| 155 |
struct HISCORE *AntiqueScore); |
| 156 |
|
| 157 |
#ifdef NETWORKING |
| 158 |
static void MetaConnectError(CurlConnection *conn, GError *err) |
| 159 |
{ |
| 160 |
dopelog(1, LF_SERVER, _("Failed to connect to metaserver at %s (%s)"), |
| 161 |
MetaServer.URL, err->message); |
| 162 |
} |
| 163 |
|
| 164 |
void log_meta_headers(gpointer data, gpointer user_data) |
| 165 |
{ |
| 166 |
char *header = data; |
| 167 |
if (*header) |
| 168 |
dopelog(4, LF_SERVER, _("MetaServer: %s"), header); |
| 169 |
} |
| 170 |
#endif |
| 171 |
|
| 172 |
/* |
| 173 |
* Sends server details to the metaserver, if specified. If "Up" is |
| 174 |
* TRUE, informs the metaserver that the server is now accepting |
| 175 |
* connections - otherwise tells the metaserver that this server is |
| 176 |
* about to go down. If "SendData" is TRUE, then also sends game |
| 177 |
* data (e.g. scores) to the metaserver. If "RespectTimeout" is TRUE |
| 178 |
* then the update is delayed if a previous update happened too |
| 179 |
* recently. If networking is disabled, this function does nothing. |
| 180 |
*/ |
| 181 |
void RegisterWithMetaServer(gboolean Up, gboolean SendData, |
| 182 |
gboolean RespectTimeout) |
| 183 |
{ |
| 184 |
#ifdef NETWORKING |
| 185 |
struct HISCORE MultiScore[NUMHISCORE], AntiqueScore[NUMHISCORE]; |
| 186 |
GString *body; |
| 187 |
gchar *prstr; |
| 188 |
gboolean ret; |
| 189 |
GError *tmp_error = NULL; |
| 190 |
int i; |
| 191 |
|
| 192 |
if (!MetaServer.Active || WantQuit || !Server) { |
| 193 |
return; |
| 194 |
} |
| 195 |
|
| 196 |
if (MetaMinTimeout > time(NULL) && RespectTimeout) { |
| 197 |
dopelog(3, LF_SERVER, |
| 198 |
_("Attempt to connect to metaserver too frequently " |
| 199 |
"- waiting for next timeout")); |
| 200 |
MetaPlayerPending = TRUE; |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
body = g_string_new(""); |
| 205 |
|
| 206 |
g_string_assign(body, "output=text&"); |
| 207 |
|
| 208 |
g_string_append_printf(body, "up=%d&port=%d&version=", Up ? 1 : 0, Port); |
| 209 |
AddURLEnc(body, VERSION); |
| 210 |
g_string_append_printf(body, "&players=%d&maxplay=%d&comment=", |
| 211 |
CountPlayers(FirstServer), MaxClients); |
| 212 |
AddURLEnc(body, MetaServer.Comment); |
| 213 |
|
| 214 |
if (MetaServer.LocalName[0]) { |
| 215 |
g_string_append(body, "&hostname="); |
| 216 |
AddURLEnc(body, MetaServer.LocalName); |
| 217 |
} |
| 218 |
if (MetaServer.Password[0]) { |
| 219 |
g_string_append(body, "&password="); |
| 220 |
AddURLEnc(body, MetaServer.Password); |
| 221 |
} |
| 222 |
|
| 223 |
if (SendData && HighScoreRead(ScoreFP, MultiScore, AntiqueScore, TRUE)) { |
| 224 |
for (i = 0; i < NUMHISCORE; i++) { |
| 225 |
if (MultiScore[i].Name && MultiScore[i].Name[0]) { |
| 226 |
g_string_append_printf(body, "&nm[%d]=", i); |
| 227 |
AddURLEnc(body, MultiScore[i].Name); |
| 228 |
g_string_append_printf(body, "&dt[%d]=", i); |
| 229 |
AddURLEnc(body, MultiScore[i].Time); |
| 230 |
g_string_append_printf(body, "&st[%d]=%s&sc[%d]=", i, |
| 231 |
MultiScore[i].Dead ? "dead" : "alive", i); |
| 232 |
AddURLEnc(body, prstr = FormatPrice(MultiScore[i].Money)); |
| 233 |
g_free(prstr); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
ret = OpenCurlConnection(&MetaConn, MetaServer.URL, body->str, &tmp_error); |
| 239 |
|
| 240 |
dopelog(2, LF_SERVER, _("Waiting for connect to metaserver at %s..."), |
| 241 |
MetaServer.URL); |
| 242 |
g_string_free(body, TRUE); |
| 243 |
if (!ret) { |
| 244 |
MetaConnectError(&MetaConn, tmp_error); |
| 245 |
g_error_free(tmp_error); |
| 246 |
} |
| 247 |
MetaPlayerPending = FALSE; |
| 248 |
|
| 249 |
MetaUpdateTimeout = time(NULL) + METAUPDATETIME; |
| 250 |
MetaMinTimeout = time(NULL) + METAMINTIME; |
| 251 |
#endif /* NETWORKING */ |
| 252 |
} |
| 253 |
|
| 254 |
#ifdef NETWORKING |
| 255 |
void HandleServerPlayer(Player *Play) |
| 256 |
{ |
| 257 |
gchar *buf; |
| 258 |
gboolean MessageRead = FALSE; |
| 259 |
|
| 260 |
while ((buf = GetWaitingPlayerMessage(Play)) != NULL) { |
| 261 |
MessageRead = TRUE; |
| 262 |
HandleServerMessage(buf, Play); |
| 263 |
g_free(buf); |
| 264 |
} |
| 265 |
/* Reset the idle timeout (if necessary) */ |
| 266 |
if (MessageRead && IdleTimeout) { |
| 267 |
Play->IdleTimeout = time(NULL) + (time_t) IdleTimeout; |
| 268 |
} |
| 269 |
} |
| 270 |
#endif /* NETWORKING */ |
| 271 |
|
| 272 |
/* |
| 273 |
* Sends details (name, ID) about player "Play" to player "To", using |
| 274 |
* message code "Code". |
| 275 |
*/ |
| 276 |
void SendPlayerDetails(Player *Play, Player *To, MsgCode Code) |
| 277 |
{ |
| 278 |
GString *text; |
| 279 |
|
| 280 |
text = g_string_new(GetPlayerName(Play)); |
| 281 |
if (HaveAbility(To, A_PLAYERID)) { |
| 282 |
g_string_append_printf(text, "^%d", Play->ID); |
| 283 |
} |
| 284 |
SendServerMessage(NULL, C_NONE, Code, To, text->str); |
| 285 |
g_string_free(text, TRUE); |
| 286 |
} |
| 287 |
|
| 288 |
/* |
| 289 |
* Checks the version of the client that has connected, and sends a |
| 290 |
* warning message if it's old. |
| 291 |
*/ |
| 292 |
void RemoteVersionCheck(Player *Play) |
| 293 |
{ |
| 294 |
/* Client didn't send a C_ABILITIES message at all, so is either broken |
| 295 |
* or is version 1.4.8 or earlier. */ |
| 296 |
if (Play->Abil.RemoteNum == 0) { |
| 297 |
SendPrintMessage(NULL, C_VERSIONCHECK, Play, |
| 298 |
_("You appear to be using an extremely old (version 1.4.x) client.^" |
| 299 |
"While this will probably work, many of the newer features^" |
| 300 |
"will be unsupported. Get the latest version from the^" |
| 301 |
"dopewars website, https://dopewars.sourceforge.io/.")); |
| 302 |
|
| 303 |
/* The client has a smaller value of A_NUM; this means that not only does |
| 304 |
* it not support some features, it doesn't even know they might exist. */ |
| 305 |
} else if (Play->Abil.RemoteNum < A_NUM) { |
| 306 |
SendPrintMessage(NULL, C_VERSIONCHECK, Play, |
| 307 |
_("Warning: your client is too old to support all of this^" |
| 308 |
"server's features. For the full \"experience\", get^" |
| 309 |
"the latest version of dopewars from the^" |
| 310 |
"website, https://dopewars.sourceforge.io/.")); |
| 311 |
} |
| 312 |
|
| 313 |
/* Otherwise, the client is either the same version as the server, or |
| 314 |
* it's newer. Both should be OK, so do nothing. */ |
| 315 |
} |
| 316 |
|
| 317 |
/* |
| 318 |
* Given a message "buf", from player "Play", performs processing and |
| 319 |
* sends suitable replies. |
| 320 |
*/ |
| 321 |
void HandleServerMessage(gchar *buf, Player *Play) |
| 322 |
{ |
| 323 |
Player *To, *tmp, *pt; |
| 324 |
GSList *list; |
| 325 |
char *Data; |
| 326 |
AICode AI; |
| 327 |
MsgCode Code; |
| 328 |
gchar *text; |
| 329 |
DopeEntry NewEntry; |
| 330 |
int i; |
| 331 |
price_t money; |
| 332 |
|
| 333 |
if (ProcessMessage(buf, Play, &To, &AI, &Code, &Data, FirstServer) == -1) { |
| 334 |
g_warning("Bad message"); |
| 335 |
return; |
| 336 |
} |
| 337 |
switch (Code) { |
| 338 |
case C_MSGTO: |
| 339 |
if (Network) { |
| 340 |
dopelog(3, LF_SERVER, "%s->%s: %s", GetPlayerName(Play), |
| 341 |
GetPlayerName(To), Data); |
| 342 |
} |
| 343 |
SendServerMessage(Play, AI, Code, To, Data); |
| 344 |
break; |
| 345 |
case C_ABILITIES: |
| 346 |
ReceiveAbilities(Play, Data); |
| 347 |
break; |
| 348 |
case C_NAME: |
| 349 |
StripTerminators(Data); |
| 350 |
pt = GetPlayerByName(Data, FirstServer); |
| 351 |
if (pt && pt != Play) { |
| 352 |
if (ConnectTimeout) { |
| 353 |
Play->ConnectTimeout = time(NULL) + (time_t) ConnectTimeout; |
| 354 |
} |
| 355 |
SendServerMessage(NULL, C_NONE, C_NEWNAME, Play, NULL); |
| 356 |
} else if (strlen(GetPlayerName(Play)) == 0 && Data[0]) { |
| 357 |
if (CountPlayers(FirstServer) < MaxClients || !Network) { |
| 358 |
RemoteVersionCheck(Play); |
| 359 |
SendAbilities(Play); |
| 360 |
CombineAbilities(Play); |
| 361 |
SendInitialData(Play); |
| 362 |
SendMiscData(Play); |
| 363 |
SetPlayerName(Play, Data); |
| 364 |
for (list = FirstServer; list; list = g_slist_next(list)) { |
| 365 |
pt = (Player *)list->data; |
| 366 |
if (pt != Play && IsConnectedPlayer(pt) && !IsCop(pt)) { |
| 367 |
SendPlayerDetails(pt, Play, C_LIST); |
| 368 |
} |
| 369 |
} |
| 370 |
if (ServerMOTD && ServerMOTD[0]) { |
| 371 |
SendPrintMessage(NULL, C_MOTD, Play, ServerMOTD); |
| 372 |
} |
| 373 |
SendServerMessage(NULL, C_NONE, C_ENDLIST, Play, NULL); |
| 374 |
RegisterWithMetaServer(TRUE, FALSE, TRUE); |
| 375 |
Play->ConnectTimeout = 0; |
| 376 |
|
| 377 |
if (Network) { |
| 378 |
dopelog(2, LF_SERVER, _("%s joins the game!"), GetPlayerName(Play)); |
| 379 |
} |
| 380 |
for (list = FirstServer; list; list = g_slist_next(list)) { |
| 381 |
pt = (Player *)list->data; |
| 382 |
if (IsConnectedPlayer(pt) && pt != Play) { |
| 383 |
SendPlayerDetails(Play, pt, C_JOIN); |
| 384 |
} |
| 385 |
} |
| 386 |
Play->EventNum = E_ARRIVE; |
| 387 |
SendPlayerData(Play); |
| 388 |
SendEvent(Play); |
| 389 |
} else { |
| 390 |
/* Message displayed in the server when too many players try to |
| 391 |
* connect */ |
| 392 |
dopelog(2, LF_SERVER, |
| 393 |
_("MaxClients (%d) exceeded - dropping connection"), |
| 394 |
MaxClients); |
| 395 |
if (MaxClients == 1) { |
| 396 |
text = g_strdup_printf( |
| 397 |
/* Message sent to a player if the |
| 398 |
server is full */ |
| 399 |
_("Sorry, but this server has a limit of " |
| 400 |
"1 player, which has been reached.^" |
| 401 |
"Please try connecting again later.")); |
| 402 |
} else { |
| 403 |
text = g_strdup_printf( |
| 404 |
/* Message sent to a player if the |
| 405 |
server is full */ |
| 406 |
_("Sorry, but this server has a limit of " |
| 407 |
"%d players, which has been reached.^" |
| 408 |
"Please try connecting again later."), |
| 409 |
MaxClients); |
| 410 |
} |
| 411 |
SendServerMessage(NULL, C_NONE, C_PRINTMESSAGE, Play, text); |
| 412 |
g_free(text); |
| 413 |
/* Make sure they do actually disconnect, eventually! */ |
| 414 |
if (ConnectTimeout) { |
| 415 |
Play->ConnectTimeout = time(NULL) + (time_t) ConnectTimeout; |
| 416 |
} |
| 417 |
} |
| 418 |
} else { |
| 419 |
/* A player changed their name during the game (unusual, and not |
| 420 |
really properly supported anyway) - notify all players of the |
| 421 |
change */ |
| 422 |
dopelog(2, LF_SERVER, _("%s will now be known as %s"), |
| 423 |
GetPlayerName(Play), Data); |
| 424 |
BroadcastToClients(C_NONE, C_RENAME, Data, Play, Play); |
| 425 |
SetPlayerName(Play, Data); |
| 426 |
} |
| 427 |
break; |
| 428 |
case C_WANTQUIT: |
| 429 |
if (Play->EventNum != E_FINISH) { |
| 430 |
FinishGame(Play, NULL); |
| 431 |
} |
| 432 |
break; |
| 433 |
case C_REQUESTJET: |
| 434 |
i = atoi(Data); |
| 435 |
/* Make sure value is within range */ |
| 436 |
if (i < 0 || i >= NumLocation) { |
| 437 |
dopelog(3, LF_SERVER, _("%s: DENIED jet to invalid location %s"), |
| 438 |
GetPlayerName(Play), Data); |
| 439 |
break; |
| 440 |
} |
| 441 |
if (Play->EventNum == E_FIGHT || Play->EventNum == E_FIGHTASK) { |
| 442 |
if (CanRunHere(Play)) { |
| 443 |
break; |
| 444 |
} else { |
| 445 |
RunFromCombat(Play, i); |
| 446 |
} |
| 447 |
if (Play->EventNum == E_WAITDONE) { |
| 448 |
Play->EventNum = Play->ResyncNum; |
| 449 |
SendEvent(Play); |
| 450 |
} |
| 451 |
} |
| 452 |
if (NumTurns > 0 && Play->Turn >= NumTurns |
| 453 |
&& Play->EventNum != E_FINISH) { |
| 454 |
/* Message displayed when a player reaches their maximum number of |
| 455 |
turns */ |
| 456 |
FinishGame(Play, _("Your dealing time is up...")); |
| 457 |
} else if (i != Play->IsAt && (NumTurns == 0 || Play->Turn < NumTurns) |
| 458 |
&& Play->EventNum == E_NONE && Play->Health > 0) { |
| 459 |
dopelog(4, LF_SERVER, "%s jets to %s", |
| 460 |
GetPlayerName(Play), Location[i].Name); |
| 461 |
Play->IsAt = i; |
| 462 |
Play->Turn++; |
| 463 |
g_date_add_days(Play->date, 1); |
| 464 |
Play->Debt = Play->Debt * (DebtInterest + 100) / 100; |
| 465 |
Play->Debt = MAX(Play->Debt, 0); |
| 466 |
Play->Bank = Play->Bank * (BankInterest + 100) / 100; |
| 467 |
Play->Bank = MAX(Play->Bank, 0); |
| 468 |
SendPlayerData(Play); |
| 469 |
Play->EventNum = E_SUBWAY; |
| 470 |
SendEvent(Play); |
| 471 |
} else { |
| 472 |
/* A player has tried to jet to a new location, but we don't allow |
| 473 |
them to. (e.g. they're still fighting someone, or they're |
| 474 |
supposed to be dead) */ |
| 475 |
dopelog(3, LF_SERVER, _("%s: DENIED jet to %s"), |
| 476 |
GetPlayerName(Play), Location[i].Name); |
| 477 |
} |
| 478 |
break; |
| 479 |
case C_REQUESTSCORE: |
| 480 |
SendHighScores(Play, FALSE, NULL); |
| 481 |
break; |
| 482 |
case C_CONTACTSPY: |
| 483 |
for (list = FirstServer; list; list = g_slist_next(list)) { |
| 484 |
tmp = (Player *)list->data; |
| 485 |
i = GetListEntry(&(tmp->SpyList), Play); |
| 486 |
if (tmp != Play && i >= 0 && tmp->SpyList.Data[i].Turns >= 0) { |
| 487 |
SendSpyReport(Play, tmp); |
| 488 |
} |
| 489 |
} |
| 490 |
break; |
| 491 |
case C_DEPOSIT: |
| 492 |
money = strtoprice(Data); |
| 493 |
if (Play->EventNum == E_BANK && Play->Bank + money >= 0 |
| 494 |
&& Play->Cash - money >= 0) { |
| 495 |
Play->Bank += money; |
| 496 |
Play->Cash -= money; |
| 497 |
SendPlayerData(Play); |
| 498 |
} |
| 499 |
break; |
| 500 |
case C_PAYLOAN: |
| 501 |
money = strtoprice(Data); |
| 502 |
if (Play->EventNum == E_LOANSHARK && money > 0 |
| 503 |
&& Play->Debt - money >= 0 && Play->Cash - money >= 0) { |
| 504 |
Play->Debt -= money; |
| 505 |
Play->Cash -= money; |
| 506 |
SendPlayerData(Play); |
| 507 |
} |
| 508 |
break; |
| 509 |
case C_BUYOBJECT: |
| 510 |
BuyObject(Play, Data); |
| 511 |
break; |
| 512 |
case C_FIGHTACT: |
| 513 |
if (Data[0] == 'R') |
| 514 |
RunFromCombat(Play, -1); |
| 515 |
else |
| 516 |
Fire(Play); |
| 517 |
break; |
| 518 |
case C_ANSWER: |
| 519 |
HandleAnswer(Play, To, Data); |
| 520 |
break; |
| 521 |
case C_DONE: |
| 522 |
if (Play->EventNum == E_WAITDONE) { |
| 523 |
Play->EventNum = Play->ResyncNum; |
| 524 |
SendEvent(Play); |
| 525 |
} else if (Play->EventNum != E_NONE && Play->EventNum < E_OUTOFSYNC) { |
| 526 |
Play->EventNum++; |
| 527 |
SendEvent(Play); |
| 528 |
} |
| 529 |
break; |
| 530 |
case C_SPYON: |
| 531 |
if (Play->Cash >= Prices.Spy) { |
| 532 |
dopelog(3, LF_SERVER, _("%s now spying on %s"), GetPlayerName(Play), |
| 533 |
GetPlayerName(To)); |
| 534 |
Play->Cash -= Prices.Spy; |
| 535 |
LoseBitch(Play, NULL, NULL); |
| 536 |
NewEntry.Play = Play; |
| 537 |
NewEntry.Turns = -1; |
| 538 |
AddListEntry(&(To->SpyList), &NewEntry); |
| 539 |
SendPlayerData(Play); |
| 540 |
} else { |
| 541 |
dopelog(2, LF_SERVER, _("%s spy on %s: DENIED"), GetPlayerName(Play), |
| 542 |
GetPlayerName(To)); |
| 543 |
} |
| 544 |
break; |
| 545 |
case C_TIPOFF: |
| 546 |
if (Play->Cash >= Prices.Tipoff) { |
| 547 |
dopelog(3, LF_SERVER, _("%s tipped off the cops to %s"), |
| 548 |
GetPlayerName(Play), GetPlayerName(To)); |
| 549 |
Play->Cash -= Prices.Tipoff; |
| 550 |
LoseBitch(Play, NULL, NULL); |
| 551 |
NewEntry.Play = Play; |
| 552 |
NewEntry.Turns = 0; |
| 553 |
AddListEntry(&(To->TipList), &NewEntry); |
| 554 |
SendPlayerData(Play); |
| 555 |
} else { |
| 556 |
g_warning(_("%s tipoff about %s: DENIED"), GetPlayerName(Play), |
| 557 |
GetPlayerName(To)); |
| 558 |
} |
| 559 |
break; |
| 560 |
case C_SACKBITCH: |
| 561 |
if (Play->Bitches.Carried > 0) { |
| 562 |
LoseBitch(Play, NULL, NULL); |
| 563 |
SendPlayerData(Play); |
| 564 |
} |
| 565 |
break; |
| 566 |
case C_MSG: |
| 567 |
if (Network) |
| 568 |
dopelog(3, LF_SERVER, "%s: %s", GetPlayerName(Play), Data); |
| 569 |
BroadcastToClients(C_NONE, C_MSG, Data, Play, Play); |
| 570 |
break; |
| 571 |
default: |
| 572 |
dopelog(0, LF_SERVER, _("Unknown message: %s:%c:%s:%s"), |
| 573 |
GetPlayerName(Play), Code, GetPlayerName(To), Data); |
| 574 |
break; |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
/* |
| 579 |
* Notifies all clients that player "Play" has left the game and |
| 580 |
* cleans up after them if necessary. |
| 581 |
*/ |
| 582 |
void ClientLeftServer(Player *Play) |
| 583 |
{ |
| 584 |
Player *tmp; |
| 585 |
GSList *list; |
| 586 |
|
| 587 |
if (!IsConnectedPlayer(Play)) |
| 588 |
return; |
| 589 |
|
| 590 |
if (Play->EventNum == E_FIGHT || Play->EventNum == E_FIGHTASK) { |
| 591 |
WithdrawFromCombat(Play); |
| 592 |
} |
| 593 |
for (list = FirstServer; list; list = g_slist_next(list)) { |
| 594 |
tmp = (Player *)list->data; |
| 595 |
if (tmp != Play) { |
| 596 |
RemoveAllEntries(&(tmp->TipList), Play); |
| 597 |
RemoveAllEntries(&(tmp->SpyList), Play); |
| 598 |
} |
| 599 |
} |
| 600 |
BroadcastToClients(C_NONE, C_LEAVE, GetPlayerName(Play), Play, Play); |
| 601 |
} |
| 602 |
|
| 603 |
/* |
| 604 |
* Closes down the server and frees up associated handles and memory. |
| 605 |
*/ |
| 606 |
void CleanUpServer() |
| 607 |
{ |
| 608 |
while (FirstServer) { |
| 609 |
FirstServer = RemovePlayer((Player *)FirstServer->data, FirstServer); |
| 610 |
} |
| 611 |
#ifdef NETWORKING |
| 612 |
if (Server) |
| 613 |
CloseSocket(ListenSock); |
| 614 |
#endif |
| 615 |
} |
| 616 |
|
| 617 |
/* |
| 618 |
* Responds to a SIGUSR1 signal, and requests the main event loop to |
| 619 |
* reregister the server with the dopewars metaserver. |
| 620 |
*/ |
| 621 |
void ReregisterHandle(int sig) |
| 622 |
{ |
| 623 |
ReregisterRequest = 1; |
| 624 |
} |
| 625 |
|
| 626 |
/* |
| 627 |
* Responds to a SIGHUP signal, and requests the main event loop to |
| 628 |
* close and then reopen the log file (if any). |
| 629 |
*/ |
| 630 |
void RelogHandle(int sig) |
| 631 |
{ |
| 632 |
RelogRequest = 1; |
| 633 |
} |
| 634 |
|
| 635 |
/* |
| 636 |
* Traps an attempt by the user to send dopewars a SIGTERM or SIGINT |
| 637 |
* (e.g. pressing Ctrl-C) and signals for a "nice" shutdown. Restores |
| 638 |
* the default signal action (to terminate without cleanup) so that |
| 639 |
* the user can still close the program easily if this cleanup code |
| 640 |
* then causes problems or long delays. |
| 641 |
*/ |
| 642 |
void BreakHandle(int sig) |
| 643 |
{ |
| 644 |
struct sigaction sact; |
| 645 |
|
| 646 |
TerminateRequest = 1; |
| 647 |
sact.sa_handler = SIG_DFL; |
| 648 |
sact.sa_flags = 0; |
| 649 |
sigemptyset(&sact.sa_mask); |
| 650 |
sigaction(SIGTERM, &sact, NULL); |
| 651 |
sigaction(SIGINT, &sact, NULL); |
| 652 |
} |
| 653 |
|
| 654 |
/* |
| 655 |
* Prints the server help screen to the given file pointer. |
| 656 |
*/ |
| 657 |
void PrintHelpTo(FILE *fp) |
| 658 |
{ |
| 659 |
int i; |
| 660 |
GString *VarName; |
| 661 |
|
| 662 |
VarName = g_string_new(""); |
| 663 |
fprintf(fp, _(HelpText), VERSION); |
| 664 |
for (i = 0; i < NUMGLOB; i++) { |
| 665 |
if (Globals[i].NameStruct[0]) { |
| 666 |
g_string_printf(VarName, "%s%s.%s", Globals[i].NameStruct, |
| 667 |
Globals[i].StructListPt ? "[x]" : "", |
| 668 |
Globals[i].Name); |
| 669 |
} else { |
| 670 |
g_string_assign(VarName, Globals[i].Name); |
| 671 |
} |
| 672 |
fprintf(fp, "%-26s %s\n", VarName->str, _(Globals[i].Help)); |
| 673 |
} |
| 674 |
fputs("\n\n", fp); |
| 675 |
g_string_free(VarName, TRUE); |
| 676 |
} |
| 677 |
|
| 678 |
/* |
| 679 |
* Displays a simple help screen listing the server commands and options. |
| 680 |
*/ |
| 681 |
void ServerHelp(void) |
| 682 |
{ |
| 683 |
int i; |
| 684 |
GString *VarName; |
| 685 |
|
| 686 |
VarName = g_string_new(""); |
| 687 |
g_print(_(HelpText), VERSION); |
| 688 |
for (i = 0; i < NUMGLOB; i++) { |
| 689 |
if (Globals[i].NameStruct[0]) { |
| 690 |
g_string_printf(VarName, "%s%s.%s", Globals[i].NameStruct, |
| 691 |
Globals[i].StructListPt ? "[x]" : "", |
| 692 |
Globals[i].Name); |
| 693 |
} else { |
| 694 |
g_string_assign(VarName, Globals[i].Name); |
| 695 |
} |
| 696 |
g_print("%-26s\t%s\n", VarName->str, _(Globals[i].Help)); |
| 697 |
} |
| 698 |
g_string_free(VarName, TRUE); |
| 699 |
} |
| 700 |
|
| 701 |
#ifdef NETWORKING |
| 702 |
static NetworkBuffer *reply_netbuf; |
| 703 |
static void ServerReply(const gchar *msg) |
| 704 |
{ |
| 705 |
int msglen; |
| 706 |
gchar *msgcp; |
| 707 |
|
| 708 |
if (reply_netbuf) { |
| 709 |
msglen = strlen(msg); |
| 710 |
while (msglen > 0 && msg[msglen - 1] == '\n') |
| 711 |
msglen--; |
| 712 |
if (msglen > 0) { |
| 713 |
msgcp = g_strndup(msg, msglen); |
| 714 |
QueueMessageForSend(reply_netbuf, msgcp); |
| 715 |
g_free(msgcp); |
| 716 |
} |
| 717 |
} else { |
| 718 |
g_print("%s", msg); |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
/* |
| 723 |
* Creates a pid file (if "PidFile" is non-NULL) and writes the process |
| 724 |
* ID into it. |
| 725 |
*/ |
| 726 |
void CreatePidFile(void) |
| 727 |
{ |
| 728 |
FILE *fp; |
| 729 |
|
| 730 |
if (!PidFile) |
| 731 |
return; |
| 732 |
fp = fopen(PidFile, "w"); |
| 733 |
if (fp) { |
| 734 |
dopelog(1, LF_SERVER, _("Maintaining pid file %s"), PidFile); |
| 735 |
fprintf(fp, "%ld\n", (long)getpid()); |
| 736 |
fclose(fp); |
| 737 |
chmod(PidFile, S_IREAD | S_IWRITE); |
| 738 |
} else { |
| 739 |
gchar *OpenError = ErrStrFromErrno(errno); |
| 740 |
g_warning(_("Cannot create pid file %s: %s"), PidFile, OpenError); |
| 741 |
g_free(OpenError); |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
/* |
| 746 |
* Removes the previously-created pid file "PidFile". |
| 747 |
*/ |
| 748 |
void RemovePidFile(void) |
| 749 |
{ |
| 750 |
if (PidFile) |
| 751 |
unlink(PidFile); |
| 752 |
} |
| 753 |
|
| 754 |
static gboolean StartServer(void) |
| 755 |
{ |
| 756 |
LastError *sockerr = NULL; |
| 757 |
GString *errstr; |
| 758 |
|
| 759 |
#ifndef CYGWIN |
| 760 |
struct sigaction sact; |
| 761 |
#endif |
| 762 |
|
| 763 |
if (!CheckHighScoreFileConfig()) |
| 764 |
return FALSE; |
| 765 |
Scanner = g_scanner_new(&ScannerConfig); |
| 766 |
Scanner->msg_handler = ScannerErrorHandler; |
| 767 |
Scanner->input_name = "(stdin)"; |
| 768 |
|
| 769 |
/* Make the output line-buffered, so that the log file (if used) is |
| 770 |
* updated regularly */ |
| 771 |
fflush(stdout); |
| 772 |
|
| 773 |
#ifdef SETVBUF_REVERSED /* 2nd and 3rd arguments are reversed on |
| 774 |
* some systems */ |
| 775 |
setvbuf(stdout, _IOLBF, (char *)NULL, 0); |
| 776 |
#else |
| 777 |
setvbuf(stdout, (char *)NULL, _IOLBF, 0); |
| 778 |
#endif |
| 779 |
|
| 780 |
Network = Server = TRUE; |
| 781 |
FirstServer = NULL; |
| 782 |
ClientMessageHandlerPt = NULL; |
| 783 |
ListenSock = CreateTCPSocket(&sockerr); |
| 784 |
if (ListenSock == SOCKET_ERROR) { |
| 785 |
errstr = g_string_new(""); |
| 786 |
g_string_assign_error(errstr, sockerr); |
| 787 |
g_log(NULL, G_LOG_LEVEL_CRITICAL, |
| 788 |
_("Cannot create server (listening) socket (%s) Aborting."), |
| 789 |
errstr->str); |
| 790 |
g_string_free(errstr, TRUE); |
| 791 |
FreeError(sockerr); |
| 792 |
exit(EXIT_FAILURE); |
| 793 |
} |
| 794 |
|
| 795 |
/* This doesn't seem to work properly under Win32 */ |
| 796 |
#ifndef CYGWIN |
| 797 |
SetReuse(ListenSock); |
| 798 |
#endif |
| 799 |
|
| 800 |
SetBlocking(ListenSock, FALSE); |
| 801 |
|
| 802 |
if (!BindTCPSocket(ListenSock, BindAddress, Port, &sockerr)) { |
| 803 |
errstr = g_string_new(""); |
| 804 |
g_string_assign_error(errstr, sockerr); |
| 805 |
g_log(NULL, G_LOG_LEVEL_CRITICAL, |
| 806 |
_("Cannot bind to port %u (%s) Aborting."), Port, errstr->str); |
| 807 |
g_string_free(errstr, TRUE); |
| 808 |
FreeError(sockerr); |
| 809 |
exit(EXIT_FAILURE); |
| 810 |
} |
| 811 |
|
| 812 |
if (listen(ListenSock, 10) == SOCKET_ERROR) { |
| 813 |
g_log(NULL, G_LOG_LEVEL_CRITICAL, |
| 814 |
_("Cannot listen to network socket. Aborting.")); |
| 815 |
exit(EXIT_FAILURE); |
| 816 |
} |
| 817 |
|
| 818 |
/* Initial startup message for the server */ |
| 819 |
dopelog(0, LF_SERVER, |
| 820 |
_("dopewars server version %s ready and waiting for " |
| 821 |
"connections on port %d."), VERSION, Port); |
| 822 |
|
| 823 |
MetaUpdateTimeout = MetaMinTimeout = 0; |
| 824 |
|
| 825 |
TerminateRequest = ReregisterRequest = RelogRequest = 0; |
| 826 |
|
| 827 |
#if !CYGWIN |
| 828 |
sact.sa_handler = ReregisterHandle; |
| 829 |
sact.sa_flags = 0; |
| 830 |
sigemptyset(&sact.sa_mask); |
| 831 |
if (sigaction(SIGUSR1, &sact, NULL) == -1) { |
| 832 |
/* Warning messages displayed if we fail to trap various signals */ |
| 833 |
g_warning(_("Cannot install SIGUSR1 interrupt handler!")); |
| 834 |
} |
| 835 |
sact.sa_handler = RelogHandle; |
| 836 |
sact.sa_flags = 0; |
| 837 |
sigemptyset(&sact.sa_mask); |
| 838 |
if (sigaction(SIGHUP, &sact, NULL) == -1) { |
| 839 |
g_warning(_("Cannot install SIGHUP interrupt handler!")); |
| 840 |
} |
| 841 |
sact.sa_handler = BreakHandle; |
| 842 |
sact.sa_flags = 0; |
| 843 |
sigemptyset(&sact.sa_mask); |
| 844 |
if (sigaction(SIGINT, &sact, NULL) == -1) { |
| 845 |
g_warning(_("Cannot install SIGINT interrupt handler!")); |
| 846 |
} |
| 847 |
if (sigaction(SIGTERM, &sact, NULL) == -1) { |
| 848 |
g_warning(_("Cannot install SIGTERM interrupt handler!")); |
| 849 |
} |
| 850 |
sact.sa_handler = SIG_IGN; |
| 851 |
sact.sa_flags = 0; |
| 852 |
if (sigaction(SIGPIPE, &sact, NULL) == -1) { |
| 853 |
g_warning(_("Cannot install pipe handler!")); |
| 854 |
} |
| 855 |
#endif |
| 856 |
return TRUE; |
| 857 |
} |
| 858 |
|
| 859 |
static void InitMetaServer() |
| 860 |
{ |
| 861 |
CurlInit(&MetaConn); |
| 862 |
#ifdef GUI_SERVER |
| 863 |
SetCurlCallback(&MetaConn, glib_timeout, glib_socket); |
| 864 |
#endif |
| 865 |
RegisterWithMetaServer(TRUE, TRUE, FALSE); |
| 866 |
} |
| 867 |
|
| 868 |
/* |
| 869 |
* Begin the process of shutting down the server. In order to do this, |
| 870 |
* we need to log out all of the currently connected players, and tell |
| 871 |
* the metaserver that we're shutting down. We only shut down properly |
| 872 |
* once all of these messages have been completely sent and |
| 873 |
* acknowledged. (Of course, this can be overridden by a SIGINT or |
| 874 |
* similar in the case of unresponsive players.) |
| 875 |
*/ |
| 876 |
void RequestServerShutdown(void) |
| 877 |
{ |
| 878 |
RegisterWithMetaServer(FALSE, FALSE, FALSE); |
| 879 |
BroadcastToClients(C_NONE, C_QUIT, NULL, NULL, NULL); |
| 880 |
WantQuit = TRUE; |
| 881 |
} |
| 882 |
|
| 883 |
/* |
| 884 |
* Returns TRUE if the actions initiated by RequestServerShutdown() |
| 885 |
* have been successfully completed, such that we can shut down the |
| 886 |
* server properly now. |
| 887 |
*/ |
| 888 |
gboolean IsServerShutdown(void) |
| 889 |
{ |
| 890 |
return (WantQuit && !FirstServer && !MetaConn.running); |
| 891 |
} |
| 892 |
|
| 893 |
static GPrintFunc StartServerReply(NetworkBuffer *netbuf) |
| 894 |
{ |
| 895 |
reply_netbuf = netbuf; |
| 896 |
if (netbuf) |
| 897 |
return g_set_print_handler(ServerReply); |
| 898 |
else |
| 899 |
return NULL; |
| 900 |
} |
| 901 |
|
| 902 |
static void FinishServerReply(GPrintFunc oldprint) |
| 903 |
{ |
| 904 |
if (oldprint) |
| 905 |
g_set_print_handler(oldprint); |
| 906 |
} |
| 907 |
|
| 908 |
static void ServerSaveConfigFile(const char *string) |
| 909 |
{ |
| 910 |
gchar *file = NULL; |
| 911 |
|
| 912 |
if (!string) { |
| 913 |
file = GetLocalConfigFile(); |
| 914 |
string = file; |
| 915 |
} |
| 916 |
if (UpdateConfigFile(file, FALSE)) { |
| 917 |
g_print(_("Configuration file saved OK as %s\n"), string); |
| 918 |
} |
| 919 |
g_free(file); |
| 920 |
} |
| 921 |
|
| 922 |
static void HandleServerCommand(char *string, NetworkBuffer *netbuf, |
| 923 |
gboolean ForceUTF8) |
| 924 |
{ |
| 925 |
GSList *list; |
| 926 |
Player *tmp; |
| 927 |
GPrintFunc oldprint; |
| 928 |
Converter *conv; |
| 929 |
|
| 930 |
oldprint = StartServerReply(netbuf); |
| 931 |
|
| 932 |
conv = Conv_New(); |
| 933 |
if (ForceUTF8) { |
| 934 |
Conv_SetCodeset(conv, "UTF-8"); |
| 935 |
} |
| 936 |
g_scanner_input_text(Scanner, string, strlen(string)); |
| 937 |
if (!ParseNextConfig(Scanner, conv, NULL, TRUE)) { |
| 938 |
if (g_ascii_strncasecmp(string, "help", 4) == 0 || g_ascii_strncasecmp(string, "h", 1) == 0 |
| 939 |
|| strcmp(string, "?") == 0) { |
| 940 |
ServerHelp(); |
| 941 |
} else if (g_ascii_strncasecmp(string, "quit", 4) == 0) { |
| 942 |
RequestServerShutdown(); |
| 943 |
} else if (g_ascii_strncasecmp(string, "msg:", 4) == 0) { |
| 944 |
BroadcastToClients(C_NONE, C_MSG, string + 4, NULL, NULL); |
| 945 |
} else if (g_ascii_strncasecmp(string, "save ", 5) == 0) { |
| 946 |
ServerSaveConfigFile(string + 5); |
| 947 |
} else if (g_ascii_strncasecmp(string, "save", 4) == 0) { |
| 948 |
ServerSaveConfigFile(NULL); |
| 949 |
} else if (g_ascii_strncasecmp(string, "list", 4) == 0) { |
| 950 |
if (FirstServer) { |
| 951 |
g_print(_("Users currently logged on:-\n")); |
| 952 |
for (list = FirstServer; list; list = g_slist_next(list)) { |
| 953 |
tmp = (Player *)list->data; |
| 954 |
if (!IsCop(tmp)) { |
| 955 |
g_print("%s\n", GetPlayerName(tmp)); |
| 956 |
} |
| 957 |
} |
| 958 |
} else |
| 959 |
g_print(_("No users currently logged on!\n")); |
| 960 |
} else if (g_ascii_strncasecmp(string, "push ", 5) == 0) { |
| 961 |
tmp = GetPlayerByName(string + 5, FirstServer); |
| 962 |
if (tmp) { |
| 963 |
g_print(_("Pushing %s\n"), GetPlayerName(tmp)); |
| 964 |
SendServerMessage(NULL, C_NONE, C_PUSH, tmp, NULL); |
| 965 |
} else |
| 966 |
g_print(_("No such user!\n")); |
| 967 |
} else if (g_ascii_strncasecmp(string, "kill ", 5) == 0) { |
| 968 |
tmp = GetPlayerByName(string + 5, FirstServer); |
| 969 |
if (tmp) { |
| 970 |
/* The named user has been removed from the server following |
| 971 |
a "kill" command */ |
| 972 |
g_print(_("%s killed\n"), GetPlayerName(tmp)); |
| 973 |
BroadcastToClients(C_NONE, C_KILL, GetPlayerName(tmp), tmp, |
| 974 |
(Player *)FirstServer->data); |
| 975 |
FirstServer = RemovePlayer(tmp, FirstServer); |
| 976 |
} else |
| 977 |
g_print(_("No such user!\n")); |
| 978 |
} else { |
| 979 |
g_print(_("Unknown command - try \"help\" for help...\n")); |
| 980 |
} |
| 981 |
} |
| 982 |
Conv_Free(conv); |
| 983 |
FinishServerReply(oldprint); |
| 984 |
} |
| 985 |
|
| 986 |
Player *HandleNewConnection(void) |
| 987 |
{ |
| 988 |
socklen_t cadsize; |
| 989 |
int ClientSock; |
| 990 |
struct sockaddr_in ClientAddr; |
| 991 |
Player *tmp; |
| 992 |
cadsize = sizeof(struct sockaddr); |
| 993 |
if ((ClientSock = accept(ListenSock, (struct sockaddr *)&ClientAddr, |
| 994 |
&cadsize)) == -1) { |
| 995 |
perror("accept socket"); |
| 996 |
exit(EXIT_FAILURE); |
| 997 |
} |
| 998 |
dopelog(2, LF_SERVER, _("got connection from %s"), |
| 999 |
inet_ntoa(ClientAddr.sin_addr)); |
| 1000 |
tmp = g_new(Player, 1); |
| 1001 |
|
| 1002 |
FirstServer = AddPlayer(ClientSock, tmp, FirstServer); |
| 1003 |
if (ConnectTimeout) { |
| 1004 |
tmp->ConnectTimeout = time(NULL) + (time_t) ConnectTimeout; |
| 1005 |
} |
| 1006 |
return tmp; |
| 1007 |
} |
| 1008 |
|
| 1009 |
void StopServer() |
| 1010 |
{ |
| 1011 |
dopelog(0, LF_SERVER, _("dopewars server terminating.")); |
| 1012 |
g_scanner_destroy(Scanner); |
| 1013 |
CleanUpServer(); |
| 1014 |
RemovePidFile(); |
| 1015 |
} |
| 1016 |
|
| 1017 |
void RemovePlayerFromServer(Player *Play) |
| 1018 |
{ |
| 1019 |
if (!WantQuit && strlen(GetPlayerName(Play)) > 0) { |
| 1020 |
dopelog(2, LF_SERVER, _("%s leaves the server!"), GetPlayerName(Play)); |
| 1021 |
ClientLeftServer(Play); |
| 1022 |
/* Blank the name, so that CountPlayers ignores this player */ |
| 1023 |
SetPlayerName(Play, NULL); |
| 1024 |
/* Report the new high scores (if any) and the new number of players |
| 1025 |
* to the metaserver */ |
| 1026 |
RegisterWithMetaServer(TRUE, TRUE, TRUE); |
| 1027 |
} |
| 1028 |
FirstServer = RemovePlayer(Play, FirstServer); |
| 1029 |
} |
| 1030 |
|
| 1031 |
#ifndef CYGWIN |
| 1032 |
static gchar sockpref[] = "/tmp/.dopewars"; |
| 1033 |
|
| 1034 |
static gchar *GetLocalSockDir(void) |
| 1035 |
{ |
| 1036 |
return g_strdup_printf("%s-%u", sockpref, Port); |
| 1037 |
} |
| 1038 |
|
| 1039 |
gchar *GetLocalSocket(void) |
| 1040 |
{ |
| 1041 |
return g_strdup_printf("%s-%u/socket", sockpref, Port); |
| 1042 |
} |
| 1043 |
|
| 1044 |
static void CloseLocalSocket(int localsock) |
| 1045 |
{ |
| 1046 |
gchar *sockname, *sockdir; |
| 1047 |
|
| 1048 |
if (localsock >= 0) |
| 1049 |
close(localsock); |
| 1050 |
|
| 1051 |
sockname = GetLocalSocket(); |
| 1052 |
sockdir = GetLocalSockDir(); |
| 1053 |
unlink(sockname); |
| 1054 |
rmdir(sockdir); |
| 1055 |
g_free(sockname); |
| 1056 |
g_free(sockdir); |
| 1057 |
} |
| 1058 |
|
| 1059 |
static int SetupLocalSocket(void) |
| 1060 |
{ |
| 1061 |
int sock; |
| 1062 |
struct sockaddr_un addr; |
| 1063 |
gchar *sockname, *sockdir; |
| 1064 |
|
| 1065 |
CloseLocalSocket(-1); |
| 1066 |
|
| 1067 |
sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 1068 |
if (sock == -1) |
| 1069 |
return -1; |
| 1070 |
|
| 1071 |
SetBlocking(sock, FALSE); |
| 1072 |
|
| 1073 |
sockname = GetLocalSocket(); |
| 1074 |
sockdir = GetLocalSockDir(); |
| 1075 |
if (mkdir(sockdir, S_IRUSR | S_IWUSR | S_IXUSR) == -1) |
| 1076 |
return -1; |
| 1077 |
|
| 1078 |
addr.sun_family = AF_UNIX; |
| 1079 |
strncpy(addr.sun_path, sockname, sizeof(addr.sun_path)); |
| 1080 |
addr.sun_path[sizeof(addr.sun_path) - 1] = '\0'; |
| 1081 |
|
| 1082 |
if (bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) |
| 1083 |
return -1; |
| 1084 |
|
| 1085 |
chmod(sockname, S_IRUSR | S_IWUSR); |
| 1086 |
g_free(sockname); |
| 1087 |
g_free(sockdir); |
| 1088 |
|
| 1089 |
listen(sock, 10); |
| 1090 |
|
| 1091 |
return sock; |
| 1092 |
} |
| 1093 |
#endif |
| 1094 |
|
| 1095 |
static void LogMetaReply(CurlConnection *conn) |
| 1096 |
{ |
| 1097 |
g_ptr_array_foreach(conn->headers, log_meta_headers, NULL); |
| 1098 |
char *ch = conn->data; |
| 1099 |
while(ch && *ch) { |
| 1100 |
char *nextch = CurlNextLine(conn, ch); |
| 1101 |
if (*ch) |
| 1102 |
dopelog(2, LF_SERVER, _("MetaServer: %s"), ch); |
| 1103 |
ch = nextch; |
| 1104 |
} |
| 1105 |
dopelog(4, LF_SERVER, _("MetaServer: (closed)")); |
| 1106 |
} |
| 1107 |
|
| 1108 |
#ifdef GUI_SERVER |
| 1109 |
static gboolean glib_timeout(gpointer userp) |
| 1110 |
{ |
| 1111 |
CurlConnection *g = userp; |
| 1112 |
int still_running; |
| 1113 |
GError *err = NULL; |
| 1114 |
if (!CurlConnectionSocketAction(g, CURL_SOCKET_TIMEOUT, 0, &still_running, |
| 1115 |
&err)) { |
| 1116 |
MetaConnectError(g, err); |
| 1117 |
g_error_free(err); |
| 1118 |
} |
| 1119 |
g->timer_event = 0; |
| 1120 |
return G_SOURCE_REMOVE; |
| 1121 |
} |
| 1122 |
|
| 1123 |
static void GuiQuitServer() |
| 1124 |
{ |
| 1125 |
gtk_main_quit(); |
| 1126 |
StopServer(); |
| 1127 |
} |
| 1128 |
|
| 1129 |
/* Called by glib when we get action on a multi socket */ |
| 1130 |
static gboolean glib_socket(GIOChannel *ch, GIOCondition condition, |
| 1131 |
gpointer data) |
| 1132 |
{ |
| 1133 |
CurlConnection *g = (CurlConnection*) data; |
| 1134 |
int still_running; |
| 1135 |
GError *err = NULL; |
| 1136 |
int fd = g_io_channel_unix_get_fd(ch); |
| 1137 |
int action = |
| 1138 |
((condition & G_IO_IN) ? CURL_CSELECT_IN : 0) | |
| 1139 |
((condition & G_IO_OUT) ? CURL_CSELECT_OUT : 0); |
| 1140 |
|
| 1141 |
if (!CurlConnectionSocketAction(g, fd, action, &still_running, &err)) { |
| 1142 |
MetaConnectError(g, err); |
| 1143 |
g_error_free(err); |
| 1144 |
} |
| 1145 |
if (still_running) { |
| 1146 |
return TRUE; |
| 1147 |
} else { |
| 1148 |
if (g->timer_event) { |
| 1149 |
dp_g_source_remove(g->timer_event); |
| 1150 |
g->timer_event = 0; |
| 1151 |
} |
| 1152 |
LogMetaReply(g); |
| 1153 |
CloseCurlConnection(g); |
| 1154 |
if (IsServerShutdown()) |
| 1155 |
GuiQuitServer(); |
| 1156 |
return FALSE; |
| 1157 |
} |
| 1158 |
} |
| 1159 |
|
| 1160 |
#endif |
| 1161 |
|
| 1162 |
/* |
| 1163 |
* Initializes server, processes network and interactive messages, and |
| 1164 |
* finally cleans up the server on exit. |
| 1165 |
*/ |
| 1166 |
void ServerLoop(struct CMDLINE *cmdline) |
| 1167 |
{ |
| 1168 |
Player *tmp; |
| 1169 |
GSList *list, *listcp; |
| 1170 |
fd_set readfs, writefs, errorfs; |
| 1171 |
int topsock; |
| 1172 |
struct timeval timeout; |
| 1173 |
int MinTimeout; |
| 1174 |
GString *LineBuf; |
| 1175 |
|
| 1176 |
gboolean DoneOK; |
| 1177 |
|
| 1178 |
#ifndef CYGWIN |
| 1179 |
gchar *buf; |
| 1180 |
int localsock; |
| 1181 |
GSList *nextlist; |
| 1182 |
GPrintFunc oldprint; |
| 1183 |
GSList *localconn = NULL; |
| 1184 |
#endif |
| 1185 |
|
| 1186 |
InitConfiguration(cmdline); |
| 1187 |
|
| 1188 |
if (!StartServer()) |
| 1189 |
return; |
| 1190 |
|
| 1191 |
#ifdef HAVE_FORK |
| 1192 |
/* Daemonize; continue if the fork was successful and we are the child, |
| 1193 |
* or if the fork failed */ |
| 1194 |
if (Daemonize && fork() > 0) |
| 1195 |
return; |
| 1196 |
#endif |
| 1197 |
CreatePidFile(); |
| 1198 |
InitMetaServer(); |
| 1199 |
|
| 1200 |
#ifndef CYGWIN |
| 1201 |
localsock = SetupLocalSocket(); |
| 1202 |
if (localsock == -1) { |
| 1203 |
dopelog(0, LF_SERVER, |
| 1204 |
_("Could not set up Unix domain socket for admin " |
| 1205 |
"connections - check permissions on /tmp!")); |
| 1206 |
} |
| 1207 |
#endif |
| 1208 |
|
| 1209 |
LineBuf = g_string_new(""); |
| 1210 |
while (1) { |
| 1211 |
FD_ZERO(&readfs); |
| 1212 |
FD_ZERO(&writefs); |
| 1213 |
FD_ZERO(&errorfs); |
| 1214 |
curl_multi_fdset(MetaConn.multi, &readfs, &writefs, &errorfs, &topsock); |
| 1215 |
FD_SET(ListenSock, &readfs); |
| 1216 |
FD_SET(ListenSock, &errorfs); |
| 1217 |
topsock = MAX(topsock + 1, ListenSock + 1); |
| 1218 |
#ifndef CYGWIN |
| 1219 |
if (localsock >= 0) { |
| 1220 |
FD_SET(localsock, &readfs); |
| 1221 |
topsock = MAX(topsock, localsock + 1); |
| 1222 |
} |
| 1223 |
for (list = localconn; list; list = g_slist_next(list)) { |
| 1224 |
NetworkBuffer *netbuf; |
| 1225 |
|
| 1226 |
netbuf = (NetworkBuffer *)list->data; |
| 1227 |
SetSelectForNetworkBuffer(netbuf, &readfs, &writefs, &errorfs, |
| 1228 |
&topsock); |
| 1229 |
} |
| 1230 |
#endif |
| 1231 |
for (list = FirstServer; list; list = g_slist_next(list)) { |
| 1232 |
tmp = (Player *)list->data; |
| 1233 |
if (!IsCop(tmp)) { |
| 1234 |
SetSelectForNetworkBuffer(&tmp->NetBuf, &readfs, &writefs, |
| 1235 |
&errorfs, &topsock); |
| 1236 |
} |
| 1237 |
} |
| 1238 |
MinTimeout = GetMinimumTimeout(FirstServer); |
| 1239 |
if (MinTimeout != -1) { |
| 1240 |
timeout.tv_sec = MinTimeout; |
| 1241 |
timeout.tv_usec = 0; |
| 1242 |
} |
| 1243 |
if (select(topsock, &readfs, &writefs, &errorfs, |
| 1244 |
MinTimeout == -1 ? NULL : &timeout) == -1) { |
| 1245 |
if (errno == EINTR) { |
| 1246 |
if (ReregisterRequest) { |
| 1247 |
ReregisterRequest = 0; |
| 1248 |
RegisterWithMetaServer(TRUE, TRUE, FALSE); |
| 1249 |
continue; |
| 1250 |
} else if (TerminateRequest) { |
| 1251 |
TerminateRequest = 0; |
| 1252 |
RequestServerShutdown(); |
| 1253 |
if (IsServerShutdown()) |
| 1254 |
break; |
| 1255 |
else |
| 1256 |
continue; |
| 1257 |
} else if (RelogRequest) { /* Re-open log file */ |
| 1258 |
RelogRequest = 0; |
| 1259 |
CloseLog(); |
| 1260 |
OpenLog(); |
| 1261 |
continue; |
| 1262 |
} else |
| 1263 |
continue; |
| 1264 |
} |
| 1265 |
perror("select"); |
| 1266 |
break; |
| 1267 |
} |
| 1268 |
FirstServer = HandleTimeouts(FirstServer); |
| 1269 |
if (FD_ISSET(ListenSock, &readfs)) { |
| 1270 |
HandleNewConnection(); |
| 1271 |
} |
| 1272 |
#ifndef CYGWIN |
| 1273 |
if (localsock >= 0 && FD_ISSET(localsock, &readfs)) { |
| 1274 |
int newlocal; |
| 1275 |
NetworkBuffer *netbuf; |
| 1276 |
|
| 1277 |
newlocal = accept(localsock, NULL, NULL); |
| 1278 |
netbuf = g_new(NetworkBuffer, 1); |
| 1279 |
|
| 1280 |
InitNetworkBuffer(netbuf, '\n', '\r', NULL); |
| 1281 |
BindNetworkBufferToSocket(netbuf, newlocal); |
| 1282 |
localconn = g_slist_append(localconn, netbuf); |
| 1283 |
oldprint = StartServerReply(netbuf); |
| 1284 |
g_print(_("dopewars server version %s ready for admin commands; " |
| 1285 |
"try \"help\" for help"), VERSION); |
| 1286 |
FinishServerReply(oldprint); |
| 1287 |
dopelog(1, LF_SERVER, _("New admin connection")); |
| 1288 |
} |
| 1289 |
list = localconn; |
| 1290 |
while (list) { |
| 1291 |
NetworkBuffer *netbuf; |
| 1292 |
|
| 1293 |
nextlist = g_slist_next(list); |
| 1294 |
netbuf = (NetworkBuffer *)list->data; |
| 1295 |
if (netbuf) { |
| 1296 |
if (RespondToSelect(netbuf, &readfs, &writefs, &errorfs, &DoneOK)) { |
| 1297 |
while ((buf = GetWaitingMessage(netbuf)) != NULL) { |
| 1298 |
dopelog(2, LF_SERVER, _("Admin command: %s"), buf); |
| 1299 |
HandleServerCommand(buf, netbuf, FALSE); |
| 1300 |
g_free(buf); |
| 1301 |
} |
| 1302 |
} |
| 1303 |
if (!DoneOK) { |
| 1304 |
dopelog(1, LF_SERVER, _("Admin connection closed")); |
| 1305 |
localconn = g_slist_remove(localconn, netbuf); |
| 1306 |
ShutdownNetworkBuffer(netbuf); |
| 1307 |
g_free(netbuf); |
| 1308 |
} |
| 1309 |
list = nextlist; |
| 1310 |
} |
| 1311 |
} |
| 1312 |
if (IsServerShutdown()) |
| 1313 |
break; |
| 1314 |
#endif |
| 1315 |
if (MetaConn.running) { |
| 1316 |
GError *tmp_error = NULL; |
| 1317 |
int still_running; |
| 1318 |
if (!CurlConnectionPerform(&MetaConn, &still_running, &tmp_error)) { |
| 1319 |
MetaConnectError(&MetaConn, tmp_error); |
| 1320 |
g_error_free(tmp_error); |
| 1321 |
if (IsServerShutdown()) |
| 1322 |
break; |
| 1323 |
} else if (still_running == 0) { |
| 1324 |
LogMetaReply(&MetaConn); |
| 1325 |
CloseCurlConnection(&MetaConn); |
| 1326 |
if (IsServerShutdown()) |
| 1327 |
break; |
| 1328 |
} |
| 1329 |
} |
| 1330 |
|
| 1331 |
/* Check all players for data; iterate over a copy of the player list, |
| 1332 |
* as HandleServerPlayer may remove players from this list! */ |
| 1333 |
listcp = g_slist_copy(FirstServer); |
| 1334 |
for (list = listcp; list; list = g_slist_next(list)) { |
| 1335 |
if (list->data && g_slist_find(FirstServer, list->data)) { |
| 1336 |
tmp = (Player *)list->data; |
| 1337 |
if (RespondToSelect(&tmp->NetBuf, &readfs, &writefs, |
| 1338 |
&errorfs, &DoneOK)) { |
| 1339 |
/* If any complete messages were read, process them */ |
| 1340 |
HandleServerPlayer(tmp); |
| 1341 |
} |
| 1342 |
if (!DoneOK) { |
| 1343 |
/* The socket has been shut down, or the buffer was filled - |
| 1344 |
* remove player */ |
| 1345 |
RemovePlayerFromServer(tmp); |
| 1346 |
if (IsServerShutdown()) { |
| 1347 |
break; |
| 1348 |
} |
| 1349 |
} |
| 1350 |
} |
| 1351 |
} |
| 1352 |
g_slist_free(listcp); |
| 1353 |
if (IsServerShutdown()) { |
| 1354 |
break; |
| 1355 |
} |
| 1356 |
} |
| 1357 |
#ifndef CYGWIN |
| 1358 |
CloseLocalSocket(localsock); |
| 1359 |
#endif |
| 1360 |
StopServer(); |
| 1361 |
g_string_free(LineBuf, TRUE); |
| 1362 |
|
| 1363 |
CurlCleanup(&MetaConn); |
| 1364 |
} |
| 1365 |
|
| 1366 |
#ifdef GUI_SERVER |
| 1367 |
static GtkWidget *TextOutput; |
| 1368 |
static gint ListenTag = 0; |
| 1369 |
static void SocketStatus(NetworkBuffer *NetBuf, gboolean Read, |
| 1370 |
gboolean Write, gboolean Exception, gboolean CallNow); |
| 1371 |
static void GuiSetTimeouts(void); |
| 1372 |
static time_t NextTimeout = 0; |
| 1373 |
static guint TimeoutTag = 0; |
| 1374 |
|
| 1375 |
static gboolean GuiDoTimeouts(gpointer data) |
| 1376 |
{ |
| 1377 |
/* Forget the TimeoutTag so that GuiSetTimeouts doesn't delete it - |
| 1378 |
* it'll be deleted automatically anyway when we return FALSE */ |
| 1379 |
TimeoutTag = 0; |
| 1380 |
NextTimeout = 0; |
| 1381 |
|
| 1382 |
FirstServer = HandleTimeouts(FirstServer); |
| 1383 |
GuiSetTimeouts(); |
| 1384 |
return FALSE; |
| 1385 |
} |
| 1386 |
|
| 1387 |
void GuiSetTimeouts(void) |
| 1388 |
{ |
| 1389 |
int MinTimeout; |
| 1390 |
time_t TimeNow; |
| 1391 |
|
| 1392 |
TimeNow = time(NULL); |
| 1393 |
MinTimeout = GetMinimumTimeout(FirstServer); |
| 1394 |
if (TimeNow + MinTimeout < NextTimeout || NextTimeout < TimeNow) { |
| 1395 |
if (TimeoutTag > 0) |
| 1396 |
dp_g_source_remove(TimeoutTag); |
| 1397 |
TimeoutTag = 0; |
| 1398 |
if (MinTimeout > 0) { |
| 1399 |
TimeoutTag = dp_g_timeout_add(MinTimeout * 1000, GuiDoTimeouts, NULL); |
| 1400 |
NextTimeout = TimeNow + MinTimeout; |
| 1401 |
} |
| 1402 |
} |
| 1403 |
} |
| 1404 |
|
| 1405 |
static void GuiServerPrintFunc(const gchar *string) |
| 1406 |
{ |
| 1407 |
TextViewAppend(GTK_TEXT_VIEW(TextOutput), string, NULL, TRUE); |
| 1408 |
} |
| 1409 |
|
| 1410 |
static void GuiServerLogMessage(const gchar *log_domain, |
| 1411 |
GLogLevelFlags log_level, |
| 1412 |
const gchar *message, gpointer user_data) |
| 1413 |
{ |
| 1414 |
GString *text; |
| 1415 |
|
| 1416 |
text = GetLogString(log_level, message); |
| 1417 |
if (text) { |
| 1418 |
g_string_append(text, "\n"); |
| 1419 |
GuiServerPrintFunc(text->str); |
| 1420 |
g_string_free(text, TRUE); |
| 1421 |
} |
| 1422 |
} |
| 1423 |
|
| 1424 |
#ifdef CYGWIN |
| 1425 |
static void ServiceFailure(const gchar *log_domain, |
| 1426 |
GLogLevelFlags log_level, const gchar *message, |
| 1427 |
gpointer user_data) |
| 1428 |
{ |
| 1429 |
SERVICE_STATUS status; |
| 1430 |
|
| 1431 |
g_print("%s\n", message); |
| 1432 |
status.dwServiceType = SERVICE_WIN32_OWN_PROCESS; |
| 1433 |
status.dwCurrentState = SERVICE_STOPPED; |
| 1434 |
status.dwControlsAccepted = SERVICE_ACCEPT_STOP; |
| 1435 |
status.dwWin32ExitCode = ERROR_NETWORK_UNREACHABLE; |
| 1436 |
status.dwCheckPoint = 0; |
| 1437 |
status.dwWaitHint = 0; |
| 1438 |
SetServiceStatus(scHandle, &status); |
| 1439 |
} |
| 1440 |
#endif |
| 1441 |
|
| 1442 |
static void GuiDoCommand(GtkWidget *widget, gpointer data) |
| 1443 |
{ |
| 1444 |
gchar *text; |
| 1445 |
|
| 1446 |
text = gtk_editable_get_chars(GTK_EDITABLE(widget), 0, -1); |
| 1447 |
gtk_editable_delete_text(GTK_EDITABLE(widget), 0, -1); |
| 1448 |
HandleServerCommand(text, NULL, TRUE); |
| 1449 |
g_free(text); |
| 1450 |
if (IsServerShutdown()) |
| 1451 |
GuiQuitServer(); |
| 1452 |
} |
| 1453 |
|
| 1454 |
static gboolean GuiHandleSocket(GIOChannel *source, GIOCondition condition, |
| 1455 |
gpointer data) |
| 1456 |
{ |
| 1457 |
Player *Play; |
| 1458 |
gboolean DoneOK; |
| 1459 |
|
| 1460 |
Play = (Player *)data; |
| 1461 |
|
| 1462 |
/* Sanity check - is the player still around? */ |
| 1463 |
if (!g_slist_find(FirstServer, (gpointer)Play)) |
| 1464 |
return TRUE; |
| 1465 |
|
| 1466 |
if (PlayerHandleNetwork(Play, condition & G_IO_IN, condition & G_IO_OUT, |
| 1467 |
condition & G_IO_ERR, &DoneOK)) { |
| 1468 |
HandleServerPlayer(Play); |
| 1469 |
GuiSetTimeouts(); /* We may have set some new timeouts */ |
| 1470 |
} |
| 1471 |
if (!DoneOK) { |
| 1472 |
RemovePlayerFromServer(Play); |
| 1473 |
if (IsServerShutdown()) |
| 1474 |
GuiQuitServer(); |
| 1475 |
} |
| 1476 |
return TRUE; |
| 1477 |
} |
| 1478 |
|
| 1479 |
void SocketStatus(NetworkBuffer *NetBuf, gboolean Read, gboolean Write, |
| 1480 |
gboolean Exception, gboolean CallNow) |
| 1481 |
{ |
| 1482 |
if (NetBuf->InputTag) |
| 1483 |
dp_g_source_remove(NetBuf->InputTag); |
| 1484 |
NetBuf->InputTag = 0; |
| 1485 |
if (Read || Write) { |
| 1486 |
NetBuf->InputTag = dp_g_io_add_watch(NetBuf->ioch, |
| 1487 |
(Read ? G_IO_IN : 0) | |
| 1488 |
(Write ? G_IO_OUT : 0) | |
| 1489 |
(Exception ? G_IO_ERR : 0), |
| 1490 |
GuiHandleSocket, |
| 1491 |
NetBuf->CallBackData); |
| 1492 |
} |
| 1493 |
if (CallNow) |
| 1494 |
GuiHandleSocket(NetBuf->ioch, 0, NetBuf->CallBackData); |
| 1495 |
} |
| 1496 |
|
| 1497 |
static gboolean GuiNewConnect(GIOChannel *source, GIOCondition condition, |
| 1498 |
gpointer data) |
| 1499 |
{ |
| 1500 |
Player *Play; |
| 1501 |
|
| 1502 |
if (condition & G_IO_IN) { |
| 1503 |
Play = HandleNewConnection(); |
| 1504 |
SetNetworkBufferCallBack(&Play->NetBuf, SocketStatus, (gpointer)Play); |
| 1505 |
} |
| 1506 |
return TRUE; |
| 1507 |
} |
| 1508 |
|
| 1509 |
static gboolean TriedPoliteShutdown = FALSE; |
| 1510 |
|
| 1511 |
static gint GuiRequestDelete(GtkWidget *widget, GdkEvent * event, |
| 1512 |
gpointer data) |
| 1513 |
{ |
| 1514 |
if (TriedPoliteShutdown) { |
| 1515 |
GuiQuitServer(); |
| 1516 |
} else { |
| 1517 |
TriedPoliteShutdown = TRUE; |
| 1518 |
HandleServerCommand("quit", NULL, FALSE); |
| 1519 |
if (IsServerShutdown()) |
| 1520 |
GuiQuitServer(); |
| 1521 |
} |
| 1522 |
return TRUE; /* Never allow automatic deletion - we |
| 1523 |
* handle it manually */ |
| 1524 |
} |
| 1525 |
|
| 1526 |
#ifdef CYGWIN |
| 1527 |
static HWND mainhwnd = NULL; |
| 1528 |
static BOOL systray = FALSE; |
| 1529 |
|
| 1530 |
static BOOL RegisterStatus(DWORD state) |
| 1531 |
{ |
| 1532 |
SERVICE_STATUS status; |
| 1533 |
|
| 1534 |
status.dwServiceType = SERVICE_WIN32_OWN_PROCESS; |
| 1535 |
status.dwCurrentState = state; |
| 1536 |
status.dwControlsAccepted = SERVICE_ACCEPT_STOP; |
| 1537 |
status.dwWin32ExitCode = NO_ERROR; |
| 1538 |
status.dwCheckPoint = 0; |
| 1539 |
status.dwWaitHint = 5000; |
| 1540 |
return SetServiceStatus(scHandle, &status); |
| 1541 |
} |
| 1542 |
|
| 1543 |
static VOID WINAPI ServiceHandler(DWORD control) |
| 1544 |
{ |
| 1545 |
DWORD state = SERVICE_RUNNING; |
| 1546 |
|
| 1547 |
switch (control) { |
| 1548 |
case SERVICE_CONTROL_STOP: |
| 1549 |
state = SERVICE_STOP_PENDING; |
| 1550 |
break; |
| 1551 |
} |
| 1552 |
if (!RegisterStatus(state)) { |
| 1553 |
dopelog(0, LF_SERVER, _("Failed to set NT Service status")); |
| 1554 |
return; |
| 1555 |
} |
| 1556 |
|
| 1557 |
if (mainhwnd |
| 1558 |
&& !PostMessage(mainhwnd, MYWM_SERVICE, 0, (LPARAM) control)) { |
| 1559 |
dopelog(0, LF_SERVER, _("Failed to post service notification message")); |
| 1560 |
return; |
| 1561 |
} |
| 1562 |
} |
| 1563 |
|
| 1564 |
static VOID WINAPI ServiceInit(DWORD argc, LPTSTR * argv) |
| 1565 |
{ |
| 1566 |
scHandle = RegisterServiceCtrlHandler("dopewars-server", ServiceHandler); |
| 1567 |
if (!scHandle) { |
| 1568 |
dopelog(0, LF_SERVER, _("Failed to register service handler")); |
| 1569 |
return; |
| 1570 |
} |
| 1571 |
if (!RegisterStatus(SERVICE_START_PENDING)) { |
| 1572 |
dopelog(0, LF_SERVER, _("Failed to set NT Service status")); |
| 1573 |
return; |
| 1574 |
} |
| 1575 |
|
| 1576 |
GuiServerLoop(NULL, TRUE); |
| 1577 |
|
| 1578 |
if (!RegisterStatus(SERVICE_STOPPED)) { |
| 1579 |
dopelog(0, LF_SERVER, _("Failed to set NT Service status")); |
| 1580 |
return; |
| 1581 |
} |
| 1582 |
} |
| 1583 |
|
| 1584 |
void ServiceMain(struct CMDLINE *cmdline) |
| 1585 |
{ |
| 1586 |
SERVICE_TABLE_ENTRY services[] = { |
| 1587 |
{"dopewars-server", ServiceInit}, |
| 1588 |
{NULL, NULL} |
| 1589 |
}; |
| 1590 |
|
| 1591 |
InitConfiguration(cmdline); |
| 1592 |
|
| 1593 |
if (!StartServiceCtrlDispatcher(services)) { |
| 1594 |
dopelog(0, LF_SERVER, _("Failed to start NT Service")); |
| 1595 |
} |
| 1596 |
} |
| 1597 |
|
| 1598 |
static LRESULT CALLBACK GuiServerWndProc(HWND hwnd, UINT msg, |
| 1599 |
WPARAM wparam, LPARAM lparam) |
| 1600 |
{ |
| 1601 |
if (hwnd == mainhwnd) |
| 1602 |
switch (msg) { |
| 1603 |
case MYWM_SERVICE: |
| 1604 |
if (lparam == SERVICE_CONTROL_STOP) { |
| 1605 |
RequestServerShutdown(); |
| 1606 |
} |
| 1607 |
break; |
| 1608 |
case MYWM_TASKBAR: |
| 1609 |
if ((UINT) lparam == WM_LBUTTONDOWN) |
| 1610 |
ShowWindow(mainhwnd, SW_SHOW); |
| 1611 |
break; |
| 1612 |
case WM_SYSCOMMAND: |
| 1613 |
if (wparam == SC_MINIMIZE && systray) { |
| 1614 |
ShowWindow(mainhwnd, SW_HIDE); |
| 1615 |
return TRUE; |
| 1616 |
} |
| 1617 |
break; |
| 1618 |
} |
| 1619 |
return FALSE; |
| 1620 |
} |
| 1621 |
|
| 1622 |
static void SetupTaskBarIcon(GtkWidget *widget) |
| 1623 |
{ |
| 1624 |
NOTIFYICONDATA nid; |
| 1625 |
|
| 1626 |
nid.cbSize = sizeof(NOTIFYICONDATA); |
| 1627 |
nid.uID = 1000; |
| 1628 |
if (widget && !widget->hWnd) |
| 1629 |
return; |
| 1630 |
if (!widget && !mainhwnd) |
| 1631 |
return; |
| 1632 |
|
| 1633 |
nid.hWnd = mainhwnd; |
| 1634 |
if (widget && MinToSysTray) { |
| 1635 |
nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; |
| 1636 |
nid.uCallbackMessage = MYWM_TASKBAR; |
| 1637 |
nid.hIcon = mainIcon; |
| 1638 |
strcpy(nid.szTip, "dopewars server - running"); |
| 1639 |
systray = Shell_NotifyIcon(NIM_ADD, &nid); |
| 1640 |
} else { |
| 1641 |
systray = FALSE; |
| 1642 |
Shell_NotifyIcon(NIM_DELETE, &nid); |
| 1643 |
} |
| 1644 |
} |
| 1645 |
#endif /* CYGWIN */ |
| 1646 |
|
| 1647 |
void GuiServerLoop(struct CMDLINE *cmdline, gboolean is_service) |
| 1648 |
{ |
| 1649 |
GtkWidget *window, *text, *hbox, *vbox, *entry, *label; |
| 1650 |
GIOChannel *listench; |
| 1651 |
|
| 1652 |
/* GTK+2 (and the GTK emulation code on WinNT systems) expects all |
| 1653 |
* strings to be UTF-8, so we force gettext to return all translations |
| 1654 |
* in this encoding here. */ |
| 1655 |
bind_textdomain_codeset(PACKAGE, "UTF-8"); |
| 1656 |
|
| 1657 |
Conv_SetInternalCodeset("UTF-8"); |
| 1658 |
WantUTF8Errors(TRUE); |
| 1659 |
|
| 1660 |
if (cmdline) { |
| 1661 |
InitConfiguration(cmdline); |
| 1662 |
} |
| 1663 |
|
| 1664 |
window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 1665 |
g_signal_connect(G_OBJECT(window), "delete_event", |
| 1666 |
G_CALLBACK(GuiRequestDelete), NULL); |
| 1667 |
gtk_window_set_default_size(GTK_WINDOW(window), 500, 250); |
| 1668 |
|
| 1669 |
/* Title of dopewars server window (if used) */ |
| 1670 |
gtk_window_set_title(GTK_WINDOW(window), _("dopewars server")); |
| 1671 |
|
| 1672 |
gtk_container_set_border_width(GTK_CONTAINER(window), 7); |
| 1673 |
|
| 1674 |
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 7); |
| 1675 |
TextOutput = text = gtk_scrolled_text_view_new(&hbox); |
| 1676 |
gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE); |
| 1677 |
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD); |
| 1678 |
gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0); |
| 1679 |
|
| 1680 |
hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4); |
| 1681 |
label = gtk_label_new(_("Command:")); |
| 1682 |
gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); |
| 1683 |
entry = gtk_entry_new(); |
| 1684 |
g_signal_connect(G_OBJECT(entry), "activate", |
| 1685 |
G_CALLBACK(GuiDoCommand), NULL); |
| 1686 |
gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0); |
| 1687 |
gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); |
| 1688 |
|
| 1689 |
gtk_container_add(GTK_CONTAINER(window), vbox); |
| 1690 |
gtk_widget_show_all(window); |
| 1691 |
|
| 1692 |
if (is_service) { |
| 1693 |
#ifdef CYGWIN |
| 1694 |
g_log_set_handler(NULL, G_LOG_LEVEL_CRITICAL, ServiceFailure, NULL); |
| 1695 |
#endif |
| 1696 |
} else { |
| 1697 |
g_set_print_handler(GuiServerPrintFunc); |
| 1698 |
g_log_set_handler(NULL, |
| 1699 |
LogMask() | G_LOG_LEVEL_MESSAGE | |
| 1700 |
G_LOG_LEVEL_WARNING, GuiServerLogMessage, NULL); |
| 1701 |
} |
| 1702 |
if (!StartServer()) |
| 1703 |
return; |
| 1704 |
InitMetaServer(); |
| 1705 |
|
| 1706 |
#ifdef CYGIN |
| 1707 |
listench = g_io_channel_win32_new_socket(ListenSock); |
| 1708 |
#else |
| 1709 |
listench = g_io_channel_unix_new(ListenSock); |
| 1710 |
#endif |
| 1711 |
ListenTag = dp_g_io_add_watch(listench, G_IO_IN, GuiNewConnect, NULL); |
| 1712 |
#ifdef CYGWIN |
| 1713 |
mainhwnd = window->hWnd; |
| 1714 |
SetupTaskBarIcon(window); |
| 1715 |
SetCustomWndProc(GuiServerWndProc); |
| 1716 |
if (is_service && !RegisterStatus(SERVICE_RUNNING)) { |
| 1717 |
dopelog(0, LF_SERVER, _("Failed to set NT Service status")); |
| 1718 |
return; |
| 1719 |
} |
| 1720 |
#endif |
| 1721 |
gtk_main(); |
| 1722 |
#ifdef CYGWIN |
| 1723 |
SetupTaskBarIcon(NULL); |
| 1724 |
#endif |
| 1725 |
} |
| 1726 |
#endif /* GUI_SERVER */ |
| 1727 |
|
| 1728 |
#endif /* NETWORKING */ |
| 1729 |
|
| 1730 |
/* |
| 1731 |
* Tells player "Play" that the game is over; display "Message". |
| 1732 |
*/ |
| 1733 |
void FinishGame(Player *Play, char *Message) |
| 1734 |
{ |
| 1735 |
Play->EventNum = E_FINISH; |
| 1736 |
ClientLeftServer(Play); |
| 1737 |
SendHighScores(Play, TRUE, Message); |
| 1738 |
|
| 1739 |
/* Blank the name, so that CountPlayers ignores this player */ |
| 1740 |
SetPlayerName(Play, NULL); |
| 1741 |
|
| 1742 |
/* Make sure they do actually disconnect, eventually! */ |
| 1743 |
if (ConnectTimeout) { |
| 1744 |
Play->ConnectTimeout = time(NULL) + (time_t) ConnectTimeout; |
| 1745 |
} |
| 1746 |
} |
| 1747 |
|
| 1748 |
/* |
| 1749 |
* Reads a batch of NUMHISCORE high scores into "HiScore" from "fp". |
| 1750 |
*/ |
| 1751 |
void HighScoreTypeRead(struct HISCORE *HiScore, FILE *fp) |
| 1752 |
{ |
| 1753 |
int i; |
| 1754 |
char *buf; |
| 1755 |
|
| 1756 |
for (i = 0; i < NUMHISCORE; i++) { |
| 1757 |
if (read_string(fp, &HiScore[i].Name) == EOF) |
| 1758 |
break; |
| 1759 |
read_string(fp, &HiScore[i].Time); |
| 1760 |
read_string(fp, &buf); |
| 1761 |
HiScore[i].Money = strtoprice(buf); |
| 1762 |
g_free(buf); |
| 1763 |
HiScore[i].Dead = (fgetc(fp) > 0); |
| 1764 |
} |
| 1765 |
} |
| 1766 |
|
| 1767 |
/* |
| 1768 |
* Writes out a batch of NUMHISCORE high scores from "HiScore" to "fp". |
| 1769 |
*/ |
| 1770 |
void HighScoreTypeWrite(struct HISCORE *HiScore, FILE *fp) |
| 1771 |
{ |
| 1772 |
int i; |
| 1773 |
gchar *text; |
| 1774 |
|
| 1775 |
for (i = 0; i < NUMHISCORE; i++) { |
| 1776 |
if (HiScore[i].Name) { |
| 1777 |
fwrite(HiScore[i].Name, strlen(HiScore[i].Name) + 1, 1, fp); |
| 1778 |
} else |
| 1779 |
fputc(0, fp); |
| 1780 |
if (HiScore[i].Time) { |
| 1781 |
fwrite(HiScore[i].Time, strlen(HiScore[i].Time) + 1, 1, fp); |
| 1782 |
} else |
| 1783 |
fputc(0, fp); |
| 1784 |
text = pricetostr(HiScore[i].Money); |
| 1785 |
fwrite(text, strlen(text) + 1, 1, fp); |
| 1786 |
g_free(text); |
| 1787 |
fputc(HiScore[i].Dead ? 1 : 0, fp); |
| 1788 |
} |
| 1789 |
} |
| 1790 |
|
| 1791 |
/* |
| 1792 |
* Closes the high score file opened by OpenHighScoreFile, below. |
| 1793 |
*/ |
| 1794 |
void CloseHighScoreFile() |
| 1795 |
{ |
| 1796 |
if (ScoreFP) { |
| 1797 |
fclose(ScoreFP); |
| 1798 |
} |
| 1799 |
ScoreFP = NULL; |
| 1800 |
} |
| 1801 |
|
| 1802 |
/* |
| 1803 |
* If we're running setuid/setgid, drop down to the privilege level of the |
| 1804 |
* user that started the dopewars process. |
| 1805 |
*/ |
| 1806 |
void DropPrivileges() |
| 1807 |
{ |
| 1808 |
#ifndef CYGWIN |
| 1809 |
|
| 1810 |
#ifdef HAVE_ISSETUGID |
| 1811 |
if (issetugid() == 0) return; |
| 1812 |
#endif |
| 1813 |
|
| 1814 |
/* Ignore the return from setregid; we'll check it ourselves to be sure |
| 1815 |
* (this avoids problems when running under fakeroot) */ |
| 1816 |
setregid(getgid(), getgid()); |
| 1817 |
if (getgid() != getegid()) { |
| 1818 |
perror("setregid"); |
| 1819 |
exit(EXIT_FAILURE); |
| 1820 |
} |
| 1821 |
|
| 1822 |
setreuid(getuid(), getuid()); |
| 1823 |
if (getuid() != geteuid()) { |
| 1824 |
perror("setreuid"); |
| 1825 |
exit(EXIT_FAILURE); |
| 1826 |
} |
| 1827 |
#endif |
| 1828 |
} |
| 1829 |
|
| 1830 |
static const gchar SCOREHEADER[] = "DOPEWARS SCORES V."; |
| 1831 |
static const guint SCOREHDRLEN = sizeof(SCOREHEADER) - 1; /* Don't include \0 */ |
| 1832 |
static const guint SCOREVERSION = 1; |
| 1833 |
|
| 1834 |
static gboolean HighScoreReadHeader(FILE *fp, gint *ScoreVersion) |
| 1835 |
{ |
| 1836 |
gchar *header; |
| 1837 |
|
| 1838 |
if (read_string(fp, &header) != EOF) { |
| 1839 |
if (header && strlen(header) > SCOREHDRLEN && |
| 1840 |
strncmp(header, SCOREHEADER, SCOREHDRLEN) == 0) { |
| 1841 |
if (ScoreVersion) |
| 1842 |
*ScoreVersion = atoi(header + SCOREHDRLEN); |
| 1843 |
g_free(header); |
| 1844 |
return TRUE; |
| 1845 |
} |
| 1846 |
} |
| 1847 |
g_free(header); |
| 1848 |
return FALSE; |
| 1849 |
} |
| 1850 |
|
| 1851 |
static void HighScoreWriteHeader(FILE *fp) |
| 1852 |
{ |
| 1853 |
gchar *header; |
| 1854 |
|
| 1855 |
header = g_strdup_printf("%s%d", SCOREHEADER, SCOREVERSION); |
| 1856 |
fwrite(header, strlen(header) + 1, 1, fp); |
| 1857 |
g_free(header); |
| 1858 |
} |
| 1859 |
|
| 1860 |
/* |
| 1861 |
* Converts an old format high score file to the new format. |
| 1862 |
*/ |
| 1863 |
void ConvertHighScoreFile(const gchar *convertfile) |
| 1864 |
{ |
| 1865 |
FILE *old; |
| 1866 |
gchar *BackupFile; |
| 1867 |
int ch; |
| 1868 |
struct HISCORE MultiScore[NUMHISCORE], AntiqueScore[NUMHISCORE]; |
| 1869 |
|
| 1870 |
BackupFile = g_strdup_printf("%s.bak", convertfile); |
| 1871 |
|
| 1872 |
old = fopen(convertfile, "r+"); |
| 1873 |
if (old) { |
| 1874 |
gboolean empty; |
| 1875 |
/* Only convert if the file is not empty, and does not have a header */ |
| 1876 |
rewind(old); |
| 1877 |
empty = (fgetc(old) == EOF); |
| 1878 |
rewind(old); |
| 1879 |
if (!empty && !HighScoreReadHeader(old, NULL)) { |
| 1880 |
FILE *backup = fopen(BackupFile, "w"); |
| 1881 |
if (backup) { |
| 1882 |
/* Make a backup of the old file */ |
| 1883 |
ftruncate(fileno(backup), 0); |
| 1884 |
rewind(backup); |
| 1885 |
rewind(old); |
| 1886 |
while (1) { |
| 1887 |
ch = fgetc(old); |
| 1888 |
if (ch == EOF) { |
| 1889 |
break; |
| 1890 |
} else { |
| 1891 |
fputc(ch, backup); |
| 1892 |
} |
| 1893 |
} |
| 1894 |
fclose(backup); |
| 1895 |
|
| 1896 |
/* Read in the scores without the header, and then write out with |
| 1897 |
* the header */ |
| 1898 |
if (!HighScoreRead(old, MultiScore, AntiqueScore, FALSE)) { |
| 1899 |
g_log(NULL, G_LOG_LEVEL_CRITICAL, |
| 1900 |
_("Error reading scores from %s."), convertfile); |
| 1901 |
} else { |
| 1902 |
ftruncate(fileno(old), 0); |
| 1903 |
rewind(old); |
| 1904 |
if (HighScoreWrite(old, MultiScore, AntiqueScore)) { |
| 1905 |
g_message(_("The high score file %s has been converted to the " |
| 1906 |
"new format.\nA backup of the old file has been " |
| 1907 |
"created as %s.\n"), convertfile, BackupFile); |
| 1908 |
} |
| 1909 |
} |
| 1910 |
} else { |
| 1911 |
gchar *errmsg = ErrStrFromErrno(errno); |
| 1912 |
g_log(NULL, G_LOG_LEVEL_CRITICAL, |
| 1913 |
_("Cannot create backup (%s) of the\nhigh score file: %s."), |
| 1914 |
BackupFile, errmsg); |
| 1915 |
g_free(errmsg); |
| 1916 |
} |
| 1917 |
} |
| 1918 |
fclose(old); |
| 1919 |
} else { |
| 1920 |
gchar *errmsg = ErrStrFromErrno(errno); |
| 1921 |
g_log(NULL, G_LOG_LEVEL_CRITICAL, |
| 1922 |
_("Cannot open high score file %s: %s."), |
| 1923 |
convertfile, errmsg); |
| 1924 |
g_free(errmsg); |
| 1925 |
} |
| 1926 |
|
| 1927 |
g_free(BackupFile); |
| 1928 |
} |
| 1929 |
|
| 1930 |
#ifdef CYGWIN |
| 1931 |
/* Try to open a high score file in the Win32-specific AppData directory */ |
| 1932 |
static FILE *OpenHighScoreAppData(int *error, gboolean *empty) |
| 1933 |
{ |
| 1934 |
FILE *fp = NULL; |
| 1935 |
HKEY key; |
| 1936 |
static const char *subkey = "Software\\Microsoft\\Windows\\CurrentVersion" |
| 1937 |
"\\Explorer\\Shell Folders"; |
| 1938 |
static const char *subval = "Local AppData"; |
| 1939 |
|
| 1940 |
*empty = FALSE; |
| 1941 |
|
| 1942 |
if (RegOpenKeyEx(HKEY_CURRENT_USER, subkey, 0, KEY_READ, |
| 1943 |
&key) == ERROR_SUCCESS) { |
| 1944 |
DWORD keylen, keytype; |
| 1945 |
if (RegQueryValueEx(key, subval, NULL, &keytype, NULL, |
| 1946 |
&keylen) == ERROR_SUCCESS && keytype == REG_SZ) { |
| 1947 |
char *keyval = g_malloc(keylen); |
| 1948 |
if (RegQueryValueEx(key, subval, NULL, &keytype, (LPBYTE)keyval, |
| 1949 |
&keylen) == ERROR_SUCCESS) { |
| 1950 |
GString *str = g_string_sized_new(keylen + 40); |
| 1951 |
g_string_assign(str, keyval); |
| 1952 |
g_free(keyval); |
| 1953 |
g_string_append(str, "\\dopewars"); |
| 1954 |
CreateDirectory(str->str, NULL); |
| 1955 |
g_string_append(str, "\\dopewars.sco"); |
| 1956 |
fp = fopen(str->str, "r+"); |
| 1957 |
if (!fp) { |
| 1958 |
fp = fopen(str->str, "w+"); |
| 1959 |
if (!fp) { |
| 1960 |
*error = errno; |
| 1961 |
} |
| 1962 |
*empty = TRUE; |
| 1963 |
} |
| 1964 |
g_string_free(str, 1); |
| 1965 |
} |
| 1966 |
} |
| 1967 |
RegCloseKey(key); |
| 1968 |
} |
| 1969 |
return fp; |
| 1970 |
} |
| 1971 |
#endif |
| 1972 |
|
| 1973 |
|
| 1974 |
/* State, set by OpenHighScoreFile, and later used by |
| 1975 |
* CheckHighScoreFileConfig */ |
| 1976 |
static gboolean EmptyFile; |
| 1977 |
static int OpenError; |
| 1978 |
|
| 1979 |
/* |
| 1980 |
* Opens the high score file for later use, and then drops privileges. |
| 1981 |
*/ |
| 1982 |
void OpenHighScoreFile(void) |
| 1983 |
{ |
| 1984 |
if (ScoreFP) { |
| 1985 |
return; /* If already opened, then we're done */ |
| 1986 |
} |
| 1987 |
|
| 1988 |
EmptyFile = FALSE; |
| 1989 |
OpenError = 0; |
| 1990 |
|
| 1991 |
/* Win32 gets upset if we use "a+" so we use this nasty hack instead */ |
| 1992 |
ScoreFP = fopen(HiScoreFile, "r+"); |
| 1993 |
if (!ScoreFP) { |
| 1994 |
ScoreFP = fopen(HiScoreFile, "w+"); |
| 1995 |
if (!ScoreFP) { |
| 1996 |
OpenError = errno; |
| 1997 |
} |
| 1998 |
EmptyFile = TRUE; |
| 1999 |
} |
| 2000 |
#ifdef CYGWIN |
| 2001 |
if (!ScoreFP) { |
| 2002 |
ScoreFP = OpenHighScoreAppData(&OpenError, &EmptyFile); |
| 2003 |
} |
| 2004 |
#endif |
| 2005 |
|
| 2006 |
/* Check for a 0-byte score file */ |
| 2007 |
if (ScoreFP && !EmptyFile) { |
| 2008 |
rewind(ScoreFP); |
| 2009 |
if (fgetc(ScoreFP) == EOF) { |
| 2010 |
EmptyFile = TRUE; |
| 2011 |
} |
| 2012 |
rewind(ScoreFP); |
| 2013 |
} |
| 2014 |
} |
| 2015 |
|
| 2016 |
/* |
| 2017 |
* Checks the high score file opened by OpenHighScoreFile, above. Also warns |
| 2018 |
* the user about other problems encountered during startup. Returns |
| 2019 |
* TRUE if it's valid; otherwise, returns FALSE. |
| 2020 |
*/ |
| 2021 |
gboolean CheckHighScoreFileConfig(void) |
| 2022 |
{ |
| 2023 |
|
| 2024 |
if (!ScoreFP) { |
| 2025 |
gchar *errstr = ErrStrFromErrno(OpenError); |
| 2026 |
g_log(NULL, G_LOG_LEVEL_CRITICAL, |
| 2027 |
_("Cannot open high score file %s.\n" |
| 2028 |
"(%s.) Either ensure you have permissions to access\n" |
| 2029 |
"this file and directory, or specify an alternate high score " |
| 2030 |
"file with the\n-f command line option."), |
| 2031 |
HiScoreFile, errstr); |
| 2032 |
g_free(errstr); |
| 2033 |
return FALSE; |
| 2034 |
} |
| 2035 |
|
| 2036 |
if (EmptyFile) { |
| 2037 |
HighScoreWriteHeader(ScoreFP); |
| 2038 |
fflush(ScoreFP); |
| 2039 |
} else if (!HighScoreReadHeader(ScoreFP, NULL)) { |
| 2040 |
g_log(NULL, G_LOG_LEVEL_CRITICAL, |
| 2041 |
_("%s does not appear to be a valid\n" |
| 2042 |
"high score file - please check it. If it is a high score file\n" |
| 2043 |
"from an older version of dopewars, then first convert it to the\n" |
| 2044 |
"new format by running \"dopewars -C %s\"\n" |
| 2045 |
"from the command line."), HiScoreFile, HiScoreFile); |
| 2046 |
return FALSE; |
| 2047 |
} |
| 2048 |
|
| 2049 |
if (ConfigErrors) { |
| 2050 |
#ifdef CYGWIN |
| 2051 |
g_warning(_("Errors were encountered during the reading of the " |
| 2052 |
"configuration file.\nAs as result, some settings may not " |
| 2053 |
"work as expected. Please consult the\n" |
| 2054 |
"file \"dopewars-log.txt\" for further details.")); |
| 2055 |
#else |
| 2056 |
g_warning(_("Errors were encountered during the reading of the " |
| 2057 |
"configuration\nfile. As a result, some settings may not " |
| 2058 |
"work as expected. Please see the\nmessages on standard " |
| 2059 |
"output for further details.")); |
| 2060 |
#endif |
| 2061 |
} |
| 2062 |
|
| 2063 |
return TRUE; |
| 2064 |
} |
| 2065 |
|
| 2066 |
/* |
| 2067 |
* Reads all the high scores into MultiScore and AntiqueScore (antique |
| 2068 |
* mode scores). If ReadHeader is TRUE, read the high score file header |
| 2069 |
* first. Returns TRUE on success, FALSE on failure. |
| 2070 |
*/ |
| 2071 |
gboolean HighScoreRead(FILE *fp, struct HISCORE *MultiScore, |
| 2072 |
struct HISCORE *AntiqueScore, gboolean ReadHeader) |
| 2073 |
{ |
| 2074 |
gint ScoreVersion = 0; |
| 2075 |
memset(MultiScore, 0, sizeof(struct HISCORE) * NUMHISCORE); |
| 2076 |
memset(AntiqueScore, 0, sizeof(struct HISCORE) * NUMHISCORE); |
| 2077 |
if (fp && ReadLock(fp) == 0) { |
| 2078 |
rewind(fp); |
| 2079 |
if (ReadHeader && !HighScoreReadHeader(fp, &ScoreVersion)) { |
| 2080 |
ReleaseLock(fp); |
| 2081 |
return FALSE; |
| 2082 |
} |
| 2083 |
HighScoreTypeRead(AntiqueScore, fp); |
| 2084 |
HighScoreTypeRead(MultiScore, fp); |
| 2085 |
ReleaseLock(fp); |
| 2086 |
} else |
| 2087 |
return FALSE; |
| 2088 |
return TRUE; |
| 2089 |
} |
| 2090 |
|
| 2091 |
/* |
| 2092 |
* Writes out all the high scores from MultiScore and AntiqueScore; returns |
| 2093 |
* TRUE on success, FALSE on failure. |
| 2094 |
*/ |
| 2095 |
gboolean HighScoreWrite(FILE *fp, struct HISCORE *MultiScore, |
| 2096 |
struct HISCORE *AntiqueScore) |
| 2097 |
{ |
| 2098 |
if (fp && WriteLock(fp) == 0) { |
| 2099 |
ftruncate(fileno(fp), 0); |
| 2100 |
rewind(fp); |
| 2101 |
HighScoreWriteHeader(fp); |
| 2102 |
HighScoreTypeWrite(AntiqueScore, fp); |
| 2103 |
HighScoreTypeWrite(MultiScore, fp); |
| 2104 |
ReleaseLock(fp); |
| 2105 |
fflush(fp); |
| 2106 |
} else |
| 2107 |
return 0; |
| 2108 |
return 1; |
| 2109 |
} |
| 2110 |
|
| 2111 |
/* |
| 2112 |
* Adds "Play" to the high score list if necessary, and then sends the |
| 2113 |
* scores over the network to "Play". |
| 2114 |
* If "EndGame" is TRUE, add the current score if it's high enough and |
| 2115 |
* display an explanatory message. "Message" is tacked onto the start |
| 2116 |
* if it's non-NULL. The client is then informed that the game's over. |
| 2117 |
*/ |
| 2118 |
void SendHighScores(Player *Play, gboolean EndGame, char *Message) |
| 2119 |
{ |
| 2120 |
struct HISCORE MultiScore[NUMHISCORE], AntiqueScore[NUMHISCORE], Score; |
| 2121 |
struct HISCORE *HiScore; |
| 2122 |
struct tm *timep; |
| 2123 |
#ifdef HAVE_GMTIME_R |
| 2124 |
struct tm tmbuf; |
| 2125 |
#endif |
| 2126 |
|
| 2127 |
time_t tim; |
| 2128 |
GString *text; |
| 2129 |
int i, j, InList = -1; |
| 2130 |
|
| 2131 |
text = g_string_new(""); |
| 2132 |
if (!HighScoreRead(ScoreFP, MultiScore, AntiqueScore, TRUE)) { |
| 2133 |
g_warning(_("Unable to read high score file %s"), HiScoreFile); |
| 2134 |
} |
| 2135 |
if (Message) { |
| 2136 |
g_string_assign(text, Message); |
| 2137 |
if (strlen(text->str) > 0) |
| 2138 |
g_string_append_c(text, '^'); |
| 2139 |
} |
| 2140 |
if (WantAntique) |
| 2141 |
HiScore = AntiqueScore; |
| 2142 |
else |
| 2143 |
HiScore = MultiScore; |
| 2144 |
if (EndGame) { |
| 2145 |
Score.Money = Play->Cash + Play->Bank - Play->Debt; |
| 2146 |
Score.Name = g_strdup(GetPlayerName(Play)); |
| 2147 |
Score.Dead = (Play->Health == 0); |
| 2148 |
tim = time(NULL); |
| 2149 |
#ifdef HAVE_GMTIME_R |
| 2150 |
timep = gmtime_r(&tim, &tmbuf); |
| 2151 |
#else |
| 2152 |
timep = gmtime(&tim); |
| 2153 |
#endif |
| 2154 |
Score.Time = g_new(char, 80); /* Yuck! */ |
| 2155 |
|
| 2156 |
strftime(Score.Time, 80, "%d-%m-%Y", timep); |
| 2157 |
Score.Time[79] = '\0'; |
| 2158 |
for (i = 0; i < NUMHISCORE; i++) { |
| 2159 |
if (InList == -1 && (Score.Money > HiScore[i].Money || |
| 2160 |
!HiScore[i].Time || HiScore[i].Time[0] == 0)) { |
| 2161 |
InList = i; |
| 2162 |
g_string_append(text, |
| 2163 |
_("Congratulations! You made the high scores!")); |
| 2164 |
SendPrintMessage(NULL, C_NONE, Play, text->str); |
| 2165 |
g_free(HiScore[NUMHISCORE - 1].Name); |
| 2166 |
g_free(HiScore[NUMHISCORE - 1].Time); |
| 2167 |
for (j = NUMHISCORE - 1; j > i; j--) { |
| 2168 |
memcpy(&HiScore[j], &HiScore[j - 1], sizeof(struct HISCORE)); |
| 2169 |
} |
| 2170 |
memcpy(&HiScore[i], &Score, sizeof(struct HISCORE)); |
| 2171 |
break; |
| 2172 |
} |
| 2173 |
} |
| 2174 |
if (InList == -1) { |
| 2175 |
g_string_append(text, |
| 2176 |
_("You didn't even make the high score table...")); |
| 2177 |
SendPrintMessage(NULL, C_NONE, Play, text->str); |
| 2178 |
} |
| 2179 |
} |
| 2180 |
SendServerMessage(NULL, C_NONE, C_STARTHISCORE, Play, NULL); |
| 2181 |
|
| 2182 |
j = 0; |
| 2183 |
for (i = 0; i < NUMHISCORE; i++) { |
| 2184 |
if (SendSingleHighScore(Play, &HiScore[i], j, InList == i)) |
| 2185 |
j++; |
| 2186 |
} |
| 2187 |
if (InList == -1 && EndGame) { |
| 2188 |
SendSingleHighScore(Play, &Score, j, TRUE); |
| 2189 |
g_free(Score.Name); |
| 2190 |
g_free(Score.Time); |
| 2191 |
} |
| 2192 |
SendServerMessage(NULL, C_NONE, C_ENDHISCORE, Play, |
| 2193 |
EndGame ? "end" : NULL); |
| 2194 |
if (!EndGame) |
| 2195 |
SendDrugsHere(Play, FALSE); |
| 2196 |
if (EndGame && !HighScoreWrite(ScoreFP, MultiScore, AntiqueScore)) { |
| 2197 |
g_warning(_("Unable to write high score file %s"), HiScoreFile); |
| 2198 |
} |
| 2199 |
for (i = 0; i < NUMHISCORE; i++) { |
| 2200 |
g_free(MultiScore[i].Name); |
| 2201 |
g_free(MultiScore[i].Time); |
| 2202 |
g_free(AntiqueScore[i].Name); |
| 2203 |
g_free(AntiqueScore[i].Time); |
| 2204 |
} |
| 2205 |
g_string_free(text, TRUE); |
| 2206 |
} |
| 2207 |
|
| 2208 |
/* |
| 2209 |
* Sends a single high score in "Score" with position "ind" to player |
| 2210 |
* "Play". If Bold is TRUE, instructs the client to display the score in |
| 2211 |
* bold text. |
| 2212 |
*/ |
| 2213 |
int SendSingleHighScore(Player *Play, struct HISCORE *Score, |
| 2214 |
int ind, gboolean Bold) |
| 2215 |
{ |
| 2216 |
gchar *Data, *prstr; |
| 2217 |
|
| 2218 |
if (!Score->Time || Score->Time[0] == 0) |
| 2219 |
return 0; |
| 2220 |
Data = g_strdup_printf("%d^%c%c%18s %-14s %-34s %8s%c", ind, |
| 2221 |
Bold ? 'B' : 'N', Bold ? '>' : ' ', |
| 2222 |
prstr = FormatPrice(Score->Money), |
| 2223 |
Score->Time, Score->Name, |
| 2224 |
Score->Dead ? _("(R.I.P.)") : "", |
| 2225 |
Bold ? '<' : ' '); |
| 2226 |
SendServerMessage(NULL, C_NONE, C_HISCORE, Play, Data); |
| 2227 |
g_free(prstr); |
| 2228 |
g_free(Data); |
| 2229 |
return 1; |
| 2230 |
} |
| 2231 |
|
| 2232 |
/* |
| 2233 |
* In order for the server to keep track of the state of each client, each |
| 2234 |
* client's state is identified by its EventNum data member. So, for example, |
| 2235 |
* there is a state for fighting the cops, a state for going to the bank, and |
| 2236 |
* so on. This function instructs client player "To" to carry out the actions |
| 2237 |
* expected of it in its current state. It is the client's responsibility to |
| 2238 |
* ensure that it carries out the correct actions to advance itself to the |
| 2239 |
* "next" state; if it fails in this duty it will hang! |
| 2240 |
*/ |
| 2241 |
void SendEvent(Player *To) |
| 2242 |
{ |
| 2243 |
price_t Money; |
| 2244 |
int i, j; |
| 2245 |
gchar *text; |
| 2246 |
Player *Play; |
| 2247 |
GSList *list; |
| 2248 |
|
| 2249 |
if (!To) |
| 2250 |
return; |
| 2251 |
if (To->EventNum == E_MAX) |
| 2252 |
To->EventNum = E_NONE; |
| 2253 |
if (To->EventNum == E_NONE || To->EventNum >= E_OUTOFSYNC) |
| 2254 |
return; |
| 2255 |
Money = To->Cash + To->Bank - To->Debt; |
| 2256 |
|
| 2257 |
ClearPrices(To); |
| 2258 |
|
| 2259 |
while (To->EventNum < E_MAX) { |
| 2260 |
switch (To->EventNum) { |
| 2261 |
case E_SUBWAY: |
| 2262 |
SendServerMessage(NULL, C_NONE, C_SUBWAYFLASH, To, NULL); |
| 2263 |
break; |
| 2264 |
case E_OFFOBJECT: |
| 2265 |
To->OnBehalfOf = NULL; |
| 2266 |
for (i = 0; i < To->TipList.Number; i++) { |
| 2267 |
dopelog(3, LF_SERVER, _("%s: Tipoff from %s"), GetPlayerName(To), |
| 2268 |
GetPlayerName(To->TipList.Data[i].Play)); |
| 2269 |
To->OnBehalfOf = To->TipList.Data[i].Play; |
| 2270 |
SendCopOffer(To, FORCECOPS); |
| 2271 |
return; |
| 2272 |
} |
| 2273 |
for (i = 0; i < To->SpyList.Number; i++) { |
| 2274 |
if (To->SpyList.Data[i].Turns < 0) { |
| 2275 |
dopelog(3, LF_SERVER, _("%s: Spy offered by %s"), GetPlayerName(To), |
| 2276 |
GetPlayerName(To->SpyList.Data[i].Play)); |
| 2277 |
To->OnBehalfOf = To->SpyList.Data[i].Play; |
| 2278 |
SendCopOffer(To, FORCEBITCH); |
| 2279 |
return; |
| 2280 |
} |
| 2281 |
To->SpyList.Data[i].Turns++; |
| 2282 |
if (To->SpyList.Data[i].Turns > 3 && |
| 2283 |
brandom(0, 100) < 10 + To->SpyList.Data[i].Turns) { |
| 2284 |
if (TotalGunsCarried(To) > 0) |
| 2285 |
j = brandom(0, NUMDISCOVER); |
| 2286 |
else |
| 2287 |
j = brandom(0, NUMDISCOVER - 1); |
| 2288 |
text = |
| 2289 |
dpg_strdup_printf(_("One of your %tde was spying for %s." |
| 2290 |
"^The spy %s!"), Names.Bitches, |
| 2291 |
GetPlayerName(To->SpyList.Data[i].Play), |
| 2292 |
_(Discover[j])); |
| 2293 |
if (j != DEFECT) |
| 2294 |
LoseBitch(To, NULL, NULL); |
| 2295 |
SendPlayerData(To); |
| 2296 |
SendPrintMessage(NULL, C_NONE, To, text); |
| 2297 |
g_free(text); |
| 2298 |
text = g_strdup_printf(_("Your spy working with %s has " |
| 2299 |
"been discovered!^The spy %s!"), |
| 2300 |
GetPlayerName(To), _(Discover[j])); |
| 2301 |
if (j == ESCAPE) |
| 2302 |
GainBitch(To->SpyList.Data[i].Play); |
| 2303 |
To->SpyList.Data[i].Play->Flags &= ~SPYINGON; |
| 2304 |
SendPlayerData(To->SpyList.Data[i].Play); |
| 2305 |
SendPrintMessage(NULL, C_NONE, To->SpyList.Data[i].Play, text); |
| 2306 |
g_free(text); |
| 2307 |
RemoveListEntry(&(To->SpyList), i); |
| 2308 |
i--; |
| 2309 |
} |
| 2310 |
} |
| 2311 |
if (Money > 3000000) |
| 2312 |
i = 130; |
| 2313 |
else if (Money > 1000000) |
| 2314 |
i = 115; |
| 2315 |
else |
| 2316 |
i = 100; |
| 2317 |
if (brandom(0, i) > 75) { |
| 2318 |
if (SendCopOffer(To, NOFORCE)) |
| 2319 |
return; |
| 2320 |
} |
| 2321 |
break; |
| 2322 |
case E_SAYING: |
| 2323 |
if (!Sanitized && brandom(0, 100) < 15 |
| 2324 |
&& (NumSubway > 0 || NumPlaying > 0)) { |
| 2325 |
int subwaychance = 50; |
| 2326 |
|
| 2327 |
if (NumSubway == 0) |
| 2328 |
subwaychance = 0; |
| 2329 |
if (NumPlaying == 0) |
| 2330 |
subwaychance = 100; |
| 2331 |
if (brandom(0, 100) < subwaychance) { |
| 2332 |
text = g_strdup_printf(_("The lady next to you on the subway " |
| 2333 |
"said,^ \"%s\"%s"), |
| 2334 |
SubwaySaying[brandom(0, NumSubway)], |
| 2335 |
brandom(0, 100) < 30 ? |
| 2336 |
_("^ (at least, you -think- that's " |
| 2337 |
"what she said)") : ""); |
| 2338 |
} else { |
| 2339 |
text = g_strdup_printf(_("You hear someone playing %s"), |
| 2340 |
Playing[brandom(0, NumPlaying)]); |
| 2341 |
} |
| 2342 |
SendPrintMessage(NULL, C_NONE, To, text); |
| 2343 |
g_free(text); |
| 2344 |
} |
| 2345 |
break; |
| 2346 |
case E_LOANSHARK: |
| 2347 |
if (To->IsAt + 1 == LoanSharkLoc && To->Debt > 0) { |
| 2348 |
text = dpg_strdup_printf(_("YN^Would you like to visit %tde?"), |
| 2349 |
Names.LoanSharkName); |
| 2350 |
SendQuestion(NULL, C_ASKLOAN, To, text); |
| 2351 |
g_free(text); |
| 2352 |
return; |
| 2353 |
} |
| 2354 |
break; |
| 2355 |
case E_BANK: |
| 2356 |
if (To->IsAt + 1 == BankLoc) { |
| 2357 |
text = dpg_strdup_printf(_("YN^Would you like to visit %tde?"), |
| 2358 |
Names.BankName); |
| 2359 |
SendQuestion(NULL, C_ASKBANK, To, text); |
| 2360 |
g_free(text); |
| 2361 |
return; |
| 2362 |
} |
| 2363 |
break; |
| 2364 |
case E_GUNSHOP: |
| 2365 |
if (To->IsAt + 1 == GunShopLoc && !Sanitized && NumGun > 0) { |
| 2366 |
text = dpg_strdup_printf(_("YN^Would you like to visit %tde?"), |
| 2367 |
Names.GunShopName); |
| 2368 |
SendQuestion(NULL, C_ASKGUNSHOP, To, text); |
| 2369 |
g_free(text); |
| 2370 |
return; |
| 2371 |
} |
| 2372 |
break; |
| 2373 |
case E_ROUGHPUB: |
| 2374 |
if (To->IsAt + 1 == RoughPubLoc) { |
| 2375 |
text = dpg_strdup_printf(_("YN^Would you like to visit %tde?"), |
| 2376 |
Names.RoughPubName); |
| 2377 |
SendQuestion(NULL, C_ASKPUB, To, text); |
| 2378 |
g_free(text); |
| 2379 |
return; |
| 2380 |
} |
| 2381 |
break; |
| 2382 |
case E_HIREBITCH: |
| 2383 |
if (To->IsAt + 1 == RoughPubLoc) { |
| 2384 |
To->Bitches.Price = prandom(Bitch.MinPrice, Bitch.MaxPrice); |
| 2385 |
text = |
| 2386 |
dpg_strdup_printf(_ |
| 2387 |
("YN^^Would you like to hire a %tde for %P?"), |
| 2388 |
Names.Bitch, To->Bitches.Price); |
| 2389 |
SendQuestion(NULL, C_ASKBITCH, To, text); |
| 2390 |
g_free(text); |
| 2391 |
return; |
| 2392 |
} |
| 2393 |
break; |
| 2394 |
case E_ARRIVE: |
| 2395 |
for (list = FirstServer; list; list = g_slist_next(list)) { |
| 2396 |
Play = (Player *)list->data; |
| 2397 |
if (IsConnectedPlayer(Play) && Play != To |
| 2398 |
&& NumGun > 0 && Play->IsAt == To->IsAt |
| 2399 |
&& Play->EventNum == E_NONE && TotalGunsCarried(To) > 0) { |
| 2400 |
text = g_strdup_printf(_("%s^%s is already here!^" |
| 2401 |
"Do you Attack, or Evade?"), |
| 2402 |
attackquestiontr, |
| 2403 |
GetPlayerName(Play)); |
| 2404 |
/* Steal this to keep track of the potential defender */ |
| 2405 |
To->OnBehalfOf = Play; |
| 2406 |
|
| 2407 |
SendDrugsHere(To, TRUE); |
| 2408 |
SendQuestion(NULL, C_MEETPLAYER, To, text); |
| 2409 |
g_free(text); |
| 2410 |
return; |
| 2411 |
} |
| 2412 |
} |
| 2413 |
SendDrugsHere(To, TRUE); |
| 2414 |
break; |
| 2415 |
default: |
| 2416 |
break; |
| 2417 |
} |
| 2418 |
To->EventNum++; |
| 2419 |
} |
| 2420 |
if (To->EventNum >= E_MAX) |
| 2421 |
To->EventNum = E_NONE; |
| 2422 |
} |
| 2423 |
|
| 2424 |
/* |
| 2425 |
* In response to client player "To" being in state E_OFFOBJECT, |
| 2426 |
* randomly engages the client in combat with the cops or offers |
| 2427 |
* other random events. Returns 0 if the client should then be |
| 2428 |
* advanced to the next state, 1 otherwise (i.e. if there are |
| 2429 |
* questions pending which the client must answer first) |
| 2430 |
* If Force==FORCECOPS, engage in combat with the cops for certain |
| 2431 |
* If Force==FORCEBITCH, offer the client a bitch for certain |
| 2432 |
*/ |
| 2433 |
int SendCopOffer(Player *To, OfferForce Force) |
| 2434 |
{ |
| 2435 |
int i; |
| 2436 |
|
| 2437 |
/* The cops are more likely to attack in locations with higher police |
| 2438 |
* presence ratings */ |
| 2439 |
i = brandom(0, 80 + Location[To->IsAt].PolicePresence); |
| 2440 |
|
| 2441 |
if (Force == FORCECOPS) |
| 2442 |
i = 100; |
| 2443 |
else if (Force == FORCEBITCH) |
| 2444 |
i = 0; |
| 2445 |
else |
| 2446 |
To->OnBehalfOf = NULL; |
| 2447 |
if (i < 33) { |
| 2448 |
return (OfferObject(To, Force == FORCEBITCH)); |
| 2449 |
} else if (i < 50) { |
| 2450 |
return (RandomOffer(To)); |
| 2451 |
} else if (Sanitized || NumCop == 0 || NumGun == 0) { |
| 2452 |
return 0; |
| 2453 |
} else { |
| 2454 |
CopsAttackPlayer(To); |
| 2455 |
return 1; |
| 2456 |
} |
| 2457 |
return 1; |
| 2458 |
} |
| 2459 |
|
| 2460 |
/* |
| 2461 |
* Has the cops attack player "Play". |
| 2462 |
*/ |
| 2463 |
void CopsAttackPlayer(Player *Play) |
| 2464 |
{ |
| 2465 |
Player *Cops; |
| 2466 |
gint CopIndex, NumDeputy, GunIndex; |
| 2467 |
|
| 2468 |
if (NumCop == 0 || NumGun == 0) { |
| 2469 |
g_warning(_("No cops or guns!")); |
| 2470 |
return; |
| 2471 |
} |
| 2472 |
|
| 2473 |
CopIndex = 1 - Play->CopIndex; |
| 2474 |
if (CopIndex < 0) { |
| 2475 |
g_warning(_("Cops cannot attack other cops!")); |
| 2476 |
return; |
| 2477 |
} |
| 2478 |
if (CopIndex > NumCop) |
| 2479 |
CopIndex = NumCop; |
| 2480 |
Cops = g_new(Player, 1); |
| 2481 |
|
| 2482 |
FirstServer = AddPlayer(0, Cops, FirstServer); |
| 2483 |
SetPlayerName(Cops, Cop[CopIndex - 1].Name); |
| 2484 |
Cops->CopIndex = CopIndex; |
| 2485 |
Cops->Cash = brandom(100, 2000); |
| 2486 |
Cops->Debt = Cops->Bank = 0; |
| 2487 |
|
| 2488 |
NumDeputy = brandom(Cop[CopIndex - 1].MinDeputies, |
| 2489 |
Cop[CopIndex - 1].MaxDeputies); |
| 2490 |
Cops->Bitches.Carried = NumDeputy; |
| 2491 |
GunIndex = Cop[CopIndex - 1].GunIndex; |
| 2492 |
if (GunIndex >= NumGun) |
| 2493 |
GunIndex = NumGun - 1; |
| 2494 |
Cops->Guns[GunIndex].Carried = |
| 2495 |
(NumDeputy * Cop[CopIndex - 1].DeputyGun) + Cop[CopIndex - 1].CopGun; |
| 2496 |
Cops->Health = 100; |
| 2497 |
|
| 2498 |
Play->EventNum++; |
| 2499 |
AttackPlayer(Cops, Play); |
| 2500 |
} |
| 2501 |
|
| 2502 |
/* |
| 2503 |
* Starts combat between player "Play" and player "Attacked"; if |
| 2504 |
* either player is currently engaged in combat, add the other |
| 2505 |
* player to the existing combat. If neither player is currently |
| 2506 |
* fighting, start a new combat between them. Either player can be |
| 2507 |
* the cops. |
| 2508 |
*/ |
| 2509 |
void AttackPlayer(Player *Play, Player *Attacked) |
| 2510 |
{ |
| 2511 |
GPtrArray *FightArray; |
| 2512 |
|
| 2513 |
g_assert(Play && Attacked); |
| 2514 |
|
| 2515 |
if (Play->FightArray && Attacked->FightArray) { |
| 2516 |
if (Play->FightArray == Attacked->FightArray) { |
| 2517 |
g_error(_("Players are already in a fight!")); |
| 2518 |
} else { |
| 2519 |
g_error(_("Players are already in separate fights!")); |
| 2520 |
} |
| 2521 |
return; |
| 2522 |
} |
| 2523 |
if (NumGun == 0) { |
| 2524 |
g_error(_("Cannot start fight - no guns to use!")); |
| 2525 |
return; |
| 2526 |
} |
| 2527 |
|
| 2528 |
if (!Play->FightArray && !Attacked->FightArray) { |
| 2529 |
FightArray = g_ptr_array_new(); |
| 2530 |
} else { |
| 2531 |
FightArray = |
| 2532 |
Play->FightArray ? Play->FightArray : Attacked->FightArray; |
| 2533 |
} |
| 2534 |
|
| 2535 |
if (!Play->FightArray) { |
| 2536 |
Play->ResyncNum = Play->EventNum; |
| 2537 |
g_ptr_array_add(FightArray, Play); |
| 2538 |
} |
| 2539 |
if (!Attacked->FightArray) { |
| 2540 |
Attacked->ResyncNum = Attacked->EventNum; |
| 2541 |
g_ptr_array_add(FightArray, Attacked); |
| 2542 |
} |
| 2543 |
Play->FightArray = Attacked->FightArray = FightArray; |
| 2544 |
Play->EventNum = Attacked->EventNum = E_FIGHT; |
| 2545 |
|
| 2546 |
Play->Attacking = Attacked; |
| 2547 |
|
| 2548 |
SendFightMessage(Attacked, Play, 0, F_ARRIVED, (price_t)0, TRUE, NULL); |
| 2549 |
|
| 2550 |
Fire(Play); |
| 2551 |
} |
| 2552 |
|
| 2553 |
/* |
| 2554 |
* Returns TRUE if player "Other" is not allied with player "Play". |
| 2555 |
*/ |
| 2556 |
gboolean IsOpponent(Player *Play, Player *Other) |
| 2557 |
{ |
| 2558 |
return TRUE; |
| 2559 |
} |
| 2560 |
|
| 2561 |
void HandleDamage(Player *Defend, Player *Attack, int Damage, |
| 2562 |
int *BitchesKilled, price_t *Loot) |
| 2563 |
{ |
| 2564 |
Inventory *Guns, *Drugs; |
| 2565 |
price_t Bounty; |
| 2566 |
|
| 2567 |
Guns = (Inventory *)g_malloc0(sizeof(Inventory) * NumGun); |
| 2568 |
Drugs = (Inventory *)g_malloc0(sizeof(Inventory) * NumDrug); |
| 2569 |
ClearInventory(Guns, Drugs); |
| 2570 |
|
| 2571 |
Bounty = 0; |
| 2572 |
if (Defend->Health <= Damage && Defend->Bitches.Carried == 0) { |
| 2573 |
Bounty = Defend->Cash + Defend->Bank - Defend->Debt; |
| 2574 |
AddInventory(Guns, Defend->Guns, NumGun); |
| 2575 |
AddInventory(Drugs, Defend->Drugs, NumDrug); |
| 2576 |
Defend->Health = 0; |
| 2577 |
} else if (Defend->Bitches.Carried > 0 && Defend->Health <= Damage) { |
| 2578 |
if (IsCop(Defend)) |
| 2579 |
LoseBitch(Defend, NULL, NULL); |
| 2580 |
else |
| 2581 |
LoseBitch(Defend, Guns, Drugs); |
| 2582 |
Defend->Health = 100; |
| 2583 |
*BitchesKilled = 1; |
| 2584 |
} else { |
| 2585 |
Defend->Health -= Damage; |
| 2586 |
} |
| 2587 |
if (IsCop(Attack)) { /* Don't let cops loot players */ |
| 2588 |
ClearInventory(Guns, Drugs); |
| 2589 |
} else { |
| 2590 |
TruncateInventoryFor(Guns, Drugs, Attack); |
| 2591 |
} |
| 2592 |
SendPlayerData(Defend); |
| 2593 |
if (Bounty < 0) |
| 2594 |
Bounty = 0; |
| 2595 |
if (!IsInventoryClear(Guns, Drugs)) { |
| 2596 |
AddInventory(Attack->Guns, Guns, NumGun); |
| 2597 |
AddInventory(Attack->Drugs, Drugs, NumDrug); |
| 2598 |
ChangeSpaceForInventory(Guns, Drugs, Attack); |
| 2599 |
} |
| 2600 |
Attack->Cash += Bounty; |
| 2601 |
if (Bounty > 0 || !IsInventoryClear(Guns, Drugs)) { |
| 2602 |
if (Bounty > 0) |
| 2603 |
*Loot = Bounty; |
| 2604 |
else |
| 2605 |
*Loot = -1; |
| 2606 |
SendPlayerData(Attack); |
| 2607 |
} |
| 2608 |
g_free(Guns); |
| 2609 |
g_free(Drugs); |
| 2610 |
} |
| 2611 |
|
| 2612 |
void GetFightRatings(Player *Attack, Player *Defend, |
| 2613 |
int *AttackRating, int *DefendRating) |
| 2614 |
{ |
| 2615 |
int i; |
| 2616 |
|
| 2617 |
/* Base values */ |
| 2618 |
*AttackRating = 80; |
| 2619 |
*DefendRating = 100; |
| 2620 |
|
| 2621 |
for (i = 0; i < NumGun; i++) { |
| 2622 |
*AttackRating += Gun[i].Damage * Attack->Guns[i].Carried; |
| 2623 |
} |
| 2624 |
if (IsCop(Attack)) |
| 2625 |
*AttackRating -= Cop[Attack->CopIndex - 1].AttackPenalty; |
| 2626 |
|
| 2627 |
*DefendRating -= 5 * Defend->Bitches.Carried; |
| 2628 |
if (IsCop(Defend)) |
| 2629 |
*DefendRating -= Cop[Defend->CopIndex - 1].DefendPenalty; |
| 2630 |
|
| 2631 |
*DefendRating = MAX(*DefendRating, 10); |
| 2632 |
*AttackRating = MAX(*AttackRating, 10); |
| 2633 |
} |
| 2634 |
|
| 2635 |
void AllowNextShooter(Player *Play) |
| 2636 |
{ |
| 2637 |
Player *NextShooter; |
| 2638 |
|
| 2639 |
if (FightTimeout) { |
| 2640 |
NextShooter = GetNextShooter(Play); |
| 2641 |
if (NextShooter && !CanPlayerFire(NextShooter)) { |
| 2642 |
NextShooter->FightTimeout = 0; |
| 2643 |
} |
| 2644 |
} |
| 2645 |
} |
| 2646 |
|
| 2647 |
void DoReturnFire(Player *Play) |
| 2648 |
{ |
| 2649 |
guint ArrayInd; |
| 2650 |
Player *Defend; |
| 2651 |
|
| 2652 |
if (!Play || !Play->FightArray) |
| 2653 |
return; |
| 2654 |
|
| 2655 |
if (FightTimeout != 0 || !IsCop(Play)) { |
| 2656 |
for (ArrayInd = 0; |
| 2657 |
Play->FightArray && ArrayInd < Play->FightArray->len; |
| 2658 |
ArrayInd++) { |
| 2659 |
Defend = (Player *)g_ptr_array_index(Play->FightArray, ArrayInd); |
| 2660 |
if (IsCop(Defend) && CanPlayerFire(Defend)) |
| 2661 |
Fire(Defend); |
| 2662 |
} |
| 2663 |
} |
| 2664 |
} |
| 2665 |
|
| 2666 |
/* |
| 2667 |
* Puts the given player into the "fight ended" state. |
| 2668 |
*/ |
| 2669 |
static void WaitForFightDone(Player *Play) |
| 2670 |
{ |
| 2671 |
if (HaveAbility(Play, A_DONEFIGHT)) { |
| 2672 |
Play->EventNum = E_WAITDONE; |
| 2673 |
} else { |
| 2674 |
Play->EventNum = Play->ResyncNum; |
| 2675 |
SendEvent(Play); |
| 2676 |
} |
| 2677 |
} |
| 2678 |
|
| 2679 |
/* |
| 2680 |
* Withdraws player "Play" from combat, and levies any penalties on |
| 2681 |
* the player for this cowardly act, if applicable. If "ToLocation" |
| 2682 |
* is >=0, then it identifies the location that the player is |
| 2683 |
* trying to run to. |
| 2684 |
*/ |
| 2685 |
void RunFromCombat(Player *Play, int ToLocation) |
| 2686 |
{ |
| 2687 |
int EscapeProb, RandNum; |
| 2688 |
guint ArrayInd; |
| 2689 |
gboolean FightingCop = FALSE; |
| 2690 |
Player *Defend; |
| 2691 |
char BackupAt; |
| 2692 |
|
| 2693 |
if (!Play || !Play->FightArray) |
| 2694 |
return; |
| 2695 |
|
| 2696 |
EscapeProb = 60; |
| 2697 |
|
| 2698 |
/* Penalise players that are attacking others */ |
| 2699 |
if (Play->Attacking) |
| 2700 |
EscapeProb /= 2; |
| 2701 |
|
| 2702 |
RandNum = brandom(0, 100); |
| 2703 |
|
| 2704 |
if (RandNum < EscapeProb) { |
| 2705 |
if (!IsCop(Play) && brandom(0, 100) < 30) { |
| 2706 |
for (ArrayInd = 0; ArrayInd < Play->FightArray->len; ArrayInd++) { |
| 2707 |
Defend = (Player *)g_ptr_array_index(Play->FightArray, ArrayInd); |
| 2708 |
if (IsCop(Defend)) { |
| 2709 |
FightingCop = TRUE; |
| 2710 |
break; |
| 2711 |
} |
| 2712 |
} |
| 2713 |
if (FightingCop) |
| 2714 |
Play->CopIndex--; |
| 2715 |
} |
| 2716 |
BackupAt = Play->IsAt; |
| 2717 |
Play->IsAt = ToLocation; |
| 2718 |
WithdrawFromCombat(Play); |
| 2719 |
Play->IsAt = BackupAt; |
| 2720 |
WaitForFightDone(Play); |
| 2721 |
} else { |
| 2722 |
SendFightMessage(Play, NULL, 0, F_FAILFLEE, (price_t)0, TRUE, NULL); |
| 2723 |
AllowNextShooter(Play); |
| 2724 |
if (FightTimeout) |
| 2725 |
SetFightTimeout(Play); |
| 2726 |
DoReturnFire(Play); |
| 2727 |
} |
| 2728 |
} |
| 2729 |
|
| 2730 |
void CheckForKilledPlayers(Player *Play) |
| 2731 |
{ |
| 2732 |
Player *Defend; |
| 2733 |
guint ArrayInd; |
| 2734 |
GPtrArray *KilledPlayers; |
| 2735 |
|
| 2736 |
KilledPlayers = g_ptr_array_new(); |
| 2737 |
for (ArrayInd = 0; ArrayInd < Play->FightArray->len; ArrayInd++) { |
| 2738 |
Defend = (Player *)g_ptr_array_index(Play->FightArray, ArrayInd); |
| 2739 |
|
| 2740 |
if (Defend && Defend != Play && IsOpponent(Play, Defend) && |
| 2741 |
Defend->Health == 0) { |
| 2742 |
g_ptr_array_add(KilledPlayers, (gpointer)Defend); |
| 2743 |
} |
| 2744 |
} |
| 2745 |
for (ArrayInd = 0; ArrayInd < KilledPlayers->len; ArrayInd++) { |
| 2746 |
Defend = (Player *)g_ptr_array_index(KilledPlayers, ArrayInd); |
| 2747 |
WithdrawFromCombat(Defend); |
| 2748 |
if (IsCop(Defend)) { |
| 2749 |
if (!IsCop(Play)) |
| 2750 |
Play->CopIndex = -Defend->CopIndex; |
| 2751 |
FirstServer = RemovePlayer(Defend, FirstServer); |
| 2752 |
} else { |
| 2753 |
FinishGame(Defend, _("You're dead! Game over.")); |
| 2754 |
} |
| 2755 |
} |
| 2756 |
|
| 2757 |
g_ptr_array_free(KilledPlayers, TRUE); |
| 2758 |
} |
| 2759 |
|
| 2760 |
/* |
| 2761 |
* If "Play" is attacking someone, and no cops are currently present, |
| 2762 |
* then have the cops intervene (with a probability dependent on the |
| 2763 |
* current location's PolicePresence) |
| 2764 |
*/ |
| 2765 |
static void CheckCopsIntervene(Player *Play) |
| 2766 |
{ |
| 2767 |
guint ArrayInd; |
| 2768 |
Player *Defend; |
| 2769 |
|
| 2770 |
if (!Play || !Play->FightArray || NumCop == 0 || NumGun == 0) |
| 2771 |
return; /* Sanity check */ |
| 2772 |
|
| 2773 |
if (!Play->Attacking) |
| 2774 |
return; /* Cops don't attack "innocent victims" ;) */ |
| 2775 |
|
| 2776 |
if (brandom(0, 100) > Location[Play->IsAt].PolicePresence) { |
| 2777 |
return; /* The cops shouldn't _always_ attack |
| 2778 |
* (unless P.P. == 100) */ |
| 2779 |
} |
| 2780 |
|
| 2781 |
for (ArrayInd = 0; Play->FightArray && ArrayInd < Play->FightArray->len; |
| 2782 |
ArrayInd++) { |
| 2783 |
Defend = (Player *)g_ptr_array_index(Play->FightArray, ArrayInd); |
| 2784 |
if (IsCop(Defend)) |
| 2785 |
return; /* We don't want _more_ cops! */ |
| 2786 |
} |
| 2787 |
|
| 2788 |
/* OK - let 'em have it... */ |
| 2789 |
CopsAttackPlayer(Play); |
| 2790 |
} |
| 2791 |
|
| 2792 |
/* |
| 2793 |
* Returns a suitable player (or cop) for "Play" to fire at. If "Play" |
| 2794 |
* is attacking a designated target already, return that, otherwise |
| 2795 |
* return the first valid opponent in the player's FightArray. |
| 2796 |
*/ |
| 2797 |
static Player *GetFireTarget(Player *Play) |
| 2798 |
{ |
| 2799 |
Player *Defend; |
| 2800 |
guint ArrayInd; |
| 2801 |
|
| 2802 |
if (Play->Attacking |
| 2803 |
&& g_slist_find(FirstServer, (gpointer)Play->Attacking)) { |
| 2804 |
return Play->Attacking; |
| 2805 |
} else { |
| 2806 |
Play->Attacking = NULL; |
| 2807 |
for (ArrayInd = 0; ArrayInd < Play->FightArray->len; ArrayInd++) { |
| 2808 |
Defend = (Player *)g_ptr_array_index(Play->FightArray, ArrayInd); |
| 2809 |
if (Defend && Defend != Play && IsOpponent(Play, Defend)) { |
| 2810 |
return Defend; |
| 2811 |
} |
| 2812 |
} |
| 2813 |
} |
| 2814 |
return NULL; |
| 2815 |
} |
| 2816 |
|
| 2817 |
static int GetArmor(Player *Play) |
| 2818 |
{ |
| 2819 |
int Armor; |
| 2820 |
|
| 2821 |
if (IsCop(Play)) { |
| 2822 |
if (Play->Bitches.Carried == 0) |
| 2823 |
Armor = Cop[Play->CopIndex - 1].Armor; |
| 2824 |
else |
| 2825 |
Armor = Cop[Play->CopIndex - 1].DeputyArmor; |
| 2826 |
} else { |
| 2827 |
if (Play->Bitches.Carried == 0) |
| 2828 |
Armor = PlayerArmor; |
| 2829 |
else |
| 2830 |
Armor = BitchArmor; |
| 2831 |
} |
| 2832 |
if (Armor == 0) |
| 2833 |
Armor = 1; |
| 2834 |
return Armor; |
| 2835 |
} |
| 2836 |
|
| 2837 |
/* |
| 2838 |
* Fires all weapons of player "Play" at an opponent, and resets |
| 2839 |
* the fight timeout (the reload time). |
| 2840 |
*/ |
| 2841 |
void Fire(Player *Play) |
| 2842 |
{ |
| 2843 |
int Damage, i, j; |
| 2844 |
int AttackRating, DefendRating; |
| 2845 |
int BitchesKilled; |
| 2846 |
price_t Loot; |
| 2847 |
FightPoint fp; |
| 2848 |
Player *Defend; |
| 2849 |
|
| 2850 |
if (!Play->FightArray) |
| 2851 |
return; |
| 2852 |
if (!CanPlayerFire(Play)) |
| 2853 |
return; |
| 2854 |
|
| 2855 |
AllowNextShooter(Play); |
| 2856 |
if (FightTimeout) |
| 2857 |
SetFightTimeout(Play); |
| 2858 |
|
| 2859 |
Defend = GetFireTarget(Play); |
| 2860 |
if (Defend) { |
| 2861 |
Damage = 0; |
| 2862 |
BitchesKilled = 0; |
| 2863 |
Loot = 0; |
| 2864 |
if (TotalGunsCarried(Play) > 0) { |
| 2865 |
GetFightRatings(Play, Defend, &AttackRating, &DefendRating); |
| 2866 |
if (brandom(0, AttackRating) > brandom(0, DefendRating)) { |
| 2867 |
fp = F_HIT; |
| 2868 |
for (i = 0; i < NumGun; i++) |
| 2869 |
for (j = 0; j < Play->Guns[i].Carried; j++) { |
| 2870 |
Damage += brandom(0, Gun[i].Damage); |
| 2871 |
} |
| 2872 |
Damage = Damage * 100 / GetArmor(Defend); |
| 2873 |
if (Damage == 0) |
| 2874 |
Damage = 1; |
| 2875 |
HandleDamage(Defend, Play, Damage, &BitchesKilled, &Loot); |
| 2876 |
} else |
| 2877 |
fp = F_MISS; |
| 2878 |
} else |
| 2879 |
fp = F_STAND; |
| 2880 |
SendFightMessage(Play, Defend, BitchesKilled, fp, Loot, TRUE, NULL); |
| 2881 |
} |
| 2882 |
CheckForKilledPlayers(Play); |
| 2883 |
|
| 2884 |
/* Careful, as we might have killed Player "Play" */ |
| 2885 |
if (g_slist_find(FirstServer, (gpointer)Play)) |
| 2886 |
DoReturnFire(Play); |
| 2887 |
|
| 2888 |
if (g_slist_find(FirstServer, (gpointer)Play)) |
| 2889 |
CheckCopsIntervene(Play); |
| 2890 |
} |
| 2891 |
|
| 2892 |
gboolean CanPlayerFire(Player *Play) |
| 2893 |
{ |
| 2894 |
return (FightTimeout == 0 || Play->FightTimeout == 0 || |
| 2895 |
Play->FightTimeout <= time(NULL)); |
| 2896 |
} |
| 2897 |
|
| 2898 |
gboolean CanRunHere(Player *Play) |
| 2899 |
{ |
| 2900 |
return (Play->ResyncNum < E_ARRIVE && Play->ResyncNum != E_NONE); |
| 2901 |
} |
| 2902 |
|
| 2903 |
/* |
| 2904 |
* To avoid boring waits, return the player who is next in line to be |
| 2905 |
* able to shoot (i.e. with the smallest FightTimeout) so that this |
| 2906 |
* player can be allowed to shoot immediately. If a player is already |
| 2907 |
* eligible to shoot, or there is a tie for "first place" then do |
| 2908 |
* nothing (i.e. return NULL). |
| 2909 |
*/ |
| 2910 |
Player *GetNextShooter(Player *Play) |
| 2911 |
{ |
| 2912 |
Player *MinPlay, *Defend; |
| 2913 |
time_t MinTimeout; |
| 2914 |
guint mintie; |
| 2915 |
guint ArrayInd; |
| 2916 |
|
| 2917 |
if (!FightTimeout) |
| 2918 |
return NULL; |
| 2919 |
|
| 2920 |
MinPlay = NULL; |
| 2921 |
MinTimeout = 0; |
| 2922 |
mintie = 0; |
| 2923 |
for (ArrayInd = 0; ArrayInd < Play->FightArray->len; ArrayInd++) { |
| 2924 |
Defend = (Player *)g_ptr_array_index(Play->FightArray, ArrayInd); |
| 2925 |
if (Defend == Play) |
| 2926 |
continue; |
| 2927 |
if (Defend->FightTimeout == 0) |
| 2928 |
return NULL; |
| 2929 |
if (MinTimeout == 0 || Defend->FightTimeout < MinTimeout |
| 2930 |
|| (Defend->FightTimeout == MinTimeout && Defend->tiebreak < mintie)) { |
| 2931 |
MinPlay = Defend; |
| 2932 |
MinTimeout = Defend->FightTimeout; |
| 2933 |
mintie = Defend->tiebreak; |
| 2934 |
} |
| 2935 |
} |
| 2936 |
|
| 2937 |
return MinPlay; |
| 2938 |
} |
| 2939 |
|
| 2940 |
void ResolveTipoff(Player *Play) |
| 2941 |
{ |
| 2942 |
GString *text; |
| 2943 |
|
| 2944 |
if (IsCop(Play) || !CanRunHere(Play)) |
| 2945 |
return; |
| 2946 |
|
| 2947 |
if (g_slist_find(FirstServer, (gpointer)Play->OnBehalfOf)) { |
| 2948 |
dopelog(4, LF_SERVER, _("%s: tipoff by %s finished OK."), |
| 2949 |
GetPlayerName(Play), GetPlayerName(Play->OnBehalfOf)); |
| 2950 |
RemoveListPlayer(&(Play->TipList), Play->OnBehalfOf); |
| 2951 |
text = g_string_new(""); |
| 2952 |
if (Play->Health == 0) { |
| 2953 |
g_string_printf(text, |
| 2954 |
_("Following your tipoff, the cops ambushed %s, " |
| 2955 |
"who was shot dead!"), GetPlayerName(Play)); |
| 2956 |
} else { |
| 2957 |
dpg_string_printf(text, |
| 2958 |
_("Following your tipoff, the cops ambushed %s, " |
| 2959 |
"who escaped with %d %tde. "), GetPlayerName(Play), |
| 2960 |
Play->Bitches.Carried, Names.Bitches); |
| 2961 |
} |
| 2962 |
GainBitch(Play->OnBehalfOf); |
| 2963 |
SendPlayerData(Play->OnBehalfOf); |
| 2964 |
SendPrintMessage(NULL, C_NONE, Play->OnBehalfOf, text->str); |
| 2965 |
g_string_free(text, TRUE); |
| 2966 |
} |
| 2967 |
Play->OnBehalfOf = NULL; |
| 2968 |
} |
| 2969 |
|
| 2970 |
/* |
| 2971 |
* Cleans up combat after player "Play" has left. |
| 2972 |
*/ |
| 2973 |
void WithdrawFromCombat(Player *Play) |
| 2974 |
{ |
| 2975 |
guint AttackInd, DefendInd; |
| 2976 |
gboolean FightDone; |
| 2977 |
Player *Attack, *Defend; |
| 2978 |
GSList *list; |
| 2979 |
gchar *text; |
| 2980 |
|
| 2981 |
for (list = FirstServer; list; list = g_slist_next(list)) { |
| 2982 |
Attack = (Player *)list->data; |
| 2983 |
if (Attack->Attacking == Play) |
| 2984 |
Attack->Attacking = NULL; |
| 2985 |
} |
| 2986 |
|
| 2987 |
if (!Play->FightArray) |
| 2988 |
return; |
| 2989 |
|
| 2990 |
ResolveTipoff(Play); |
| 2991 |
FightDone = TRUE; |
| 2992 |
for (AttackInd = 0; AttackInd < Play->FightArray->len; AttackInd++) { |
| 2993 |
Attack = (Player *)g_ptr_array_index(Play->FightArray, AttackInd); |
| 2994 |
for (DefendInd = 0; DefendInd < AttackInd; DefendInd++) { |
| 2995 |
Defend = (Player *)g_ptr_array_index(Play->FightArray, DefendInd); |
| 2996 |
if (Attack != Play && Defend != Play && IsOpponent(Attack, Defend)) { |
| 2997 |
FightDone = FALSE; |
| 2998 |
break; |
| 2999 |
} |
| 3000 |
} |
| 3001 |
if (!FightDone) |
| 3002 |
break; |
| 3003 |
} |
| 3004 |
|
| 3005 |
SendFightLeave(Play, FightDone); |
| 3006 |
g_ptr_array_remove(Play->FightArray, (gpointer)Play); |
| 3007 |
|
| 3008 |
if (FightDone) { |
| 3009 |
for (DefendInd = 0; DefendInd < Play->FightArray->len; DefendInd++) { |
| 3010 |
Defend = (Player *)g_ptr_array_index(Play->FightArray, DefendInd); |
| 3011 |
Defend->FightArray = NULL; |
| 3012 |
ResolveTipoff(Defend); |
| 3013 |
if (IsCop(Defend)) { |
| 3014 |
FirstServer = RemovePlayer(Defend, FirstServer); |
| 3015 |
} else if (Defend->Health == 0) { |
| 3016 |
FinishGame(Defend, _("You're dead! Game over.")); |
| 3017 |
} else if (CanRunHere(Defend) |
| 3018 |
&& brandom(0, 100) > Location[Defend->IsAt].PolicePresence) { |
| 3019 |
Defend->EventNum = E_DOCTOR; |
| 3020 |
Defend->DocPrice = prandom(Bitch.MinPrice, Bitch.MaxPrice) * |
| 3021 |
Defend->Health / 500; |
| 3022 |
text = |
| 3023 |
dpg_strdup_printf(_ |
| 3024 |
("YN^Do you pay a doctor %P to sew you up?"), |
| 3025 |
Defend->DocPrice); |
| 3026 |
SendQuestion(NULL, C_ASKSEW, Defend, text); |
| 3027 |
g_free(text); |
| 3028 |
} else { |
| 3029 |
WaitForFightDone(Defend); |
| 3030 |
} |
| 3031 |
} |
| 3032 |
g_ptr_array_free(Play->FightArray, TRUE); |
| 3033 |
} |
| 3034 |
Play->FightArray = NULL; |
| 3035 |
Play->Attacking = NULL; |
| 3036 |
} |
| 3037 |
|
| 3038 |
/* |
| 3039 |
* Inform player "To" of random offers or happenings. Returns 0 if |
| 3040 |
* the client can immediately be advanced to the next state, or 1 |
| 3041 |
* there are first questions to be answered. |
| 3042 |
*/ |
| 3043 |
int RandomOffer(Player *To) |
| 3044 |
{ |
| 3045 |
int r, amount, ind; |
| 3046 |
GString *text; |
| 3047 |
|
| 3048 |
r = brandom(0, 100); |
| 3049 |
|
| 3050 |
text = g_string_new(NULL); |
| 3051 |
|
| 3052 |
if (!Sanitized && (r < 10)) { |
| 3053 |
g_string_assign(text, _("You were mugged in the subway!")); |
| 3054 |
To->Cash = To->Cash * (price_t)brandom(80, 95) / 100l; |
| 3055 |
} else if (r < 30) { |
| 3056 |
amount = brandom(3, 7); |
| 3057 |
ind = IsCarryingRandom(To, amount); |
| 3058 |
if (ind == -1 && amount > To->CoatSize) { |
| 3059 |
g_string_free(text, TRUE); |
| 3060 |
return 0; |
| 3061 |
} |
| 3062 |
if (ind == -1) { |
| 3063 |
ind = brandom(0, NumDrug); |
| 3064 |
dpg_string_printf(text, |
| 3065 |
_("You meet a friend! He gives you %d %tde."), |
| 3066 |
amount, Drug[ind].Name); |
| 3067 |
To->Drugs[ind].Carried += amount; |
| 3068 |
To->CoatSize -= amount; |
| 3069 |
} else { |
| 3070 |
dpg_string_printf(text, |
| 3071 |
_("You meet a friend! You give him %d %tde."), |
| 3072 |
amount, Drug[ind].Name); |
| 3073 |
To->Drugs[ind].TotalValue = |
| 3074 |
To->Drugs[ind].TotalValue * (To->Drugs[ind].Carried - |
| 3075 |
amount) / To->Drugs[ind].Carried; |
| 3076 |
To->Drugs[ind].Carried -= amount; |
| 3077 |
To->CoatSize += amount; |
| 3078 |
} |
| 3079 |
SendPlayerData(To); |
| 3080 |
SendPrintMessage(NULL, C_NONE, To, text->str); |
| 3081 |
} else if (Sanitized) { |
| 3082 |
/* Debugging message: we would normally have a random drug-related |
| 3083 |
event here, but "Sanitized" mode is turned on */ |
| 3084 |
dopelog(3, LF_SERVER, _("Sanitized away a RandomOffer")); |
| 3085 |
} else if (r < 50) { |
| 3086 |
amount = brandom(3, 7); |
| 3087 |
ind = IsCarryingRandom(To, amount); |
| 3088 |
if (ind != -1) { |
| 3089 |
dpg_string_printf(text, _("Police dogs chase you for %d blocks! " |
| 3090 |
"You dropped some %tde! That's a drag, man!"), |
| 3091 |
brandom(3, 7), Names.Drugs); |
| 3092 |
To->Drugs[ind].TotalValue = To->Drugs[ind].TotalValue * |
| 3093 |
(To->Drugs[ind].Carried - amount) / To->Drugs[ind].Carried; |
| 3094 |
To->Drugs[ind].Carried -= amount; |
| 3095 |
To->CoatSize += amount; |
| 3096 |
SendPlayerData(To); |
| 3097 |
SendPrintMessage(NULL, C_NONE, To, text->str); |
| 3098 |
} else { |
| 3099 |
ind = brandom(0, NumDrug); |
| 3100 |
amount = brandom(3, 7); |
| 3101 |
if (amount > To->CoatSize) { |
| 3102 |
g_string_free(text, TRUE); |
| 3103 |
return 0; |
| 3104 |
} |
| 3105 |
dpg_string_printf(text, |
| 3106 |
_("You find %d %tde on a dead dude in the subway!"), |
| 3107 |
amount, Drug[ind].Name); |
| 3108 |
To->Drugs[ind].Carried += amount; |
| 3109 |
To->CoatSize -= amount; |
| 3110 |
SendPlayerData(To); |
| 3111 |
SendPrintMessage(NULL, C_NONE, To, text->str); |
| 3112 |
} |
| 3113 |
} else if (r < 60 |
| 3114 |
&& To->Drugs[WEED].Carried + To->Drugs[HASHISH].Carried > 0) { |
| 3115 |
ind = (To->Drugs[WEED].Carried > |
| 3116 |
To->Drugs[HASHISH].Carried) ? WEED : HASHISH; |
| 3117 |
amount = brandom(2, 6); |
| 3118 |
if (amount > To->Drugs[ind].Carried) |
| 3119 |
amount = To->Drugs[ind].Carried; |
| 3120 |
dpg_string_printf(text, |
| 3121 |
_("Your mama made brownies with some of your %tde! " |
| 3122 |
"They were great!"), Drug[ind].Name); |
| 3123 |
To->Drugs[ind].TotalValue = To->Drugs[ind].TotalValue * |
| 3124 |
(To->Drugs[ind].Carried - amount) / To->Drugs[ind].Carried; |
| 3125 |
To->Drugs[ind].Carried -= amount; |
| 3126 |
To->CoatSize += amount; |
| 3127 |
SendPlayerData(To); |
| 3128 |
SendPrintMessage(NULL, C_NONE, To, text->str); |
| 3129 |
} else if (r < 65) { |
| 3130 |
g_string_assign(text, |
| 3131 |
_("YN^There is some weed that smells like paraquat " |
| 3132 |
"here!^It looks good! Will you smoke it? ")); |
| 3133 |
To->EventNum = E_WEED; |
| 3134 |
SendQuestion(NULL, C_NONE, To, text->str); |
| 3135 |
g_string_free(text, TRUE); |
| 3136 |
return 1; |
| 3137 |
} else if (NumStoppedTo > 0) { |
| 3138 |
g_string_printf(text, _("You stopped to %s."), |
| 3139 |
StoppedTo[brandom(0, NumStoppedTo)]); |
| 3140 |
amount = brandom(1, 10); |
| 3141 |
if (To->Cash >= amount) |
| 3142 |
To->Cash -= amount; |
| 3143 |
SendPlayerData(To); |
| 3144 |
SendPrintMessage(NULL, C_NONE, To, text->str); |
| 3145 |
} |
| 3146 |
g_string_free(text, TRUE); |
| 3147 |
return 0; |
| 3148 |
} |
| 3149 |
|
| 3150 |
/* |
| 3151 |
* Offers player "To" bitches/trenchcoats or guns. If ForceBitch is |
| 3152 |
* TRUE, then a bitch is definitely offered. Returns 0 if the client |
| 3153 |
* can advance immediately to the next state, 1 otherwise. |
| 3154 |
*/ |
| 3155 |
int OfferObject(Player *To, gboolean ForceBitch) |
| 3156 |
{ |
| 3157 |
int ObjNum; |
| 3158 |
gchar *text = NULL; |
| 3159 |
|
| 3160 |
if (brandom(0, 100) < 50 || ForceBitch) { |
| 3161 |
if (WantAntique) { |
| 3162 |
To->Bitches.Price = prandom(MINTRENCHPRICE, MAXTRENCHPRICE); |
| 3163 |
text = dpg_strdup_printf(_("YN^Would you like to buy a bigger " |
| 3164 |
"trenchcoat for %P?"), To->Bitches.Price); |
| 3165 |
} else { |
| 3166 |
To->Bitches.Price = |
| 3167 |
prandom(Bitch.MinPrice, Bitch.MaxPrice) / (price_t)10; |
| 3168 |
text = |
| 3169 |
dpg_strdup_printf(_ |
| 3170 |
("YN^Hey dude! I'll help carry your %tde for a " |
| 3171 |
"mere %P. Yes or no?"), Names.Drugs, |
| 3172 |
To->Bitches.Price); |
| 3173 |
} |
| 3174 |
SendQuestion(NULL, C_ASKBITCH, To, text); |
| 3175 |
g_free(text); |
| 3176 |
return 1; |
| 3177 |
} else if (!Sanitized && NumGun > 0 |
| 3178 |
&& (TotalGunsCarried(To) < To->Bitches.Carried + 2)) { |
| 3179 |
ObjNum = brandom(0, NumGun); |
| 3180 |
To->Guns[ObjNum].Price = Gun[ObjNum].Price / 10; |
| 3181 |
if (Gun[ObjNum].Space > To->CoatSize) |
| 3182 |
return 0; |
| 3183 |
text = dpg_strdup_printf(_("YN^Would you like to buy a %tde for %P?"), |
| 3184 |
Gun[ObjNum].Name, To->Guns[ObjNum].Price); |
| 3185 |
SendQuestion(NULL, C_ASKGUN, To, text); |
| 3186 |
g_free(text); |
| 3187 |
return 1; |
| 3188 |
} |
| 3189 |
return 0; |
| 3190 |
} |
| 3191 |
|
| 3192 |
/* Whether a particular drug is especially cheap or expensive */ |
| 3193 |
enum DealType { |
| 3194 |
DT_NORMAL, DT_CHEAP, DT_EXPENSIVE |
| 3195 |
}; |
| 3196 |
|
| 3197 |
/* |
| 3198 |
* Generates drug prices and drug busts etc. for player "To" |
| 3199 |
* "Deal" is an array of size NumDrug. |
| 3200 |
*/ |
| 3201 |
static void GenerateDrugsHere(Player *To, enum DealType *Deal) |
| 3202 |
{ |
| 3203 |
int NumEvents, NumDrugs, NumRandom, i; |
| 3204 |
|
| 3205 |
for (i = 0; i < NumDrug; i++) { |
| 3206 |
To->Drugs[i].Price = 0; |
| 3207 |
Deal[i] = DT_NORMAL; |
| 3208 |
} |
| 3209 |
NumEvents = 0; |
| 3210 |
if (brandom(0, 100) < 70) |
| 3211 |
NumEvents = 1; |
| 3212 |
if (brandom(0, 100) < 40 && NumEvents == 1) |
| 3213 |
NumEvents = 2; |
| 3214 |
if (brandom(0, 100) < 5 && NumEvents == 2) |
| 3215 |
NumEvents = 3; |
| 3216 |
NumDrugs = 0; |
| 3217 |
while (NumEvents > 0) { |
| 3218 |
i = brandom(0, NumDrug); |
| 3219 |
if (Deal[i] != DT_NORMAL) |
| 3220 |
continue; |
| 3221 |
if (Drug[i].Expensive && (!Drug[i].Cheap || brandom(0, 100) < 50)) { |
| 3222 |
Deal[i] = DT_EXPENSIVE; |
| 3223 |
To->Drugs[i].Price = prandom(Drug[i].MinPrice, Drug[i].MaxPrice) |
| 3224 |
* Drugs.ExpensiveMultiply; |
| 3225 |
NumDrugs++; |
| 3226 |
NumEvents--; |
| 3227 |
} else if (Drug[i].Cheap) { |
| 3228 |
Deal[i] = DT_CHEAP; |
| 3229 |
To->Drugs[i].Price = prandom(Drug[i].MinPrice, Drug[i].MaxPrice) |
| 3230 |
/ Drugs.CheapDivide; |
| 3231 |
NumDrugs++; |
| 3232 |
NumEvents--; |
| 3233 |
} |
| 3234 |
} |
| 3235 |
NumRandom = brandom(Location[To->IsAt].MinDrug, |
| 3236 |
Location[To->IsAt].MaxDrug); |
| 3237 |
if (NumRandom > NumDrug) |
| 3238 |
NumRandom = NumDrug; |
| 3239 |
|
| 3240 |
NumDrugs = NumRandom - NumDrugs; |
| 3241 |
while (NumDrugs > 0) { |
| 3242 |
i = brandom(0, NumDrug); |
| 3243 |
if (To->Drugs[i].Price == 0) { |
| 3244 |
To->Drugs[i].Price = prandom(Drug[i].MinPrice, Drug[i].MaxPrice); |
| 3245 |
NumDrugs--; |
| 3246 |
} |
| 3247 |
} |
| 3248 |
} |
| 3249 |
|
| 3250 |
/* |
| 3251 |
* Sends details of drug prices to player "To". If "DisplayBusts" |
| 3252 |
* is TRUE, also regenerates drug prices and sends details of |
| 3253 |
* special events such as drug busts. |
| 3254 |
*/ |
| 3255 |
void SendDrugsHere(Player *To, gboolean DisplayBusts) |
| 3256 |
{ |
| 3257 |
int i; |
| 3258 |
enum DealType *Deal = NULL; |
| 3259 |
gchar *prstr; |
| 3260 |
GString *text; |
| 3261 |
gboolean First; |
| 3262 |
|
| 3263 |
Deal = g_new0(enum DealType, NumDrug); |
| 3264 |
if (DisplayBusts) |
| 3265 |
GenerateDrugsHere(To, Deal); |
| 3266 |
|
| 3267 |
text = g_string_new(NULL); |
| 3268 |
First = TRUE; |
| 3269 |
if (DisplayBusts) { |
| 3270 |
for (i = 0; i < NumDrug; i++) { |
| 3271 |
if (Deal[i] != DT_NORMAL) { |
| 3272 |
if (!First) |
| 3273 |
g_string_append_c(text, '^'); |
| 3274 |
if (Deal[i] == DT_CHEAP) { |
| 3275 |
g_string_append(text, Drug[i].CheapStr); |
| 3276 |
} else { |
| 3277 |
dpg_string_append_printf(text, brandom(0, 100) < 50 |
| 3278 |
? Drugs.ExpensiveStr1 : Drugs.ExpensiveStr2, |
| 3279 |
Drug[i].Name); |
| 3280 |
} |
| 3281 |
First = FALSE; |
| 3282 |
} |
| 3283 |
} |
| 3284 |
} |
| 3285 |
g_free(Deal); |
| 3286 |
|
| 3287 |
if (!First) |
| 3288 |
SendPrintMessage(NULL, C_NONE, To, text->str); |
| 3289 |
g_string_truncate(text, 0); |
| 3290 |
for (i = 0; i < NumDrug; i++) { |
| 3291 |
g_string_append_printf(text, "%s^", |
| 3292 |
(prstr = pricetostr(To->Drugs[i].Price))); |
| 3293 |
g_free(prstr); |
| 3294 |
} |
| 3295 |
SendServerMessage(NULL, C_NONE, C_DRUGHERE, To, text->str); |
| 3296 |
g_string_free(text, TRUE); |
| 3297 |
} |
| 3298 |
|
| 3299 |
/* |
| 3300 |
* Handles the incoming message in "answer" from player "From" and |
| 3301 |
* intended for player "To". |
| 3302 |
*/ |
| 3303 |
void HandleAnswer(Player *From, Player *To, char *answer) |
| 3304 |
{ |
| 3305 |
int i; |
| 3306 |
gchar *text; |
| 3307 |
Player *Defender; |
| 3308 |
|
| 3309 |
if (!From || From->EventNum == E_NONE) |
| 3310 |
return; |
| 3311 |
if (answer[0] == 'Y' && From->EventNum == E_OFFOBJECT |
| 3312 |
&& From->Bitches.Price && From->Bitches.Price > From->Cash) |
| 3313 |
answer[0] = 'N'; |
| 3314 |
if ((From->EventNum == E_FIGHT || From->EventNum == E_FIGHTASK) |
| 3315 |
&& CanRunHere(From)) { |
| 3316 |
From->EventNum = E_FIGHT; |
| 3317 |
if (answer[0] == 'R' || answer[0] == 'Y') { |
| 3318 |
RunFromCombat(From, -1); |
| 3319 |
} else { |
| 3320 |
Fire(From); |
| 3321 |
} |
| 3322 |
} else if (answer[0] == 'Y') |
| 3323 |
switch (From->EventNum) { |
| 3324 |
case E_OFFOBJECT: |
| 3325 |
if (g_slist_find(FirstServer, (gpointer)From->OnBehalfOf)) { |
| 3326 |
dopelog(3, LF_SERVER, _("%s: offer was on behalf of %s"), |
| 3327 |
GetPlayerName(From), GetPlayerName(From->OnBehalfOf)); |
| 3328 |
if (From->Bitches.Price) { |
| 3329 |
text = dpg_strdup_printf(_("%s has accepted your %tde!" |
| 3330 |
"^Use the G key to contact your spy."), |
| 3331 |
GetPlayerName(From), Names.Bitch); |
| 3332 |
From->OnBehalfOf->Flags |= SPYINGON; |
| 3333 |
SendPlayerData(From->OnBehalfOf); |
| 3334 |
SendPrintMessage(NULL, C_NONE, From->OnBehalfOf, text); |
| 3335 |
g_free(text); |
| 3336 |
i = GetListEntry(&(From->SpyList), From->OnBehalfOf); |
| 3337 |
if (i >= 0) |
| 3338 |
From->SpyList.Data[i].Turns = 0; |
| 3339 |
} |
| 3340 |
} |
| 3341 |
if (From->Bitches.Price) { |
| 3342 |
text = g_strdup_printf("bitch^0^1"); |
| 3343 |
BuyObject(From, text); |
| 3344 |
g_free(text); |
| 3345 |
} else { |
| 3346 |
for (i = 0; i < NumGun; i++) |
| 3347 |
if (From->Guns[i].Price) { |
| 3348 |
text = g_strdup_printf("gun^%d^1", i); |
| 3349 |
BuyObject(From, text); |
| 3350 |
g_free(text); |
| 3351 |
break; |
| 3352 |
} |
| 3353 |
} |
| 3354 |
From->OnBehalfOf = NULL; |
| 3355 |
From->EventNum++; |
| 3356 |
SendEvent(From); |
| 3357 |
break; |
| 3358 |
case E_LOANSHARK: |
| 3359 |
SendServerMessage(NULL, C_NONE, C_LOANSHARK, From, NULL); |
| 3360 |
break; |
| 3361 |
case E_BANK: |
| 3362 |
SendServerMessage(NULL, C_NONE, C_BANK, From, NULL); |
| 3363 |
break; |
| 3364 |
case E_GUNSHOP: |
| 3365 |
for (i = 0; i < NumGun; i++) |
| 3366 |
From->Guns[i].Price = Gun[i].Price; |
| 3367 |
SendServerMessage(NULL, C_NONE, C_GUNSHOP, From, NULL); |
| 3368 |
break; |
| 3369 |
case E_HIREBITCH: |
| 3370 |
text = g_strdup_printf("bitch^0^1"); |
| 3371 |
BuyObject(From, text); |
| 3372 |
g_free(text); |
| 3373 |
From->EventNum++; |
| 3374 |
SendEvent(From); |
| 3375 |
break; |
| 3376 |
case E_ROUGHPUB: |
| 3377 |
From->EventNum++; |
| 3378 |
SendEvent(From); |
| 3379 |
break; |
| 3380 |
case E_WEED: |
| 3381 |
FinishGame(From, _("You hallucinated for three days on the wildest " |
| 3382 |
"trip you ever imagined!^Then you died because " |
| 3383 |
"your brain disintegrated!")); |
| 3384 |
break; |
| 3385 |
case E_DOCTOR: |
| 3386 |
if (From->Cash >= From->DocPrice) { |
| 3387 |
From->Cash -= From->DocPrice; |
| 3388 |
From->Health = 100; |
| 3389 |
SendPlayerData(From); |
| 3390 |
} |
| 3391 |
WaitForFightDone(From); |
| 3392 |
break; |
| 3393 |
default: |
| 3394 |
break; |
| 3395 |
} else if (From->EventNum == E_ARRIVE) { |
| 3396 |
if ((answer[0] == 'A' || answer[0] == 'T') && |
| 3397 |
IsConnectedPlayer(From->OnBehalfOf) && |
| 3398 |
g_slist_find(FirstServer, (gpointer)From->OnBehalfOf)) { |
| 3399 |
Defender = From->OnBehalfOf; |
| 3400 |
From->OnBehalfOf = NULL; /* So we don't think it was a tipoff */ |
| 3401 |
if (Defender->IsAt == From->IsAt) { |
| 3402 |
if (answer[0] == 'A') { |
| 3403 |
From->EventNum = Defender->EventNum = E_NONE; |
| 3404 |
AttackPlayer(From, Defender); |
| 3405 |
} |
| 3406 |
} else { |
| 3407 |
text = g_strdup_printf(_("Too late - %s has just left!"), |
| 3408 |
GetPlayerName(Defender)); |
| 3409 |
SendPrintMessage(NULL, C_MISSFIGHT, From, text); |
| 3410 |
g_free(text); |
| 3411 |
From->EventNum++; |
| 3412 |
SendEvent(From); |
| 3413 |
} |
| 3414 |
} else { |
| 3415 |
From->EventNum++; |
| 3416 |
SendEvent(From); |
| 3417 |
} |
| 3418 |
From->OnBehalfOf = NULL; |
| 3419 |
} else |
| 3420 |
switch (From->EventNum) { |
| 3421 |
case E_ROUGHPUB: |
| 3422 |
From->EventNum++; |
| 3423 |
From->EventNum++; |
| 3424 |
SendEvent(From); |
| 3425 |
break; |
| 3426 |
case E_DOCTOR: |
| 3427 |
WaitForFightDone(From); |
| 3428 |
break; |
| 3429 |
case E_HIREBITCH: |
| 3430 |
case E_GUNSHOP: |
| 3431 |
case E_BANK: |
| 3432 |
case E_LOANSHARK: |
| 3433 |
case E_OFFOBJECT: |
| 3434 |
case E_WEED: |
| 3435 |
if (g_slist_find(FirstServer, (gpointer)From->OnBehalfOf)) { |
| 3436 |
dopelog(3, LF_SERVER, _("%s: offer was on behalf of %s"), |
| 3437 |
GetPlayerName(From), GetPlayerName(From->OnBehalfOf)); |
| 3438 |
if (From->Bitches.Price && From->EventNum == E_OFFOBJECT) { |
| 3439 |
text = dpg_strdup_printf(_("%s has rejected your %tde!"), |
| 3440 |
GetPlayerName(From), Names.Bitch); |
| 3441 |
GainBitch(From->OnBehalfOf); |
| 3442 |
SendPlayerData(From->OnBehalfOf); |
| 3443 |
SendPrintMessage(NULL, C_NONE, From->OnBehalfOf, text); |
| 3444 |
g_free(text); |
| 3445 |
RemoveListPlayer(&(From->SpyList), From->OnBehalfOf); |
| 3446 |
} |
| 3447 |
} |
| 3448 |
From->EventNum++; |
| 3449 |
SendEvent(From); |
| 3450 |
break; |
| 3451 |
default: |
| 3452 |
break; |
| 3453 |
} |
| 3454 |
} |
| 3455 |
|
| 3456 |
/* |
| 3457 |
* Processes a request stored in "data" from player "From" to buy an |
| 3458 |
* object (bitch, gun, or drug). |
| 3459 |
* Objects can be sold if the amount given in "data" is negative, and |
| 3460 |
* given away if their current price is zero. |
| 3461 |
*/ |
| 3462 |
void BuyObject(Player *From, char *data) |
| 3463 |
{ |
| 3464 |
char *cp, *type; |
| 3465 |
int index, i, amount; |
| 3466 |
|
| 3467 |
cp = data; |
| 3468 |
type = GetNextWord(&cp, ""); |
| 3469 |
index = GetNextInt(&cp, 0); |
| 3470 |
amount = GetNextInt(&cp, 0); |
| 3471 |
if (strcmp(type, "drug") == 0) { |
| 3472 |
if (index >= 0 && index < NumDrug |
| 3473 |
&& From->Drugs[index].Carried + amount >= 0 |
| 3474 |
&& From->CoatSize - amount >= 0 && (From->Drugs[index].Price != 0 |
| 3475 |
|| amount < 0) |
| 3476 |
&& From->Cash >= amount * From->Drugs[index].Price) { |
| 3477 |
if (amount > 0) { |
| 3478 |
From->Drugs[index].TotalValue += amount * From->Drugs[index].Price; |
| 3479 |
} else if (From->Drugs[index].Carried != 0) { |
| 3480 |
From->Drugs[index].TotalValue = From->Drugs[index].TotalValue * |
| 3481 |
(From->Drugs[index].Carried + |
| 3482 |
amount) / From->Drugs[index].Carried; |
| 3483 |
} |
| 3484 |
From->Drugs[index].Carried += amount; |
| 3485 |
From->CoatSize -= amount; |
| 3486 |
From->Cash -= amount * From->Drugs[index].Price; |
| 3487 |
SendPlayerData(From); |
| 3488 |
|
| 3489 |
if (!Sanitized && NumCop > 0 && NumGun > 0 |
| 3490 |
&& (From->Drugs[index].Price == 0 && |
| 3491 |
brandom(0, 100) < Location[From->IsAt].PolicePresence)) { |
| 3492 |
gchar *text; |
| 3493 |
|
| 3494 |
text = dpg_strdup_printf(_("The cops spot you dropping %tde!"), |
| 3495 |
Names.Drugs); |
| 3496 |
SendPrintMessage(NULL, C_NONE, From, text); |
| 3497 |
g_free(text); |
| 3498 |
CopsAttackPlayer(From); |
| 3499 |
} |
| 3500 |
} |
| 3501 |
} else if (strcmp(type, "gun") == 0) { |
| 3502 |
if (index >= 0 && index < NumGun |
| 3503 |
&& TotalGunsCarried(From) + amount >= 0 |
| 3504 |
&& TotalGunsCarried(From) + amount <= From->Bitches.Carried + 2 |
| 3505 |
&& From->Guns[index].Price != 0 |
| 3506 |
&& From->CoatSize - amount * Gun[index].Space >= 0 |
| 3507 |
&& From->Cash >= amount * From->Guns[index].Price) { |
| 3508 |
From->Guns[index].Carried += amount; |
| 3509 |
From->CoatSize -= amount * Gun[index].Space; |
| 3510 |
From->Cash -= amount * From->Guns[index].Price; |
| 3511 |
SendPlayerData(From); |
| 3512 |
} |
| 3513 |
} else if (strcmp(type, "bitch") == 0) { |
| 3514 |
if (From->Bitches.Carried + amount >= 0 |
| 3515 |
&& From->Bitches.Price != 0 |
| 3516 |
&& amount * From->Bitches.Price <= From->Cash) { |
| 3517 |
for (i = 0; i < amount; i++) |
| 3518 |
GainBitch(From); |
| 3519 |
if (amount > 0) |
| 3520 |
From->Cash -= amount * From->Bitches.Price; |
| 3521 |
SendPlayerData(From); |
| 3522 |
} |
| 3523 |
} |
| 3524 |
} |
| 3525 |
|
| 3526 |
/* |
| 3527 |
* Clears the bitch and gun prices stored for player "Play". |
| 3528 |
*/ |
| 3529 |
void ClearPrices(Player *Play) |
| 3530 |
{ |
| 3531 |
int i; |
| 3532 |
|
| 3533 |
Play->Bitches.Price = 0; |
| 3534 |
for (i = 0; i < NumGun; i++) |
| 3535 |
Play->Guns[i].Price = 0; |
| 3536 |
} |
| 3537 |
|
| 3538 |
/* |
| 3539 |
* Gives player "Play" a new bitch (or larger trenchcoat). |
| 3540 |
*/ |
| 3541 |
void GainBitch(Player *Play) |
| 3542 |
{ |
| 3543 |
Play->CoatSize += 10; |
| 3544 |
Play->Bitches.Carried++; |
| 3545 |
} |
| 3546 |
|
| 3547 |
/* |
| 3548 |
* Loses one bitch belonging to player "Play". If drugs or guns are |
| 3549 |
* lost with the bitch, 1 is returned (0 otherwise) and the lost |
| 3550 |
* items are added to "Guns" and "Drugs" if non-NULL. |
| 3551 |
*/ |
| 3552 |
int LoseBitch(Player *Play, Inventory *Guns, Inventory *Drugs) |
| 3553 |
{ |
| 3554 |
int losedrug = 0, i, num, drugslost; |
| 3555 |
int *GunIndex, tmp; |
| 3556 |
GunIndex = g_new(int, NumGun); |
| 3557 |
|
| 3558 |
ClearInventory(Guns, Drugs); |
| 3559 |
Play->CoatSize -= 10; |
| 3560 |
if (TotalGunsCarried(Play) > 0) { |
| 3561 |
if (brandom(0, 100) < |
| 3562 |
TotalGunsCarried(Play) * 100 / (Play->Bitches.Carried + 2)) { |
| 3563 |
for (i = 0; i < NumGun; i++) |
| 3564 |
GunIndex[i] = i; |
| 3565 |
for (i = 0; i < NumGun * 5; i++) { |
| 3566 |
num = brandom(0, NumGun - 1); |
| 3567 |
tmp = GunIndex[num + 1]; |
| 3568 |
GunIndex[num + 1] = GunIndex[num]; |
| 3569 |
GunIndex[num] = tmp; |
| 3570 |
} |
| 3571 |
for (i = 0; i < NumGun; i++) { |
| 3572 |
if (Play->Guns[GunIndex[i]].Carried > 0) { |
| 3573 |
Play->Guns[GunIndex[i]].Carried--; |
| 3574 |
losedrug = 1; |
| 3575 |
Play->CoatSize += Gun[GunIndex[i]].Space; |
| 3576 |
if (Guns) |
| 3577 |
Guns[GunIndex[i]].Carried++; |
| 3578 |
break; |
| 3579 |
} |
| 3580 |
} |
| 3581 |
} |
| 3582 |
} |
| 3583 |
for (i = 0; i < NumDrug; i++) |
| 3584 |
if (Play->Drugs[i].Carried > 0) { |
| 3585 |
num = |
| 3586 |
(int)((float)Play->Drugs[i].Carried / |
| 3587 |
(Play->Bitches.Carried + 2.0) + 0.5); |
| 3588 |
if (num > 0) { |
| 3589 |
Play->Drugs[i].TotalValue = Play->Drugs[i].TotalValue * |
| 3590 |
(Play->Drugs[i].Carried - num) / Play->Drugs[i].Carried; |
| 3591 |
Play->Drugs[i].Carried -= num; |
| 3592 |
if (Drugs) |
| 3593 |
Drugs[i].Carried += num; |
| 3594 |
Play->CoatSize += num; |
| 3595 |
losedrug = 1; |
| 3596 |
} |
| 3597 |
} |
| 3598 |
while (Play->CoatSize < 0) { |
| 3599 |
drugslost = 0; |
| 3600 |
for (i = 0; i < NumDrug; i++) { |
| 3601 |
if (Play->Drugs[i].Carried > 0) { |
| 3602 |
losedrug = 1; |
| 3603 |
drugslost = 1; |
| 3604 |
Play->Drugs[i].TotalValue = Play->Drugs[i].TotalValue * |
| 3605 |
(Play->Drugs[i].Carried - 1) / Play->Drugs[i].Carried; |
| 3606 |
Play->Drugs[i].Carried--; |
| 3607 |
Play->CoatSize++; |
| 3608 |
if (Play->CoatSize >= 0) |
| 3609 |
break; |
| 3610 |
} |
| 3611 |
} |
| 3612 |
if (!drugslost) |
| 3613 |
for (i = 0; i < NumGun; i++) { |
| 3614 |
if (Play->Guns[i].Carried > 0) { |
| 3615 |
losedrug = 1; |
| 3616 |
Play->Guns[i].Carried--; |
| 3617 |
Play->CoatSize += Gun[i].Space; |
| 3618 |
if (Play->CoatSize >= 0) |
| 3619 |
break; |
| 3620 |
} |
| 3621 |
} |
| 3622 |
} |
| 3623 |
Play->Bitches.Carried--; |
| 3624 |
g_free(GunIndex); |
| 3625 |
return losedrug; |
| 3626 |
} |
| 3627 |
|
| 3628 |
/* |
| 3629 |
* If fight timeouts are in force, sets the timeout for the given player. |
| 3630 |
*/ |
| 3631 |
void SetFightTimeout(Player *Play) |
| 3632 |
{ |
| 3633 |
if (FightTimeout) { |
| 3634 |
Play->FightTimeout = time(NULL) + (time_t) FightTimeout; |
| 3635 |
|
| 3636 |
/* Make sure we have a higher tiebreak value than any other player with |
| 3637 |
* the same fight timeout (since FightTimeout only has second resolution, |
| 3638 |
* and possibly only microseconds have elapsed) */ |
| 3639 |
Play->tiebreak = 0; |
| 3640 |
if (Play->FightTimeout) { |
| 3641 |
GSList *listpt; |
| 3642 |
|
| 3643 |
for (listpt = FirstServer; listpt; listpt = g_slist_next(listpt)) { |
| 3644 |
Player *listplay = (Player *)listpt->data; |
| 3645 |
|
| 3646 |
if (listplay && listplay != Play |
| 3647 |
&& listplay->FightTimeout == Play->FightTimeout) { |
| 3648 |
Play->tiebreak = MAX(Play->tiebreak, listplay->tiebreak + 1); |
| 3649 |
} |
| 3650 |
} |
| 3651 |
} |
| 3652 |
} else { |
| 3653 |
Play->FightTimeout = 0; |
| 3654 |
} |
| 3655 |
} |
| 3656 |
|
| 3657 |
/* |
| 3658 |
* Removes any fight timeout for the given player. |
| 3659 |
*/ |
| 3660 |
void ClearFightTimeout(Player *Play) |
| 3661 |
{ |
| 3662 |
Play->FightTimeout = 0; |
| 3663 |
} |
| 3664 |
|
| 3665 |
/* |
| 3666 |
* Given the time of a pending event in "timeout" and the current time in |
| 3667 |
* "timenow", updates "mintime" with the number of seconds to that event, |
| 3668 |
* unless "mintime" is already smaller (as long as it's not -1, which |
| 3669 |
* means "uninitialized"). Returns 1 if the timeout has already expired. |
| 3670 |
*/ |
| 3671 |
long AddTimeout(time_t timeout, time_t timenow, long *mintime) |
| 3672 |
{ |
| 3673 |
if (timeout == 0) |
| 3674 |
return 0; |
| 3675 |
else if (timeout <= timenow) |
| 3676 |
return 1; |
| 3677 |
else { |
| 3678 |
if (*mintime < 0 || timeout - timenow < *mintime) |
| 3679 |
*mintime = timeout - timenow; |
| 3680 |
return 0; |
| 3681 |
} |
| 3682 |
} |
| 3683 |
|
| 3684 |
/* |
| 3685 |
* Returns the number of seconds until the next scheduled event. If such |
| 3686 |
* an event has already expired, returns 0. If no events are pending, |
| 3687 |
* returns -1. "First" should point to a list of valid players. |
| 3688 |
*/ |
| 3689 |
long GetMinimumTimeout(GSList *First) |
| 3690 |
{ |
| 3691 |
Player *Play; |
| 3692 |
GSList *list; |
| 3693 |
long mintime = -1; |
| 3694 |
time_t timenow; |
| 3695 |
|
| 3696 |
timenow = time(NULL); |
| 3697 |
#ifdef NETWORKING |
| 3698 |
curl_multi_timeout(MetaConn.multi, &mintime); |
| 3699 |
#endif |
| 3700 |
if (AddTimeout(MetaMinTimeout, timenow, &mintime)) |
| 3701 |
return 0; |
| 3702 |
if (AddTimeout(MetaUpdateTimeout, timenow, &mintime)) |
| 3703 |
return 0; |
| 3704 |
for (list = First; list; list = g_slist_next(list)) { |
| 3705 |
Play = (Player *)list->data; |
| 3706 |
if (AddTimeout(Play->FightTimeout, timenow, &mintime) || |
| 3707 |
AddTimeout(Play->IdleTimeout, timenow, &mintime) || |
| 3708 |
AddTimeout(Play->ConnectTimeout, timenow, &mintime)) |
| 3709 |
return 0; |
| 3710 |
} |
| 3711 |
return mintime; |
| 3712 |
} |
| 3713 |
|
| 3714 |
/* |
| 3715 |
* Given a list of players in "First", checks to see if any events |
| 3716 |
* have timed out, and if so, performs the necessary actions. The |
| 3717 |
* new start of the list is returned, since a player may be removed |
| 3718 |
* if their ConnectTimeout has expired. |
| 3719 |
*/ |
| 3720 |
GSList *HandleTimeouts(GSList *First) |
| 3721 |
{ |
| 3722 |
GSList *list, *nextlist; |
| 3723 |
Player *Play; |
| 3724 |
time_t timenow; |
| 3725 |
|
| 3726 |
timenow = time(NULL); |
| 3727 |
if (MetaMinTimeout <= timenow) { |
| 3728 |
MetaMinTimeout = 0; |
| 3729 |
if (MetaPlayerPending) { |
| 3730 |
dopelog(3, LF_SERVER, _("Sending pending updates to the metaserver...")); |
| 3731 |
RegisterWithMetaServer(TRUE, TRUE, FALSE); |
| 3732 |
} |
| 3733 |
} |
| 3734 |
if (MetaUpdateTimeout != 0 && MetaUpdateTimeout <= timenow) { |
| 3735 |
dopelog(3, LF_SERVER, _("Sending reminder message to the metaserver...")); |
| 3736 |
RegisterWithMetaServer(TRUE, FALSE, FALSE); |
| 3737 |
} |
| 3738 |
list = First; |
| 3739 |
while (list) { |
| 3740 |
nextlist = g_slist_next(list); |
| 3741 |
Play = (Player *)list->data; |
| 3742 |
if (Play->IdleTimeout != 0 && Play->IdleTimeout <= timenow) { |
| 3743 |
Play->IdleTimeout = 0; |
| 3744 |
dopelog(1, LF_SERVER, _("Player removed due to idle timeout")); |
| 3745 |
SendPrintMessage(NULL, C_NONE, Play, |
| 3746 |
"Disconnected due to idle timeout"); |
| 3747 |
ClientLeftServer(Play); |
| 3748 |
/* Blank the name, so that CountPlayers ignores this player */ |
| 3749 |
SetPlayerName(Play, NULL); |
| 3750 |
/* Make sure they do actually disconnect, eventually! */ |
| 3751 |
if (ConnectTimeout) { |
| 3752 |
Play->ConnectTimeout = time(NULL) + (time_t) ConnectTimeout; |
| 3753 |
} |
| 3754 |
} else if (Play->ConnectTimeout != 0 |
| 3755 |
&& Play->ConnectTimeout <= timenow) { |
| 3756 |
Play->ConnectTimeout = 0; |
| 3757 |
dopelog(1, LF_SERVER, _("Player removed due to connect timeout")); |
| 3758 |
First = RemovePlayer(Play, First); |
| 3759 |
} else if (IsConnectedPlayer(Play) && |
| 3760 |
Play->FightTimeout != 0 && Play->FightTimeout <= timenow) { |
| 3761 |
ClearFightTimeout(Play); |
| 3762 |
if (IsCop(Play)) |
| 3763 |
Fire(Play); |
| 3764 |
else |
| 3765 |
SendFightReload(Play); |
| 3766 |
} |
| 3767 |
list = nextlist; |
| 3768 |
} |
| 3769 |
return First; |
| 3770 |
} |