-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
345 lines (308 loc) · 10.1 KB
/
game.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
comparison_id = 0;
image1_perm = 0;
image2_perm = 0;
handlers_registered = 0;
submit_lock = 0;
waiting_next = true;
next_comparison = 0;
game_started = false;
like_url_template = "//www.facebook.com/plugins/like.php?"+
"appId=411664055513252&"+
"href={{IMAGE-URL}}&"+
"send=false&"+
"layout=button_count&"+
"width=90&"+
"height=21&"+
"show_faces=false&"+
"action=like&"+
"colorscheme=light&"+
"font=lucida+grande";
function preload_images(src1, src2)
{
$("#pre-image1").attr("src", src1);
//alert("src1= " + src1);
$("#pre-image2").attr("src", src2);
//alert("src2= " + src2);
}
function register_handlers()
{
$("#play-button").click(function(){
//alert("Clicked!");
start_game();
});
$("#noreg-play-button").click(function(){
//alert("Clicked!");
start_game();
});
}
function setup_comparison(comparison)
{
$("#loading-animation").hide();
$('#comparison-box').fadeIn("fast");
$('#better').fadeIn("fast");
$('#image1').attr("src", comparison['art1']['image']);//.fadeIn("fast");
$('#zoom1').attr("href", comparison['art1']['image']);// loads the image for magnification
$('#image1').attr("permalink", comparison['art1']['permalink']);
$('#like-image1').attr("src", like_url_template.replace("{{IMAGE-URL}}", comparison['art1']['image']));
$('#image2').attr("src", comparison['art2']['image']);//.fadeIn("fast");
$('#zoom2').attr("href", comparison['art2']['image']);// loads the image for magnification
$('#image2').attr("permalink", comparison['art2']['permalink']);
$('#like-image2').attr("src", like_url_template.replace("{{IMAGE-URL}}", comparison['art2']['image']));
$('#game-box').css("display", "block");
$('#comparison-box').css("opacity", "1");
comparison_id = comparison['id'];
image1_perm = comparison['art1']['permalink'];
image2_perm = comparison['art2']['permalink'];
// activate cloud-zoom
$('.cloud-zoom').CloudZoom();
}
function comp_back(data)
{
//$('.result').html(data);
//alert(data);
if (waiting_next)
{
waiting_next = false;
try {
comparison = JSON.parse(data);
} catch (err) {
//invalid response from server?
waiting_next = true;
setTimeout(new_comparison, 500);
}
setup_comparison(comparison);
submit_lock = 0;
if (!handlers_registered)
{
$('#zoom1').click(function(e) {
e.preventDefault();
if (!submit_lock)
{
submit_selection(comparison_id, image1_perm, image2_perm);
}
});
$('#zoom2').click(function(e) {
e.preventDefault();
if (!submit_lock)
{
submit_selection(comparison_id, image2_perm, image1_perm);
}
});
$('#neither-button').click(function() {
if (!submit_lock)
{
//new_comparison();
submit_selection(comparison_id, false, false);
}
});
handlers_registered = 1;
}
}
else
{
next_comparison = JSON.parse(data);
preload_images(next_comparison['art1']['image'], next_comparison['art2']['image']);
}
$('#comparison-box').css("opacity", "1");
}
function new_comparison()
{
var url = 'new-comparison.php';
$.get(url, comp_back);
}
function submit_selection(comparison_id, winner, loser)
{
if (next_comparison)
{
setup_comparison(next_comparison);
next_comparison = 0;
waiting_next = false;
}
else
{
submit_lock = 1;
waiting_next = true;
$('#comparison-box').css("opacity", "0.5");
new_comparison();
}
new_comparison();
if (!winner) {
// "Neither" case. Nothing to submit.
return;
}
var url = 'submit.php?comp_id=' + comparison_id + (winner ? '&winner='+winner : '');
$.get(url, function(data) {
try
{
var result = JSON.parse(data);
if (result['warn'] == "email")
{
noty({"text":"Login with Facebook to save your choices!",
"theme":"noty_theme_mitgux","layout":"topRight","type":"information",
"animateOpen":{"height":"toggle"},"animateClose":{"height":"toggle"},
"speed":500,"timeout":5000,"closeButton":true,"closeOnSelfClick":true,
"closeOnSelfOver":false,"modal":false});
}
if (result['response'].match(/success/i))
{
//alert("Successful comparison!");
}
if (result['saved-to'] == 'session' && $("#fb-welcome-message").html() != "")
{
$("div.fb-login-button").show();
$("#fb-welcome-message").html("");
noty({"text":"Login with Facebook to save your choices!",
"theme":"noty_theme_mitgux","layout":"topRight","type":"information",
"animateOpen":{"height":"toggle"},"animateClose":{"height":"toggle"},
"speed":500,"timeout":5000,"closeButton":true,"closeOnSelfClick":true,
"closeOnSelfOver":false,"modal":false});
} else if (!result['saved-to'] && $("#fb-welcome-message").html() == "")
{
$("div.fb-login-button").hide();
$("#fb-welcome-message").html("Welcome <b class='welcome-name'>" + me.first_name + "</b>!");
}
}
catch (error)
{
//alert("Invalid response!\n"+data);
}
});
//submit stats
var url = 'stats.php?comp_id=' + comparison_id + (winner ? '&winner='+winner : '') + (loser ? '&loser='+loser : '');
$.get(url, function(data) {
try
{
var result = JSON.parse(data);
var obj = {
link: 'http://www.artsicle.com/art/' + result['permalink'],
picture: result['image'],
caption: '"' + result['name'] + '" by ' + result['artist']['full'],
winrate: result['winrate'],
battles: result['battles'],
size: result['size'],
medium: result['medium']
};
noty({"text":"<img class='notif-image' src='"+obj['picture']+"'/>"+
"<div class='notif-message'>You voted for "+obj['caption']+". "+
"<span class='notif-emp'>"+obj['winrate']+"%</span> of people agree with you, "+
"given <span class='notif-emp'>"+obj['battles']+"</span> battles.</div><div class='notif-dummy'></div>",
"theme":"noty_theme_mitgux", "layout":"bottomRight",
"type":"information","animateOpen":{"height":"toggle"},
"animateClose":{"height":"toggle"},"speed":500,"timeout":3000,
"closeButton":true,"closeOnSelfClick":false,
"closeOnSelfOver":false,"modal":false});
}
catch (error)
{
//alert("Invalid response!\n"+data);
//$('#choice').html(data);
}
});
}
function start_game()
{
if (!game_started)
{
game_started = true;
$('#comparison-box').hide();
$('.welcome-message').hide();
$('#login-message').hide();
$('#play-button').hide();
$('#gallerybox').hide();
stop_time(); //stop loading the gallery
$("#loading-animation").show();
//$("#play-button").hide();
//$("#login-message").hide();
//$("#fblog").hide();
}
new_comparison();
new_comparison();
}
/////////////////////////////CODE FOR THE GALLERY//////////////////////////////////
var images = new Array();
var altText = new Array();
var permalink = new Array();
/*
images[0] = 'images/gallery/1.jpg';
images[1] = 'images/gallery/2.jpg';
images[2] = 'images/gallery/3.jpg';
images[3] = 'images/gallery/4.jpg';
images[4] = 'images/gallery/5.jpg';
images[5] = 'images/gallery/6.jpg';
altText[0] = 'Image 1';
altText[1] = 'Image 2';
altText[2] = 'Image 3';
altText[3] = 'Image 4';
altText[4] = 'Image 5';
altText[5] = 'Image 6';*/
var timer_on = "true";
var t = 0;
var Y = 0;
var artsicle_url = 'http://www.artsicle.com/art/';
function change(X,Y){
$(X).attr('src' , images[Y]);
$(X).attr('alt' , altText[Y]);
$(X).attr('onClick' , 'window.open("'+permalink[Y]+'")');
}
function replaceThings(X,Y){
$(X).fadeOut(350, function() {change(X,Y);
});
$(X).fadeIn(350, function() {});
}
function repeat(){
if (timer_on == "false")
return;
replaceThings("#0", Y); Y = (Y+1) % images.length;
replaceThings("#1", Y); Y = (Y+1) % images.length;
replaceThings("#2", Y); Y = (Y+1) % images.length;
if(timer_on == "true"){
t = setTimeout("repeat()", 5000);
}
}
function start_time(){
if (timer_on == "false"){
timer_on = "true";
repeat();
}
}
function stop_time(){
if (!typeof t === 'undefined') {
clearTimeout(t);
}
timer_on = "false";
}
function prepare(){
//t = setTimeout("repeat()", 5000);
$(".gallery").mouseenter(
function () {
stop_time();
});
$(".gallery").mouseleave(
function () {
t = setTimeout("start_time()", 5000);
});
}
function loadImages(){
var url = 'gallery.php';
$.get(url, function(data) {
try
{
var result = JSON.parse(data);
console.log(result);
for (i in result.popular){
images[i] = result.popular[i].image;
altText[i] = result.popular[i].name;
permalink[i] = artsicle_url + result.popular[i].permalink;
}
repeat();
}
catch (error)
{
//$('#choice').html(data);
}
});
}
$(document).ready(function () {
loadImages();
prepare();
});