home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 126 / af126a.adf / Football.lzx / football / exec / CupScheduleNextRound.rexx next >
OS/2 REXX Batch file  |  1999-05-22  |  42KB  |  1,260 lines

  1. /* ***********************************************************************
  2.  
  3.    CUP NEXT ROUND SCHEDULER PROGRAM FOR FOOTBALL REXX SUITE
  4.   ----------------------------------------------------------
  5.                    Copyright  Mark Naughton 1997
  6.  
  7.  
  8. Version    Date     History
  9. --------------------------------------------------------------------------
  10.  1.0       091297   Started. Has been planned for 3 weeks.
  11.            131297   After a two day break, have completed the code. Now
  12.                     for the testing... Searching out those problems...
  13.            141297   Code works alright up to the Final where it failed due
  14.                     to 'datafile2' being used instead of 'datafile'.
  15.                     Amended Final code to handle replays. One Cup has been
  16.                     run through and now works. Three more to go.
  17.            151297   Improved round names when matches are displayed.
  18.                     Added code to change current round, after the final
  19.                     is played to FINISHED so you will still be able to
  20.                     view results and run scripts on the Cup.
  21.            161297   Amended bettername so it handled replays in early
  22.                     rounds. Undummied code to swap teams around when a
  23.                     replay is needed - Home:=Away and Away:=Home.
  24.                     Third place doesn't get setup - oh dear.
  25.            171297   Noticed that the teams for third place aren't stored
  26.                     - amended. Third also didn't store losing teams in
  27.                     working file. Winning teams are now stored with "#"
  28.                     and the losing ones are deleted when Third has been
  29.                     played.
  30.            181297   Fixed bug where teams for Final Replay where picked
  31.                     up from the teams in Third Place Play-Off. Added
  32.                     cosmetic changes to reports. Its working so all
  33.                     debug information will be taken out.
  34.            070698   Added code for manually scheduling.
  35.            290898   With the addition of two-legged matches and away goals
  36.                     this program was going to be the beast to amend.
  37.                     Started planning the amendments.
  38.            030998   Nearly five days later, the amendments have been made.
  39.                     Now for the testing...
  40.            050998   What a nightmare. Testing is complete and many little
  41.                     bugs were found but not documented. Take it from me,
  42.                     it works..(kind of).
  43.            100998   Made several improvements during the course of finding
  44.                     the bug (found in CupUpdateMatches.rexx) which missed
  45.                     data lines during recreation of '.scf' file.
  46.            120998   Amended winner check to handle away goals when there
  47.                     is a different score after extra time to that at full
  48.                     time.
  49.  
  50. **************************************************************************
  51.  
  52. Procedure           (Where Third is mentioned, it only gets the data
  53. ---------                                     if the option has been set)
  54.  
  55. 1. Check files exist.
  56. 2. Read '.cf' file and get settings.
  57. 3. Read '.cfrw' file. Store and get current round settings.
  58. 4. Read matches from '.scf' file.
  59. 5. Check if any of these matches haven't been played - if they haven't then
  60.    give error and exit.
  61. 6. If current round is Third, set round to Final, adjust teams, write back
  62.    to '.cfrw' file, give message and exit.
  63. 7. If at the end of the number of rounds, read Final and Third matches.
  64. 8. If Third is set, get the winner/loser of the Third Place Play-Off.
  65. 9. Search for the Final, find winner. If replay, store after checking its
  66.    not already been stored.
  67. 10.If not a replay, write IGNORE in replay array.
  68. 11.If replays exist, adjust roundname.
  69. 12.Go through the replays, setting them to not played.
  70. 13.Write '.cfrw' file back to itself.
  71. 14.Append replays back to '.scf' file and give message and exit.
  72. 15.If no replays exist (linked to No.11), if history (if set) file doesn't
  73.    exist, then create it.
  74. 16.Append winner/loser and if set, third and fourth place.
  75. 17.Change round to 'Finished'. Write '.cfrw' file. Give end message and
  76.    exit.
  77. 18.If not at the end of the number of rounds (linked to No.7), search
  78.    matches, finding winners/losers and replays (if applicable).
  79. 19.If replays exist, search '.cfrw' file, putting '#' in front of teams
  80.    not in replays.
  81. 20.Adjust roundname.
  82. 21.Write not played and swap teams, checking replays aren't ignored and
  83.    if they are, decrement the count.
  84. 22.If no replays, get teams minus the '#'. Write '.cfrw' file.
  85. 23.If replays append to '.scf' file, give message and exit.
  86. 24.If no replays (linked to No.19) exist, read '.cfrw' file again, getting
  87.    the settings.
  88. 25.Read matches for current round.
  89. 26.Search matches, storing winners/losers. Update '.cfrw' settings then
  90.    rewrite '.cfrw' file.
  91. 27.If Third, dummy out finalists.
  92. 28.If Semi-Finals, write Third (if set) and Final matches, give message
  93.    and exit.
  94. 29.Random match generator - get number and store.
  95. 30.Append matches to '.scf' file, writing home and away teams according to
  96.    the randomly generated array.
  97. 31.Give message for next round and exit.
  98. 32.Routine 'whosthewinr' - depending on the settings for each round
  99.    (Extra Time and Penalties), decide who the winner and loser are, and
  100.    return the winner on the left side of '*****'. Return '#####' if its
  101.    the 1st leg of Two.
  102. 33.Routine 'bettername' - using the current round name, create a more
  103.    useful name, ie. "Quart" becomes "Quarter Finals" and "1st Replays"
  104.    becomes "1st Round Replays", etc.
  105.  
  106. ************************************************************************** */
  107. PARSE ARG league_stuff
  108.  
  109. version      = 1
  110. input_file   = '.cf'
  111. input2_file  = '.scf'
  112. input3_file  = '.cfrw'
  113. input4_file  = '.cfh'
  114. output3_file = '.temp'
  115. indicator    = 'RAM:MatchesToBeDone'
  116. title        = '*CUP_TITLE='
  117. thirdpl      = '*CUP_THIRD='
  118. rounddef     = '*CUP_RNDDEF='
  119. currond      = '*CUP_ROND='
  120. currrondn    = '*CUP_CRDN='
  121. roundddef    = '*CUP_RDEF='
  122. cuphistory   = '*CUP_HISTORY='
  123. thirdpl      = '*CUP_THIRD='
  124. teams_cnt    = '*CUP_TCNT='
  125. awaygs       = '*CUP_AWAYG='
  126. schedtype    = '*CUP_SCHEDULE='
  127. pkversion    = '*  Version='
  128. matches.     = '???'
  129. separator    = '*'
  130. teams.       = '???'
  131. rnds.        = '???'
  132. lines.       = '???'
  133. replays.     = '???'
  134. working.     = '???'
  135. losing.      = '???'
  136. repct        = 0
  137. repno.       = '???'
  138. tcount       = 0
  139. ttc          = 0
  140. rndcnt       = 0
  141. linesct      = 0
  142. league_file  = "Data/"league_stuff
  143. curnd        = ''
  144. curndn       = 0
  145. not_played   = "__   __"
  146.  
  147. if exists(league_file||input_file) = 0 then exit
  148. if exists(league_file||input2_file) = 0 then exit
  149. if exists(league_file||input3_file) = 0 then exit
  150.  
  151. if open(datafile,league_file || input_file,'r') then do
  152.    do while ~eof(datafile)
  153.       line = readln(datafile)
  154.       if pos(title,line) > 0 then        cupname = delstr(line,1,11)
  155.       if pos(cuphistory,line) > 0 then   cuph    = delstr(line,1,13)
  156.       if pos(thirdpl,line) > 0 then      third   = delstr(line,1,11)
  157.       if pos(awaygs,line) > 0 then       awaygls = delstr(line,1,11)
  158.       if pos(schedtype,line) > 0 then    scht    = delstr(line,1,14)
  159.       if pos(rounddef,line) > 0 then do
  160.          rndcnt = rndcnt + 1
  161.          rnds.rndcnt = delstr(line,1,12)
  162.       end
  163.    end
  164.    close(datafile)
  165. end
  166. else do
  167.    say
  168.    say "ERROR :    (CupScheduleNextRound)"
  169.    say
  170.    say "Unable to open '"league_file || input_file"' file."
  171.    exit
  172. end
  173.  
  174. if open(datafile,league_file || input3_file,'r') then do
  175.    do while ~eof(datafile)
  176.       line = readln(datafile)
  177.       linesct = linesct + 1
  178.       lines.linesct = line
  179.       if pos(currond,line)   > 0 then crond  = strip(substr(line,11,2))
  180.       if pos(roundddef,line) > 0 then tcrondn= strip(substr(line,11,5))
  181.       if pos(currrondn,line) > 0 then do
  182.          crondn = strip(substr(line,11,30))
  183.          tleg_no = 0
  184.          if pos("1 Leg",line) > 0 then tleg_no = 1
  185.          if pos("2 Legs",line) > 0 then tleg_no = 2
  186.       end
  187.    end
  188.    close(datafile)
  189. end
  190. else do
  191.    say
  192.    say "ERROR :    (CupScheduleNextRound)"
  193.    say
  194.    say "Unable to open '"league_file || input3_file"' file."
  195.    exit
  196. end
  197.  
  198. leg_number = 0
  199. if pos("Leg",crondn) > 0 then do
  200.    if pos("2 Legs",crondn) > 0 then
  201.       leg_number = 2
  202.    else
  203.       leg_number = 1
  204. end
  205.                     /* this can be here or later....decide! */
  206. mkr = 0
  207. ttc = 0
  208. if open(datafile,league_file || input2_file,'r') then do
  209.    do while ~eof(datafile)
  210.       line = readln(datafile)
  211.       if pos("*Round="strip(tcrondn),line) > 0 & mkr = 0 then do
  212.          if pos("Leg",line) > 0 then do
  213.             if tleg_no = 1 & pos("1 Leg",line) > 0 then mkr = 1
  214.             if tleg_no = 2 & pos("2 Legs",line) > 0 then mkr = 1
  215.          end
  216.          else
  217.             mkr = 1
  218.       end
  219.       if mkr = 1 then do
  220.          ttc = ttc + 1
  221.          matches.ttc = line
  222.       end
  223.    end
  224.    close(datafile)
  225. end
  226. else do
  227.    say
  228.    say "ERROR :    (CupScheduleNextRound)"
  229.    say
  230.    say "Unable to open '"league_file || input2_file"' file."
  231.    exit
  232. end
  233.  
  234. do i=1 to ttc
  235.    if pos(not_played,matches.i) > 0 & pos("#",matches.i) = 0 & pos("Third",crondn) = 0 then do
  236.       say
  237.       say center("'"cupname"'",78)
  238.       say "-------------------------------------------------------------------------------"
  239.       say
  240.       say "INFORMATION :"
  241.       say
  242.       say "Not all matches have been played. This program will schedule the next round's"
  243.       say "matches when all of the matches have been played."
  244.       say
  245.       say "-------------------------------------------------------------------------------"
  246.       exit
  247.    end
  248. end
  249.  
  250. if pos("Third",crondn) > 0 then do
  251.    do i=1 to linesct
  252.       if pos(currond,lines.i)   > 0 then lines.i=currond||rndcnt
  253.       if pos(currrondn,lines.i) > 0 then do
  254.          if pos("Leg",rnds.rndcnt) > 0 then do
  255.             if pos("2 Legs",crondn) > 0 then
  256.                lines.i=currrondn||"Final            2 Legs"
  257.             else
  258.                lines.i=currrondn||"Final            1 Leg"
  259.          end
  260.          else
  261.            lines.i=currrondn||"Final"
  262.       end
  263.       if pos(roundddef,lines.i) > 0 then lines.i=roundddef||rnds.rndcnt
  264.       if pos(separator,lines.i) = 0 then do
  265.          if pos("#",lines.i) = 0 then
  266.             lines.i = "-DELETED-"
  267.          else do
  268.             parse var lines.i "#"teamname
  269.             lines.i = teamname
  270.          end
  271.       end
  272.    end
  273.    if open(datafile,league_file || input3_file,'w') then do
  274.       do p=1 to i
  275.          if p = i then
  276.             writech(datafile,lines.p)
  277.          else
  278.             writeln(datafile,lines.p)
  279.       end
  280.       close(datafile)
  281.    end
  282.    else do
  283.       say
  284.       say "ERROR :    (CupScheduleNextRound)"
  285.       say
  286.       say "Unable to open '"league_file || input3_file"' for recreation."
  287.       exit
  288.    end
  289.    leg_do = "Blank"
  290.    if pos("2 Legs",rnds.rndcnt) > 0 then
  291.       leg_do = "1 Leg"
  292.    say
  293.    say center("'"cupname"'",78)
  294.    say "-------------------------------------------------------------------------------"
  295.    say
  296.    say "Round : "bettername("Final",leg_do)
  297.    say
  298.    do i=1 to ttc
  299.       if pos("*Round=Final",matches.i) > 0 then do
  300.          g = i + 2
  301.          say matches.g
  302.          leave
  303.       end
  304.    end
  305.    say
  306.    say "-------------------------------------------------------------------------------"
  307.    exit
  308. end
  309.  
  310. if crond = rndcnt then do
  311.    if pos("YES",third) > 0 then
  312.       temprond = "Third"
  313.    else
  314.       temprond = "Final"
  315.    mkr = 0
  316.    ttc = 0
  317.    matchcnt = 0
  318.    if open(datafile,league_file || input2_file,'r') then do
  319.       do while ~eof(datafile)
  320.          line = readln(datafile)
  321.          if pos("*Round="strip(temprond),line) then
  322.             mkr = 1
  323.          if mkr = 1 then do
  324.             ttc = ttc + 1
  325.             matches.ttc = line
  326.          end
  327.          if pos(separator,line) = 0 & pos("#",line) = 0 & line~='' then
  328.             matchcnt = matchcnt + 1
  329.       end
  330.       close(datafile)
  331.    end
  332.    else do
  333.       say
  334.       say "ERROR :    (CupScheduleNextRound)"
  335.       say
  336.       say "Unable to open '"league_file || input2_file"' file."
  337.       exit
  338.    end
  339.  
  340.    if pos("YES",third) > 0 then do
  341.       where = crond - 1
  342.       ermm  = substr(rnds.where,1,5)
  343.       posit = 0
  344.       do i=1 to ttc
  345.          if pos("Third",matches.i) > 0 then do
  346.             posit = i + 2
  347.             leave
  348.          end
  349.       end
  350.       thirdstrng = whosthewinr(posit,rnds.where,ermm,0,"NO")
  351.    end
  352.    mkr = 0
  353.    if pos("Leg",crondn) > 0 then do
  354.       if pos("2 Legs",crondn) > 0 then
  355.          leg_number = 2
  356.       else
  357.          leg_number = 1
  358.    end
  359.    else
  360.       leg_number = 0
  361.    do i=1 to ttc
  362.       if pos("Final",matches.i) > 0 then
  363.          mkr = 1
  364.       if pos('*',matches.i) = 0 & pos('#',matches.i) = 0 & matches.i~='' & mkr = 1 then do
  365.          match_1f = i
  366.          strng = whosthewinr(i,rnds.crond,crondn,leg_number,awaygls)
  367.          if pos("REPLAY",strng) > 0 then do
  368.             repno.repct = i
  369.             teama = strip(substr(matches.i,1,30))
  370.             teamb = strip(substr(matches.i,41,30))
  371.             do k=1 to repct
  372.                if pos(teama,replays.k) > 0 & pos(teamb,replays.k) > 0 & repno.k~=i then do
  373.                   replays.k = "IGNORE"
  374.                end
  375.             end
  376.          end
  377.          if pos("NOP",strng) = 0 & pos("REPLAY",strng) = 0 then do
  378.             teama = strip(substr(matches.i,1,30))
  379.             teamb = strip(substr(matches.i,41,30))
  380.             do k=1 to repct
  381.                if pos(teama,replays.k) > 0 & pos(teamb,replays.k) > 0 then do
  382.                   replays.k = "IGNORE"
  383.                end
  384.             end
  385.          end
  386.       end
  387.    end
  388.    if repct > 0 then do
  389.       do j=1 to linesct
  390.          if pos(currrondn,lines.j) > 0 & pos("Replay",lines.j) = 0 then
  391.             lines.j=lines.j||" Replay"
  392.       end
  393.       trepct = repct
  394.       do i=1 to repct
  395.          if pos("IGNORE",replays.i) = 0 then
  396.             replays.i = overlay(not_played,replays.i,32)
  397.          else
  398.             trepct = trepct - 1
  399.       end
  400.       if trepct = 0 then repct = 0
  401.       if open(datafile,league_file || input3_file,'w') then do
  402.          do p=1 to linesct
  403.             if p = linesct then
  404.                writech(datafile,lines.p)
  405.             else
  406.                writeln(datafile,lines.p)
  407.          end
  408.          close(datafile)
  409.       end
  410.       else do
  411.          say
  412.          say "ERROR :    (CupScheduleNextRound)"
  413.          say
  414.          say "Unable to open '"league_file || input3_file"' for recreation."
  415.          exit
  416.       end
  417.       if repct > 0 then do
  418.          if open(datafile,league_file || input2_file,'a') then do
  419.             writeln(datafile,"*Round="tcrondn" Replay")
  420.             writeln(datafile,"*")
  421.             do p=1 to repct
  422.                if pos("IGNORE",replays.p) = 0 then
  423.                   writeln(datafile,replays.p)
  424.             end
  425.             writeln(datafile,"*")
  426.             close(datafile)
  427.          end
  428.          else do
  429.             say
  430.             say "ERROR :    (CupScheduleNextRound)"
  431.             say
  432.             say "Unable to open '"league_file || input2_file"' for amendment."
  433.             exit
  434.          end
  435.          say
  436.          say center("'"cupname"'",78)
  437.          say "-------------------------------------------------------------------------------"
  438.          say
  439.          say "Round : "bettername(word(crondn,1)" Replay ","Blank")
  440.          say
  441.          do i=1 to repct
  442.             if pos("IGNORE",replays.i) = 0 then
  443.                say replays.i
  444.          end
  445.          say
  446.          say "-------------------------------------------------------------------------------"
  447.          exit
  448.       end
  449.    end
  450.    if repct = 0 then do
  451.       winning = strng
  452.       if pos("#####",winning) > 0 then do
  453.          parse var winning l1teamh "#####" l1teama
  454.          do j=1 to linesct
  455.             if pos(currrondn,lines.j) > 0 then
  456.                lines.j=currrondn||word(tcrondn,1)"   2 Legs"
  457.          end
  458.          if open(datafile,league_file || input3_file,'w') then do
  459.             do p=1 to linesct
  460.                if p = linesct then
  461.                   writech(datafile,lines.p)
  462.                else
  463.                   writeln(datafile,lines.p)
  464.             end
  465.             close(datafile)
  466.          end
  467.          else do
  468.             say
  469.             say "ERROR :    (CupScheduleNextRound)"
  470.             say
  471.             say "Unable to open '"league_file || input3_file"' for recreation."
  472.             exit
  473.          end
  474.          if open(datafile,league_file || input2_file,'a') then do
  475.             writeln(datafile,"*Round="word(tcrondn,1)"   2 Legs")
  476.             writeln(datafile,"*")
  477.             writeln(datafile,left(l1teama,30," ")" __   __ " l1teamh)
  478.             writeln(datafile,left("#1st Leg",30," ")" "right(strip(substr(matches.match_1f,37,2)),2)"   "right(strip(substr(matches.match_1f,32,2)),2))
  479.             writeln(datafile,"*")
  480.             close(datafile)
  481.          end
  482.          else do
  483.             say
  484.             say "ERROR :    (CupScheduleNextRound)"
  485.             say
  486.             say "Unable to open '"league_file || input2_file"' for amendment."
  487.             exit
  488.          end
  489.          say
  490.          say center("'"cupname"'",78)
  491.          say "-------------------------------------------------------------------------------"
  492.          say
  493.          say "Round : "bettername(word(crondn,1),"2 Legs")
  494.          say
  495.          s1 = strip(substr(matches.match_1f,37,2))
  496.          s2 = strip(substr(matches.match_1f,32,2))
  497.          say left(l1teama,30," ")" ("s1")  __   __  ("s2")   "l1teamh
  498.          say
  499.          say "-------------------------------------------------------------------------------"
  500.          exit
  501.       end
  502.       parse var winning winner"*****"loser
  503.       parse var thirdstrng thirdplace"*****"fourthplace
  504.  
  505.       if pos("YES",cuph) > 0 then do
  506.          if exists(league_file||input4_file) = 0 then do
  507.             if open(datafile,league_file||input4_file,'w') then do
  508.                writeln(datafile,"*")
  509.                writeln(datafile,"** History for '"cupname"'")
  510.                writeln(datafile,"*")
  511.                close(datafile)
  512.             end
  513.             else do
  514.                say
  515.                say "ERROR :    (CupScheduleNextRound)"
  516.                say
  517.                say "Cannot write to '"league_file||input4_file"'."
  518.                exit
  519.             end
  520.          end
  521.  
  522.          if open(datafile,league_file||input4_file,'a') then do
  523.             writeln(datafile,"*WINNER="winner)
  524.             writeln(datafile,"*RUNNERUP="loser)
  525.             if pos("YES",third) > 0 then do
  526.                writeln(datafile,"*THIRD="thirdplace)
  527.                writeln(datafile,"*FOURTH="fourthplace)
  528.             end
  529.             writeln(datafile,"*")
  530.             close(datafile)
  531.          end
  532.          else do
  533.             say
  534.             say "ERROR :    (CupScheduleNextRound)"
  535.             say
  536.             say "Cannot append to '"league_file||input4_file"'."
  537.             exit
  538.          end
  539.       end
  540.                             /* write FINISHED to Working file so it can be read by Football */
  541.                             /* but no scores will be able to be updated.                    */
  542.       do i=1 to linesct
  543.          if pos(currrondn,lines.i) > 0 then lines.i=currrondn||"Finished"
  544.       end
  545.       if open(datafile,league_file || input3_file,'w') then do
  546.          do p=1 to i
  547.             if p = i then
  548.                writech(datafile,lines.p)
  549.             else
  550.                writeln(datafile,lines.p)
  551.          end
  552.          close(datafile)
  553.       end
  554.       else do
  555.          say
  556.          say "ERROR :    (CupScheduleNextRound)"
  557.          say
  558.          say "Unable to open '"league_file || input3_file"' for recreation."
  559.          exit
  560.       end
  561.  
  562.       say
  563.       say center("'"cupname"'",78)
  564.       say "-------------------------------------------------------------------------------"
  565.       say
  566.       say "All matches have been played, totalling "matchcnt", (including replays or legs "
  567.       say "if applicable)."
  568.       say
  569.       say
  570.       say "                        WINNER   :    "upper(winner)
  571.       say
  572.       say "                        RunnerUp :    "loser
  573.       if pos("YES",third) > 0 then do
  574.          say "                        Third    :    "thirdplace
  575.          say "                        Fourth   :    "fourthplace
  576.       end
  577.       say
  578.       say "-------------------------------------------------------------------------------"
  579.       exit
  580.    end
  581. end
  582. if crond ~= rndcnt then do
  583.    do i=1 to ttc
  584.       if pos('*',matches.i) = 0 & pos('#',matches.i) = 0 & matches.i~='' then do
  585.          strng = whosthewinr(i,rnds.crond,crondn,leg_number,awaygls)
  586.          if pos("REPLAY",strng) > 0 then do
  587.             repno.repct = i
  588.             teama = strip(substr(matches.i,1,30))
  589.             teamb = strip(substr(matches.i,41,30))
  590.             do k=1 to repct
  591.                if pos(teama,replays.k) > 0 & pos(teamb,replays.k) > 0 & repno.k~=i then do
  592.                   replays.k = "IGNORE"
  593.                end
  594.             end
  595.          end
  596.          if pos("NOP",strng) = 0 & pos("REPLAY",strng) = 0 then do
  597.             teama = strip(substr(matches.i,1,30))
  598.             teamb = strip(substr(matches.i,41,30))
  599.             do k=1 to repct
  600.                if pos(teama,replays.k) > 0 & pos(teamb,replays.k) > 0 then do
  601.                   replays.k = "IGNORE"
  602.                end
  603.             end
  604.          end
  605.       end
  606.    end
  607.  
  608.    if repct > 0 then do
  609.       do j=1 to linesct
  610.          if pos(separator,lines.j) = 0 & pos("#",lines.j) = 0 then do
  611.             willhe = 0
  612.             do p=1 to repct
  613.                if pos(strip(lines.j),replays.p) > 0 then
  614.                   willhe = 1
  615.             end
  616.             if willhe = 0 then
  617.                lines.j = insert("#",lines.j,0)
  618.          end
  619.       end
  620.       do j=1 to linesct
  621.          if pos(currrondn,lines.j) > 0 & pos("Replay",lines.j) = 0 then
  622.             lines.j=lines.j||" Replays"
  623.       end
  624.       trepct = repct
  625.       do i=1 to repct
  626.          if pos("IGNORE",replays.i) = 0 then do
  627.             replays.i = overlay(not_played,replays.i,32)
  628.             teama = strip(substr(replays.i,1,30))    /* this code swaps the teams round for replays */
  629.             teamb = strip(substr(replays.i,41,30))
  630.             replays.i = overlay(teama,replays.i,41,30)
  631.             replays.i = overlay(teamb,replays.i,1,30)
  632.          end
  633.          else
  634.             trepct = trepct - 1
  635.       end
  636.       if trepct = 0 then repct = 0
  637.       if repct = 0 then do
  638.          do p=1 to linesct
  639.             if pos("#",lines.p) > 0 then
  640.                lines.p = delstr(lines.p,1,1)
  641.          end
  642.       end
  643.       if open(datafile,league_file || input3_file,'w') then do
  644.          do p=1 to linesct
  645.             if p = linesct then
  646.                writech(datafile,lines.p)
  647.             else
  648.                writeln(datafile,lines.p)
  649.          end
  650.          close(datafile)
  651.       end
  652.       else do
  653.          say
  654.          say "ERROR :    (CupScheduleNextRound)"
  655.          say
  656.          say "Unable to open '"league_file || input3_file"' for recreation."
  657.          exit
  658.       end
  659.       if repct > 0 then do
  660.          if open(datafile,league_file || input2_file,'a') then do
  661.             writeln(datafile,"*Round="tcrondn" Replays")
  662.             writeln(datafile,"*")
  663.             do p=1 to repct
  664.                if pos("IGNORE",replays.p) = 0 then
  665.                   writeln(datafile,replays.p)
  666.             end
  667.             writeln(datafile,"*")
  668.             close(datafile)
  669.          end
  670.          else do
  671.             say
  672.             say "ERROR :    (CupScheduleNextRound)"
  673.             say
  674.             say "Unable to open '"league_file || input2_file"' for recreation."
  675.             exit
  676.          end
  677.          roundname = bettername(crondn,"Blank")
  678.          say
  679.          say center("'"cupname"'",78)
  680.          say "-------------------------------------------------------------------------------"
  681.          say
  682.          say "Round : "roundname
  683.          say
  684.          say "The following replays will need to be played :"
  685.          say
  686.          do i=1 to repct
  687.             if pos("IGNORE",replays.i) = 0 then
  688.                say replays.i
  689.          end
  690.          say
  691.          say "-------------------------------------------------------------------------------"
  692.          exit
  693.       end
  694.    end
  695.    if repct = 0 then do
  696.       linesct = 0
  697.       if open(datafile,league_file || input3_file,'r') then do
  698.          do while ~eof(datafile)
  699.             line = readln(datafile)
  700.             linesct = linesct + 1
  701.             lines.linesct = line
  702.             if pos(currond,line)   > 0 then crond  = strip(substr(line,11,2))
  703.             if pos(roundddef,line) > 0 then tcrondn= strip(substr(line,11,5))
  704.             if pos(currrondn,line) > 0 then do
  705.                crondn = strip(substr(line,11,30))
  706.                tleg_no = 0
  707.                if pos("1 Leg",line) > 0 then tleg_no = 1
  708.                if pos("2 Legs",line) > 0 then tleg_no = 2
  709.             end
  710.          end
  711.          close(datafile)
  712.       end
  713.       else do
  714.          say
  715.          say "ERROR :    (CupScheduleNextRound)"
  716.          say
  717.          say "Unable to open '"league_file || input3_file"' file."
  718.          exit
  719.       end
  720.       ttc = 0
  721.       mkr = 0
  722.       if open(datafile,league_file || input2_file,'r') then do
  723.          do while ~eof(datafile)
  724.             line = readln(datafile)
  725.             if pos("*Round="strip(tcrondn),line) > 0 & mkr = 0 then do
  726.                if pos("Leg",line) > 0 then do
  727.                   if tleg_no = 1 & pos("1 Leg",line) > 0 then mkr = 1
  728.                   if tleg_no = 2 & pos("2 Legs",line) > 0 then mkr = 1
  729.                end
  730.                else
  731.                   mkr = 1
  732.             end
  733.             if mkr = 1 then do
  734.                ttc = ttc + 1
  735.                matches.ttc = line
  736.             end
  737.          end
  738.          close(datafile)
  739.       end
  740.       else do
  741.          say
  742.          say "ERROR :    (CupScheduleNextRound)"
  743.          say
  744.          say "Unable to open '"league_file || input2_file"' file."
  745.          exit
  746.       end
  747.       winct  = 0
  748.       losct  = 0
  749.       do i=1 to ttc
  750.          if pos("*Round",matches.i) > 0 then
  751.             parse var matches.i "*Round="crondn
  752.          if pos(separator,matches.i) = 0 & pos("#",matches.i) = 0 & matches.i~='' then do
  753.             strng = whosthewinr(i,rnds.crond,crondn,tleg_no,awaygls)
  754.             if pos("REPLAY",strng) = 0 & pos("NOP",strng) = 0 then do
  755.                if pos("#####",strng) = 0 then do
  756.                   parse var strng tempa "*****" tempb
  757.                   mn = 0
  758.                   do k=1 to winct
  759.                      if pos(strip(tempa),working.k) > 0 then mn=1
  760.                   end
  761.                   if mn = 0 then do
  762.                      winct = winct + 1
  763.                      working.winct = strip(tempa)
  764.                   end
  765.                   mn = 0
  766.                   do k=1 to losct
  767.                      if pos(strip(tempb),losing.k) > 0 then mn=1
  768.                   end
  769.                   if mn = 0 then do
  770.                      losct = losct + 1
  771.                      losing.losct = strip(tempb)
  772.                   end
  773.                end
  774.                if pos("#####",strng) > 0 then do
  775.                   parse var strng tempa "#####" tempb
  776.                   winct = winct + 1
  777.                   working.winct = strip(tempb)
  778.                   winct = winct + 1
  779.                   working.winct = strip(tempa)
  780.                   losct = losct + 1
  781.                   losing.losct  = matches.i
  782.                end
  783.             end
  784.             strng = ""
  785.          end
  786.       end
  787.       if tleg_no = 1 then do
  788.          do j=1 to linesct
  789.             if pos(currrondn,lines.j) > 0 then
  790.                lines.j=currrondn||word(tcrondn,1)"   2 Legs"
  791.          end
  792.          if open(datafile,league_file || input3_file,'w') then do
  793.             do p=1 to linesct
  794.                if p = linesct then
  795.                   writech(datafile,lines.p)
  796.                else
  797.                   writeln(datafile,lines.p)
  798.             end
  799.             close(datafile)
  800.          end
  801.          else do
  802.             say
  803.             say "ERROR :    (CupScheduleNextRound)"
  804.             say
  805.             say "Unable to open '"league_file || input3_file"' for recreation."
  806.             exit
  807.          end
  808.          if open(datafile,league_file || input2_file,'a') then do
  809.             writeln(datafile,"*Round="word(tcrondn,1)"   2 Legs")
  810.             writeln(datafile,"*")
  811.             lg1ct = 0
  812.             do i=1 to winct by 2
  813.                l = i + 1
  814.                writeln(datafile,left(working.i,30," ")" __   __ " working.l)
  815.                lg1ct = lg1ct + 1
  816.                leg1match = losing.lg1ct
  817.                leg1match = overlay(" ",leg1match,1,30)
  818.                leg1match = overlay(" ",leg1match,41,30)
  819.                leg1match = overlay("#1st Leg",leg1match,1)
  820.                score1    = substr(leg1match,32,2)
  821.                score2    = substr(leg1match,37,2)
  822.                leg1match = overlay(score2,leg1match,32,2)
  823.                leg1match = overlay(score1,leg1match,37,2)
  824.                writeln(datafile,leg1match)
  825.             end
  826.             writeln(datafile,"*")
  827.             close(datafile)
  828.          end
  829.          else do
  830.             say
  831.             say "ERROR :    (CupScheduleNextRound)"
  832.             say
  833.             say "Unable to open '"league_file || input2_file"' for amendment."
  834.             exit
  835.          end
  836.          say
  837.          say center("'"cupname"'",78)
  838.          say "-------------------------------------------------------------------------------"
  839.          say
  840.          say "Round : "bettername(word(crondn,1),"2 Legs")
  841.          say
  842.          lg1ct = 0
  843.          do i=1 to winct by 2
  844.             l = i + 1
  845.             lg1ct = lg1ct + 1
  846.             leg1match = losing.lg1ct
  847.             s1 = strip(substr(losing.lg1ct,37,2))
  848.             s2 = strip(substr(losing.lg1ct,32,2))
  849.             say left(working.i,30," ")" ("s1")  __   __  ("s2")   "working.l
  850.          end
  851.          say
  852.          say "-------------------------------------------------------------------------------"
  853.          exit
  854.       end
  855.  
  856.       crond = crond + 1
  857.       if pos("Third",crond) > 0 then
  858.          winct = winct + losct
  859.       do j=1 to linesct
  860.          if pos(roundddef,lines.j) > 0 then lines.j=roundddef||rnds.crond
  861.          if pos(currond,lines.j) > 0 then   lines.j=currond||crond
  862.          if pos(currrondn,lines.j) > 0 then do
  863.             if pos("2 Legs",rnds.crond) > 0 then
  864.                lines.j=currrondn||substr(rnds.crond,1,5)"  1 Leg"
  865.             else
  866.                lines.j=currrondn||substr(rnds.crond,1,5)
  867.          end
  868.          if pos(teams_cnt,lines.j) > 0 then lines.j=teams_cnt||winct
  869.       end
  870.       if open(datafile,league_file || input3_file,'w') then do
  871.          do j=1 to linesct
  872.             if pos(separator,lines.j) > 0 then
  873.                writeln(datafile,lines.j)
  874.          end
  875.          if pos("Third",crond) > 0 then do
  876.             writeln(datafile,"#"working.1)
  877.             writeln(datafile,"#"working.2)
  878.             writeln(datafile,losing.1)
  879.             writech(datafile,losing.2)
  880.          end
  881.          else do
  882.             do j=1 to winct
  883.                if j = winct then
  884.                   writech(datafile,working.j)
  885.                else
  886.                   writeln(datafile,working.j)
  887.             end
  888.          end
  889.          close(datafile)
  890.       end
  891.       else do
  892.          say
  893.          say "ERROR :    (CupScheduleNextRound)"
  894.          say
  895.          say "Unable to open '"league_file || input3_file"' for recreation."
  896.          exit
  897.       end
  898.       if pos("Semi",tcrondn) > 0 then do
  899.          if open(outfile,league_file || input2_file,"a") then do
  900.             if pos("YES",third) > 0 then do
  901.                writeln(outfile,"*Round=Third")
  902.                writeln(outfile,"*")
  903.                writeln(outfile,left(losing.1,30)" __   __ " losing.2)
  904.                writeln(outfile,"*")
  905.             end
  906.             leg_do = ""
  907.             if pos("2 Legs",rnds.rndcnt) > 0 then
  908.                leg_do = "1 Leg"
  909.             writeln(outfile,"*Round=Final   "leg_do)
  910.             writeln(outfile,"*")
  911.             writeln(outfile,left(working.1,30)" __   __ " working.2)
  912.             writech(outfile,"*")
  913.             close(outfile)
  914.          end
  915.          else do
  916.             say
  917.             say "ERROR :    (CupScheduleNextRound)"
  918.             say
  919.             say "Unable to append to '"league_file || input2_file"'."
  920.             exit
  921.          end
  922.          leg_do = "Blank"
  923.          if pos("2 Legs",rnds.rndcnt) > 0 then
  924.             leg_do = "1 Leg"
  925.          say
  926.          say center("'"cupname"'",78)
  927.          say "-------------------------------------------------------------------------------"
  928.          say
  929.          say "Round : "bettername("Final",leg_do)
  930.          say
  931.          say left(working.1,30)"  v  "working.2
  932.          say
  933.          say
  934.          if pos("YES",third) > 0 then do
  935.             say "Round : Third Place Play-Off"
  936.             say
  937.             say left(losing.1,30)"  v  "losing.2
  938.             say
  939.          end
  940.          say
  941.          say "-------------------------------------------------------------------------------"
  942.          exit
  943.       end
  944.  
  945.       if pos("AUTOMATIC",scht) > 0 then do
  946.          nums = ''
  947.          numct= 0
  948.          closed = winct
  949.          rndseed= winct * rndcnt
  950.          do while closed ~= 0
  951.             t = random(1,closed,time('s'))
  952.             if find(nums,t) = 0 then do
  953.                if numct = 0 then do
  954.                   numct = 1
  955.                   nums  = t
  956.                end
  957.                else do
  958.                   numct = numct + 1
  959.                   nums = nums||" "||t
  960.                end
  961.             end
  962.             if numct = winct-1 then do
  963.                do f=1 to winct
  964.                   if pos(f,nums) = 0 then
  965.                      nums = nums||" "f
  966.                end
  967.                numct = numct + 1
  968.                closed = 0
  969.             end
  970.          end
  971.       end
  972.       if open(outfile,league_file || input2_file,"a") then do
  973.          leg_do = ""
  974.          if pos("2 Legs",rnds.crond) > 0 then
  975.             leg_do = "1 Leg"
  976.          writeln(outfile,"*Round="word(rnds.crond,1)"   "leg_do)
  977.          writeln(outfile,"*")
  978.          if pos("AUTOMATIC",scht) > 0 then do
  979.             home = 0
  980.             away = 0
  981.             do i=1 to winct by 2
  982.                home = word(nums,i)
  983.                away = word(nums,i+1)
  984.                writeln(outfile,left(working.home,30)" __   __ " working.away)
  985.             end
  986.             writeln(outfile,"*")
  987.          end
  988.          close(outfile)
  989.          if pos("AUTOMATIC",scht) = 0 then do
  990.             if open(datafile,indicator,'w') then do
  991.                writeln(datafile,"Matches To Be Scheduled")
  992.                close(datafile)
  993.             end
  994.             else do
  995.                say
  996.                say "ERROR :    (CupScheduleNextRound)"
  997.                say
  998.                say "Matches need to be manually scheduled but the indicator"
  999.                say "file cannot be created."
  1000.                exit
  1001.             end
  1002.          end
  1003.       end
  1004.       else do
  1005.          say
  1006.          say "ERROR :    (CupScheduleNextRound)"
  1007.          say
  1008.          say "Unable to append to '"league_file || input2_file"'."
  1009.          exit
  1010.       end
  1011.       leg_do = "Blank"
  1012.       if pos("2 Legs",rnds.crond) > 0 then
  1013.          leg_do = "1 Leg"
  1014.       roundname = bettername(substr(rnds.crond,1,5),leg_do)
  1015.       say
  1016.       say center("'"cupname"'",78)
  1017.       say "-------------------------------------------------------------------------------"
  1018.       say
  1019.       say "Round : "roundname
  1020.       say
  1021.       say "The following matches have been scheduled : "
  1022.       say
  1023.       if pos("AUTOMATIC",scht) > 0 then do
  1024.          do i=1 to winct by 2
  1025.             home = word(nums,i)
  1026.             away = word(nums,i+1)
  1027.             say left(working.home,30)" __   __ " working.away
  1028.          end
  1029.          say
  1030.          say "-------------------------------------------------------------------------------"
  1031.       end
  1032.       exit
  1033.    end
  1034. end
  1035.  
  1036. exit
  1037.  
  1038. /* Routine ----------------------------------------------------------- */
  1039.  
  1040. whosthewinr:
  1041. parse arg start,rdef,wroundname,num_leg,gls_away
  1042.  
  1043. et   = 0
  1044. pe   = 0
  1045. etr  = 0
  1046. per  = 0
  1047. spos = start
  1048. fchk = 0
  1049.  
  1050. if pos("Replay",wroundname) = 0 then do
  1051.    if pos("Extra Time",rdef) > 0 then et = 2
  1052.    if pos("Penalties",rdef) > 0 then  pe = 2
  1053.    if pos("2 Legs",rdef) > 0 & num_leg = 1 then do
  1054.       teama = strip(substr(matches.spos,1,30))
  1055.       teamb = strip(substr(matches.spos,41,30))
  1056.       return teama"#####"teamb
  1057.    end
  1058.    teama = strip(substr(matches.spos,1,30))
  1059.    teamb = strip(substr(matches.spos,41,30))
  1060.    form = strip(substr(matches.spos,32,2))
  1061.    agam = strip(substr(matches.spos,37,2))
  1062.    if pos("2 Legs",rdef) > 0 & num_leg = 2 then do
  1063.       spos = spos + 1
  1064.       for1l = strip(substr(matches.spos,32,2))
  1065.       aga1l = strip(substr(matches.spos,37,2))
  1066.       if for1l = '' | aga1l = '' then return "NOP"
  1067.       for = form + for1l
  1068.       aga = agam + aga1l
  1069.    end
  1070.    else do
  1071.       for = form
  1072.       aga = agam
  1073.    end
  1074.    if for > aga then do
  1075.       return teama"*****"teamb
  1076.    end
  1077.    if for < aga then do
  1078.       return teamb"*****"teama
  1079.    end
  1080.    if num_leg = 2 & pos("YES",gls_away) > 0 then do
  1081.       if for = aga then do
  1082.          if for1l > agam then return teama"*****"teamb
  1083.          if for1l < agam then return teamb"*****"teama
  1084.       end
  1085.    end
  1086.    if for = aga & et = 2 then do
  1087.       spos = spos + 1
  1088.       for1 = strip(substr(matches.spos,32,2))
  1089.       aga1 = strip(substr(matches.spos,37,2))
  1090.       if pos("2 Legs",rdef) > 0 & num_leg = 2 then do
  1091.          f1 = for1 + for1l
  1092.          a1 = aga1 + aga1l
  1093.          if f1 > a1 then return teama"*****"teamb
  1094.          if f1 < a1 then return teamb"*****"teama
  1095.          if f1 = a1 then do
  1096.             if pos("YES",gls_away) > 0 then do
  1097.                if for1l > aga1 then return teama"*****"teamb
  1098.                if for1l < aga1 then return teamb"*****"teama
  1099.                if for1l = aga1 then do
  1100.                   if for1 = for1l & aga1 = aga1l then
  1101.                      for1 = aga1
  1102.                   else do
  1103.                      if aga1 > for1l then return teamb"*****"teama
  1104.                      if aga1 < for1l then return teama"*****"teamb
  1105.                      if aga1 = for1l then for1 = aga1
  1106.                   end
  1107.                end
  1108.             end
  1109.             else
  1110.                for1 = aga1    /* to force penalties when ET after 2 legs */
  1111.          end
  1112.       end
  1113.       if for1 > aga1 then return teama"*****"teamb
  1114.       if for1 < aga1 then return teamb"*****"teama
  1115.       if for1 = aga1 & pe = 2 then do
  1116.          spos = spos + 1
  1117.          for2 = strip(substr(matches.spos,32,2))
  1118.          aga2 = strip(substr(matches.spos,37,2))
  1119.          if for2 > aga2 then return teama"*****"teamb
  1120.          if for2 < aga2 then return teamb"*****"teama
  1121.          if for2 = aga2 then do
  1122.             repct = repct + 1
  1123.             replays.repct = matches.start
  1124.             return "REPLAY"
  1125.          end
  1126.       end
  1127.       else do
  1128.          repct = repct + 1
  1129.          replays.repct = matches.start
  1130.          return "REPLAY"
  1131.       end
  1132.    end
  1133.    if for = aga & pe = 2 & et = 0 then do
  1134.       spos = spos + 1
  1135.       for1 = strip(substr(matches.spos,32,2))
  1136.       aga1 = strip(substr(matches.spos,37,2))
  1137.       if for1 > aga1 then return teama"*****"teamb
  1138.       if for1 < aga1 then return teamb"*****"teama
  1139.       if for1 = aga1 then do
  1140.          repct = repct + 1
  1141.          replays.repct = matches.start
  1142.          return "REPLAY"
  1143.       end
  1144.    end
  1145.    if for = aga & pe = 0 & et = 0 then do
  1146.       repct = repct + 1
  1147.       replays.repct = matches.start
  1148.       return "REPLAY"
  1149.    end
  1150. end
  1151.  
  1152. spos = start
  1153. if pos("Replay",wroundname) > 0 then do
  1154.    if pos("Extra Time",rdef) > 0 then   et  = 2
  1155.    if pos("E.T If Rep.",rdef) > 0 then  etr = 2
  1156.    if pos("Penalties",rdef) > 0 then    pe  = 2
  1157.    if pos("Pen If Rep.",rdef) > 0 then  per = 2
  1158.  
  1159.    teama = strip(substr(matches.spos,1,30))
  1160.    teamb = strip(substr(matches.spos,41,30))
  1161.    for = strip(substr(matches.spos,32,2))
  1162.    aga = strip(substr(matches.spos,37,2))
  1163.    if for > aga then return teama"*****"teamb
  1164.    if for < aga then return teamb"*****"teama
  1165.    if for = aga & (etr = 2 | et = 2) then do
  1166.       spos = spos + 1
  1167.       for1 = 0
  1168.       aga1 = 0
  1169.       for1 = strip(substr(matches.spos,32,2))
  1170.       aga1 = strip(substr(matches.spos,37,2))
  1171.       if for1 > aga1 then return teama"*****"teamb
  1172.       if for1 < aga1 then return teamb"*****"teama
  1173.       if for1 = aga1 & (per = 2 | pe = 2) then do
  1174.          spos = spos + 1
  1175.          for2 = strip(substr(matches.spos,32,2))
  1176.          aga2 = strip(substr(matches.spos,37,2))
  1177.          if for2 > aga2 then return teama"*****"teamb
  1178.          if for2 < aga2 then return teamb"*****"teama
  1179.          if for2 = aga2 then do
  1180.             repct = repct + 1
  1181.             replays.repct = matches.start
  1182.             return "REPLAY"
  1183.          end
  1184.       end
  1185.       else do
  1186.          repct = repct + 1
  1187.          replays.repct = matches.start
  1188.          return "REPLAY"
  1189.       end
  1190.    end
  1191.    if for = aga & per = 2 & et = 0 & etr = 0 then do
  1192.       spos = spos + 1
  1193.       for1 = strip(substr(matches.spos,32,2))
  1194.       aga1 = strip(substr(matches.spos,37,2))
  1195.       if for1 > aga1 then return teama"*****"teamb
  1196.       if for1 < aga1 then return teamb"*****"teama
  1197.       if for1 = aga1 then do
  1198.          repct = repct + 1
  1199.          replays.repct = matches.start
  1200.          return "REPLAY"
  1201.       end
  1202.    end
  1203.    if for = aga & pe = 0 & et = 0 & etr = 0 & per = 0 then do
  1204.       repct = repct + 1
  1205.       replays.repct = matches.start
  1206.       return "REPLAY"
  1207.    end
  1208. end
  1209.  
  1210. return "NOP"
  1211.  
  1212. /* Routine ----------------------------------------------------------- */
  1213.  
  1214. bettername:
  1215. parse arg crn,legless
  1216.  
  1217. trdn = substr(crn,1,1)
  1218. if datatype(trdn,'n') = 1 then do
  1219.    if pos("Replay",crn) > 0 then
  1220.       trn = strip(word(crn,1))" Round "strip(word(crn,2))
  1221.    else do
  1222.       parse var crn roundno" "extra
  1223.       trn = strip(crn)" Round "extra
  1224.    end
  1225. end
  1226. else do
  1227.    if pos("Final",crn) > 0 then do
  1228.       parse var crn . " "extra
  1229.       if pos("Replay",crn) > 0 then
  1230.          trn = "Final "extra
  1231.       else
  1232.          trn = "Final"
  1233.    end
  1234.    if pos("Semi",crn) > 0 then do
  1235.       parse var crn . " "extra
  1236.       if pos("Replay",crn) > 0 then
  1237.          trn = "Semi-Final "extra
  1238.       else
  1239.          trn = "Semi-Finals"
  1240.    end
  1241.    if pos("Quart",crn) > 0 then do
  1242.       parse var crn . " "extra
  1243.       if pos("Replay",crn) > 0 then
  1244.          trn = "Quarter-Final "extra
  1245.       else
  1246.          trn = "Quarter-Finals"
  1247.    end
  1248.    if pos("Third",crn) > 0 then do
  1249.       trn = "Third Place Play-Off"
  1250.    end
  1251. end
  1252.  
  1253. if pos("1 Leg",legless) > 0 then
  1254.    trn = trn||"  (1st Leg)"
  1255. if pos("2 Legs",legless) > 0 then
  1256.    trn = trn||"  (2nd Leg)"
  1257.  
  1258. return trn
  1259.  
  1260. /* ------------------------------------------------------------------- */