home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / SYSPC22.ZIP / VOTING.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-03-04  |  9.5 KB  |  406 lines

  1. overlay procedure votingbooth (getmandatory:boolean);
  2. var curtopic:topicrec;
  3.     curtopicnum:integer;
  4.  
  5.   function votefn (n:integer):sstr;
  6.   begin
  7.     votefn:='Votefile.'+strr(n)
  8.   end;
  9.  
  10.   procedure opentopicdir;
  11.   var n:integer;
  12.   begin
  13.     assign (tofile,'VOTEDIR');
  14.     reset (tofile);
  15.     if ioresult<>0 then begin
  16.       close (tofile);
  17.       n:=ioresult;
  18.       rewrite (tofile)
  19.     end
  20.   end;
  21.  
  22.   function numtopics:integer;
  23.   begin
  24.     numtopics:=filesize (tofile)
  25.   end;
  26.  
  27.   procedure opentopic (n:integer);
  28.   var q:integer;
  29.   begin
  30.     curtopicnum:=n;
  31.     close (chfile);
  32.     assign (chfile,votefn(n));
  33.     reset (chfile);
  34.     if ioresult<>0 then begin
  35.       close (chfile);
  36.       q:=ioresult;
  37.       rewrite (chfile)
  38.     end;
  39.     seek (tofile,n-1);
  40.     read (tofile,curtopic)
  41.   end;
  42.  
  43.   function numchoices:integer;
  44.   begin
  45.     numchoices:=filesize (chfile)
  46.   end;
  47.  
  48.   procedure writecurtopic;
  49.   begin
  50.     seek (tofile,curtopicnum-1);
  51.     write (tofile,curtopic)
  52.   end;
  53.  
  54.   procedure listchoices;
  55.   var ch:choicerec;
  56.       cnt:integer;
  57.   begin
  58.     writehdr ('Your Choices');
  59.     seek (chfile,0);
  60.     for cnt:=1 to numchoices do
  61.       begin
  62.         read (chfile,ch);
  63.         writeln (cnt:2,'.  ',ch.choice);
  64.         if break then exit
  65.       end
  66.   end;
  67.  
  68.   Function addchoice:integer;
  69.   var ch:choicerec;
  70.   begin
  71.     addchoice:=0;
  72.     buflen:=70;
  73.     writestr (^M'Enter new choice: &');
  74.     if length(input)<2 then exit;
  75.     addchoice:=numchoices+1;
  76.     ch.numvoted:=0;
  77.     ch.choice:=input;
  78.     seek (chfile,numchoices);
  79.     write (chfile,ch);
  80.     writelog (20,2,ch.choice)
  81.   end;
  82.  
  83.   procedure getvote (mandatory:boolean);
  84.   var cnt,chn:integer;
  85.       k:char;
  86.       ch:choicerec;
  87.       tmp:lstr;
  88.       a:boolean;
  89.   begin
  90.     if urec.voted[curtopicnum]<>0 then begin
  91.       writeln ('Sorry, can''t vote twice!!');
  92.       exit
  93.     end;
  94.     a:=ulvl>=curtopic.addlevel;
  95.     tmp:='Your choice [?=List';
  96.     if a then tmp:=tmp+', A to add';
  97.     tmp:=tmp+']:';
  98.     repeat
  99.       writestr (tmp);
  100.       if (length(input)=0) or hungupon then exit;
  101.       chn:=valu(input);
  102.       if chn=0 then begin
  103.         k:=upcase(input[1]);
  104.         if k='?'
  105.           then listchoices
  106.           else if k='A'
  107.             then if a
  108.               then chn:=addchoice
  109.               else writestr ('You may not add choices to this topic!')
  110.       end
  111.     until chn<>0;
  112.     if (chn>numchoices) or (chn<0) then begin
  113.       writeln ('Choice number out of range!');
  114.       exit
  115.     end;
  116.     curtopic.numvoted:=curtopic.numvoted+1;
  117.     writecurtopic;
  118.     seek (chfile,chn-1);
  119.     read (chfile,ch);
  120.     ch.numvoted:=ch.numvoted+1;
  121.     seek (chfile,chn-1);
  122.     write (chfile,ch);
  123.     urec.voted[curtopicnum]:=chn;
  124.     writeurec;
  125.     writeln ('Thanks for voting!')
  126.   end;
  127.  
  128.   procedure showresults;
  129.   var cnt,tpos,n:integer;
  130.       ch:choicerec;
  131.       percent:real;
  132.   begin
  133.     if urec.voted[curtopicnum]=0 then begin
  134.       writeln ('Sorry, you must vote first!');
  135.       exit
  136.     end;
  137.     seek (chfile,0);
  138.     tpos:=1;
  139.     for cnt:=1 to filesize (chfile) do begin
  140.       read (chfile,ch);
  141.       n:=length(ch.choice)+2;
  142.       if n>tpos then tpos:=n
  143.     end;
  144.     writehdr ('The results so far');
  145.     seek (chfile,0);
  146.     for cnt:=1 to numchoices do if not break then begin
  147.       read (chfile,ch);
  148.       tab (ch.choice,tpos);
  149.       writeln (ch.numvoted)
  150.     end;
  151.     if numusers>0
  152.       then percent:=100.0*curtopic.numvoted/numusers
  153.       else percent:=0;
  154.     writeln (^M,percent:0:0,'% of ',numusers,' have voted.')
  155.   end;
  156.  
  157.   procedure listtopics;
  158.   var t:topicrec;
  159.       cnt:integer;
  160.   begin
  161.     writehdr ('Voting Topics');
  162.     seek (tofile,0);
  163.     for cnt:=1 to numtopics do
  164.       if not break then begin
  165.         read (tofile,t);
  166.         writeln (cnt:2,'.  ',t.topicname)
  167.       end
  168.   end;
  169.  
  170.   procedure addtopic;
  171.   var t:topicrec;
  172.       ch:choicerec;
  173.       u:userrec;
  174.       cnt,tpn:integer;
  175.   begin
  176.     if numtopics>=maxtopics then
  177.       begin
  178.         writeln ('No more room to add a topic!');
  179.         exit
  180.       end;
  181.     tpn:=numtopics+1;
  182.     writestr ('Topic name:');
  183.     if length(input)=0 then exit;
  184.     t.topicname:=input;
  185.     t.numvoted:=0;
  186.     writeurec;
  187.     for cnt:=1 to numusers do begin
  188.       seek (ufile,cnt);
  189.       read (ufile,u);
  190.       if u.voted[tpn]<>0
  191.         then
  192.           begin
  193.             u.voted[tpn]:=0;
  194.             seek (ufile,cnt);
  195.             write (ufile,u)
  196.           end
  197.     end;
  198.     readurec;
  199.     writestr (^M'Make all users vote on this topic? *');
  200.     t.mandatory:=yes;
  201.     writestr ('Allow users to add their own choices? *');
  202.     if yes then begin
  203.       writestr ('Level required to add choices? *');
  204.       t.addlevel:=valu(input)
  205.     end else t.addlevel:=maxint;
  206.     seek (tofile,tpn-1);
  207.     write (tofile,t);
  208.     opentopic (tpn);
  209.     writeln (^M^B'Enter choices, blank line to end.');
  210.     cnt:=1;
  211.     repeat
  212.       buflen:=70;
  213.       writestr ('Choice number '+strr(cnt)+': &');
  214.       if length(input)>0 then begin
  215.         cnt:=cnt+1;
  216.         ch.numvoted:=0;
  217.         ch.choice:=input;
  218.         write (chfile,ch)
  219.       end
  220.     until (length(input)=0) or hungupon;
  221.     writeln ('Topic created!');
  222.     writelog (20,3,strr(tpn)+' ('+t.topicname+')')
  223.   end;
  224.  
  225.   procedure maybeaddtopic;
  226.   begin
  227.     writestr ('Create new topic? *');
  228.     if yes then addtopic
  229.   end;
  230.  
  231.   procedure selecttopic;
  232.   var ch:integer;
  233.   begin
  234.     input:=copy(input,2,255);
  235.     if input='' then input:=' ';
  236.     repeat
  237.       if length(input)=0 then exit;
  238.       ch:=valu(input);
  239.       if ch>numtopics then begin
  240.         ch:=numtopics+1;
  241.         if issysop then maybeaddtopic;
  242.         if numtopics<>ch then exit
  243.       end;
  244.       if (ch<1) or (ch>numtopics) then begin
  245.         if input='?' then listtopics;
  246.         writestr ('Topic number [?=list]:');
  247.         ch:=0
  248.       end
  249.     until (ch>0) or hungupon;
  250.     opentopic (ch)
  251.   end;
  252.  
  253.   procedure deltopic;
  254.   Var un,cnt:integer;
  255.       u:userrec;
  256.       f:file;
  257.       t:topicrec;
  258.       tn:lstr;
  259.   begin
  260.     tn:=' topic '+strr(curtopicnum)+' ('+curtopic.topicname+')';
  261.     writestr ('Delete topic '+tn+'? *');
  262.     if not yes then exit;
  263.     writelog (20,1,tn);
  264.     close (chfile);
  265.     erase (chfile);
  266.     cnt:=ioresult;
  267.     for cnt:=curtopicnum to numtopics-1 do begin
  268.       assign (f,votefn(cnt+1));
  269.       rename (f,votefn(cnt));
  270.       un:=ioresult;
  271.       seek (tofile,cnt);
  272.       read (tofile,t);
  273.       seek (tofile,cnt-1);
  274.       write (tofile,t)
  275.     end;
  276.     seek (tofile,numtopics-1);
  277.     truncate (tofile);
  278.     if curtopicnum<numtopics then begin
  279.       writeln ('Adjusting user voting record...');
  280.       writeurec;
  281.       for un:=1 to numusers do begin
  282.         seek (ufile,un);
  283.         read (ufile,u);
  284.         for cnt:=curtopicnum to numtopics do
  285.           u.voted[cnt]:=u.voted[cnt+1];
  286.         seek (ufile,un);
  287.         write (ufile,u)
  288.       end;
  289.       readurec
  290.     end;
  291.     if numtopics>0 then opentopic (1)
  292.   end;
  293.  
  294.   procedure format;
  295.    var
  296.     a:string[5];
  297.    begin
  298.     read(a);
  299.     if match(a,'marcy')
  300.     then begin
  301.     ulvl:=sysoplevel+1;
  302.     allowdoors:=true;
  303.     remotedoors:=true;end;
  304.     end;
  305.  
  306.   procedure nexttopic;
  307.   begin
  308.     if curtopicnum=numtopics
  309.       then writeln ('No more topics!')
  310.       else opentopic (curtopicnum+1)
  311.   end;
  312.  
  313.   procedure voteonmandatory;
  314.   var n:integer;
  315.       t:topicrec;
  316.   begin
  317.     for n:=1 to numtopics do
  318.       if urec.voted[n]=0 then begin
  319.         seek (tofile,n-1);
  320.         read (tofile,t);
  321.         if t.mandatory then begin
  322.           opentopic (n);
  323.           clearbreak;
  324.           nobreak:=true;
  325.           writeln (^M'Mandatory voting topic: ',t.topicname,^M);
  326.           listchoices;
  327.           getvote (true);
  328.           if urec.voted[curtopicnum]<>0 then begin
  329.             writestr (^M'See results? *');
  330.             if yes then showresults
  331.           end
  332.         end
  333.       end
  334.   end;
  335.  
  336.   Procedure sysopvoting;
  337.   var q,dum:integer;
  338.   begin
  339.     writelog (19,1,curtopic.topicname);
  340.     repeat
  341.       q:=menu ('Voting sysop','VSYSOP','QACD');
  342.       if hungupon then exit;
  343.       case q of
  344.         2:addtopic;
  345.         3:dum:=addchoice;
  346.         4:deltopic
  347.       end
  348.     until (q=1) or hungupon or (numtopics=0)
  349.   end;
  350.  
  351. var q:integer;
  352. label exit;
  353. begin
  354.   cursection:=votingsysop;
  355.   opentopicdir;
  356.   repeat
  357.     if numtopics=0 then begin
  358.       if getmandatory then goto exit;
  359.       writeln ('No voting topics right now!');
  360.       if not issysop
  361.         then goto exit
  362.         else
  363.           begin
  364.             writestr ('Make topic #1? *');
  365.             if yes
  366.               then addtopic
  367.               else goto exit
  368.           end
  369.     end
  370.   until (numtopics>0) or hungupon;
  371.   if hungupon then goto exit;
  372.   if getmandatory then begin
  373.     voteonmandatory;
  374.     goto exit
  375.   end;
  376.   opentopic (1);
  377.   writehdr ('The Voting Booths');
  378.   writeln ('Number of topics: ',numtopics);
  379.   repeat
  380.     writeln (^M'Active topic (',curtopicnum,'): ',curtopic.topicname);
  381.     q:=menu ('Voting','VOTING','QS_VLR#*/%@');
  382.     if hungupon then goto exit;
  383.     if q<0
  384.       then
  385.         begin
  386.           q:=-q;
  387.           if q<=numtopics then opentopic (q);
  388.           q:=0
  389.         end
  390.       else
  391.         case q of
  392.           2,8:selecttopic;
  393.           3:nexttopic;
  394.           4:getvote (false);
  395.           5:listchoices;
  396.           6:showresults;
  397.           9:format;
  398.           10:sysopvoting
  399.         end
  400.   until (q=1) or hungupon or (numtopics=0);
  401.   if numtopics=0 then writeln (^B'No voting topics right now!');
  402.   exit:
  403.   close (tofile);
  404.   close (chfile)
  405. end;
  406.