Skip to content
This repository has been archived by the owner on Oct 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #147 from atfinke/2020.08
Browse files Browse the repository at this point in the history
2020.08
  • Loading branch information
atfinke committed Jul 20, 2020
2 parents a625148 + a310d44 commit f30f580
Show file tree
Hide file tree
Showing 37 changed files with 632 additions and 200 deletions.
18 changes: 16 additions & 2 deletions WKRKit/WKRKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
140CE30424C2967900ECA175 /* WKRLanguageHackery.swift in Sources */ = {isa = PBXBuildFile; fileRef = 140CE30324C2967900ECA175 /* WKRLanguageHackery.swift */; };
140CE30524C2967900ECA175 /* WKRLanguageHackery.swift in Sources */ = {isa = PBXBuildFile; fileRef = 140CE30324C2967900ECA175 /* WKRLanguageHackery.swift */; };
140FCFC31F36547E00FFA84E /* WKRResultsInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 140FCFC21F36547E00FFA84E /* WKRResultsInfo.swift */; };
140FCFC51F36549900FFA84E /* WKRPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 140FCFC41F36549900FFA84E /* WKRPlayer.swift */; };
140FCFCB1F3654D500FFA84E /* WKRPlayerState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 140FCFCA1F3654D500FFA84E /* WKRPlayerState.swift */; };
Expand All @@ -17,6 +19,9 @@
141427FF21FBB84600C48788 /* WKRGameKitNetwork.swift in Sources */ = {isa = PBXBuildFile; fileRef = 141427FE21FBB84600C48788 /* WKRGameKitNetwork.swift */; };
142CB29A202EAC6B00BC6A33 /* WKRSoloNetwork.swift in Sources */ = {isa = PBXBuildFile; fileRef = 142CB299202EAC6B00BC6A33 /* WKRSoloNetwork.swift */; };
142CB29E202EC42000BC6A33 /* WKRPeerNetworkConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 142CB29D202EC42000BC6A33 /* WKRPeerNetworkConfig.swift */; };
144F33C024C34A24006E4437 /* WKRDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 144F33BF24C34A24006E4437 /* WKRDefaults.swift */; };
144F33C124C34A24006E4437 /* WKRDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 144F33BF24C34A24006E4437 /* WKRDefaults.swift */; };
144F33C224C34A24006E4437 /* WKRDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 144F33BF24C34A24006E4437 /* WKRDefaults.swift */; };
1452B3721F36524400E81FD8 /* WKRPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1452B3711F36524400E81FD8 /* WKRPage.swift */; };
1452B3741F36529300E81FD8 /* WKRKitConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1452B3731F36529300E81FD8 /* WKRKitConstants.swift */; };
1452B3761F3652E900E81FD8 /* WKRRaceDurationConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1452B3751F3652E900E81FD8 /* WKRRaceDurationConstants.swift */; };
Expand Down Expand Up @@ -106,6 +111,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
140CE30324C2967900ECA175 /* WKRLanguageHackery.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WKRLanguageHackery.swift; sourceTree = "<group>"; };
140FCFC21F36547E00FFA84E /* WKRResultsInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WKRResultsInfo.swift; sourceTree = "<group>"; };
140FCFC41F36549900FFA84E /* WKRPlayer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WKRPlayer.swift; sourceTree = "<group>"; };
140FCFCA1F3654D500FFA84E /* WKRPlayerState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WKRPlayerState.swift; sourceTree = "<group>"; };
Expand All @@ -115,6 +121,7 @@
141427FE21FBB84600C48788 /* WKRGameKitNetwork.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WKRGameKitNetwork.swift; sourceTree = "<group>"; };
142CB299202EAC6B00BC6A33 /* WKRSoloNetwork.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WKRSoloNetwork.swift; sourceTree = "<group>"; };
142CB29D202EC42000BC6A33 /* WKRPeerNetworkConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WKRPeerNetworkConfig.swift; sourceTree = "<group>"; };
144F33BF24C34A24006E4437 /* WKRDefaults.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WKRDefaults.swift; sourceTree = "<group>"; };
1452B3711F36524400E81FD8 /* WKRPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WKRPage.swift; sourceTree = "<group>"; };
1452B3731F36529300E81FD8 /* WKRKitConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WKRKitConstants.swift; sourceTree = "<group>"; };
1452B3751F3652E900E81FD8 /* WKRRaceDurationConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WKRRaceDurationConstants.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -320,6 +327,8 @@
14EB9F651F36833A00FF1A9E /* WKROperation.swift */,
14B4DB86222674E2007D4B54 /* WKRLogEvent.swift */,
1499606D2226FCBF00259B68 /* WKRDurationFormatter.swift */,
140CE30324C2967900ECA175 /* WKRLanguageHackery.swift */,
144F33BF24C34A24006E4437 /* WKRDefaults.swift */,
);
path = Other;
sourceTree = "<group>";
Expand Down Expand Up @@ -567,12 +576,14 @@
146E054724AE3E9E001E1917 /* WKRPlayer.swift in Sources */,
146E054B24AE3EB2001E1917 /* WKRLinkedPagesFetcher.swift in Sources */,
146E054124AE3E95001E1917 /* WKRSeenFinalArticlesStore.swift in Sources */,
140CE30524C2967900ECA175 /* WKRLanguageHackery.swift in Sources */,
146E053F24AE3E95001E1917 /* WKRHistoryEntry.swift in Sources */,
146E054224AE3E95001E1917 /* WKRPage.swift in Sources */,
146E053024AE3E80001E1917 /* WKRRaceDurationConstants.swift in Sources */,
146E053B24AE3E8F001E1917 /* WKRDurationFormatter.swift in Sources */,
146E053C24AE3E8F001E1917 /* WKRKit+Array.swift in Sources */,
146E053924AE3E85001E1917 /* WKRRaceSettings.swift in Sources */,
144F33C124C34A24006E4437 /* WKRDefaults.swift in Sources */,
146E054424AE3E9E001E1917 /* WKRPlayerRaceStats.swift in Sources */,
146E053324AE3E85001E1917 /* WKRReadyStates.swift in Sources */,
146E053A24AE3E8F001E1917 /* WKRLogEvent.swift in Sources */,
Expand Down Expand Up @@ -637,8 +648,10 @@
142CB29E202EC42000BC6A33 /* WKRPeerNetworkConfig.swift in Sources */,
149960702226FCBF00259B68 /* WKRDurationFormatter.swift in Sources */,
147EF6FE2202470700583D73 /* WKRSeenFinalArticlesStore.swift in Sources */,
144F33C024C34A24006E4437 /* WKRDefaults.swift in Sources */,
140FCFCD1F3654EB00FFA84E /* WKRHistoryEntry.swift in Sources */,
141427FF21FBB84600C48788 /* WKRGameKitNetwork.swift in Sources */,
140CE30424C2967900ECA175 /* WKRLanguageHackery.swift in Sources */,
14EB9F621F36832C00FF1A9E /* WKRMultiWindowNetwork.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -647,6 +660,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
144F33C224C34A24006E4437 /* WKRDefaults.swift in Sources */,
148164B11F5C94F200AC7178 /* WKRKitPageFetcherTests.swift in Sources */,
148164AF1F5C94E800AC7178 /* WKRKitRaceTests.swift in Sources */,
149FF8171F362B3D000A5D96 /* WKRKitTests.swift in Sources */,
Expand Down Expand Up @@ -851,7 +865,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 13.1;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 2020.07;
MARKETING_VERSION = 2020.08;
OTHER_SWIFT_FLAGS = "";
PRODUCT_BUNDLE_IDENTIFIER = com.andrewfinke.WKRKit;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
Expand Down Expand Up @@ -882,7 +896,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 13.1;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 2020.07;
MARKETING_VERSION = 2020.08;
OTHER_SWIFT_FLAGS = "";
PRODUCT_BUNDLE_IDENTIFIER = com.andrewfinke.WKRKit;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
Expand Down
141 changes: 94 additions & 47 deletions WKRKit/WKRKit/Game/WKRPreRaceConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@
//

import WKRUIKit
import os.log

/// Used to transmit voting data and starting page
public struct WKRPreRaceConfig: Codable, Equatable {

// MARK: - Properties

/// The voting info
internal var votingState: WKRVotingState
/// The starting page
internal let startingPage: WKRPage

// MARK: Initialization

/// Creates a WKRPreRaceConfig object
///
/// - Parameters:
Expand All @@ -29,9 +30,9 @@ public struct WKRPreRaceConfig: Codable, Equatable {
self.votingState = votingState
self.startingPage = startingPage
}

// MARK: - Creation

/// Creates a race config object based on starting page and voting data
///
/// - Returns: The new race config
Expand All @@ -42,58 +43,80 @@ public struct WKRPreRaceConfig: Codable, Equatable {
}
return (WKRRaceConfig(starting: startingPage, ending: page), logEvent)
}

/// Creates a WKRPreRaceConfig object
///
/// - Parameter completionHandler: The handler holding the new config object
static func new(settings: WKRGameSettings, completionHandler: @escaping ((_ config: WKRPreRaceConfig?, _ logEvents: [WKRLogEvent]) -> Void)) {
var logEvents = [WKRLogEvent]()
let operationQueue = OperationQueue()

var potentialFinalPages = [WKRPage]()
var startingPage: WKRPage?

var fastlaneFinalPage: WKRPage?

let completedOperation = BlockOperation {
// Used for quick debug to set first page to Apple and final page to first link on page.
if WKRKitConstants.current.isQuickRaceMode {
let startingURL = URL(string: "https://en.m.wikipedia.org/wiki/Apple_Inc.")!
startingPage = WKRPage(title: "Apple Inc.", url: startingURL)

let endingURL = URL(string: "https://en.m.wikipedia.org/wiki/Apple_Park")!
let fakeEnd = WKRPage(title: "Apple Park", url: endingURL)

potentialFinalPages.removeLast()
potentialFinalPages.insert(fakeEnd, at: 0)
}

// Uses a set to remove any duplicates. This could happen if two final articles end up redirecting to the same page.
let finalPages = Array(Set(potentialFinalPages.prefix(WKRKitConstants.current.votingArticlesCount)))

let events = logEvents.compactMap { $0 }
if !finalPages.isEmpty, let page = startingPage {
let config = WKRPreRaceConfig(startingPage: page, votingState: WKRVotingState(pages: finalPages))
var config = WKRPreRaceConfig(startingPage: page, votingState: WKRVotingState(pages: finalPages))
if WKRDefaults.isFastlaneSnapshotInstance, let page = fastlaneFinalPage {
config.votingState.fastlaneVotes(finalPage: page)
}
completionHandler(config, events)
} else {
completionHandler(nil, events)
}
}

// Gets the starting page
let startingPageOperation = WKROperation()
startingPageOperation.addExecutionBlock { [unowned startingPageOperation] in
switch settings.startPage {
case .random:
WKRPageFetcher.fetchRandom { page in
startingPage = page
startingPageOperation.state = .isFinished
if WKRDefaults.isFastlaneSnapshotInstance {
if WKRLanguageHackery.shared.isEnglish {
guard let url = URL(string: "https://en.m.wikipedia.org/wiki/Walt_Disney_World_Monorail_System") else { fatalError() }
startingPage = WKRPage(title: "Walt Disney World Monorail System", url: url)
startingPageOperation.state = .isFinished
} else {
WKRLanguageHackery.shared.adjustedPath(for: "/Walt_Disney") { path in
guard let path = path else { fatalError() }
WKRPageFetcher.fetch(path: path, useCache: true) { page, _ in
guard let page = page else { fatalError() }
startingPage = page
startingPageOperation.state = .isFinished
}
}
}
} else {
WKRPageFetcher.fetchRandom { page in
startingPage = page
startingPageOperation.state = .isFinished
os_log("fetched random start: %{public}s", log: .articlesValidation, type: .info, page?.title ?? "-")
}
}
case .custom(let page):
startingPage = page
startingPageOperation.state = .isFinished
}
}
completedOperation.addDependency(startingPageOperation)

let endingPageOperations: [BlockOperation]
switch settings.endPage {
case .curatedVoting:
Expand All @@ -104,39 +127,63 @@ public struct WKRPreRaceConfig: Codable, Equatable {
}
var randomPaths = [String]()
let numberOfPagesToFetch = WKRKitConstants.current.votingArticlesCount + 1

// pages are suffled so we can just take index 0-n from the shuffled array
for index in 0..<numberOfPagesToFetch where index < finalArticles.count {
randomPaths.append(finalArticles[index])
}


var fastlanePath: String?
if WKRDefaults.isFastlaneSnapshotInstance {
if WKRLanguageHackery.shared.isEnglish {
randomPaths[0] = "/Star_Wars:_Galaxy%27s_Edge"
} else {
randomPaths[0] = "/Magic_Kingdom"
}
fastlanePath = randomPaths[0]
}

// All the operations for get WKRPage objects to vote on
endingPageOperations = randomPaths.map { path -> WKROperation in
let operation = WKROperation()
operation.addExecutionBlock { [unowned operation] in
// don't use cache to make sure to get most recent page
WKRPageFetcher.fetch(path: path, useCache: false) { page, isRedirect in
// 1. Make sure not redirect
// 2. Make sure page not nil
// 3. Make sure page not already in voting list for this race
// 4. Make sure page is not a link to a section "/USA#History"
// 5. Sometimes removed pages redirect to the Wikipedia homepage.
// 6. Make sure path in unseen
// 7/8. Make sure link not equal to starting page
if !isRedirect,
let page = page,
!potentialFinalPages.contains(page),
!page.url.absoluteString.contains("#"),
page.title != "Wikipedia, the free encyclopedia",
finalArticles.contains(page.path),
let startingPage = startingPage,
startingPage.url.absoluteString.lowercased() != page.url.absoluteString.lowercased() {
potentialFinalPages.append(page)
} else {
logEvents.append(WKRLogEvent(type: .votingArticleValidationFailure,
attributes: ["PagePath": path]))

WKRLanguageHackery.shared.adjustedPath(for: path) { adjustedPath in
guard let adjustedPath = adjustedPath else {
operation.state = .isFinished
os_log("fetched error: no adjusted path", log: .articlesValidation, type: .error)
return
}

// don't use cache to make sure to get most recent page
WKRPageFetcher.fetch(path: adjustedPath, useCache: false) { page, isRedirect in
// 1. Make sure not redirect
// 2. Make sure page not nil
// 3. Make sure page not already in voting list for this race
// 4. Make sure page is not a link to a section "/USA#History"
// 5. Sometimes removed pages redirect to the Wikipedia homepage.
// 6. Make sure path in unseen
// 7/8. Make sure link not equal to starting page
if !isRedirect,
let page = page,
!potentialFinalPages.contains(page),
!page.url.absoluteString.contains("#"),
page.title != "Wikipedia, the free encyclopedia",
let startingPage = startingPage,
startingPage.url.absoluteString.lowercased() != page.url.absoluteString.lowercased() {
potentialFinalPages.append(page)
os_log("fetched final: %{public}s", log: .articlesValidation, type: .info, page.title ?? "-")
} else {
logEvents.append(WKRLogEvent(type: .votingArticleValidationFailure,
attributes: ["PagePath": adjustedPath]))
os_log("fetched error final: %{public}s", log: .articlesValidation, type: .error, page?.title ?? "-")
}

if WKRDefaults.isFastlaneSnapshotInstance && fastlanePath == path {
fastlaneFinalPage = page
}
operation.state = .isFinished
}
operation.state = .isFinished
}
}
operation.addDependency(startingPageOperation)
Expand All @@ -145,7 +192,7 @@ public struct WKRPreRaceConfig: Codable, Equatable {
}
case .randomVoting:
let numberOfPagesToFetch = Int(Double(WKRKitConstants.current.votingArticlesCount) * 1.5)

// All the operations for get WKRPage objects to vote on
endingPageOperations = (0..<numberOfPagesToFetch).map { _ -> WKROperation in
let operation = WKROperation()
Expand All @@ -157,7 +204,7 @@ public struct WKRPreRaceConfig: Codable, Equatable {
let title = page.title,
!page.url.absoluteString.contains("#"),
title != "Wikipedia, the free encyclopedia",
title.count < WKRKitConstants.current.pageTitleMaxRandomLength {
title.count < WKRKitConstants.current.pageTitleMaxRandomLength {
potentialFinalPages.append(page)
} else {
// Don't log the analytics, keep validation to just curated articles
Expand All @@ -179,9 +226,9 @@ public struct WKRPreRaceConfig: Codable, Equatable {
completedOperation.addDependency(operation)
endingPageOperations = [operation]
}

operationQueue.addOperations([startingPageOperation, completedOperation], waitUntilFinished: false)
operationQueue.addOperations(endingPageOperations, waitUntilFinished: false)
}

}
Loading

0 comments on commit f30f580

Please sign in to comment.