Skip to content
This repository has been archived by the owner on Dec 24, 2022. It is now read-only.

Commit

Permalink
Added online status, perm ban info, fix timeout, remove cache for unr…
Browse files Browse the repository at this point in the history
…egistered users
  • Loading branch information
cjmaxik committed May 31, 2021
1 parent 4dc486f commit 8c15a11
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 30 deletions.
36 changes: 32 additions & 4 deletions source/background.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import optionsStorage from './options-storage.js'
import { readFromCache, removeExpiredItems } from './cache'
import { readFromCache, removeExpiredItems, removeFromCache } from './cache'

// Listeners for a content scripts
browser.runtime.onMessage.addListener((request, _sender) => {
Expand Down Expand Up @@ -31,17 +31,29 @@ const delay = (key) => new Promise(resolve => setTimeout(resolve, key * 150 + Ma
* @param {String} request.steamId Steam ID
* @param {String} request.key Delay key
* @param {?Boolean} request.withGames Check for games if no TMP profile
* @param {?Boolean} request.withMap Check for online status
* @returns {Object}
*/
const queryPlayer = async (request) => {
const playerInfo = await readFromCache(request.steamId, async function () {
const key = `player:${request.steamId}`;
const playerInfo = await readFromCache(key, async function () {
await delay(request.key)
const response = await fetch(`https://api.truckersmp.com/v2/player/${request.steamId}?ref=truckersmp-steam-helper&v=${cacheBusting()}`)

return await response.json()
})

if ((!playerInfo.data || playerInfo.data.error) && request.withGames) {
playerInfo.games = await queryGames(request)
// Check if the user is not registered
if (!playerInfo.data || playerInfo.data.error) {
removeFromCache(key)

if (request.withGames) {
playerInfo.games = await queryGames(request)
}
} else {
if (request.withMap && !playerInfo.data.response.banned) {
playerInfo.online = await queryMap(playerInfo.data.response.id)
}
}

return playerInfo
Expand All @@ -63,6 +75,7 @@ const cacheBusting = () => {
* Check user games
* @param {Object} request Data object
* @param {String} request.steamId Steam ID
* @returns {Object}
*/
const queryGames = async (request) => {
const response = await fetch(`https://steamcommunity.com/profiles/${request.steamId}/games/?xml=1&v=${cacheBusting()}`)
Expand Down Expand Up @@ -123,6 +136,21 @@ const queryGames = async (request) => {
return games
}

/**
* Check user online status
* @param {string} id TruckersMP ID
* @returns {Object}
*/
const queryMap = async (id) => {
const mapData = await readFromCache(`map:${id}`, async function () {
const response = await fetch(`https://traffic.krashnz.com/api/v2/user/${id}?ref=truckersmp-steam-helper&v=${cacheBusting()}`)

return await response.json()
}, 1)

return !!(!mapData.data.error && mapData.data.response.online);
}

// Cache governor
removeExpiredItems()
browser.alarms.create({ periodInMinutes: 10.0 })
Expand Down
18 changes: 13 additions & 5 deletions source/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const writeToCache = (key, data, timeout) => {
* @param {String} key Cache key (Steam ID by default)
* @param {Function} callback Callback function (if data is not cached)
* @param {?Number} timeout Cache timeout (in minutes)
* @returns {Object}
* @returns {Object<Object, String>}
*/
const readFromCache = async (key, callback, timeout = 60) => {
const cachedData = JSON.parse(localStorage.getItem(key)) || null
Expand All @@ -28,17 +28,25 @@ const readFromCache = async (key, callback, timeout = 60) => {
}

// Execute a callback, put response in a new cache entry
localStorage.removeItem(key)
removeFromCache(key)
const data = await callback(key)
writeToCache(key, data, cacheTimeout)

// Return with timeout 0 to indicate an updated value
return {
data,
timeout: 0
timeout: cacheTimeout
}
}

/**
* Remove from cache
* @param key
*/
const removeFromCache = (key) => {
localStorage.removeItem(key);
}

/**
* Remove expired items
* Scheduled job
Expand All @@ -59,12 +67,12 @@ const removeExpiredItems = () => {
continue
}

localStorage.removeItem(key)
removeFromCache(key)
console.log('Deleted stale key:', key)
}

console.log('Done')
console.groupEnd()
}

export { readFromCache, removeExpiredItems }
export { readFromCache, removeFromCache, removeExpiredItems }
20 changes: 12 additions & 8 deletions source/contentScripts/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ const init = () => {
query: 'options'
}
).then((options) => {
let steamId = null
steamId = getSteamId()
const steamId = getSteamId()

if (!steamId) {
console.log('steamId cannot be found! Aborted.')
Expand Down Expand Up @@ -55,11 +54,12 @@ const queryPlayer = (steamId, options) => {
query: 'player',
steamId: steamId,
key: 0,
withGames: true
withGames: true,
withMap: true
}
).then((playerInfo) => {
if (playerInfo.data && !playerInfo.data.error) {
return renderProfile(playerInfo.data.response, options, playerInfo.data.timeout !== 0)
return renderProfile(playerInfo.data.response, playerInfo.online, options, playerInfo.timeout)
}
console.warn('Something wrong with TruckersMP data! Looking for the games on Steam profile...', playerInfo)

Expand Down Expand Up @@ -88,18 +88,22 @@ const renderNoProfile = (games, options) => {
/**
* Render profile with TruckersMP data
* @param {Object} player
* @param {Boolean} online
* @param {Object} options
* @param {Boolean} isCached
* @param {string} timeout
*/
const renderProfile = (player, options, isCached) => {
const renderProfile = (player, online, options, timeout) => {
if (player) {
player = currentTier(player)
}

timeout = new Date(timeout).toLocaleString()

const template = profileTemplate({
...player,
online,
...options,
isCached,
timeout,
iconURL: browser.extension.getURL('icons/tmp.png')
})

Expand Down Expand Up @@ -133,7 +137,7 @@ const insertTemplate = (template) => {
* @return {*}
*/
const currentTier = (player) => {
let currentTier = null
let currentTier

switch (player.patreon.tierId) {
case 3822370:
Expand Down
22 changes: 21 additions & 1 deletion source/css/profile.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,29 @@ div.tmp_block div.showcase_stat {

.tmp_banned {
float: right;
color: #000;
padding: 0 5px;
border-radius: 2px;
color: white !important;
background-color: #ca2842 !important;
}

.profile_customization_update_text {
line-height: 30px;
position: absolute;
right: 5px;
top: 0;
padding: 5px;
}

.showcase_stat.favoritegroup_ingame .value a {
color: inherit;
}

.showcase_stat.favoritegroup_ingame .value a:hover {
color: #66C0F4;
cursor: pointer;
}

.showcase_stat.favoritegroup_offline .value {
color: #a94847;
}
2 changes: 1 addition & 1 deletion source/templates/friend.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
<img src="{{ iconURL }}" width="10px" alt="Icon"> {{#if id}}{{groupName}}{{else}}¯\_(ツ)_/¯{{/if}}
</span>
{{#if banned}}
<span class="em_count tmp_banned">Banned</span>
<span class="em_count tmp_banned">{{#unless bannedUntil}}PERM {{/unless}}banned</span>
{{/if}}
4 changes: 2 additions & 2 deletions source/templates/invite.hbs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<br/>
<div class="invite_block_details leftLongInviterSmallText">
<img src="{{ iconURL }}" width="10px">
<img src="{{ iconURL }}" alt="Icon" width="10px">
<a href="https://truckersmp.com/user/{{ id }}" target="_blank" style="color: #ff1744 !important;">
{{ groupName }}
</a>
{{#if vtc}}{{#if vtc.inVTC}} | Member of <a href="https://truckersmp.com/vtc/{{ vtc.id }}" target="_blank">
{{ vtc.name }}
</a>{{/if}}{{/if}}
{{#if banned}} | <span class="tmp_banned">Banned</span>{{/if}}
{{#if banned}} | <span class="tmp_banned">{{#unless bannedUntil}}PERM {{/unless}}banned</span>{{/if}}
</div>
2 changes: 1 addition & 1 deletion source/templates/noProfile.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="profile_customization">
<div class="profile_customization_header" {{#if isCached}}data-tooltip-html="Cached for 1 hour" {{/if}}>
<div class="profile_customization_header">
<img src="{{ iconURL }}" width="16px" alt="Logo"> No TruckersMP account ¯\_(ツ)_/¯
</div>
<div class="profile_customization_block tmp_block">
Expand Down
38 changes: 30 additions & 8 deletions source/templates/profile.hbs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<div class="profile_customization">
<div class="profile_customization_header" {{#if isCached}}data-tooltip-html="Cached for 1 hour" {{/if}}>
<img src="{{ iconURL }}" width="16px" alt="Logo"> TruckersMP Info
<div class="profile_customization_header">
<span {{#if timeout}}data-tooltip-html="Cached until {{ timeout }}"{{/if}}><img src="{{ iconURL }}" width="16px" alt="Logo"> TruckersMP Info</span>
{{#if banned}}
<span class="tmp_banned">Banned</span>
<span class="tmp_banned" {{#if bannedUntil}}data-tooltip-html="Until {{ bannedUntil }}"{{/if}}>Banned{{#unless bannedUntil}} permanently!{{/unless}}</span>
{{/if}}
</div>

<div class="profile_customization_block {{#if banned}} tmp_banned_block {{/if}} tmp_block">
<div class="favoritegroup_showcase">
<div class="showcase_content_bg">
Expand All @@ -14,6 +15,7 @@
<img src="{{ avatar }}" alt="{{ nickname }}'s avatar">
</a>
</div>

<div class="favoritegroup_content">
<div class="favoritegroup_namerow ellipsis">
<a class="favoritegroup_name whiteLink" href="https://truckersmp.com/user/{{ id }}" target="_blank"
Expand All @@ -32,9 +34,9 @@
{{#if showPatreon}}{{#if patreon}}{{#with patreon}}
{{#unless hidden}}{{#if active}}{{#if currentTier}}
<div class="favoritegroup_description">
<span style="color: {{ color }};">
Active patron: {{ currentTier }}
</span>
<span style="color: {{ color }};">
Active patron: {{ currentTier }}
</span>
</div>
{{/if}}{{/if}}{{/unless}}
{{/with}}{{/if}}{{/if}}
Expand All @@ -56,12 +58,32 @@
<div class="value">{{ id }}</div>
<div class="label">TMP ID</div>
</div>

{{#if bansCount}}
<div class="showcase_stat favoritegroup_ingame">
<div class="value">{{bansCount}}</div>
<div class="showcase_stat favoritegroup_online">
<div class="value">{{ bansCount }}</div>
<div class="label">Active Bans</div>
</div>
{{/if}}

{{#unless banned}}
{{#if online}}
<div class="showcase_stat favoritegroup_ingame">
<div class="value">
<a href="https://map.truckersmp.com/follow/{{ id }}?ref=truckersmp-steam-helper" target="_blank" data-tooltip-html="Click to follow on the map">Online</a>
</div>
<div class="label">TMP Status</div>
</div>
{{/if}}

{{#unless online}}
<div class="showcase_stat favoritegroup_offline">
<div class="value">Offline</div>
<div class="label">TMP Status</div>
</div>
{{/unless}}
{{/unless}}

<div style="clear: left;"></div>
</div>
</div>
Expand Down

0 comments on commit 8c15a11

Please sign in to comment.