feat(stores): EA and Ubisoft stores browse their catalogs, and EA's Get for free works in the app (#2527)

* feat(stores): EA and Ubisoft stores browse their catalogs with prices, deals and search

* feat(stores): Get for free adds EA's free games to the account in the app

* fix(stores): an owned EA game matches its store card by its full product name
This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-11-12 12:08:33 -05:00
1 parent 08a8de9bc4
commit a5712c0d20
47 files changed
+614 -12

No files matched your search

@@ -106,6 +106,7 @@ fun detailCatalogItem(
library.firstOrNull { it.store == store && it.id == id }
?: found.firstOrNull { it.store == store && it.id == id }
?: shelves?.all?.firstOrNull { it.store == store && it.id == id }
?: shelves?.all?.firstOrNull { it.store == store && it.extra[StoreFront.STORE_CARD] == id }
fun mergeItems(old: List<CatalogItem>?, new: List<CatalogItem>): List<CatalogItem> {
if (old.isNullOrEmpty()) return new
@@ -0,0 +1,119 @@
package com.droiddeck.launcher.stores
import android.content.SharedPreferences
import org.json.JSONArray
import org.json.JSONObject
import java.text.Normalizer
import java.util.Locale
/** Store shelves fetched from a public catalog, kept in memory and on disk between refreshes. */
internal class StoreFront(
private val store: Store,
private val prefs: () -> SharedPreferences,
private val fetch: () -> Catalog,
private val key: String = "store_front",
private val ttlMs: Long = 3 * 60 * 60 * 1000L,
) {
/** One refresh: the shelves and every game the store lists, for search. */
class Catalog(val shelves: StoreShelves, val all: List<CatalogItem> = shelves.all)
@Volatile private var memory: Catalog? = null
@Volatile private var memoryAt = 0L
fun cached(): StoreShelves? = cachedCatalog()?.shelves
fun everything(): List<CatalogItem> = cachedCatalog()?.all.orEmpty()
private fun cachedCatalog(): Catalog? = memory ?: decode(prefs().getString(key, null))?.also { memory = it }
/** The shelves, refreshed when stale; a refresh that fails keeps the last good set. */
fun shelves(force: Boolean): StoreShelves? {
val now = System.currentTimeMillis()
if (!force) {
memory?.takeIf { now - memoryAt in 0 until ttlMs }?.let { return it.shelves }
val at = prefs().getLong("${key}_at", 0L)
if (now - at in 0 until ttlMs) decode(prefs().getString(key, null))?.let { memory = it; memoryAt = at; return it.shelves }
}
val fresh = runCatching(fetch).onFailure { StoresState.logLine("${store.label}: store shelves failed: ${it.javaClass.simpleName} ${it.message.orEmpty().take(160)}") }
.getOrNull()?.takeUnless { it.shelves.isEmpty }
?: return cached()
prefs().edit().putString(key, encode(fresh)).putLong("${key}_at", now).apply()
memory = fresh
memoryAt = now
return fresh.shelves
}
private fun encode(c: Catalog): String = JSONObject().apply {
val s = c.shelves
listOf(s.whatsNew, s.deals, s.free, s.trending).zip(SHELVES).forEach { (items, k) -> put(k, JSONArray(items.map { it.id })) }
put("art", JSONArray(s.art))
put("items", JSONArray().apply { (c.all + s.all).distinctBy { it.id }.forEach { put(itemJson(it)) } })
}.toString()
private fun decode(text: String?): Catalog? {
val o = text?.let { runCatching { JSONObject(it) }.getOrNull() } ?: return null
val items = o.optJSONArray("items")?.let { a -> (0 until a.length()).mapNotNull { a.optJSONObject(it)?.let(::itemFrom) } }.orEmpty()
val byId = items.associateBy { it.id }
fun shelf(k: String) = o.optJSONArray(k)?.let { a -> (0 until a.length()).mapNotNull { byId[a.optString(it)] } }.orEmpty()
val art = o.optJSONArray("art")?.let { a -> List(a.length()) { a.optString(it) }.filter { it.isNotEmpty() } }.orEmpty()
val shelves = StoreShelves(shelf("new"), shelf("deals"), shelf("free"), shelf("trending"), art).takeUnless { it.isEmpty } ?: return null
return Catalog(shelves, items)
}
private fun itemJson(i: CatalogItem) = JSONObject()
.put("id", i.id).put("title", i.title).put("image", i.imageUrl.orEmpty()).put("tall", i.tallImageUrl.orEmpty()).put("tags", i.tags)
.put("free", i.isFree).put("hasPrice", i.hasPrice).put("final", i.finalPrice).put("orig", i.originalPrice).put("disc", i.discountPercent)
.put("url", i.storeUrl).put("dev", i.developer).put("pub", i.publisher).put("rel", i.releaseDate).put("desc", i.description)
.put("mature", i.mature).put("logo", i.logoUrl.orEmpty()).put("extra", JSONObject(i.extra as Map<*, *>))
private fun itemFrom(o: JSONObject): CatalogItem? {
val extra = LinkedHashMap<String, String>()
o.optJSONObject("extra")?.let { e -> e.keys().forEach { k -> extra[k] = e.optString(k) } }
return CatalogItem(
store = store, id = o.optString("id").ifEmpty { return null }, title = o.optString("title").ifEmpty { return null },
imageUrl = o.optString("image").ifEmpty { null }, tallImageUrl = o.optString("tall").ifEmpty { null }, tags = o.optString("tags"),
isFree = o.optBoolean("free"), hasPrice = o.optBoolean("hasPrice"), finalPrice = o.optString("final"), originalPrice = o.optString("orig"),
discountPercent = o.optInt("disc"), storeUrl = o.optString("url"), developer = o.optString("dev"), publisher = o.optString("pub"),
releaseDate = o.optString("rel"), description = o.optString("desc"), mature = o.optBoolean("mature"),
logoUrl = o.optString("logo").ifEmpty { null }, extra = extra,
)
}
companion object {
/** On an owned game standing in for a store card: the card's id, so a page opened on the card still finds it. */
const val STORE_CARD = "storeCard"
private val SHELVES = listOf("new", "deals", "free", "trending")
private val MARKS = Regex("[\\u2122\\u00AE\\u00A9]")
private val COMBINING = Regex("\\p{M}+")
private val NOT_WORD = Regex("[^\\p{L}\\p{N}]+")
/** A title reduced to letters and digits, so "Far Cry® 6" and "Far Cry 6" meet, and "Pokémon" meets "Pokemon". */
fun titleKey(title: String): String =
Normalizer.normalize(title.replace(MARKS, ""), Normalizer.Form.NFD).replace(COMBINING, "")
.lowercase(Locale.ROOT).replace(NOT_WORD, " ").trim()
/**
* Shelf cards the account owns become its library entry, which carries what installing needs,
* keeping the shelf's art and price.
*/
fun markOwned(s: StoreShelves, library: List<CatalogItem>?, match: (CatalogItem, CatalogItem) -> Boolean): StoreShelves {
if (library.isNullOrEmpty()) return s
fun mark(list: List<CatalogItem>) = markOwned(list, library, match)
return StoreShelves(mark(s.whatsNew), mark(s.deals), mark(s.free), mark(s.trending), s.art)
}
fun markOwned(list: List<CatalogItem>, library: List<CatalogItem>?, match: (CatalogItem, CatalogItem) -> Boolean): List<CatalogItem> {
if (library.isNullOrEmpty()) return list
return list.map { shelf ->
val owned = library.firstOrNull { match(shelf, it) } ?: return@map shelf
owned.copy(
imageUrl = shelf.imageUrl ?: owned.imageUrl, tallImageUrl = owned.tallImageUrl ?: shelf.tallImageUrl,
tags = owned.tags.ifBlank { shelf.tags }, description = owned.description.ifBlank { shelf.description },
releaseDate = owned.releaseDate.ifBlank { shelf.releaseDate }, storeUrl = shelf.storeUrl.ifBlank { owned.storeUrl },
developer = owned.developer.ifBlank { shelf.developer }, mature = shelf.mature,
extra = owned.extra + (STORE_CARD to shelf.id),
)
}.distinctBy { it.id }
}
}
}
@@ -236,6 +236,27 @@ object StoresState {
var itchPicking by mutableStateOf<CatalogItem?>(null)
var gameJoltPicking by mutableStateOf<CatalogItem?>(null)
/** Store cards whose free game is being added to the account. */
val claiming = mutableStateMapOf<String, Boolean>()
fun canClaim(item: CatalogItem): Boolean = !item.accessible && isSignedIn(item.store) && backends[item.store]?.canClaim(item) == true
/** Adds a free game to the account, then refreshes the library so it can be installed; [failed] runs when the store refused. */
fun claim(context: Context, item: CatalogItem, failed: () -> Unit) {
val backend = backends[item.store] ?: return failed()
if (claiming.put(item.key, true) == true) return
val app = context.applicationContext
background("stores-claim", onFailure = { main.post { claiming.remove(item.key); failed() } }) {
val ok = backend.claim(app, item)
logLine("${item.store.label}: ${item.title} ${if (ok) "added to the library" else "could not be added; opening the store page"}")
if (ok) backend.syncLibrary(app, true)
main.post {
claiming.remove(item.key)
if (!ok) failed()
}
}
}
fun requestInstall(context: Context, item: CatalogItem) {
askForNotifications(context)
if (item.store == Store.ITCH && item.extra["uploadId"].isNullOrEmpty()) { itchPicking = item; return }
@@ -359,6 +380,12 @@ interface StoreBackend {
fun syncLibrary(context: Context, force: Boolean)
fun loadShelves(context: Context, force: Boolean)
fun search(context: Context, query: String): List<CatalogItem> = emptyList()
/** Whether this store can add a free game to the account itself, rather than through its website. */
fun canClaim(item: CatalogItem): Boolean = false
/** Adds a free game to the account; true once the store has it in the account's library. */
fun claim(context: Context, item: CatalogItem): Boolean = false
fun install(context: Context, item: CatalogItem, root: java.io.File)
fun uninstall(context: Context, game: InstalledStoreGame)
}
@@ -22,6 +22,7 @@ object EaApi {
val downloadable: Boolean,
val trial: Boolean,
val ownership: List<String>,
val productName: String = "",
)
class Offer(
@@ -109,6 +110,7 @@ object EaApi {
downloadable = product.optBoolean("downloadable", true),
trial = product.optJSONObject("trialDetails")?.optString("trialType").orEmpty().let { it.isNotEmpty() && it != "null" },
ownership = (0 until (methods?.length() ?: 0)).map { methods!!.optString(it) },
productName = product.optString("name").replace("\n", " ").trim(),
)
}
val next = owned.optString("next").takeIf { it.isNotBlank() && it != "null" }
@@ -197,6 +199,18 @@ object EaApi {
}
}
/** The free offer EA sells under a store slug, or null when the game has none. */
fun freeOffer(token: String, slug: String): String? {
val variables = JSONObject().put("slug", slug).put("locale", "en-us").put("overrideCountryCode", "US")
val product = call(token, "GetGameProductOfferIds", variables).optJSONObject("game")?.optJSONObject("lowestPriceGameProduct") ?: return null
return product.optString("originOfferId").takeIf { product.optBoolean("isFree") && it.isNotBlank() && it != "null" }
}
/** Adds a free offer to the account, as the EA app's "Get for free" does; the answer names what EA refused. */
fun grantEntitlement(token: String, offerId: String): JSONObject =
call(token, "grantEntitlement", JSONObject().put("input", JSONObject().put("offerId", offerId).put("source", EaAuth.CLIENT_ID)))
.optJSONObject("grantEntitlement") ?: throw EaApiException(0, "EA gave no answer to the grant")
fun legacyEntitlements(token: String): List<JSONObject> {
val out = ArrayList<JSONObject>()
var next: String? = "0"
@@ -11,6 +11,7 @@ import com.droiddeck.launcher.stores.InstalledStoreGame
import com.droiddeck.launcher.stores.LoginFlows
import com.droiddeck.launcher.stores.Store
import com.droiddeck.launcher.stores.StoreBackend
import com.droiddeck.launcher.stores.StoreFront
import com.droiddeck.launcher.stores.StoreInstallRoot
import com.droiddeck.launcher.stores.StoreLoginActivity
import com.droiddeck.launcher.stores.StoresState
@@ -40,6 +41,8 @@ object EaBackend : StoreBackend {
val app = context.applicationContext
EaAuth.signOut(app)
EaLibrary.reset(app)
signOuts.incrementAndGet()
StoresState.post { StoresState.shelves.remove(store) }
EaUpdates.available.clear()
runCatching { WebCookies.expire(EaLogin.SIGN_IN_URLS, ".ea.com", listOf("/")) {} }
}
@@ -50,7 +53,32 @@ object EaBackend : StoreBackend {
Thread({ EaLibrary.sync(app, force) }, "ea-sync").start()
}
override fun loadShelves(context: Context, force: Boolean) {}
override fun loadShelves(context: Context, force: Boolean) {
val app = context.applicationContext
val account = signOuts.get()
EaStore.cached(app)?.let { s -> StoresState.post { StoresState.shelves[store] = StoreFront.markOwned(s, StoresState.library[store], EaStore::sameGame) } }
Thread({
val shelves = EaStore.featured(app, force) ?: return@Thread
StoresState.post { if (signOuts.get() == account) StoresState.shelves[store] = StoreFront.markOwned(shelves, StoresState.library[store], EaStore::sameGame) }
}, "ea-shelves").start()
}
private val signOuts = java.util.concurrent.atomic.AtomicInteger()
override fun canClaim(item: CatalogItem): Boolean = item.isFree && !item.extra["slug"].isNullOrEmpty()
override fun claim(context: Context, item: CatalogItem): Boolean {
val app = context.applicationContext
val token = EaAuth.accessToken(app) ?: return false
val slug = item.extra["slug"] ?: return false
val offer = EaApi.freeOffer(token, slug) ?: run { StoresState.logLine("ea: no free offer for $slug"); return false }
val answer = EaApi.grantEntitlement(token, offer)
StoresState.logLine("ea: grant $offer for $slug: $answer")
return answer.optBoolean("success") || answer.optJSONArray("entitledOfferIds")?.let { ids -> (0 until ids.length()).any { ids.optString(it) == offer } } == true
}
override fun search(context: Context, query: String): List<CatalogItem> =
StoreFront.markOwned(EaStore.search(context.applicationContext, query), StoresState.library[store], EaStore::sameGame)
override fun install(context: Context, item: CatalogItem, root: java.io.File) {
val app = context.applicationContext
@@ -5,8 +5,8 @@ import android.content.SharedPreferences
import com.droiddeck.launcher.R
import com.droiddeck.launcher.stores.CatalogItem
import com.droiddeck.launcher.stores.Store
import com.droiddeck.launcher.stores.StoreFront
import com.droiddeck.launcher.stores.StoreNet
import com.droiddeck.launcher.stores.StoreShelves
import com.droiddeck.launcher.stores.StoresState
import com.droiddeck.launcher.stores.mergeItems
import org.json.JSONArray
@@ -36,13 +36,14 @@ data class EaGame(
val masterTitleId: String = "",
val cloudSaves: String = "",
val locales: List<String> = emptyList(),
val productName: String = "",
) {
fun toJson(): JSONObject = JSONObject()
.put("offerId", offerId).put("contentId", contentId).put("title", title).put("slug", slug)
.put("tall", tall).put("wide", wide).put("logo", logo).put("version", version)
.put("executePath", executePath).put("installCheck", installCheck).put("parameters", parameters)
.put("multiplayerId", multiplayerId).put("masterTitleId", masterTitleId).put("cloudSaves", cloudSaves)
.put("locales", JSONArray(locales))
.put("locales", JSONArray(locales)).put("productName", productName)
companion object {
fun fromJson(o: JSONObject): EaGame? {
@@ -63,14 +64,15 @@ data class EaGame(
masterTitleId = o.optString("masterTitleId"),
cloudSaves = o.optString("cloudSaves"),
locales = (0 until (locales?.length() ?: 0)).map { locales!!.optString(it) },
productName = o.optString("productName"),
)
}
}
}
object EaLibrary {
private const val KEY_CACHE = "library_cache_v2"
private const val KEY_SYNCED_AT = "library_synced_at_v2"
private const val KEY_CACHE = "library_cache_v3"
private const val KEY_SYNCED_AT = "library_synced_at_v3"
private const val THROTTLE_MS = 6 * 60 * 60 * 1000L
private const val ART_TTL_MS = 7 * 24 * 60 * 60 * 1000L
private const val ART_THREADS = 4
@@ -118,7 +120,7 @@ object EaLibrary {
owned = true,
sizeBytes = prefs.getLong("size_${g.offerId}", 0L),
storeUrl = STORE_URL,
extra = mapOf("contentId" to g.contentId, "slug" to g.slug, "version" to g.version),
extra = mapOf("contentId" to g.contentId, "slug" to g.slug, "version" to g.version, "productName" to g.productName),
logoUrl = g.logo.ifEmpty { null },
)
}
@@ -199,8 +201,7 @@ object EaLibrary {
StoresState.post {
if (gen != generation.get()) return@post
StoresState.library[store] = mergeItems(StoresState.library[store], items)
val installed = StoresState.installed.filter { it.sidecar.store == store }.map { it.sidecar.id }.toSet()
StoresState.shelves[store] = StoreShelves(trending = items.filterNot { it.id in installed }, art = StoresState.shelves[store]?.art.orEmpty())
StoresState.shelves[store]?.let { StoresState.shelves[store] = StoreFront.markOwned(it, items, EaStore::sameGame) }
}
}
@@ -271,6 +272,7 @@ object EaLibrary {
tall = img?.tall.orEmpty(), wide = img?.wide.orEmpty(), logo = img?.logo.orEmpty(), version = offer.gameVersion,
executePath = offer.executePathOverride, installCheck = offer.installCheckOverride, parameters = offer.executeParameters,
multiplayerId = offer.multiplayerId, masterTitleId = offer.masterTitleId, cloudSaves = offer.cloudSaves, locales = offer.locales,
productName = o.productName,
)
}
if (!storeCache(app, games, gen)) return
@@ -0,0 +1,120 @@
package com.droiddeck.launcher.stores.ea
import android.content.Context
import com.droiddeck.launcher.stores.CatalogItem
import com.droiddeck.launcher.stores.Store
import com.droiddeck.launcher.stores.StoreFront
import com.droiddeck.launcher.stores.StoreNet
import com.droiddeck.launcher.stores.StoreShelves
import com.droiddeck.launcher.stores.cleanStoreText
import org.json.JSONObject
import java.time.Instant
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
/** EA's PC catalog, read from the game pages ea.com renders for the EA app's games. */
object EaStore {
private const val SITE = "https://www.ea.com"
private const val CATALOG = "$SITE/games/library/pc-download"
private const val PAGE_SIZE = 12
private const val MAX_PAGES = 20
private const val SHELF = 24
private val NEXT_DATA = Regex("""<script id="__NEXT_DATA__" type="application/json">(.*?)</script>""", RegexOption.DOT_MATCHES_ALL)
private var front: StoreFront? = null
private fun front(context: Context): StoreFront = synchronized(this) {
front ?: StoreFront(Store.EA, { EaPrefs.get(context) }, { fetchAll().let { StoreFront.Catalog(shelves(it), it) } }, key = "store_front_catalog").also { front = it }
}
fun cached(context: Context): StoreShelves? = front(context).cached()
fun featured(context: Context, force: Boolean): StoreShelves? = front(context).shelves(force)
fun search(context: Context, query: String): List<CatalogItem> {
val q = StoreFront.titleKey(query)
if (q.length < 2) return emptyList()
return front(context).everything().filter { StoreFront.titleKey(it.title).contains(q) }
}
/** Whether a store card and a library entry are the same game. */
fun sameGame(shelf: CatalogItem, owned: CatalogItem): Boolean {
val slug = shelf.extra["slug"].orEmpty()
if (slug.isNotEmpty() && slug == owned.extra["slug"]) return true
val shelfKey = StoreFront.titleKey(shelf.title).ifEmpty { return false }
return listOf(owned.title, owned.extra["productName"].orEmpty()).any { StoreFront.titleKey(it) == shelfKey }
}
/**
* The device's currency. ea.com's pages are cached with whichever region asked first, so one page can carry
* roubles and the next dollars; a price in another currency is left off rather than shown wrong.
*/
private fun localCurrency(): String =
runCatching { java.util.Currency.getInstance(java.util.Locale.getDefault()).currencyCode }.getOrNull() ?: "USD"
internal fun parsePage(html: String, currency: String = localCurrency()): Pair<List<CatalogItem>, Int> {
val data = NEXT_DATA.find(html)?.groupValues?.get(1) ?: return emptyList<CatalogItem>() to 0
val props = JSONObject(data).optJSONObject("props")?.optJSONObject("pageProps") ?: return emptyList<CatalogItem>() to 0
val games = props.optJSONArray("games") ?: return emptyList<CatalogItem>() to 0
val items = (0 until games.length()).mapNotNull { games.optJSONObject(it) }.mapNotNull { item(it, currency) }
return items to props.optInt("totalItems", items.size)
}
internal fun item(g: JSONObject, currency: String): CatalogItem? {
fun s(o: JSONObject?, k: String) = o?.optString(k)?.takeIf { it.isNotBlank() && it != "null" }
val slug = s(g, "slug") ?: return null
val title = s(g, "title")?.replace("\n", " ")?.trim() ?: return null
val art = g.optJSONObject("packArt")
val price = g.optJSONObject("price")
val total = s(price, "displayTotal").orEmpty()
val now = s(price, "displayTotalWithDiscount") ?: total
val discount = price?.optInt("discountPercentage") ?: 0
val free = g.optBoolean("isFreeToPlay") || now.filter { it.isDigit() }.trimStart('0').isEmpty() && now.isNotEmpty()
val shown = !free && s(price, "currency").equals(currency, true)
val genres = g.optJSONArray("genres")
val franchise = s(g, "franchiseSlug")
return CatalogItem(
store = Store.EA, id = "ea-store:$slug", title = title,
imageUrl = s(art, "ar16X9") ?: s(art, "ar2X1"), tallImageUrl = s(art, "ar2X3") ?: s(art, "ar3X4"),
tags = (0 until (genres?.length() ?: 0)).mapNotNull { genres!!.optJSONObject(it)?.optString("name")?.takeIf(String::isNotBlank) }.take(3).joinToString(", "),
isFree = free, hasPrice = shown, finalPrice = if (shown) now else "",
originalPrice = if (shown && discount > 0) total else "", discountPercent = discount.coerceAtLeast(0),
storeUrl = if (franchise != null) "$SITE/games/$franchise/$slug" else "$SITE/games/$slug",
publisher = "Electronic Arts", releaseDate = s(g, "releaseDate")?.take(10).orEmpty(),
description = cleanStoreText(s(g, "shortDescription")),
extra = buildMap { put("slug", slug); s(g, "tier")?.let { put("tier", it) } },
)
}
private fun page(n: Int): Pair<List<CatalogItem>, Int>? =
StoreNet.get(if (n == 1) CATALOG else "$CATALOG?page=$n", headers = mapOf("Accept" to "text/html"))?.let(::parsePage)
/** Every page of the catalog; a page that fails fails the whole refresh, so a partial list never replaces a good one. */
private fun fetchAll(): List<CatalogItem> {
val (first, total) = page(1) ?: throw java.io.IOException("ea.com catalog page 1 failed")
val pages = ((total + PAGE_SIZE - 1) / PAGE_SIZE).coerceIn(1, MAX_PAGES)
val rest = java.util.concurrent.ConcurrentHashMap<Int, List<CatalogItem>>()
val pool = Executors.newFixedThreadPool(4)
try {
for (n in 2..pages) pool.execute { page(n)?.first?.let { rest[n] = it } }
} finally {
pool.shutdown()
if (!pool.awaitTermination(1, TimeUnit.MINUTES)) pool.shutdownNow()
}
(2..pages).firstOrNull { !rest.containsKey(it) }?.let { throw java.io.IOException("ea.com catalog page $it failed") }
return (first + (2..pages).flatMap { rest.getValue(it) }).distinctBy { it.id }
}
internal fun shelves(all: List<CatalogItem>, now: Instant = Instant.now()): StoreShelves {
val today = now.toString().take(10)
val released = all.filter { it.releaseDate.isNotEmpty() && it.releaseDate <= today }
val headline = released.filter { it.extra["tier"] == "Tier1" || it.extra["tier"] == "Tier2" }
return StoreShelves(
whatsNew = released.sortedByDescending { it.releaseDate }.take(SHELF),
deals = released.filter { it.discountPercent > 0 }.sortedByDescending { it.discountPercent }.take(SHELF),
free = released.filter { it.isFree }.take(SHELF),
trending = (headline + released).distinctBy { it.id }.take(SHELF),
art = all.mapNotNull { it.imageUrl }.take(SHELF),
)
}
}
@@ -10,6 +10,7 @@ import com.droiddeck.launcher.stores.InstalledStoreGame
import com.droiddeck.launcher.stores.LoginFlows
import com.droiddeck.launcher.stores.Store
import com.droiddeck.launcher.stores.StoreBackend
import com.droiddeck.launcher.stores.StoreFront
import com.droiddeck.launcher.stores.StoreInstallRoot
import com.droiddeck.launcher.stores.download.DownloadEntry
import com.droiddeck.launcher.stores.download.DownloadQueue
@@ -44,7 +45,18 @@ object UbisoftBackend : StoreBackend {
Thread({ UbisoftLibrary.sync(app, force) }, "ubisoft-sync").start()
}
override fun loadShelves(context: Context, force: Boolean) {}
override fun loadShelves(context: Context, force: Boolean) {
val app = context.applicationContext
val account = UbisoftLibrary.account()
UbisoftStore.cached(app)?.let { s -> StoresState.post { StoresState.shelves[store] = StoreFront.markOwned(s, StoresState.library[store], UbisoftStore::sameGame) } }
Thread({
val shelves = UbisoftStore.featured(app, force) ?: return@Thread
StoresState.post { if (UbisoftLibrary.account() == account) StoresState.shelves[store] = StoreFront.markOwned(shelves, StoresState.library[store], UbisoftStore::sameGame) }
}, "ubisoft-shelves").start()
}
override fun search(context: Context, query: String): List<CatalogItem> =
StoreFront.markOwned(UbisoftStore.search(query), StoresState.library[store], UbisoftStore::sameGame)
override fun install(context: Context, item: CatalogItem, root: java.io.File) {
val app = context.applicationContext
@@ -97,6 +109,9 @@ object UbisoftLibrary {
StoresState.post { StoresState.shelves.remove(Store.UBISOFT) }
}
/** Changes on every sign-out, so work begun for the old account can tell. */
fun account(): Int = generation.get()
private fun post(gen: Int, block: () -> Unit) = StoresState.post { if (generation.get() == gen) block() }
internal fun parsePage(body: String): Pair<List<CatalogItem>, Int> {
@@ -224,8 +239,7 @@ object UbisoftLibrary {
StoresState.status.remove(Store.UBISOFT)
StoresState.problems.remove(Store.UBISOFT)
StoresState.library[Store.UBISOFT] = games
StoresState.shelves[Store.UBISOFT] = com.droiddeck.launcher.stores.StoreShelves(
trending = games.sortedByDescending { it.extra.containsKey("productId") }, art = games.mapNotNull { it.imageUrl })
StoresState.shelves[Store.UBISOFT]?.let { StoresState.shelves[Store.UBISOFT] = StoreFront.markOwned(it, games, UbisoftStore::sameGame) }
}
StoresState.logLine("Ubisoft: ${games.size} owned games")
} catch (e: Exception) {
@@ -0,0 +1,129 @@
package com.droiddeck.launcher.stores.ubisoft
import android.content.Context
import android.net.Uri
import com.droiddeck.launcher.stores.CatalogItem
import com.droiddeck.launcher.stores.Store
import com.droiddeck.launcher.stores.StoreFront
import com.droiddeck.launcher.stores.StoreNet
import com.droiddeck.launcher.stores.StoreShelves
import com.droiddeck.launcher.stores.cleanStoreText
import com.droiddeck.launcher.stores.matureByAge
import org.json.JSONArray
import org.json.JSONObject
import java.text.NumberFormat
import java.time.Instant
import java.util.Currency
import java.util.Locale
/** Ubisoft's PC catalog, from the product search the Ubisoft Store website uses. */
object UbisoftStore {
/** The Ubisoft Store website's own read-only search key, sent by every store page. */
private const val APP = "XELY3U4LOD"
private const val KEY = "5638539fd9edb8f2c6b024b49ec375bd"
private const val SHELF = 24
private val RATING_AGE = mapOf("esrb-m" to 17, "esrb-ao" to 18, "pegi-18" to 18, "pegi-16" to 16, "usk-18" to 18)
/** The store sites with an English catalog: site, index locale, currency. */
private val SITES = mapOf(
"US" to Triple("us", "en_US", "USD"), "GB" to Triple("uk", "en_GB", "GBP"),
"CA" to Triple("ca", "en_CA", "CAD"), "AU" to Triple("anz", "en_AU", "AUD"), "NZ" to Triple("anz", "en_AU", "NZD"),
)
private fun site() = SITES[Locale.getDefault().country.uppercase(Locale.ROOT)] ?: SITES.getValue("US")
private fun index(site: Triple<String, String, String>, order: String) = "production__${site.first}_ubisoft__products__${site.second}__$order"
private val fronts = HashMap<Triple<String, String, String>, StoreFront>()
/** Each store site keeps its own shelves, so a region change never shows another region's prices. */
private fun front(context: Context): StoreFront = synchronized(fronts) {
val site = site()
fronts.getOrPut(site) {
StoreFront(Store.UBISOFT, { context.applicationContext.getSharedPreferences("stores.ubisoft", Context.MODE_PRIVATE) },
{ fetch(site) }, key = "store_front_${site.first}_${site.third}")
}
}
fun cached(context: Context): StoreShelves? = front(context).cached()
fun featured(context: Context, force: Boolean): StoreShelves? = front(context).shelves(force)
fun search(query: String): List<CatalogItem> {
val term = query.trim()
if (term.length < 2) return emptyList()
return query(site(), "best_sellers", 40, query = term).orEmpty()
}
/** Whether a store card and a library entry are the same game. */
fun sameGame(shelf: CatalogItem, owned: CatalogItem): Boolean {
val product = shelf.extra["productId"].orEmpty()
if (product.isNotEmpty() && product == owned.extra["productId"]) return true
val key = StoreFront.titleKey(owned.title)
return key.isNotEmpty() && key == baseTitle(shelf.title)
}
/** A product title without its edition, so "Far Cry® 6 - Gold Edition" is "far cry 6". */
internal fun baseTitle(title: String): String = StoreFront.titleKey(title.substringBefore(" - "))
/** One page of the index, or null when Ubisoft did not answer. */
private fun query(site: Triple<String, String, String>, order: String, hits: Int, query: String = "", filters: String = "", facets: List<String> = emptyList()): List<CatalogItem>? {
val facetFilters = JSONArray().put("product_type:Games").put("Platform:PC (Digital)").apply { facets.forEach { put(it) } }
val url = Uri.parse("https://$APP-dsn.algolia.net/1/indexes/${index(site, order)}").buildUpon()
.appendQueryParameter("query", query)
.appendQueryParameter("hitsPerPage", (hits * 2).toString())
.appendQueryParameter("facetFilters", facetFilters.toString())
.apply { if (filters.isNotEmpty()) appendQueryParameter("filters", filters) }
.build().toString()
val body = StoreNet.get(url, headers = mapOf("X-Algolia-Application-Id" to APP, "X-Algolia-API-Key" to KEY)) ?: return null
return parse(JSONObject(body), site.third).take(hits)
}
internal fun parse(answer: JSONObject, currency: String, now: Instant = Instant.now()): List<CatalogItem> {
val hits = answer.optJSONArray("hits") ?: return emptyList()
val items = (0 until hits.length()).mapNotNull { hits.optJSONObject(it) }.mapNotNull { item(it, currency, now) }
return items.groupBy { baseTitle(it.title) }.values.map { editions -> editions.firstOrNull { " - " !in it.title } ?: editions.first() }
}
private fun money(amount: Double, currency: String): String = NumberFormat.getCurrencyInstance(Locale.US).apply {
this.currency = Currency.getInstance(currency)
}.format(amount)
internal fun item(h: JSONObject, currency: String, now: Instant): CatalogItem? {
fun s(k: String) = h.optString(k).takeIf { it.isNotBlank() && it != "null" }
val id = s("objectID") ?: s("id") ?: return null
val title = s("title") ?: s("name") ?: return null
if (s("Free_offer") == "demo") return null
val price = h.optJSONObject("price")?.optDouble(currency)?.takeUnless { it.isNaN() }
val base = h.optJSONObject("default_price")?.optDouble(currency)?.takeUnless { it.isNaN() }
val discount = s("promotion_percentage")?.toDoubleOrNull()?.toInt() ?: 0
val released = s("release_date")?.let { runCatching { Instant.parse(it) }.getOrNull() }
if (released != null && released.isAfter(now) && s("preorder") != "true") return null
val free = s("Free_offer") == "freeplay" || price == 0.0
val image = s("image_link")?.substringBefore("?")
return CatalogItem(
store = Store.UBISOFT, id = "ubisoft-store:$id", title = title.trim(),
imageUrl = image, tallImageUrl = image, tags = listOfNotNull(s("Genre"), s("Edition")).joinToString(", "),
isFree = free, hasPrice = price != null, finalPrice = price?.let { money(it, currency) }.orEmpty(),
originalPrice = if (discount > 0 && base != null) money(base, currency) else "", discountPercent = if (discount > 0 && base != null) discount else 0,
storeUrl = s("link") ?: s("linkWeb").orEmpty(), publisher = "Ubisoft", releaseDate = released?.toString()?.take(10).orEmpty(),
description = cleanStoreText(s("longDescription")),
mature = s("adult").equals("TRUE", true) || matureByAge(listOfNotNull(s("Rating")?.let(RATING_AGE::get))),
extra = buildMap {
s("productLauncherIDString")?.let { put("productId", it) }
if (s("partOfUbisoftPlus").equals("true", true)) put("ubisoftPlus", "true")
},
)
}
/** All four rows, or a failure when any of them failed, so a partial refresh never replaces a good one. */
private fun fetch(site: Triple<String, String, String>): StoreFront.Catalog {
fun row(order: String, filters: String = "", facets: List<String> = emptyList()) =
query(site, order, SHELF, filters = filters, facets = facets) ?: throw java.io.IOException("Ubisoft store $order $filters$facets failed")
val trending = row("best_sellers")
val newest = row("release_date")
val deals = row("best_sellers", filters = "promotion_percentage > 0")
val free = row("best_sellers", facets = listOf("Free_offer:freeplay"))
return StoreFront.Catalog(StoreShelves(whatsNew = newest, deals = deals, free = free, trending = trending, art = trending.mapNotNull { it.imageUrl }))
}
}
@@ -505,7 +505,12 @@ internal fun GameActions(
InstallButton(stringResource(R.string.stores_install), leadOn) { StoresState.requestInstall(ctx, item) }
SecondaryButton(stringResource(R.string.stores_view_on, item.store.shortLabel)) { openStoreUrl(ctx, item) }
}
item != null && item.isFree -> PrimaryButton(stringResource(R.string.stores_get_free), main = true, modifier = leadOn) { openStoreUrl(ctx, item) }
item != null && item.isFree -> {
val adding = StoresState.claiming[item.key] == true
PrimaryButton(stringResource(if (adding) R.string.stores_claiming else R.string.stores_get_free), enabled = !adding, main = true, modifier = leadOn) {
if (StoresState.canClaim(item)) StoresState.claim(ctx, item) { openStoreUrl(ctx, item) } else openStoreUrl(ctx, item)
}
}
item != null -> PrimaryButton(buyLabel(item), main = true, modifier = leadOn) { openStoreUrl(ctx, item) }
}
if (cloud && (download != null || failed != null)) {
+1
View File
@@ -2335,6 +2335,7 @@
<string name="stores_dl_interrupted">منقطع</string>
<string name="stores_install_size">تثبيت · %1$s</string>
<string name="stores_get_free">احصل عليها مجانًا</string>
<string name="stores_claiming">جارٍ الإضافة إلى المكتبة…</string>
<string name="stores_view_on">عرض على %1$s</string>
<string name="stores_buy_price">شراء · %1$s</string>
<string name="stores_installed_chip">مثبَّتة</string>
@@ -2304,6 +2304,7 @@
<string name="stores_dl_interrupted">Interrumpida</string>
<string name="stores_install_size">Instalar · %1$s</string>
<string name="stores_get_free">Obtener gratis</string>
<string name="stores_claiming">Agregando a la biblioteca…</string>
<string name="stores_view_on">Ver en %1$s</string>
<string name="stores_buy_price">Comprar · %1$s</string>
<string name="stores_installed_chip">Instalado</string>
+1
View File
@@ -2235,6 +2235,7 @@
<string name="stores_dl_interrupted">Прекъснато</string>
<string name="stores_install_size">Инсталация · %1$s</string>
<string name="stores_get_free">Вземи безплатно</string>
<string name="stores_claiming">Добавяне в библиотеката…</string>
<string name="stores_view_on">Виж в %1$s</string>
<string name="stores_buy_price">Купи · %1$s</string>
<string name="stores_installed_chip">Инсталирана</string>
+1
View File
@@ -2285,6 +2285,7 @@
<string name="stores_dl_interrupted">Přerušeno</string>
<string name="stores_install_size">Instalace · %1$s</string>
<string name="stores_get_free">Získat zdarma</string>
<string name="stores_claiming">Přidávání do knihovny…</string>
<string name="stores_view_on">Zobrazit na %1$s</string>
<string name="stores_buy_price">Koupit · %1$s</string>
<string name="stores_installed_chip">Nainstalováno</string>
+1
View File
@@ -2235,6 +2235,7 @@
<string name="stores_dl_interrupted">Afbrudt</string>
<string name="stores_install_size">Installation · %1$s</string>
<string name="stores_get_free">Få gratis</string>
<string name="stores_claiming">Føjer til biblioteket…</string>
<string name="stores_view_on">Se på %1$s</string>
<string name="stores_buy_price">Køb · %1$s</string>
<string name="stores_installed_chip">Installeret</string>
+1
View File
@@ -2227,6 +2227,7 @@
<string name="stores_dl_interrupted">Unterbrochen</string>
<string name="stores_install_size">Installieren · %1$s</string>
<string name="stores_get_free">Kostenlos erhalten</string>
<string name="stores_claiming">Wird zur Bibliothek hinzugefügt…</string>
<string name="stores_view_on">Auf %1$s ansehen</string>
<string name="stores_buy_price">Kaufen · %1$s</string>
<string name="stores_installed_chip">Installiert</string>
+1
View File
@@ -2235,6 +2235,7 @@
<string name="stores_dl_interrupted">Διακόπηκε</string>
<string name="stores_install_size">Εγκατάσταση · %1$s</string>
<string name="stores_get_free">Απόκτηση δωρεάν</string>
<string name="stores_claiming">Προσθήκη στη βιβλιοθήκη…</string>
<string name="stores_view_on">Προβολή στο %1$s</string>
<string name="stores_buy_price">Αγορά · %1$s</string>
<string name="stores_installed_chip">Εγκατεστημένο</string>
+1
View File
@@ -2260,6 +2260,7 @@
<string name="stores_dl_interrupted">Interrumpida</string>
<string name="stores_install_size">Instalar · %1$s</string>
<string name="stores_get_free">Obtener gratis</string>
<string name="stores_claiming">Añadiendo a la biblioteca…</string>
<string name="stores_view_on">Ver en %1$s</string>
<string name="stores_buy_price">Comprar · %1$s</string>
<string name="stores_installed_chip">Instalado</string>
+1
View File
@@ -2235,6 +2235,7 @@
<string name="stores_dl_interrupted">Keskeytynyt</string>
<string name="stores_install_size">Asenna · %1$s</string>
<string name="stores_get_free">Hanki ilmaiseksi</string>
<string name="stores_claiming">Lisätään kirjastoon…</string>
<string name="stores_view_on">Näytä %1$s:ssä</string>
<string name="stores_buy_price">Osta · %1$s</string>
<string name="stores_installed_chip">Asennettu</string>
+1
View File
@@ -2260,6 +2260,7 @@
<string name="stores_dl_interrupted">Interrompu</string>
<string name="stores_install_size">Installer · %1$s</string>
<string name="stores_get_free">Obtenir gratuitement</string>
<string name="stores_claiming">Ajout à la bibliothèque…</string>
<string name="stores_view_on">Voir sur %1$s</string>
<string name="stores_buy_price">Acheter · %1$s</string>
<string name="stores_installed_chip">Installé</string>
+1
View File
@@ -2235,6 +2235,7 @@
<string name="stores_dl_interrupted">बाधित</string>
<string name="stores_install_size">इंस्टॉल · %1$s</string>
<string name="stores_get_free">मुफ़्त में पाएं</string>
<string name="stores_claiming">लाइब्रेरी में जोड़ा जा रहा है…</string>
<string name="stores_view_on">%1$s पर देखें</string>
<string name="stores_buy_price">खरीदें · %1$s</string>
<string name="stores_installed_chip">इंस्टॉल है</string>
+1
View File
@@ -2235,6 +2235,7 @@
<string name="stores_dl_interrupted">Megszakítva</string>
<string name="stores_install_size">Telepítés · %1$s</string>
<string name="stores_get_free">Ingyenes beszerzés</string>
<string name="stores_claiming">Hozzáadás a könyvtárhoz…</string>
<string name="stores_view_on">Megtekintés itt: %1$s</string>
<string name="stores_buy_price">Megvétel · %1$s</string>
<string name="stores_installed_chip">Telepítve</string>
+1
View File
@@ -2210,6 +2210,7 @@
<string name="stores_dl_interrupted">Terhenti</string>
<string name="stores_install_size">Instal · %1$s</string>
<string name="stores_get_free">Dapatkan gratis</string>
<string name="stores_claiming">Menambahkan ke pustaka…</string>
<string name="stores_view_on">Lihat di %1$s</string>
<string name="stores_buy_price">Beli · %1$s</string>
<string name="stores_installed_chip">Terinstal</string>
+1
View File
@@ -2252,6 +2252,7 @@
<string name="stores_dl_interrupted">Interrotto</string>
<string name="stores_install_size">Installa · %1$s</string>
<string name="stores_get_free">Ottieni gratis</string>
<string name="stores_claiming">Aggiunta alla libreria…</string>
<string name="stores_view_on">Visualizza su %1$s</string>
<string name="stores_buy_price">Acquista · %1$s</string>
<string name="stores_installed_chip">Installato</string>
+1
View File
@@ -2210,6 +2210,7 @@
<string name="stores_dl_interrupted">中断</string>
<string name="stores_install_size">インストール · %1$s</string>
<string name="stores_get_free">無料で入手</string>
<string name="stores_claiming">ライブラリに追加中…</string>
<string name="stores_view_on">%1$sで見る</string>
<string name="stores_buy_price">購入 · %1$s</string>
<string name="stores_installed_chip">インストール済み</string>
+1
View File
@@ -2210,6 +2210,7 @@
<string name="stores_dl_interrupted">중단됨</string>
<string name="stores_install_size">설치 · %1$s</string>
<string name="stores_get_free">무료로 받기</string>
<string name="stores_claiming">라이브러리에 추가하는 중…</string>
<string name="stores_view_on">%1$s에서 보기</string>
<string name="stores_buy_price">구매 · %1$s</string>
<string name="stores_installed_chip">설치됨</string>
+1
View File
@@ -2210,6 +2210,7 @@
<string name="stores_dl_interrupted">Terganggu</string>
<string name="stores_install_size">Pasang · %1$s</string>
<string name="stores_get_free">Dapatkan percuma</string>
<string name="stores_claiming">Menambah ke pustaka…</string>
<string name="stores_view_on">Lihat pada %1$s</string>
<string name="stores_buy_price">Beli · %1$s</string>
<string name="stores_installed_chip">Dipasang</string>
+1
View File
@@ -2235,6 +2235,7 @@
<string name="stores_dl_interrupted">Avbrutt</string>
<string name="stores_install_size">Installer · %1$s</string>
<string name="stores_get_free">Hent gratis</string>
<string name="stores_claiming">Legger til i biblioteket …</string>
<string name="stores_view_on">Vis på %1$s</string>
<string name="stores_buy_price">Kjøp · %1$s</string>
<string name="stores_installed_chip">Installert</string>
+1
View File
@@ -2235,6 +2235,7 @@
<string name="stores_dl_interrupted">Onderbroken</string>
<string name="stores_install_size">Installeren · %1$s</string>
<string name="stores_get_free">Gratis verkrijgen</string>
<string name="stores_claiming">Toevoegen aan bibliotheek…</string>
<string name="stores_view_on">Bekijken op %1$s</string>
<string name="stores_buy_price">Kopen · %1$s</string>
<string name="stores_installed_chip">Geïnstalleerd</string>
+1
View File
@@ -2285,6 +2285,7 @@
<string name="stores_dl_interrupted">Przerwano</string>
<string name="stores_install_size">Instalacja · %1$s</string>
<string name="stores_get_free">Odbierz bezpłatnie</string>
<string name="stores_claiming">Dodawanie do biblioteki…</string>
<string name="stores_view_on">Zobacz w %1$s</string>
<string name="stores_buy_price">Kup · %1$s</string>
<string name="stores_installed_chip">Zainstalowano</string>
@@ -2252,6 +2252,7 @@
<string name="stores_dl_interrupted">Interrompida</string>
<string name="stores_install_size">Instalar · %1$s</string>
<string name="stores_get_free">Obter grátis</string>
<string name="stores_claiming">A adicionar à biblioteca…</string>
<string name="stores_view_on">Ver em %1$s</string>
<string name="stores_buy_price">Comprar · %1$s</string>
<string name="stores_installed_chip">Instalado</string>
+1
View File
@@ -2252,6 +2252,7 @@
<string name="stores_dl_interrupted">Interrompido</string>
<string name="stores_install_size">Instalar · %1$s</string>
<string name="stores_get_free">Obter grátis</string>
<string name="stores_claiming">Adicionando à biblioteca…</string>
<string name="stores_view_on">Ver em %1$s</string>
<string name="stores_buy_price">Comprar · %1$s</string>
<string name="stores_installed_chip">Instalado</string>
+1
View File
@@ -2260,6 +2260,7 @@
<string name="stores_dl_interrupted">Întreruptă</string>
<string name="stores_install_size">Instalare · %1$s</string>
<string name="stores_get_free">Obține gratuit</string>
<string name="stores_claiming">Se adaugă în bibliotecă…</string>
<string name="stores_view_on">Vezi pe %1$s</string>
<string name="stores_buy_price">Cumpără · %1$s</string>
<string name="stores_installed_chip">Instalat</string>
+1
View File
@@ -2368,6 +2368,7 @@
<string name="stores_dl_interrupted">Прервано</string>
<string name="stores_install_size">Установить · %1$s</string>
<string name="stores_get_free">Получить бесплатно</string>
<string name="stores_claiming">Добавление в библиотеку…</string>
<string name="stores_view_on">Открыть в %1$s</string>
<string name="stores_buy_price">Купить · %1$s</string>
<string name="stores_installed_chip">Установлено</string>
+1
View File
@@ -2235,6 +2235,7 @@
<string name="stores_dl_interrupted">Avbruten</string>
<string name="stores_install_size">Installation · %1$s</string>
<string name="stores_get_free">Hämta gratis</string>
<string name="stores_claiming">Lägger till i biblioteket …</string>
<string name="stores_view_on">Visa på %1$s</string>
<string name="stores_buy_price">Köp · %1$s</string>
<string name="stores_installed_chip">Installerat</string>
+1
View File
@@ -2210,6 +2210,7 @@
<string name="stores_dl_interrupted">ถูกขัดจังหวะ</string>
<string name="stores_install_size">ติดตั้ง · %1$s</string>
<string name="stores_get_free">รับไปเลยฟรี</string>
<string name="stores_claiming">กำลังเพิ่มลงในคลัง…</string>
<string name="stores_view_on">ดูบน %1$s</string>
<string name="stores_buy_price">ซื้อ · %1$s</string>
<string name="stores_installed_chip">ติดตั้งแล้ว</string>
+1
View File
@@ -2227,6 +2227,7 @@
<string name="stores_dl_interrupted">Kesintiye uğradı</string>
<string name="stores_install_size">Yükle · %1$s</string>
<string name="stores_get_free">Ücretsiz al</string>
<string name="stores_claiming">Kitaplığa ekleniyor…</string>
<string name="stores_view_on">%1$s\'te görüntüle</string>
<string name="stores_buy_price">Satın al · %1$s</string>
<string name="stores_installed_chip">Yüklü</string>
+1
View File
@@ -2285,6 +2285,7 @@
<string name="stores_dl_interrupted">Перервано</string>
<string name="stores_install_size">Встановити · %1$s</string>
<string name="stores_get_free">Отримати безкоштовно</string>
<string name="stores_claiming">Додавання до бібліотеки…</string>
<string name="stores_view_on">Переглянути на %1$s</string>
<string name="stores_buy_price">Купити · %1$s</string>
<string name="stores_installed_chip">Встановлено</string>
+1
View File
@@ -2210,6 +2210,7 @@
<string name="stores_dl_interrupted">Bị gián đoạn</string>
<string name="stores_install_size">Cài · %1$s</string>
<string name="stores_get_free">Lấy miễn phí</string>
<string name="stores_claiming">Đang thêm vào thư viện…</string>
<string name="stores_view_on">Xem trên %1$s</string>
<string name="stores_buy_price">Mua · %1$s</string>
<string name="stores_installed_chip">Đã cài</string>
@@ -2291,6 +2291,7 @@
<string name="stores_dl_interrupted">已中断</string>
<string name="stores_install_size">安装 · %1$s</string>
<string name="stores_get_free">免费领取</string>
<string name="stores_claiming">正在添加到游戏库…</string>
<string name="stores_view_on">在 %1$s 上查看</string>
<string name="stores_buy_price">购买 · %1$s</string>
<string name="stores_installed_chip">已安装</string>
@@ -2210,6 +2210,7 @@
<string name="stores_dl_interrupted">已中斷</string>
<string name="stores_install_size">安裝 · %1$s</string>
<string name="stores_get_free">免費領取</string>
<string name="stores_claiming">正在加入遊戲庫…</string>
<string name="stores_view_on">在 %1$s 查看</string>
<string name="stores_buy_price">購買 · %1$s</string>
<string name="stores_installed_chip">已安裝</string>
@@ -2210,6 +2210,7 @@
<string name="stores_dl_interrupted">已中斷</string>
<string name="stores_install_size">安裝 · %1$s</string>
<string name="stores_get_free">免費取得</string>
<string name="stores_claiming">正在加入收藏庫…</string>
<string name="stores_view_on">在 %1$s 檢視</string>
<string name="stores_buy_price">購買 · %1$s</string>
<string name="stores_installed_chip">已安裝</string>
+1
View File
@@ -2585,6 +2585,7 @@
<string name="stores_dl_interrupted">Interrupted</string>
<string name="stores_install_size">Install · %1$s</string>
<string name="stores_get_free">Get for free</string>
<string name="stores_claiming">Adding to library…</string>
<string name="stores_view_on">View on %1$s</string>
<string name="stores_buy_price">Buy · %1$s</string>
<string name="stores_installed_chip">Installed</string>
@@ -0,0 +1,15 @@
query GetGameProductOfferIds($slug: String!, $locale: Locale!, $overrideCountryCode: String!) {
game(slug: $slug, locale: $locale) {
id
isFreeToPlay
slug
lowestPriceGameProduct(availabilities: [ACQUIRABLE], overrideCountryCode: $overrideCountryCode) {
id
isFree
name
originOfferId
__typename
}
__typename
}
}
@@ -0,0 +1,23 @@
mutation grantEntitlement($input: GrantEntitlementInput!) {
grantEntitlement(input: $input) {
success
entitledOfferIds
entitlementType
errors {
... on GrantEntitlementOfferDoesNotExistError {
__typename
}
... on GrantEntitlementNotQualifiedError {
__typename
}
... on GrantEntitlementNotFreeOrNoAppropriateSubscriptionError {
__typename
}
... on GrantEntitlementUpstreamConflictError {
__typename
}
__typename
}
__typename
}
}
@@ -1,5 +1,6 @@
package com.droiddeck.launcher.stores.ea
import com.droiddeck.launcher.stores.CatalogItem
import com.droiddeck.launcher.stores.Store
import com.droiddeck.launcher.stores.StoreGameSidecar
import com.droiddeck.launcher.stores.StoreLaunch
@@ -239,6 +240,47 @@ class EaTest {
assertTrue(EaLibrary.withoutGameDownload(emptyList()).isEmpty())
}
@Test fun theStorePageNamesEachGameWithItsPrice() {
val game = { slug: String, title: String, date: String, price: String, sale: String, off: Int, free: Boolean ->
"""{"slug":"$slug","title":"$title
","isFreeToPlay":$free,"franchiseSlug":"f","tier":"Tier1","releaseDate":"$date",
"packArt":{"ar16X9":"https://img/$slug-wide","ar2X3":"https://img/$slug-tall"},"genres":[{"name":"RPG"}],
"price":{"displayTotal":"$price","displayTotalWithDiscount":"$sale","discountPercentage":$off,"currency":"USD"}}"""
}
val html = """<html><script id="__NEXT_DATA__" type="application/json">{"props":{"pageProps":{"totalItems":40,"games":[""" +
game("ultima-7", "Ultima VII", "2026-11-05T18:00:00.000Z", "$4.99", "$2.49", 50, false) + "," +
game("redsec", "REDSEC", "2025-10-28T15:00:00.000Z", "$0.00", "$0.00", 0, true) + "," +
game("future", "Future", "2027-03-01T00:00:00.000Z", "$69.99", "$69.99", 0, false) + "]}}}</script></html>"
val (items, total) = EaStore.parsePage(html, "USD")
assertEquals("", EaStore.parsePage(html, "EUR").first.first().finalPrice)
assertEquals(40, total)
val ultima = items.first()
assertEquals("Ultima VII", ultima.title)
assertEquals("$2.49", ultima.finalPrice)
assertEquals("$4.99", ultima.originalPrice)
assertEquals(50, ultima.discountPercent)
assertEquals("https://www.ea.com/games/f/ultima-7", ultima.storeUrl)
assertTrue(items[1].isFree)
val shelves = EaStore.shelves(items, java.time.Instant.parse("2026-11-12T00:00:00Z"))
assertEquals(listOf("Ultima VII", "REDSEC"), shelves.whatsNew.map { it.title })
assertEquals(listOf("Ultima VII"), shelves.deals.map { it.title })
assertEquals(listOf("REDSEC"), shelves.free.map { it.title })
val owned = CatalogItem(Store.EA, "Origin.OFR.1", "Ultima™ VII", null, owned = true, extra = mapOf("slug" to "other"))
val marked = com.droiddeck.launcher.stores.StoreFront.markOwned(shelves, listOf(owned), EaStore::sameGame)
assertEquals("Origin.OFR.1", marked.deals.single().id)
assertEquals("https://img/ultima-7-wide", marked.deals.single().imageUrl)
val bf = CatalogItem(Store.EA, "Origin.OFR.50.0005511", "Battlefield™", null, owned = true, extra = mapOf("slug" to "glacier", "productName" to "Battlefield™ 6"))
assertTrue(EaStore.sameGame(CatalogItem(Store.EA, "ea-store:battlefield-6", "Battlefield™ 6", null, extra = mapOf("slug" to "battlefield-6")), bf))
assertFalse(EaStore.sameGame(CatalogItem(Store.EA, "ea-store:bf-2042", "Battlefield™ 2042", null), bf))
}
@Test fun titlesMatchAcrossMarksAndAccentsButNeverWhenEmpty() {
assertEquals("pokemon", com.droiddeck.launcher.stores.StoreFront.titleKey("Pokémon™"))
assertEquals("ファイナル 7", com.droiddeck.launcher.stores.StoreFront.titleKey("ファイナル® 7"))
val shelf = CatalogItem(Store.EA, "ea-store:a", "™", null)
assertFalse(EaStore.sameGame(shelf, CatalogItem(Store.EA, "Origin.OFR.2", "®", null, owned = true)))
}
@Test fun ownedPageParses() {
val page = JSONObject(
"""{"me":{"ownedGameProducts":{"next":"100","items":[{"originOfferId":"Origin.OFR.50.0001","product":{"id":"x","name":"N","downloadable":true,"gameSlug":"titanfall-2",
@@ -45,6 +45,36 @@ class UbisoftTest {
assertEquals(listOf("space-gone", "space-sc"), UbisoftLibrary.assemble(profile, null).map { it.id })
}
@Test fun theStoreSearchNamesEachGameWithItsPrice() {
fun hit(id: String, title: String, price: Double, base: Double, off: String, date: String, free: String = "") = JSONObject()
.put("objectID", id).put("title", title).put("price", JSONObject().put("USD", price)).put("default_price", JSONObject().put("USD", base))
.put("promotion_percentage", off).put("release_date", date).put("Free_offer", free).put("link", "https://store.ubisoft.com/us/$id.html")
.put("image_link", "https://img/$id.jpg?sw=300").put("productLauncherIDString", id.takeLast(3)).put("Rating", "esrb-m")
val answer = JSONObject().put("hits", JSONArray()
.put(hit("p-109", "Far Cry® 6", 15.0, 60.0, "75", "2021-10-07T00:00:00.000Z"))
.put(hit("p-110", "Far Cry® 6 - Gold Edition", 30.0, 100.0, "70", "2021-10-07T00:00:00.000Z"))
.put(hit("p-111", "Some Demo", 0.0, 0.0, "", "2021-10-07T00:00:00.000Z", free = "demo"))
.put(hit("p-112", "Next Year", 70.0, 70.0, "", "2030-01-01T00:00:00.000Z")))
val items = UbisoftStore.parse(answer, "USD", java.time.Instant.parse("2026-11-12T00:00:00Z"))
assertEquals(listOf("Far Cry® 6"), items.map { it.title })
val game = items.single()
assertEquals("$15.00", game.finalPrice)
assertEquals("$60.00", game.originalPrice)
assertEquals(75, game.discountPercent)
assertEquals("https://img/p-109.jpg", game.imageUrl)
assertTrue(game.mature)
val owned = CatalogItem(Store.UBISOFT, "space-fc6", "Far Cry 6", null, owned = true, extra = mapOf("productId" to "5266"))
val ezio = CatalogItem(Store.UBISOFT, "ubisoft-store:x", "Assassin's Creed® The Ezio Collection", null)
assertTrue(UbisoftStore.sameGame(game, owned))
assertTrue(!UbisoftStore.sameGame(ezio, CatalogItem(Store.UBISOFT, "space-ac", "Assassin's Creed™", null, owned = true)))
}
@Test fun theBaseEditionStandsForItsGameWhicheverComesFirst() {
fun hit(id: String, title: String) = JSONObject().put("objectID", id).put("title", title).put("price", JSONObject().put("USD", 10.0))
val answer = JSONObject().put("hits", JSONArray().put(hit("gold", "Far Cry® 6 - Gold Edition")).put(hit("base", "Far Cry® 6")))
assertEquals(listOf("ubisoft-store:base"), UbisoftStore.parse(answer, "USD").map { it.id })
}
@Test fun theEntitlementNamesTheGamesApplication() {
val body = JSONObject().put("items", JSONArray()
.put(JSONObject().put("productId", 82).put("applicationId", "app-82"))