/*************************************************************************
  Empire v2.00 By Crazy Cad  Copyright (C) 1990,1991,1992 Updated 6-7-92 
    * *  Do Not Modify This Program Without Consent of Crazy Cad  * *
  Emp.UpEmp (Updates Emperor Hall of Fame) Version 2.00   RELEASE Version
 *************************************************************************
 
 See Empire documentation for additional program information.

 To contact me call at the Amiga Castle BBS (214) 219-0493
 or write to me at the following address:
 
 Chris A.D.
 Post Box 294331
 Lewisville, TX 75029-4331

 This program is authorized to be ran on C-Net Amiga BBS Systems Only.
 
 ************************************************************************/

options results
signal on error
signal on syntax
signal on ioerr
signal on halt 
errors=0

transmit 'f1n1c5 Empire Emperor Hall Of Fame Generation by c9Crazy Cadn1q1'


call ReadConfig      	/* Read Emp.Config.  (Get Path to Emp.Emperors) */
call ReadEmperors    	/* Read Emp.Emperors into memory */

call SortEmp		/* Sort EmpName.X and EmpTime.X */
call SaveEmperors       /* Save Sorted Emp.Emperors file */

NumEmp=min(NumEmp,15)   /* Only concerned with Top 15 or # we have */

call MakePretty		/* Determine Max length of names, max size to make Honor Roll pretty */
call MakeFile	        /* Make normal size Emperor Honor Roll */ 

DONE:
transmit 'n1c5 Empire Emperor Hall Of Fame Generation Completedn1q1'
EXIT


READCONFIG:
 /* PURPOSE: 		Read Emp.Config variables		*/
 /* PRECONDITIONS:     	Emp.Config exists.  			*/
 /* POSTCONDITIONS:    	Emp.Config vars (Paths) are in memory	*/
 filen='PFILES:Emp.Config'
 if ~exists(filen) then do
  transmit 'Emp.Config missing.'
  transmit 'Program Aborted.'
  signal done
 end
 transmit 'c7- c6Reading Emp.Config'
 call open(config,filen,'R')
 do until eof(config)=1
  interpret readln(config)
 end
 call close config
return /* READCONFIG */


READEMPERORS:
 /* PURPOSE: 		Read in Emperors file  			*/
 /* PRECONDITIONS:     	Emp.Emperors exists.  Emp.Config in Mem	*/
 /* POSTCONDITIONS:    	NumEmp=# of Emperors. 			*/
 /*          		EmpName.X is Name of Emperors 		*/
 /*          		EmpTime.X is Times crowned Emperor 	*/
 filename=path.2||'Emp.Emperors'
 if ~exists(filename) then do
  transmit 'c7- c6Emp.Emperors File Does NOT Exist (Yet)'
  transmit 'c7- c6Checking for Emp.Emperor file.'
  if ~exists(path.2||'Emp.Emperor') then do
   transmit 'c7- c6Emp.Emperor File Not Found. (No Emperors Yet)'
   transmit 'c7- c6Program Aborted.'
   signal done
  end
  else do
   call CREATEEMP
   RETURN /* Emp.Emperors Created and READ */
  end
 end
 transmit 'c7- c6Reading Emp.Emperors'
 call open(empfile,filename,'R')
 NumEmp=0
 do until eof(empfile)
  NumEmp=NumEmp+1
  EmpName.NumEmp=readln(empfile)
  EmpTime.NumEmp=readln(empfile)
  if length(EmpName.NumEmp)<3 then NumEmp=NumEmp-1
 end
 call close empfile
 if NumEmp=0 then do
  address command 'DELETE "'filename'"'
  transmit 'c7- c6Emp.Emperors file is EMPTY.  Deleting Emp.Emperors.'
  signal READEMPERORS
 end
RETURN /* READEMPERORS */


SORTEMP: 
 /* PURPOSE: 		Sort EmpName.X and EmpTime.X		*/
 /* PRECONDITIONS:     	EmpName.X, EmpTime.X, NumEmp defined.	*/
 /* POSTCONDITIONS:    	EmpName.X and EmpTime.X sorted by times	*/
 /*          		EmpNum is unchanged	 		*/
 transmit 'c7- c6Sorting'
 do passcount=1 to NumEmp-1
  minindex=passcount
  do placecount=passcount+1 to numEmp
   if EmpTime.placecount > EmpTime.minindex then minindex=placecount
  end
  temp=EmpTime.minindex
  temp2=EmpName.minindex
  EmpTime.minindex=EmpTime.passcount
  EmpName.minindex=EmpName.passcount
  EmpTime.passcount=temp
  EmpName.passcount=temp2
 end
return /* SORTEMP */

SAVEEMPERORS:
 /* PURPOSE: 		Save (sorted) Emperors file		*/
 /* PRECONDITIONS:     	EmpName.X, EmpTime.X, NumEmp in mem	*/
 /* POSTCONDITIONS:    	(sorted) Emp.Emperors file written	*/
 filename=path.2||'Emp.Emperors'
 transmit 'c7- c6Saving Emp.Emperors'
 call open(empfile,filename,'W')
 do z=1 to NumEmp
  call writeln(empfile,EmpName.Z)
  call writeln(empfile,EmpTime.Z)
 end
 call close empfile
RETURN /* SAVEEMPERORS */


MAKEPRETTY:
 /* PURPOSE: 		Look at Max EmpName and EmpTime		*/
 /* PRECONDITIONS:     	EmpName.X, EmpTime.X, NumEmp in mem	*/
 /* POSTCONDITIONS:    	MU=Multiplier MAXNAME=len of Max name	*/
 sendstring 'c7- c6Thinking'
 MAXNAME=5
 do z=1 to NumEmp
  sendstring '.'
  MAXNAME=MAX(MAXNAME,length(EmpName.Z))
 end
 MU=1
 TRYSMALL=0
 TRYBIG=0
 TRYING:
 sendstring '.'
 if TRYBIG=0 | TRYSMALL=0 then do
  if (EmpTime.1)*MU > (60-maxname-length(EmpTime.1)) then do
   TRYSMALL=1
   MU=mu-1
   signal TRYING
  end 
  if (EmpTime.1)*MU < (60-maxname-length(EmpTime.1)) then do
   TRYBIG=1
   MU=mu+1
   signal TRYING
  end
 end
 transmit ' '
 call close empfile
RETURN /* MAKEPRETTY */



MAKEFILE:
 /* PURPOSE: 		Make/Save Normal sized honor file	*/
 /* PRECONDITIONS:     	EmpName.X, EmpTime.X, NumEmp, in mem	*/
 /*                     MU=Multiplier, MaxName=Max Name Length  */
 /* POSTCONDITIONS:    	Emp.TopEmp generated 			*/
 file1=path.2||'Emp.TopEmp'
 file2=path.2||'Emp.TopEmp.tt4'
 transmit 'c7- c6Writing Emperor Hall Of Fame'

 call open(empfile1,file1,'W')
 call open(empfile2,file2,'W')
 
 call writeln(empfile1,'f1n1                       c6z4  Emperor Hall Of Fame  q1n1')
 call writeln(empfile2,'f1n1[10;19!SAPPHIRE.font!n1cc             Emperor Hall Of Fame[10;0!q1n1')
 
 do z=1 to NumEmp
  mes='  c7'||right(z,2)||'c5.c2 '||left(EmpName.z,MAXNAME+2)||'c3'copies('*',max(1,(MU*EmpTime.Z)))||' c6('EmpTime.Z')'
  call writeln(empfile1,mes)
  call writeln(empfile2,mes)
 end

 call writeln(empfile1,'n1')
 call writeln(empfile2,'n1')

 call close empfile1
 call close empfile2

RETURN /* MAKEFILE */

CREATEEMP:
 /* PURPOSE: 		Create Emp.Emperors from Emp.Emperor 	*/
 /* PRECONDITIONS:     	Emp.Emperor exists.  Emp.Config in Mem	*/
 /* POSTCONDITIONS:    	Emp.Emperor file created		*/
 filename=path.2||'Emp.Emperor'
 transmit 'c7- c6Reading Emp.Emperor'
 call open(empfile,filename,'R')
 NumEmp=0
 do until eof(empfile)
  InEmp=readln(empfile)
  z=words(InEmp)
  if z>2 then do
   InEmp=delword(InEmp,z-2,3)   /* Get rid of on Month xx */
   found=0
   do zz=1 to NumEmp
    if InEmp=EmpName.zz then do /* Emperor is in list, Inc Times */
     found=1
     EmpTime.zz=(EmpTime.zz)+1
    end
   end
   if found=0 then do           /* Emperor is not in the list, Add */
    NumEmp=NumEmp+1
    EmpName.NumEmp=InEmp
    EmpTime.NumEmp=1
   end
  end
 end
 call close empfile
 filename=path.2||'Emp.Emperors'
 transmit 'c7- c6Writing Emp.Emperors'
 call open(empfile,filename,'W')
 do z=1 to NumEmp
  call writeln(empfile,EmpName.Z)
  call writeln(empfile,EmpTime.Z)
 end
 call close empfile
RETURN

ERROR:
type='Error';x=sigl;code=rc
signal ERRORLOG
SYNTAX:
type='Syntax Error';x=sigl;code=rc
signal ERRORLOG
IOERR:
type='I/O Error';code=rc;x=sigl
signal ERRORLOG
ERRORLOG:
if errors>1 then exit
errors=errors+1
fn='Error'
call clock
mes='c7'date time 'c6'name' Emp.UpEmp 'type' in line:' x
call logit(0,fn)
logentry mes
mes='c2'sourceline(x) LF ' Error Code='code' ('errortext(code)')'
call logit(0,fn)
transmit 'c7Oops, c6It seems we got a 'type' in line:' x 
exit
HALT:
transmit 'Emp.UpEmp HALTED.'
exit

CLOCK:
time=right(' '||time('c'),7)
date=left(date(M),3)||' '||left(date(E),2)
return

LOGIT:
arg p,fn
filen=path.p||fn
call clock
if exists(filen)=0 then call open(logi,filen,'W')
  else call open(logi,filen,'A')
call writeln(logi,mes)
call close logi
return 