home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 126 / af126a.adf / Football.lzx / football / exec / League.rexx < prev    next >
OS/2 REXX Batch file  |  1999-05-22  |  14KB  |  388 lines

  1. /* ***********************************************************************
  2.  
  3.    LEAGUE PROGRAM FOR FOOTBALL REXX SUITE
  4.   ----------------------------------------
  5.                    Copyright  Mark Naughton 1996
  6.  
  7.  
  8. Version    Date     History
  9. --------------------------------------------------------------------------
  10.  1.0       090996   First release.
  11.            090996   As the sort doesnt flip the negative numbers, was
  12.                     thought to be a problem, the league is checked and
  13.                     flipped correctly (LEAGUE2.REXX)
  14.            120996   Amended same code as it didn't work if the two 'signs'
  15.                     were different. Now flips them correctly.
  16.                     (LEAGUE2.REXX)
  17.            180996   Changed record-count so that it is stored in element
  18.                     zero of each array. Deleted some variable assigns
  19.                     that weren't needed. Amended reading into arrays so
  20.                     that the count is set correctly for one element.
  21.                     Removed all "call "references before library routines.
  22.            180996   Removed all "call " references, used element zero to
  23.                     store the record-count. (LEAGUE2.REXX)
  24.            260996   Amended program so that the sortprogram and the REXX
  25.                     program to display the league are called from here
  26.                     instead of a separate script file. Changed so that the
  27.                     count is not stored in 'zero' element.
  28.            270996   Added code for points_per_draw and points_per_loss.
  29.                     Updated to use new filenames. Improved error messages.
  30.                     Inserted code from LEAGUE2.REXX to speed things up and
  31.                     to tidy the code. Finally got the table to be sorted
  32.                     properly, using a version of VSORTA from asd_library.
  33.  1.1       111196   Added argument so it can become a component of
  34.                     FOOTBALL. Removed GAMEPLAY and EXTRACT. Removed all
  35.                     messages.
  36.            131196   Added checks for files - if not found, exits without
  37.                     a message.
  38.            141196   Added code to highlight the leader.
  39.            201196   Added code to sort out which teams are relegated and
  40.                     then to display the line at the correct place.
  41.            211196   Updated and tidied the display.
  42.            231296   Shortened lines when displaying the league.
  43.            241296   Tidied up display again.
  44.  1.2       110497   Added code to support Points_Per_Goals. Amended the
  45.                     table display.
  46.            070597   Amended code that checks league_file to see if they
  47.                     are in the right order - was using wrong parameters.
  48.            080597   Amended as FOOTSORT is only used for league table file
  49.                     sorting.
  50.  2.0       240997   Tidied up code and added errors in case any files cannot
  51.                     be read or written to. Simplified reading of teams and
  52.                     data (getting it from '.df' now, not '.stats') and
  53.                     getting the total teams without having to re-read the
  54.                     '.stats' file. Added promotion bar.
  55.            151297   Tidied display.
  56.            030599   Fixed bug in relegation bar - it would show it at the
  57.                     maximum position for the number of teams relegated but
  58.                     this wasn't true when a team was relegated and the
  59.                     others still had to fight it out - same for the
  60.                     promotion bar.
  61.            050599   Okay, I decided to change the display again.
  62.  
  63. **************************************************************************
  64.  
  65. Procedure
  66. ---------
  67.  
  68. 1. Check files exist. Open 'Teams.df'.
  69. 2. Read in teams and their associated data; WIN, DRAW, LOST etc.
  70. 3. Close file. Open 'Teams.stats'. Read data. Close file.
  71. 4. Open 'Teams.sf' for reading.
  72. 5. Read each line that has a game played, and from that get the team names
  73.    and the score. Then update the relevant team data with the result.
  74. 6. Write data in form of a league table to an output file.
  75. 7. Close file.
  76. 8. Call external sortprogram to sort the output file.
  77. 9. Open 'Teams.stats' and read the number of teams, then close.
  78. 10.Open 'League.output' and read contents into two arrays.
  79. 11.Check to see if the teams are in the right order, comparing the points
  80.    of each one with the next and if the goal-differences are in the wrong
  81.    order, then swap the two teams.
  82. 12.Check to see which teams are relegated by calculating the maximum number
  83.    of points available and seeing if the lower teams are able to move up.
  84.    If not they are relegated, according to the number set.
  85. 13.Check to see which teams are promoted by using the same method as above.
  86. 14.Display league and exit.
  87.  
  88. ************************************************************************** */
  89. ARG league_file
  90.  
  91. version      = 2
  92. league_file  = "Data/" || league_file
  93. input_file   = '.stats'
  94. input2_file  = '.sf'
  95. input3_file  = '.df'
  96. output_file  = 'Data/League.output'
  97. title        = '*LEAGUE_NAME='
  98. points_win   = '*POINTS_PER_WIN='
  99. points_drw   = '*POINTS_PER_DRW='
  100. points_lse   = '*POINTS_PER_LSE='
  101. points_gls   = '*POINTS_PER_GLS='
  102. releg        = '*RELEGATION='
  103. playother    = '*PLAY_OTHER='
  104. promoted     = '*PROMOTED='
  105. team_counter = '*COUNTR='
  106. team         = '*TEAM='
  107. played       = '*PLY='
  108. won          = '*WIN='
  109. drawn        = '*DRW='
  110. lost         = '*LST='
  111. goalsf       = '*GOF='
  112. goalsa       = '*GOA='
  113. points       = '*PTS='
  114. separator    = '*'
  115. teams.       = '???'
  116. teams2.      = '???'
  117. m_ply.       = '???'
  118. m_win.       = '???'
  119. m_drw.       = '???'
  120. m_lost.      = '???'
  121. m_gof.       = '???'
  122. m_goa.       = '???'
  123. m_pts.       = '???'
  124. teams_ctr    = 0
  125. playo        = 2
  126. reg          = 2
  127. ptsgls       = 0
  128. ptswin       = 2
  129. ptsdrw       = 1
  130. ptslse       = 0
  131. promo        = 0
  132. not_played   = '__   __'
  133.  
  134.  
  135. if exists(league_file || input_file) = 0  then exit
  136. if exists(league_file || input2_file) = 0 then exit
  137. if exists(league_file || input3_file) = 0 then exit
  138.  
  139.  
  140. tcount = 0
  141. if open(datafile,league_file || input3_file,'r') then do
  142.    do while ~eof(datafile)
  143.       line = readln(datafile)
  144.       if pos(title,line) > 0 then        league_title = delstr(line,1,13)
  145.       if pos(points_win,line) > 0 then   ptswin=delstr(line,1,16)
  146.       if pos(points_drw,line) > 0 then   ptsdrw=delstr(line,1,16)
  147.       if pos(points_lse,line) > 0 then   ptslse=delstr(line,1,16)
  148.       if pos(points_gls,line) > 0 then   ptsgls=delstr(line,1,16)
  149.       if pos(releg,line) > 0 then        reg = delstr(line,1,12)
  150.       if pos(playother,line) > 0 then    playo = delstr(line,1,12)
  151.       if pos(promoted,line) > 0 then     promo = delstr(line,1,10)
  152.       if pos(separator,line) = 0 then do
  153.          line = strip(line)
  154.          tcount       = tcount + 1
  155.          teams.tcount = line
  156.       end
  157.    end
  158.    close(datafile)
  159. end
  160. else do
  161.    say
  162.    say "ERROR :    (League)"
  163.    say
  164.    say "Unable to open '"league_file || input3_file"' file."
  165.    exit
  166. end
  167.  
  168. if open(datafile,league_file || input_file,'r') then do
  169.    do while ~eof(datafile)
  170.       line = readln(datafile)
  171.       if pos(team,line) > 0 then
  172.          teams_ctr = teams_ctr + 1
  173.       if pos(played,line) > 0 then  m_ply.teams_ctr  = delstr(line,1,5)
  174.       if pos(won,line) > 0 then     m_won.teams_ctr  = delstr(line,1,5)
  175.       if pos(drawn,line) > 0 then   m_drw.teams_ctr  = delstr(line,1,5)
  176.       if pos(lost,line) > 0 then    m_lost.teams_ctr = delstr(line,1,5)
  177.       if pos(goalsf,line) > 0 then  m_gof.teams_ctr  = delstr(line,1,5)
  178.       if pos(goalsa,line) > 0 then  m_goa.teams_ctr  = delstr(line,1,5)
  179.       if pos(points,line) > 0 then  m_pts.teams_ctr  = delstr(line,1,5)
  180.    end
  181.    close(datafile)
  182. end
  183. else do
  184.    say
  185.    say "ERROR :    (League)"
  186.    say
  187.    say "Unable to open '"league_file || input_file"'."
  188.    exit
  189. end
  190.  
  191. if open(datafile,league_file || input2_file,'r') then do
  192.    do while ~eof(datafile)
  193.       line = readln(datafile)
  194.       if pos(separator,line) = 0 then do
  195.          if pos(not_played,line) = 0 then do
  196.             home_team = strip(substr(line,1,30))
  197.             goals_for = substr(line,32,2)
  198.             goals_aga = substr(line,37,2)
  199.             away_team = strip(substr(line,41,30))
  200.  
  201.             do i=1 to teams_ctr
  202.                if home_team = teams.i then do
  203.                   m_ply.i = m_ply.i + 1
  204.                   m_gof.i = m_gof.i + goals_for
  205.                   m_goa.i = m_goa.i + goals_aga
  206.                   if goals_for = goals_aga then do
  207.                      m_drw.i = m_drw.i + 1
  208.                      m_pts.i = m_pts.i + ptsdrw
  209.                   end
  210.                   if goals_for < goals_aga then do
  211.                      m_lost.i= m_lost.i + 1
  212.                      m_pts.i = m_pts.i + ptslse
  213.                   end
  214.                   if goals_for > goals_aga then do
  215.                      m_won.i = m_won.i + 1
  216.                      m_pts.i = m_pts.i + ptswin
  217.                   end
  218.                   m_pts.i = m_pts.i + (goals_for * ptsgls)
  219.                end
  220.             end
  221.             do i=1 to teams_ctr
  222.                if away_team = teams.i then do
  223.                   m_ply.i = m_ply.i + 1
  224.                   m_gof.i = m_gof.i + goals_aga
  225.                   m_goa.i = m_goa.i + goals_for
  226.                   if goals_for = goals_aga then do
  227.                      m_drw.i = m_drw.i + 1
  228.                      m_pts.i = m_pts.i + ptsdrw
  229.                   end
  230.                   if goals_for < goals_aga then do
  231.                      m_won.i = m_won.i + 1
  232.                      m_pts.i = m_pts.i + ptswin
  233.                   end
  234.                   if goals_for > goals_aga then do
  235.                      m_lost.i= m_lost.i + 1
  236.                      m_pts.i = m_pts.i + ptslse
  237.                   end
  238.                   m_pts.i = m_pts.i + (goals_aga * ptsgls)
  239.                end
  240.             end
  241.          end
  242.       end
  243.    end
  244.    close(datafile)
  245. end
  246. else do
  247.    say
  248.    say "ERROR :    (League)"
  249.    say
  250.    say "Unable to open '"league_file || input2_file"'."
  251.    exit
  252. end
  253.  
  254. if open(outfile,output_file,"w") then do
  255.    do i=1 to teams_ctr
  256.       line = teams.i
  257.       line = insert(" ",line,length(line)+1,30-length(line))
  258.       mp = right(m_ply.i,3)
  259.       mw = right(m_won.i,3)
  260.       md = right(m_drw.i,3)
  261.       ml = right(m_lost.i,3)
  262.       mgf = right(m_gof.i,5)
  263.       mga = right(m_goa.i,5)
  264.       mpts = right(m_pts.i,7)
  265.       writech(outfile,line"   "mp" "mw" "md" "ml" "mgf" "mga"  "mpts"      ")
  266.       itemp=mgf-mga
  267.       if itemp>0 then
  268.          itemp=insert("+",itemp,0,1)
  269.       itemp=right(itemp,4)
  270.       writeln(outfile,itemp)
  271.    end
  272.    close(outfile)
  273. end
  274. else do
  275.    say
  276.    say "ERROR :    (League)"
  277.    say
  278.    say "Unable to update the League. Cannot write to '"output_file"'."
  279.    exit
  280. end
  281.  
  282. address command 'Exec/footsort '
  283.  
  284. if open(datafile2,output_file,'r') then do
  285.    do i=1 to tcount
  286.       line = readln(datafile2)
  287.       teams.i  = line
  288.       teams2.i = line
  289.    end
  290.    close(datafile2)
  291. end
  292. else do
  293.    say
  294.    say "ERROR :    (League)"
  295.    say
  296.    say "Cannot read '"output_file"'."
  297.    exit
  298. end
  299.  
  300. ctr = 0
  301. do while swapctr > 0
  302.    swapctr=9
  303.    do i=1 to tcount-1                               /* was 64,4 and 77,4 */
  304.       swapdone=0
  305.       j = i + 1
  306.       if strip(substr(teams.i,64,7)) = strip(substr(teams.j,64,7)) then do
  307.          goaldiffa = strip(substr(teams.i,77,5))
  308.          goaldiffb = strip(substr(teams.j,77,5))
  309.          if goaldiffa < goaldiffb then do
  310.             teams.i = teams2.j
  311.             teams.j = teams2.i
  312.             teams2.i= teams.i
  313.             teams2.j= teams.j
  314.             swapdone= 1
  315.             ctr = ctr + 1
  316.          end
  317.       end
  318.       if swapdone = 1 then
  319.          break
  320.    end
  321.    if ctr = 0 then
  322.       swapctr = 0
  323.    else
  324.       ctr = 0
  325. end
  326.  
  327. if reg > 0 then do             /* this bit of code checks to see which teams are relegated */
  328.    posreg   = 0
  329.    wherereg = tcount - reg
  330.    regpts   = strip(substr(teams.wherereg,64,7))         /* was 64,4 */
  331.    do i=tcount to 1 by -1
  332.       b = (((tcount-1) * playo) - strip(substr(teams.i,36,2))) * ptswin
  333.       c = strip(substr(teams.i,64,7)) + b
  334.       if c < regpts then posreg = i                 /* calculates the max points avail */
  335.    end
  336.    if posreg < (wherereg+1) then posreg = wherereg + 1     /* was 0 - set to reqd pos if higher */
  337.                                                            /* Bug fix 0305 - relegation bar problems */
  338. end
  339.  
  340. /* ---------------------------------------------- */
  341.  
  342.  
  343. if promo > 0 then do          /* this bit of code checks to see which teams are promoted */
  344.    pospro   = 0
  345.    wherepro = promo + 1
  346.    propts   = strip(substr(teams.wherepro,64,7))         /* was 64,4 */
  347.    do i=1 to tcount
  348.       b = (((tcount-1) * playo) - strip(substr(teams.i,36,2))) * ptswin
  349.       c = strip(substr(teams.i,64,7)) + b
  350.       if c > propts then pospro = i                 /* calculates the max points avail */
  351.    end
  352.    if pospro > (wherepro-1) then pospro = wherepro - 1     /* was 0 - set to reqd pos if higher */
  353. end
  354.  
  355.  
  356. /* ---------------------------------------------- */
  357.  
  358. say
  359. say center(league_title,88)
  360. say "-----------------------------------------------------------------------------------------"
  361. say
  362. say
  363. say "    Team                               Pl Won Drw Lst GoalsF GoalsA    Pts     GoalDiff"
  364. say "-----------------------------------------------------------------------------------------"
  365.  
  366. do i=1 to tcount
  367.    if strip(substr(teams.i,76,5)) = 0 then teams.i=overlay("",teams.i,76,5," ")
  368.    if i >= posreg then teams.i = upper(teams.i)
  369.    if i<10 then do
  370.       if i = 1 then teams.i = upper(teams.i)
  371.       say " "i") "teams.i
  372.    end
  373.    else
  374.       say i") "teams.i
  375.    if i = (tcount-reg) & reg > 0 then do       /* was posreg */
  376.       say "_________________________________________________________________________________________"
  377.       say
  378.    end
  379.    if i = (promo+1) & promo > 0 then do        /* was pospro */
  380.       say "_________________________________________________________________________________________"
  381.       say
  382.    end
  383. end
  384.  
  385. say "-----------------------------------------------------------------------------------------"
  386. say
  387.  
  388. exit