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

   PERFORMANCE RATING PROGRAM FOR FOOTBALL REXX SUITE
  ----------------------------------------------------

    Copyright  Mark Naughton 1998
                                        From an idea by Steve Holland

Version    Date     History
--------------------------------------------------------------------------
 1.0       160998   First release. Will give different tables according to
                    the values set below.
           230998   Bug found where the values to be calculated for the
                    position were summed to be Zero - Program failed.
                    Fixed.
           250899   Added error msg to file checks.
           280899   Converted to use locale. Some error messages, before
                    reading the locale, will still be in English.
           210500   Fixed bug where script would fail due to non-checking
                    of a blank match.

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

Procedure
---------

1. Check files exist. Open 'Teams.df'.
2. Read in teams and their associated data; WIN, DRAW, LOST etc.
3. 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. Format data in form of a league table to two arrays.
8. Sort array.
9. Display table and exit.

************************************************************************** */
PARSE ARG league_file

version = 1

hgfp = 1          /* Points to be added for each goal scored at Home */
hgap = 1.5        /* Points to be added for each goal scored against at Home */
hcsp = 2          /* Points to be added for each clean sheet at Home */
hpts = 1          /* Points to be added for points received at Home */
agfp = 1.5        /* Points to be added for each goal scored Away */
agap = 1          /* Points to be added for each goal scored against Away */
acsp = 3          /* Points to be added for each clean sheet Away */
apts = 1.5        /* Points to be added for points received Away */
switch = 0

league_file  = "Data/" || league_file
input2_file  = '.sf'
input3_file  = '.df'
title        = '*LEAGUE_NAME='
points_win   = '*POINTS_PER_WIN='
points_drw   = '*POINTS_PER_DRW='
points_lse   = '*POINTS_PER_LSE='
separator    = '*'
teams.       = '???'
teams2.      = '???'
h_gof.       = '???'
h_goa.       = '???'
h_pts.       = '???'
h_cls.       = '???'
h_gms.       = '???'
a_gof.       = '???'
a_goa.       = '???'
a_pts.       = '???'
a_cls.       = '???'
a_gms.       = '???'
t_num.       = '???'
ptswin       = 2
ptsdrw       = 1
ptslse       = 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 :    (PerformanceRating)"
   say
   say "Cannot read 'Data/Football.locale' for the locale settings."
   exit
end

locdir = locdir"User/PerformanceRating.data"

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

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 :    (PerformanceRating)"
   say
   say "Cannot find '"locdir"' to read locale settings."
   exit
end

if exists(league_file || input2_file) = 0 then do
   say
   say pr_error
   say
   say pr_one"'"league_file || input2_file"'."
   exit
end

if exists(league_file || input3_file) = 0 then do
   say
   say pr_error
   say
   say pr_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(points_win,line) > 0 then   ptswin=delstr(line,1,16)
      if pos(points_drw,line) > 0 then   ptsdrw=delstr(line,1,16)
      if pos(points_lse,line) > 0 then   ptslse=delstr(line,1,16)
      if pos(separator,line) = 0 & line ~= "" then do
         line = strip(line)
         tcount       = tcount + 1
         teams.tcount = line
      end
   end
   close(datafile)
end
else do
   say
   say pr_error
   say
   say pr_two"'"league_file || input3_file"'."
   exit
end

do i=1 to tcount
   h_gof.i = 0
   h_goa.i = 0
   h_pts.i = 0
   h_cls.i = 0
   h_gms.i = 0
   a_gof.i = 0
   a_goa.i = 0
   a_pts.i = 0
   a_cls.i = 0
   a_gms.i = 0
end

if open(datafile,league_file || input2_file,'r') then do
   do while ~eof(datafile)
      line = readln(datafile)
      if pos(separator,line) = 0 & line ~= "" 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 tcount
               if home_team = teams.i then do
                  h_gof.i = h_gof.i + goals_for
                  h_goa.i = h_goa.i + goals_aga
                  h_gms.i = h_gms.i + 1
                  if goals_aga = 0 then
                     h_cls.i = h_cls.i + 1
                  if goals_for = goals_aga then
                     h_pts.i = h_pts.i + ptsdrw
                  if goals_for < goals_aga then
                     h_pts.i = h_pts.i + ptslse
                  if goals_for > goals_aga then
                     h_pts.i = h_pts.i + ptswin
               end
            end
            do i=1 to tcount
               if away_team = teams.i then do
                  a_gof.i = a_gof.i + goals_aga
                  a_goa.i = a_goa.i + goals_for
                  a_gms.i = a_gms.i + 1
                  if goals_for = 0 then
                     a_cls.i = a_cls.i + 1
                  if goals_for = goals_aga then
                     a_pts.i = a_pts.i + ptsdrw
                  if goals_for < goals_aga then
                     a_pts.i = a_pts.i + ptswin
                  if goals_for > goals_aga then
                     a_pts.i = a_pts.i + ptslse
               end
            end
         end
      end
   end
   close(datafile)
end
else do
   say
   say pr_error
   say
   say pr_two"'"league_file || input2_file"'."
   exit
end

do i=1 to tcount
   hp1 = ((h_gof.i * hgfp) + (h_cls.i * hcsp) + (h_pts.i * hpts))
   hp2 =(h_goa.i * hgap)
   hp = hp1-hp2
   ap1 = ((a_gof.i * agfp) + (a_cls.i * acsp) + (a_pts.i * apts))
   ap2 = (a_goa.i * agap)
   ap = ap1-ap2
   if switch = 1 then do
      hp = trunc(hp / h_gms.i,3)
      ap = trunc(ap / a_gms.i,3)
   end
   t_num.i = trunc(hp + ap,3)
end

do i=1 to tcount
   line = teams.i
   line = insert(" ",line,length(line)+1,30-length(line))
   hg = right(h_gms.i,3)
   hf = right(h_gof.i,3)
   ha = right(h_goa.i,3)
   hc = right(h_cls.i,3)
   hp = right(h_pts.i,3)
   ag = right(a_gms.i,3)
   af = right(a_gof.i,3)
   aa = right(a_goa.i,3)
   ac = right(a_cls.i,3)
   ap = right(a_pts.i,3)
   teams.i = line"    "hg"  "hf"  "ha" "hc" "hp"   "ag"  "af"  "aa" "ac" "ap"      "right(t_num.i,7,' ')
   teams2.i= line"    "hg"  "hf"  "ha" "hc" "hp"   "ag"  "af"  "aa" "ac" "ap"      "right(t_num.i,7,' ')
end

ctr = 0
do while swapctr > 0
   swapctr=9
   do i=1 to tcount-1
      swapdone=0
      j = i + 1
      if strip(substr(teams.i,84,12)) < strip(substr(teams.j,84,12)) then do
         teams.i = teams2.j
         teams.j = teams2.i
         teams2.i= teams.i
         teams2.j= teams.j
         swapdone= 1
         ctr = ctr + 1
      end
      if swapdone = 1 then
         break
   end
   if ctr = 0 then
      swapctr = 0
   else
      ctr = 0
end

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

say
say center(pr_three"'"league_title"'",88)
say "-------------------------------------------------------------------------------------------------"
say
if switch = 1 then
   say pr_four
say
say pr_txt1
say
say pr_txt2
say "-------------------------------------------------------------------------------------------------"

do i=1 to tcount
   if teams.i ~= "" then do
      if i<10 then
         say " "i"  "teams.i
      else
         say i"  "teams.i
   end
end
say "-------------------------------------------------------------------------------------------------"
say

exit