private static Entity ensurePlayerExists(String username, DatastoreService store) { Query q = new Query(Transport.PLAYER) .setFilter(new Query.FilterPredicate("identity", Query.FilterOperator.EQUAL, username)); Entity player = store.prepare(q).asSingleEntity(); if (player == null) { Long x = 10l; Long y = 10l; player = new Entity(Transport.PLAYER); player.setProperty("identity", username); player.setProperty("speed", 4); PointSet points = new PointSet(20, 20); points.add(new Transport.Point(x, y)); player.setProperty("path", new Gson().toJson(new Transport.Points(points))); store.put(player); QueueFactory.getDefaultQueue().add(withUrl("/tick").param("identity", username)); } return player; }
private static Entity ensureMapExists(Entity player, DatastoreService store) { Query mq = new Query(Transport.MAP); Entity map = store.prepare(mq).asSingleEntity(); Random rand = new Random(); if (map == null) { map = new Entity(Transport.MAP); String mapId = "sandbox"; int width = 20; int height = 20; map.setProperty("width", width); map.setProperty("height", height); map.setProperty("identity", mapId); store.put(map); for (int j = 0; j < 8; j++) { int sx = width / 2; int sy = height / 2; for (int i = 0; i < 100; i++) { switch (rand.nextInt(4)) { case 0: sx++; break; case 1: sy++; break; case 2: sx--; break; case 3: sy--; break; } generateScenery(mapId, sx, sy, 10, 10, store); } } player.setProperty("map", mapId); store.put(player); } return map; }