/*************************************************************************

		What *I* think is the best trivia yet!

     --- Zen and the Art of Trivia ---
          by ZEN of Emephyt BBS!

 **************************************
 **                                  **
 ** entire executable taken over and **
 **    modified by Basil Barnes      **
 **     with permission of Zen       **
 **                                  **
 ** (sysop DEVIL BBS (403)-484-9200) **
 **                                  **
 **      1200 - 14400 @ 760 megs     **
 **                                  **
 **************************************

        LATEST VERSION AS OF MARCH 5, 1992

     
			Make a directory in Doors called Ztriv.
			Put the question files in it, together with the instruct file.
			All other files will be automatically generated by the program.
			See Zentriv.doc for instructions on how to add to the questions.
 
                                     Basil Barnes, Sysop Devil BBS
                                     403-484-9200
***************************************************************************/

arg Username ansi
makedir("Doors:ZTriv")
subdir = "Doors:ZTriv"           /* subdirectory for support files */
Hiscores = subdir||"/Hiscores"
totalusers = subdir||"/Totalusers"
filesnum = 5                   /* number of different question files */
right = 5                      /* points for correct answer */
wrong = 0                      /* points for wrong answer (negative is possible!) */
totalnum = 100 / right
cls = "[H[J"
signal on syntax

ansi = strip(ansi,B)
username = translate(username," ","_")
id = username

gfx = (ansi == "COLOR")

if (gfx) then do
red = '[31m'
green = '[32m'
yellow = '[33m'
cyan = '[36m'
redonyellow = '[31;43m'
cyanonyellow = '[36;43m'
blackonyellow = '[30;43m'
reset = '[0m'
end
else do
red = ''
green = ''
yellow = ''
cyan = ''
redonyellow = ''
cyanonyellow = ''
blackonyellow = ''
reset = ''
end

Begin1:
say cls
if ~exists(hiscores) then do
  if ~exists(totalusers) then do
	  call open(tot,totalusers,'W')
	  writeln(tot,1)
	  call close(tot)
	end
	call open(hi,hiscores,"W")
	call writeln(hi,username)
	call writeln(hi,0)
	call close(hi)
end
  call open(tot,totalusers,'R')
  highnum = readln(tot)
  call close(tot)
  call open(hi,hiscores,'R')
  do i = 1 to highnum
    hiuser.i = readln(hi)
    hipts.i = readln(hi)
    if i <= 15 then lowestscore = hipts.i
  end
  call close(hi)

Begin:
say cls
say "                  "redonyellow"                                        "reset
say "                  "redonyellow"========================================"reset
say "                  "redonyellow"  Zen and the Art of Trivia - Main Menu "reset
say "                  "redonyellow"  Written jointly by Zen & Basil Barnes "reset
say "                  "redonyellow"    Sysop Amiga Devil BBS (1:342/53)    "reset
say "                  "redonyellow"========================================"reset
say "                  "redonyellow"           ["blackonyellow"I"redonyellow"]nstructions               "reset
say "                  "redonyellow"           ["blackonyellow"H"redonyellow"]igh Scores                "reset
say "                  "redonyellow"           ["blackonyellow"T"redonyellow"]rivia Questions           "reset
say "                  "redonyellow"           ["blackonyellow"Q"redonyellow"]uit                       "reset
say "                  "redonyellow"                                        "reset
call writech(stdout,yellow"Option: "reset)
opt = readch(stdin,10) ; call check opt ; opt = left(upper(opt),1)

select
	when opt = "I" then do
		say cls
		say cyan
		address command "dlg:df "subdir"/instruct"
		call Pressreturn
		say reset
		signal Begin
	end
	when opt = "H" then do
		call showscores
		signal Begin
	end
	when opt = "T" then signal Zentriv
	when opt = "Q" then signal Finish
	otherwise signal Begin
end
signal Begin

Showscores:
    say cls
    say "                     "redonyellow"                              "reset
    say "                     "redonyellow"  User                  Score "reset
    say "                     "redonyellow" ---------------------------- "reset
    if highnum > 15 then maxnum = 15
    else maxnum = highnum
    do i = 1 to maxnum
      call writech(stdout,"                     "redonyellow" "left(hiuser.i,20))
      if hipts.i < 100 then call writech(stdout,"     ")
         else call writech(stdout,"    ")
      call writech(stdout,left(hipts.i,3))
      if hipts.i < 100 then say " "||reset
         else say "  "||reset
      end
    say " "
	call Pressreturn
return

Zentriv:
group = random(1,filesnum,time(S))
trivdat = "questions"||group
say ""
say ""
say green"Please wait ... loading trivia questions ..."reset

pragma('D',subdir)
if ~exists(trivdat) then do
	say ""
	say red"Question file is missing!"reset
	call delay(100)
	signal Finish
end

if exists(trivdat) then do
call open(trivia,trivdat,'R')
xtriv = 0
i = 1
do while ~eof(trivia)
questnum.i = 0
quest.i = readln(trivia)
do z = 1 to 4
 ans.z.i = readln(trivia)
end
i = i + 1
xtriv = xtriv + 1
end
xtriv = xtriv - 1
end
if xtriv = 0 then do
	say ""
	say red"There are no questions!"reset
	say red"*** Trivia data file is present, but there are no questions! ***"reset
	call delay(100)
	signal Finish
end

points = 0
say cls
say ""
say green"Remember you have to answer "totalnum" questions."reset

Trivloop:
counter = 1
say " "
do while counter <= totalnum                    /*  <----- */
 say yellow"Question #"counter||reset
 say ""
 do until questnum.index = 0
  index = random(1,xtriv,time('S'))
 end

/* now we have a question that hasn't been asked before, and we ensure
   that we don't ask it again by changing its index to 1
*/
 questnum.index = 1
 say cyan||quest.index||reset
 say ' '
 do i = 1 to 4
  if substr(ans.i.index,1,1) ~= "-" then say yellow||i")  "substr(ans.i.index,2)||reset 
  end
 say ""
 do forever
  say cyan
  call writech(stdout,"Answer [0 = Quit]? ")
  answer = readch(stdin,10) ; call check answer ; answer = left(answer,1)
  say reset
  if answer = 0 then signal trivloop1
  if ~datatype(answer,'W') | answer < 1 | answer > 4 | substr(ans.answer.index,1,1)="-" then iterate
  else break
  end
say ""
if substr(ans.answer.index,1,1) = "!" then do
  randnum = random(1,7,time('s'))
  say " "
select
  when randnum = 1 then say green"Good answer!  Correct."reset
  when randnum = 2 then say green"Right on..that is correct."reset
  when randnum = 3 then say green"That is correct!"reset
  when randnum = 4 then say green"Awesome!  That is correct!"reset
  when randnum = 5 then say green"Well done...that is correct!"reset
  when randnum = 6 then say green"Correct!"reset
  when randnum = 7 then say green"Perfect!  That is correct!"reset
  otherwise say green"That's it!  Correct!"reset
  end
  say " "
  points = points + right
  end
else do
  randnum = random(1,7,time('s'))
  say " "
select
  when randnum = 1 then say red"That's not it...incorrect!"reset
  when randnum = 2 then say red"No, sorry, you guessed wrong."reset
  when randnum = 3 then say red"Sorry, wrong answer."reset
  when randnum = 4 then say red"Sorry, that is incorrect"reset
  when randnum = 5 then say red"Nope, not that one...incorrect."reset
  when randnum = 6 then say red"BUZZZT...wrong."reset
  when randnum = 7 then say red"That is incorrect...sorry"reset
  otherwise say red"Incorrect!"reset
  end
  say " "
  points = points + wrong
  end
counter = counter + 1
end
call Scoring
do i = 1 to xtriv
questnum.i = 0
end
trivloop1:
call close(trivia)
signal Begin1

/************************************
 *       Subroutines here           *
 ************************************/
Scoring:
say " "
say yellow"You scored "points" points."reset
say " "

do i = 1 to highnum
  if id == hiuser.i then do
    if points > hipts.i then do
      say cyan"Congratulations!  You've improved your previous best score from "hipts.i||reset
      hipts.i = points
      call delay(100)
    end
    else do
      say cyan"Unfortunately, you did not improve on your previous best score of "hipts.i||reset
      call delay(100)
    end
    call sort
      call open(hi,hiscores,'W')
      do j = 1 to highnum
       writeln(hi,hiuser.j)
       writeln(hi,hipts.j)
      end
      call close(hi)
      call showscores
    return
    end
end
  highnum = highnum + 1
  hiuser.highnum = id
  hipts.highnum = points
  call sort
  open(tot,totalusers,'W')
  writeln(tot,highnum)
  close(tot)
  open(hi,hiscores,'W')
  do i = 1 to highnum
    writeln(hi,hiuser.i)
    writeln(hi,hipts.i)
  end
  close(hi)
  say ""
if points >= lowestscore then do
  say green"Congratulations!  Your score has bumped somebody off the list."reset
  call delay(100)
 end
 else do
  if highnum >= 15 then do
  say yellow"Sorry, but you did not make the high score list!"reset
  say yellow"Try again next time!"reset
  call delay(100)
  end
end
call showscores
return

Sort:
 do forever
   changes = 0
   do i = 1 to highnum - 1
    ihatearexx = i+1
    if hipts.ihatearexx > hipts.i then
     do
      temp = hipts.i
      tempname = hiuser.i
      hipts.i = hipts.ihatearexx          /* Bubblehead sort */
      hiuser.i = hiuser.ihatearexx
      hipts.ihatearexx = temp
      hiuser.ihatearexx = tempname
      changes = 1
     end
   end
   if changes = 0 then leave
 end
return

Pressreturn:
	junk = ""
	call writech(stdout,"Press [RETURN]")
	junk = readch(stdin,1)
return

Check: procedure
	parse arg isthisit
	if left(upper(isthisit),6) = "ENDCLI" then exit
return

Syntax:
code = rc
line = sigl
say " "
say "FATAL ERROR! ABORTING!"
say " "
call delay(120)
logentry "Fatal error in ZenTrivia"
shell 'COMMAND' "DATE >>ram:errlog"
open(err,'ram:errlog','A')
writeln(err,id" - ZenTrivia")
writeln(err,"Line "line" - Code "code)
writeln(err,errortext(code))
writeln(err,"")

Finish:
say ""
say ""
exit
