File: nMeta.c

package info (click to toggle)
conquest 8.1-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 7,984 kB
  • ctags: 3,086
  • sloc: ansic: 39,393; sh: 8,540; yacc: 446; makefile: 296; lex: 146
file content (379 lines) | stat: -rw-r--r-- 9,437 bytes parent folder | download
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* 
 * meta select server node
 *
 * can NODE_EXIT or transfer to ConSvr node
 *
 * $Id: nMeta.c,v 1.7 2004/11/24 01:31:12 jon Exp $
 *
 * Copyright 1999-2004 Jon Trulson under the ARTISTIC LICENSE. (See LICENSE).
 */

#include "c_defs.h"
#include "context.h"
#include "global.h"
#include "datatypes.h"
#include "color.h"
#include "conf.h"
#include "gldisplay.h"
#include "node.h"
#include "client.h"
#include "clientlb.h"
#include "nMeta.h"
#include "nConsvr.h"
#include "gldisplay.h"
#include "glmisc.h"
#include "cqkeys.h"

static char *header = "Server List";
static char *header2fmt = "(Page %d of %d)";
static char headerbuf[BUFFER_SIZE];
static char header2buf[BUFFER_SIZE];
static char *eprompt = "Arrow keys to select, [TAB] or [ENTER] to accept, any other key to quit.";

static const int servers_per_page = 8;
static int flin, llin, clin, pages, curpage;

struct _srvvec {
  Unsgn16 vers;
  char hostname[MAXHOSTNAME + 10];
};

static struct _srvvec servervec[META_MAXSERVERS] = {};

static int nMetaDisplay(dspConfig_t *);
static int nMetaInput(int ch);

static metaSRec_t *metaServerList;
static int nums;

static scrNode_t nMetaNode = {
  nMetaDisplay,                 /* display */
  NULL,                         /* idle */
  nMetaInput,                   /* input */
  NULL                          /* next */
};


static void dispServerInfo(dspConfig_t *dsp, metaSRec_t *metaServerList, 
                           int num)
{
  static char buf1[BUFFER_SIZE];
  static char buf2[BUFFER_SIZE];
  static char buf3[BUFFER_SIZE];
  static char buf4[BUFFER_SIZE];
  static char buf5[BUFFER_SIZE];
  static char buf6[BUFFER_SIZE];
  static char buf7[BUFFER_SIZE];
  static char pbuf1[BUFFER_SIZE];
  static char pbuf2[BUFFER_SIZE];
  static char pbuf3[BUFFER_SIZE];
  static char pbuf4[BUFFER_SIZE];
  static char pbuf5[BUFFER_SIZE];
  static char pbuf6[BUFFER_SIZE];
  static char pbuf7[BUFFER_SIZE];
  GLfloat x, y, w, h;
  static int inited = FALSE;
  static const int hcol = 2, icol = 11;
  int tlin = 3;

  tlin = 3;
  x = dsp->ppCol;
  y = (dsp->ppRow * tlin);
  w = (dsp->wW - (dsp->ppCol * 3.0));
  h = (dsp->ppRow * 8.2);

  if (!inited)
    {
      inited = TRUE;
      sprintf(pbuf1, "#%d#Server: ", MagentaColor);
      sprintf(buf1, "#%d#%%s", NoColor);

      sprintf(pbuf2, "#%d#Version: ", MagentaColor);
      sprintf(buf2, "#%d#%%s", NoColor);

      sprintf(pbuf3, "#%d#Status: ", MagentaColor);
      sprintf(buf3, 
              "#%d#Ships #%d#%%d/%%d #%d#"
              "(#%d#%%d #%d#active, #%d#%%d #%d#vacant, "
              "#%d#%%d #%d#robot)",
              NoColor, CyanColor, NoColor,
              CyanColor, NoColor, CyanColor, NoColor, 
              CyanColor, NoColor);

      sprintf(pbuf4, "#%d#Flags: ", MagentaColor);
      sprintf(buf4, "#%d#%%s", NoColor);

      sprintf(pbuf5, "#%d#MOTD: ", MagentaColor);
      sprintf(buf5, "#%d#%%s", NoColor);

      sprintf(pbuf6, "#%d#Contact: ", MagentaColor);
      sprintf(buf6, "#%d#%%s", NoColor);

      sprintf(pbuf7, "#%d#Time: ", MagentaColor);
      sprintf(buf7, "#%d#%%s", NoColor);
    }

  cprintf(tlin, hcol, ALIGN_NONE, pbuf1);
  cprintf(tlin++, icol, ALIGN_NONE, buf1, metaServerList[num].servername);

  cprintf(tlin, hcol, ALIGN_NONE, pbuf2);
  cprintf(tlin++, icol, ALIGN_NONE, buf2, metaServerList[num].serverver);

  cprintf(tlin, hcol, ALIGN_NONE, pbuf3);
  cprintf(tlin++, icol, ALIGN_NONE, buf3,
          (metaServerList[num].numactive + metaServerList[num].numvacant +
           metaServerList[num].numrobot),
	  metaServerList[num].numtotal, 
          metaServerList[num].numactive, 
	  metaServerList[num].numvacant, metaServerList[num].numrobot);

  cprintf(tlin, hcol, ALIGN_NONE, pbuf4);
  cprintf(tlin++, icol, ALIGN_NONE, buf4, 
          clntServerFlagsStr(metaServerList[num].flags));

  cprintf(tlin, hcol, ALIGN_NONE, pbuf5);
  cprintf(tlin++, icol, ALIGN_NONE, buf5, metaServerList[num].motd);

  cprintf(tlin, hcol, ALIGN_NONE, pbuf6);
  cprintf(tlin++, icol, ALIGN_NONE, buf6, metaServerList[num].contact);

  cprintf(tlin, hcol, ALIGN_NONE, pbuf7);
  cprintf(tlin++, icol, ALIGN_NONE, buf7, metaServerList[num].walltime);

  drawLineBox(x, y, w, h, CyanColor, 2.0);

  return;
}


void nMetaInit(metaSRec_t *ServerList, int numserv)
{
  int i;
  metaServerList = ServerList;
  nums = numserv;

  /* this is the number of required pages,        
     though page accesses start at 0 */
  if (nums >= servers_per_page)
    {
      pages = nums / servers_per_page;
      if ((nums % servers_per_page) != 0)
        pages++;                /* for runoff */
    }
  else
    pages = 1;

				/* init the servervec array */
  for (i=0; i < nums; i++)
  {
    if (metaServerList[i].version >= 2) /* valid for newer meta protocols */
      servervec[i].vers = metaServerList[i].protovers;
    else
      servervec[i].vers = PROTOCOL_VERSION; /* always 'compatible' */

    snprintf(servervec[i].hostname, (MAXHOSTNAME + 10) - 1, "%s:%hu",
            metaServerList[i].altaddr,
            metaServerList[i].port);
  }

  curpage = 0;

  flin = 11;			/* first server line */
  llin = 0;			/* last server line on this page */
  clin = 0;			/* current server line */

  setNode(&nMetaNode);

  return;
}

static int nMetaDisplay(dspConfig_t *dsp)
{
  int i, k;
  char *dispmac;
  int lin;
  int col;

  sprintf(header2buf, header2fmt, curpage + 1, pages);
  sprintf(headerbuf, "%s %s", header, header2buf);

  lin = 1;
  
  cprintf(lin, 0, ALIGN_CENTER, "#%d#%s", NoColor, headerbuf);
  
  lin = flin;
  col = 1;
  
  i = 0;			/* start at index 0 */

				/* figure out the last editable line on
				   this page */

  if (curpage == (pages - 1)) /* last page - might be less than full */
    llin = (nums % servers_per_page);	/* ..or more than empty? ;-) */
  else
    llin = servers_per_page;
  
  i = 0;
  while (i < llin)
    {			/* display this page */
      /* get the server number for this line */
      k = (curpage * servers_per_page) + i; 
      
      dispmac = servervec[k].hostname;

      /* highlight the currently selected line */
      if (i == clin)
        {
          if (servervec[k].vers == PROTOCOL_VERSION)
            cprintf(lin, col, ALIGN_NONE, "#%d#%s#%d#",
                    RedLevelColor, dispmac, NoColor);
          else
            cprintf(lin, col, ALIGN_NONE, 
                    "#%d#%s#%d# (unavailable - incompatible protocol)",
                    BlueColor |CQC_A_BOLD, dispmac, NoColor);

        }
      else
        {
          if (servervec[k].vers == PROTOCOL_VERSION)
            cprintf(lin, col, ALIGN_NONE, "#%d#%s#%d#",
                    InfoColor, dispmac, NoColor);
          else
            cprintf(lin, col, ALIGN_NONE, 
                    "#%d#%s#%d# (unavailable - incompatible protocol)",
                    BlueColor, dispmac, NoColor);
        }

      
      lin++;
      i++;
    }
  
  cprintf(MSG_LIN1, 1, ALIGN_NONE, eprompt);
  
  if (clin >= llin)
    clin = llin - 1;
  
  dispServerInfo(dsp, metaServerList, clin);

  return NODE_OK;
}


  
static int nMetaInput(int ch)
{
  int i;

  ch = CQ_CHAR(ch) | CQ_FKEY(ch);

  switch(ch)
    {
    case CQ_KEY_UP:		/* up */
    case CQ_KEY_LEFT:
    case 'w':
    case 'k':
      clin--;
      if (clin < 0)
        {
          if (pages != 1)
            {
              curpage--;
              if (curpage < 0)
                {
                  curpage = pages - 1;
                }
            }
          
          /* setup llin  for current page */
          if (curpage == (pages - 1)) 
            llin = (nums % servers_per_page);
          else
            llin = servers_per_page;
          
          clin = llin - 1; 
        }
      break;
      
    case CQ_KEY_DOWN:		/* down */
    case CQ_KEY_RIGHT:
    case 'x':
    case 'j':
      clin++;
      if (clin >= llin)
        {
          if (pages != 1)
            {
              curpage++;
              if (curpage >= pages)
                {
                  curpage = 0;
                }
            }
          
          clin = 0; 
        }
      break;
      
    case CQ_KEY_PAGE_UP:		/* prev page */
      if (pages != 1)
        {
          curpage--;
          if (curpage < 0)
            {
              curpage = pages - 1;
            }
        }
      
      break;
      
    case CQ_KEY_PAGE_DOWN:		/* next page */
      if (pages != 1)
        {
          curpage++;
          if (curpage >= pages)
            {
              curpage = 0;
            }
        }
      
      break;
      
    case TERM_NORMAL:	/* selected one */
    case TERM_EXTRA:

      if (cInfo.remotehost)
        {
          free(cInfo.remotehost);
          cInfo.remotehost = NULL;
        }
      
      i = (curpage * servers_per_page) + clin;

      if ((metaServerList[i].protovers == PROTOCOL_VERSION) ||
          metaServerList[i].version < 2) /* too old to know for sure */
        {
          if ((cInfo.remotehost = strdup(metaServerList[i].altaddr)) == NULL)
            {
              clog("strdup(metaServerList[i]) failed");
              return NODE_EXIT;
            }
          cInfo.remoteport = metaServerList[i].port;
          
          /* transfer to the Consvr node */
          nConsvrInit(cInfo.remotehost, cInfo.remoteport);
        }
      else
        mglBeep();

      break;
      
    default:		/* everything else */
      return NODE_EXIT;
      break;
    }
  
  return NODE_OK;
}