home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD1.bin
/
new
/
game
/
think
/
chaos
/
src
/
players.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-10-14
|
14KB
|
593 lines
/* Chaos: The Chess HAppening Organisation System V5.3
Copyright (C) 1993 Jochen Wiedmann
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
$RCSfile: Players.c,v $
$Revision: 3.2 $
$Date: 1994/10/14 09:44:02 $
This file contains the functions of the Players-menu.
Computer: Amiga 1200 Compiler: Dice 2.07.54 (3.0)
Author: Jochen Wiedmann
Am Eisteich 9
72555 Metzingen
Phone: 07123 / 14881
Internet: wiedmann@mailserv.zdv.uni-tuebingen.de
*/
#ifndef CHAOS_H
#include "chaos.h"
#endif
#include <time.h>
/*
InsertPlayer inserts a player into the alphabetically sorted list of
players.
Inputs: plrlist - pointer to the list, where the player should be
inserted
tn - pointer to the Player structure of the new player
*/
static void InsertPlayer (struct List *plrlist, struct Player *tn)
{ struct Player *node;
for (node = (struct Player *) PlayerList.lh_Head;
node->Tn_Node.ln_Succ != NULL;
node = (struct Player *) node->Tn_Node.ln_Succ)
{ if (Stricmp((STRPTR) tn->Name, (STRPTR) node->Name) < 0)
{ break;
}
}
Insert(&PlayerList, (struct Node *) tn, node->Tn_Node.ln_Pred);
}
/*
This function removes preceding blanks from a string.
Inputs: str - a pointer to the string, where the blanks should
be removed
Result: a pointer to the first nonblank character in str (may be \0!)
*/
static char *WithoutBlanks(char *str)
{ if (str != NULL)
{ while (*str == ' ')
{ ++str;
}
}
return(str);
}
/*
CheckPlayerValid() checks, if a players data is valid.
Inputs: plr - a pointer to the player structure
user - TRUE, if an error message may be shown to the user
Result: TRUE, if the player is valid, FALSE otherwise
Note: This functions assumes, that plr is not included in PlayerList!
*/
int CheckPlayerValid(struct Player *plr, int user)
{ struct Player *cmpplr;
char *birthday = WithoutBlanks(plr->BirthDay);
char *name = WithoutBlanks(plr->Name);
struct tm tm;
/*
Check if the players name and birthday are valid
*/
if (*WithoutBlanks(name) == '\0')
{ if (user)
{ ShowError((char *) GetChaosString(MSG_MISSING_PLAYER_NAME));
}
return(FALSE);
}
if (*birthday != '\0' && !atotm(birthday, &tm))
{ if (user)
{ ShowError((char *) GetChaosString(MSG_BIRTHDAY_ERROR));
}
return(FALSE);
}
/*
Check, if the players name is unique.
*/
for (cmpplr = (struct Player *) PlayerList.lh_Head;
cmpplr->Tn_Node.ln_Succ != NULL;
cmpplr = (struct Player *) cmpplr->Tn_Node.ln_Succ)
{ if (Stricmp((STRPTR) cmpplr->Name, (STRPTR) name) == 0)
{ if (user)
{ ShowError((char *) GetChaosString(MSG_PlayerExists), plr->Name);
}
return(FALSE);
}
}
return(TRUE);
}
/*
The AddPlayer() function adds a new player to the list of players.
It is assumed, that CheckPlayer() and CheckPlayerNameUnique() are
already called.
Inputs: plr a pointer to a player structure
Results: TRUE, if successfull, FALSE otherwise
*/
int AddPlayer(struct Player *plr)
{ struct Player *newplr, **plrptr;
struct Game *game;
/*
Allocate memory for the new player
*/
if ((newplr = GetMem(&TrnMem, sizeof(*newplr))) == NULL)
{ return(FALSE);
}
/*
Initialize data
*/
strcpy(newplr->Name, WithoutBlanks(plr->Name));
strcpy(newplr->Street, WithoutBlanks(plr->Street));
strcpy(newplr->Village, WithoutBlanks(plr->Village));
strcpy(newplr->PhoneNr, WithoutBlanks(plr->PhoneNr));
strcpy(newplr->ChessClub, WithoutBlanks(plr->ChessClub));
strcpy(newplr->BirthDay, WithoutBlanks(plr->BirthDay));
strcpy(newplr->DWZ, WithoutBlanks(plr->DWZ));
newplr->ELO = plr->ELO;
newplr->Flags = plr->Flags & (TNFLAGSF_SENIOR|TNFLAGSF_JUNIOR|
TNFLAGSF_WOMAN|TNFLAGSF_JUNIORA|
TNFLAGSF_JUNIORB|TNFLAGSF_JUNIORC|
TNFLAGSF_JUNIORD|TNFLAGSF_JUNIORE);
newplr->Tn_Node.ln_Name = newplr->Name;
newplr->Tn_Node.ln_Type = (UBYTE) -1;
newplr->Tn_Node.ln_Pri = 0;
/*
The Swiss Pairing allows adding players, until round 1 is finished.
Possibly we have to select an opponent.
*/
if(NumRounds > 0)
{ /*
Allocate a Game structure
*/
if ((game = newplr->First_Game = GetMem(&TrnMem, sizeof(*game)))
== NULL)
{ PutMem(newplr);
return(FALSE);
}
game->BoardNr = NumPlayers / 2;
/*
Add the new player to the bottom of the internal rankings.
*/
for (plrptr = &RankingFirst; *plrptr != NULL;
plrptr = &((*plrptr)->RankNext))
{
}
*plrptr = newplr;
/*
If the number of players was odd, the new players opponent will
be the player, who had a point for free.
*/
if ((NumPlayers % 2) != 0)
{ struct Player *opponent;
for (opponent = RankingFirst;
(opponent->Flags & TNFLAGSF_HADFREE) == 0;
opponent = opponent->RankNext)
{
}
opponent->Flags &= ~TNFLAGSF_HADFREE;
game->Opponent = opponent;
game->Result = -1;
game->Flags = (RangeRand(2) == 0) ? GMFLAGSF_WHITE : 0;
newplr->HowMuchWhite = newplr->HowMuchWhiteLast =
game->Flags ? 1 : -1;
game = opponent->First_Game;
game->Opponent = newplr;
game->Result = -1;
game->BoardNr = newplr->First_Game->BoardNr;
game->Flags = GMFLAGSF_WHITE-newplr->First_Game->Flags;
opponent->HowMuchWhite = opponent->HowMuchWhiteLast =
game->Flags ? 1 : -1;
opponent->Points = 0;
NumGamesMissing++;
}
/*
The new player gets a point for free, if the number of players
was even.
*/
else
{ newplr->Flags |= TNFLAGSF_HADFREE;
newplr->Points = WinnerPoints;
game->Result = 2;
game->Flags = GMFLAGSF_POINTFORFREE|GMFLAGSF_NOFIGHT;
}
}
InsertPlayer(&PlayerList, newplr);
NumPlayers++;
IsSaved = FALSE;
return(TRUE);
}
/*
The AddPlayers() function allows the user to enter new players.
*/
void AddPlayers(void)
{ struct Player plr;
int new = TRUE;
/*
Check, if we may enter new players.
*/
if (NumRounds >= 1 && RoundRobinTournament)
{ ShowError((char *) GetChaosString(MSG_NO_NEW_PLAYERS_ROUND_ROBIN));
return;
}
if (NumRounds > 1 && SwissPairingTournament)
{ ShowError((char *) GetChaosString(MSG_NO_NEW_PLAYERS_SWISS_PAIR));
return;
}
if (NumRounds > 0)
{ if (!AskContinue((char *) GetChaosString(MSG_NEW_PLAYER_REQUEST)))
{ return;
}
}
if (!InitPlrWnd((char *) GetChaosString(WND_PLAYER_ADD_TITLE)))
{ return;
}
memset(&plr, 0, sizeof(plr));
for (;;)
{ if (!ProcessPlrWnd(&plr, new))
{ break;
}
new = FALSE;
if (CheckPlayerValid(&plr, TRUE))
{ if (!AddPlayer(&plr))
{ break;
}
memset(&plr, 0, sizeof(plr));
new = TRUE;
}
}
TerminatePlrWnd();
}
/*
The InportPlayers() function allows to add players from a previous
tournament.
*/
void ImportPlayers(void)
{ void *PlrMem = NULL;
struct List ImportPlayerList;
/*
Check, if we may enter new players.
*/
if (NumRounds >= 1 && RoundRobinTournament)
{ ShowError((char *) GetChaosString(MSG_NO_NEW_PLAYERS_ROUND_ROBIN));
return;
}
if (NumRounds > 1 && SwissPairingTournament)
{ ShowError((char *) GetChaosString(MSG_NO_NEW_PLAYERS_SWISS_PAIR));
return;
}
if (NumRounds > 0)
{ if (!AskContinue((char *) GetChaosString(MSG_NEW_PLAYER_REQUEST)))
{ return;
}
}
if (LoadTournament(NUL