home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 189.img / TCS120S.ZIP / VOTING.PAS < prev    next >
Pascal/Delphi Source File  |  1989-03-05  |  11KB  |  455 lines

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