Skip to content

Commit

Permalink
Implement buying debuff potions
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipthelen committed Oct 7, 2019
1 parent f4e0f56 commit fca20b8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
8 changes: 7 additions & 1 deletion HabitRPG/Repositories/Implementations/UserRepository.swift
Expand Up @@ -70,7 +70,7 @@ class UserRepository: BaseRepository<UserLocalRepository> {
}

func useSkill(skill: SkillProtocol, targetId: String? = nil) -> Signal<SkillResponseProtocol?, Never> {
return UseSkillCall(skill: skill, target: targetId).objectSignal.on(value: {[weak self] skillResponse in
return UseSkillCall(key: skill.key ?? "", target: skill.target ?? "", targetID: targetId).objectSignal.on(value: {[weak self] skillResponse in
if let response = skillResponse {
self?.localRepository.save(userID: self?.currentUserId, skillResponse: response)
}
Expand All @@ -92,6 +92,12 @@ class UserRepository: BaseRepository<UserLocalRepository> {
})
}

func useDebuffItem(key: String) -> Signal<UserProtocol?, Never> {
return UseSkillCall(key: key).objectSignal.flatMap(.latest) {[weak self] _ in
return self?.retrieveUser() ?? Signal.empty
}
}

func runCron(tasks: [TaskProtocol]) {
var disposable: Disposable?
getUser().take(first: 1).on(value: {[weak self]user in
Expand Down
Expand Up @@ -423,6 +423,15 @@ class HRPGBuyItemModalViewController: UIViewController, Themeable {
HRPGBuyItemModalViewController.displayViewController(name: "InsufficientGoldViewController", parent: topViewController)
}
})
} else if (purchaseType == "debuffPotion") {
userRepository.useDebuffItem(key: key).observeResult { (result) in
switch result {
case .success(_):
successBlock()
case .failure(_):
HRPGBuyItemModalViewController.displayViewController(name: "InsufficientGoldViewController", parent: topViewController)
}
}
} else {
inventoryRepository.buyObject(key: key, price: value, text: text).observeResult({ (result) in
switch result {
Expand Down
Expand Up @@ -11,9 +11,12 @@ import Habitica_Models
import ReactiveSwift

public class UseSkillCall: ResponseObjectCall<SkillResponseProtocol, APISkillResponse> {
public init(skill: SkillProtocol, target: String? = nil, stubHolder: StubHolderProtocol? = StubHolder(responseCode: 200, stubFileName: "user.json")) {
var url = "user/class/cast/\(skill.key ?? "")?targetType=\(skill.target ?? "")"
if let targetId = target {
public init(key: String, target: String? = nil, targetID: String? = nil, stubHolder: StubHolderProtocol? = StubHolder(responseCode: 200, stubFileName: "user.json")) {
var url = "user/class/cast/\(key)"
if let target = target {
url += "?targetType=\(target)"
}
if let targetId = targetID {
url += "&targetId=\(targetId)"
}
super.init(httpMethod: .POST, endpoint: url, stubHolder: stubHolder)
Expand Down

0 comments on commit fca20b8

Please sign in to comment.