<commit_before>//
//  ViewController.swift
//  2for1
//
//  Created by <NAME> on 12/24/16.
//  Copyright © 2016 Flatiron. All rights reserved.
//

import UIKit

class GameViewController: UIViewController {

    let game = Game()
    let screenHeight = UIScreen.main.bounds.height
    let screenWidth = UIScreen.main.bounds.width
    let blurEffectView = UIVisualEffectView()
    override var prefersStatusBarHidden: Bool { return true }
    
    @IBOutlet weak var menuView: MenuView!
    @IBOutlet weak var gameView: GameView!
    @IBOutlet weak var instructionsView: InstructionsView!
    @IBOutlet weak var scoreView: ScoreView!
    @IBOutlet weak var addDieOrDrinkView: AddDieOrDrinkView!
    @IBOutlet weak var nextPlayerView: NextPlayerView!
    @IBOutlet weak var nextPlayerViewLeadingConstraint: NSLayoutConstraint!
    @IBOutlet weak var addDieOrDrinkLeadingConstraint: NSLayoutConstraint!
    @IBOutlet weak var gameCoverView: UIView!
    @IBOutlet weak var rolledHighEnoughView: CheckMarkView!
    @IBOutlet weak var rolledHighEnoughLeadingConstraint: NSLayoutConstraint!
    @IBOutlet weak var drinkView: CheckMarkView!
    @IBOutlet weak var drinkViewLeadingConstraint: NSLayoutConstraint!
    @IBOutlet weak var rollLabel: UILabel!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        makeViewsCircles()
        applyTapGestures()
        addGradients()
        setInitialGameSettings()
        setUpMenu()
        setupBlur()
    }

    
    override func viewDidAppear(_ animated: Bool) {
        animate(view: nextPlayerView, constraint: nextPlayerViewLeadingConstraint, coverGameView: true)
    }
    
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showInfoFromGame" {
            let dest = segue.destination as! GameInstructionsViewController
            dest.delegate = self
        }
    }
    
    
}



//MARK: Game Actions
extension GameViewController {
  
    func diceSwiped(_ sender: UISwipeGestureRecognizer) {
        switch game.action {
        case .roll: rolled(sender)
        case .rollAddedDie: rollAddedDie(sender)
        default: break
        }
    }
    
    
    func rolled(_ sender: UISwipeGestureRecognizer) {
        let results = game.roll()
        gameView.diceGrid.animateDice(in: sender.direction) { (success) in
            self.gameView.diceGrid.updateDiceImages(dice: self.game.dice)
            if results.tiedRoll {
                self.tiedRoll()
            } else if results.wonRoll && self.rolledHighEnoughLeadingConstraint.constant == 0 {
                self.animate(view: self.rolledHighEnoughView, constraint: self.rolledHighEnoughLeadingConstraint, coverGameView: false)
            } else if !results.wonRoll && self.addDieOrDrinkLeadingConstraint.constant == 0 {
                self.animate(view: self.addDieOrDrinkView, constraint: self.addDieOrDrinkLeadingConstraint, coverGameView: true)
            }
            self.updateInstructions()
        }
    }
    
    
    func addDie() {
        game.addDie()
        gameView.diceGrid.displayNumberOfDice(number: game.dice.count)
        gameView.diceGrid.updateDiceImages(dice: game.dice)
        self.animate(view: addDieOrDrinkView, constraint: addDieOrDrinkLeadingConstraint, coverGameView: true)
        updateInstructions()
        scoreView.updateLabels(score: game.score, drinks: game.drinks)
    }
    
    
    func rollAddedDie(_ sender: UISwipeGestureRecognizer) {
        let result = game.rollAddedDie()
        gameView.diceGrid.animateDice(in: sender.direction) { (success) in
            self.gameView.diceGrid.updateDiceImages(dice: self.game.dice)
            if result {
                self.animate(view: self.rolledHighEnoughView, constraint: self.rolledHighEnoughLeadingConstraint, coverGameView: false)
                self.game.instructions = "\(self.game.player!.tag!) rolled high enough!"
            } else {
                self.tiedRoll()
            }
            self.updateInstructions()
        }
    }
    
    
    func rolledHighEnoughTapped() {
        game.passDice()
        nextPlayerView.picImageView.image = game.player?.playerImage
        animate(view: rolledHighEnoughView, constraint: rolledHighEnoughLeadingConstraint, coverGameView: false)
        animate(view: nextPlayerView, constraint: nextPlayerViewLeadingConstraint, coverGameView: true)
        gameView.diceGrid.displayNumberOfDice(number: game.dice.count)
        gameView.diceGrid.updateDiceImages(dice: game.dice)
        scoreView.updateLabels(score: game.score, drinks: game.drinks)
        updateInstructions()
    }
    
    
    func drink() {
        game.instructions = "\(game.player!.tag!) must drink \(game.drinks) "
        game.drinks == 1 ? game.instructions.append("drink!") : game.instructions.append("drinks!")
        animate(view: addDieOrDrinkView, constraint: addDieOrDrinkLeadingConstraint, coverGameView: true)
        animate(view: drinkView, constraint: drinkViewLeadingConstraint, coverGameView: false)
        updateInstructions()
    }
    
    
    func tiedRoll() {
        if game.playerRoll == game.score {
            game.instructions = "\(game.player!.tag!) tied the score and must drink \(game.drinks) "
            game.drinks == 1 ? game.instructions.append("drink!") : game.instructions.append("drinks!")
        } else {
            game.instructions = "\(game.player!.tag!) rolled too low and must drink \(game.drinks) drinks!"
        }
        if drinkViewLeadingConstraint.constant == 0 {
            animate(view: drinkView, constraint: drinkViewLeadingConstraint, coverGameView: false)
        }
    }
    
    
    func resetGame() {
        if drinkViewLeadingConstraint.constant < 0 {
            animate(view: drinkView, constraint: drinkViewLeadingConstraint, coverGameView: false)
        }
        if addDieOrDrinkLeadingConstraint.constant < 0 {
            animate(view: addDieOrDrinkView, constraint: addDieOrDrinkLeadingConstraint, coverGameView: true)
        }
        if rolledHighEnoughLeadingConstraint.constant < 0 {
            animate(view: rolledHighEnoughView, constraint: rolledHighEnoughLeadingConstraint, coverGameView: false)
        }
        
        game.resetGame()
        gameView.diceGrid.displayNumberOfDice(number: 1)
        gameView.diceGrid.updateDiceImages(dice: game.dice)
        
        if nextPlayerViewLeadingConstraint.constant == 0 {
            animate(view: nextPlayerView, constraint: nextPlayerViewLeadingConstraint, coverGameView: true)
        }
        scoreView.updateLabels(score: game.score, drinks: game.drinks)
        updateInstructions()
    }
    
    
}



//MARK: Helper Methods
extension GameViewController {
    
    func openCloseMenuTapped(_ sender: UIGestureRecognizer) {
        menuView.isCollapsed ? menuView.expandMenu() : menuView.collapseMenu(withDelay: 0)
    }
    
    
    func dismissVC() {
        self.dismiss(animated: true, completion: nil)<commit_msg>fixed game score label to update for dice rolls<commit_after>//
//  ViewController.swift
//  2for1
//
//  Created by <NAME> on 12/24/16.
//  Copyright © 2016 Flatiron. All rights reserved.
//

import UIKit

class GameViewController: UIViewController {

    let game = Game()
    let screenHeight = UIScreen.main.bounds.height
    let screenWidth = UIScreen.main.bounds.width
    let blurEffectView = UIVisualEffectView()
    override var prefersStatusBarHidden: Bool { return true }
    
    @IBOutlet weak var menuView: MenuView!
    @IBOutlet weak var gameView: GameView!
    @IBOutlet weak var instructionsView: InstructionsView!
    @IBOutlet weak var scoreView: ScoreView!
    @IBOutlet weak var addDieOrDrinkView: AddDieOrDrinkView!
    @IBOutlet weak var nextPlayerView: NextPlayerView!
    @IBOutlet weak var nextPlayerViewLeadingConstraint: NSLayoutConstraint!
    @IBOutlet weak var addDieOrDrinkLeadingConstraint: NSLayoutConstraint!
    @IBOutlet weak var gameCoverView: UIView!
    @IBOutlet weak var rolledHighEnoughView: CheckMarkView!
    @IBOutlet weak var rolledHighEnoughLeadingConstraint: NSLayoutConstraint!
    @IBOutlet weak var drinkView: CheckMarkView!
    @IBOutlet weak var drinkViewLeadingConstraint: NSLayoutConstraint!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        makeViewsCircles()
        applyTapGestures()
        addGradients()
        setInitialGameSettings()
        setUpMenu()
        setupBlur()
    }

    
    override func viewDidAppear(_ animated: Bool) {
        animate(view: nextPlayerView, constraint: nextPlayerViewLeadingConstraint, coverGameView: true)
    }
    
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showInfoFromGame" {
            let dest = segue.destination as! GameInstructionsViewController
            dest.delegate = self
        }
    }
    
    
}



//MARK: Game Actions
extension GameViewController {
  
    func diceSwiped(_ sender: UISwipeGestureRecognizer) {
        switch game.action {
        case .roll: rolled(sender)
        case .rollAddedDie: rollAddedDie(sender)
        default: break
        }
    }
    
    
    func rolled(_ sender: UISwipeGestureRecognizer) {
        let results = game.roll()
        gameView.diceGrid.animateDice(in: sender.direction) { (success) in
            self.gameView.diceGrid.updateDiceImages(dice: self.game.dice)
            if results.tiedRoll {
                self.tiedRoll()
            } else if results.wonRoll && self.rolledHighEnoughLeadingConstraint.constant == 0 {
                self.animate(view: self.rolledHighEnoughView, constraint: self.rolledHighEnoughLeadingConstraint, coverGameView: false)
            } else if !results.wonRoll && self.addDieOrDrinkLeadingConstraint.constant == 0 {
                self.animate(view: self.addDieOrDrinkView, constraint: self.addDieOrDrinkLeadingConstraint, coverGameView: true)
            }
            self.updateInstructions()
            self.gameView.scoreLabel.text = String(self.game.playerRoll)
        }

    }
    
    
    func addDie() {
        game.addDie()
        gameView.diceGrid.displayNumberOfDice(number: game.dice.count)
        gameView.diceGrid.updateDiceImages(dice: game.dice)
        self.animate(view: addDieOrDrinkView, constraint: addDieOrDrinkLeadingConstraint, coverGameView: true)
        updateInstructions()
        scoreView.updateLabels(score: game.score, drinks: game.drinks)
    }
    
    
    func rollAddedDie(_ sender: UISwipeGestureRecognizer) {
        let result = game.rollAddedDie()
        gameView.diceGrid.animateDice(in: sender.direction) { (success) in
            self.gameView.diceGrid.updateDiceImages(dice: self.game.dice)
            if result {
                self.animate(view: self.rolledHighEnoughView, constraint: self.rolledHighEnoughLeadingConstraint, coverGameView: false)
                self.game.instructions = "\(self.game.player!.tag!) rolled high enough!"
            } else {
                self.tiedRoll()
            }
            self.updateInstructions()
        }
    }
    
    
    func rolledHighEnoughTapped() {
        game.passDice()
        nextPlayerView.picImageView.image = game.player?.playerImage
        animate(view: rolledHighEnoughView, constraint: rolledHighEnoughLeadingConstraint, coverGameView: false)
        animate(view: nextPlayerView, constraint: nextPlayerViewLeadingConstraint, coverGameView: true)
        gameView.diceGrid.displayNumberOfDice(number: game.dice.count)
        gameView.diceGrid.updateDiceImages(dice: game.dice)
        scoreView.updateLabels(score: game.score, drinks: game.drinks)
        updateInstructions()
    }
    
    
    func drink() {
        game.instructions = "\(game.player!.tag!) must drink \(game.drinks) "
        game.drinks == 1 ? game.instructions.append("drink!") : game.instructions.append("drinks!")
        animate(view: addDieOrDrinkView, constraint: addDieOrDrinkLeadingConstraint, coverGameView: true)
        animate(view: drinkView, constraint: drinkViewLeadingConstraint, coverGameView: false)
        updateInstructions()
    }
    
    
    func tiedRoll() {
        if game.playerRoll == game.score {
            game.instructions = "\(game.player!.tag!) tied the score and must drink \(game.drinks) "
            game.drinks == 1 ? game.instructions.append("drink!") : game.instructions.append("drinks!")
        } else {
            game.instructions = "\(game.player!.tag!) rolled too low and must drink \(game.drinks) drinks!"
        }
        if drinkViewLeadingConstraint.constant == 0 {
            animate(view: drinkView, constraint: drinkViewLeadingConstraint, coverGameView: false)
        }
    }
    
    
    func resetGame() {
        if drinkViewLeadingConstraint.constant < 0 {
            animate(view: drinkView, constraint: drinkViewLeadingConstraint, coverGameView: false)
        }
        if addDieOrDrinkLeadingConstraint.constant < 0 {
            animate(view: addDieOrDrinkView, constraint: addDieOrDrinkLeadingConstraint, coverGameView: true)
        }
        if rolledHighEnoughLeadingConstraint.constant < 0 {
            animate(view: rolledHighEnoughView, constraint: rolledHighEnoughLeadingConstraint, coverGameView: false)
        }
        
        game.resetGame()
        gameView.diceGrid.displayNumberOfDice(number: 1)
        gameView.diceGrid.updateDiceImages(dice: game.dice)
        
        if nextPlayerViewLeadingConstraint.constant == 0 {
            animate(view: nextPlayerView, constraint: nextPlayerViewLeadingConstraint, coverGameView: true)
        }
        scoreView.updateLabels(score: game.score, drinks: game.drinks)
        gameView.scoreLabel.text = "0"
        updateInstructions()
    }
    
    
}



//MARK: Helper Methods
extension GameViewController {
    
    func openCloseMenuTapped(_ sender: UIGestureRecognizer) {
        menuView.isCollapsed ? menuView.expandMenu() : menuView.collapseMenu(withDelay: 0)
    }
    
    
    func dismissVC() {
        self.dismiss(animated: true, completion: nil)
{'fraction_non_alphanumeric': 0.08777651730005673, 'fraction_numerical': 0.003332387975042541, 'mean_word_length': 2.7998383620689653, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, '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': '2243103', 'n_tokens_mistral': 4153, 'n_tokens_neox': 4002, 'n_words': 818}