/* Mode=Run */
/* ***********************************************************************

   TABLE OF TRUTH PROGRAM FOR FOOTBALL REXX SUITE
  ----------------------------------------------------
                   Copyright  Mark Naughton 2000

                                  From a suggestion by Martin Bilek


Version    Date     History
--------------------------------------------------------------------------
           270899   Used alternative League script as template before
                    customisation.

 1.0       260500   First release - customise points below for different
                    statistics. Displays win/draw/loss/goals/pts for home
                    and away with total points and goal difference.


**************************************************************************

Procedure
---------

1. Check files exist. Open 'Teams.df'.
2. Read in teams and their associated data; WIN, DRAW, LOST etc.
3. Close file. Open 'Teams.stats'. Read data. Close file.
4. Open 'Teams.sf' for reading.
5. Read each line that has a game played, and from that get the team names
   and the score. Then update the relevant team data with the result.
6. Write data in form of a league table to an output file.
7. Close file.
8. Call external sortprogram to sort the output file.
9. Open 'Teams.stats' and read the number of teams, then close.
10.Open 'League.output' and read contents into two arrays.
11.Check to see if the teams are in the right order, comparing the points
   of each one with the next and if the goal-differences are in the wrong
   order, then swap the two teams.
12.Check to see which teams are relegated by calculating the maximum number
   of points available and seeing if the lower teams are able to move up.
   If not they are relegated, according to the number set.
13.Check to see which teams are promoted by using the same method as above.
14.Display league and exit.

************************************************************************** */
parse arg league_file


/* ---------------------------------------------------------------- */
/* Change these values for Home and Away to see your Table of Truth */
/* ---------------------------------------------------------------- */

hptswin = 0
hptsdrw = -2
hptslse = -3

aptswin = 3
aptsdrw = 1
aptslse = 0

/* ---------------------------------------------------------------- */

version      = 2
league_file  = "Data/" || league_file
input_file   = '.stats'
input2_file  = '.sf'
input3_file  = '.df'
output_file  = 'Data/League.output'
title        = '*LEAGUE_NAME='
points_win   = '*POINTS_PER_WIN='
points_drw   = '*POINTS_PER_DRW='
points_lse   = '*POINTS_PER_LSE='
points_gls   = '*POINTS_PER_GLS='
releg        = '*RELEGATION='
playother    = '*PLAY_OTHER='
promoted     = '*PROMOTED='
team_counter = '*COUNTR='
team         = '*TEAM='
played       = '*PLY='
won          = '*WIN='
drawn        = '*DRW='
lost         = '*LST='
goalsf       = '*GOF='
goalsa       = '*GOA='
points       = '*PTS='
separator    = '*'
teams.       = '???'
teams2.      = '???'
m_ply.       = '???'
m_win.       = '???'
m_drw.       = '???'
m_lost.      = '???'
m_gof.       = '???'
m_goa.       = '???'
m_pts.       = '???'
h_ply.       = '???'
h_win.       = '???'
h_drw.       = '???'
h_lost.      = '???'
h_gof.       = '???'
h_goa.       = '???'
h_pts.       = '???'
a_ply.       = '???'
a_win.       = '???'
a_drw.       = '???'
a_lost.      = '???'
a_gof.       = '???'
a_goa.       = '???'
a_pts.       = '???'
ah_teams.    = '???'
teams_ctr    = 0
not_played   = '__   __'


if open(datafile,"Data/Football.locale",'r') then do
   line = readln(datafile)
   locdir = strip(line)
   close(datafile)
end
else do
   say
   say "ERROR :    (TableOfTruth)"
   say
   say "Cannot read 'Data/Football.locale' for the locale settings."
   exit
end

if open(datafile,"ENV:FootballRXPath",'r') then do
   line = readln(datafile)
   rxdir = strip(line)
   close(datafile)
end
else
   rxdir = "SYS:Rexxc/"

locdir = locdir"User/TableOfTruth.data"

if exists(locdir) > 0 then do
  address command rxdir'rx 'locdir
  VarCount = getclip('VarCount')
  do i = 1 to VarCount
    interpret getclip('var.'i)
  end
end
else do
   say
   say "ERROR :    (TableOfTruth)"
   say
   say "Cannot find '"locdir"' to read locale settings."
   exit
end

if exists(league_file || input_file) = 0  then do
   say
   say al_error
   say
   say al_one"'"league_file||input_file"'."
   exit
end
if exists(league_file || input2_file) = 0 then do
   say
   say al_error
   say
   say al_one"'"league_file||input2_file"'."
   exit
end
if exists(league_file || input3_file) = 0 then do
   say
   say al_error
   say
   say al_one"'"league_file||input3_file"'."
   exit
end


tcount = 0
if open(datafile,league_file || input3_file,'r') then do
   do while ~eof(datafile)
      line = readln(datafile)
      if pos(title,line) > 0 then        league_title = delstr(line,1,13)
      if pos(separator,line) = 0 & line ~= "" then do
         line = strip(line)
         tcount          = tcount + 1
         teams.tcount    = line
         ah_teams.tcount = line
      end
   end
   close(datafile)
end
else do
   say
   say al_error
   say
   say al_two"'"league_file || input3_file"'."
   exit
end

if open(datafile,league_file || input_file,'r') then do
   do while ~eof(datafile)
      line = readln(datafile)
      if pos(team,line) > 0 then
         teams_ctr = teams_ctr + 1
      if pos(played,line) > 0 then  m_ply.teams_ctr  = delstr(line,1,5)
      if pos(won,line) > 0 then     m_won.teams_ctr  = delstr(line,1,5)
      if pos(drawn,line) > 0 then   m_drw.teams_ctr  = delstr(line,1,5)
      if pos(lost,line) > 0 then    m_lost.teams_ctr = delstr(line,1,5)
      if pos(goalsf,line) > 0 then  m_gof.teams_ctr  = delstr(line,1,5)
      if pos(goalsa,line) > 0 then  m_goa.teams_ctr  = delstr(line,1,5)
      if pos(points,line) > 0 then  m_pts.teams_ctr  = delstr(line,1,5)
   end
   close(datafile)
end
else do
   say
   say al_error
   say
   say al_two"'"league_file || input_file"'."
   exit
end

ggg = 0
do i=1 to teams_ctr
   if m_ply.i ~= 0 then ggg = 1
end
if ggg = 1 then do
   do i=1 to teams_ctr
      h_ply.i = m_ply.i
      h_won.i = m_won.i
      h_drw.i = m_drw.i
      h_lost.i= m_lost.i
      h_gof.i = m_gof.i
      h_goa.i = m_goa.i
      h_pts.i = m_pts.i
   end
end
else do
   do i=1 to teams_ctr
      h_ply.i = 0
      h_won.i = 0
      h_drw.i = 0
      h_lost.i= 0
      h_gof.i = 0
      h_goa.i = 0
      h_pts.i = 0
      a_ply.i = 0
      a_won.i = 0
      a_drw.i = 0
      a_lost.i= 0
      a_gof.i = 0
      a_goa.i = 0
      a_pts.i = 0
   end
end

if open(datafile,league_file || input2_file,'r') then do
   do while ~eof(datafile)
      line = readln(datafile)
      if pos(separator,line) = 0 then do
         if pos(not_played,line) = 0 then do
            home_team = strip(substr(line,1,30))
            goals_for = substr(line,32,2)
            goals_aga = substr(line,37,2)
            away_team = strip(substr(line,41,30))

            do i=1 to teams_ctr
               if home_team == teams.i then do
                  m_ply.i = m_ply.i + 1
                  m_gof.i = m_gof.i + goals_for
                  m_goa.i = m_goa.i + goals_aga
                  h_ply.i = h_ply.i + 1
                  h_gof.i = h_gof.i + goals_for
                  h_goa.i = h_goa.i + goals_aga
                  if goals_for = goals_aga then do
                     m_drw.i = m_drw.i + 1
                     m_pts.i = m_pts.i + hptsdrw
                     h_drw.i = h_drw.i + 1
                     h_pts.i = h_pts.i + hptsdrw
                  end
                  if goals_for < goals_aga then do
                     m_lost.i= m_lost.i + 1
                     m_pts.i = m_pts.i + hptslse
                     h_lost.i= h_lost.i + 1
                     h_pts.i = h_pts.i + hptslse
                  end
                  if goals_for > goals_aga then do
                     m_won.i = m_won.i + 1
                     m_pts.i = m_pts.i + hptswin
                     h_won.i = h_won.i + 1
                     h_pts.i = h_pts.i + hptswin
                  end
               end
            end
            do i=1 to teams_ctr
               if away_team == teams.i then do
                  m_ply.i = m_ply.i + 1
                  m_gof.i = m_gof.i + goals_aga
                  m_goa.i = m_goa.i + goals_for
                  a_ply.i = a_ply.i + 1
                  a_gof.i = a_gof.i + goals_aga
                  a_goa.i = a_goa.i + goals_for
                  if goals_for = goals_aga then do
                     m_drw.i = m_drw.i + 1
                     m_pts.i = m_pts.i + aptsdrw
                     a_drw.i = a_drw.i + 1
                     a_pts.i = a_pts.i + aptsdrw
                  end
                  if goals_for < goals_aga then do
                     m_won.i = m_won.i + 1
                     m_pts.i = m_pts.i + aptswin
                     a_won.i = a_won.i + 1
                     a_pts.i = a_pts.i + aptswin
                  end
                  if goals_for > goals_aga then do
                     m_lost.i= m_lost.i + 1
                     m_pts.i = m_pts.i + aptslse
                     a_lost.i= a_lost.i + 1
                     a_pts.i = a_pts.i + aptslse
                  end
               end
            end
         end
      end
   end
   close(datafile)
end
else do
   say
   say al_error
   say
   say al_two"'"league_file || input2_file"'."
   exit
end

if open(outfile,output_file,"w") then do
   do i=1 to teams_ctr
      line = left(teams.i,30,' ')
      mp = right(m_ply.i,3)
      mw = right(m_won.i,3)
      md = right(m_drw.i,3)
      ml = right(m_lost.i,3)
      mgf = right(m_gof.i,5)
      mga = right(m_goa.i,5)
      mpts = right(m_pts.i,7)
      writech(outfile,line"   "mp" "mw" "md" "ml" "mgf" "mga"  "mpts"      ")
      itemp=mgf-mga
      if itemp>0 then
         itemp=insert("+",itemp,0,1)
      itemp=right(itemp,4)
      writeln(outfile,itemp)
   end
   close(outfile)
end
else do
   say
   say al_error
   say
   say al_three"'"output_file"'."
   exit
end

address command 'Exec/footsort '

if open(datafile2,output_file,'r') then do
   do i=1 to tcount
      line = readln(datafile2)
      teams.i  = line
      teams2.i = line
   end
   close(datafile2)
end
else do
   say
   say al_error
   say
   say al_four"'"output_file"'."
   exit
end

ctr = 0
do while swapctr > 0
   swapctr=9
   do i=1 to tcount-1                               /* was 64,4 and 77,4 */
      swapdone=0
      j = i + 1
      if strip(substr(teams.i,64,7)) = strip(substr(teams.j,64,7)) then do
         goaldiffa = strip(substr(teams.i,77,5))
         goaldiffb = strip(substr(teams.j,77,5))
         if goaldiffa < goaldiffb then do
            teams.i = teams2.j
            teams.j = teams2.i
            teams2.i= teams.i
            teams2.j= teams.j
            swapdone= 1
            ctr = ctr + 1
         end
      end
      if swapdone = 1 then
         break
   end
   if ctr = 0 then
      swapctr = 0
   else
      ctr = 0
end


/* ---------------------------------------------- */

lengtt = 0
do i=1 to tcount
   if length(ah_teams.i) > lengtt then
      lengtt = length(ah_teams.i)
end
if lengtt > 30 then
   lengtt = 30

tabtitle = al_five""left(" ",lengtt+3,' ')""al_six
tabunder = left("-",length(tabtitle)+4,'-')

say
say center(league_title,88)
say tabunder
say
if ggg = 1 then do
   say al_seven
   say al_eight
end
say
say al_nine""left("Team",lengtt+2,' ')""al_ten
say
say tabtitle
say tabunder
say

do i=1 to tcount
   team1 = strip(substr(teams.i,1,30))
   do j=1 to tcount
      if pos(team1,ah_teams.j) > 0 then do
         h1 = right(h_ply.j,4,' ')
         h2 = right(h_won.j,4,' ')
         h3 = right(h_drw.j,4,' ')
         h4 = right(h_lost.j,4,' ')
         h5 = right(h_gof.j,4,' ')
         h6 = right(h_goa.j,4,' ')
         h7 = right(h_pts.j,5,' ')
         a1 = right(a_ply.j,4,' ')
         a2 = right(a_won.j,4,' ')
         a3 = right(a_drw.j,4,' ')
         a4 = right(a_lost.j,4,' ')
         a5 = right(a_gof.j,4,' ')
         a6 = right(a_goa.j,4,' ')
         a7 = right(a_pts.j,5,' ')
         hatot = right(h_pts.j+a_pts.j,5,' ')
         gdif = substr(teams.i,76,5)
         if strip(gdif) = 0 then
            gdif = " "
         rocky = left(i,2,' ')||") "h1||h2||h3||h4||h5||h6||h7||"  "left(ah_teams.j,lengtt+2)"  "a1||a2||a3||a4||a5||a6||a7"   "hatot"   "gdif
         say rocky
      end
   end
end

say
say tabunder
say
say

say al_a1
uline = "-"
do i=1 to length(al_a1)
   uline=uline"-"
end
say uline
say
say strip(al_nine)" "al_a2" : "right(hptswin,3)
say strip(al_nine)" "al_a3" : "right(hptsdrw,3)
say strip(al_nine)" "al_a4" : "right(hptslse,3)
say
say strip(al_ten)" "al_a2" : "right(aptswin,3)
say strip(al_ten)" "al_a3" : "right(aptsdrw,3)
say strip(al_ten)" "al_a4" : "right(aptslse,3)


exit