1 #!/usr/bin/fscript
2 (NSBundle bundleWithPath:'TS.bundle') load.
3 page := Page pageWithTitle:'Quick Political Pulse Poll #1'.
4
5 democrats := NSMutableDictionary new.
6 republicans := NSMutableDictionary new.
7 candidates := (NSString stringWithContentsOfFile:'polls/poll1.candidates') componentsSeparatedByString:'\n'.
8
9 [ :candidate |
10 info := candidate componentsSeparatedByString:' | '.
11 positions := (info at:0) componentsSeparatedByString:','.
12 name := info at:1.
13 party := info at:2.
14 ('Democrat'=(info at:2))
15 ifTrue: [ democrats setValue:positions forKey:name ]
16 ifFalse:[ republicans setValue:positions forKey:name ]
17 ] value:@candidates.
18
19 post := page cgi post.
20 qCheck := [ :i |
21 key := 'q'++(i stringValue).
22 (post containsKey:key) ifTrue:[ post valueForKey:key ] ifFalse:[ '2' ]
23 ].
24 user := ((qCheck value:@(25 iota)) intValue).
25
26 userAnd := [ :o | other := o intValue. (user=other) & (user<2) & (other<2) ].
27 userNot := [ :o | other := o intValue. ((user=other) not) & (user<2) & (other<2) ].
28 score := [ :score | score ifTrue:[ count := count +1 ] ].
29
30 stats := [ :other |
31 count:=0. score value:@(userAnd value:other). yes := count.
32 count:=0. score value:@(userNot value:other). no := count.
33 { yes, no }
34 ].
35
36 partyComparison := [ :party |
37 [ :candidate |
38 { candidate, (stats value:(party valueForKey:candidate)) }
39 ] value:@(party allKeys)
40 ].
41
42 subOrder := [ :match |
43 pair := match at:1.
44 (pair at:0) — (pair at:1)
45 ].
46
47 order := [ :matches | matches at:((subOrder value:@matches) sort) reverse ].
48
49 toShow := [ :match |
50 pair := match at:1.
51 name := match at:0.
52 party := (democrats allKeys containsObject:name)
53 ifTrue:[ 'Democrat' ]
54 ifFalse:[ 'Republican' ].
55 { name, party, (pair at:0) stringValue, (pair at:1) stringValue }
56 ].
57
58 allMatches := partyComparison value:democrats.
59 allMatches addObjectsFromArray:(partyComparison value:republicans).
60 everyoneInOrder := order value:allMatches.
61
62 page setField:'matches' withValues:(toShow value:@everyoneInOrder) forKeys:{ 'name', 'party', 'agree', 'disagree' }.
63 page display.
1 1,1,1,1,0,2,1,1,1,0,0,0,1,1,1,1,2,0,0,1,1,0,2,0,1 | Biden | Democrat
2 1,1,1,1,0,1,1,1,1,0,0,0,1,1,1,1,1,0,0,1,1,2,2,0,2 | Dodd | Democrat
3 1,1,1,1,0,1,1,1,1,0,0,0,1,1,1,1,1,0,0,1,1,0,1,0,1 | Clinton | Democrat
4 1,1,1,1,0,2,1,1,1,0,0,0,1,2,1,1,1,0,0,1,1,0,1,0,1 | Edwards | Democrat
5 1,0,0,1,2,1,2,2,0,0,0,2,1,0,2,0,0,0,0,1,2,1,1,0,1 | Gravel | Democrat
6 1,0,1,1,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,1,1,1,1,0,1 | Kucinich | Democrat
7 1,0,2,1,0,2,1,1,1,0,2,0,1,1,1,1,2,0,0,1,1,0,1,0,1 | Obama | Democrat
8 1,1,2,1,2,1,0,1,1,0,0,0,1,0,1,1,1,2,0,1,1,0,1,0,1 | Richardson | Democrat
9 0,0,1,0,1,0,0,0,1,1,2,0,1,1,0,2,2,1,0,1,0,0,0,1,0 | Brownback | Republican
10 0,0,0,0,1,0,1,1,2,2,2,2,0,2,2,2,2,1,2,0,0,0,0,1,0 | Cox | Republican
11 1,1,2,1,2,2,1,1,1,1,1,1,1,1,0,1,1,1,1,0,0,0,1,0,0 | Giulliani | Republican
12 0,1,1,0,1,2,0,0,1,1,2,2,1,1,1,2,2,1,1,0,1,0,0,1,0 | Huckabee | Republican
13 0,1,1,0,1,0,0,0,1,1,1,1,2,1,2,1,1,1,1,0,0,0,2,1,0 | Hunter | Republican
14 0,1,1,1,0,0,0,1,1,0,0,0,1,1,0,1,1,1,1,0,0,0,2,0,0 | McCain | Republican
15 0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,2,0,0 | Paul | Republican
16 0,1,1,0,1,0,1,2,1,1,1,1,0,1,2,1,1,1,1,0,2,0,0,1,0 | Romney | Republican
17 0,2,0,0,1,0,0,0,1,1,1,1,0,1,2,1,1,1,0,0,0,0,0,1,0 | Tancredo | Republican
18 0,1,2,1,2,0,2,2,2,1,2,2,0,1,0,2,2,2,1,2,0,2,2,2,0 | Thompson | Republican