def upgrade():
    upgrader = Upgrader()
    instance_list = json.loads(kv.get("instance-list", "[]"))
    for instance_id in instance_list:
        upgraded = False
        instance = json.loads(kv.get("instance-"+instance_id))
        schema_version = instance.get("schema_version", 0)
        while schema_version < 1:
            print("upgrading %s from %d to %d" % (instance_id, schema_version, schema_version+1))
            getattr(upgrader, "upgrade_%d_to_%d" % (schema_version, schema_version+1))(instance)
            schema_version = instance.get("schema_version", 0)
            upgraded = True
        if upgraded:
            kv.set("instance-"+instance_id, json.dumps(instance))
def upgrade():
    upgrader = Upgrader()
    instance_list = json.loads(kv.get("instance-list", "[]"))
    for instance_id in instance_list:
        upgraded = False
        instance = json.loads(kv.get("instance-" + instance_id))
        schema_version = instance.get("schema_version", 0)
        while schema_version < 1:
            print("upgrading %s from %d to %d" %
                  (instance_id, schema_version, schema_version + 1))
            getattr(upgrader, "upgrade_%d_to_%d" %
                    (schema_version, schema_version + 1))(instance)
            schema_version = instance.get("schema_version", 0)
            upgraded = True
        if upgraded:
            kv.set("instance-" + instance_id, json.dumps(instance))
Пример #3
0
    def get(self):
        instance_id = self.get_argument("instance_id")
        instance_json = kv.get("instance-" + instance_id, None)
        if instance_json is None:
            return self.send_error(404)
        instance = json.loads(instance_json)

        auth_header = self.request.headers.get("Authorization", None)
        host_header = self.request.headers.get("Host")
        if not auth_header:
            return self.fail_auth(instance_id)

        import base64
        username, password = base64.decodestring(
            auth_header.split(" ")[1].encode("ascii")).decode("utf-8").split(
                ":")

        if username not in ("blue", "red", "admin"):
            return self.fail_auth(instance_id)
        if instance[username + "_pw"] != password:
            return self.fail_auth(instance_id)

        self.render("templates/instance.html",
                    username=username,
                    instance_id=instance_id,
                    instance=instance,
                    host=host_header,
                    protocol=self.request.protocol)
Пример #4
0
    def get(self):
        instance_id = self.get_argument("instance_id")
        instance_json = kv.get("instance-" + instance_id, None)
        if instance_json is None:
            return self.send_error(404)
        instance = json.loads(instance_json)

        auth_header = self.request.headers.get("Authorization", None)
        host_header = self.request.headers.get("Host")
        if not auth_header:
            return self.fail_auth(instance_id)

        import base64

        username, password = base64.decodestring(auth_header.split(" ")[1].encode("ascii")).decode("utf-8").split(":")

        if username not in ("blue", "red", "admin"):
            return self.fail_auth(instance_id)
        if instance[username + "_pw"] != password:
            return self.fail_auth(instance_id)

        self.render(
            "templates/instance.html",
            username=username,
            instance_id=instance_id,
            instance=instance,
            host=host_header,
            protocol=self.request.protocol,
        )
Пример #5
0
    def handle_create_instance_request(self, msg):
        instance_id = kv.get("next_instance_id") or "1"
        kv.set("next_instance_id", base36encode(int(instance_id, 36) + 1))

        instance = {}
        instance["id"] = instance_id
        instance["schema_version"] = 1
        instance["mizname"] = msg["filename"]
        instance["mizhash"] = msg["md5hash"]
        if msg["no_passwords"]:
            instance["red_pw"] = ""
            instance["blue_pw"] = ""
            instance["admin_pw"] = ""
        else:
            instance["red_pw"] = util.makepw()
            instance["blue_pw"] = util.makepw()
            instance["admin_pw"] = util.makepw()
        instance["red_spectator_pw"] = util.makepw()
        instance["blue_spectator_pw"] = util.makepw()

        instance["data"] = msg["data"]

        for obj in instance["data"]["objects"].values():
            if "x" in obj and "z" in obj:
                lon, lat = dcs_proj(obj["z"], obj["x"], inverse=True)
                obj["lon"] = lon
                obj["lat"] = lat
                del obj["z"]
                del obj["x"]

        kv.set("instance-" + instance_id, json.dumps(instance))

        instance_list = json.loads(kv.get("instance-list", "[]"))
        instance_list.append(instance_id)

        kv.set("instance-list", json.dumps(instance_list))

        return {
            "success": True,
            "instance_id": instance_id,
            "red_pw": instance["red_pw"],
            "blue_pw": instance["blue_pw"],
            "red_spectator_pw": instance["red_spectator_pw"],
            "blue_spectator_pw": instance["blue_spectator_pw"],
            "admin_pw": instance["admin_pw"],
        }
Пример #6
0
    def handle_create_instance_request(self, msg):
        instance_id = kv.get("next_instance_id") or "1"
        kv.set("next_instance_id", base36encode(int(instance_id, 36) + 1))

        instance = {}
        instance["id"] = instance_id
        instance["schema_version"] = 1
        instance["mizname"] = msg["filename"]
        instance["mizhash"] = msg["md5hash"]
        if msg["no_passwords"]:
            instance["red_pw"] = ""
            instance["blue_pw"] = ""
            instance["admin_pw"] = ""
        else:
            instance["red_pw"] = util.makepw()
            instance["blue_pw"] = util.makepw()
            instance["admin_pw"] = util.makepw()
        instance["red_spectator_pw"] = util.makepw()
        instance["blue_spectator_pw"] = util.makepw()

        instance["data"] = msg["data"]

        for obj in instance["data"]["objects"].values():
            if "x" in obj and "z" in obj:
                lon, lat = dcs_proj(obj["z"], obj["x"], inverse=True)
                obj["lon"] = lon
                obj["lat"] = lat
                del obj["z"]
                del obj["x"]

        kv.set("instance-" + instance_id, json.dumps(instance))

        instance_list = json.loads(kv.get("instance-list", "[]"))
        instance_list.append(instance_id)

        kv.set("instance-list", json.dumps(instance_list))

        return {
            "success": True,
            "instance_id": instance_id,
            "red_pw": instance["red_pw"],
            "blue_pw": instance["blue_pw"],
            "red_spectator_pw": instance["red_spectator_pw"],
            "blue_spectator_pw": instance["blue_spectator_pw"],
            "admin_pw": instance["admin_pw"],
        }
Пример #7
0
    def handle_set_liveries_request(self, msg):
        instance = json.loads(kv.get("instance-" + msg["instance_id"]))
        admin_pw = msg["admin_pw"]

        # verify password
        if admin_pw != instance["admin_pw"]:
            return {"success": False, "error_msg": "Invalid password."}

        instance["data"]["liveries"] = msg["liveries"]
        kv.set("instance-" + msg["instance_id"], json.dumps(instance))

        return {"success": True}
Пример #8
0
    def handle_set_liveries_request(self, msg):
        instance = json.loads(kv.get("instance-" + msg["instance_id"]))
        admin_pw = msg["admin_pw"]

        # verify password
        if admin_pw != instance["admin_pw"]:
            return {"success": False, "error_msg": "Invalid password."}

        instance["data"]["liveries"] = msg["liveries"]
        kv.set("instance-" + msg["instance_id"], json.dumps(instance))

        return {"success": True}
Пример #9
0
    def handle_transaction_request(self, msg):
        instance = json.loads(kv.get("instance-" + self.instance_id))
        if not instance:
            return {"success": False, "error_msg": "instance does not exist."}

        if self.spectator_mode:
            return {"success": True, "transaction_applied": False}

        for key in msg["transaction"]["deleted_object_ids"]:
            if key not in instance["data"]["objects"]:
                return {"success": True, "transaction_applied": False, "log": "object already deleted: %s" % key}

        for key, value in msg["transaction"]["preconditions"].items():
            if key not in instance["data"]["objects"]:
                return {
                    "success": True,
                    "transaction_applied": False,
                    "log": "object to be updated does not exist: %s" % key,
                }
            if pformat(value) != pformat(instance["data"]["objects"][key]):
                pprint.pprint(value)
                pprint.pprint(instance["data"]["objects"][key])
                return {"success": True, "transaction_applied": False, "log": "precondition not met: %s" % key}

        for key, value in msg["transaction"]["updated_data"].items():
            instance["data"]["objects"][key] = msg["transaction"]["updated_data"][key]

        for key in msg["transaction"]["deleted_object_ids"]:
            del instance["data"]["objects"][key]

        instance["data"]["version"] += 1
        kv.set("instance-" + self.instance_id, json.dumps(instance))

        for ws in logged_in_websockets:
            if ws != self:
                try:
                    changeset = msg["transaction"]
                    changeset["version_after"] = instance["data"]["version"]
                    changeset = ws.filter_tx(changeset)
                    changeset_str = json.dumps({"type": "changeset", "changeset": changeset})
                    ws.write_message(changeset_str)
                except:
                    print("Error sending changeset.")
                    pass

        changeset = msg["transaction"]
        changeset["version_after"] = instance["data"]["version"]
        changeset = self.filter_tx(changeset)
        return {"success": True, "transaction_applied": True, "changeset": changeset}
Пример #10
0
    def handle_save_mission_request(self, msg):
        instance = json.loads(kv.get("instance-" + msg["instance_id"]))
        admin_pw = msg["admin_pw"]

        # verify password
        if admin_pw != instance["admin_pw"]:
            return {"success": False, "error_msg": "Invalid password."}

        data_copy = json.loads(json.dumps(instance["data"]))
        for obj in data_copy["objects"].values():
            if "lat" in obj and "lon" in obj:
                z, x = dcs_proj(obj["lon"], obj["lat"])
                obj["z"] = z
                obj["x"] = x

        return {"success": True, "data": data_copy, "filename": instance["mizname"], "md5hash": instance["mizhash"]}
Пример #11
0
    def handle_instance_info_request(self, msg):
        instance = json.loads(kv.get("instance-" + msg["instance_id"]))
        admin_pw = msg["admin_pw"]

        # verify password
        if admin_pw != instance["admin_pw"]:
            return {"success": False, "error_msg": "Invalid password."}

        return {
            "success": True,
            "instance_id": msg["instance_id"],
            "red_pw": instance["red_pw"],
            "blue_pw": instance["blue_pw"],
            "red_spectator_pw": instance["red_spectator_pw"],
            "blue_spectator_pw": instance["blue_spectator_pw"],
            "admin_pw": instance["admin_pw"],
        }
Пример #12
0
    def handle_instance_info_request(self, msg):
        instance = json.loads(kv.get("instance-" + msg["instance_id"]))
        admin_pw = msg["admin_pw"]

        # verify password
        if admin_pw != instance["admin_pw"]:
            return {"success": False, "error_msg": "Invalid password."}

        return {
            "success": True,
            "instance_id": msg["instance_id"],
            "red_pw": instance["red_pw"],
            "blue_pw": instance["blue_pw"],
            "red_spectator_pw": instance["red_spectator_pw"],
            "blue_spectator_pw": instance["blue_spectator_pw"],
            "admin_pw": instance["admin_pw"],
        }
Пример #13
0
    def handle_login_request(self, msg):
        global next_id_prefix_int

        instance_json = kv.get("instance-" + msg["instance_id"])
        if not instance_json:
            return {"success": False, "error_msg": "instance does not exist."}
        instance = json.loads(instance_json)
        if (
            instance[msg["coalition"] + "_pw"] == msg["password"]
            or instance[msg["coalition"] + "_spectator_pw"] == msg["password"]
        ):
            self.instance_id = msg["instance_id"]
            self.spectator_mode = instance[msg["coalition"] + "_spectator_pw"] == msg["password"]
            self.coalition = msg["coalition"]
            logged_in_websockets.append(self)
            id_prefix = base36encode(next_id_prefix_int) + "/"
            next_id_prefix_int += 1
            return {"success": True, "data": self.filter_data(instance["data"]), "id_prefix": id_prefix}
        else:
            return {"success": False, "error_msg": "invalid password."}
Пример #14
0
    def handle_save_mission_request(self, msg):
        instance = json.loads(kv.get("instance-" + msg["instance_id"]))
        admin_pw = msg["admin_pw"]

        # verify password
        if admin_pw != instance["admin_pw"]:
            return {"success": False, "error_msg": "Invalid password."}

        data_copy = json.loads(json.dumps(instance["data"]))
        for obj in data_copy["objects"].values():
            if "lat" in obj and "lon" in obj:
                z, x = dcs_proj(obj["lon"], obj["lat"])
                obj["z"] = z
                obj["x"] = x

        return {
            "success": True,
            "data": data_copy,
            "filename": instance["mizname"],
            "md5hash": instance["mizhash"]
        }
Пример #15
0
    def handle_login_request(self, msg):
        global next_id_prefix_int

        instance_json = kv.get("instance-" + msg["instance_id"])
        if not instance_json:
            return {"success": False, "error_msg": "instance does not exist."}
        instance = json.loads(instance_json)
        if instance[msg["coalition"]+"_pw"] == msg["password"] or \
           instance[msg["coalition"]+"_spectator_pw"] == msg["password"]:
            self.instance_id = msg["instance_id"]
            self.spectator_mode = (
                instance[msg["coalition"] +
                         "_spectator_pw"] == msg["password"])
            self.coalition = msg["coalition"]
            logged_in_websockets.append(self)
            id_prefix = base36encode(next_id_prefix_int) + "/"
            next_id_prefix_int += 1
            return {
                "success": True,
                "data": self.filter_data(instance["data"]),
                "id_prefix": id_prefix
            }
        else:
            return {"success": False, "error_msg": "invalid password."}
Пример #16
0
    def handle_transaction_request(self, msg):
        instance = json.loads(kv.get("instance-" + self.instance_id))
        if not instance:
            return {"success": False, "error_msg": "instance does not exist."}

        if self.spectator_mode:
            return {"success": True, "transaction_applied": False}

        for key in msg["transaction"]["deleted_object_ids"]:
            if key not in instance["data"]["objects"]:
                return {
                    "success": True,
                    "transaction_applied": False,
                    "log": "object already deleted: %s" % key
                }

        for key, value in msg["transaction"]["preconditions"].items():
            if key not in instance["data"]["objects"]:
                return {
                    "success": True,
                    "transaction_applied": False,
                    "log": "object to be updated does not exist: %s" % key
                }
            if pformat(value) != pformat(instance["data"]["objects"][key]):
                pprint.pprint(value)
                pprint.pprint(instance["data"]["objects"][key])
                return {
                    "success": True,
                    "transaction_applied": False,
                    "log": "precondition not met: %s" % key
                }

        for key, value in msg["transaction"]["updated_data"].items():
            instance["data"]["objects"][key] = msg["transaction"][
                "updated_data"][key]

        for key in msg["transaction"]["deleted_object_ids"]:
            del instance["data"]["objects"][key]

        instance["data"]["version"] += 1
        kv.set("instance-" + self.instance_id, json.dumps(instance))

        for ws in logged_in_websockets:
            if ws != self:
                try:
                    changeset = msg["transaction"]
                    changeset["version_after"] = instance["data"]["version"]
                    changeset = ws.filter_tx(changeset)
                    changeset_str = json.dumps({
                        "type": "changeset",
                        "changeset": changeset
                    })
                    ws.write_message(changeset_str)
                except:
                    print("Error sending changeset.")
                    pass

        changeset = msg["transaction"]
        changeset["version_after"] = instance["data"]["version"]
        changeset = self.filter_tx(changeset)
        return {
            "success": True,
            "transaction_applied": True,
            "changeset": changeset
        }