del: Enable the previously dead code
[cacert-votebot.git] / src / org / cacert / votebot / CAcertVoteMechanics.java
1 package org.cacert.votebot;
2
3 import java.util.HashMap;
4 import java.util.Map.Entry;
5
6 /**
7 * Reprenents the voting-automata for voting in IRC chanenls.
8 */
9 public class CAcertVoteMechanics {
10
11 public static enum VoteType {
12 AYE, NAYE, ABSTAIN
13 }
14
15 public static enum State {
16 RUNNING, IDLE
17 }
18
19 State state = State.IDLE;
20
21 String voteCaller;
22
23 String topic;
24
25 private String vote(String voter, String actor, VoteType type) {
26 votes.put(voter, type);
27
28 if (voter.equals(actor)) {
29 return "Thanks " + actor + " I count your vote as " + type;
30 } else {
31 return "Thanks " + actor + " I count your vote for " + voter + " as " + type;
32 }
33 }
34
35 HashMap<String, VoteType> votes = new HashMap<>();
36
37 private String voteError(String actor) {
38 return "Sorry " + actor + ", I did not understand your vote, your current vote state remains unchanged!";
39 }
40
41 /**
42 * Adds a vote to the current topic. This interprets proxies.
43 *
44 * @param actor
45 * the person that sent this vote
46 * @param txt
47 * the text that the person sent.
48 * @return A message to
49 *
50 * <pre>
51 * actor
52 * </pre>
53 *
54 * indicating tha result of his action.
55 */
56 public synchronized String evaluateVote(String actor, String txt) {
57 if (state != State.RUNNING) {
58 return "Sorry " + actor + ", but currently no vote is running.";
59 }
60
61 String voter = actor;
62 String value = null;
63
64 if (txt.toLowerCase().matches("^\\s*proxy\\s.*")) {
65 String[] parts = txt.split("\\s+");
66 if (parts.length == 3) {
67 voter = parts[1];
68 value = parts[2];
69 }
70 } else {
71 value = txt.replaceAll("^\\s*|\\s*$", "");
72 }
73
74 if (value == null) {
75 return voteError(actor);
76 } else {
77 value = value.toLowerCase();
78
79 switch (value) {
80 case "aye":
81 case "yes":
82 case "oui":
83 case "ja": {
84 return vote(voter, actor, VoteType.AYE);
85 }
86 case "naye":
87 case "nay":
88 case "no":
89 case "non":
90 case "nein": {
91 return vote(voter, actor, VoteType.NAYE);
92 }
93 case "abstain":
94 case "enthaltung":
95 case "enthalten":
96 case "abs": {
97 return vote(voter, actor, VoteType.ABSTAIN);
98 }
99 }
100 }
101
102 return voteError(actor);
103 }
104
105 /**
106 * A new vote begins.
107 *
108 * @param from
109 * the nick that called the vote
110 * @param topic
111 * the topic of the vote
112 * @return A response to
113 *
114 * <pre>
115 * from
116 * </pre>
117 *
118 * indicating success or failure.
119 */
120 public synchronized String callVote(String from, String topic) {
121 if (state != State.IDLE) {
122 return "Sorry, a vote is already running";
123 }
124
125 voteCaller = from;
126 this.topic = topic;
127 votes.clear();
128
129 state = State.RUNNING;
130
131 return "Vote started.";
132 }
133
134 /**
135 * Ends a vote.
136 *
137 * @return An array of Strings containing result status messages.
138 */
139 public synchronized String[] closeVote() {
140 int[] res = new int[VoteType.values().length];
141
142 for (Entry<String, VoteType> i : votes.entrySet()) {
143 res[i.getValue().ordinal()]++;
144 }
145
146 String[] results = new String[VoteType.values().length];
147
148 for (int i = 0; i < res.length; i++) {
149 results[i] = (VoteType.values()[i] + ": " + res[i]);
150 }
151
152 votes.clear();
153 voteCaller = null;
154 state = State.IDLE;
155
156 return results;
157 }
158
159 public String getTopic() {
160 return topic;
161 }
162
163 public State getState() {
164 return state;
165 }
166
167 public String getCurrentResult() {
168 return votes.toString();
169 }
170
171 }