// playercard.cpp
#include "common.h"
void PlayerCards_Init (void)
{
// this function resets the player cards array, preparing it for a new use
playercards = NULL; // reset the array pointer
playercard_count = 0; // and set the element count to zero
return; // that's all there is
}
void PlayerCards_Shutdown (void)
{
// this function empties the player cards array, closing windows and freeing allocated resources
int playercard_index;
// for each player card we know...
for (playercard_index = 0; playercard_index < playercard_count; playercard_index++)
{
if (IsWindow (playercards[playercard_index].hWnd))
DestroyWindow (playercards[playercard_index].hWnd); // close any eventual player card window
SAFE_free ((void **) &playercards[playercard_index].fingertext); // free the player card's finger text buffer
playercards[playercard_index].fingertext_length = 0; // and reset this buffer's size
}
SAFE_free ((void **) &playercards); // free the player cards array
playercard_count = 0; // and reset the player cards count
return; // finished
}
playercard_t *PlayerCard_FindOrCreate (const wchar_t *nickname)
{
// this function returns the index in the player cards array of the player card whose nickname is specified,
// creating a new slot for him if he can't be found.
playercard_t *playercard;
int playercard_index;
player_t *local_player;
// see if we are already displaying this player card, loop through all of them...
for (playercard_index = 0; playercard_index < playercard_count; playercard_index++)
if (playercards[playercard_index].is_active && (_wcsicmp (nickname, playercards[playercard_index].nickname) == 0))
break; // break as soon as we find it
// have we NOT found it ?
if (playercard_index == playercard_count)
{
// no exact match found ; loop again to see if we have a similar name in the empty cards
for (playercard_index = 0; playercard_index < playercard_count; playercard_index++)
if (playercards[playercard_index].is_active && !playercards[playercard_index].got_reply
&& (_wcsnicmp (nickname, playercards[playercard_index].nickname, wcslen (playercards[playercard_index].nickname)) == 0))
break; // break as soon as we find it
}
// have we STILL NOT found it ?
if (playercard_index == playercard_count)
{
// we haven't found it, so add a new player card in our array. See if there's a free slot first...
for (playercard_index = 0; playercard_index < playercard_count; playercard_index++)
if (!playercards[playercard_index].is_active)
break; // break as soon as we find one
// have we NOT found one ? if so, resize our player cards array and add this playercard in it
if (playercard_index == playercard_count)
{
playercards = (playercard_t *) SAFE_realloc (playercards, playercard_count, playercard_count + 1, sizeof (playercard_t), false);
playercard_count++; // we know now one player card more
}
// get a quick access to playercard. Only do it after the realloc, as the array may have moved.
playercard = &playercards[playercard_index];
// find local player and see whether this player card is ours
local_player = Player_FindByType (PLAYER_HUMAN);
// reset some values for this new player card
playercard->is_active = true;
playercard->is_own = ((local_player != NULL) && (_wcsicmp (nickname, local_player->name) == 0));
wcscpy_s (playercard->nickname, WCHAR_SIZEOF (playercard->nickname), nickname); // save nickname
playercard->got_reply = false;
playercard->doesnt_exist = false;
playercard->hWnd = NULL;
playercard->fingertext = NULL; // no finger text yet
playercard->fingertext_length = 0;
playercard->gamestyleratings = NULL; // no game style ratings yet
playercard->gamestylerating_count = 0;
playercard->minutes_online = 0;
playercard->seconds_idle = 0;
playercard->disconnection_day = 0;
playercard->disconnection_month = 0;
playercard->disconnection_year = 0;
playercard->game_played = 0;
playercard->game_name[0] = 0;
// and fire up the new player card dialog box
DialogBox_PlayerCard (playercard_index);
}
// else we've found it
else
{
// get a quick access to player card
playercard = &playercards[playercard_index];
// does the player card window exist AND is it currently NOT the foreground window ?
if (IsWindow (playercard->hWnd) && (GetForegroundWindow () != playercard->hWnd))
{
ShowWindow (playercard->hWnd, SW_RESTORE); // restore it from the taskbar
SetForegroundWindow (playercard->hWnd); // bring the window to front if necessary
}
}
return (playercard); // return pointer to player card
}
void PlayerCard_AppendPersonalData (playercard_t *playercard, const wchar_t *text)
{
// helper function to quickly add an entry to an player card's personal data window
static const wchar_t *format_string = L"%s\r\n";
static const int extra_length = 2;
int text_length;
// get text length
text_length = wcslen (text);
// resize the personal data text buffer
playercard->fingertext = (wchar_t *) SAFE_realloc (playercard->fingertext,
playercard->fingertext_length,
playercard->fingertext_length + text_length + extra_length + 1,
sizeof (wchar_t), false);
// append the message to the personal data text and save the new text length
swprintf_s (&playercard->fingertext[playercard->fingertext_length],
text_length + extra_length + 1, // +1 for null terminator
format_string, text);
playercard->fingertext_length += text_length + extra_length; // save new string length
return; // finished
}