Subversion Repositories Games.Chess Giants

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 1
// challenge.cpp
2
 
3
#include "common.h"
4
 
5
 
6
void Challenges_Init (void)
7
{
8
   // this function resets the challenges array, preparing it for a new use
9
 
10
   challenges = NULL; // reset the array pointer
11
   challenge_count = 0; // and set the element count to zero
12
 
13
   return; // that's all there is
14
}
15
 
16
 
17
void Challenges_Shutdown (void)
18
{
19
   // this function empties the challenges array, closing windows and freeing allocated resources
20
 
21
   int challenge_index;
22
 
23
   // for each challenge we know...
24
   for (challenge_index = 0; challenge_index < challenge_count; challenge_index++)
25
      if (IsWindow (challenges[challenge_index].hWnd))
26
         EndDialog (challenges[challenge_index].hWnd, 0); // close any eventual opened challenge window
27
   SAFE_free ((void **) &challenges); // free the challenges array
28
   challenge_count = 0; // and reset the challenges count
29
 
30
   return; // finished
31
}
32
 
33
 
34
challenge_t *Challenge_FindOrCreate (const wchar_t *nickname)
35
{
36
   // this function returns a pointer to the interlocutor whose nickname is specified,
37
   // creating a new slot for him in the interlocutors array if he can't be found.
38
 
39
   challenge_t *challenge;
40
   int challenge_index;
41
 
42
   // see if we are already displaying this challenge, loop through all of them...
43
   for (challenge_index = 0; challenge_index < challenge_count; challenge_index++)
44
      if (challenges[challenge_index].is_active && (_wcsicmp (nickname, challenges[challenge_index].challenger) == 0))
45
         break; // break as soon as we find it
46
 
47
   // have we NOT found it ?
48
   if (challenge_index == challenge_count)
49
   {
50
      // we haven't found it, so add a new challenge in our array. See if there's a free slot first...
51
      for (challenge_index = 0; challenge_index < challenge_count; challenge_index++)
52
         if (!challenges[challenge_index].is_active)
53
            break; // break as soon as we find one
54
 
55
      // have we NOT found one ? if so, resize our challenges array and add this challenge in it
56
      if (challenge_index == challenge_count)
57
      {
58
         challenges = (challenge_t *) SAFE_realloc (challenges, challenge_count, challenge_count + 1, sizeof (challenge_t), false);
59
         challenge_count++; // we know now one challenge more
60
      }
61
 
62
      // get a quick access to challenge. Only do it after the realloc, as the array may have moved.
63
      challenge = &challenges[challenge_index];
64
 
65
      // reset some values for this new challenge
66
      memset (challenge, 0, sizeof (challenge_t));
67
      challenge->is_active = true;
68
      wcscpy_s (challenge->challenger, WCHAR_SIZEOF (challenge->challenger), nickname); // save nickname
69
 
70
      // and fire up the new challenge dialog box
71
      DialogBox_Challenge (challenge_index);
72
   }
73
 
74
   // else we've found it
75
   else
76
   {
77
      // get a quick access to challenge
78
      challenge = &challenges[challenge_index];
79
 
80
      // does the challenge window exist AND is it currently NOT the foreground window ?
81
      if (IsWindow (challenge->hWnd) && (GetForegroundWindow () != challenge->hWnd))
82
      {
83
         ShowWindow (challenge->hWnd, SW_RESTORE); // restore it from the taskbar
84
         SetForegroundWindow (challenge->hWnd); // bring the window to front if necessary
85
      }
86
   }
87
 
88
   return (challenge); // return index of challenge
89
}
90
 
91
 
92
challenge_t *Challenge_Find (const wchar_t *nickname)
93
{
94
   // this function returns a pointer to the interlocutor whose nickname is specified
95
 
96
   int challenge_index;
97
 
98
   // see if we are already displaying this challenge, loop through all of them...
99
   for (challenge_index = 0; challenge_index < challenge_count; challenge_index++)
100
      if (challenges[challenge_index].is_active && (_wcsicmp (nickname, challenges[challenge_index].challenger) == 0))
101
         return (&challenges[challenge_index]); // found it, return a pointer to it
102
 
103
   return (NULL); // we couldn't find this challenge, return NULL
104
}
105
 
106
 
107
void Challenge_UpdateData (challenge_t *challenge, challenge_t *new_challenge)
108
{
109
   // helper function to quickly copy the game data of a new challenge structure into another
110
 
111
   // update challenge data
112
   challenge->challenger_level = new_challenge->challenger_level;
113
   challenge->color = new_challenge->color;
114
   challenge->is_rated = new_challenge->is_rated;
115
   wcscpy_s (challenge->game_type, WCHAR_SIZEOF (challenge->game_type), new_challenge->game_type);
116
   challenge->initial_time = new_challenge->initial_time;
117
   challenge->increment = new_challenge->increment;
118
 
119
   // and tell our challenge window there's a new message to display
120
   challenge->update_dialog = true;
121
 
122
   return; // finished
123
}