home *** CD-ROM | disk | FTP | other *** search
/ Current Shareware 1994 January / SHAR194.ISO / catalogs / mtchplay.zip / MP_DATA.C < prev    next >
Text File  |  1993-02-12  |  13KB  |  512 lines

  1. /* MP_DATA.C  data input and storage routines */
  2.  
  3. #include <conio.h>
  4. #include <string.h>
  5. #include <dos.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include <stdio.h>
  9. #include "defines.h"
  10.  
  11. extern PLAYER player[MAXFL][MAXSZ];
  12.  
  13. extern int NP[];           /* number of players in each flight */
  14. extern char mark[];        /* single shaded block */
  15. extern char namefield[];   /* 19 marks */
  16. extern char hcpfield[];
  17. extern char scorefield[];
  18. extern int no_space;       /* flag for get_string() */
  19.  
  20. extern int norm_attr, high_attr;
  21. extern int recalc[];
  22. extern int left, top, right, bottom;
  23. extern int numflights;
  24.  
  25. /* function prototypes */
  26. void
  27.   chk(int),
  28.   correct(int, int),
  29.   delete(int, int),
  30.   init_kb_screen(void),
  31.   kb_entry(void),
  32.   init_flights_screen(void),
  33.   init_score_screen(void),
  34.   score_entry(void),
  35.   scroll(int,int,int,int,int),
  36.   show_msgs(int);
  37. int
  38.   correct_hcp(int *),
  39.   correct_name(char *),
  40.   correct_scores(char *),
  41.   error_msg(char *, int, int),
  42.   getline(FILE *,char *),
  43.   read_char(int,int);
  44.  
  45. extern void
  46.   calc_all(void),
  47.   normal_exit(int),
  48.   rect(int, int, int, int, int, int),
  49.   show_data(int,int,int),
  50.   std_screen(void),
  51.   write_data(FILE *);
  52. extern int
  53.   find(char *, int *),
  54.   get_string(char *, char *, char *, int, int),
  55.   menu_select(int,int,int,int,int,char *,int,int,int,int,int,int,
  56.                 int,int,int,int *,char **);
  57.  
  58. /* ====================================================================== */
  59. void
  60. init_score_screen()       /* instructions for score_entry() */
  61. {
  62.   int col2 = 23, col3 = 26, col4 = 29;
  63.   char *instruction[] = {
  64.         "    SCORE ENTRY",
  65.         "",
  66.         "Enter desired scores,",
  67.         "then return to the",
  68.         "main menu by entering",
  69.         "a blank name or by",
  70.         "pressing Esc.",
  71.         "",
  72.         "Scores may be entered",
  73.         "only for players on the",
  74.         "current roster, since",
  75.         "it is not possible to",
  76.         "add names now."};
  77.   int i;
  78.  
  79.   std_screen();
  80.   textattr(high_attr);
  81.   window(28,1,80,1);
  82.   clrscr();
  83.   gotoxy(8,1); cputs("Name");
  84.   gotoxy(col2-1,1); cputs("Hcp");
  85.   gotoxy(col3,1); cputs("Flt");
  86.   gotoxy(col4+7,1); cputs("Scores");
  87.   window(2,2,26,24);
  88.   textattr(high_attr);
  89.   clrscr();
  90.   for (i=0; i<13; i++) {
  91.     gotoxy(2,i+2);
  92.     cputs(instruction[i]);
  93.   }
  94.   window(28,2,80,25);
  95.   textattr(norm_attr);
  96.   clrscr();
  97. }
  98.  
  99. /* ---------------------------------------------------------------------- */
  100. void
  101. score_entry(void)
  102. {
  103.   int col1 = 2, col2 = 23, col3 = 26, col4 = 29;
  104.   int row = 1;
  105.   int c,i,f,k,r;
  106.   char pname[21], scores[25], h[3], flt[3];
  107.   char *blank_name = "                     ";
  108.   int hcp=0, flight=0;
  109.   FILE *fp;
  110.   int changed[MAXFL];  /* flags for calculation needed */
  111.   int changeflag = 0;  /* set if any changes made */
  112.   extern char datafile[];
  113.   int flght[10], index[10];
  114.  
  115.   for (i=0; i<MAXFL; i++) changed[i] = 0;
  116.   init_score_screen();           /* set up entry screen */
  117.  
  118.   while(1) {
  119.     r = get_string("",pname,namefield,col1, row);
  120.     if (!r) break;
  121.     r = findn(pname,flght,index);
  122.     if (r == -1) {
  123.       error_msg("Not found in entry list",27+col2-2,1+row);
  124.       continue;
  125.     }
  126.     else if (r > 0) {
  127.       r = pick_one(1+row, r, flght, index);
  128.       if (r == -1)
  129.         continue;
  130.     }
  131.     flight = flght[r];
  132.     strcpy(pname, player[flight][index[r]].name);
  133.     hcp = player[flight][index[r]].hcp;
  134.     gotoxy(col1,row);
  135.     cputs(blank_name);
  136.     gotoxy(col1,row);
  137.     cputs(pname);
  138.     gotoxy(col2,row);
  139.     cprintf("%d",hcp);
  140.     gotoxy(col3,row);
  141.     cprintf("%d",flight);
  142.     if (player[flight][index[r]].scores[0] != 0) {
  143.       error_msg("Scores already entered",27+col2-2,1+row);
  144.       gotoxy(col1,row);
  145.       cputs("                                        ");
  146.       continue;
  147.     }
  148. get_scores:
  149.     no_space = 1;       /* ignore user-entered spaces */
  150.     c = get_string("",scores,scorefield,col4,row);
  151.     no_space = 0;       /* reset flag */
  152.     if (c == 0)         /* scores already entered */
  153.       continue;
  154.     if (c>0 && c<24) {
  155.       error_msg("18 scores required",27+col4-18,1+row);
  156.       goto get_scores;
  157.     }
  158.     enter_scores(flight,index[r],scores);
  159.     changed[flight] = 1;
  160.     changeflag = 1;
  161.     row++;
  162.     if (row == 24) {
  163.       scroll(28,2,79,24,norm_attr);
  164.       row = 23;
  165.     }
  166.   }
  167.   if (changeflag) {          /* update and save data if any entered */
  168.     clrscr();
  169.     for (k=0; k<MAXFL; k++)
  170.       if (changed[k]) {
  171.         set_gnp(k);
  172.         calc_results(k);
  173.       }
  174.     fp = fopen(datafile,"w");  /* automatic save after every kb_session */
  175.     write_data(fp);
  176.     fclose(fp);
  177.     changeflag = 0;
  178.   }
  179. }
  180.  
  181. /*---------------------------------------------------------------------------*/
  182. enter_scores(int flight, int i, char *scores)
  183. {
  184.   int g,h;
  185.   extern ndx[];
  186.  
  187.   strcpy(player[flight][i].scores,scores);
  188.   if (player[flight][i].hcp < 0)    /* if temporary hcp, use Callaway */
  189.     player[flight][i].hcp = callaway(scores);
  190. }
  191.  
  192. /*---------------------------------------------------------------------------*/
  193. error_msg(char *msg, int col, int row)  /* col,row absolute coordinates */
  194. {
  195.   char buf[160], msg1[80];
  196.   int length,c;
  197.   extern int mono;
  198.   struct text_info text;
  199.  
  200.   gettextinfo(&text);
  201.   window(1,1,80,25);
  202.   length = strlen(msg) + 2;
  203.   if (col+length > 80)           /* not enough room for message */
  204.     return 0;
  205.   msg1[0] = ' ';                 /* add spaces at start and end of msg */
  206.   strcpy (&msg1[1],msg);
  207.   strcat(msg1," ");
  208.   gettext(col, row, col+length, row, buf);   /* save current screen area */
  209.   if (!mono)
  210.     textattr(16*4+7);
  211.   else
  212.     textattr(norm_attr);
  213.   gotoxy(col,row);
  214.   cputs(msg1);
  215.   gotoxy(wherex()-1,row);         /* in case cursor is on */
  216.   c = getch();
  217.   puttext(col,row,col+length,row,buf);
  218.   textattr(norm_attr);
  219.   window(text.winleft,text.wintop,text.winright,text.winbottom);
  220.   return c;
  221. }
  222.  
  223. /*---------------------------------------------------------------------------*/
  224. void
  225. chk(int flight)
  226. {
  227.   int i,j,f,k,n;
  228.   int start_index, stop_index;
  229.  
  230.   asort(flight);
  231.   show_msgs(flight);
  232.   start_index = 0;
  233.   stop_index = MIN(24,NP[flight]-1);
  234.   for (i=start_index; i<=stop_index; i++)
  235.     show_data(flight, start_index+i, i+1);
  236.  
  237.   while (1) {
  238.     while (kbhit())
  239.       getch();                  /* clear kb buffer */
  240.     switch(getkey()) {
  241.       case ESC:           /* Esc or CR returns to main menu */
  242.       case CR:
  243.         return;
  244.  
  245.       case PgUp:
  246.         if (flight == 0)
  247.           flight = numflights-1;
  248.         else
  249.           flight--;
  250.         chk(flight);       /* shift to next smaller nonempty flight */
  251.         return;
  252.  
  253.       case PgDn:
  254.         if (flight == numflights-1)
  255.           flight = 0;
  256.         else
  257.           flight++;
  258.         chk(flight);        /* shift to next larger nonempty flight */
  259.         return;
  260.     }
  261.   }
  262. }
  263.  
  264. /* ---------------------------------------------------------------------- */
  265. void
  266. show_msgs(int flight)
  267. {
  268.   int i, num_msgs=6;
  269.   char *msg[] = {
  270.     "      FLIGHT  ",
  271.     "",
  272.     "  PgUp = Prev Flt.",
  273.     "  PgDn = Next Flt.",
  274.     "",
  275.     "Esc returns to menu"
  276.   };
  277.  
  278.   std_screen();
  279.   window(2,2,26,24);
  280.   textattr(high_attr);
  281.   clrscr();
  282.   for (i=0; i<num_msgs; i++) {
  283.     gotoxy(2,i+2);
  284.     cputs(msg[i]);
  285.   }
  286.   gotoxy(2+14,2);
  287.   cprintf("%d",flight);
  288.   window(left,top,right,bottom);
  289.   textattr(norm_attr);
  290.   clrscr();
  291. }
  292.  
  293. /* ---------------------------------------------------------------------- */
  294. void
  295. correct(int flight, int j)
  296. {
  297.   int col =  41, row = 3;
  298.   int scol = 27+19, ecol = 27+34, srow = 7, erow = 13;
  299.   int norm_attr1=WHITE+16*BROWN;
  300.   int high_attr1=WHITE+16*BLACK;
  301.   int tag_attr1=YELLOW+16*BROWN;
  302.   int tag_high1=YELLOW+16*BLACK;
  303.   char *item[] = { "  Name   ",
  304.                    "  Hcp    ",
  305.                    "  Scores ",
  306.                    "  Accept ",
  307.                    "  Delete "};
  308.   int numitems = 5, tag_indx = 2;
  309.   static int current = 0;
  310.   int selection;
  311.   int ltr[] = {'n','h','s','a','d'};
  312.   int i,hcp;
  313.   char pname[21],scores[25];
  314.  
  315.   if (mono) {
  316.     norm_attr1 = tag_attr1 = norm_attr;
  317.     high_attr1 = tag_high1 = high_attr;
  318.   }
  319.  
  320.   textattr(norm_attr);
  321.   window(left,top,right,bottom);
  322.   clrscr();
  323.   window(1,1,80,25);
  324.   gotoxy(col,row);
  325.   cprintf("  Name: %s",player[flight][j].name);
  326.   gotoxy(col,row+1);
  327.   cprintf("   Hcp: %2d",player[flight][j].hcp);
  328.   gotoxy(col,row+2);
  329.   if (player[flight][j].scores[0] == '\0')
  330.     cputs("Scores: None entered");
  331.   else
  332.     cprintf("Scores: %s",player[flight][j].scores);
  333.  
  334.   while (1) {
  335.     selection = menu_select(scol,srow,ecol,erow,1,"Correct:",norm_attr1,
  336.       high_attr1,tag_attr1,tag_high1,tag_indx,numitems-1,current,2,2,ltr,item);
  337.     if (selection == 27 || selection == 3)
  338.       break;
  339.     if (selection == 0) {
  340.       i = correct_name(pname);
  341.       if (i) {
  342.         recalc[flight] = 1;
  343.         strcpy(player[flight][j].name,pname);
  344.         gotoxy(col+8,row);
  345.         cputs(pname);
  346.       }
  347.       current = 1;
  348.     }
  349.     else if (selection == 1) {
  350.       i = correct_hcp(&hcp);
  351.       if (i) {
  352.         recalc[flight] = 1;
  353.         player[flight][j].points += (hcp - player[flight][j].hcp);
  354.         player[flight][j].net = player[flight][j].gross - hcp;
  355.         player[flight][j].hcp = hcp;
  356.         gotoxy(col+8,row+1);
  357.         clreol();
  358.         cprintf("%2d",hcp);
  359.       }
  360.       current = 2;
  361.     }
  362.     else if (selection == 2) {
  363.       i = correct_scores(scores);
  364.       if (i) {
  365.         recalc[flight] = 1;
  366.         enter_scores(flight,j,scores);       /* change scores, gross, net */
  367.         gotoxy(col+8,row+2);
  368.         cprintf("%s",scores);
  369.       }
  370.       current = 3;
  371.     }
  372.     else if (selection == 4) {
  373.       i = error_msg("Delete this entry (Y/N)?",41,16);
  374.       if (toupper(i) != 'Y')
  375.         break;
  376.       delete(flight,j);
  377.       recalc[flight] = 1;
  378.       break;
  379.     }
  380.   }
  381. }
  382.  
  383. correct_name(char *pname)
  384. {
  385.   int r, col1 = 41, row = 16;
  386.   int flight;
  387.   int ret_val = 0;
  388.  
  389.   textattr(norm_attr);
  390.   window(1,1,80,25);
  391.   r = get_string("Name",pname,namefield,col1, row);
  392.   if (!r)
  393.     goto ret;
  394.   if (find(pname,&flight) != -1) {
  395.     error_msg("Already entered",col1,row+1);
  396.     goto ret;
  397.   }
  398.   ret_val = 1;
  399. ret:
  400.     gotoxy(30,row);
  401.     clreol();
  402.     return ret_val;
  403. }
  404.  
  405. correct_hcp(int *hcp)
  406. {
  407.     int r;
  408.     int col1 = 41, row = 16;
  409.     char h[4];
  410.  
  411.   window(1,1,80,25);
  412.   textattr(norm_attr);
  413.     r = get_string("Handicap",h,hcpfield,col1,row);
  414.     if (r) {
  415.       *hcp = atoi(h);
  416.       if (*hcp > 40) *hcp = 40;
  417.     }
  418.     gotoxy(30,row);
  419.     clreol();
  420.     return r;
  421. }
  422.  
  423. correct_scores(char *scores)
  424. {
  425.     int r;
  426.     int col1 = 41, row = 16;
  427.     int i,ret_val = 1;
  428.  
  429.   window(1,1,80,25);
  430.   textattr(norm_attr);
  431.   no_space = 1;       /* ignore user-entered spaces */
  432.   r = get_string("Scores",scores,scorefield,col1,row);
  433.   no_space = 0;       /* reset flag */
  434.   if (r == 0) {
  435.     ret_val = 0;
  436.     goto ret;
  437.   }
  438.   if (r < 0) r = -r;
  439.   if (r>0 && r<24) {
  440.     error_msg("18 scores required",col1,row+1);
  441.     ret_val = 0;
  442.     goto ret;
  443.   }
  444.   for (i=0; i<24; i++) {
  445.     if (i==3 || i==7 || i==11 || i==12 || i==16 || i==20)
  446.       continue;
  447.     if (!isdigit(scores[i])) {
  448.       error_msg("Invalid score",col1,row+1);
  449.       ret_val = 0;
  450.       goto ret;
  451.     }
  452.   }
  453. ret:
  454.     gotoxy(30,row);
  455.     clreol();
  456.     return ret_val;
  457. }
  458.  
  459. void
  460. delete(int flight, int index)
  461. {
  462.   int i;
  463.  
  464.   if (index >= NP[flight])
  465.     return;
  466.   NP[flight]--;
  467.   if (NP[flight] == 0) {
  468.     numflights = 0;
  469.     return;
  470.   }
  471.   for (i=index; i<NP[flight]; i++)
  472.     player[flight][i] = player[flight][i+1];
  473. }
  474.  
  475. /* ---------------------------------------------------------------------- */
  476. int
  477. getline(FILE *fp, char *s)
  478. {
  479.   char ch, *p=s;
  480.  
  481.   while((ch = fgetc(fp)) == ' ')
  482.     ;
  483.   if (ch == '\n' || ch == EOF) {
  484.     s[0] = '\0';
  485.     return 0;        /* blank line */
  486.   }
  487.   *p++ = ch;
  488.   while (1) {
  489.     ch = fgetc(fp);
  490.     if (ch == '\n' || ch == EOF)
  491.       break;
  492.     *p++ = ch;
  493.   }
  494.   *p = '\0';
  495.   return 1;           /* non_blank line */
  496. }
  497.  
  498. /*---------------------------------------------------------------------------*/
  499. void
  500. scroll(int left, int top, int right, int bottom, int attr)
  501. {
  502.   _AH = 6;
  503.   _AL = 1;              /* scroll one line */
  504.   _BH = attr;
  505.   _CH = top-1;
  506.   _CL = left-1;
  507.   _DH = bottom-1;
  508.   _DL = right-1;
  509.   geninterrupt(0x10);
  510. }
  511.  
  512.