Commits

Ivan Vučica committed 19fd2dd

Playfield. Starting the game. Paddle position transmitted over network.

Comments (0)

Files changed (5)

 <script src="zxmpp/jquery.min.js"></script>
 <script src="pong-network.js"></script>
 <script src="pong-invitations.js"></script>
+<script src="pong-game.js"></script>
 <?php
 if(file_exists("defaults.php")) include_once 'defaults.php';
 zxmpp_headers();
 </div>
 
 <div class="roster" id="roster">
-    
+</div>
+
+<div class="playfield" id="playfield">
+<div id="paddle1" class="paddle"></div>
+<div id="paddle2" class="paddle"></div>
 </div>
 
 </body>
+var pongGame_talkTo = false;
+
+function pongGame_start(fullJid)
+{
+    pongGame_talkTo = fullJid;
+    $('#invitations').slideUp().hide();
+    $('#roster').slideUp().hide();
+    $('#playfield').fadeIn();
+    
+    window.addEventListener("mousemove", pongGame_onMouseMove, false);
+    
+    
+    pongGame_sendUpdate();
+}
+
+function pongGame_sendUpdate()
+{
+    var packet = new zxmpp.packet(zxmpp);
+    
+    var invitationNode = packet.xml.createElementNS("http://tvzpong.vucica.net/protocol/", "update");
+
+    var iq = new zxmpp.stanzaIq(zxmpp);
+    iq.appendIqToPacket(packet, /*id base name*/"pongupdate", "set", pongGame_talkTo);
+    
+    var content = $('#paddle1')[0].style.left;
+    content = content.substr(0, content.length - 2); // remove 'px'
+    var contentnode = packet.xml.createTextNode(content);
+    invitationNode.appendChild(contentnode);
+    
+    var replyHandler = function pongInvitation_sendDemandReplyHandler(zxmpp, original, response)
+    {
+        if(response.type == "error")
+            console.error("Got error");
+        else if (response.type == "result")
+        {
+            pongGame_sendUpdate();
+        }
+    };
+    
+    iq.onReply.push(replyHandler);
+    packet.iqXML.appendChild(invitationNode);
+    
+    packet.send("poll");
+
+}
+function pongGame_getUpdate(iqstanza, xml)
+{
+    pongGame_positionOpponentPaddle(xml.textContent);
+}
+function pongGame_onMouseMove(event)
+{
+    var newX = event['offsetX'] - $('#paddle1')[0].offsetWidth/2;
+    var newY = event['offsetY'];
+    
+    $('#paddle1')[0].style.left = newX + 'px';
+    //$('#paddle2')[0].style.left = newX + 'px';
+}
+function pongGame_positionOpponentPaddle(pos)
+{
+    $('#paddle2')[0].style.left = pos + 'px';
+}

pong-invitations.js

     
     zxmpp.setOwnPresence("dnd", "Playing TVZ Pong", 0);
 
-    pong_talkTo = fullJid;
-
+    pongGame_start(fullJid);
 }
 function pongInvitation_receiveDecline(fullJid)
 {
             
             zxmpp.setOwnPresence("dnd", "Playing TVZ Pong", 0);
 
-            pong_talkTo = fullJid;
+            pongGame_start(fullJid);
             
             for(var invite in pongInvitations)
             {
     zxmpp.addIqParser("invitation#http://tvzpong.vucica.net/protocol/", pongZXMPP_onIqInvitation);
     zxmpp.addIqParser("accept#http://tvzpong.vucica.net/protocol/", pongZXMPP_onIqAccept);
     zxmpp.addIqParser("decline#http://tvzpong.vucica.net/protocol/", pongZXMPP_onIqDecline);
+    zxmpp.addIqParser("update#http://tvzpong.vucica.net/protocol/", pongZXMPP_onIqUpdate);
 
     zxmpp.main(cfg, user, password);
     zxmpp.setOwnPresence("avail", "Working on TVZ Pong", 1);
     
     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';
+    //$('#roster').hide().slideDown();
+    //document.getElementById("loginbox").style.display = 'none';
+    $('#loginbox').slideUp().hide();
 }
 
 ///////////////////////////////
     packet.send("poll");
     return true;
 }
+function pongZXMPP_onIqUpdate(zxmpp, iqstanza, xml)
+{
+    pongGame_getUpdate(iqstanza, xml);
+    
+    // send confirmation
+    var packet = new zxmpp.packet(zxmpp);
+    var iq = new zxmpp.stanzaIq(zxmpp);
+    iq.appendIqToPacket(packet, /* id */false, /* type */ "result", /* destination */iqstanza.from, iqstanza.id);
+    packet.send("poll");
+    return true;
+}
 
 
 function pongZXMPP_onPacket(sender, packet)
     padding-bottom: 5px;
 }
 
+/*********** playfield ************/
 
+.playfield
+{
+    display: none;
+    width: 800px;
+    height: 400px;
+    background: #222;
+    margin-left: auto;
+    margin-right: auto;
+    
+    position: relative;
+}
+
+.playfield .paddle
+{
+    width: 100px;
+    height: 20px;
+    background-color: white;
+    position: absolute;
+}
+
+.playfield #paddle1
+{
+    bottom: 10px;
+}
+.playfield #paddle2
+{
+    top: 10px;
+}