-
Notifications
You must be signed in to change notification settings - Fork 0
/
home.php
325 lines (314 loc) · 9.93 KB
/
home.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
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
<?php
session_start();
require_once('php/variables.php');
if (!isset($_SESSION['username'])) {
header('Location: index.php');
}
$error = array();
if (isset($_POST['username'])) {
//Make sure user exists
$s = $db->prepare('select * from users where username=:username');
$s->execute(array(':username' => $_POST['username']));
$row = $s->fetch();
$pass = true;
if (empty($row)) {
$error["message"] = "That user does not exist";
$pass = false;
}
if ($pass) {
//Create new game
$db->exec('insert into games(user1, user2, uuid, winner) values("'.$_SESSION['username'].'","'.$_POST['username'].'","'. uniqid() .'", NULL)');
}
//Redirect to prevent form resubmission
header('Location: home.php');
}
?>
<!doctype html>
<html>
<head>
<title><?php echo $title; ?> - Home</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap-responsive.min.css">
<link href='http://fonts.googleapis.com/css?family=Jura:400,600' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/skycaptains.css">
<link rel="shortcut icon" href="img/favicon.ico">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css">
<link rel="stylesheet" href="css/home.css">
</head>
<body>
<div class="container-fluid">
<div class="row-fluid">
<a class="btn btn-danger pull-right" href="php/logout.php">Logout</a>
</div>
<div class="row-fluid">
<div class="span7">
<h2>Hello, <?php echo $_SESSION['username']; ?></h2>
</div>
<div class="span5">
<form class="form-inline" id="challenge-form" method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<h3>Send a game request</h3>
<label class="control-label" for="username">Challenger Username:</label>
<br />
<input type="text" id="username" name="username" />
<button class="btn btn-primary" type="submit">Send Request</button>
</form>
<?php if (isset($error['message'])) { ?>
<div class="alert alert-error">
<p><?php echo $error['message']; ?></p>
</div>
<?php } ?>
</div>
</div>
<div class="row-fluid">
<div class="alert alert-error hide" id="game-server-error">
<p>The game server could not be reached. Please try again later.</p>
</div>
</div>
<div class="row-fluid">
<div class="span6">
<h3>Game Requests</h3>
<table class="table table-hover">
<thead>
<tr>
<th>Challenger</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
//Get all game requests
$s = $db->prepare('select * from games where user2=:username and winner is null');
$s->execute(array(':username' => $_SESSION['username']));
$rows = $s->fetchAll();
foreach($rows as $row) {
?>
<tr>
<td><?php echo $row['user1']; ?></td>
<td>
<a type="button" class="btn" href="play.php?game=<?php echo $row['uuid']; ?>">Play</a>
<a type="button" class="btn btn-danger" href="php/deletegame.php?game=<?php echo $row["uuid"]; ?>">Decline</a>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<div class="span6">
<h3>Your Challenges</h3>
<table class="table table-hover">
<thead>
<tr>
<th>Oppenent</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
//Get all game requests
$s = $db->prepare('select * from games where user1=:username and winner is null');
$s->execute(array(':username' => $_SESSION['username']));
$rows = $s->fetchAll();
foreach($rows as $row) {
?>
<tr>
<td><?php echo $row['user2']; ?></td>
<td>
<a type="button" class="btn" href="play.php?game=<?php echo $row['uuid']; ?>">Play</a>
<a type="button" class="btn btn-danger" href="php/deletegame.php?game=<?php echo $row['uuid']; ?>">Delete</a>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<h3>Completed Games [Last 15 Played]</h3>
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Opponent</th>
<th>Winner</th>
</tr>
</thead>
<tbody>
<?php
//Get all completed games
//Limit 15
$s = $db->prepare('select * from games where (user1=:username or user2=:username) and winner is not null order by completed desc limit 15');
$s->execute(array(":username" => $_SESSION['username']));
$rows = $s->fetchAll();
foreach($rows as $row) {
$winner = $row["winner"] == $_SESSION['username'];
$opponent = null;
if ($row["user1"] != $_SESSION['username']) {
$opponent = $row["user1"];
} else {
$opponent = $row["user2"];
}
?>
<tr class="<?php if ($winner) { ?>success<?php } else { ?>error<?php } ?>">
<td><?php echo $row["completed"]; ?></td>
<td><?php echo $opponent; ?></td>
<td><?php echo $row["winner"]; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div class="row-fluid" id="chat-bar">
<div class="hide span12" id="chat-box">
<div class="row-fluid">
<div class="span9" id="messages">
<?php
//Get messages from last 5 minutes
$s = $db->prepare("select * from messages where message_time >= date_sub(utc_timestamp(), interval 5 minute)");
$s->execute();
$rows = $s->fetchAll();
foreach ($rows as $row) {
$u = $row['user'];
$t = $row['message_time'];
$m = $row['message'];
?>
<span class="time"><?php echo $t; ?> </span>
<span class="user">[<?php echo $u; ?>] </span>
<span class="message"><?php echo $m ?></span>
<br />
<?php } ?>
</div>
<div class="span3" id="chat-users">
<strong>Online Users</strong><br/>
<?php
//Get logged in users
$s = $db->prepare("select username from users where loggedin=TRUE order by username asc");
$s->execute();
$rows = $s->fetchAll();
foreach($rows as $row) {
if ($row['username'] != $_SESSION['username']) {
?>
<span class='online'><i class='icon-circle'></i></span> <?php echo $row['username']; ?><br />
<?php } } ?>
</div>
</div>
<div class="row-fluid">
<div class="span12" id="input-box">
<form class="form-inline" id="input-form">
<input type="text" class="span11" id="message" autocomplete="off" placeholder="Press Enter to send" />
<button type="submit" class="btn" id="send-message">Send</button>
</form>
</div>
</div>
</div>
<div class="span12 text-center" id="chat-box-trigger">
<p><strong>SkyCaptains Chat</strong></p>
</div>
</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="http://autobahn.s3.amazonaws.com/js/autobahn.min.js"></script>
<script type="text/javascript">
var sess = null; //Autobahn session
var me = "<?php echo $_SESSION['username']; ?>"; //username
/**
* Updates users in chat window
*/
function updateUsers() {
var chat = $("#chat-users");
var me = "<?php echo $_SESSION['username']; ?>";
$.get("php/users.php", {}, function(data) {
data = $.parseJSON(data);
$(chat).empty();
$(chat).append('<strong>Online Users</strong><br/>');
for (var i = 0; i < data.length; i++) {
var user = data[i];
if (user[0] != me) {
$(chat).append("<span class='online'><i class='icon-circle'>" +
"</i></span> " + user[0] + "<br />");
}
}
});
}
/**
* Callback function on websocket message received
* Parameters
* topicUri {String} uri the message passed through
* event {Array} array containing message data
*/
function messageReceived(topicUri, event) {
var m = event["message"];
var t = event["time"];
var u = event["from"];
$("#messages").append('' +
'<span class="time">' + t + ' </span>' +
'<span class="user">[' + u + '] </span>' +
'<span class="message">' + m + '</span><br />');
}
$(document).ready(function() {
//Slide chat box
$("#chat-box-trigger").click(function() {
$("#chat-box").slideToggle();
});
//Websocket connection
var wsuri = "ws://<?php echo getenv("SKYCAPTAINS_SERVER"); ?>:<?php echo getenv("SKYCAPTAINS_PORT"); ?>";
ab.connect(wsuri,
function(session) {
console.log("connected");
sess = session;
sess.subscribe("http://skycaptains.com/chat", messageReceived);
},
function(code, reason) {
//Connection lost
console.log("connection lost");
$("#messages").append(
"The chat server could not be reached. Please try again later.<br />");
//Disable input
$("#message").attr("disabled", "disabled");
//Show game server error
$("#game-server-error").show();
sess = null;
}
);
//Update logged in users every 8 seconds
setInterval(updateUsers, 8000);
//Prevent blank submission for game requests
$("#challenge-form").submit(function() {
//Can't challenge empty and can't challenge yourself
if ($("#username").val() == "" || $("#username").val() == me) {
return false;
}
});
$("#input-form").submit(function() {
//send message via websockets
var m = $("#message").val();
if (sess && m != "") {
var time = new Date().toISOString().replace("T", " ");
time = time.substring(0, time.length - 5);
sess.publish("http://skycaptains.com/chat",
{"from" : me,
"time" : time,
"message" : m}
);
//send message to db
$.post("php/sendmessage.php", {"from" : me, "time" : time, "message": m}, function() { });
//add message to messages div
$("#messages").append('' +
'<span class="time">' + time + ' </span>' +
'<span class="user">[' + me + '] </span>' +
'<span class="message">' + m + '</span><br />');
//Clear old message
$("#message").val("");
}
return false;
});
}); //End document ready
</script>
</body>
</html>