-
Notifications
You must be signed in to change notification settings - Fork 1
/
voting.html
142 lines (130 loc) · 5.38 KB
/
voting.html
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
<!DOCTYPE html>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css">
<script defer src="https://code.getmdl.io/1.3.0/material.min.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.7.5/firebase.js"></script>
<html lang="en">
<head>
<title>Voting</title>
</head>
<header class=mdl-layout__header>
<div class="mdl-layout__header-row">
<span class="mdl-layout-title">Voting system</span>
</div>
</header>
<main class="mdl-layout__content">
<body>
<p style="color: black; font-size: 20px">Vote for a candidate.</p>
<div class="demo-ribbon"></div>
<main class="demo-main mdl-layout__content">
<div class="demo-container mdl-grid">
<div class="mdl-cell mdl-cell--2-col mdl-cell--hide-tablet mdl-cell--hide-phone"></div>
<div class="demo-content mdl-color--white mdl-shadow--4dp content mdl-color-text--grey-800 mdl-cell mdl-cell--8-col">
<div style="width:1920px; margin:0 auto;"></div>
<div class="demo-crumbs mdl-color-text--grey-500">Logged in > Voting</div>
<h3>Please choose someone to vote for, before logging out!</h3>
<p id="jacobID" style="color: black; font-size: 20px;"></p>
<a id="jacobVote" type="button" onclick="incrementJacob()" class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--raised mdl-button--colored">Vote for Jacob</a>
<p id="jamesID" style="color: black; font-size: 20px;">James</p>
<a id="jamesVote" type="button" onclick="incrementJames()" class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--raised mdl-button--colored">Vote for James</a>
<p id="jesseID" style="color: black; font-size: 20px;">Jesse</p>
<a id="jesseVote" type="button" onclick="incrementJesse()" class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--raised mdl-button--colored">Vote for Jesse</a>
<a id="logOut" type="button" onclick="logout()" class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--raised mdl-button--colored" style="display:none">Log out</a>
</div>
</div>
</body>
<script>
// Initialize Firebase
var config = {
apiKey: "AIzaSyDOXaSH_7_NEvrtUMZg6-L3eJXVIS2jOAI",
authDomain: "project1-1d4a2.firebaseapp.com",
databaseURL: "https://project1-1d4a2.firebaseio.com",
storageBucket: "gs://project1-1d4a2.appspot.com"
};
firebase.initializeApp(config);
// Get a reference to the database service
var db = firebase.database();
var alldata = db.ref("serverData");
var vcaVAL = db.ref("serverData").child("variables").child("VCA");
var ccaVAL = db.ref("serverData").child("variables").child("CCA");
var jacobVAL = db.ref("serverData").child("variables").child("Jacob");
var jamesVAL = db.ref("serverData").child("variables").child("James");
var jesseVAL = db.ref("serverData").child("variables").child("Jesse");
alldata.on("value", function(snapshot) {
data = snapshot.val();
jacob = data.variables.Jacob
james = data.variables.James
jesse = data.variables.Jesse
total = james + jacob + jesse
<!-- Ensures dividing by 0 does not occur -->
if (total == 0) {
total = 1
}
document.getElementById("jacobID").innerHTML = "Jacob " + Math.round((jacob/total)*100,4) + "%"
document.getElementById("jamesID").innerHTML = "James " + Math.round((james/total)*100,4) + "%"
document.getElementById("jesseID").innerHTML = "Jesse " + Math.round((jesse/total)*100,4) + "%"
console.log("Values have been shown.")
});
function incrementJacob() {
console.log("Incrementing Jacob")
jacobVote.style.display = "none";
jamesVote.style.display = "none";
jesseVote.style.display = "none";
logOut.style.display= "inline";
return alldata.once("value").then(function(snapshot) {
jacobVAL.transaction(function(jacobVAL) {
return (jacobVAL || 0) + 1;
});
ccaVAL.transaction(function(ccaVAL) {
return (ccaVAL || 0) - 1;
});
});
};
function incrementJesse() {
console.log("Incrementing Jesse")
jacobVote.style.display = "none";
jamesVote.style.display = "none";
jesseVote.style.display = "none";
logOut.style.display= "inline";
return alldata.once("value").then(function(snapshot) {
jesseVAL.transaction(function(jesseVAL) {
return (jesseVAL || 0) + 1;
});
ccaVAL.transaction(function(ccaVAL) {
return (ccaVAL || 0) - 1;
});
});
};
function incrementJames() {
console.log("Incrementing James")
jacobVote.style.display = "none";
jamesVote.style.display = "none";
jesseVote.style.display = "none";
logOut.style.display= "inline";
return alldata.once("value").then(function(snapshot) {
jamesVAL.transaction(function(jamesVAL) {
return (jamesVAL || 0) + 1;
});
ccaVAL.transaction(function(ccaVAL) {
return (ccaVAL || 0) - 1;
});
});
};
function logout() {
console.log("Logging out")
return alldata.once("value").then(function(snapshot) {
var data = snapshot.val()
cca = data.variables.CCA
vca = data.variables.VCA
if(vca >= cca) {
console.log("Logging out. Directing to login page.");
document.getElementById("logOut").href = "/"
}
else {
console.log("Login unseccesful. Redirecting to error page.");
document.getElementById("logOut").href = "/error"
}
});
};
</script>
</html>