David Jones avatar David Jones committed 165b101

adding updates.

Comments (0)

Files changed (3)

create_map_canvas.py

+from PlanetWars import *
+
+template = """
+<html>
+<canvas width="600" height="600" id="map"></canvas>
+<script>
+window.onload = function() {
+   planets = %s;
+   Map.draw_all(planets);
+}
+</script>
+<script src="map.js"></script>
+</html>
+"""
+
+
+def create_canvas(input_lines, output_file):
+    pw = PlanetWars(input_lines)
+    width = 600
+    height = 600
+    planets = ["{x: %f, y: %f, idx: %d, rate: %d}" %
+               (p.x, p.y, p.planet_id, p.growth_rate)
+               for p in pw.planets]
+
+    s = "[%s]" % (",\n    ".join(planets))
+
+
+    of = open(output_file, "w")
+    of.write(template % s)
+
+
+import sys
+
+def main(argv=None):
+    if not argv:
+        argv = sys.argv
+    if len(argv) < 3:
+        print >>sys.stderr, "usage %s input_file output_file" % sys.argv[0]
+    args = argv[1:]
+    input_lines = open(args[0]).readlines()
+    create_canvas(input_lines,args[1])
+    return 0
+
+
+if __name__ == "__main__":
+    import sys
+    lines = open(sys.argv[1]).readlines()
+    outfile = sys.argv[2]
+    create_canvas(lines, outfile)
+    
+
+
+Array.map = function (f) {
+   var result = [],
+    i = 0;
+    for(i = 0; i < result.length; i += 1) {
+        result[i] = f(this[i]);
+    }
+
+}
+
+var Map = {
+    fold : function(f, init, data) {
+        var i = 0,
+        result = init;
+        for(i = 0; i < data.length; i += 1) {
+            result = f(result, data[i]);
+        }
+        return result;
+    },
+    draw_all: function (planets) {
+        var canvas = document.getElementById("map"),
+        context = canvas.getContext("2d"),
+        size = canvas.width,
+        i = 0,
+        edge = 80,
+        scale = 0,
+        rate_scale = 15,
+        max_value = 0,
+        tmp,
+        draw_planet = function (planet, context) {
+            var x = planet.x * scale + edge,
+            y = size - (planet.y * scale + edge),
+            idx = planet.idx,
+            radius = Math.sqrt(planet.rate) * rate_scale;
+            
+            context.beginPath();
+            context.arc(x, y, radius, 0, 2*Math.PI, false);
+            context.stroke();
+            context.font = "12pt bold sans";
+            context.textAlign = "center";
+            context.textBaseline = "middle";
+            context.strokeStyle = "#000";
+            context.fillText(idx, x, y);
+        };
+        tmp = planets.map(function (p) { return p.x; });
+        max_value = Map.fold(Math.max, 0, tmp);
+        tmp = planets.map(function (p) {return p.y; });
+        max_value = Math.max(max_value, Map.fold(Math.max, 0, tmp));
+        scale = (size - 2 * edge) * 1.0 / max_value;
+        for(i = 0; i < planets.length; i += 1) {
+            draw_planet(planets[i], context);
+        }
+        
+    }
+}
         print "played %d of %d games" % (count, options.num_games)
         print "time is now: %s, waiting %d seconds" % (time.asctime(), wait)
         time.sleep(wait)
+        print "sleeping 2 more seconds..."
+        time.sleep(2)
         
                       
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.