jhylands

wf.js

Jun 24th, 2013
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //allow the die to roll only once
  2. var rolled=false;
  3. //get the game id from the php to the javascript
  4. function roll(gid){
  5. if(!rolled){
  6.                         var xmlhttp;
  7.                         if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
  8.                             xmlhttp=new XMLHttpRequest();
  9.                         }
  10.                         else{// code for IE6, IE5
  11.                             xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  12.                         }
  13.                             xmlhttp.onreadystatechange=function(){
  14.                             if (xmlhttp.readyState==4 && xmlhttp.status==200){
  15.                                                                 var resp = xmlhttp.responseText;
  16.                                 document.getElementById("die").innerHTML=resp;
  17.                                     senddata(resp);
  18.                                                         }
  19.                         }
  20.                         xmlhttp.open("GET","die.php" ,true);
  21.                         xmlhttp.send();
  22.                                                 
  23.                                                 rolled=true;
  24.                                                 }
  25.                    
  26. }
  27. function nextgo(gid){
  28.                         var xmlhttp;
  29.                         if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
  30.                             xmlhttp=new XMLHttpRequest();
  31.                         }
  32.                         else{// code for IE6, IE5
  33.                             xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  34.                         }
  35.                             xmlhttp.onreadystatechange=function(){
  36.                             if (xmlhttp.readyState==4 && xmlhttp.status==200){
  37.                               var resp = xmlhttp.responseText;
  38.                                 rolled=false;
  39.                             }
  40.                         }
  41.                         xmlhttp.open("GET","nextgo.php?gid=" + gid ,true);
  42.                         xmlhttp.send();
  43. }     
  44. function moveme(gid,uid,roll){
  45. var req = "move.php?gid=" + gid  + "&uid=" + uid + "&roll=" + roll;
  46. //alert(req);
  47.                         var xmlhttp;
  48.                         if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
  49.                             xmlhttp=new XMLHttpRequest();
  50.                         }
  51.                         else{// code for IE6, IE5
  52.                             xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  53.                         }
  54.                             xmlhttp.onreadystatechange=function(){
  55.                             if (xmlhttp.readyState==4 && xmlhttp.status==200){
  56.                               var resp = xmlhttp.responseText;
  57.                                 getpos(gid);
  58.                             }
  59.                         }
  60.                         xmlhttp.open("GET",req,true);
  61.                         xmlhttp.send();
  62. }    
  63. //global variable lines, holds each player's position in case of update
  64. var lines;
  65. function getpos(gid){
  66. var xmlhttp;
  67. if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
  68.     xmlhttp=new XMLHttpRequest();
  69. }else{// code for IE6, IE5
  70.     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  71. }
  72. xmlhttp.onreadystatechange=function(){
  73.     if (xmlhttp.readyState==4 && xmlhttp.status==200){
  74.         //returns a list of positions split by comma
  75.         var resp = xmlhttp.responseText;
  76.         //if this is the first time the function is called then inisiate lines
  77.         if(lines ==null){
  78.             lines = resp.split(",");
  79.             //start construction of players in their positions
  80.             drwplyrs(lines);
  81.         }else{
  82.         //otherwise update positions
  83.         var income;
  84.         income = resp.split(",");
  85.         //start construction of players in their positions
  86.         drwplyrs(income);
  87.         clrplyrs(income);
  88.         lines = income;
  89.         }//end of else line != null
  90.     } //end of ajax returned
  91. }//end of ajax onready state
  92. xmlhttp.open("GET","getpos.php?gid=" + gid ,true);
  93. xmlhttp.send();
  94. }//end of getpos function
  95. //variable to store the PID -> FBID
  96. var FBID;
  97. //the start of a function that gets the FBID of a user and adds it to a locally stored array so profile pics can be referenced to when the game finds changes in the position on the board
  98. function FBIDret(gid){
  99. var xmlhttp;
  100. if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
  101. xmlhttp=new XMLHttpRequest();
  102. }
  103. else{// code for IE6, IE5
  104. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  105. }
  106. xmlhttp.onreadystatechange=function(){
  107. if (xmlhttp.readyState==4 && xmlhttp.status==200){
  108. var resp = xmlhttp.responseText;
  109. FBID = resp.split(",");
  110. getpos(gid);
  111. }
  112. }
  113. xmlhttp.open("GET","scripts/pidtoid.php?gid=" + gid ,true);
  114. xmlhttp.send();
  115. }
  116. //global array to store the property names to save time when updating player positions
  117. var propnames;
  118. //filling the array above
  119. function getPropNames(){
  120. var xmlhttp;
  121. if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
  122.     xmlhttp=new XMLHttpRequest();
  123. }else{// code for IE6, IE5
  124.     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  125. }
  126. xmlhttp.onreadystatechange=function(){
  127.     if (xmlhttp.readyState==4 && xmlhttp.status==200){
  128.         var resp = xmlhttp.responseText;
  129.         //returns a list of positions split by comma
  130.         propnames = resp.split(",");
  131.     }
  132. }
  133. xmlhttp.open("GET","scripts/getpropertylist.php",true);
  134. xmlhttp.send();
  135. }
  136. //function to update the text on properties when players move around the board
  137. function chan(propertyID,text){
  138. if(text==0){
  139. text = propnames[propertyID];
  140. }
  141. var x = document.getElementById("P" + propertyID);
  142. //check for default text
  143. x.innerHTML = text;
  144. }
  145. //find a string in an array
  146. function doesListCont(needle,hay){
  147. var contains;
  148. contains = 0;
  149. for (i=0;i<hay.length;i++){
  150.     if(needle==hay[i]){
  151.         contains = 1;
  152.     }
  153. }
  154. return contains;
  155. }
  156. //draw people in the right places
  157. function drwplyrs(income){
  158. var ardd; // string of all the lines[] that has alredy been used in past builds
  159. ardd = ".";
  160. var stri;
  161. for (i=0;i<income.length-1;i++){
  162. stri="";
  163. if(ardd.indexOf("." + i + ".") == -1){
  164.     for (n=0;n<income.length-1;n++){
  165.         if(income[n]==income[i]){
  166.         //alert(income[n] + ":n=>" + n + "," + income[i] + ":i=>" + i);
  167.         stri = stri + '<img class="profileimage" name="" src="https://graph.facebook.com/' + FBID[n] + '/picture" width="25" height="25" alt="">';
  168.         ardd = ardd + n + ".";
  169.         }
  170.         chan(income[i],stri);
  171.     }//end for(n)
  172. }
  173. }//end for(i)
  174. }//end of drwplyrs function
  175. //clear the squars in which people has exited
  176. function clrplyrs(income){
  177. //for each player
  178. for (n=0;n<income.length-1;n++){
  179. //alert(doesListCont(lines[n],income) + "," + lines[n] + "," +i); // debugging code
  180. //if there was someone I and now there isn't in position lines[i]
  181.     if(doesListCont(lines[n],income) == 0){
  182.         if(lines[n]!=null){
  183.         chan(lines[n],0);
  184.         }else{
  185.         //lines[n] is null this shouldn't happen :/
  186.         }
  187.     }
  188. }
  189. }
  190. //variable to stop polling twice at once
  191. var inpoll;
  192. //variable to hold data
  193. var database = new Array();
  194. function poll(gid){
  195.     //state that polling has started
  196.     inpoll=true;
  197.     //send request
  198.         var xmlhttp;
  199.     if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
  200.     xmlhttp=new XMLHttpRequest();
  201.     }
  202.     else{// code for IE6, IE5
  203.         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  204.     }
  205.     xmlhttp.onreadystatechange=function(){
  206.     if (xmlhttp.readyState==4 && xmlhttp.status==200){
  207.         var resp;
  208.         resp=xmlhttp.responseText;
  209.         var entries;
  210.         entries = resp.split(";");
  211.         for (i=0;i<entries.length;i++){
  212.             database[i] = entries[i].split(",");
  213.         }
  214.         //state that polling has finished
  215.         inpoll=false;
  216.     }
  217.     }
  218.     xmlhttp.open("GET","scripts/poll.php?gid=" + gid,true);
  219.     xmlhttp.send();
  220. }
Advertisement
Add Comment
Please, Sign In to add comment