Did they even play?

Show play stats on Steam Gifts winners pages. Adapted from kelnage's Do You Even Play, Bro? script.

目前為 2017-07-31 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Did they even play?
// @namespace    https://www.steamgifts.com/user/lext
// @version      0.1
// @description  Show play stats on Steam Gifts winners pages. Adapted from kelnage's Do You Even Play, Bro? script.
// @author       Lex
// @match        https://www.steamgifts.com/giveaway/*/winners
// @require      http://code.jquery.com/jquery-3.2.1.min.js
// @connect      api.steampowered.com
// @grant        GM_xmlhttpRequest
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_deleteValue
// ==/UserScript==

(function() {
    'use strict';

    const PLAYTIME_URL = "https://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/"; // takes a steamid and API key
    const ACHIEVEMENTS_URL = "https://api.steampowered.com/ISteamUserStats/GetPlayerAchievements/v0001/"; // takes a steamid, appid and API key
    const SG_USER_URL = "https://www.steamgifts.com/user/";
    const GAME_ACHIEVEMENTS_URL = "http://steamcommunity.com/profiles/$1/stats/$2/achievements/";
    const PLAYTIME_CACHE_KEY = "PLAYTIME_CACHE_";
    const ACHIEVEMENT_CACHE_KEY = "ACHIEVEMENT_CACHE_";
    const API_KEY_REGEXP = /[0-9A-Z]{32}/;

    const APPID = $("div.featured__inner-wrap a.global__image-outer-wrap").attr('href').match(/\/(\d+)/)[1];

    var STEAM_API_KEY = GM_getValue("STEAM_API_KEY");

    if (STEAM_API_KEY == "undefined") {
        var apiDiv = $(`<form><input class="steamApiKey" style="margin-left: 15px; width: 300px" type="text" value="STEAM_API_KEY_HERE"/>
<input style="float:left;" name="btnSubmit" type="button" value="Submit"></input></form>`);
        apiDiv.find("input[name='btnSubmit']").click(function(){
            const api_key = $(this).parents("form:first").find("input.steamApiKey").val();
            if (api_key.match(API_KEY_REGEXP)) {
                GM_setValue("STEAM_API_KEY", api_key);
                $(this).parent().text(" - API key set");
            } else {
                alert("Error! Invalid API Key");
            }
        });
        $("div.page__heading__breadcrumbs").append(apiDiv);
        return;
    } else {
        var apiDelDiv = $(`<div style="margin-left: 15px"><a href="#">Delete API Key for Did they even play?</a></div>`);
        apiDelDiv.find("a").one("click", function(){
            $(this).parent().text("Deleted");
            GM_deleteValue("STEAM_API_KEY");
        });
        $("div.page__heading__breadcrumbs").append(apiDelDiv);
        main();
    }

    function fetchGamePlaytimes(steamID64, callback) {
        GM_xmlhttpRequest({
            "method": "GET",
            "url": PLAYTIME_URL + "?steamid=" + steamID64 + "&key=" + STEAM_API_KEY,
            "onload": function(response) {
                const data = JSON.parse(response.responseText);
                if (data && data.response)
                    callback(data.response.games);
                else
                    callback();
            }
        });
    }

    function fetchGamePlaytime(steamID64, gameid, callback) {
        fetchGamePlaytimes(steamID64, function(games) {
            for(var i = 0; i < games.length; i++) {
                if (games[i].appid == gameid)
                    return callback(games[i].playtime_forever); // playtime in minutes
            }
        });
    }

    function fetchAchievementStats(appid, steamID64, callback) {
        GM_xmlhttpRequest({
            "method": "GET",
            "url": ACHIEVEMENTS_URL + "?appid=" + appid + "&steamid=" + steamID64 + "&key=" + STEAM_API_KEY,
            "onload": function(response) {
                var data;
                try {
                    data = JSON.parse(response.responseText);
                    var achievements = data.playerstats.achievements;
                    if (achievements) {
                        const achieved = achievements.filter(function(achievement) { return achievement.achieved == 1; }).length;
                        const total = achievements.length;
                        callback({"achieved": achieved, "total": total});
                    } else {
                        callback({"achieved": 0, "total": 0});
                    }
                } catch(err) {
                    callback({"achieved": 0, "total": 0});
                }
            }
        });
    }

    function fetchSteamID (username, callback) {
        GM_xmlhttpRequest({
            "method": "GET",
            "url": SG_USER_URL + username,
            "onload": function(response) {
                const steamID = $('a[data-tooltip="Visit Steam Profile"]', response.responseText).attr("href").match(/\d{17}/)[0];
                callback(steamID);
            }
        });
    }

    function main() {
        $("p.table__column__heading a").each(function(){
            var self = $(this);
            fetchSteamID(self.text(), function(steamID) {
                const achUrl = GAME_ACHIEVEMENTS_URL.replace("$1", steamID).replace("$2", APPID);
                const achievementLink = $(`<a href="${achUrl}"> - <span class="DTEP_ACHIEVEMENTS"/> Achievements</a><span class="DTEP_PLAYTIME"/>`);
                self.parent().parent().append(achievementLink);

                fetchGamePlaytime(steamID, APPID, function(playtime) {
                    var hours = playtime / 60;
                    hours = +hours.toFixed(2);
                    self.parent().parent().find(".DTEP_PLAYTIME").text(", " + hours + " hours total");
                });

                fetchAchievementStats(APPID, steamID, function(achievements) {
                    self.parent().parent().find(".DTEP_ACHIEVEMENTS").text(achievements.achieved + " / " + achievements.total + " ");
                });
            });
        });
    }
})();