]> Some of my projects - tim-server.git/commitdiff
Add high score web page. master
authorAPTX <marek321@gmail.com>
Mon, 10 Jan 2011 21:40:32 +0000 (22:40 +0100)
committerAPTX <marek321@gmail.com>
Mon, 10 Jan 2011 21:40:32 +0000 (22:40 +0100)
chatserver.php
index.php
stripes.png [new file with mode: 0644]
style.css [new file with mode: 0644]

index 5999c4ffa27222770149cb667736a240b6af26f0..f1d863e6b590459f37513dad1c8d570aa407f484 100644 (file)
@@ -61,6 +61,23 @@ function genkey()
 
 $client_names = array();
 
 
 $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;
 while (true) {
        // create a copy, so $clients doesn't get modified by socket_select()
        $read = $clients;
@@ -78,7 +95,7 @@ while (true) {
                $client_names[$newkey] = '';
                
                // send the client a welcome message
                $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";
                
                socket_getpeername($newsock, $ip);
                echo "New client connected: {$ip}\n";
@@ -98,45 +115,36 @@ while (true) {
                
                // check if the client is disconnected
                if ($data === false) {
                
                // 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";
                        // 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;
                }
                        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)) {
                
                // 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 (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;
                                }
                                        continue;
                                }
-                               socket_write($read_sock, ">> AUTH ERROR\n");
+                               $client_names[$key] = $result;
+                               sendAllButOne("-> " . $result . " connected.\n", $clients, $read_sock);
                                continue;
                        }
 
                                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
                }
                
        } // end of reading foreach
index d0e29e8e19421933418ca28dfc9e6cd0b3e2220b..ee4d07c60281f571247342cf4e94bb867dcf47a6 100644 (file)
--- a/index.php
+++ b/index.php
@@ -12,9 +12,9 @@ switch($_GET['a'])
        case 'setscore':
                return set_score();
        case 'score':
        case 'setscore':
                return set_score();
        case 'score':
-       default:
                return score();
                return score();
-               die();
+       default:
+               return score(true);
 }
 
 function login() {
 }
 
 function login() {
@@ -90,7 +90,7 @@ function success($message)
 </message>';
 }
 
 </message>';
 }
 
-function score()
+function score($html = false)
 {
        global $entityManager;
 
 {
        global $entityManager;
 
@@ -100,20 +100,40 @@ function score()
                            ->setMaxResults(20)
                                ->getResult();
 
                            ->setMaxResults(20)
                                ->getResult();
 
-    print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<message>\n\t<scores>\n";
-       
        $position = 0;
        $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()
 }
 
 function set_score()
diff --git a/stripes.png b/stripes.png
new file mode 100644 (file)
index 0000000..9f36727
Binary files /dev/null and b/stripes.png differ
diff --git a/style.css b/style.css
new file mode 100644 (file)
index 0000000..da9834a
--- /dev/null
+++ b/style.css
@@ -0,0 +1,69 @@
+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