Revision: 5267
Author:   [email protected]
Date:     Mon Feb  4 19:39:29 2013
Log:      initial support for random matches

http://code.google.com/p/google-caja/source/detail?r=5267

Added:
 /trunk/src/com/google/caja/demos/capman/js/jquery.csv-0.71.min.js
Modified:
 /trunk/src/com/google/caja/demos/capman/css/capman.css
 /trunk/src/com/google/caja/demos/capman/index.html
 /trunk/src/com/google/caja/demos/capman/js/pacman.js

=======================================
--- /dev/null
+++ /trunk/src/com/google/caja/demos/capman/js/jquery.csv-0.71.min.js Mon Feb 4 19:39:29 2013
@@ -0,0 +1,73 @@
+
+RegExp.escape=function(s){return s.replace(/[-\/\\^$*+?.()| [\]{}]/g,'\\$&');};(function($){'use strict' +$.csv={defaults:{separator:',',delimiter:'"',headers:true},hooks:{castToScalar:function(value,state){var hasDot=/\./;if(isNaN(value)){return value;}else{if(hasDot.test(value)){return parseFloat(value);}else{var integer=parseInt(value);if(isNaN(integer)){return null;}else{return integer;}}}}},parsers:{parse:function(csv,options){var separator=options.separator;var delimiter=options.delimiter;if(!options.state.rowNum){options.state.rowNum=1;}
+if(!options.state.colNum){options.state.colNum=1;}
+var data=[];var entry=[];var state=0;var value=''
+var exit=false;function endOfEntry(){state=0;value='';if(options.start&&options.state.rowNum<options.start){entry=[];options.state.rowNum++;options.state.colNum=1;return;} +if(options.onParseEntry===undefined){data.push(entry);}else{var hookVal=options.onParseEntry(entry,options.state);if(hookVal!==false){data.push(hookVal);}}
+entry=[];if(options.end&&options.state.rowNum>=options.end){exit=true;}
+options.state.rowNum++;options.state.colNum=1;}
+function endOfValue(){if(options.onParseValue===undefined){entry.push(value);}else{var hook=options.onParseValue(value,options.state);if(hook!==false){entry.push(hook);}}
+value='';state=0;options.state.colNum++;}
+var escSeparator=RegExp.escape(separator);var escDelimiter=RegExp.escape(delimiter);var match=/(D|S|\n|\r| [^DS\r\n]+)/;var matchSrc=match.source;matchSrc=matchSrc.replace(/S/g,escSeparator);matchSrc=matchSrc.replace(/D/g,escDelimiter);match=RegExp(matchSrc,'gm');csv.replace(match,function(m0){if(exit){return;}
+switch(state){case 0:if(m0===separator){value+='';endOfValue();break;}
+if(m0===delimiter){state=1;break;}
+if(m0==='\n'){endOfValue();endOfEntry();break;}
+if(/^\r$/.test(m0)){break;}
+value+=m0;state=3;break;case 1:if(m0===delimiter){state=2;break;}
+value+=m0;state=1;break;case 2:if(m0===delimiter){value+=m0;state=1;break;}
+if(m0===separator){endOfValue();break;}
+if(m0==='\n'){endOfValue();endOfEntry();break;}
+if(/^\r$/.test(m0)){break;}
+throw new Error('CSVDataError: Illegal State [Row:'+options.state.rowNum+'][Col:'+options.state.colNum+']');case 3:if(m0===separator){endOfValue();break;}
+if(m0==='\n'){endOfValue();endOfEntry();break;}
+if(/^\r$/.test(m0)){break;}
+if(m0===delimiter){throw new Error('CSVDataError: Illegal Quote [Row:'+options.state.rowNum+'][Col:'+options.state.colNum+']');} +throw new Error('CSVDataError: Illegal Data [Row:'+options.state.rowNum+'][Col:'+options.state.colNum+']');default:throw new Error('CSVDataError: Unknown State [Row:'+options.state.rowNum+'][Col:'+options.state.colNum+']');}});if(entry.length!==0){endOfValue();endOfEntry();} +return data;},splitLines:function(csv,options){var separator=options.separator;var delimiter=options.delimiter;if(!options.state.rowNum){options.state.rowNum=1;} +var entries=[];var state=0;var entry='';var exit=false;function endOfLine(){state=0;if(options.start&&options.state.rowNum<options.start){entry='';options.state.rowNum++;return;} +if(options.onParseEntry===undefined){entries.push(entry);}else{var hookVal=options.onParseEntry(entry,options.state);if(hookVal!==false){entries.push(hookVal);}}
+entry='';if(options.end&&options.state.rowNum>=options.end){exit=true;}
+options.state.rowNum++;}
+var escSeparator=RegExp.escape(separator);var escDelimiter=RegExp.escape(delimiter);var match=/(D|S|\n|\r| [^DS\r\n]+)/;var matchSrc=match.source;matchSrc=matchSrc.replace(/S/g,escSeparator);matchSrc=matchSrc.replace(/D/g,escDelimiter);match=RegExp(matchSrc,'gm');csv.replace(match,function(m0){if(exit){return;}
+switch(state){case 0:if(m0===separator){entry+=m0;state=0;break;}
+if(m0===delimiter){entry+=m0;state=1;break;}
+if(m0==='\n'){endOfLine();break;}
+if(/^\r$/.test(m0)){break;}
+entry+=m0;state=3;break;case 1:if(m0===delimiter){entry+=m0;state=2;break;}
+entry+=m0;state=1;break;case 2:var prevChar=entry.substr(entry.length-1);if(m0===delimiter&&prevChar===delimiter){entry+=m0;state=1;break;}
+if(m0===separator){entry+=m0;state=0;break;}
+if(m0==='\n'){endOfLine();break;}
+if(m0==='\r'){break;}
+throw new Error('CSVDataError: Illegal state [Row:'+options.state.rowNum+']');case 3:if(m0===separator){entry+=m0;state=0;break;}
+if(m0==='\n'){endOfLine();break;}
+if(m0==='\r'){break;}
+if(m0===delimiter){throw new Error('CSVDataError: Illegal quote [Row:'+options.state.rowNum+']');} +throw new Error('CSVDataError: Illegal state [Row:'+options.state.rowNum+']');default:throw new Error('CSVDataError: Unknown state [Row:'+options.state.rowNum+']');}});if(entry!==''){endOfLine();} +return entries;},parseEntry:function(csv,options){var separator=options.separator;var delimiter=options.delimiter;if(!options.state.rowNum){options.state.rowNum=1;}
+if(!options.state.colNum){options.state.colNum=1;}
+var entry=[];var state=0;var value='';function endOfValue(){if(options.onParseValue===undefined){entry.push(value);}else{var hook=options.onParseValue(value,options.state);if(hook!==false){entry.push(hook);}}
+value='';state=0;options.state.colNum++;}
+if(!options.match){var escSeparator=RegExp.escape(separator);var escDelimiter=RegExp.escape(delimiter);var match=/(D|S|\n|\r| [^DS\r\n]+)/;var matchSrc=match.source;matchSrc=matchSrc.replace(/S/g,escSeparator);matchSrc=matchSrc.replace(/D/g,escDelimiter);options.match=RegExp(matchSrc,'gm');} +csv.replace(options.match,function(m0){switch(state){case 0:if(m0===separator){value+='';endOfValue();break;}
+if(m0===delimiter){state=1;break;}
+if(m0==='\n'||m0==='\r'){break;}
+value+=m0;state=3;break;case 1:if(m0===delimiter){state=2;break;}
+value+=m0;state=1;break;case 2:if(m0===delimiter){value+=m0;state=1;break;}
+if(m0===separator){endOfValue();break;}
+if(m0==='\n'||m0==='\r'){break;}
+throw new Error('CSVDataError: Illegal State [Row:'+options.state.rowNum+'][Col:'+options.state.colNum+']');case 3:if(m0===separator){endOfValue();break;}
+if(m0==='\n'||m0==='\r'){break;}
+if(m0===delimiter){throw new Error('CSVDataError: Illegal Quote [Row:'+options.state.rowNum+'][Col:'+options.state.colNum+']');} +throw new Error('CSVDataError: Illegal Data [Row:'+options.state.rowNum+'][Col:'+options.state.colNum+']');default:throw new Error('CSVDataError: Unknown State [Row:'+options.state.rowNum+'][Col:'+options.state.colNum+']');}});endOfValue();return entry;}},toArray:function(csv,options,callback){var options=(options!==undefined?options:{});var config={};config.callback=((callback!==undefined&&typeof(callback)==='function')?callback:false);config.separator='separator'in options?options.separator:$.csv.defaults.separator;config.delimiter='delimiter'in options?options.delimiter:$.csv.defaults.delimiter;var state=(options.state!==undefined?options.state:{});var options={delimiter:config.delimiter,separator:config.separator,onParseEntry:options.onParseEntry,onParseValue:options.onParseValue,state:state} +var entry=$.csv.parsers.parseEntry(csv,options);if(!config.callback){return entry;}else{config.callback('',entry);}},toArrays:function(csv,options,callback){var options=(options!==undefined?options:{});var config={};config.callback=((callback!==undefined&&typeof(callback)==='function')?callback:false);config.separator='separator'in options?options.separator:$.csv.defaults.separator;config.delimiter='delimiter'in options?options.delimiter:$.csv.defaults.delimiter;var data=[];var options={delimiter:config.delimiter,separator:config.separator,onParseEntry:options.onParseEntry,onParseValue:options.onParseValue,start:options.start,end:options.end,state:{rowNum:1,colNum:1}};data=$.csv.parsers.parse(csv,options);if(!config.callback){return data;}else{config.callback('',data);}},toObjects:function(csv,options,callback){var options=(options!==undefined?options:{});var config={};config.callback=((callback!==undefined&&typeof(callback)==='function')?callback:false);config.separator='separator'in options?options.separator:$.csv.defaults.separator;config.delimiter='delimiter'in options?options.delimiter:$.csv.defaults.delimiter;config.headers='headers'in options?options.headers:$.csv.defaults.headers;options.start='start'in options?options.start:1;if(config.headers){options.start++;}
+if(options.end&&config.headers){options.end++;}
+var lines=[];var data=[];var options={delimiter:config.delimiter,separator:config.separator,onParseEntry:options.onParseEntry,onParseValue:options.onParseValue,start:options.start,end:options.end,state:{rowNum:1,colNum:1},match:false};var headerOptions={delimiter:config.delimiter,separator:config.separator,start:1,end:1,state:{rowNum:1,colNum:1}} +var headerLine=$.csv.parsers.splitLines(csv,headerOptions);var headers=$.csv.toArray(headerLine[0],options);var lines=$.csv.parsers.splitLines(csv,options);options.state.colNum=1;if(headers){options.state.rowNum=2;}else{options.state.rowNum=1;} +for(var i=0,len=lines.length;i<len;i++){var entry=$.csv.toArray(lines[i],options);var object={};for(var j in headers){object[headers[j]]=entry[j];}
+data.push(object);options.state.rowNum++;}
+if(!config.callback){return data;}else{config.callback('',data);}},fromArrays:function(arrays,options,callback){var options=(options!==undefined?options:{});var config={};config.callback=((callback!==undefined&&typeof(callback)==='function')?callback:false);config.separator='separator'in options?options.separator:$.csv.defaults.separator;config.delimiter='delimiter'in options?options.delimiter:$.csv.defaults.delimiter;config.escaper='escaper'in options?options.escaper:$.csv.defaults.escaper;config.experimental='experimental'in options?options.experimental:false;if(!config.experimental){throw new Error('not implemented');}
+var output=[];for(i in arrays){output.push(arrays[i]);}
+if(!config.callback){return output;}else{config.callback('',output);}},fromObjects2CSV:function(objects,options,callback){var options=(options!==undefined?options:{});var config={};config.callback=((callback!==undefined&&typeof(callback)==='function')?callback:false);config.separator='separator'in options?options.separator:$.csv.defaults.separator;config.delimiter='delimiter'in options?options.delimiter:$.csv.defaults.delimiter;config.experimental='experimental'in options?options.experimental:false;if(!config.experimental){throw new Error('not implemented');}
+var output=[];for(i in objects){output.push(arrays[i]);}
+if(!config.callback){return output;}else{config.callback('',output);}}};$.csvEntry2Array=$.csv.toArray;$.csv2Array=$.csv.toArrays;$.csv2Dictionary=$.csv.toObjects;})(jQuery);
=======================================
--- /trunk/src/com/google/caja/demos/capman/css/capman.css Mon Feb 4 14:38:37 2013 +++ /trunk/src/com/google/caja/demos/capman/css/capman.css Mon Feb 4 19:39:29 2013
@@ -19,6 +19,23 @@
   margin-bottom: 0.5em;
   margin-right: 0.5em;
 }
+
+#match-buttons {
+  list-style: none;
+  margin: 2em 0 0 0;
+}
+#match-buttons li a {
+  width: 3em;
+  margin-bottom: 0.5em;
+  margin-right: 0.5em;
+}
+#match-buttons li.label b {
+  width: 3em;
+  display: inline-block;
+  margin: 0 0.5em 0.5em 0;
+  text-align: center;
+}
+
 #pacman {
   margin: 0px;
 }
@@ -69,3 +86,7 @@
   border-top: 1px solid #5e93ff;
   padding-top: 1em;
 }
+
+#match-form label {
+  font-weight: normal;
+}
=======================================
--- /trunk/src/com/google/caja/demos/capman/index.html Mon Feb 4 16:53:51 2013 +++ /trunk/src/com/google/caja/demos/capman/index.html Mon Feb 4 19:39:29 2013
@@ -12,11 +12,12 @@
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js" type="text/javascript"></script> <link href="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />

-<script src="js/prettify.js"></script>
+  <script src="js/prettify.js"></script>
   <script src="//caja.appspot.com/caja.js"></script>
   <script src="js/pacman.js"></script>
   <script src="js/modernizr-1.5.min.js"></script>
   <script src="js/codemirror-compressed.js"></script>
+  <script src="js/jquery.csv-0.71.min.js"></script>

   <link rel="stylesheet" href="css/codemirror.css" type="text/css">
   <link rel="stylesheet" href="css/prettify.css" type="text/css">
@@ -58,8 +59,13 @@
<li><a href="#" class="maia-button maia-button-secondary" id="pause">Pause</a></li> <li><a href="#" class="maia-button maia-button-secondary" id="mute">Mute</a></li>
         </ul>
+        <ul id="match-buttons">
+          <li class="label"><b>Random Match</b></li>
+ <li><a href="javascript:;" class="maia-button" id="match-run">Run</a></bli> + <li><a href="javascript:;" class="maia-button maia-button-secondary" id="match-auto">Auto<a></li>
+        </ul>
       </div>
-
+
       <div class="maia-col-5">
         <div id="pacman"></div>
       </div>
@@ -93,6 +99,14 @@
         <button id="pacman-load" class="maia-button">load</button>
         <div id="pacman-code"></div>
       </form>
+      <h3>Random Match Configuration</h3>
+      <form id="match-form" onsubmit="return false">
+        <label>
+          <input id="match-data-key" type="text"
+                 value="0AiX0HQJhbQOddFJOS3VsZ0dGVGRfVWtxbUxob3c5OEE">
+          Spreadsheet Key
+        </label>
+      </form>
     </div>
     <div class="maia-col-4">
       <div class="maia-notification">
=======================================
--- /trunk/src/com/google/caja/demos/capman/js/pacman.js Mon Feb 4 16:53:51 2013 +++ /trunk/src/com/google/caja/demos/capman/js/pacman.js Mon Feb 4 19:39:29 2013
@@ -1197,6 +1197,9 @@
     return false;
   };

+  var pacmanCode;
+  var ghostCode;
+
   function init(wrapper, root, start, pause, mute, playerEditors) {
     var i, len, ghost,
         blockSize = wrapper.offsetWidth / 19,
@@ -1260,7 +1263,7 @@

     var pacmanEditor = playerEditors[1];
     var pacmanParent = pacmanEditor.parentNode;
-    var pacmanCode = CodeMirror(pacmanEditor, {
+    pacmanCode = CodeMirror(pacmanEditor, {
         value: defaultPacmanCode,
         mode:  "javascript",
         lineNumbers: true
@@ -1289,7 +1292,7 @@

     var ghostEditor = playerEditors[0];
     var ghostParent = ghostEditor.parentNode;
-    var ghostCode = CodeMirror(ghostEditor, {
+    ghostCode = CodeMirror(ghostEditor, {
       value: defaultGhostCode,
       mode:  "javascript",
       lineNumbers: true
@@ -1320,6 +1323,9 @@
     $(pause).click(togglePause);
     $(mute).click(toggleSound);

+    $('#match-run').click(matchRun);
+    $('#match-auto').click(matchAuto);
+
     map.draw(ctx);
     dialog("Loading ...");

@@ -1343,6 +1349,60 @@
     load(audio_files, function() { loaded(); });
   };

+  function matchRun() {
+ var url = 'https://docs.google.com/spreadsheet/pub?output=csv&single=true';
+    var key = $('#match-data-key').val();
+    if (!key) { console.error('No match data key?'); return; }
+    key = encodeURIComponent(key);
+    $.ajax(url + '&gid=0&key=' + key, {
+      datatype: 'text', fail: loadFail
+    }).done(function (capmanData) {
+      $.ajax(url + '&gid=1&key=' + key, {
+        datatype: 'text', fail: loadFail
+      }).done(function (ghostData) {
+        matchRunFromData(capmanData, ghostData);
+      });
+    });
+  }
+
+  function loadFail(xhr, status, message) {
+    console.log('ajax failed', xhr, status, message);
+  }
+
+  function matchRunFromData(capmanData, ghostData) {
+    capmanData = $.csv.toArrays(capmanData);
+    ghostData = $.csv.toArrays(ghostData);
+    var capmanEntry = matchPick(capmanData);
+    var ghostEntry = matchPick(ghostData);
+    $.ajax(capmanEntry[0], {
+      datatype: 'text', fail: loadFail
+    }).done( function (capmanText) {
+      $('#pacman-url').val(capmanEntry[0]);
+      pacmanCode.setValue(capmanText);
+      $.ajax(ghostEntry[0], {
+        datatype: 'text', fail: loadFail
+      }).done(function (ghostText) {
+        $('#ghost-url').val(ghostEntry[0]);
+        ghostCode.setValue(ghostText);
+        startNewGame();
+      });
+    });
+  }
+
+  function matchPick(entries) {
+    var a = [];
+    for (var i = 0; i < entries.length; i++) {
+      if (/^https?:/i.test(entries[i][0])) {
+        a.push(entries[i]);
+      }
+    }
+    return a[Math.floor(a.length * Math.random())];
+  }
+
+  function matchAuto() {
+    console.log('matchAuto stub XXX');
+  }
+
   function load(arr, callback) {
     if (arr.length === 0) {
         callback();

--

--- You received this message because you are subscribed to the Google Groups "Google Caja Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to