diff --git a/Briscola-Multiplayer.xcodeproj/project.pbxproj b/Briscola-Multiplayer.xcodeproj/project.pbxproj index 33239c5..b9fc7f4 100644 --- a/Briscola-Multiplayer.xcodeproj/project.pbxproj +++ b/Briscola-Multiplayer.xcodeproj/project.pbxproj @@ -11,7 +11,6 @@ 2A02042C23CA142C00D7F469 /* PersistentContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A02042B23CA142C00D7F469 /* PersistentContainer.swift */; }; 2A02043323CA14C300D7F469 /* GameDatabase.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 2A02043123CA14C300D7F469 /* GameDatabase.xcdatamodeld */; }; 2A02043523CA197000D7F469 /* DatabaseHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A02043423CA197000D7F469 /* DatabaseHandler.swift */; }; - 2A0203EB23CA0F1A00D7F469 /* Player.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 2A0203E923CA0F1A00D7F469 /* Player.xcdatamodeld */; }; 2A15020923DB3558000A5886 /* Types.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A15020823DB3558000A5886 /* Types.swift */; }; 2A15020B23DB3F41000A5886 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A15020A23DB3F41000A5886 /* Extensions.swift */; }; 2A15020D23DB3F5B000A5886 /* Protocols.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A15020C23DB3F5B000A5886 /* Protocols.swift */; }; @@ -19,6 +18,7 @@ 2A2CB76123D6E2BA009E4C51 /* Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A2CB76023D6E2BA009E4C51 /* Utilities.swift */; }; 2A2CDAC223D206300050F840 /* FacebookManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A2CDAC123D206300050F840 /* FacebookManager.swift */; }; 2A3D8FA223B9417A00B40C1D /* AI-PlayerEmulator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A3D8FA123B9417A00B40C1D /* AI-PlayerEmulator.swift */; }; + 2AA65A4323DE60F40059547B /* SocialController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AA65A4223DE60F40059547B /* SocialController.swift */; }; 2AA9C1B323B79DA400B3E4A4 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AA9C1B223B79DA400B3E4A4 /* AppDelegate.swift */; }; 2AA9C1B523B79DA400B3E4A4 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AA9C1B423B79DA400B3E4A4 /* SceneDelegate.swift */; }; 2AA9C1BA23B79DA400B3E4A4 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2AA9C1B823B79DA400B3E4A4 /* Main.storyboard */; }; @@ -33,7 +33,6 @@ 2AA9F59B23D3626B00AE69B4 /* SessionManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AA9F59A23D3626B00AE69B4 /* SessionManager.swift */; }; 2AA9F59D23D363AF00AE69B4 /* GameController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AA9F59C23D363AF00AE69B4 /* GameController.swift */; }; 2AFAFA5223C3D83C00F186EE /* ResultsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AFAFA5123C3D83C00F186EE /* ResultsController.swift */; }; - 2AFAFA5423C3D8AD00F186EE /* SocialsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AFAFA5323C3D8AD00F186EE /* SocialsController.swift */; }; 2AFAFA5623C3D8CB00F186EE /* NavigationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AFAFA5523C3D8CB00F186EE /* NavigationController.swift */; }; 2AFAFA5823C3DE2700F186EE /* MenuController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AFAFA5723C3DE2700F186EE /* MenuController.swift */; }; 88E0A91621DC02CD48DD7A87 /* Pods_Briscola_Multiplayer_Briscola_MultiplayerUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 88AA94EBE5798D378EA6C4BB /* Pods_Briscola_Multiplayer_Briscola_MultiplayerUITests.framework */; }; @@ -65,7 +64,6 @@ 2A02042B23CA142C00D7F469 /* PersistentContainer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersistentContainer.swift; sourceTree = ""; }; 2A02043223CA14C300D7F469 /* GameDatabase.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = GameDatabase.xcdatamodel; sourceTree = ""; }; 2A02043423CA197000D7F469 /* DatabaseHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatabaseHandler.swift; sourceTree = ""; }; - 2A0203EA23CA0F1A00D7F469 /* Player.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = Player.xcdatamodel; sourceTree = ""; }; 2A15020823DB3558000A5886 /* Types.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Types.swift; sourceTree = ""; }; 2A15020A23DB3F41000A5886 /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = ""; }; 2A15020C23DB3F5B000A5886 /* Protocols.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Protocols.swift; sourceTree = ""; }; @@ -73,6 +71,7 @@ 2A2CB76023D6E2BA009E4C51 /* Utilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utilities.swift; sourceTree = ""; }; 2A2CDAC123D206300050F840 /* FacebookManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FacebookManager.swift; sourceTree = ""; }; 2A3D8FA123B9417A00B40C1D /* AI-PlayerEmulator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AI-PlayerEmulator.swift"; sourceTree = ""; }; + 2AA65A4223DE60F40059547B /* SocialController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SocialController.swift; sourceTree = ""; }; 2AA9C1AF23B79DA400B3E4A4 /* Briscola-Multiplayer.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Briscola-Multiplayer.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 2AA9C1B223B79DA400B3E4A4 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 2AA9C1B423B79DA400B3E4A4 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; @@ -93,7 +92,6 @@ 2AA9F59A23D3626B00AE69B4 /* SessionManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SessionManager.swift; sourceTree = ""; }; 2AA9F59C23D363AF00AE69B4 /* GameController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameController.swift; sourceTree = ""; }; 2AFAFA5123C3D83C00F186EE /* ResultsController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResultsController.swift; sourceTree = ""; }; - 2AFAFA5323C3D8AD00F186EE /* SocialsController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SocialsController.swift; sourceTree = ""; }; 2AFAFA5523C3D8CB00F186EE /* NavigationController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationController.swift; sourceTree = ""; }; 2AFAFA5723C3DE2700F186EE /* MenuController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuController.swift; sourceTree = ""; }; 2BD14A3BF1FF1F988C1ACC2D /* Pods_Briscola_Multiplayer.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Briscola_Multiplayer.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -210,10 +208,10 @@ isa = PBXGroup; children = ( 2AFAFA5123C3D83C00F186EE /* ResultsController.swift */, - 2AFAFA5323C3D8AD00F186EE /* SocialsController.swift */, 2AFAFA5523C3D8CB00F186EE /* NavigationController.swift */, 2AFAFA5723C3DE2700F186EE /* MenuController.swift */, 2AA9F59C23D363AF00AE69B4 /* GameController.swift */, + 2AA65A4223DE60F40059547B /* SocialController.swift */, ); path = Controllers; sourceTree = ""; @@ -524,12 +522,11 @@ buildActionMask = 2147483647; files = ( 2A02043323CA14C300D7F469 /* GameDatabase.xcdatamodeld in Sources */, - 2AA9F59F23D3646F00AE69B4 /* SessionManagerDelegate.swift in Sources */, 2A02043523CA197000D7F469 /* DatabaseHandler.swift in Sources */, 2A02042C23CA142C00D7F469 /* PersistentContainer.swift in Sources */, 2A15020923DB3558000A5886 /* Types.swift in Sources */, 2A3D8FA223B9417A00B40C1D /* AI-PlayerEmulator.swift in Sources */, - 2A0203EB23CA0F1A00D7F469 /* Player.xcdatamodeld in Sources */, + 2A0203EB23CA0F1A00D7F469 /* (null) in Sources */, 2AA9F59B23D3626B00AE69B4 /* SessionManager.swift in Sources */, 2AA9C1EA23B7A3FD00B3E4A4 /* Card.swift in Sources */, 2AA9C1B323B79DA400B3E4A4 /* AppDelegate.swift in Sources */, @@ -540,10 +537,10 @@ 2AA9C1F523B7CED400B3E4A4 /* Player.swift in Sources */, 2AFAFA5623C3D8CB00F186EE /* NavigationController.swift in Sources */, 2A2CB76123D6E2BA009E4C51 /* Utilities.swift in Sources */, - 2AFAFA5423C3D8AD00F186EE /* SocialsController.swift in Sources */, 2A15020D23DB3F5B000A5886 /* Protocols.swift in Sources */, 2AA9C1F023B7A4F300B3E4A4 /* GameHandler.swift in Sources */, 2A2CDAC223D206300050F840 /* FacebookManager.swift in Sources */, + 2AA65A4323DE60F40059547B /* SocialController.swift in Sources */, 2A15020F23DB4744000A5886 /* SessionObjects.swift in Sources */, 2AFAFA5223C3D83C00F186EE /* ResultsController.swift in Sources */, 2AA9C1B523B79DA400B3E4A4 /* SceneDelegate.swift in Sources */, diff --git a/Briscola-Multiplayer/Controllers/GameController.swift b/Briscola-Multiplayer/Controllers/GameController.swift index 8533670..c93b765 100644 --- a/Briscola-Multiplayer/Controllers/GameController.swift +++ b/Briscola-Multiplayer/Controllers/GameController.swift @@ -36,6 +36,7 @@ class GameController: UIViewController { private var playersCardImgViews: Array> = []; private var tableCardImgViews: Array = []; + private var playersNamesLabels: Array = []; private var playersPointsLabels: Array = []; // @@ -67,14 +68,15 @@ class GameController: UIViewController { private func initSinglePlayerGame() { // SINGLE-PLAYER - let playersTypes: [PlayerType] = [.local, .emulator]; + var playersNames: [String] = [CONSTANTS.EMULATOR_PLAYER_NAME, CONSTANTS.EMULATOR_PLAYER_NAME]; + playersNames[localPlayerIndex!] = gameOptions.localPlayerName; // prepare player's cards variables. prepareAssets(); // load all the cards and load 3 cards foreach player, load // the trump card, create players and load 3 cards for these. - gameHandler.initSinglePlayer(numberOfPlayers: gameOptions.numberOfPlayers, localPlayerIndex: localPlayerIndex!, playersType: playersTypes); + gameHandler.initSinglePlayer(numberOfPlayers: gameOptions.numberOfPlayers, localPlayerIndex: localPlayerIndex!, playersName: playersNames); // gestures initGestures() @@ -98,16 +100,20 @@ class GameController: UIViewController { // SINGLE-PLAYER localPlayerIndex = localPlayerNewIndex; + var playersTypes: [PlayerType] = [.remote, .remote]; playersTypes[localPlayerIndex!] = .local; + var playersNames: [String] = [CONSTANTS.REMOTE_PLAYER_NAME, CONSTANTS.REMOTE_PLAYER_NAME]; + playersNames[localPlayerIndex!] = gameOptions.localPlayerName; + DispatchQueue.main.async { // prepare player's cards variables. self.prepareAssets(); // load all the cards and load 3 cards foreach player, load // the trump card, create players and load 3 cards for these. - self.gameHandler.initMultiPlayer(numberOfPlayers: self.gameOptions.numberOfPlayers, localPlayerIndex: self.localPlayerIndex!, playersType: playersTypes, deckCards: deckCardsConverted); + self.gameHandler.initMultiPlayer(numberOfPlayers: self.gameOptions.numberOfPlayers, localPlayerIndex: self.localPlayerIndex!, playersType: playersTypes, playersName: playersNames, deckCards: deckCardsConverted); // gestures self.initGestures() @@ -125,6 +131,8 @@ class GameController: UIViewController { self.playersCardImgViews.append([]); self.tableCardImgViews.append(UIImageView(image: nil)); self.tableCardImgViews.append(UIImageView(image: nil)); + self.playersNamesLabels.append(UILabel()); + self.playersNamesLabels.append(UILabel()); self.playersPointsLabels.append(UILabel()); self.playersPointsLabels.append(UILabel()); @@ -150,6 +158,8 @@ class GameController: UIViewController { // player's points and name labels + self.playersNamesLabels[currentPlayerIndex] = self.lp_labelName; + self.playersNamesLabels[remotePlayer1Index] = self.rp1_labelName; self.playersPointsLabels[currentPlayerIndex] = self.lp_LabelPoints; self.playersPointsLabels[remotePlayer1Index] = self.rp1_LabelPoints; } @@ -217,6 +227,7 @@ class GameController: UIViewController { // display points DispatchQueue.main.async { for (pIndex, player) in self.gameHandler.players.enumerated() { + self.playersNamesLabels[pIndex].text = String(player.name); self.playersPointsLabels[pIndex].text = String(player.deckPoints); } } @@ -266,7 +277,7 @@ class GameController: UIViewController { if (!allRequiredPlayersAreConnected) { return; } let sharedCardsDeck: [String] = (gameHandler.loadCards()).map { $0.name; }; - let initObject: SS_InitObj = SS_InitObj(senderPlayerIndex: localPlayerIndex!, cardsDeck: sharedCardsDeck); + let initObject: SS_InitObj = SS_InitObj(senderPlayerIndex: localPlayerIndex!, cardsDeck: sharedCardsDeck, senderPlayerName: gameOptions.localPlayerName); DispatchQueue.main.async { let sended = self.sessionManager!.sendData(data: initObject.toData()); @@ -293,12 +304,17 @@ class GameController: UIViewController { // MARK: Navigation public func goToNextView() { - let nextController = ResultsController(); - - // setting properties of new controller - nextController.gameInstance = gameHandler; + self.performSegue(withIdentifier: "goToGameResultView", sender: self); + } + + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { + let resultController = segue.destination as? ResultsController; - self.navigationController!.pushViewController(nextController, animated: true) + if resultController != nil { + if (segue.identifier == "goToGameResultView") { + resultController!.gameInstance = self.gameHandler; + } + } } } @@ -308,9 +324,6 @@ class GameController: UIViewController { extension GameController: SessionControllerDelegate { - // - // MARK: Triggers - func sessionDidChangeState() { // Ensure UI updates occur on the main queue. DispatchQueue.main.async { diff --git a/Briscola-Multiplayer/Controllers/MenuController.swift b/Briscola-Multiplayer/Controllers/MenuController.swift index 47f2971..c36530a 100644 --- a/Briscola-Multiplayer/Controllers/MenuController.swift +++ b/Briscola-Multiplayer/Controllers/MenuController.swift @@ -22,7 +22,7 @@ class MenuController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - gameOptions = GameOptions(mode: .singleplayer, numberOfPlayers: 2, indexOfStarterPlayer: 0); + gameOptions = GameOptions(mode: .singleplayer, numberOfPlayers: 2, indexOfStarterPlayer: 0, localPlayerName: CONSTANTS.LOCAL_PLAYER_NAME); } // @@ -32,7 +32,7 @@ class MenuController: UIViewController { let gameController = segue.destination as? GameController; if (gameController != nil) { if (segue.identifier == "singlePlayerButton") { - self.gameOptions?.mode = .singleplayer + self.gameOptions?.mode = .singleplayer; } if (segue.identifier == "multiPlayerButton") { @@ -42,7 +42,7 @@ class MenuController: UIViewController { gameController!.gameOptions = self.gameOptions!; } - let socialsController = segue.destination as? SocialsController; + let socialsController = segue.destination as? SocialController; if (socialsController != nil) {} } diff --git a/Briscola-Multiplayer/Controllers/NavigationController.swift b/Briscola-Multiplayer/Controllers/NavigationController.swift index a621493..983f931 100644 --- a/Briscola-Multiplayer/Controllers/NavigationController.swift +++ b/Briscola-Multiplayer/Controllers/NavigationController.swift @@ -9,12 +9,4 @@ import Foundation import UIKit -class NavigationController: UINavigationController{ - - override func viewDidLoad() { - super.viewDidLoad() - -// let gameController = UIStoryboard.init(name: "GameViewController", bundle: Bundle.main).instantiateViewController(withIdentifier: "GameViewController") as? GameViewController -// self.navigationController?.pushViewController(gameController!, animated: true) - } -} +class NavigationController: UINavigationController {} diff --git a/Briscola-Multiplayer/Controllers/ResultsController.swift b/Briscola-Multiplayer/Controllers/ResultsController.swift index 74b705a..e45c48a 100644 --- a/Briscola-Multiplayer/Controllers/ResultsController.swift +++ b/Briscola-Multiplayer/Controllers/ResultsController.swift @@ -11,9 +11,95 @@ import UIKit class ResultsController: UIViewController { - public var gameInstance: GameHandler = GameHandler.init(); + // + // MARK: exposed variables + + public var gameInstance: GameHandler!; + + // + // MARK: variables + + private var databaseHandler: DatabaseHandler?; + private var localPlayer: PlayerModel?; + private var remotePlayer: PlayerModel?; + + private var matchResultSaved: Bool = false; + + // + // MARK: @IBOutlet + + @IBOutlet weak var saveResultButton: UIButton! + @IBOutlet weak var localPlayerMatchResultLabel: UILabel! + @IBOutlet weak var localPlayerPointsLabel: UILabel! + @IBOutlet weak var remotePlayerPointsLabel: UILabel! + + // + // MARK: initializers override func viewDidLoad() { super.viewDidLoad(); + + // Database Handler + let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate; + self.databaseHandler = DatabaseHandler(appDelegate.persistentContainer); + + // get players + localPlayer = gameInstance.players.first(where: {$0.type == .local})!; + remotePlayer = gameInstance.players.first(where: {$0.type != .local})!; + + render(); } + + // + // MARK: + + private func render() { + // button + saveResultButton.isEnabled = !matchResultSaved; + + // match result label + var localPlayerMatchResultText: String = "HAI PAREGGIATO!"; + if (localPlayer!.deckPoints > remotePlayer!.deckPoints) { + localPlayerMatchResultText = "HAI VINTO!"; + } else if (localPlayer!.deckPoints < remotePlayer!.deckPoints) { + localPlayerMatchResultText = "HAI PERSO!"; + } + localPlayerMatchResultLabel!.text = localPlayerMatchResultText; + + // players points + localPlayerPointsLabel.text = String(localPlayer!.deckPoints); + remotePlayerPointsLabel.text = String(remotePlayer!.deckPoints); + + // alert + // ... + } + + // + // MARK: @IBAction + + @IBAction func saveResultButton(_ sender: Any) { + guard let dbHandler: DatabaseHandler = databaseHandler else { return; } + + matchResultSaved = true; + + let localPlayerCards: [String] = localPlayer!.currentDeck.map({ $0.name }); + let remotePlayerCards: [String] = remotePlayer!.currentDeck.map({ $0.name }); + + let localPlayerCardsParsed: String = localPlayerCards.joined(separator: ";"); + let remotePlayerCardsParsed: String = remotePlayerCards.joined(separator: ";"); + + let localPlayerHandsWon = localPlayerCards.count / 2; + let remotePlayerHandsWon = localPlayerCards.count / 2; + + let dbLocalPlayer: DB_Player = DB_Player(index: localPlayer!.index, name: localPlayer!.name, type: localPlayer!.type); + let dbRemotePlayer: DB_Player = DB_Player(index: remotePlayer!.index, name: remotePlayer!.name, type: remotePlayer!.type); + + let dbLocalPlayerResult: DB_Result = DB_Result(cards: localPlayerCardsParsed, handsWon: localPlayerHandsWon, points: localPlayer!.deckPoints); + let dbRemotePlayerResult: DB_Result = DB_Result(cards: remotePlayerCardsParsed, handsWon: remotePlayerHandsWon, points: remotePlayer!.deckPoints); + + let match: DB_Match = DB_Match(localPlayer: dbLocalPlayer, localPlayerResult: dbLocalPlayerResult, remotePlayer: dbRemotePlayer, remotePlayerResult: dbRemotePlayerResult); + + dbHandler.saveMatch(match); + } + } diff --git a/Briscola-Multiplayer/Controllers/SocialController.swift b/Briscola-Multiplayer/Controllers/SocialController.swift new file mode 100644 index 0000000..e421895 --- /dev/null +++ b/Briscola-Multiplayer/Controllers/SocialController.swift @@ -0,0 +1,117 @@ +// +// SocialsController.swift +// Briscola-Multiplayer +// +// Created by Matteo Conti on 27/01/2020. +// Copyright © 2020 Matteo Conti. All rights reserved. +// + +import Foundation +import UIKit +import FBSDKLoginKit +import FBSDKShareKit + +// +// MARK: UITableViewCell + +class DatabseResultViewCell: UITableViewCell { + + @IBOutlet weak var localPlayerNameLabel: UILabel! + @IBOutlet weak var remotePlayerNameLabel: UILabel! + + @IBOutlet weak var localPlayerPointsLabel: UILabel! + @IBOutlet weak var remotePlayerPointsLabel: UILabel! + + @IBOutlet weak var FBShareActionButton: UIButton! + +} + + +class SocialController: UITableViewController { + + // MARK: Variables + + var facebookManager: FacebookManager?; + var databaseHandler: DatabaseHandler?; + var dbMatchesRecords: [DB_Match] = []; + + // + // MARK: Methods + + override func viewDidLoad() { + super.viewDidLoad(); + + // Facebook + facebookManager = FacebookManager(permissions: ["public_profile"]); + + // Database Handler + let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate; + self.databaseHandler = DatabaseHandler(appDelegate.persistentContainer); + + // get all matches on database. + dbMatchesRecords = databaseHandler!.getMatches(); + } + + // + // MARK: @IBAction + + @objc private func performFBShare(_ sender: UIButton) { + // use the tag of button as index + let matchToShare: DB_Match = dbMatchesRecords[sender.tag]; + + // show the facebook share dialog + facebookManager?.shareTextOnFaceBook(controller: self, match: matchToShare); + + // {SharingDelegate} class handle the response + } + +} + +// +// MARK: UITableViewController + +extension SocialController { + + override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return dbMatchesRecords.count; + } + + override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: "DatabaseMatchRecord") as! DatabseResultViewCell; + + let currentMatch = dbMatchesRecords[indexPath.row]; + cell.localPlayerNameLabel.text = String(describing: currentMatch.localPlayer.name); + cell.remotePlayerNameLabel.text = String(describing: currentMatch.remotePlayer.name); + cell.localPlayerPointsLabel.text = String(describing: currentMatch.localPlayerResult.points); + cell.remotePlayerPointsLabel.text = String(describing: currentMatch.remotePlayerResult.points); + + // assign the index of the match to button tag + cell.FBShareActionButton.tag = indexPath.row; + // call the performFBShare(_:) method when tapped + cell.FBShareActionButton.addTarget(self, action: #selector(performFBShare(_:)), for: .touchUpInside); + + return cell; + } + +} + +// +// MARK: Facebook + +extension SocialController: SharingDelegate { + + func sharer(_ sharer: Sharing, didCompleteWithResults results: [String : Any]) { + if sharer.shareContent.pageID != nil { + print("Share: Success") + } + } + + func sharer(_ sharer: Sharing, didFailWithError error: Error) { + print("Share: Fail") + } + + func sharerDidCancel(_ sharer: Sharing) { + print("Share: Cancel") + } + +} diff --git a/Briscola-Multiplayer/Controllers/SocialsController.swift b/Briscola-Multiplayer/Controllers/SocialsController.swift deleted file mode 100644 index 3e7404f..0000000 --- a/Briscola-Multiplayer/Controllers/SocialsController.swift +++ /dev/null @@ -1,106 +0,0 @@ -// -// SocialsController.swift -// Briscola-Multiplayer -// -// Created by Matteo Conti on 06/01/2020. -// Copyright © 2020 Matteo Conti. All rights reserved. -// - -import Foundation -import UIKit -import FBSDKLoginKit -import FBSDKShareKit - - -class SocialsController: UIViewController { - - // - // MARK: Variables - - var facebookManager: FacebookManager?; - - // - // MARK: @IBOutlet - - @IBOutlet weak var FBCustomSharingButton: UIButton! - - // - // MARK: Methods - - override func viewDidLoad() { - super.viewDidLoad() - - facebookManager = FacebookManager(permissions: ["public_profile"]); - } - - // - // MARK: @IBAction - - @IBAction private func performFBShare(_ sender: Any) { - facebookManager?.shareTextOnFaceBook(controller: self); - - // response is in the { self } extension. - } - - @IBAction private func performFBLogout() { - facebookManager!.logout(); - print("[INFO] FB logout done!"); - } - - // - // MARK: Callbacks - -} - -// -// MARK: Facebook - -extension SocialsController: SharingDelegate { - - func sharer(_ sharer: Sharing, didCompleteWithResults results: [String : Any]) { - if sharer.shareContent.pageID != nil { - print("Share: Success") - } - } - - func loginDidComplete(_ result: LoginManagerLoginResult?, errors: Error?) { - if errors != nil { - // Process error - print("[INFO] FB Login failed with error \(errors!)"); - - return; - } - - guard let loginResult = result else { - print("[INFO] FB Login: something went wrong."); - return; - } - - // LOGIN EXECUTED - - print("[INFO] FB login result \(result!)"); - - // IS CANCELED ? - if (loginResult.isCancelled) { - print("[INFO] FB User cancelled login."); - return - } - - // LOGIN COMPLETED SUCCESSFULLY - // If you ask for multiple permissions at once, you - // should check if specific permissions missing - if loginResult.grantedPermissions.contains("email") - { - // Do work - } - } - - func sharer(_ sharer: Sharing, didFailWithError error: Error) { - print("Share: Fail") - } - - func sharerDidCancel(_ sharer: Sharing) { - print("Share: Cancel") - } - -} diff --git a/Briscola-Multiplayer/CoreData/GameDatabase.xcdatamodeld/GameDatabase.xcdatamodel/contents b/Briscola-Multiplayer/CoreData/GameDatabase.xcdatamodeld/GameDatabase.xcdatamodel/contents index 43731ff..acaa381 100644 --- a/Briscola-Multiplayer/CoreData/GameDatabase.xcdatamodeld/GameDatabase.xcdatamodel/contents +++ b/Briscola-Multiplayer/CoreData/GameDatabase.xcdatamodeld/GameDatabase.xcdatamodel/contents @@ -1,13 +1,10 @@ - - - - - - - + + + + @@ -20,8 +17,8 @@ - + - + \ No newline at end of file diff --git a/Briscola-Multiplayer/Libraries/DatabaseHandler.swift b/Briscola-Multiplayer/Libraries/DatabaseHandler.swift index 0d8f64f..44be0dc 100644 --- a/Briscola-Multiplayer/Libraries/DatabaseHandler.swift +++ b/Briscola-Multiplayer/Libraries/DatabaseHandler.swift @@ -31,11 +31,9 @@ class DatabaseHandler { // // MARK: Private Methods - private func savePlayer(_ player: PlayerModel) -> NSManagedObject { - let context = container.viewContext; - - let entity = NSEntityDescription.entity(forEntityName: PLAYER_TABLE_NAME, in: context) - let newPlayer = NSManagedObject(entity: entity!, insertInto: context); + private func savePlayer(_ player: DB_Player) -> NSManagedObject { + let entity = NSEntityDescription.entity(forEntityName: PLAYER_TABLE_NAME, in: container.viewContext) + let newPlayer = NSManagedObject(entity: entity!, insertInto: container.viewContext); newPlayer.setValue(player.index, forKey: "index"); newPlayer.setValue(player.name, forKey: "name"); @@ -45,41 +43,56 @@ class DatabaseHandler { return newPlayer; } + private func savePlayerResult(_ result: DB_Result) -> NSManagedObject { + let entity = NSEntityDescription.entity(forEntityName: RESULT_TABLE_NAME, in: container.viewContext) + let newPlayerResult = NSManagedObject(entity: entity!, insertInto: container.viewContext); + + newPlayerResult.setValue(result.cards, forKey: "cards"); + newPlayerResult.setValue(result.handsWon, forKey: "handsWon"); + newPlayerResult.setValue(result.points, forKey: "points"); + container.saveContext(); + + return newPlayerResult; + } + // // MARK: Public Methods - public func getMatches() -> Array { + public func getMatches() -> [DB_Match] { let request = NSFetchRequest(entityName: MATCH_TABLE_NAME); request.returnsObjectsAsFaults = false // request.predicate = NSPredicate(format: "age = %@", "12") // run the query - let results = container.getContext(request: request) ?? []; - for data in results as! [NSManagedObject] { - print(data.value(forKey: "playersCount") as! Int); - print(data.value(forKey: "player1") ?? "[INFO] value not found"); - print(data.value(forKey: "player2") ?? "[INFO] value not found"); + var results: [DB_Match] = []; + let dbQueryResults = container.getContext(request: request) ?? []; + for match in dbQueryResults as! [Matches] { + results.append(DB_Match.instanceFrom(match)); } return results; } - public func saveMatch(players: Array) { + public func saveMatch(_ match: DB_Match) { let context = container.viewContext; let entity = NSEntityDescription.entity(forEntityName: MATCH_TABLE_NAME, in: context) let newMatch = NSManagedObject(entity: entity!, insertInto: context); - // create players - for (pIndex, player) in players.enumerated() { - let playerCreated: NSManagedObject = savePlayer(player); - // assign player to this match - newMatch.setValue(playerCreated, forKey: "player\(pIndex + 1)"); - } + // players + let localPlayer: NSManagedObject = savePlayer(match.localPlayer); + let remotePlayer: NSManagedObject = savePlayer(match.remotePlayer); + newMatch.setValue(localPlayer, forKey: "localPlayer"); + newMatch.setValue(remotePlayer, forKey: "remotePlayer"); + + // results + let localPlayerResult: NSManagedObject = savePlayerResult(match.localPlayerResult); + let remotePlayerResult: NSManagedObject = savePlayerResult(match.remotePlayerResult); + newMatch.setValue(localPlayerResult, forKey: "localPlayerResult"); + newMatch.setValue(remotePlayerResult, forKey: "remotePlayerResult"); - newMatch.setValue(players.count, forKey: "playersCount"); container.saveContext(); } diff --git a/Briscola-Multiplayer/Libraries/FacebookManager.swift b/Briscola-Multiplayer/Libraries/FacebookManager.swift index 1eb0f90..f0c5a03 100644 --- a/Briscola-Multiplayer/Libraries/FacebookManager.swift +++ b/Briscola-Multiplayer/Libraries/FacebookManager.swift @@ -80,10 +80,22 @@ class FacebookManager { return "https://graph.facebook.com/\(userId)/picture?type=large"; } - public func shareTextOnFaceBook(controller: SocialsController) { + public func shareTextOnFaceBook(controller: SocialController, match: DB_Match) { let shareContent = ShareLinkContent() - shareContent.contentURL = URL.init(string: "https://developers.facebook.com")! //your link - shareContent.quote = "Text to be shared" + + // url + shareContent.contentURL = URL.init(string: CONSTANTS.APP_GITHUB_REPOSITORY_LINK)!; + + // message + var localPlayerMatchResultAction: String = "lost"; + if (match.hasLocalPlayerWon()) { + localPlayerMatchResultAction = "won"; + } else if (match.hasLocalPlayerDrew()) { + localPlayerMatchResultAction = "drew"; + } + + // show share dialog + shareContent.quote = "I \(localPlayerMatchResultAction) a game against \(match.remotePlayer.name)"; ShareDialog(fromViewController: controller, content: shareContent, delegate: controller).show() } diff --git a/Briscola-Multiplayer/Libraries/GameHandler.swift b/Briscola-Multiplayer/Libraries/GameHandler.swift index 9599edc..4b957b3 100644 --- a/Briscola-Multiplayer/Libraries/GameHandler.swift +++ b/Briscola-Multiplayer/Libraries/GameHandler.swift @@ -27,7 +27,7 @@ public class GameHandler { // // MARK: Public Methods - public func initSinglePlayer(numberOfPlayers: Int, localPlayerIndex: Int, playersType: [PlayerType]) { + public func initSinglePlayer(numberOfPlayers: Int, localPlayerIndex: Int, playersName: [String]) { self.mode = .singleplayer; gameEnded = false; @@ -38,15 +38,16 @@ public class GameHandler { trumpCard = cards.last!; // players - var playersType: [PlayerType] = [] + var playersType: [PlayerType] = []; for i in 0.. Bool { @@ -187,7 +188,7 @@ public class GameHandler { return card; } - private func _initializePlayers(numberOfPlayers: Int, playersType: Array) { + private func _initializePlayers(numberOfPlayers: Int, playersType: Array, playersName: [String]) { // foreach player: create the first hand and instance the model. for playerIndex in 0.., type: PlayerType) { + init(index: Int, initialHand: Array, type: PlayerType, name: String?) { self.index = index; - self.name = "player-\(index)"; self.cardsHand = initialHand; self.type = type; + + if name == nil { + self.name = "player-\(index)"; + } else { + self.name = name!; + } } // diff --git a/Briscola-Multiplayer/Storyboards/Base.lproj/Main.storyboard b/Briscola-Multiplayer/Storyboards/Base.lproj/Main.storyboard index 935551e..f43d389 100644 --- a/Briscola-Multiplayer/Storyboards/Base.lproj/Main.storyboard +++ b/Briscola-Multiplayer/Storyboards/Base.lproj/Main.storyboard @@ -56,8 +56,9 @@ -