Permalink
Browse files

Merge branch 'master' of https://github.com/shonenA/JuSpinBeatRankWeb

  • Loading branch information...
2 parents ccc492f + 1f0e03c commit 04c69399a6b804d5e3ba27929c5dabe069a38964 @kimdwkimdw kimdwkimdw committed Feb 14, 2012
Showing with 247 additions and 131 deletions.
  1. +123 −131 index.html
  2. +124 −0 script/juspinbeat.js
View
@@ -56,11 +56,6 @@
border-bottom: 1px solid #ddd;
}
- @media (max-width: 640px) {
- .modal { width: 320px; margin-left: -160px; margin-top: -160px; }
- .modal-body { width: 260px; }
- }
-
/* jbt */
.records th { text-align: center; background: #444; color: white; }
@@ -84,6 +79,11 @@
<ul class="nav">
<li><a href="#record">Record</a></li>
<li><a href="#history">History</a></li>
+ <li class="dropdown">
+ <a href="#" class="dropdown-toggle">List</a>
+ <ul class="dropdown-menu">
+ </ul>
+ </li>
</ul>
</div>
</div>
@@ -105,162 +105,154 @@
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="script/jquery.tablesorter.js"></script>
+<script src="script/juspinbeat.js"></script>
<script>
+(function (global) {
+var _params = location.search.substr(1).split("&"), _return_dict={};
+for ( var i = 0; i < _params.length; ++i ) {
+ t = _params[i].split("=")
+ _return_dict[t[0]] = t[1]
+}
+global.params = _return_dict;
+})(window);
+
$(function(){
- function makeUserRecords( records ) {
- var ret = '<tbody>';
- var r = new Array()
- var best = 0;
- for( i in records ) {
- records[i].name = i;
- var total = 0;
- for( j in records[i] ) {
- if( new Number(records[i][j]) == records[i][j] )
- total += new Number(records[i][j]);
- }
- records[i].total = total;
- if( best < total ) best = total;
- r.push(records[i]);
- }
- r.sort(function(a, b) {
- return a.total > b.total ? -1 : 1;
- });
+ // dropdown
+ $('.dropdown-toggle').click(function(){
+ $('.dropdown-menu').toggle().find('a').click(function(){$('.dropdown-menu').toggle();});
+ return false;
+ });
- function addCommas(nStr)
- {
- nStr += '';
- x = nStr.split('.');
- x1 = x[0];
- x2 = x.length > 1 ? '.' + x[1] : '';
- var rgx = /(\d+)(\d{3})/;
- while (rgx.test(x1)) {
- x1 = x1.replace(rgx, '$1' + ',' + '$2');
- }
- return x1 + x2;
+ // cache
+ $.juspinbeat = $.juspinbeat || {};
+ $.juspinbeat.cache = $.juspinbeat.cache || {'record':{},'history':{}};
+
+ $.juspinbeat.busy = function(work, value) {
+ var works = ['record', 'history'];
+ if( 'boolean' === typeof value ) {
+ this[work] = value;
+ return this;
}
- function formatScore( score, rank ) {
- if( typeof(rank) === 'undefined' ) {
- rank = score;
+ for( var i in works ) {
+ if( this[works[i]] ) {
+ return true;
}
- var html = '<span class="rank">';
- if( rank < 500000 ) html += "E";
- else if( rank < 700000 ) html += "D";
- else if( rank < 800000 ) html += "C";
- else if( rank < 850000 ) html += "B";
- else if( rank < 900000 ) html += "A";
- else if( rank < 950000 ) html += "S";
- else if( rank < 980000 ) html += "SS";
- else if( rank < 1000000 ) html += "SSS";
- else html += "EXC";
- html += '</span>';
- html += '<span class="score pull-right">'+addCommas(score)+'</span>';
- return html;
}
+ return false;
+ };
- for( i in r ) {
- var c = r[i];
+ $.juspinbeat.currentContestId = null;
- ret += '<tr class="record">';
- ret += '<td>' + (c.name + ' <span class="pull-right best-diff">(' + (addCommas(c.total - best)) + ')</span><br />' + c.last_played) + '</td>';
+ var API_DOMAIN = "http://laika.redfeel.net:4416";
- var played = 0;
- c.length = 10;
- cArray = [].slice.call(c,0)
- for ( j in cArray )
- {
- ret += '<td>' + formatScore(cArray[j]) + '</td>';
- if( cArray[j] > 0 ) played++;
- }
- played = played?played:1;
- ret += '<td>' + formatScore(c.total, c.total/played) + '</td>';
- ret += "</tr>";
- }
- ret += '</tbody>';
- return ret;
- }
+ function dataValidate( data, mandatory ) {
+ // TODO 적절한 에러 핸들링
+ if( 'undefined' === typeof data ) return false;
- function makeHeader( musiclist ) {
- var ret = '<thead><tr><th>User</th>';
- for( i in musiclist ) {
- var c = musiclist[i];
- var cl = "difficulty-" + c[1];
- ret += '<th class="'+ cl +'">' + c[0] + '</th>';
+ if( data.code !== 'ok' ) {
+ return false;
}
- ret += "<th>Total</th></tr></thead>";
- return ret;
- }
- function makeHistory( history ) {
- var ret = '<table class="history"><thead>';
- ret += '<tr>';
- ret += '<th>Date</th>';
- ret += '<th>User</th>';
- ret += '<th>Music</th>';
- ret += '<th>Score</th>';
- ret += '</tr></thead><tbody>';
- for( i in history ) {
- var c = history[i];
- ret += '<tr>';
- ret += '<td>'+c.date+'</td>';
- ret += '<td>'+c.user+'</td>';
- ret += '<td class="difficulty-'+c.difficulty+'">'+c.music+'</td>';
- ret += '<td>'+c.score+'</td>';
- ret += '</tr>';
- }
- ret += '</tbody></table>';
- return ret;
- }
+ if( mandatory instanceof Array ) { for( var i in mandatory ) {
+ if( 'undefined' === typeof data[mandatory[i]] ) return false;
+ }}
- function prepareHistory( history ) {
- history.sort(function(a,b){return a.date < b.date ? -1 : 1; });
- var table = {};
- for( i in history ) {
- var c = history[i];
- var key = c.user + ':' + c.music + ':' + c.difficulty;
- if( typeof(table[key]) === 'undefined' ) {
- table[key] = 0;
- }
- c.score = new Number(c.score);
- var diff = c.score - table[key];
- if( diff > 0 ) {
- table[key] = c.score;
- }
- if( diff >= 0 ) diff = '+' + diff;
- c.score += (' (' + diff + ')');
- }
+ return true;
}
- $.getJSON('http://laika.redfeel.net:4416/201205?callback=?',function(data){
- if( typeof(data) === 'undefined' ) return;
-
- var html = '<table class="records">';
- if( typeof(data.musiclist) !== 'undefined' ) {
- html += makeHeader(data.musiclist);
+ function renderRecordTable(data) {
+ if( !dataValidate(data, ['music_list', 'user_records']) ) {
+ return;
}
- if( typeof(data.user_records) !== 'undefined' ) {
- html += makeUserRecords(data.user_records);
- }
+ var html = '<table class="records">';
+ html += makeHeader(data.music_list);
+ html += makeUserRecords(data.user_records);
html += "</table>";
$('#record').append(html);
$('.records').tablesorter({
headers:{0:{sorter:false}},
textExtraction: function(cell) {
- return new Number($(cell).find('.score').text().replace(/,/g,'')) * -1;
+ return new Number($(cell).find('.score').text().replace(/,/g,'')) * -1;
},
sortList:[[6,0]]
- });
+ });
- html = '';
- if( typeof(data.history) !== 'undefined' ) {
- prepareHistory(data.history);
- html += makeHistory(data.history);
+ $.juspinbeat.cache['record'][$.juspinbeat.currentContestId] = data;
+ $.juspinbeat.busy('record', false);
+ }
+
+ function renderHistoryTable(data) {
+ if( !dataValidate(data, ['history']) ) {
+ return;
}
+
+ var html = '';
+ prepareHistory(data.history);
+ html += makeHistory(data.history);
$('#history').append(html);
$('.history').tablesorter({sortList:[[0,1]]});
- });
+
+ $.juspinbeat.cache['history'][$.juspinbeat.currentContestId] = data;
+ $.juspinbeat.busy('history', false);
+ }
+
+ function initTable( contestId ) {
+ if( $.juspinbeat.busy() ) return;
+
+ $.juspinbeat.currentContestId = contestId;
+
+ // Record Table
+ if( $.juspinbeat.cache['record'][contestId] ) {
+ renderRecordTable($.juspinbeat.cache['record'][contestId]);
+ } else {
+ $.juspinbeat.busy('record', true);
+ $.getJSON(API_DOMAIN + '/contest/' + contestId + '?callback=?', renderRecordTable);
+ }
+
+ // History Table
+ if( $.juspinbeat.cache['history'][contestId] ) {
+ renderHistoryTable($.juspinbeat.cache['history'][contestId]);
+ } else {
+ $.juspinbeat.busy('history', true);
+ $.getJSON(API_DOMAIN + '/contest/' + contestId + '/history?callback=?', renderHistoryTable);
+ }
+ }
+
+ function removeTable() {
+ $('.history').remove();
+ $('.records').remove();
+ }
+
+ var contest = params['contest'];
+ if ( 'undefined' === typeof contest ) {
+ $.getJSON(API_DOMAIN + '/contest?callback=?', function(data) {
+ if( !dataValidate(data, ['current_contest_list']) ) {
+ return;
+ }
+
+ var contestList = data.current_contest_list;
+ if( contestList.length == 0 ) {
+ return;
+ }
+
+ for( var i in contestList ) {
+ (function(i){
+ var contest = contestList[i];
+ $('<li>').append($('<a>').click(function() {
+ removeTable();
+ initTable(contest.id);
+ }).text(contest.name).css('cursor','pointer')).appendTo('.dropdown-menu');
+ })(i);
+ }
+
+ initTable(contestList[Math.floor(Math.random()*contestList.length)].id);
+ });
+ } else {
+ initTable(contest);
+ }
});
</script>
</body>
Oops, something went wrong.

0 comments on commit 04c6939

Please sign in to comment.