Skip to content

Commit

Permalink
Support filtering ladder by username prefix (for OLT) (smogon#1326)
Browse files Browse the repository at this point in the history
  • Loading branch information
scheibo authored and Zarel committed Jul 18, 2019
1 parent b2a90ba commit 80aaab1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
19 changes: 15 additions & 4 deletions js/client-ladder.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
});

this.LadderRoom = HTMLRoom.extend({
events: {
'submit .search': 'submitSearch'
},
type: 'ladder',
title: 'Ladder',
initialize: function () {
Expand All @@ -104,6 +107,7 @@
}, this);
},
curFormat: '',
curSearchVal: '',
join: function () {},
leave: function () {},
update: function () {
Expand Down Expand Up @@ -135,22 +139,29 @@
var self = this;
this.$el.html('<div class="ladder pad"><p><button name="selectFormat"><i class="fa fa-chevron-left"></i> Format List</button></p><p><em>Loading...</em></p></div>');
if (app.localLadder) {
app.send('/cmd laddertop ' + format);
app.send('/cmd laddertop ' + format + (this.curSearchVal ? ' ,' + this.curSearchVal : ''));
} else {
$.get('/ladder.php', {
format: format,
server: Config.server.id.split(':')[0],
output: 'html'
output: 'html',
prefix: this.curSearchVal
}, function (data) {
if (self.curFormat !== format) return;
var buf = '<div class="ladder pad"><p><button name="selectFormat"><i class="fa fa-chevron-left"></i> Format List</button></p><p><button class="button" name="refresh"><i class="fa fa-refresh"></i> Refresh</button></p>';
buf += '<h3>' + BattleLog.escapeFormat(format) + ' Top 500</h3>';
var buf = '<div class="ladder pad"><p><button name="selectFormat"><i class="fa fa-chevron-left"></i> Format List</button></p><p><button class="button" name="refresh"><i class="fa fa-refresh"></i> Refresh</button>';
buf += '<form class="search"><input type="text" name="searchval" class="textbox searchinput" value="' + BattleLog.escapeHTML(self.curSearchVal || '') + '" placeholder="username prefix" /><button type="submit"> Search</button></form></p>';
buf += '<h3>' + BattleLog.escapeFormat(format) + ' Top ' + BattleLog.escapeHTML(self.curSearchVal ? "- '" + self.curSearchVal + "'" : '500') + '</h3>';
buf += data + '</div>';
self.$el.html(buf);
}, 'html');
}
}
},
submitSearch: function (e) {
e.preventDefault();
this.curSearchVal = this.$('input[name=searchval]').val();
this.update();
},
showHelp: function () {
var buf = '<div class="ladder pad"><p><button name="selectFormat"><i class="fa fa-chevron-left"></i> Format List</button></p>';
buf += '<h3>How the ladder works</h3>';
Expand Down
3 changes: 2 additions & 1 deletion ladder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
$formatid = 'OU';

if (@$_REQUEST['format']) $formatid = $_REQUEST['format'];
if (@$_REQUEST['format']) $prefix = $_REQUEST['prefix'];

if (!ctype_alnum($formatid)) {
die('denied');
Expand All @@ -22,7 +23,7 @@
</tr>
<?php

$toplist = $ladder->getTop();
$toplist = $ladder->getTop($prefix);

$i=0;

Expand Down
18 changes: 16 additions & 2 deletions lib/ntbb-ladder.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ function getAllRatings(&$user) {
return true;
}

function getTop() {
function getTop($prefix) {
global $ladderdb;
$needUpdate = true;
$top = array();
Expand All @@ -224,7 +224,21 @@ function getTop() {
// $limit = 1000;
// }

$res = $ladderdb->query("SELECT * FROM `{$ladderdb->prefix}ladder` WHERE `formatid` = '{$this->formatid}' ORDER BY `elo` DESC LIMIT $limit");
if ($prefix) {
// The ladder database can't really handle large queries which aren't indexed, so we instead perform
// an indexed query for additional rows and filter them down further. This is obviously *not* guaranteed
// to return exactly $limit results, but should be 'good enough' in practice.
$overfetch = $limit * 4;
$res = $ladderdb->query(
"SELECT * (SELECT * FROM `{$ladderdb->prefix}ladder` WHERE `formatid` = ? ORDER BY `elo` DESC LIMIT ?) WHERE `userid` LIKE ? LIMIT ?)",
[$this->formatid, $overfetch, "{$prefix}%", $limit]
);
} else {
$res = $ladderdb->query(
"SELECT * FROM `{$ladderdb->prefix}ladder` WHERE `formatid` = ? ORDER BY `elo` DESC LIMIT ?",
[$this->formatid, $limit]
);
}

$j = 0;
while ($row = $ladderdb->fetch_assoc($res)) {
Expand Down

0 comments on commit 80aaab1

Please sign in to comment.