-
Notifications
You must be signed in to change notification settings - Fork 12
/
findslot.c
247 lines (220 loc) · 6.77 KB
/
findslot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* findslot.c
*
* Kevin Smith 03/23/88
*/
#include "config.h"
#include "copyright2.h"
#include <stdio.h>
#include <sys/types.h>
#include "Wlib.h"
#include "defs.h"
#include "struct.h"
#include "data.h"
#include "packets.h"
#include "cowmain.h"
#include "defaults.h"
#include "findslot.h"
#include "newwin.h"
#include "socket.h"
/*
window hierarchy diagram
w wait queue window
\_ quit button
\_ count button (not reactive)
\_ motd button
w_motd message of the day window
*/
/* wait queue window constants, in pixels */
#define WQ_W 405 /* window width */
#define WQ_H 100 /* window height */
#define WQ_HEAD 20 /* heading height */
#define BM 10 /* button margin */
/* calculate an X coordinate for centering given text */
static int xc(char *s)
{
return WQ_W / 6 - BM - strlen(s) * W_Textwidth / 2;
}
/* draw or redraw the heading in the wait queue window */
static void draw_heading(W_Window w)
{
char buf[60];
sprintf(buf, "You are on the queue at %s, please wait.", serverName);
W_WriteText(w, 10, 10, textColor, buf, strlen(buf), W_RegularFont);
}
/* draw or redraw the quit button */
static void draw_b_quit(W_Window w)
{
char *s = "Quit";
W_WriteText(w, xc(s), 25, textColor, s, strlen(s), W_RegularFont);
}
/* draw or redraw the count button */
static void draw_b_count(W_Window ww, W_Window cw, int count)
{
char *s1 = "queue";
char *s2 = "position";
char buf[80];
W_WriteText(cw, xc(s1), 5, textColor, s1, strlen(s1), W_RegularFont);
W_WriteText(cw, xc(s2), 15, textColor, s2, strlen(s2), W_RegularFont);
sprintf(buf, " %d ", count);
if (count == -1) strcpy(buf, " ? ");
W_WriteText(cw, xc(buf), 35, textColor, buf, strlen(buf), W_RegularFont);
/* update the window title bar for the window manager to display */
sprintf(buf, "Q%d @ %s", count, serverName);
W_SetWindowName(ww, buf);
}
/* draw or redraw the motd button */
static void draw_b_motd(W_Window w)
{
char *s1 = "show server";
char *s2 = "welcome message";
W_WriteText(w, xc(s1), 20, textColor, s1, strlen(s1), W_RegularFont);
W_WriteText(w, xc(s2), 30, textColor, s2, strlen(s2), W_RegularFont);
}
/* find a server slot, returns the slot number once it is known, but
if the server reports we are placed in a queue then display the wait
queue window and maintain the count while optionally showing the
server message of the day */
int findslot(void)
{
int oldcount = -1;
W_Window w, b_quit, b_count, b_motd;
W_Window w_motd;
extern int motd_last;
int WaitMotdLine = 0;
int mapMotd = booleanDefault("showMotd", 0);
W_Event event;
/* Wait for some kind of indication about in/not in */
while (queuePos == -1) {
socketPauseNoUser();
if (serverDead) {
#if defined(SOUND)
Exit_Sound();
#endif
fprintf(stderr, "server connection lost, during pre-queue\n");
terminate(0);
}
readFromServer(NULL);
if (me != NULL) {
/* We are in! */
return me->p_no;
}
}
/* We have to wait. Make appropriate windows, etc... */
w = W_MakeWindow("wait", 0, 0, WQ_W, WQ_H, NULL, 2,
foreColor);
b_quit = W_MakeWindow("waitquit", BM, WQ_HEAD + BM,
WQ_W / 3 - BM * 2, WQ_H - WQ_HEAD - 2 - BM * 2,
w, 2, W_Red);
b_count = W_MakeWindow("count", WQ_W / 3 + BM, WQ_HEAD + BM,
WQ_W / 3 - BM * 2, WQ_H - WQ_HEAD - 2 - BM * 2,
w, 2, W_Yellow);
b_motd = W_MakeWindow("motdbutton", 2 * WQ_W / 3 + BM, WQ_HEAD + BM,
WQ_W / 3 - BM * 2, WQ_H - WQ_HEAD - 2 - BM * 2,
w, 2, W_Green);
W_MapWindow(w);
W_MapWindow(b_count);
W_MapWindow(b_motd);
W_MapWindow(b_quit);
if (mapMotd) {
w_motd = W_MakeWindow("waitmotd", 1, WQ_W + 1, TWINSIDE,
TWINSIDE, 0, 2, foreColor);
W_MapWindow(w_motd);
showMotd(w_motd, WaitMotdLine);
}
/* a local event loop, exits when the player is given a slot, or if
the server disconnects, or if the user choses to quit */
for (;;) {
socketPause();
readFromServer(NULL);
if (serverDead) {
#if defined(SOUND)
Exit_Sound();
#endif
fprintf(stderr, "server connection lost, during queue wait\n");
terminate(0);
}
while (W_EventsPending()) {
W_NextEvent(&event);
switch ((int) event.type) {
case W_EV_BUTTON:
case W_EV_KEY:
if (mapMotd && event.Window == w_motd) {
if (event.key == ' ' || event.key == 'q') {
W_DestroyWindow(w_motd);
mapMotd = !mapMotd;
} else {
if (event.key == 'b') {
WaitMotdLine -= 28;
WaitMotdLine = MAX(WaitMotdLine, 0);
} else {
WaitMotdLine += 28;
if (WaitMotdLine > motd_last)
WaitMotdLine = 0;
}
W_ClearWindow(w_motd);
showMotd(w_motd, WaitMotdLine);
break;
}
} else if (event.Window == b_motd) {
if (mapMotd) {
W_DestroyWindow(w_motd);
} else {
w_motd = W_MakeWindow("waitmotd", 1, WQ_W + 1,
TWINSIDE, TWINSIDE, 0, 2,
foreColor);
W_MapWindow(w_motd);
WaitMotdLine = 0;
showMotd(w_motd, WaitMotdLine);
}
mapMotd = !mapMotd;
} else if (event.Window == b_quit || event.key == 'q') {
#if defined(SOUND)
Exit_Sound();
#endif
fprintf(stderr, "you selected quit\n");
terminate(0);
}
break;
case W_EV_EXPOSE:
if (event.Window == w) {
draw_heading(w);
} else if (event.Window == w_motd) {
showMotd(w_motd, WaitMotdLine);
} else if (event.Window == b_quit) {
draw_b_quit(b_quit);
} else if (event.Window == b_count) {
draw_b_count(w, b_count, queuePos);
} else if (event.Window == b_motd) {
draw_b_motd(b_motd);
}
break;
case W_EV_CLOSED:
if (event.Window == w) {
fprintf(stderr, "you quit, by closing the wait window\n");
terminate(0);
} else if (event.Window == w_motd) {
W_DestroyWindow(w_motd);
mapMotd = 0;
}
break;
default:
break;
}
}
/* keep the count button up to date based on the server responses */
if (queuePos != oldcount) {
draw_b_count(w, b_count, queuePos);
oldcount = queuePos;
}
/* if we have a slot now, destroy the windows and begin playing */
if (me != NULL) {
W_DestroyWindow(w);
if (mapMotd) {
W_DestroyWindow(w_motd);
}
W_Beep();
W_Beep();
return me->p_no;
}
}
}