Skip to content

Commit

Permalink
Optimize contest problems fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
tiger2005 committed Nov 6, 2021
1 parent 566e018 commit b6cf5ea
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 50 deletions.
35 changes: 31 additions & 4 deletions js/authorize.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ function getFtaa(){
return ret;
}
function submitLogout(cb){
if(currentLoginHandle == ""){
cb();
return;
}
$.ajax({
url: settings.mainURL,
success: function(data){
Expand Down Expand Up @@ -75,7 +79,15 @@ function submitLogin(){
submitLogout(function(){$.ajax({
url: settings.mainURL,
success: function(data){
var csrf = queryCsrf.exec(data)[1];
var csrf = queryCsrf.exec(data);
if(csrf == null){
$(".settingsLoginButton").html(`<span info='errorCsrfLoadFailed'>${languageOption.error.errorCsrfLoadFailed}</span>`);
setTimeout(function(){
$(".settingsLoginButton").html(`<span info='settingsLoginButton' onclick="submitLogin()">${languageOption.general.settingsLoginButton}</span>`);
}, 2000)
return;
}
csrf = csrf[1];
$.ajax({
url: settings.mainURL + '/enter?back=%2F&locale=en',
type: "POST",
Expand Down Expand Up @@ -130,7 +142,12 @@ function submitSolution(ci, idx, code, lang, S, E){
$.ajax({
url: settings.mainURL,
success: function(data){
var csrf = queryCsrf.exec(data)[1];
var csrf = queryCsrf.exec(data);
if(csrf == null){
E('errorCsrfLoadFailed', languageOption.error.errorCsrfLoadFailed);
return;
}
csrf = csrf[1];
$.ajax({
url: settings.mainURL + `/${ci >= 100000 ? "gym" : "contest"}/` + ci + '/submit',
type: "POST",
Expand Down Expand Up @@ -219,7 +236,12 @@ function loadContestPassedStatus(S, E){
$.ajax({
url: settings.mainURL + '/contests',
success: function(data){
var csrf = queryCsrf.exec(data)[1];
var csrf = queryCsrf.exec(data);
if(csrf == null){
E();
return;
}
csrf = csrf[1];
$.ajax({
url: settings.mainURL + '/data/contests',
type: "POST",
Expand Down Expand Up @@ -270,7 +292,12 @@ function registerContest(ci, S, E){
$.ajax({
url: settings.mainURL,
success: function(data){
var csrf = queryCsrf.exec(data)[1];
var csrf = queryCsrf.exec(data);
if(csrf == null){
E();
return;
}
csrf = csrf[1];
$.ajax({
url: settings.mainURL + '/contestRegistration/' + ci,
type: "POST",
Expand Down
30 changes: 14 additions & 16 deletions js/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ function initContestNewWinPage(){
}

function initSinglePage(){
$(".forceLoadStandings").css("display", "inline");
$(".singleUserAvatar").attr("src", "");
$(".singleUsernameDisplayer").attr("class", "").addClass("singleUsernameDisplayer").html("");
$(".singleRatingDisplayer > div:first-child").attr("class", "setInlineBlock").html("");
Expand Down Expand Up @@ -1010,10 +1011,7 @@ function singleContestantSyncOfficialSettings(un, ci, json, p){
if(settings.openProblems && $(".singleContestProgressRatingChangesDisplayer > span:first-child").attr("info") != "contestFinished"){
if(singleFirstTimeLoaded){
singleFirstTimeLoaded = false;
var l = [];
for(var i=0; i<contestJsonProblems.length; i++)
l.push(contestContestId + contestJsonProblems[i].index);
openProblemWin(l, contestContestId);
openContestProblems(contestContestId);
}
}
}
Expand Down Expand Up @@ -1132,10 +1130,7 @@ function singleContestantSyncUnofficialSettings(un, ci, json, p){
if(settings.openProblems && $(".singleContestProgressRatingChangesDisplayer > span:first-child").attr("info") != "contestFinished"){
if(singleFirstTimeLoaded){
singleFirstTimeLoaded = false;
var l = [];
for(var i=0; i<contestJsonProblems.length; i++)
l.push(contestContestId + contestJsonProblems[i].index);
openProblemWin(l, contestContestId);
openContestProblems(contestContestId);
}
}
}
Expand Down Expand Up @@ -1244,10 +1239,7 @@ function singleVirtualSyncUnofficialSettings(un, ci, json, p){
if(settings.openProblems && $(".singleContestProgressRatingChangesDisplayer > span:first-child").attr("info") != "contestFinished"){
if(singleFirstTimeLoaded){
singleFirstTimeLoaded = false;
var l = [];
for(var i=0; i<contestJsonProblems.length; i++)
l.push(contestContestId + contestJsonProblems[i].index);
openProblemWin(l, contestContestId);
openContestProblems(contestContestId);
}
}
}
Expand Down Expand Up @@ -1536,6 +1528,7 @@ function singleContestantMainTrack(currSingleLastTimeUpdate, un, ci){


function singleVirtualMainTrack(currSingleLastTimeUpdate, un, ci, tm){
$(".forceLoadStandings").css("display", "none");
virtualProvidedStartTime = tm;
contestRanks = [0, 0];
contestRankLast = [0, 0];
Expand Down Expand Up @@ -1652,6 +1645,10 @@ function singleContestantWaitToStart(currLastTimeUpdate, un, ci){
var reloadTimeCount = function(){
if(currLastTimeUpdate != singleLastTimeUpdate) return;
if(startTime <= (new Date()).getTime()){
if(settings.openProblems){
singleFirstTimeLoaded = false;
openContestProblems(ci);
}
singleContestantMainTrack(currLastTimeUpdate, un, ci);
return;
}
Expand Down Expand Up @@ -1905,6 +1902,10 @@ function singleVirtualWaitToStart(currLastTimeUpdate, un, ci, tm){
var reloadTimeCount = function(){
if(currLastTimeUpdate != singleLastTimeUpdate) return;
if(startTime <= (new Date()).getTime()){
if(settings.openProblems){
singleFirstTimeLoaded = false;
openContestProblems(ci);
}
singleVirtualMainTrack(currLastTimeUpdate, un, ci, tm);
return;
}
Expand Down Expand Up @@ -2545,8 +2546,5 @@ $(".forceLoadStandings").click(function(){
setTimeout(function(){loadInfo(settings.codeforcesApiUrl + "/contest.standings", {contestId: ci, showUnofficial: settings.openRankPredict == 2}, ["result"], 1, false);}, 500);
})
$(".openProblems").click(function(){
var l = [];
for(var i=0; i<contestJsonProblems.length; i++)
l.push(contestContestId + contestJsonProblems[i].index);
openProblemWin(l, contestContestId);
openContestProblems(contestContestId);
})
103 changes: 73 additions & 30 deletions js/problem.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function initProblemPageInfo(page, data, id){
problemNewWinJQ.append(`<script>loadCopyOption()</script>`)
}
}
function loadProblem(x){
function loadProblem(x, info){
if(problemCurrentPageList.find(function(q){return q[0] == x}) == undefined) return;
var p = problemCurrentPageList.findIndex(function(q){return q[0] == x});
problemNewWinJQ.find(`.innerContent > [problem-id=${x}]`).html(`<div style="display: grid; place-items: center; width: 100%; height: 100%"><i class="fas fa-sync-alt fa-spin fa-3x"></i></div>`)
Expand All @@ -166,7 +166,12 @@ function loadProblem(x){
killProblemListItem($(this).attr("id"));
flushProblemNewWin();
})
problemCurrentPageList[p][1] = $.ajax({
if(info != undefined){
problemCurrentPageList[p][2] = 0;
initProblemPageInfo(problemNewWinJQ.find(`.innerContent > [problem-id=${x}]`), $(info).find(`.problemindexholder[problemindex=${getProblemIndexes(x)[1]}] .problem-statement`), p);
return;
}
problemCurrentPageList[p][1] =$.ajax({
url: settings.mainURL + `/${getProblemIndexes(x)[0] >= 100000 ? "gym" : "contest"}/` + getProblemIndexes(x)[0] + '/problem/' + getProblemIndexes(x)[1] + '?locale=en',
success: function(data){
if(data.indexOf(`data-entityId="${getProblemIndexes(x)[0]}"`) == -1){
Expand Down Expand Up @@ -263,15 +268,15 @@ function reloadProblem(x){
problemCurrentPageList[p][2] = 0;
loadProblem(x);
}
function addProblems(x, gid){
function addProblems(x, gid, info){
if(gid == undefined){
for(var i=0; i<x.length; i++){
if(getProblemIndexes(x[i])[0] == -1) continue;
if(problemCurrentPageList.find(function(q){return q[0] == x[i]}) != undefined) continue;
problemFocusOn = problemCurrentPageList.length;
problemCurrentPageList.push([x[i], null, 1, [[], []], {}]);
problemNewWinJQ.find(".innerContent").append(`<div class="innerContentPage" problem-id="${x[i]}" style="display: none"><div style="display: grid; place-items: center; width: 100%; height: 100%"><i class="fas fa-sync-alt fa-spin fa-3x"></i></div></div>`)
loadProblem(x[i]);
loadProblem(x[i], info);
}
flushProblemNewWin();
}
Expand All @@ -287,7 +292,7 @@ function addProblems(x, gid){
for(var i=0; i<x.length; i++){
problemCurrentPageList.push([x[i], null, 1, [[], []], {}]);
problemNewWinJQ.find(".innerContent").append(`<div class="innerContentPage" problem-id="${x[i]}" style="display: none"><div style="display: grid; place-items: center; width: 100%; height: 100%"><i class="fas fa-sync-alt fa-spin fa-3x"></i></div></div>`)
loadProblem(x[i]);
loadProblem(x[i], info);
}
flushProblemNewWin();
}
Expand All @@ -304,7 +309,7 @@ function addProblems(x, gid){
problemCurrentPageList.splice(problemGroupRange[gid][0] + i, 0, [x[i], null, 1, [[], []], {}]);
problemNewWinJQ.find(`.innerContent > [problem-id=${x[i]}]`).remove();
problemNewWinJQ.find(".innerContent").append(`<div class="innerContentPage" problem-id="${x[i]}" style="display: none"><div style="display: grid; place-items: center; width: 100%; height: 100%"><i class="fas fa-sync-alt fa-spin fa-3x"></i></div></div>`)
loadProblem(x[i]);
loadProblem(x[i], info);
}
flushProblemNewWin();
}
Expand Down Expand Up @@ -348,17 +353,17 @@ function loadContestProblemset(cid, S, E){
}
cid = Number(cid);
$.ajax({
url: settings.mainURL + `/${cid >= 100000 ? "gym" : "contest"}/` + cid,
url: settings.mainURL + `/${cid >= 100000 ? "gym" : "contest"}/` + cid + '/problems',
success: function(data){
if(data.indexOf(`data-entityId="${cid}"`) == -1){
if(data.indexOf(`class="problem-statement"`) == -1){
E(); return;
}
var q = $(data).find("table.problems");
var q = $(data);
var ret = [];
q.find("td.id a").each(function(){
ret.push(String(cid) + $.trim($(this).html()));
q.find(".problemindexholder").each(function(){
ret.push(String(cid) + $.trim($(this).attr("problemindex")));
})
S(ret);
S(ret, q);
},
error: function(){
E();
Expand Down Expand Up @@ -416,22 +421,6 @@ function flushProblemNewWin(){
problemNewWinJQ.find(".innerContent > div").css("display", "none");
problemNewWinJQ.find(`.innerContent > [problem-id=${problemCurrentPageList[problemFocusOn][0]}]`).css("display", "block");
})
problemNewWinJQ.find(".prevProblem").unbind("click").click(function(){
problemNewWinJQ.find(".sideBarItem").eq(problemFocusOn).removeClass("chosen");
problemFocusOn = Math.max(problemFocusOn - 1, 0);
problemNewWinJQ.find(".sideBarItem").eq(problemFocusOn).addClass("chosen");
problemNewWinJQ.find(".problemName").html(problemCurrentPageList[problemFocusOn][0]);
problemNewWinJQ.find(".innerContent > div").css("display", "none");
problemNewWinJQ.find(`.innerContent > [problem-id=${problemCurrentPageList[problemFocusOn][0]}]`).css("display", "block");
})
problemNewWinJQ.find(".nextProblem").unbind("click").click(function(){
problemNewWinJQ.find(".sideBarItem").eq(problemFocusOn).removeClass("chosen");
problemFocusOn = Math.min(problemFocusOn + 1, problemCurrentPageList.length - 1);
problemNewWinJQ.find(".sideBarItem").eq(problemFocusOn).addClass("chosen");
problemNewWinJQ.find(".problemName").html(problemCurrentPageList[problemFocusOn][0]);
problemNewWinJQ.find(".innerContent > div").css("display", "none");
problemNewWinJQ.find(`.innerContent > [problem-id=${problemCurrentPageList[problemFocusOn][0]}]`).css("display", "block");
})
problemNewWinJQ.find(".closeCurrentProblemPage").unbind("click").click(function(){
event.stopPropagation();
killProblemListItem($(this).attr("id"));
Expand Down Expand Up @@ -506,7 +495,7 @@ function flushProblemNewWin(){
problemNewWinJQ.find(".problemInfoInputArea button").html(`<i class="fas fa-sync fa-spin"></i>`);
problemNewWinJQ.find(".contestInfoInputArea button").html(`<i class="fas fa-sync fa-spin"></i>`);
var R = problemNewWinJQ.find(".contestInfoInputArea input").val();
loadContestProblemset(R, function(data){
loadContestProblemset(R, function(data, info){
problemNewWinJQ.find(".problemInfoInputArea button").removeAttr("disabled");
problemNewWinJQ.find(".contestInfoInputArea button").removeAttr("disabled");
problemNewWinJQ.find(".problemInfoInputArea button").html(`<i class="fas fa-paper-plane"></i>`);
Expand All @@ -515,7 +504,7 @@ function flushProblemNewWin(){
setTimeout(function(){
problemNewWinJQ.find(".addProblemWindow").css("display", "none");
}, 500);
addProblems(data, R);
addProblems(data, R, info);
}, function(){
problemNewWinJQ.find(".problemInfoInputArea button").removeClass("primaryColor").addClass("dangerColor");
problemNewWinJQ.find(".contestInfoInputArea button").removeClass("primaryColor").addClass("dangerColor");
Expand Down Expand Up @@ -709,4 +698,58 @@ function openProblemWin(xx, gid){
problemCurrentPageList[i][1].abort();
})
});
}
function addContest(ci){
loadContestProblemset(ci, function(data, info){
addProblems(data, ci, info);
}, function(){});
}
function openContestProblems(xx){
if(!RunInNwjs) return;
if(problemNewWinOpened){
addContest(xx);
return;
}
problemNewWinOpened = true;
nw.Window.open("problem.html",{
"title": "Codeforces Problems",
"icon": "favicon.png",
"width": 600,
"height": 420,
"position": "center",
"resizable": true,
"min_width": 450,
"min_height": 290,
"fullscreen":false,
"show_in_taskbar":true,
"show":true,
"kiosk":false,
"always_on_top":false,
"frame":false,
"transparent":true,
}, function(x){
problemNewWin = x;
problemNewWin.on("loaded", function(){
problemNewWinJQ = $(problemNewWin.window.document.body);
initProblemNewWin();
problemCurrentPageList = [];
problemFocusOn = 0;
problemLoadQueue = [];
problemLoadRunning = 0;
problemGroupRange = {};
problemLoadID = 0;
// problemNewWin.showDevTools();
addContest(xx);
addContest(xx);
problemNewWinLoaded = true;
flushProblemNewWin();
})
problemNewWin.on("closed", function(){
problemNewWinOpened = false;
problemNewWinLoaded = false;
for(var i=0; i<problemCurrentPageList.length; i++)
if(problemCurrentPageList[i][1] != null)
problemCurrentPageList[i][1].abort();
})
});
}

0 comments on commit b6cf5ea

Please sign in to comment.