<commit_before>//
// Created by <NAME> on 25/02/17.
// Copyright (c) 2017 <NAME>. All rights reserved.
//

import Foundation
import Unbox

class ArenaWatcher: Watcher {
	
	static let _instance = ArenaWatcher()
	
	static func start(handler: PowerEventHandler) {
		_instance.handler = handler
		_instance.startWatching()
	}
	
	static func stop() {
		_instance.stopWatching()
	}
	
	static func isRunning() -> Bool {
		return _instance.isRunning
	}
	
    private let heroes: [CardClass] = [
            .warrior,
            .shaman,
            .rogue,
            .paladin,
            .hunter,
            .druid,
            .warlock,
            .mage,
            .priest
    ]
	
	static var hero: CardClass = .neutral
    private var cardTiers: [ArenaCard] = []
    private var currentCards: [String] = []
	private var handler: PowerEventHandler?
	
    override func run() {
        if cardTiers.count == 0 {
            loadCardTiers()
        }

        while isRunning {
			
			guard let choices = MirrorHelper.getArenaDraftChoices() else {
				Thread.sleep(forTimeInterval: refreshInterval)
				continue

			}
            if choices.count != 3 {
                Thread.sleep(forTimeInterval: refreshInterval)
                continue
            }

            var cards: [Card] = []
            for mirrorCard in choices {
                if let cardInfo = cardTiers.first(where: { $0.id == mirrorCard.cardId }),
                    let card = Cards.by(cardId: mirrorCard.cardId),
                    let index = heroes.index(of: ArenaWatcher.hero) {

                    let value = cardInfo.values[index]
                    let costs = value.matches("([0-9]+)")
                    card.cost = Int(costs.first?.value ?? "0") ?? 0
                    card.isBadAsMultiple = value.contains("^")
                    card.count = 1
                    cards.append(card)
                }
            }

            if cards.count == 3 {
                let ids = cards.map { $0.id }
                if ids.sorted() != currentCards.sorted() {
                    logger.debug("cards: \(cards)")
                    currentCards = ids
					
					handler?.setArenaOptions(cards: cards.sorted { $0.cost > $1.cost })
                }
            }
            
            Thread.sleep(forTimeInterval: refreshInterval)
        }
		
		handler?.setArenaOptions(cards: [])
        queue = nil
    }

    private func loadCardTiers() {<commit_msg>Check for "*" instead of "^"<commit_after>//
// Created by <NAME> on 25/02/17.
// Copyright (c) 2017 <NAME>. All rights reserved.
//

import Foundation
import Unbox

class ArenaWatcher: Watcher {
	
	static let _instance = ArenaWatcher()
	
	static func start(handler: PowerEventHandler) {
		_instance.handler = handler
		_instance.startWatching()
	}
	
	static func stop() {
		_instance.stopWatching()
	}
	
	static func isRunning() -> Bool {
		return _instance.isRunning
	}
	
    private let heroes: [CardClass] = [
            .warrior,
            .shaman,
            .rogue,
            .paladin,
            .hunter,
            .druid,
            .warlock,
            .mage,
            .priest
    ]
	
	static var hero: CardClass = .neutral
    private var cardTiers: [ArenaCard] = []
    private var currentCards: [String] = []
	private var handler: PowerEventHandler?
	
    override func run() {
        if cardTiers.count == 0 {
            loadCardTiers()
        }

        while isRunning {
			
			guard let choices = MirrorHelper.getArenaDraftChoices() else {
				Thread.sleep(forTimeInterval: refreshInterval)
				continue

			}
            if choices.count != 3 {
                Thread.sleep(forTimeInterval: refreshInterval)
                continue
            }

            var cards: [Card] = []
            for mirrorCard in choices {
                if let cardInfo = cardTiers.first(where: { $0.id == mirrorCard.cardId }),
                    let card = Cards.by(cardId: mirrorCard.cardId),
                    let index = heroes.index(of: ArenaWatcher.hero) {

                    let value = cardInfo.values[index]
                    let costs = value.matches("([0-9]+)")
                    card.cost = Int(costs.first?.value ?? "0") ?? 0
                    card.isBadAsMultiple = value.contains("*")
                    card.count = 1
                    cards.append(card)
                }
            }

            if cards.count == 3 {
                let ids = cards.map { $0.id }
                if ids.sorted() != currentCards.sorted() {
                    logger.debug("cards: \(cards)")
                    currentCards = ids
					
					handler?.setArenaOptions(cards: cards.sorted { $0.cost > $1.cost })
                }
            }
            
            Thread.sleep(forTimeInterval: refreshInterval)
        }
		
		handler?.setArenaOptions(cards: [])
        queue = nil
    }

    private func loadCardTiers() {
{'fraction_non_alphanumeric': 0.1071064956220729, 'fraction_numerical': 0.008959478721238036, 'mean_word_length': 1.5267489711934157, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2636086', 'n_tokens_mistral': 1509, 'n_tokens_neox': 1339, 'n_words': 316}