PageSourceSearch

https://coolubg2.github.io/js/calculate-playtime.js

js coolubg2.github.io collected 2026-10-03 08:49:47 UTC 3,766 bytes, 105 lines download raw bytes

1// INDEX DB NAMES ARE STORED IN INDEX.HTML below highlight/hover functions
2// Function to open the IndexedDB
3function openDB() {
4    return new Promise((resolve, reject) => {
5        const request = indexedDB.open(DB_NAME, DB_VERSION);
6
7        request.onupgradeneeded = function (event) {
8            const db = event.target.result;
9            // Create an object store for playtime logs if it doesn't exist
10            if (!db.objectStoreNames.contains(STORE_NAME)) {
11                db.createObjectStore(STORE_NAME, { keyPath: "gameId" });
12            }
13        };
14
15        request.onsuccess = function (event) {
16            resolve(event.target.result); // Resolve with the database
17        };
18
19        request.onerror = function (event) {
20            reject("Error opening IndexedDB: " + event.target.errorCode); // Reject if there's an error
21        };
22    });
23}
24
25// Function to get playtime logs for a specific game
26async function getPlaytimeLogs(gameId) {
27    const db = await openDB();
28    return new Promise((resolve, reject) => {
29        const transaction = db.transaction(STORE_NAME, "readonly");
30        const store = transaction.objectStore(STORE_NAME);
31        const request = store.get(gameId);
32
33        request.onsuccess = function (event) {
34            const result = event.target.result;
35            if (result) {
36                resolve(result.logs); // Resolve with the logs of the game
37            } else {
38                resolve([]); // Return empty array if no logs exist for the game
39            }
40        };
41
42        request.onerror = function (event) {
43            reject("Error fetching playtime logs: " + event.target.errorCode); // Reject if error occurs
44        };
45    });
46}
47
48// Function to update or add playtime logs for a specific game
49async function updatePlaytimeLogs(gameId, logEntry) {
50    const db = await openDB();
51    return new Promise((resolve, reject) => {
52        const transaction = db.transaction(STORE_NAME, "readwrite");
53        const store = transaction.objectStore(STORE_NAME);
54        const request = store.get(gameId);
55
56        request.onsuccess = function (event) {
57            const result = event.target.result;
58
59            if (result) {
60                // If game exists, add the new log entry
61                result.logs.push(logEntry);
62                store.put(result); // Update the game log entry
63            } else {
64                // If game doesn't exist, create a new entry
65                store.add({ gameId: gameId, logs: [logEntry] });
66            }
67
68            resolve("Log entry added/updated successfully");
69        };
70
71        request.onerror = function (event) {
72            reject("Error updating playtime logs: " + event.target.errorCode); // Reject if error occurs
73        };
74    });
75}
76
77// Playtime Calculation Function
78async function calculatePlaytime(gameId) {
79    try {
80        // Get playtime logs for the game from IndexedDB
81        const logs = await getPlaytimeLogs(gameId);
82
83        if (logs.length === 0) {
84            return "Never Played"; // If no logs exist, return "Never Played"
85        }
86
87        const gamePlaytime = logs.length;
88
89        if (gamePlaytime === 1) {
90            return "1 minute"; // If only one log entry, return 1 minute
91        }
92
93        // Convert minutes to hours if over 120 minutes
94        if (gamePlaytime > 120) {
95            let hours = gamePlaytime / 60;
96            hours = hours % 1 === 0 ? hours.toFixed(0) : hours.toFixed(1); // Remove decimal if .0
97            return `${hours} hours`;
98        }
99
100        return `${gamePlaytime} minutes`; // Otherwise, return minutes
101    } catch (error) {
102        console.error("Error calculating playtime:", error);
103        return "Never Played"; // Return "Never Played" if there's an error
104    }
105}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.