Subversion Repositories Games.Chess Giants

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 1
// playercard.cpp
2
 
3
#include "common.h"
4
 
5
 
6
void PlayerCards_Init (void)
7
{
8
   // this function resets the player cards array, preparing it for a new use
9
 
10
   playercards = NULL; // reset the array pointer
11
   playercard_count = 0; // and set the element count to zero
12
 
13
   return; // that's all there is
14
}
15
 
16
 
17
void PlayerCards_Shutdown (void)
18
{
19
   // this function empties the player cards array, closing windows and freeing allocated resources
20
 
21
   int playercard_index;
22
 
23
   // for each player card we know...
24
   for (playercard_index = 0; playercard_index < playercard_count; playercard_index++)
25
   {
26
      if (IsWindow (playercards[playercard_index].hWnd))
27
         DestroyWindow (playercards[playercard_index].hWnd); // close any eventual player card window
28
      SAFE_free ((void **) &playercards[playercard_index].fingertext); // free the player card's finger text buffer
29
      playercards[playercard_index].fingertext_length = 0; // and reset this buffer's size
30
   }
31
   SAFE_free ((void **) &playercards); // free the player cards array
32
   playercard_count = 0; // and reset the player cards count
33
 
34
   return; // finished
35
}
36
 
37
 
38
playercard_t *PlayerCard_FindOrCreate (const wchar_t *nickname)
39
{
40
   // this function returns the index in the player cards array of the player card whose nickname is specified,
41
   // creating a new slot for him if he can't be found.
42
 
43
   playercard_t *playercard;
44
   int playercard_index;
45
   player_t *local_player;
46
 
47
   // see if we are already displaying this player card, loop through all of them...
48
   for (playercard_index = 0; playercard_index < playercard_count; playercard_index++)
49
      if (playercards[playercard_index].is_active && (_wcsicmp (nickname, playercards[playercard_index].nickname) == 0))
50
         break; // break as soon as we find it
51
 
52
   // have we NOT found it ?
53
   if (playercard_index == playercard_count)
54
   {
55
      // no exact match found ; loop again to see if we have a similar name in the empty cards
56
      for (playercard_index = 0; playercard_index < playercard_count; playercard_index++)
57
         if (playercards[playercard_index].is_active && !playercards[playercard_index].got_reply
58
             && (_wcsnicmp (nickname, playercards[playercard_index].nickname, wcslen (playercards[playercard_index].nickname)) == 0))
59
            break; // break as soon as we find it
60
   }
61
 
62
   // have we STILL NOT found it ?
63
   if (playercard_index == playercard_count)
64
   {
65
      // we haven't found it, so add a new player card in our array. See if there's a free slot first...
66
      for (playercard_index = 0; playercard_index < playercard_count; playercard_index++)
67
         if (!playercards[playercard_index].is_active)
68
            break; // break as soon as we find one
69
 
70
      // have we NOT found one ? if so, resize our player cards array and add this playercard in it
71
      if (playercard_index == playercard_count)
72
      {
73
         playercards = (playercard_t *) SAFE_realloc (playercards, playercard_count, playercard_count + 1, sizeof (playercard_t), false);
74
         playercard_count++; // we know now one player card more
75
      }
76
 
77
      // get a quick access to playercard. Only do it after the realloc, as the array may have moved.
78
      playercard = &playercards[playercard_index];
79
 
80
      // find local player and see whether this player card is ours
81
      local_player = Player_FindByType (PLAYER_HUMAN);
82
 
83
      // reset some values for this new player card
84
      playercard->is_active = true;
85
      playercard->is_own = ((local_player != NULL) && (_wcsicmp (nickname, local_player->name) == 0));
86
      wcscpy_s (playercard->nickname, WCHAR_SIZEOF (playercard->nickname), nickname); // save nickname
87
      playercard->got_reply = false;
88
      playercard->doesnt_exist = false;
89
      playercard->hWnd = NULL;
90
      playercard->fingertext = NULL; // no finger text yet
91
      playercard->fingertext_length = 0;
92
      playercard->gamestyleratings = NULL; // no game style ratings yet
93
      playercard->gamestylerating_count = 0;
94
      playercard->minutes_online = 0;
95
      playercard->seconds_idle = 0;
96
      playercard->disconnection_day = 0;
97
      playercard->disconnection_month = 0;
98
      playercard->disconnection_year = 0;
99
      playercard->game_played = 0;
100
      playercard->game_name[0] = 0;
101
 
102
      // and fire up the new player card dialog box
103
      DialogBox_PlayerCard (playercard_index);
104
   }
105
 
106
   // else we've found it
107
   else
108
   {
109
      // get a quick access to player card
110
      playercard = &playercards[playercard_index];
111
 
112
      // does the player card window exist AND is it currently NOT the foreground window ?
113
      if (IsWindow (playercard->hWnd) && (GetForegroundWindow () != playercard->hWnd))
114
      {
115
         ShowWindow (playercard->hWnd, SW_RESTORE); // restore it from the taskbar
116
         SetForegroundWindow (playercard->hWnd); // bring the window to front if necessary
117
      }
118
   }
119
 
120
   return (playercard); // return pointer to player card
121
}
122
 
123
 
124
void PlayerCard_AppendPersonalData (playercard_t *playercard, const wchar_t *text)
125
{
126
   // helper function to quickly add an entry to an player card's personal data window
127
 
128
   static const wchar_t *format_string = L"%s\r\n";
129
   static const int extra_length = 2;
130
 
131
   int text_length;
132
 
133
   // get text length
134
   text_length = wcslen (text);
135
 
136
   // resize the personal data text buffer
137
   playercard->fingertext = (wchar_t *) SAFE_realloc (playercard->fingertext,
138
                                                      playercard->fingertext_length,
139
                                                      playercard->fingertext_length + text_length + extra_length + 1,
140
                                                      sizeof (wchar_t), false);
141
 
142
   // append the message to the personal data text and save the new text length
143
   swprintf_s (&playercard->fingertext[playercard->fingertext_length],
144
               text_length + extra_length + 1, // +1 for null terminator
145
               format_string, text);
146
   playercard->fingertext_length += text_length + extra_length; // save new string length
147
 
148
   return; // finished
149
}