home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / x / xhearts.zip / SELECT.C < prev    next >
C/C++ Source or Header  |  1992-01-07  |  3KB  |  167 lines

  1. #include "misc.h"
  2. #include "defs.h"
  3. #include "client.h"
  4. #include <malloc.h>
  5.  
  6. table_ptr    first_table,
  7.         cur_table;
  8.  
  9. int        table_count = 0,    /* # tables existing */
  10.         cur_screen_table,    /* nth table shown as table 1 */
  11.         screen_table_id[8];    /* table_id of tables on screen */
  12.  
  13. char        joined, joining;
  14.  
  15.  
  16. /*
  17.  * Find table given unique table_id in buf[1].
  18.  */
  19. table_ptr
  20. find_table(buf)
  21. char    *buf;
  22. {
  23.     table_ptr    cur_table;
  24.     int        table_num;
  25.  
  26.     (void) sscanf(buf + 1, "%d", &table_num);
  27.     for (cur_table = first_table;
  28.             cur_table && cur_table->table_id != table_num;
  29.             cur_table = cur_table->next_table)
  30.         ;
  31.     return(cur_table);
  32. }
  33.  
  34. /*
  35.  * Create new table given table_id in buf[1].  Return pointer to new table.
  36.  */
  37. table_ptr
  38. new_table(buf)
  39. char    *buf;
  40. {
  41.     table_ptr    cur_ptr, new_table_ptr;
  42.     int        i, table_num;
  43.  
  44.     (void) sscanf(buf + 1, "%d", &table_num);
  45.     new_table_ptr = (table_ptr) malloc(sizeof(struct table));
  46.     new_table_ptr->next_table = NULL;
  47.     new_table_ptr->table_id = table_num;
  48.     new_table_ptr->closed = FALSE;
  49.     new_table_ptr->data = NULL;
  50.     for (i = 0; i < 4; i++)
  51.         (void) strcpy(new_table_ptr->player_name[i], "<empty>");
  52.     if (first_table) {
  53.         for (cur_ptr = first_table; cur_ptr->next_table;
  54.                 cur_ptr = cur_ptr->next_table)
  55.             ;
  56.         cur_ptr->next_table = new_table_ptr;
  57.     }
  58.     else
  59.         first_table = new_table_ptr;
  60.     ++table_count;
  61.     return(new_table_ptr);
  62. }
  63.  
  64. /*
  65.  * Update current table entry based on buf.  Return table # modified.
  66.  */
  67. update_table(buf)
  68. char    *buf;
  69. {
  70.     int        i;
  71.     table_ptr    tmp_table;
  72.  
  73.     switch (buf[0]) {
  74.     case 't' :
  75.         if ((cur_table = find_table(buf)) == NULL)
  76.             cur_table = new_table(buf);
  77.         break;
  78.     case 'h' :
  79.         (void) sscanf(buf + 1, "%d", &cur_table->hand);
  80.         break;
  81.     case 'r' :
  82.         (void) sscanf(buf + 1, "%d", &cur_table->round);
  83.         break;
  84.     case 'p' :
  85.         (void) strcpy(cur_table->player_name[buf[1] - '0'], buf + 2);
  86.         break;
  87.     case 'x' :
  88.         if (tmp_table = find_table(buf)) {
  89.             for (i = 0; i < 8; i++)
  90.                 if (screen_table_id[i] = tmp_table->table_id)
  91.                     screen_table_id[i] = 0;
  92.             tmp_table->table_id = 0;
  93.             tmp_table->closed = TRUE;
  94.         }
  95.     }
  96. }
  97.  
  98. dist_died(i)
  99. int i;
  100. {
  101.   if (i == 1)
  102.     death("Distributor died!! 1");
  103.   else if (i == 2)
  104.     death("Distributor died!! 2");
  105.   else if (i == 3)
  106.     death("Distributor died!! 3");
  107.   else if (i == 4)
  108.     death("Distributor died!! 4");
  109. }
  110.  
  111. select_game()
  112. {
  113.     int    dealer_port;
  114.     table_ptr    temp_table;
  115.     char buf[64], another;
  116.  
  117.     option_init();
  118.     /*
  119.      * Get current games info
  120.      */
  121.     do {
  122.         if (read_socket(dist_socket, buf) == 0)
  123.             dist_died(1);
  124.         if (buf[0] != 'g')
  125.             update_table(buf);
  126.     }
  127.     while (buf[0] != 'g');
  128.  
  129.     show_tables(cur_screen_table = 1);
  130.  
  131.     /*
  132.      * Wait for user input or table update info
  133.      */
  134.     joined = joining = FALSE;
  135.  
  136.     do {
  137.       option_scan(&another);
  138.     }
  139.     while (!joined);
  140.  
  141.     /*
  142.      * Now free up malloced table space
  143.      */
  144.     option_clear();
  145.     while (first_table) {
  146.         temp_table = first_table;
  147.         first_table = first_table->next_table;
  148.         free ((char *) temp_table);
  149.     }
  150.     table_count = 0;
  151.     option_init();
  152.  
  153.     if (!first_game && another)
  154.         /*
  155.          * Play another game with current dealer.
  156.          */
  157.         dealer_port = 0;
  158.     else {
  159.         if (read_socket(dist_socket, buf) == 0)
  160.             dist_died(4);
  161.         (void) sscanf(buf + 1, "%d", &dealer_port);
  162.     }
  163.     (void) close(dist_socket);
  164.     close_socket(dist_socket);
  165.     return(dealer_port);
  166. }
  167.