Skip to content

Instantly share code, notes, and snippets.

@Hypnosphi
Last active October 28, 2015 10:57
Show Gist options
  • Save Hypnosphi/583ea8ed048d0203b53b to your computer and use it in GitHub Desktop.
Save Hypnosphi/583ea8ed048d0203b53b to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<head id="Head1">
<meta charset="utf-8">
<title>Achivements</title>
<script src="//vk.com/js/api/xd_connection.js?2" type="text/javascript"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
var points = {},
polls = [],
index = {},
uid;
console.log(1);
$(document).ready(function () {
VK.init(function () {
walllength = 0;
VK.loadParams(document.location.href);
var me;
var ingroup = 0;
uid = VK.params.viewer_id;
VK.callMethod("showSettingsBox", 8192);
VK.api("users.get", { user_ids: VK.params.viewer_id, test_mode: 0 }, function (data) {
if (data.response) {
CreateInput();
// GetComments(0);
// setTimeout(function () { GetComments(100); }, 1000);
//setTimeout(function () { alert(commentIds.length); }, 3000);
/// setTimeout(function () { commentsLn = commentIds.length; GetUsers(0); }, 2000);
}
else {
alert('fail');
};
});
}, '5.37');
});
function SortQuests() {
$('#mainbody').html("");
points = {};
polls = [];
index = {};
GetPolls(0);
}
function GetPolls(offset) {
var rid = $( "#refselector option:selected" ).val(),
tid = $( "#targetselector option:selected" ).val();
query = 'var o=' + offset + ',res=[],i=0,c,a;' +
'while(i<25&&(!c||c>o)){' +
'a=API.wall.get({"owner_id":'+rid+',"filter":"","offset":o,"count":"100"});' +
'c=a.count;res.push([email protected]);' +
'i=i+1;o=o+100;}return {count:c,arrs:res};',
sentTime = Date.now();
$('#waitbar').text("Подождите, мы тупим первый раз... " + Math.round(offset * 100 / count) + "%");
VK.api("execute", { code: query, test_mode: 0, v: 5.37 }, function (data) {
//alert(JSON.stringify(data.response));
// $('#test').html(JSON.stringify(data.response));
var passedTime = Date.now() - sentTime,
timeout = Math.max(0, 340 - passedTime),
arrs = data.response.arrs,
count = data.response.count;
arrs.forEach(function(arr) {
arr.filter(function(item) {
return item != null;
})
.forEach(function(atts) {
atts.filter(function(att) {
var poll = att.poll;
return poll
&& poll.anonymous != 1
&& (uid != tid || poll.answer_id) // when calculating for yourself, pick only polls you've voted in
&& !index[poll.id];
})
.forEach(function(att) {
var poll = att.poll;
poll.maxVotes = poll.answers.reduce(
function(memo, answer) {
return Math.max(memo, answer.votes)
},
0
);
poll.answer_ids = poll.answers
.map(function(answer) {
return answer.id;
})
.join(',');
polls.push(poll);
index[poll.id] = true; // store poll id in a "set" to prevent dublicates
});
});
});
offset += 2500;
if (offset >= count) {
$('#waitbar').text("");
setTimeout(GetResults, timeout, 0, null, 0);
// $('#test').html(JSON.stringify(polls));
return;
}
else {
setTimeout(GetPolls, timeout, offset);
}
});
}
function GetResults(offset, poll, i) {
var tid = $( "#targetselector option:selected" ).val(),
rid = $( "#refselector option:selected" ).val(),
pollset = [],
prev,
query = 'var res=[],a;',
sentTime = Date.now();
$('#waitbar').text("Подождите, мы тупим второй раз... " + Math.round(i * 100 / polls.length) + "%");
while (pollset.length < 25) {
offset || (poll = polls[i++]);
query += 'res.push(API.polls.getVoters(' +
JSON.stringify({
owner_id : rid,
poll_id : poll.id,
offset : offset,
count : 1000,
answer_ids: poll.answer_id
}) +
'));';
pollset.push(poll);
offset += 1000;
offset > poll.maxVotes && (offset = 0);
}
query += 'return res;';
// $('#test').html(tid + " " + rid + " " + pollid);
VK.api("execute", {
code: query,
test_mode: 0,
v: 5.37
}, function (data) {
//$('#test').html(JSON.stringify(data));
var arr,
passedTime = Date.now() - sentTime,
timeout = Math.max(0, 340 - passedTime);
if (data.response == null) {
throw('responce is null');
}
data.response[0].users.items.forEach(
function(user) {
points[user] ? (points[user] = 1) : points[user]++;
}
);
if (i >= polls.length) {
$('#waitbar').text("");
setTimeout(FillResults, timeout);
} else {
setTimeout(GetResults, timeout, offset, poll, i);
}
});
}
function foo(obj) {
var tid = $( "#targetselector option:selected" ).val();
return Object.keys(obj)
.filter(function(id) {
return id != tid;
})
.sort(function(a, b) {
return obj[a] - obj[b];
})
.map(function(id) {
return {
user: id,
score: obj[id] * 100 / polls.length
};
});
}
function FillResults() {
var list = foo(points),
top = list.slice(0, 10);
VK.api("execute", {
code:
'var a; ' +
'a= API.users.get({"user_ids":"' + top.join(',') + '","fields":"photo_50","name_case":"Nom"});' +
'return a;'
, test_mode: 0
}, function (data) {
if (data.response) {
//alert(JSON.stringify(data.response));
// $('#test').html(JSON.stringify(b));
//return;
var result = "Всего рассмотрено " + polls.length + " опросов. <br/><table border='1'><tbody>";
for (var i = 0; i < data.response.length; i++) {
var arr2 = data.response[i];
result += "<tr><td><a href='https://vk.com/id" + a[i] + "' > <img src='" + arr2.photo_50 + "' /></a></td>" +
"<td>" + arr2.last_name + "<br/> " + arr2.first_name + "</td>" +
"<td>" + "Совпадение: " + parseInt(b[i] * 100 / u0[0]) + "%</td></tr>";
}
result += "</tbody></table>";
$('#test').html();
$('#mainbody').html(result);
$('#result').css('display', '');
}
else {
//alert('Что-то пошло не так ' + JSON.stringify(data));
// $('#test').html(JSON.stringify(data));
};
});
}
function CreateInput() {
$("#targetselector").append($('<option/>', {
value: uid,
text: "Для себя"
}));
return;
VK.api("execute", {
code:
'var a; ' +
'a= API.friends.get({"user_id":"' + uid + '","order":"name","fields":"name","name_case":"Gen"});' +
'return a;'
, test_mode: 0
}, function (data) {
if (data.response) {
//alert(JSON.stringify(data.response));
//$('#test').html(JSON.stringify(data.response));
var arr2 = data.response;
//$('#test').html(JSON.stringify(arr2));
// return;
// for (var i = 0; i < arr2.items.length - 1; i++) {
// $("#targetselector").append($('<option/>', {
// value: arr2.items[i].id,
// text: arr2.items[i].last_name + " " + arr2.items[i].first_name
// }));
/// }
}
else {
//alert('Что-то пошло не так ' + JSON.stringify(data));
$('#test').html(JSON.stringify(data));
};
});
}
</script>
<style>
table
{
font-family: "Trebuchet MS" , Arial, Helvetica, sans-serif;
width: 100%;
border-collapse: collapse;
}
table td, table th
{
font-size: 1em;
border: 1px solid #98bf21;
padding: 3px 7px 2px 7px;
}
table th
{
font-size: 1.1em;
text-align: left;
padding-top: 5px;
padding-bottom: 4px;
background-color: #A7C942;
color: #ffffff;
}
table tbody
{
overflow: auto;
}
.didit td
{
background-color: #A5DF00;
}
.notdidit td
{
background-color: #FE642E;
}
</style>
</head>
<body>
<form style="background-color: #F1F1F1; width: 550px; height: 850px; margin-left: 20px;
overflow-y: scroll;" id="Form1">
<h1>Найди близкого человека сейчас!</h1>
<h2 id="hh">У кого мы будем искать близость?</h2>
<select name="refselector" id="refselector">
<option value="17">У Беспалова</option>
<option value="14">У Городецкого</option>
<option value="52767">У Бабской</option>
<option value="79796">У Усковой</option>
<option value="7586">У Влахова</option>
</select>
<h2 id="H2">Для кого мы будем искать близость?</h2>
<select name="targetselector" id="targetselector">
</select>
<br /> <br />
<input type="button" onclick="SortQuests()" value="Поискать" />
<br />
<div id="test"></div>
<br />
<h3 id="waitbar"></h3>
<div style="display: none" id="result">
<h2 id="H1">Итак. Список близких душ</h2>
<div id="mainbody">
</div>
</div>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment