Play Windows games on Apple Silicon Macs. Free, open-source app that installs the official Battle.net, Steam, Epic and GOG launchers and lists your games in one library.
HTML apple-siliconbattle-netcrossover-alternatived3dmetaldiablo-ii-resurrecteddxmtea-appepic-gamesgame-porting-toolkitgogmac-gamingmacosmacos-gamingrockstar-gamessteamubisoft-connectwhisky-alternativewindows-gameswinewine-wrapper
App: ask players who keep coming back for a GitHub star (#130)
Mikek3eabd35ab4dc
diff --git a/.github/workflows/check.yml b/.github/workflows/check.ymlindex 5c190861ca3..d097f6007f2 100644--- a/.github/workflows/check.yml+++ b/.github/workflows/check.yml@@ -24,5 +24,7 @@ jobs:run: |swiftc app/UpdateCheck.swift tests/test_update_check.swift -o /tmp/soju-update-tests/tmp/soju-update-tests+ swiftc app/StarAsk.swift tests/test_star_ask.swift -o /tmp/soju-star-tests+ /tmp/soju-star-tests- name: Build and verify the native apprun: bash scripts/build-app.shdiff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex d7eed5ea62d..f7eb8a582fc 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -41,6 +41,7 @@ python3 -m unittest discover -s tests -qnode --test tests/test_steam_login.cjsswiftc app/PurchaseLinks.swift tests/test_purchase_links.swift -o /tmp/t1 && /tmp/t1swiftc app/UpdateCheck.swift tests/test_update_check.swift -o /tmp/t2 && /tmp/t2+swiftc app/StarAsk.swift tests/test_star_ask.swift -o /tmp/t3 && /tmp/t3bash scripts/build-app.sh```diff --git a/app/Library.swift b/app/Library.swiftindex 8ba95887d6c..2f80269c053 100644--- a/app/Library.swift+++ b/app/Library.swift@@ -166,6 +166,7 @@ extension SojuModel {try p.run()failed = falsepreferences.opened[game.id] = Date(); savePreferences()+ starAsk.launched(game.id); starAsk.save()activity = game.platform == .didim ? "Playing " + game.title + "…" : "Opening " + game.title + " through " + game.platform.title + "…"// An Xbox game runs inside this process, so it counts as running until it exits.// Other clients may stay alive for hours: there it is only a launch request.@@ -367,6 +368,9 @@ struct LibraryView: View {Label("Add Didim Labs game", systemImage: "plus")}.disabled(model.busy).help("Install a native Mac game from a downloaded .zip")}+ if page == "library", model.starAsk.shouldAsk(), let id = model.starAsk.favorite() {+ StarCard(title: model.games.first(where: { $0.id == id })?.title)+ }HStack(spacing: 12) {HStack(spacing: 8) {Image(systemName: "magnifyingglass").foregroundStyle(.secondary)@@ -656,3 +660,35 @@ struct GameArtwork: View {}}}++/// Shown only to players who keep coming back to a game (StarAsk). One click opens the repository.+struct StarCard: View {+ @EnvironmentObject var model: SojuModel+ let title: String?+ var body: some View {+ HStack(alignment: .center, spacing: 16) {+ Image(systemName: "star.fill").font(.system(size: 30)).foregroundStyle(.yellow)+ VStack(alignment: .leading, spacing: 5) {+ Text((title.map { $0 + " keeps running on Soju." } ?? "Soju keeps working for you.") + " Would you star it on GitHub?")+ .font(.system(size: 14, weight: .semibold)).fixedSize(horizontal: false, vertical: true)+ Text("Soju is free and open source. Stars are how other Mac players find it, and it takes one click.")+ .font(.system(size: 12)).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)+ }+ Spacer(minLength: 8)+ VStack(alignment: .trailing, spacing: 6) {+ Button {+ NSWorkspace.shared.open(StarAsk.repo); model.starAsk.finish(); model.starAsk.save()+ } label: { Label("Star on GitHub", systemImage: "star") }+ .buttonStyle(.borderedProminent).controlSize(.large).keyboardShortcut(.defaultAction)+ HStack(spacing: 10) {+ Button("Join Discord") { NSWorkspace.shared.open(SojuModel.discord) }.buttonStyle(.link)+ Button("Later") { model.starAsk.later(); model.starAsk.save() }.buttonStyle(.link)+ Button("Don't ask again") { model.starAsk.finish(); model.starAsk.save() }.buttonStyle(.link)+ }.font(.system(size: 11))+ }+ }.padding(16).frame(maxWidth: .infinity, alignment: .leading)+ .background(Color.yellow.opacity(0.10), in: RoundedRectangle(cornerRadius: 12))+ .overlay(RoundedRectangle(cornerRadius: 12).stroke(Color.yellow.opacity(0.35)))+ .accessibilityElement(children: .contain)+ }+}diff --git a/app/Soju.swift b/app/Soju.swiftindex ef70e0cf750..b904b644bc4 100644--- a/app/Soju.swift+++ b/app/Soju.swift@@ -113,6 +113,7 @@ struct SojuLogo: View {@Published var stoppingGames: Set<String> = []@Published var libraryIssue: String?@Published var preferences = LibraryPreferences.load()+ @Published var starAsk = StarAsk.load()var gameLaunches: [String: Process] = [:]var scanner: Process?@Published var installed: Set<Launcher> = []diff --git a/app/StarAsk.swift b/app/StarAsk.swiftnew file mode 100644index 00000000000..673e99b33f6--- /dev/null+++ b/app/StarAsk.swift@@ -0,0 +1,56 @@+import Foundation++/// When to ask for a GitHub star: only players who clearly have Soju working for them.+/// The sign is the same game started on three different days (a game that fails is not+/// started again on later days), or games started on five different days in total.+/// Asked at most twice, a week apart; "Star on GitHub" or "Don't ask again" ends it.+struct StarAsk: Codable, Equatable {+ var days: [String: Set<String>] = [:] // game id -> calendar days it was started (yyyy-MM-dd)+ var asked = 0+ var lastAsked: Date?+ var done = false++ static let repo = URL(string: "https://github.com/BCD1210/soju")!+ static let key = "soju.starask.v1"++ static func day(_ date: Date) -> String {+ let f = DateFormatter()+ f.locale = Locale(identifier: "en_US_POSIX")+ f.timeZone = .current+ f.dateFormat = "yyyy-MM-dd"+ return f.string(from: date)+ }++ mutating func launched(_ game: String, at date: Date = Date()) {+ var set = days[game] ?? []+ set.insert(StarAsk.day(date))+ // Keep the file small: the last 30 days per game are plenty for the rule.+ if set.count > 30 { set = Set(set.sorted().suffix(30)) }+ days[game] = set+ }++ /// The game that shows Soju works best (most days), if the rule is met.+ func favorite() -> String? {+ guard let best = days.max(by: { $0.value.count < $1.value.count || ($0.value.count == $1.value.count && $0.key > $1.key) }) else { return nil }+ let total = days.values.reduce(into: Set<String>()) { $0.formUnion($1) }.count+ return best.value.count >= 3 || total >= 5 ? best.key : nil+ }++ func shouldAsk(now: Date = Date()) -> Bool {+ guard !done, asked < 2, favorite() != nil else { return false }+ if let last = lastAsked, now.timeIntervalSince(last) < 7 * 24 * 3600 { return false }+ return true+ }++ mutating func later(now: Date = Date()) { asked += 1; lastAsked = now; if asked >= 2 { done = true } }+ mutating func finish() { done = true }++ static func load() -> StarAsk {+ guard let data = UserDefaults.standard.data(forKey: key),+ let value = try? JSONDecoder().decode(StarAsk.self, from: data) else { return StarAsk() }+ return value+ }+ func save() {+ if let data = try? JSONEncoder().encode(self) { UserDefaults.standard.set(data, forKey: StarAsk.key) }+ }+}diff --git a/scripts/build-app.sh b/scripts/build-app.shindex 91952d9bd80..80d6140c9e3 100755--- a/scripts/build-app.sh+++ b/scripts/build-app.sh@@ -10,7 +10,7 @@ trap 'rm -rf "$STAGE"' EXITAPP="$STAGE/Soju.app"mkdir -p "$APP/Contents/MacOS" "$APP/Contents/Resources/soju"swiftc -swift-version 5 -parse-as-library -O -target arm64-apple-macosx14.0 \- "$ROOT/app/Soju.swift" "$ROOT/app/Library.swift" "$ROOT/app/GameSettings.swift" "$ROOT/app/Accounts.swift" "$ROOT/app/Discover.swift" "$ROOT/app/PurchaseLinks.swift" "$ROOT/app/SteamLogin.swift" "$ROOT/app/Store.swift" "$ROOT/app/Sponsor.swift" "$ROOT/app/Notify.swift" "$ROOT/app/TV.swift" "$ROOT/app/UpdateCheck.swift" "$ROOT/app/Update.swift" -o "$APP/Contents/MacOS/Soju"+ "$ROOT/app/Soju.swift" "$ROOT/app/Library.swift" "$ROOT/app/GameSettings.swift" "$ROOT/app/Accounts.swift" "$ROOT/app/Discover.swift" "$ROOT/app/PurchaseLinks.swift" "$ROOT/app/SteamLogin.swift" "$ROOT/app/Store.swift" "$ROOT/app/Sponsor.swift" "$ROOT/app/Notify.swift" "$ROOT/app/TV.swift" "$ROOT/app/UpdateCheck.swift" "$ROOT/app/Update.swift" "$ROOT/app/StarAsk.swift" -o "$APP/Contents/MacOS/Soju"for dir in scripts tools patches third_party resources; doditto "$ROOT/$dir" "$APP/Contents/Resources/soju/$dir"donediff --git a/tests/test_star_ask.swift b/tests/test_star_ask.swiftnew file mode 100644index 00000000000..852159dac08--- /dev/null+++ b/tests/test_star_ask.swift@@ -0,0 +1,35 @@+import Foundation++@main struct StarAskTests {+ static func at(_ day: Int) -> Date { Date(timeIntervalSince1970: 1_790_000_000 + Double(day) * 86400) }+ static func main() {+ var s = StarAsk()+ assert(!s.shouldAsk(now: at(0)))+ s.launched("steam:1", at: at(0)); s.launched("steam:1", at: at(0).addingTimeInterval(600))+ s.launched("steam:1", at: at(1))+ assert(s.favorite() == nil && !s.shouldAsk(now: at(1))) // two days is not enough+ s.launched("steam:2", at: at(1))+ s.launched("steam:1", at: at(2))+ assert(s.favorite() == "steam:1" && s.shouldAsk(now: at(2))) // same game, third day++ var t = StarAsk() // five days, different games+ for d in 0..<5 { t.launched("game:\(d)", at: at(d)) }+ assert(t.shouldAsk(now: at(5)))++ s.later(now: at(2))+ assert(!s.shouldAsk(now: at(5))) // a week between asks+ assert(s.shouldAsk(now: at(9)))+ s.later(now: at(9))+ assert(s.done && !s.shouldAsk(now: at(30))) // asked twice at most++ var u = StarAsk()+ for d in 0..<3 { u.launched("x", at: at(d)) }+ u.finish()+ assert(!u.shouldAsk(now: at(3)))+ for d in 0..<40 { u.launched("x", at: at(d)) }+ assert(u.days["x"]!.count == 30)+ let data = try! JSONEncoder().encode(u)+ assert(try! JSONDecoder().decode(StarAsk.self, from: data) == u)+ print("star ask: ok")+ }+}