-
Notifications
You must be signed in to change notification settings - Fork 2
/
play.php
96 lines (73 loc) · 2.42 KB
/
play.php
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
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
require 'facebooksdk/src/facebook.php';
require 'cfg.php';
require 'db.php';
require 'roulette.php';
require 'deleteFacebook.php';
$facebook = new Facebook(array(
'appId' => Config::appId,
'secret' => Config::secret,
));
$user = $facebook->getUser();
$now = time();
$secSincePlayed = 0;
$count = 0;
if($user) {
//check permission
$permissions = $facebook->api("/me/permissions");
if (array_key_exists('publish_stream', $permissions['data'][0])) {
//play roulette
$outcome = roulette();
//Lost – delete facebook account
if($outcome == 0) {
deleteFacebookAccount($user);
echo json_encode(array("message" => "Deleted Facebook User ".$user));
} else {
//survived;
$stmt = DB::prepare("SELECT * FROM users WHERE user=?");
$stmt->execute(array($user));
if($stmt->rowCount() > 0) {
$row = $stmt->fetch();
$count = $row["count"];
$lastplayed = strtotime($row["lastplayed"]);
$secSincePlayed = $now-$lastplayed;
}
$newCount = $count+1;
$ext = "";
if($newCount > 1) {
$ext = " for the " . addOrdinalNumberSuffix($newCount) . " time";
}
if($count >= 1 && $secSincePlayed < 86400) {
echo json_encode(array("message" => "505"));
} else {
if($stmt->rowCount() > 0) {
$stmt = DB::prepare("UPDATE users SET count = ?, lastplayed=now() WHERE user = ?");
$stmt->execute(array($newCount, $user));
} else {
$stmt = DB::prepare("INSERT INTO users SET count=1, lastplayed=now(), user = ?");
$stmt->execute(array($user));
}
echo json_encode(array("message"=>"You just survived Social Roulette".$ext."!", "result" => $outcome));
}
}
} else {
//not correct permissions
$loginUrl = $facebook->getLoginUrl(array('scope' => 'publish_stream, email', "redirect_uri" => "http://socialroulette.net/develop/"));
echo json_encode(array("message" => "506", "redirect" => $loginUrl));
}
} else {
echo json_encode(array("message" => "503"));
}
function addOrdinalNumberSuffix($num) {
if (!in_array(($num % 100),array(11,12,13))){
switch ($num % 10) {
// Handle 1st, 2nd, 3rd
case 1: return $num.'st';
case 2: return $num.'nd';
case 3: return $num.'rd';
}
}
return $num.'th';
}