var currEcmUid = ""; var currLocUid = ""; var currLocLang = "en"; var wsUid = ""; var cpMetadata = null; var lbTier = 0; var setTimeoutId = 0; var websocket = null; $(document).ready(function () { var urlParams = getUrlParams(); currEcmUid = urlParams["ecm_uid"]; currLocUid = urlParams["location_id"]; currLocLang = urlParams["lang"]; if (currEcmUid != null && currLocUid == null) { lbTier = 3; wsUid = currEcmUid; } else if (currEcmUid != null && currLocUid != null) { lbTier = 3.5; wsUid = currEcmUid + currLocUid; } else { showMessage("Invalid leaderboard UID", MESSAGE_TYPE_ERROR); return; } var postData = new Object(); if (lbTier == 3) { postData["request"] = "get_cat_ecm_metadata"; postData["ecm_uid"] = currEcmUid; } else if (lbTier == 3.5) { postData["request"] = "get_cat_ecm_loc_metadata"; postData["ecm_uid"] = currEcmUid; postData["location_id"] = currLocUid; } postData["lang"] = currLocLang; callAPI(postData, function(json) { $(".title-text").html(json["title"]); if (lbTier == 3) { $(".sub-title-text").html(json["sub_title"]); } else if (lbTier == 3.5) { $(".sub-title-text").html(json["location_name"]); } $(".header-left-image img").attr("src", json["left_image"]); $(".header-right-image img").attr("src", json["right_image"]); cpMetadata = json; $("#layout1").show(); loadLeaderboard(); connectWS(); }); }); function loadLeaderboard() { var postData = new Object(); if (lbTier == 3) { postData["request"] = "get_competition_leaderboard"; postData["ecm_uid"] = currEcmUid; } else if (lbTier == 3.5) { postData["request"] = "get_competition_loc_leaderboard"; postData["ecm_uid"] = currEcmUid; postData["location_id"] = currLocUid; } postData["lang"] = currLocLang; callAPI(postData, function(json) { fillNamesInBoard(json["data"]); setScrollContainer(); startScroll(); }); } function fillNamesInBoard(data) { $(".board1-scroll-content-ref").html(""); $(".board1-top-content-ref").html(""); var scrollContent = $(".board1-scroll-content-ref"); scrollContent.stop(); scrollContent.animate({ "marginTop": "0px" }, 500); for (var i = 0; i < data.length; i++) { var dataItem = data[i]; dataItem["index"] = (1 + i); addName(dataItem); } } function setScrollContainer() { var numOfTops = $(".board1-top-content-ref").children().length; var spacer = (numOfTops == 0 ? 0 : 0.87); $(".board1-scroll-container-ref").css("top", (numOfTops * 2.344 + spacer) + "vw"); } function addName(nameData) { var container = ".board1-scroll-content-ref"; var type = "other"; var rank = nameData["rank"]; var rt = nameData["rt"]; var isDisqual = (nameData["rt"] == 0); if (nameData["index"] <= cpMetadata["num_of_freeze"] && !isDisqual) { container = ".board1-top-content-ref"; } if (nameData["is_done"]) { type = "top"; } if (isDisqual) { type = "dis"; rank = "----"; rt = "Disqualified"; } var html = $(".board1_line_top").html(); html = html.replace(/#1#/g, type) .replace("#2#", rank) .replace("#3#", nameData["country"]) .replace("#4#", nameData["country"]) .replace("#5#", nameData["name"]) .replace("#6#", nameData["location_name"]) .replace("#7#", nameData["age"]) .replace("#8#", (nameData["is_male"] ? "Male" : "Female")) .replace("#9#", nameData["martial_art"]) .replace("#10#", nameData["round"]) .replace("#11#", (nameData["aggregated_points"] ? nameData["aggregated_points"] : 0)); $(container).append(html); } function animateContent(secs) { var scrollContainer = $(".board1-scroll-container-ref"); var scrollContent = $(".board1-scroll-content-ref"); var animationOffset = (scrollContainer.height() - parseInt(scrollContainer.css("top"))) - scrollContent.height(); var anim = scrollContent.animate({ "marginTop": (animationOffset)+ "px" }, secs * 1000); setTimeoutId = setTimeout(function () { setTimeoutId = 0; loadLeaderboard(); }, 3000 + secs * 1000); } function startScroll() { var scrollContainer = $(".board1-scroll-container-ref"); var scrollContent = $(".board1-scroll-content-ref"); var diff = scrollContent.height() - (scrollContainer.height() - parseInt(scrollContainer.css("top"))); if (diff <= 0) { setTimeoutId = setTimeout(function () { setTimeoutId = 0; loadLeaderboard(); }, 10000); return; } var secs = 2 + diff / 45; setTimeoutId = setTimeout(function () { setTimeoutId = 0; animateContent(secs); }, 5000); } function connectWS() { var wsUri = "wss://api.ludusmaterials.com/socket.php"; websocket = new WebSocket(wsUri); websocket.onopen = function(ev) { $(".ws_off_indicator").hide(); var msg = new Object(); msg["message_type"] = "register"; msg["client_type"] = "leaderboard"; msg["level"] = lbTier; msg["uid"] = wsUid; websocket.send(JSON.stringify(msg)); } websocket.onmessage = function(ev) { var response = JSON.parse(ev.data); var action = response["action"]; if (action == "refresh" || action == "results") { if (setTimeoutId != 0) { clearTimeout(setTimeoutId); setTimeoutId = 0; } loadLeaderboard(); } } websocket.onerror = function(ev) { } websocket.onclose = function(ev) { $(".ws_off_indicator").show(); websocket = null; setTimeout(function () { connectWS(); }, 1000); } }