-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbeaconator.js
156 lines (117 loc) · 3.39 KB
/
beaconator.js
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
153
154
155
156
/* Beaconator js */
$(document).ready(function(){
$option_area = $('#options_list');
$new_options = $('#new_options');
$add_option = $('#add_option');
$beaconize = $('#beaconize');
$reset = $('#reset');
$results = $('#results');
$about = $('#about');
$about_content = $('#about-content');
var num_options = 2;
var options = [];
var beacons_used = 0;
var random_string = "";
var beacon_string = "";
var beacon_value = 0;
// Hide results on page load
$results.hide();
// Hide about on page load
$about_content.hide();
// Adds a new option to the list
$add_option.click(function(){
// Increment counter
num_options++;
// Add preconfigured html to the page
var opt = "<div id='opt" + num_options + "'>";
opt += "<label>Option " + num_options + "</label>";
opt += "<div class='input-group'>";
opt += "<input type='text' id='Option" + num_options + "' class='form-control' />";
opt += "<span class='input-group-btn'>";
opt += "<input type='button' id='clear" + num_options + "' class='btn red inline clear_option' value='×' />";
opt += "</span>";
opt += "</div>";
opt += "</div>";
$new_options.append(opt);
});
$(document).on('click', '.clear_option', function (sel){
// Slightly messy... deal with it
var option_id = parseInt(sel.target.id[5]);
// Clear the input value
//$("#Option" + option_id).val("");
// Move every value below that id up one spot
for(var i = (option_id+1); i <= num_options; i++){
var opt_id_first = "#Option" + (i-1);
var opt_id_second = "#Option" + i;
var opt_val = $(opt_id_second).val();
$(opt_id_first).val(opt_val);
}
// Remove the last id
$('#opt' + num_options).remove();
num_options--;
});
$beaconize.click(function(){
// Clear out array
options = [];
// Parse options into array
for(var i = 1; i <= num_options; i++){
var option_id = "#Option" + i;
options.push($option_area.find(option_id).val());
}
options = shuffle(options);
// Read beacon API
$.ajax({
type:"GET",
url:"beacon",
contentType: "text/plain",
dataType:"text",
success:function(data) {
// Store data in string
random_string = data;
// Bounds check for multiple requests
if(beacons_used > 15){
beacons_used = 0;
}
// Parse 8 hex chars from string
beacon_string = random_string.substring(beacons_used*8, (beacons_used+1)*8);
// Convert hex to decimal
beacon_value = parseInt(beacon_string, 16);
// Perform modulo
var selection = (beacon_value % options.length);
// Append results
$('.decision').html(options[selection]);
// Increment counter
beacons_used++;
},
error:function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
// Display results
$results.show();
});
$reset.click(function(){
// Shut. Down. Everything.
for(var i = 1; i <= num_options; i++){
// Reset all option values
var option_id = "#Option" + i;
$(option_id).val("");
}
// Reset options
num_options = 2;
options = [];
// Clear out added options
$new_options.empty();
// Hide results
$results.hide();
});
$about.click(function(){
// Smooth toggle
$about_content.slideToggle();
});
function shuffle(o){
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
});