Skip to content

Instantly share code, notes, and snippets.

@kus
Last active November 1, 2019 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kus/530584d96d8f900838303934ce4056d1 to your computer and use it in GitHub Desktop.
Save kus/530584d96d8f900838303934ce4056d1 to your computer and use it in GitHub Desktop.
Find out Steam games hour played grouped
// Open https://steamcommunity.com/my/games/?tab=all and make sure you are logged in to Steam.
// Open your browsers console (Ctrl + Shift + J on Chrome or Ctrl + Shift + K on Firefox)
// and paste all of this code (select all) in the text box (bottom) and hit enter
// If you get an error from the script you likely aren't running a modern browser
// you can paste the code here https://babeljs.io/repl and copy the output and run that.
(() => {
const SHOW_TOP_GAMES = 5;
const RE_HOURS = /^[0-9.,]+/;
const RE_CLEAN_HOURS = /[^0-9.]+/g;
let totalHours = 0;
let games = Array.from(document.querySelectorAll('.gameListRow')).map(v => {
let hours = 0;
const match = (v.querySelector('.hours_played').innerHTML || '').match(RE_HOURS);
if (match) {
hours = +match[0].replace(RE_CLEAN_HOURS, '');
}
totalHours += hours;
return {
name: v.querySelector('.gameListRowItemName').innerHTML.replace(/&/g, '&'),
hours
}
});
const totalGames = games.length;
games.sort((a,b) => (a.hours > b.hours) ? 1 : ((b.hours > a.hours) ? -1 : 0));
const max = games[games.length - 1].hours;
let keys = [0, 0.1, 0.5, 1];
let groups = {
0: 0,
0.1: 0,
0.5: 0,
1: 0
};
let top = {};
for(let i = 1; i <= Math.ceil(Math.log(max)/Math.log(2)); i++) {
const k = Math.pow(2, i);
keys.push(k);
groups[k] = 0;
}
games.forEach((v, i) => {
for (let j = 0; j < keys.length; j++) {
if (v.hours >= keys[j] && v.hours < keys[j+1]) {
groups[keys[j]]++;
if (totalGames - i <= SHOW_TOP_GAMES) {
if (!top[keys[j]]) {
top[keys[j]] = [];
}
top[keys[j]].push(`${v.name}: ${v.hours}`);
}
break;
}
}
});
const str = `Total games: ${totalGames}\nTotal hours: ${+totalHours.toFixed(2)}\nHours played:\n${keys.map((k, i) => groups[k] > 0 ? `${k} - ${(+keys[i+1] - 0.01).toFixed(2)} hours: ${groups[k]} (${(groups[k] / totalGames * 100).toFixed(1)}%)\n${top[k] ? ' - ' + top[k].join('\n - ') + '\n' : ''}` : '').join('')}`;
window.prompt(str, str);
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment