-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFreeClicker.java
152 lines (124 loc) · 5.15 KB
/
FreeClicker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* Created by Vishnu on 11/20/2016.
*/
import org.json.simple.JSONArray;
import org.json.simple.parser.ParseException;
import org.json.simple.parser.JSONParser;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Random;
import static spark.Spark.*;
public class FreeClicker {
private static ArrayList<String> users = new ArrayList<>();
private static ArrayList<String> options = new ArrayList<>();
private static int[] results = new int[5];
private static ViewBar vb = new ViewBar();
private static int numVotes = 0;
private static String chartPage = "";
private static void updateLists(String user, String option) {
System.out.print("\nUsers in list: ");
for(int i = 0;i<users.size();i++)
System.out.print(users.get(i)+" ");
System.out.println();
if(users==null || users.size()==0) {
users.add(user);
options.add(option);
numVotes++;
vb.numVotesLabel.setText(""+numVotes);
if(option.equalsIgnoreCase("A")) results[0]++;
else if(option.equalsIgnoreCase("B")) results[1]++;
else if(option.equalsIgnoreCase("C")) results[2]++;
else if(option.equalsIgnoreCase("D")) results[3]++;
else if(option.equalsIgnoreCase("E")) results[4]++;
}
else {
for(int i = 0;i<users.size();i++) {
if(users.get(i).equals(user)) {
String coption = options.get(i);
System.out.println("Current Option: "+coption+" Current user: "+users.get(i));
options.set(i, option);
if(option.equalsIgnoreCase("A")) results[0]++;
else if(option.equalsIgnoreCase("B")) results[1]++;
else if(option.equalsIgnoreCase("C")) results[2]++;
else if(option.equalsIgnoreCase("D")) results[3]++;
else if(option.equalsIgnoreCase("E")) results[4]++;
if(coption.equalsIgnoreCase("A")) results[0]--;
else if(coption.equalsIgnoreCase("B")) results[1]--;
else if(coption.equalsIgnoreCase("C")) results[2]--;
else if(coption.equalsIgnoreCase("D")) results[3]--;
else if(coption.equalsIgnoreCase("E")) results[4]--;
return;
}
}
users.add(user);
options.add(option);
numVotes++;
vb.numVotesLabel.setText(""+numVotes);
if(option.equalsIgnoreCase("A")) results[0]++;
else if(option.equalsIgnoreCase("B")) results[1]++;
else if(option.equalsIgnoreCase("C")) results[2]++;
else if(option.equalsIgnoreCase("D")) results[3]++;
else if(option.equalsIgnoreCase("E")) results[4]++;
}
}
private static void newQuestion() {
results[0] = results[1] = results[2] = results[3] = results[4] = 0;
users = new ArrayList<>();
options = new ArrayList<>();
vb.numVotesLabel.setText("0");
numVotes = 0;
vb.timeLabel.setText("0:00");
}
public static void main(String ar[]) throws IOException {
newQuestion();
JSONParser parser = new JSONParser();
Random r = new Random();
int port = 1000 + r.nextInt(64535);
String roomName = "CS 182";
setIpAddress("0.0.0.0");
setPort(port);
try {
FileReader fr = new FileReader("chart.html");
BufferedReader br = new BufferedReader(fr);
String S;
while((S=br.readLine())!=null) {
if(S.indexOf("%%IPPORT%%")!=-1)
S = S.replace("%%IPPORT%%", "localhost:"+port);
chartPage += S+"\n";
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
DatagramSocket ds = new DatagramSocket();
ds.connect(InetAddress.getByName("8.8.8.8"), 10002);
String json = String.format("{ \"room\" : \"%s\" , \"ip\" : \"%s\" , \"port\" : %d }", roomName, ds.getLocalAddress().getHostAddress(), port);
QRScreen qr = new QRScreen(json);
} catch (Exception e) {
e.printStackTrace();
}
get("/results", (req, res) -> {
String S = String.format("[%d,%d,%d,%d,%d]",results[0], results[1], results[2], results[3], results[4]);
return S;
});
get("/", (req, res) -> chartPage);
post("/answer/", (req, res) -> {
try {
Object obj = parser.parse(req.body());
JSONArray array = (JSONArray)obj;
updateLists((String)array.get(0), (String)array.get(1));
System.out.println((String)array.get(0)+": "+(String)array.get(1));
} catch (ParseException e) {
e.printStackTrace();
}
return "success";
});
}
}