$client_names = array();
+
+function sendAllButOne($message, array $clients, $socket)
+{
+ global $sock;
+ // send this to all the clients in the $clients array (except the first one, which is a listening socket)
+ foreach ($clients as $send_sock) {
+
+ // if its the listening sock or the client that we got the message from, go to the next one in the list
+ if ($send_sock == $sock || $send_sock == $socket)
+ continue;
+
+ // write the message to the client -- add a newline character to the end of the message
+ socket_write($send_sock, $message);
+
+ } // end of broadcast foreach
+}
+
while (true) {
// create a copy, so $clients doesn't get modified by socket_select()
$read = $clients;
$client_names[$newkey] = '';
// send the client a welcome message
- socket_write($newsock, ">> Connected. There are ".(count($clients) - 1)." client(s) connected to the server\n>> Type /quit to closse chat view.\n");
+ socket_write($newsock, ">> Connected. There are ".(count($clients) - 1)." client(s) connected to the server\n");
socket_getpeername($newsock, $ip);
echo "New client connected: {$ip}\n";
// check if the client is disconnected
if ($data === false) {
+ sendAllButOne("<- " . $client_names[$key] . " disconnected.\n", $clients, $read_sock);
// remove client for $clients array
unset($clients[$key]);
unset($client_names[$key]);
echo "client disconnected.\n";
- // continue to the next client to read from, if any
continue;
}
-
+
// trim off the trailing/beginning white spaces
$data = trim($data);
// check if there is any data after trimming off the spaces
if (!empty($data)) {
+ // Auth
if (empty($client_names[$key]))
{
$result = auth($data);
- if ($result !== false)
+ if ($result === false)
{
- $client_names[$key] = $result;
+ socket_write($read_sock, ">> AUTH ERROR\n");
continue;
}
- socket_write($read_sock, ">> AUTH ERROR\n");
+ $client_names[$key] = $result;
+ sendAllButOne("-> " . $result . " connected.\n", $clients, $read_sock);
continue;
}
- $sender_name = $client_names[$key];
- // send this to all the clients in the $clients array (except the first one, which is a listening socket)
- foreach ($clients as $send_sock) {
-
- // if its the listening sock or the client that we got the message from, go to the next one in the list
- if ($send_sock == $sock || $send_sock == $read_sock)
- continue;
-
- // write the message to the client -- add a newline character to the end of the message
- socket_write($send_sock, $sender_name . ": " . $data."\n");
-
- } // end of broadcast foreach
-
+ // If authed, send the message to others
+ sendAllButOne($client_names[$key] . ": " . $data."\n", $clients, $read_sock);
}
} // end of reading foreach
case 'setscore':
return set_score();
case 'score':
- default:
return score();
- die();
+ default:
+ return score(true);
}
function login() {
</message>';
}
-function score()
+function score($html = false)
{
global $entityManager;
->setMaxResults(20)
->getResult();
- print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<message>\n\t<scores>\n";
-
$position = 0;
- foreach ($myScores AS $score)
+
+ if ($html)
{
- print "\t\t<entry>\n"
- . "\t\t\t<position>" . ++$position . "</position>\n"
- . "\t\t\t<user-id>" . $score->getUser()->id() . "</user-id>\n"
- . "\t\t\t<user-name>" . $score->getUser()->getName() . "</user-name>\n"
- . "\t\t\t<score>" . $score->getGscore() . "</score>\n"
- . "\t\t</entry>\n";
+ print "<!DOCTYPE HTML>\n"
+ . "<html>\n\t<head>\n"
+ . "\t\t" . '<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />'
+ . "\n\t\t" . '<link href="style.css" rel="stylesheet" type="text/css" />'
+ . "\n\t\t<title>TIM Game High Scores</title>\n"
+ . "\t</head>\n\t<body>\n\t\t<h1>TIM Game High Scores</h1>\n";
+ foreach ($myScores AS $score)
+ {
+ print "\t\t<div class=\"entrywrap\">\n\t\t\t<div class=\"entry\">\n"
+ . "\t\t\t\t<div class=\"position\">" . ++$position . "</div>\n"
+ . "\t\t\t\t<span class=\"user\">" . $score->getUser()->getName() . "</span>\n"
+ . "\t\t\t\t<span class=\"score\">" . $score->getGscore() . "</span>\n"
+ . "\t\t\t</div>\n\t\t</div>\n";
+ }
+ print "\t</body>\n</html>\n";
+ }
+ else
+ {
+ print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<message>\n\t<scores>\n";
+ foreach ($myScores AS $score)
+ {
+ print "\t\t<entry>\n"
+ . "\t\t\t<position>" . ++$position . "</position>\n"
+ . "\t\t\t<user-id>" . $score->getUser()->id() . "</user-id>\n"
+ . "\t\t\t<user-name>" . $score->getUser()->getName() . "</user-name>\n"
+ . "\t\t\t<score>" . $score->getGscore() . "</score>\n"
+ . "\t\t</entry>\n";
+ }
+ print "\t</scores>\n</message>\n";
}
- print "\t</scores>\n</message>\n";
-
}
function set_score()
--- /dev/null
+html {
+ height: 100%;
+ background-color: #343434;
+ background-image:url('stripes.png');
+ background-attachment:fixed;
+}
+
+body {
+ min-width: 600px;
+ max-width: 1000px;
+ margin: 0 auto; /* center */ padding: 0 20px;
+ color: white;
+ font-size 2em;
+}
+
+h1 {
+ text-align: center
+}
+
+.entrywrap {
+ width: 100%;
+ height: 40px;
+ margin-top: 3px;
+ margin-bottom: 3px;
+ background: rgba(192,192,192, 0.2);
+ border-radius: 10px;
+ -moz-border-radius: 10px;
+ -webkit-border-radius: 10px;
+ padding: 5px 5px 5px 5px;
+ display: table;
+}
+
+.entry {
+ display: table-cell; vertical-align: middle;
+}
+
+div.entrywrap:nth-child(2) {
+ color: gold;
+ font-size: 5em;
+}
+
+div.entrywrap:nth-child(3) {
+ color: silver;
+ font-size: 4em;
+}
+
+div.entrywrap:nth-child(4) {
+ color: brown;
+ font-size: 3em;
+}
+
+.position {
+ float: left;
+ width: 33%;
+}
+
+.user {
+ float: right;
+ clear: right;
+ text-align: right;
+ width: 33%;
+}
+
+.score {
+ float: left;
+ text-align: center;
+ margin: auto;
+ width: 33%;
+}
\ No newline at end of file