diff --git a/Pendu/GameEngine.swift b/Pendu/GameEngine.swift index 8c43086bd88e02328128e16a398f5bb76534f8cf..2fa18691d7d2af2ca34f61e8562b51e78359cbee 100644 --- a/Pendu/GameEngine.swift +++ b/Pendu/GameEngine.swift @@ -6,24 +6,32 @@ // import UIKit +import CoreData class GameEngine: NSObject { - private var listThemeAnimalsEasy : [String] = ["Chien", "Chat", "Lion", "Girafe", "Éléphant", "Serpent", "Oiseau", "Tigre", "Papillon", "Singe"] - private var listThemeSportsEasy : [String] = ["Football", "Basket-ball", "Tennis", "Natation", "Rugby", "Golf", "Volley-ball", "Baseball", "Athlétisme", "Boxe"] - private var listThemeFoodEasy : [String] = ["Pomme", "Banane", "Carotte", "Poisson", "Riz", "Fromage", "Pain", "Chocolat", "Fraise", "Poulet"] - private var listThemeCitiesEasy : [String] = ["Paris", "Londres", "New York", "Rome", "Tokyo", "Sydney", "Barcelone", "Berlin", "Istanbul", "Mumbai"] - private var listThemeColorsEasy : [String] = ["Rouge", "Bleu", "Vert", "Jaune", "Rose", "Violet", "Orange", "Gris", "Marron", "Noir"] + private var listThemeAnimalsEasy : [String] = ["Chien", "Chat", "Lion", "Girafe", "Éléphant", "Serpent", "Oiseau", "Tigre", "Papillon", "Singe"]; + private var listThemeSportsEasy : [String] = ["Football", "Basket-ball", "Tennis", "Natation", "Rugby", "Golf", "Volley-ball", "Baseball", "Athlétisme", "Boxe"]; + private var listThemeFoodEasy : [String] = ["Pomme", "Banane", "Carotte", "Poisson", "Riz", "Fromage", "Pain", "Chocolat", "Fraise", "Poulet"]; + private var listThemeCitiesEasy : [String] = ["Paris", "Londres", "New York", "Rome", "Tokyo", "Sydney", "Barcelone", "Berlin", "Istanbul", "Mumbai"]; + private var listThemeColorsEasy : [String] = ["Rouge", "Bleu", "Vert", "Jaune", "Rose", "Violet", "Orange", "Gris", "Marron", "Noir"]; - private var listThemeAnimalsHard : [String] = ["Chimpanzé", "Lynx", "Hippopotame", "Pélican", "Kangourou", "Anaconda", "Chimère", "Panda roux", "Calmar géant", "Ouistiti"] - private var listThemeSportsHard : [String] = ["Escrime", "Squash", "Haltérophilie", "Badminton", "Ski de fond", "Pentathlon moderne", "Canoë-kayak", "Aviron", "Patinage artistique", "Triathlon"] - private var listThemeFoodHard : [String] = ["Artichaut", "Quinoa", "Courge", "Wasabi", "Chou-fleur", "Sarrasin", "Salsifis", "Morue", "Canneberge", "Pistache"] - private var listThemeCitiesHard : [String] = ["Copenhague", "Budapest", "Dubrovnik", "Séoul", "Marrakech", "Reykjavik", "La Nouvelle-Orléans", "Bratislava", "Wellington", "Accra"] - private var listThemeColorsHard : [String] = ["Écarlate", "Azur", "Émeraude", "Safran", "Pourpre", "Indigo", "Sienne", "Cyan", "Vermillon", "Lavande"] + private var listThemeAnimalsHard : [String] = ["Chimpanzé", "Lynx", "Hippopotame", "Pélican", "Kangourou", "Anaconda", "Chimère", "Panda roux", "Calmar géant", "Ouistiti"]; + private var listThemeSportsHard : [String] = ["Escrime", "Squash", "Haltérophilie", "Badminton", "Ski de fond", "Pentathlon moderne", "Canoë-kayak", "Aviron", "Patinage artistique", "Triathlon"]; + private var listThemeFoodHard : [String] = ["Artichaut", "Quinoa", "Courge", "Wasabi", "Chou-fleur", "Sarrasin", "Salsifis", "Morue", "Canneberge", "Pistache"]; + private var listThemeCitiesHard : [String] = ["Copenhague", "Budapest", "Dubrovnik", "Séoul", "Marrakech", "Reykjavik", "La Nouvelle-Orléans", "Bratislava", "Wellington", "Accra"]; + private var listThemeColorsHard : [String] = ["Écarlate", "Azur", "Émeraude", "Safran", "Pourpre", "Indigo", "Sienne", "Cyan", "Vermillon", "Lavande"]; - private var wordToGuess : String = "" - private var attemptsRemaining : Int = 0 - private var guessedLetters : [Character] = [] + private var wordToGuess : String = ""; + private var attemptsRemaining : Int = 0; + private var guessedLetters : [Character] = []; + private var theme : String = ""; + private var difficulty : String = ""; + private var playerName : String = ""; + private var score : Int = 0; + private var combo : Int = 0; + + private var managedObjectContext: NSManagedObjectContext?; override init() { // @@ -31,6 +39,8 @@ class GameEngine: NSObject { func WordToGuess(theme: String, difficulty: String) -> String{ var word = "" + self.theme = theme; + self.difficulty = difficulty; if theme == "Animals"{ if difficulty == "Easy"{ let randomInt = Int.random(in: 0..<listThemeAnimalsEasy.count) @@ -95,6 +105,24 @@ class GameEngine: NSObject { public func saveScore() -> Void { // Enregistrement du score dans la base Core Data + + // Création du gestionnaire d'objets persistants + let context: NSManagedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext; + + // Création de l'instance de l'entité CoreData + let entity = NSEntityDescription.entity(forEntityName: "Score", in: context)!; + let object = NSManagedObject(entity: entity, insertInto: context); + object.setValue(theme, forKeyPath: "categoryName"); + object.setValue(difficulty, forKeyPath: "difficulty"); + object.setValue(playerName, forKeyPath: "playerName"); + object.setValue(score, forKeyPath: "score"); + + // On enregistre + do { + try context.save(); + } catch let error as NSError { + print("Erreur lors de l'enregistrement des données : \(error), \(error.userInfo)"); + } } static func getOnlineScoreboard() -> Void { diff --git a/Pendu/Pendu.xcdatamodeld/Pendu.xcdatamodel/contents b/Pendu/Pendu.xcdatamodeld/Pendu.xcdatamodel/contents index c446d97726d6a01470796fa216f7c9176d7d3996..b13b2ba73d77d3a2042ecea1671b4756a3529846 100644 --- a/Pendu/Pendu.xcdatamodeld/Pendu.xcdatamodel/contents +++ b/Pendu/Pendu.xcdatamodeld/Pendu.xcdatamodel/contents @@ -1,12 +1,12 @@ <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="20086" systemVersion="21E258" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier=""> - <entity name="Scoreboard" representedClassName="Scoreboard" syncable="YES" codeGenerationType="class"> + <entity name="Score" representedClassName="Score" syncable="YES" codeGenerationType="class"> <attribute name="categoryName" optional="YES" attributeType="String"/> <attribute name="difficulty" optional="YES" attributeType="String"/> <attribute name="playerName" optional="YES" attributeType="String"/> <attribute name="score" optional="YES" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES"/> </entity> <elements> - <element name="Scoreboard" positionX="-63" positionY="-18" width="128" height="89"/> + <element name="Score" positionX="-63" positionY="-18" width="128" height="89"/> </elements> </model> \ No newline at end of file