Commits

Ivan Vučica committed afd6ef3

Renamed pong.js to pong-network.js to reflect future separation of UI and game code.

Comments (0)

Files changed (3)

 <title>TVZ Pong</title>
 <link rel="stylesheet" href="style.css">
 <link href='http://fonts.googleapis.com/css?family=Luckiest+Guy' rel='stylesheet' type='text/css'>
-<script src="pong.js"></script>
+<script src="pong-network.js"></script>
 <?php
 if(file_exists("defaults.php")) include_once 'defaults.php';
 zxmpp_headers();
+var zxmpp;
+function pongLogin(server, username, password)
+{
+    var user = username.split('@')[0];
+    var domain = username.split('@')[1];
+    
+    var cfg = {
+        "bind-url": "zxmpp/punjab-bind/",
+        "route": "xmpp:" + server,
+        "domain": domain,
+        "boshwait": 5
+    };
+    
+    zxmpp = new zxmppClass();
+    zxmpp.onConnectionTerminate.push(pongZXMPP_onConnectionTerminate);
+    zxmpp.onPresenceUpdate.push(pongZXMPP_onPresenceUpdate);
+    zxmpp.onRosterUpdate.push(pongZXMPP_onRosterUpdate);
+    zxmpp.onMessage.push(pongZXMPP_onMessage);
+    zxmpp.onPacket.push(pongZXMPP_onPacket);
+
+    zxmpp.main(cfg, user, password);
+    zxmpp.setOwnPresence("avail", "Working on TVZ Pong", 1);
+    zxmpp.clientFeatureExtensions["tvzpong"]=["http://tvzpong.vucica.net/protocol/"];
+    
+    document.getElementById("roster").style.display = 'inherit';
+    document.getElementById("roster").innerHTML = "<div style='text-align: center;'><img src='spinner.gif'><br>Logging in...</div>";
+    document.getElementById("loginbox").style.display = 'none';
+}
+
+///////////////////////////////
+
+var pong_rosterContent = {};
+
+function pong_rosterAdded(jid, show, display, status)
+{  
+    pong_rosterContent[jid] = { 'jid':jid, 'show':show, 'display':display, 'status':status };
+    
+    pong_rosterRefresh();
+}
+function pong_rosterRemoved(jid)
+{
+    delete pong_rosterContent[jid];
+    pong_rosterRefresh();
+}
+function pong_rosterRefresh()
+{
+    var roster = "<div style='text-align: center;'><div style='height: 24px;'></div></div>";
+    var atLeastOneUserOnline = false;
+    for(var jid in pong_rosterContent)
+    {
+        var entry = pong_rosterContent[jid];
+        var supportsPong = pong_rosterContactSupportsPong(jid);
+
+        roster += "<div class='contact " + entry['show'];
+        if(supportsPong)
+            roster += " supportsPong ";
+        roster += "' ";
+        if(supportsPong)
+            roster += " onclick=\"pong_invite('" + jid + "')\" ";
+        roster +=">";
+        
+        roster += entry["display"];
+        if(supportsPong)
+        {
+            roster += " *** ";
+        }
+        roster += "</div>";
+        if(entry['show'] != 'unavailable')
+            atLeastOneUserOnline = true;
+    }
+    if(!atLeastOneUserOnline)
+    {
+        roster += "<div style='text-align: center;'>noone online</div>";
+    }
+    document.getElementById('roster').innerHTML = roster;
+
+}
+
+function pong_rosterContactSupportsPong(jid)
+{
+    var presences = zxmpp.getPresencesForBareJid(jid);
+    var supportsPong = false;
+
+    for (var resource in presences)
+    {
+        var presence = presences[resource];
+        if(presence.caps && presence.caps.supports("http://tvzpong.vucica.net/protocol/"))
+            supportsPong = true;
+    }
+    return supportsPong;
+}
+
+function pong_rosterContactResourcesSupportingPong(jid)
+{
+    var presences = zxmpp.getPresencesForBareJid(jid.split("/")[0]);
+    var supportsPong = [];
+    
+    for (var resource in presences)
+    {
+        if(presences[resource].caps && presences[resource].caps.supports("http://tvzpong.vucica.net/protocol/"))
+        {
+            supportsPong.push(resource);
+        }
+    }
+    return supportsPong;
+}
+
+
+////////////////////////////////
+var pong_talkTo = false;
+function pong_invite(jid)
+{
+    var resources = pong_rosterContactResourcesSupportingPong(jid);
+    
+    for(var resourceId in resources)
+    {
+        var resource = resources[resourceId];
+        var packet = new zxmpp.packet(zxmpp);
+        var message = new zxmpp.stanzaMessage(zxmpp);
+        message.appendToPacket(packet, zxmpp.fullJid, jid + "/" + resource, "chat", false); // pass no body
+        var invitationNode = packet.xml.createElementNS("http://tvzpong.vucica.net/protocol/", "invitation");
+        packet.messageXML.appendChild(invitationNode);
+        packet.send("poll");
+    }
+}
+function pong_sendData(jid, data)
+{
+    //var resources = tuntap_rosterContactResourcesSupportingTunTap(jid);
+    
+    //for(var resourceId in resources)
+    {
+        //var resource = resources[resourceId];
+        var packet = new zxmpp.packet(zxmpp);
+        var message = new zxmpp.stanzaMessage(zxmpp);
+        message.appendToPacket(packet, zxmpp.fullJid, jid, "chat");// pass no body
+        var dataNode = packet.xml.createElementNS("http://tvzpong.vucica.net/protocol/", "data");
+
+        var dataText = packet.xml.createTextNode(data);
+        dataNode.appendChild(dataText);
+
+        packet.messageXML.appendChild(dataNode);
+        packet.send("poll");
+    }
+}
+
+///////////////////////////////
+
+function pongZXMPP_onConnectionTerminate(sender, code, humanreadable)
+{
+    alert("Disconnected.\n\Reason: " + humanreadable + "\n\nCode: " + code);
+}
+
+function pongZXMPP_onPresenceUpdate(sender, presence)
+{
+    var topPresence = sender.getTopPresenceForBareJid(presence.bareJid);
+    if(topPresence)
+    {
+        console.log(" - Updating " + topPresence.bareJid + " with " + topPresence.show + " and " + topPresence.status);
+        //				zxmppui.presenceUpdate(topPresence.bareJid, topPresence.show, false, topPresence.status);
+        
+        if(!pong_rosterContent[topPresence.bareJid])
+        {
+            //console.warn("!!!!! NO ROSTER ENTRY FOR " + topPresence.bareJid);
+            // Happens when getting report for ourselves.
+        }
+        else
+        {
+            pong_rosterContent[topPresence.bareJid].show = topPresence.show;
+            pong_rosterContent[topPresence.bareJid].status = topPresence.status;
+            pong_rosterRefresh();
+
+            
+        }
+
+    }
+    else
+    {
+        console.warn("no top presence for " + presence.bareJid);
+    }
+
+}
+function pongZXMPP_onRosterUpdate(sender, item)
+{
+    var presence = sender.getTopPresenceForBareJid(item.bareJid);
+    
+    if(item.subscription != "removed" && item.subscription != "none")
+    {
+        
+        var allowed = true;
+        if(allowed)
+        {
+            var presenceShow = presence ? presence.show : "unavailable";
+            var display = item.name ? item.name : item.bareJid.split('@')[0];
+            var presenceStatus = presence ? presence.status : "";
+            
+            var vcard = sender.vCards[item.bareJid];
+            if(vcard && vcard.fn)
+                display = vcard.fn;
+            
+            pong_rosterAdded(item.bareJid, presenceShow, display, presenceStatus);
+        }
+        
+        
+    }
+    else
+    {
+        pong_rosterRemoved(item.bareJid);
+    }
+}
+
+
+function pongZXMPP_onMessage(sender, messageStanza)
+{
+    if(messageStanza.body)
+    {
+        var text = messageStanza.body;
+        
+        if(messageStanza.type == "error")
+        {
+            text = "ERROR with message: " + text;
+        }
+        /*
+        zxmppZ_showNotification(undefined, messageStanza.from, text);
+         */
+    }
+    /*
+    <? if ($useZXMPPUI) { ?>
+        zxmppui.messageStanzaReceived(messageStanza);
+		<? } ?>
+    */
+    var xml = messageStanza.messageNode;
+    
+    if(xml && xml.childNodes) 
+    {
+        for(var i in xml.childNodes)
+        {
+            var child = xml.childNodes[i];
+            if(!child.nodeName) continue;
+            
+            this.zxmpp.util.easierAttrs(child);
+            switch(child.extendedNodeName)
+            {
+                    // handling of nonstandard messages
+                    
+                case "http://tvzpong.vucica.net/protocol/+invitation":
+                    /*
+                    var subdata = {};
+                    for(var j in child.childNodes)
+                    {
+                        var subChild = child.childNodes[j];
+                        if(!subChild.nodeName) continue;
+                        if(subChild.firstChild && subChild.firstChild.nodeValue)
+                            subdata[subChild.nodeName] = subChild.firstChild.nodeValue;
+                    }
+                    
+                    var fakeMessage = new Object();
+                    fakeMessage.from = "Evaluator"; //messageStanza.from.split("@")[0]";
+                    fakeMessage.body = "<br>Evaluacija zadatka " + subdata["problemname"] + " dovršena! Imate " + subdata["points"] + " bodova. <a target=\"_blank\" href=\"printablerun.php?app=evaluator&evlshw=log&user=<?=$account?>&file=" + subdata["problemname"] + "\">Log</a>";
+                    */
+                    /*
+                    zxmppZ_onMessage(sender, fakeMessage);
+                     */
+                    alert("Invitation from " + messageStanza.from);
+		    pong_talkTo = messageStanza.from;
+                    break;
+		case "http://tvzpong.vucica.net/protocol/+data":
+                    //net_handler(child.firstChild.nodeValue);
+                    break;
+            }
+        }
+    }
+}
+
+function pongZXMPP_onPacket(sender, packet)
+{
+    if(packet && packet.incomingStanza && packet.incomingStanza.iqXML)
+    {
+        for(var childId in packet.incomingStanza.iqXML.childNodes)
+        {
+            var child = packet.incomingStanza.iqXML.childNodes[childId];
+            
+            // handling of nonstandard non-message packets
+            
+            if(child.nodeName == "jingle")
+            {
+                // do whatever you want ;-)
+                
+                // note that the iq may or may not be handled, and 
+                // the error may or may not be dispatched already.
+                // act accordingly. this mechanism is not really a
+                // good way to implement jingle, considering zxmpp
+                // has already dispatched an iq error message.
+                
+            }
+        }
+        
+        // we may have received caps update.
+        // refresh roster!
+        pong_rosterRefresh();
+    }
+    
+
+}
+

pong.js

-var zxmpp;
-function pongLogin(server, username, password)
-{
-    var user = username.split('@')[0];
-    var domain = username.split('@')[1];
-    
-    var cfg = {
-        "bind-url": "zxmpp/punjab-bind/",
-        "route": "xmpp:" + server,
-        "domain": domain,
-        "boshwait": 5
-    };
-    
-    zxmpp = new zxmppClass();
-    zxmpp.onConnectionTerminate.push(pongZXMPP_onConnectionTerminate);
-    zxmpp.onPresenceUpdate.push(pongZXMPP_onPresenceUpdate);
-    zxmpp.onRosterUpdate.push(pongZXMPP_onRosterUpdate);
-    zxmpp.onMessage.push(pongZXMPP_onMessage);
-    zxmpp.onPacket.push(pongZXMPP_onPacket);
-
-    zxmpp.main(cfg, user, password);
-    zxmpp.setOwnPresence("avail", "Working on TVZ Pong", 1);
-    zxmpp.clientFeatureExtensions["tvzpong"]=["http://tvzpong.vucica.net/protocol/"];
-    
-    document.getElementById("roster").style.display = 'inherit';
-    document.getElementById("roster").innerHTML = "<div style='text-align: center;'><img src='spinner.gif'><br>Logging in...</div>";
-    document.getElementById("loginbox").style.display = 'none';
-}
-
-///////////////////////////////
-
-var pong_rosterContent = {};
-
-function pong_rosterAdded(jid, show, display, status)
-{  
-    pong_rosterContent[jid] = { 'jid':jid, 'show':show, 'display':display, 'status':status };
-    
-    pong_rosterRefresh();
-}
-function pong_rosterRemoved(jid)
-{
-    delete pong_rosterContent[jid];
-    pong_rosterRefresh();
-}
-function pong_rosterRefresh()
-{
-    var roster = "<div style='text-align: center;'><div style='height: 24px;'></div></div>";
-    var atLeastOneUserOnline = false;
-    for(var jid in pong_rosterContent)
-    {
-        var entry = pong_rosterContent[jid];
-        var supportsPong = pong_rosterContactSupportsPong(jid);
-
-        roster += "<div class='contact " + entry['show'];
-        if(supportsPong)
-            roster += " supportsPong ";
-        roster += "' ";
-        if(supportsPong)
-            roster += " onclick=\"pong_invite('" + jid + "')\" ";
-        roster +=">";
-        
-        roster += entry["display"];
-        if(supportsPong)
-        {
-            roster += " *** ";
-        }
-        roster += "</div>";
-        if(entry['show'] != 'unavailable')
-            atLeastOneUserOnline = true;
-    }
-    if(!atLeastOneUserOnline)
-    {
-        roster += "<div style='text-align: center;'>noone online</div>";
-    }
-    document.getElementById('roster').innerHTML = roster;
-
-}
-
-function pong_rosterContactSupportsPong(jid)
-{
-    var presences = zxmpp.getPresencesForBareJid(jid);
-    var supportsPong = false;
-
-    for (var resource in presences)
-    {
-        var presence = presences[resource];
-        if(presence.caps && presence.caps.supports("http://tvzpong.vucica.net/protocol/"))
-            supportsPong = true;
-    }
-    return supportsPong;
-}
-
-function pong_rosterContactResourcesSupportingPong(jid)
-{
-    var presences = zxmpp.getPresencesForBareJid(jid.split("/")[0]);
-    var supportsPong = [];
-    
-    for (var resource in presences)
-    {
-        if(presences[resource].caps && presences[resource].caps.supports("http://tvzpong.vucica.net/protocol/"))
-        {
-            supportsPong.push(resource);
-        }
-    }
-    return supportsPong;
-}
-
-
-////////////////////////////////
-var pong_talkTo = false;
-function pong_invite(jid)
-{
-    var resources = pong_rosterContactResourcesSupportingPong(jid);
-    
-    for(var resourceId in resources)
-    {
-        var resource = resources[resourceId];
-        var packet = new zxmpp.packet(zxmpp);
-        var message = new zxmpp.stanzaMessage(zxmpp);
-        message.appendToPacket(packet, zxmpp.fullJid, jid + "/" + resource, "chat", false); // pass no body
-        var invitationNode = packet.xml.createElementNS("http://tvzpong.vucica.net/protocol/", "invitation");
-        packet.messageXML.appendChild(invitationNode);
-        packet.send("poll");
-    }
-}
-function pong_sendData(jid, data)
-{
-    //var resources = tuntap_rosterContactResourcesSupportingTunTap(jid);
-    
-    //for(var resourceId in resources)
-    {
-        //var resource = resources[resourceId];
-        var packet = new zxmpp.packet(zxmpp);
-        var message = new zxmpp.stanzaMessage(zxmpp);
-        message.appendToPacket(packet, zxmpp.fullJid, jid, "chat");// pass no body
-        var dataNode = packet.xml.createElementNS("http://tvzpong.vucica.net/protocol/", "data");
-
-        var dataText = packet.xml.createTextNode(data);
-        dataNode.appendChild(dataText);
-
-        packet.messageXML.appendChild(dataNode);
-        packet.send("poll");
-    }
-}
-
-///////////////////////////////
-
-function pongZXMPP_onConnectionTerminate(sender, code, humanreadable)
-{
-    alert("Disconnected.\n\Reason: " + humanreadable + "\n\nCode: " + code);
-}
-
-function pongZXMPP_onPresenceUpdate(sender, presence)
-{
-    var topPresence = sender.getTopPresenceForBareJid(presence.bareJid);
-    if(topPresence)
-    {
-        console.log(" - Updating " + topPresence.bareJid + " with " + topPresence.show + " and " + topPresence.status);
-        //				zxmppui.presenceUpdate(topPresence.bareJid, topPresence.show, false, topPresence.status);
-        
-        if(!pong_rosterContent[topPresence.bareJid])
-        {
-            //console.warn("!!!!! NO ROSTER ENTRY FOR " + topPresence.bareJid);
-            // Happens when getting report for ourselves.
-        }
-        else
-        {
-            pong_rosterContent[topPresence.bareJid].show = topPresence.show;
-            pong_rosterContent[topPresence.bareJid].status = topPresence.status;
-            pong_rosterRefresh();
-
-            
-        }
-
-    }
-    else
-    {
-        console.warn("no top presence for " + presence.bareJid);
-    }
-
-}
-function pongZXMPP_onRosterUpdate(sender, item)
-{
-    var presence = sender.getTopPresenceForBareJid(item.bareJid);
-    
-    if(item.subscription != "removed" && item.subscription != "none")
-    {
-        
-        var allowed = true;
-        if(allowed)
-        {
-            var presenceShow = presence ? presence.show : "unavailable";
-            var display = item.name ? item.name : item.bareJid.split('@')[0];
-            var presenceStatus = presence ? presence.status : "";
-            
-            var vcard = sender.vCards[item.bareJid];
-            if(vcard && vcard.fn)
-                display = vcard.fn;
-            
-            pong_rosterAdded(item.bareJid, presenceShow, display, presenceStatus);
-        }
-        
-        
-    }
-    else
-    {
-        pong_rosterRemoved(item.bareJid);
-    }
-}
-
-
-function pongZXMPP_onMessage(sender, messageStanza)
-{
-    if(messageStanza.body)
-    {
-        var text = messageStanza.body;
-        
-        if(messageStanza.type == "error")
-        {
-            text = "ERROR with message: " + text;
-        }
-        /*
-        zxmppZ_showNotification(undefined, messageStanza.from, text);
-         */
-    }
-    /*
-    <? if ($useZXMPPUI) { ?>
-        zxmppui.messageStanzaReceived(messageStanza);
-		<? } ?>
-    */
-    var xml = messageStanza.messageNode;
-    
-    if(xml && xml.childNodes) 
-    {
-        for(var i in xml.childNodes)
-        {
-            var child = xml.childNodes[i];
-            if(!child.nodeName) continue;
-            
-            this.zxmpp.util.easierAttrs(child);
-            switch(child.extendedNodeName)
-            {
-                    // handling of nonstandard messages
-                    
-                case "http://tvzpong.vucica.net/protocol/+invitation":
-                    /*
-                    var subdata = {};
-                    for(var j in child.childNodes)
-                    {
-                        var subChild = child.childNodes[j];
-                        if(!subChild.nodeName) continue;
-                        if(subChild.firstChild && subChild.firstChild.nodeValue)
-                            subdata[subChild.nodeName] = subChild.firstChild.nodeValue;
-                    }
-                    
-                    var fakeMessage = new Object();
-                    fakeMessage.from = "Evaluator"; //messageStanza.from.split("@")[0]";
-                    fakeMessage.body = "<br>Evaluacija zadatka " + subdata["problemname"] + " dovršena! Imate " + subdata["points"] + " bodova. <a target=\"_blank\" href=\"printablerun.php?app=evaluator&evlshw=log&user=<?=$account?>&file=" + subdata["problemname"] + "\">Log</a>";
-                    */
-                    /*
-                    zxmppZ_onMessage(sender, fakeMessage);
-                     */
-                    alert("Invitation from " + messageStanza.from);
-		    pong_talkTo = messageStanza.from;
-                    break;
-		case "http://tvzpong.vucica.net/protocol/+data":
-                    //net_handler(child.firstChild.nodeValue);
-                    break;
-            }
-        }
-    }
-}
-
-function pongZXMPP_onPacket(sender, packet)
-{
-    if(packet && packet.incomingStanza && packet.incomingStanza.iqXML)
-    {
-        for(var childId in packet.incomingStanza.iqXML.childNodes)
-        {
-            var child = packet.incomingStanza.iqXML.childNodes[childId];
-            
-            // handling of nonstandard non-message packets
-            
-            if(child.nodeName == "jingle")
-            {
-                // do whatever you want ;-)
-                
-                // note that the iq may or may not be handled, and 
-                // the error may or may not be dispatched already.
-                // act accordingly. this mechanism is not really a
-                // good way to implement jingle, considering zxmpp
-                // has already dispatched an iq error message.
-                
-            }
-        }
-        
-        // we may have received caps update.
-        // refresh roster!
-        pong_rosterRefresh();
-    }
-    
-
-}
-