Subversion Repositories Games.Chess Giants

Rev

Rev 161 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 1
// dialog_sendchallenge.cpp
2
 
3
#include "../common.h"
4
 
5
 
6
// dialog template
7
#define THIS_DIALOG DIALOG_SENDCHALLENGE
8
 
9
 
10
// local type definitions
11
typedef struct sendchallenge_s
12
{
13
   wchar_t challengee[32]; // challengee's nickname
172 pmbaty 14
   wchar_t game_rules[32]; // the rules with which to play (standard, losers, etc)
1 pmbaty 15
   int color; // color we want to play
16
   bool is_rated; // whether this game is rated or not
17
   int initial_time; // initial time of the game for each color
18
   int increment; // eventual time increment (Fischer pace)
19
} sendchallenge_t;
20
 
21
 
22
// global variables used in this module only
172 pmbaty 23
static sendchallenge_t sendchallenge = { L"", L"", COLOR_UNSPECIFIED, false, 2, 12};
1 pmbaty 24
static HWND hThisWnd = NULL;
25
 
26
 
27
// prototypes of local functions
28
static void StartThread_ThisDialog (void *thread_parms);
29
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
30
 
31
 
32
void DialogBox_SendChallenge (wchar_t *challengee_name)
33
{
34
   // helper function to fire up the modeless dialog box
35
 
36
   // prepare the sendchallenge structure
37
   wcscpy_s (sendchallenge.challengee, WCHAR_SIZEOF (sendchallenge.challengee), challengee_name);
172 pmbaty 38
   sendchallenge.game_rules[0] = 0; // no special rules until told otherwise
1 pmbaty 39
 
140 pmbaty 40
   is_dialogbox_displayed = true;
1 pmbaty 41
   _beginthread (StartThread_ThisDialog, 0, NULL); // fire up the thread
42
   return; // return as soon as the thread is fired up
43
}
44
 
45
 
46
void DialogBox_SendChallenge_Validated (void)
47
{
48
   // callback function called by the main game thread when the dialog box is validated
49
 
50
   player_t *network_player;
51
 
52
   // remember this callback is no longer to be called
53
   is_dialogbox_sendchallenge_validated = false;
54
 
55
   network_player = Player_FindByType (PLAYER_INTERNET); // quick access to network player
56
   if (network_player == NULL)
57
      return; // consistency check
58
 
59
   // send the challenge
172 pmbaty 60
   Player_SendBuffer_Add (network_player, 1000, L"match %s %s %d %d %s %s\n",
1 pmbaty 61
                                                sendchallenge.challengee,
62
                                                (sendchallenge.is_rated ? L"rated" : L"unrated"),
63
                                                sendchallenge.initial_time,
64
                                                sendchallenge.increment,
172 pmbaty 65
                                                (sendchallenge.color == COLOR_BLACK ? L"black" : (sendchallenge.color == COLOR_WHITE ? L"white" : L"")),
66
                                                sendchallenge.game_rules);
1 pmbaty 67
 
68
   // send a notification to this player's chat window
69
   Interlocutor_Notify (Interlocutor_FindOrCreate (sendchallenge.challengee), LOCALIZE (L"Chat_InvitationSent"), sendchallenge.challengee);
70
   return; // finished, invitation is sent
71
}
72
 
73
 
74
static void StartThread_ThisDialog (void *thread_parms)
75
{
76
   // this function runs in a separate thread, for that's the only way (seemingly)
77
   // to implement a non-modal message box using the Common Controls library.
78
 
79
   if (IsWindow (hThisWnd))
80
      SendMessage (hThisWnd, WM_CLOSE, 0, 0); // if such a dialog already exists, close it
81
 
82
   // display the dialog box
83
   if (DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog) == 1)
84
      is_dialogbox_sendchallenge_validated = true; // notify main game thread to fire up our callback
140 pmbaty 85
   is_dialogbox_displayed = false;
1 pmbaty 86
 
124 pmbaty 87
   the_board.reevaluate = true; // refresh the GUI buttons if needed
1 pmbaty 88
   return; // _endthread() implied
89
}
90
 
91
 
92
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
93
{
94
   // message handler for the dialog box
95
 
96
   unsigned short wParam_hiword;
97
   unsigned short wParam_loword;
98
   wchar_t temp_string[256];
99
   BOOL was_translated; // needs to be of type BOOL (int)
100
 
101
   // filter out the commonly used message values
102
   wParam_hiword = HIWORD (wParam);
103
   wParam_loword = LOWORD (wParam);
104
 
105
   // have we just fired up this window ?
106
   if (message == WM_INITDIALOG)
107
   {
108
      hThisWnd = hWnd; // save the dialog window handle
109
 
110
      // center the window
111
      CenterWindow (hWnd, hMainWnd);
112
 
113
      // set window title and control texts
114
      SetWindowText (hWnd, LOCALIZE (L"SendChallenge_Title"));
115
 
116
      swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), LOCALIZE (L"SendChallenge_Question"), sendchallenge.challengee);
117
      SetDlgItemText (hWnd, STATICTEXT_SENDCHALLENGE_QUESTION, temp_string);
118
 
119
      SetDlgItemText (hWnd, STATICTEXT_SENDCHALLENGE_COLOR, LOCALIZE (L"SendChallenge_ColorIWishToPlay"));
120
      ComboBox_AddString (GetDlgItem (hWnd, COMBOBOX_SENDCHALLENGE_COLOR), L"");
161 pmbaty 121
      ComboBox_AddString (GetDlgItem (hWnd, COMBOBOX_SENDCHALLENGE_COLOR), LOCALIZE (L"Games_Black"));
1 pmbaty 122
      ComboBox_AddString (GetDlgItem (hWnd, COMBOBOX_SENDCHALLENGE_COLOR), LOCALIZE (L"Games_White"));
123
      ComboBox_SetCurSel (GetDlgItem (hWnd, COMBOBOX_SENDCHALLENGE_COLOR), 1 + sendchallenge.color);
124
 
125
      SetDlgItemText (hWnd, STATICTEXT_SENDCHALLENGE_INITIALTIME, LOCALIZE (L"SendChallenge_InitialTime"));
126
      SetDlgItemInt (hWnd, EDITBOX_SENDCHALLENGE_INITIALTIME, sendchallenge.initial_time, false);
127
      SetDlgItemText (hWnd, STATICTEXT_SENDCHALLENGE_INCREMENT, LOCALIZE (L"SendChallenge_Increment"));
128
      SetDlgItemInt (hWnd, EDITBOX_SENDCHALLENGE_INCREMENT, sendchallenge.increment, false);
129
 
130
      SetDlgItemText (hWnd, CHECKBOX_SENDCHALLENGE_RATETHISGAME, LOCALIZE (L"SendChallenge_RatedUnrated"));
131
      Button_SetCheck (GetDlgItem (hWnd, CHECKBOX_SENDCHALLENGE_RATETHISGAME), (sendchallenge.is_rated ? BST_CHECKED : BST_UNCHECKED));
132
 
133
      SetDlgItemText (hWnd, BUTTON_INVITE, LOCALIZE (L"Button_Invite"));
134
      SetDlgItemText (hWnd, BUTTON_CANCEL, LOCALIZE (L"Button_Cancel"));
135
      SetDlgItemText (hWnd, STATICTEXT_SENDCHALLENGE_STATUSBAR, LOCALIZE (L"SendChallenge_StatusBar"));
136
 
137
      // convert the status bar message to hyperlink
138
      ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_SENDCHALLENGE_STATUSBAR));
139
   }
140
 
141
   // else did we click the close button on the title bar ?
142
   else if (message == WM_CLOSE)
143
      EndDialog (hWnd, 0); // close the dialog box
144
 
145
   // else did we take action on one of the controls ?
146
   else if (message == WM_COMMAND)
147
   {
148
      // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key)
149
      // OR was it the "cancel" button ?
150
      if ((wParam_loword == IDCANCEL) || (wParam_loword == BUTTON_CANCEL))
151
         EndDialog (hWnd, 0); // close the dialog box
152
 
153
      // else was it the "invite" button ?
154
      else if (wParam_loword == BUTTON_INVITE)
155
      {
156
         // retrieve the control values
157
         sendchallenge.color = ComboBox_GetCurSel (GetDlgItem (hWnd, COMBOBOX_SENDCHALLENGE_COLOR)) - 1;
158
         sendchallenge.initial_time = GetDlgItemInt (hWnd, EDITBOX_SENDCHALLENGE_INITIALTIME, &was_translated, false);
159
         if (!was_translated || (sendchallenge.initial_time == 0))
160
            sendchallenge.initial_time = 2; // default value
161
         sendchallenge.increment = GetDlgItemInt (hWnd, EDITBOX_SENDCHALLENGE_INCREMENT, &was_translated, false);
162
         if (!was_translated)
163
            sendchallenge.increment = 12; // default value
164
         sendchallenge.is_rated = (Button_GetCheck (GetDlgItem (hWnd, CHECKBOX_SENDCHALLENGE_RATETHISGAME)) != 0);
165
 
172 pmbaty 166
         sendchallenge.game_rules[0] = 0; // TODO: support other game rules
167
 
1 pmbaty 168
         EndDialog (hWnd, 1); // close the dialog box and return OK
169
      }
170
 
171
      // else was it the status bar hyperlink ?
172
      else if (wParam_loword == STATICTEXT_SENDCHALLENGE_STATUSBAR)
173
         ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized
174
   }
175
 
176
   // call the default dialog message processing function to keep things going
177
   return (false);
178
}