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

  1. /* ***********************************************************************
  2.  
  3.    CUP SCHEDULER PROGRAM FOR FOOTBALL REXX SUITE
  4.   -----------------------------------------------
  5.                    Copyright  Mark Naughton 1997
  6.  
  7.  
  8. Version    Date     History
  9. --------------------------------------------------------------------------
  10.  1.0       121097   Wrote the random match generator.
  11.            201097   Wrote the main routine that creates all the files,
  12.                     such as sorting the 'cf' file, creating the
  13.                     working/history files and randomly generating a
  14.                     schedule. This program will ONLY create the files and
  15.                     not maintain them. That is left to
  16.                     "CupScheduleNextRound.rexx".
  17.            221097   Tidied report and added number of matches to be
  18.                     played. If a history file is found but it is switched
  19.                     off then it will not be deleted.
  20.            311097   Changed format of 'working' file - added CRDN.
  21.            091297   Cup history file creation has been dummied out for
  22.                     the time being.
  23.            151297   Tidied display and removed CupHistory code.
  24.            060698   Added code for manual scheduling.
  25.            260898   Added code for handling 2 legs.
  26.            050998   Didn't read setting for away goals.
  27.  
  28. **************************************************************************
  29.  
  30. Procedure
  31. ---------
  32.  
  33. 1. Check files exist.
  34. 2. Read '.cf' and write teams to output file.
  35. 3. Using external program, sort the teams.
  36. 4. Re-read the teams and recreate '.cf'.
  37. 5. Open '.cf' for reading.
  38. 6. Extract data and store.
  39. 7. If no separator in it, then take it as a
  40.    team. Strip blanks and store.
  41. 8. Close input file.
  42. 9. Create '.cfrw' working file with round/teams.
  43. 10.Randomly generate who is going to play who and store in array but only
  44.    if the matches are to be automatically scheduled.
  45. 11.Create '.scf' file and using the order in which the numbers were
  46.    generated earlier, write the particular teams.
  47. 12.Write report and exit.
  48.  
  49. ************************************************************************** */
  50. PARSE ARG league_stuff
  51.  
  52. version      = 1
  53. input_file   = '.cf'
  54. input2_file  = '.scf'
  55. input3_file  = '.cfrw'
  56. input4_file  = '.cfh'
  57. output3_file = '.temp'
  58. title        = '*CUP_TITLE='
  59. thirdpl      = '*CUP_THIRD='
  60. rounddef     = '*CUP_RNDDEF='
  61. cuphistory   = '*CUP_HISTORY='
  62. schedtype    = '*CUP_SCHEDULE='
  63. awaygs       = '*CUP_AWAYG='
  64. pkversion    = '*  Version='
  65. matches.     = '???'
  66. separator    = '*'
  67. teams.       = '???'
  68. rnds.        = '???'
  69. tcount       = 0
  70. ttc          = 0
  71. rndcnt       = 0
  72. league_file  = "Data/"league_stuff
  73.  
  74.  
  75. if exists(league_file || input_file) = 0 then exit
  76.  
  77. ttc = 0
  78. tcount = 0
  79. if open(datafile,league_file || input_file,'r') then do
  80.    if open(datafile2,league_file || output3_file,'w') then do
  81.       do while ~eof(datafile)
  82.          line = readln(datafile)
  83.          line = strip(line)
  84.          if pos(separator,line) > 0 then do
  85.             tcount = tcount + 1
  86.             teams.tcount = line
  87.          end
  88.          else do
  89.             ttc = ttc + 1
  90.             writeln(datafile2,line)
  91.          end
  92.       end
  93.       close(datafile2)
  94.    end
  95.    else do
  96.      say
  97.      say "ERROR :    (CupScheduler)"
  98.      say
  99.      say "Cannot write to '"league_file||output3_file"'."
  100.      close(datafile)
  101.      exit
  102.    end
  103.    close(datafile)
  104. end
  105. else do
  106.    say
  107.    say "ERROR :    (CupScheduler)"
  108.    say
  109.    say "Cannot open '"league_file || input_file"' for reading/sorting."
  110.    exit
  111. end
  112.  
  113. address command 'sort 'league_file || output3_file league_file || output3_file
  114.  
  115. if open(datafile2,league_file || output3_file,'r') then do
  116.    if open(datafile,league_file || input_file,'w') then do
  117.       do i=1 to tcount
  118.          writeln(datafile,teams.i)
  119.       end
  120.       do j=1 to ttc                   /* while ~eof(datafile2)  */
  121.          line = readln(datafile2)
  122.          line = strip(line)
  123.          if j = ttc then
  124.             writech(datafile,line)
  125.          else
  126.             writeln(datafile,line)
  127.       end
  128.       close(datafile)
  129.    end
  130.    else do
  131.      say
  132.      say "ERROR :    (CupScheduler)"
  133.      say
  134.      say "Cannot write to '"league_file||input_file"'."
  135.      close(datafile2)
  136.      exit
  137.    end
  138.    close(datafile2)
  139. end
  140. else do
  141.    say
  142.    say "ERROR :    (CupScheduler)"
  143.    say
  144.    say "Cannot read '"league_file||output3_file"'."
  145.    close(datafile)
  146.    exit
  147. end
  148.  
  149. address command 'delete >NIL: 'league_file || output3_file
  150.  
  151. tcount = 0
  152. if open(datafile,league_file || input_file,'r') then do
  153.    do while ~eof(datafile)
  154.       line = readln(datafile)
  155.       if pos(title,line) > 0 then        cupname  = delstr(line,1,11)
  156.       if pos(thirdpl,line) > 0 then      third    = delstr(line,1,11)
  157.       if pos(awaygs,line) > 0 then       awaygoals= delstr(line,1,11)
  158.       if pos(pkversion,line) > 0 then    fversion = delstr(line,1,12)
  159.       if pos(cuphistory,line) > 0 then   cuph     = delstr(line,1,13)
  160.       if pos(schedtype,line) > 0 then    scht     = delstr(line,1,14)
  161.       if pos(rounddef,line) > 0 then do
  162.          rndcnt = rndcnt + 1
  163.          rnds.rndcnt = delstr(line,1,12)
  164.       end
  165.       if pos(separator,line) = 0 then do
  166.          line = strip(line)
  167.          tcount       = tcount + 1
  168.          teams.tcount = line
  169.       end
  170.    end
  171.    close(datafile)
  172. end
  173. else do
  174.    say
  175.    say "ERROR :    (CupScheduler)"
  176.    say
  177.    say "Unable to open '"league_file || input_file"' file."
  178.    exit
  179. end
  180.  
  181. if open(datafile2,league_file || input3_file,'w') then do
  182.    writeln(datafile2,"*")
  183.    writeln(datafile2,"* WORKING CUP FILE")
  184.    writeln(datafile2,"*")
  185.    writeln(datafile2,"*CUP_TITLE="cupname)
  186.    writeln(datafile2,"*CUP_ROND=1")
  187.    if pos("2 Legs",rnds.1) > 0 then
  188.       writeln(datafile2,"*CUP_CRDN="left(rnds.1,6)"  1 Leg")
  189.    else
  190.       writeln(datafile2,"*CUP_CRDN="left(rnds.1,6))
  191.    writeln(datafile2,"*CUP_RDEF="rnds.1)
  192.    writeln(datafile2,"*CUP_TCNT="tcount)
  193.    writeln(datafile2,"*")
  194.    do i=1 to tcount
  195.       if i = tcount then
  196.          writech(datafile2,teams.i)
  197.       else
  198.          writeln(datafile2,teams.i)
  199.    end
  200.    close(datafile2)
  201. end
  202. else do
  203.    say
  204.    say "ERROR :    (CupScheduler)"
  205.    say
  206.    say "Unable to write to '"league_file || input3_file"' file."
  207.    exit
  208. end
  209.  
  210. /* Schedule matches */
  211.  
  212. if pos("AUTOMATIC",scht) > 0 then do
  213.    nums = ''
  214.    numct= 0
  215.    closed = tcount
  216.    rndseed= tcount * rndcnt
  217.    do while closed ~= 0
  218.       t = random(1,closed,time('s'))
  219.       if find(nums,t) = 0 then do
  220.          if numct = 0 then do
  221.             numct = 1
  222.             nums  = t
  223.          end
  224.          else do
  225.             numct = numct + 1
  226.             nums = nums||" "||t
  227.          end
  228.       end
  229.       if numct = tcount-1 then do
  230.          do f=1 to tcount
  231.             if pos(f,nums) = 0 then
  232.                nums = nums||" "f
  233.          end
  234.          numct = numct + 1
  235.          closed = 0
  236.       end
  237.    end
  238. end
  239.  
  240. if open(outfile,league_file || input2_file,"w") then do
  241.    writeln(outfile,"*")
  242.    writeln(outfile,"**" cupname)
  243.    writeln(outfile,"*")
  244.    writeln(outfile,"*")
  245.    if pos("2 Legs",rnds.1) > 0 then
  246.       writeln(outfile,"*Round="word(rnds.1,1)"   1 Leg")
  247.    else
  248.       writeln(outfile,"*Round="word(rnds.1,1))
  249.    writeln(outfile,"*")
  250.    if pos("AUTOMATIC",scht) > 0 then do
  251.       home = 0
  252.       away = 0
  253.       do i=1 to tcount by 2
  254.          home = word(nums,i)
  255.          away = word(nums,i+1)
  256.          writech(outfile,left(teams.home,30))
  257.          writeln(outfile," __   __ " teams.away)
  258.       end
  259.       writech(outfile,"*")
  260.    end
  261.    close(outfile)
  262. end
  263. else do
  264.    say
  265.    say "ERROR :    (CupScheduler)"
  266.    say
  267.    say "Unable to write to '"league_file || input2_file"'."
  268.    exit
  269. end
  270.  
  271. matches = 0
  272. left    = tcount
  273.  
  274. do while left ~= 1
  275.    matches = matches + (left/2)
  276.    left = left / 2
  277. end
  278. if pos("YES",third) > 0 then
  279.    matches = matches + 1
  280.  
  281. do i=1 to rndcnt                    /* amend count if two legs */
  282.    if pos("2 Legs",rnds.i) > 0 then
  283.       matches = matches + 1
  284. end
  285.  
  286. say
  287. say center("Report for '"cupname"'",78)
  288. say "-------------------------------------------------------------------------------"
  289. say "                                               Created "date('e')" at "time()
  290. say
  291. say "Version: "fversion
  292. say "_______________________________________________________________________________"
  293. say
  294. say "Number of teams   : "left(tcount,3)"        Number of Matches : "matches
  295. say "                               (excluding replays)"
  296. say
  297. if pos("MANUAL",scht) > 0 then
  298.    say "Matches will be manually scheduled."
  299. else
  300.    say "Matches will be automatically scheduled."
  301. if pos("YES",third) > 0 then
  302.    say "Third place play-offs will take place."
  303. if pos("YES",cuph) > 0 then
  304.    say "Cup history will be stored."
  305. if pos("YES",awaygoals) > 0 then do
  306.    say "Away Goals will be used for all 2-leg matches. If there aren't"
  307.    say "any, then it will be ignored."
  308. end
  309. say
  310. say "'"cupname"' is scheduled for "rndcnt" rounds, with the following settings"
  311. say "regarding Extra Time and Penalties and Legs for each round as listed below..."
  312. say
  313. say "Round  ExtraTime     Penalties       Legs"
  314. say "--------------------------------------------------"
  315. say
  316. do i=1 to rndcnt
  317.    if pos("1 Leg",rnds.i) > 0 then do
  318.       k = pos("1 Leg",rnds.i)
  319.       rnds.i = overlay("      ",rnds.i,k)
  320.       say rnds.i
  321.    end
  322.    else
  323.       say rnds.i
  324. end
  325. say
  326. say "_______________________________________________________________________________"
  327. say
  328. say "Team Listing: "
  329. say "-------------"
  330. say
  331. do i=1 to tcount
  332.    say teams.i
  333. end
  334. say
  335. say "_______________________________________________________________________________"
  336. say
  337. if pos("2 Legs",rnds.1) > 0 then
  338.    say "Round: "bettername(word(rnds.1,1),"1 Leg")
  339. else
  340.    say "Round: "bettername(word(rnds.1,1),"Blank")
  341. say "------------------------------------"
  342. say
  343. if pos("AUTOMATIC",scht) > 0 then do
  344.    home = 0
  345.    away = 0
  346.    do i=1 to tcount by 2
  347.       home = word(nums,i)
  348.       away = word(nums,i+1)
  349.       say left(teams.home,30)" __   __  "teams.away
  350.    end
  351.    say
  352.    say "-------------------------------------------------------------------------------"
  353.    say
  354. end
  355.  
  356. exit
  357.  
  358. /* Routine ----------------------------------------------------------- */
  359.  
  360. bettername:
  361. parse arg crn,legless
  362.  
  363. trdn = strip(substr(crn,1,1))
  364. if datatype(trdn,'n') = 1 then do
  365.    parse var crn roundno" "extra
  366.    trn = strip(crn)" Round "extra
  367. end
  368. else do
  369.    if pos("Final",crn) > 0 then do
  370.       parse var crn . " "extra
  371.       if pos("Replay",crn) > 0 then
  372.          trn = "Final "extra
  373.       else
  374.          trn = "Final"
  375.    end
  376.    if pos("Semi",crn) > 0 then do
  377.       parse var crn . " "extra
  378.       if pos("Replay",crn) > 0 then
  379.          trn = "Semi-Final "extra
  380.       else
  381.          trn = "Semi-Finals"
  382.    end
  383.    if pos("Quart",crn) > 0 then do
  384.       parse var crn . " "extra
  385.       if pos("Replay",crn) > 0 then
  386.          trn = "Quarter-Final "extra
  387.       else
  388.          trn = "Quarter-Finals"
  389.    end
  390.    if pos("Third",crn) > 0 then do
  391.       trn = "Third Place Play-Off"
  392.    end
  393. end
  394.  
  395. if pos("1 Leg",legless) > 0 then
  396.    trn = trn||"  (1st Leg)"
  397. if pos("2 Legs",legless) > 0 then
  398.    trn = trn||"  (2nd Leg)"
  399.  
  400. return trn
  401.  
  402. /* ------------------------------------------------------------------- */