// challenge.cpp
 
 
 
#include "common.h"
 
 
 
 
 
void Challenges_Init (void)
 
{
 
   // this function resets the challenges array, preparing it for a new use
 
 
 
   challenges = NULL; // reset the array pointer
 
   challenge_count = 0; // and set the element count to zero
 
 
 
   return; // that's all there is
 
}
 
 
 
 
 
void Challenges_Shutdown (void)
 
{
 
   // this function empties the challenges array, closing windows and freeing allocated resources
 
 
 
   int challenge_index;
 
 
 
   // for each challenge we know...
 
   for (challenge_index = 0; challenge_index < challenge_count; challenge_index++)
 
      if (IsWindow (challenges[challenge_index].hWnd))
 
         EndDialog (challenges[challenge_index].hWnd, 0); // close any eventual opened challenge window
 
   SAFE_free ((void **) &challenges); // free the challenges array
 
   challenge_count = 0; // and reset the challenges count
 
 
 
   return; // finished
 
}
 
 
 
 
 
challenge_t *Challenge_FindOrCreate (const wchar_t *nickname)
 
{
 
   // this function returns a pointer to the interlocutor whose nickname is specified,
 
   // creating a new slot for him in the interlocutors array if he can't be found.
 
 
 
   challenge_t *challenge;
 
   int challenge_index;
 
 
 
   // see if we are already displaying this challenge, loop through all of them...
 
   for (challenge_index = 0; challenge_index < challenge_count; challenge_index++)
 
      if (challenges[challenge_index].is_active && (_wcsicmp (nickname, challenges[challenge_index].challenger) == 0))
 
         break; // break as soon as we find it
 
 
 
   // have we NOT found it ?
 
   if (challenge_index == challenge_count)
 
   {
 
      // we haven't found it, so add a new challenge in our array. See if there's a free slot first...
 
      for (challenge_index = 0; challenge_index < challenge_count; challenge_index++)
 
         if (!challenges[challenge_index].is_active)
 
            break; // break as soon as we find one
 
 
 
      // have we NOT found one ? if so, resize our challenges array and add this challenge in it
 
      if (challenge_index == challenge_count)
 
      {
 
         challenges = (challenge_t *) SAFE_realloc (challenges, challenge_count, challenge_count + 1, sizeof (challenge_t), false);
 
         challenge_count++; // we know now one challenge more
 
      }
 
 
 
      // get a quick access to challenge. Only do it after the realloc, as the array may have moved.
 
      challenge = &challenges[challenge_index];
 
 
 
      // reset some values for this new challenge
 
      memset (challenge, 0, sizeof (challenge_t));
 
      challenge->is_active = true;
 
      wcscpy_s (challenge->challenger, WCHAR_SIZEOF (challenge->challenger), nickname); // save nickname
 
 
 
      // and fire up the new challenge dialog box
 
      DialogBox_Challenge (challenge_index);
 
   }
 
 
 
   // else we've found it
 
   else
 
   {
 
      // get a quick access to challenge
 
      challenge = &challenges[challenge_index];
 
 
 
      // does the challenge window exist AND is it currently NOT the foreground window ?
 
      if (IsWindow (challenge->hWnd) && (GetForegroundWindow () != challenge->hWnd))
 
      {
 
         ShowWindow (challenge->hWnd, SW_RESTORE); // restore it from the taskbar
 
         SetForegroundWindow (challenge->hWnd); // bring the window to front if necessary
 
      }
 
   }
 
 
 
   return (challenge); // return index of challenge
 
}
 
 
 
 
 
challenge_t *Challenge_Find (const wchar_t *nickname)
 
{
 
   // this function returns a pointer to the interlocutor whose nickname is specified
 
 
 
   int challenge_index;
 
 
 
   // see if we are already displaying this challenge, loop through all of them...
 
   for (challenge_index = 0; challenge_index < challenge_count; challenge_index++)
 
      if (challenges[challenge_index].is_active && (_wcsicmp (nickname, challenges[challenge_index].challenger) == 0))
 
         return (&challenges[challenge_index]); // found it, return a pointer to it
 
 
 
   return (NULL); // we couldn't find this challenge, return NULL
 
}
 
 
 
 
 
void Challenge_UpdateData (challenge_t *challenge, challenge_t *new_challenge)
 
{
 
   // helper function to quickly copy the game data of a new challenge structure into another
 
 
 
   // update challenge data
 
   challenge->challenger_level = new_challenge->challenger_level;
 
   challenge->color = new_challenge->color;
 
   challenge->is_rated = new_challenge->is_rated;
 
   wcscpy_s (challenge->game_type, WCHAR_SIZEOF (challenge->game_type), new_challenge->game_type);
 
   challenge->initial_time = new_challenge->initial_time;
 
   challenge->increment = new_challenge->increment;
 
 
 
   // and tell our challenge window there's a new message to display
 
   challenge->update_dialog = true;
 
 
 
   return; // finished
 
}