tournament
Version:
Tournament interface
91 lines (77 loc) • 2.27 kB
JavaScript
var $ = require('interlude');
var o = { NONE: 0 }; // no player marker same for all tournaments
o.findMatch = function (ms, id) {
return $.firstBy(function (m) {
return (id.s === m.id.s) &&
(id.r === m.id.r) &&
(id.m === m.id.m);
}, ms);
};
o.findMatches = function (ms, id) {
return ms.filter(function (m) {
return (id.s == null || m.id.s === id.s) &&
(id.r == null || m.id.r === id.r) &&
(id.m == null || m.id.m === id.m);
});
};
o.findMatchesRanged = function (ms, lb, ub) {
ub = ub || {};
return ms.filter(function (m) {
return (lb.s == null || m.id.s >= lb.s) &&
(lb.r == null || m.id.r >= lb.r) &&
(lb.m == null || m.id.m >= lb.m) &&
(ub.s == null || m.id.s <= ub.s) &&
(ub.r == null || m.id.r <= ub.r) &&
(ub.m == null || m.id.m <= ub.m);
});
};
o.metadata = function (ms) {
return ms.reduce(function (acc, el) {
if (el.data) {
acc.push({ id: el.id, data: el.data });
}
return acc;
}, []);
};
o.partitionMatches = function (ms, splitKey, filterKey, filterVal) {
var res = [];
for (var i = 0; i < ms.length; i += 1) {
var m = ms[i];
if (filterVal == null || m.id[filterKey] === filterVal) {
if (!Array.isArray(res[m.id[splitKey] - 1])) {
res[m.id[splitKey] - 1] = [];
}
res[m.id[splitKey] - 1].push(m);
}
}
return res;
};
o.matchesForPlayer = function (ms, playerId) {
return ms.filter(function (m) {
return m.p.indexOf(playerId) >= 0;
});
};
o.players = function (ms) {
var ps = ms.reduce(function (acc, m) {
return acc.concat(m.p); // collect all players in given matches
}, []);
return $.nub(ps).filter($.gt(o.NONE)).sort($.compare());
};
// This may replace rounds in future versions
o.rounds = function (ms) {
return $.nub(ms.map($.get('id', 'r'))).sort($.compare());
};
o.upcoming = function (ms, playerId) {
return ms.filter(function (m) {
return m.p.indexOf(playerId) >= 0 && !m.m
});
};
o.started = function (ms) {
return ms.some(function (m) {
return m.p.every($.gt(o.NONE)) && m.m; // not an automatically scored match
});
};
o.playable = function (m) {
return !m.p.some($.eq(o.NONE));
};
module.exports = o;