historic is well saved

dev_vm_saveHistorique
Rayhân HASSOU 10 months ago
parent dcdc6113d1
commit a171478e2c

@ -70,6 +70,7 @@
ECE777142C2068F400D354B0 /* EditPlayerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECE777132C2068F400D354B0 /* EditPlayerView.swift */; };
ECF3FD312C2722C600F5E62B /* CDHistoriqueExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECF3FD302C2722C600F5E62B /* CDHistoriqueExtension.swift */; };
ECF3FD332C27238F00F5E62B /* Historique.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECF3FD322C27238F00F5E62B /* Historique.swift */; };
ECF3FD3A2C29A20D00F5E62B /* HistoricListVM.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECF3FD392C29A20D00F5E62B /* HistoricListVM.swift */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@ -168,6 +169,7 @@
ECF3FD352C299CC100F5E62B /* Fight.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = Fight.mp3; sourceTree = "<group>"; };
ECF3FD372C299CC500F5E62B /* Start.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = Start.mp3; sourceTree = "<group>"; };
ECF3FD382C299CC800F5E62B /* SelectFighterSound.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = SelectFighterSound.mp3; sourceTree = "<group>"; };
ECF3FD392C29A20D00F5E62B /* HistoricListVM.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoricListVM.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -321,8 +323,8 @@
646FA83F2C0730B4001466BA /* Game */ = {
isa = PBXGroup;
children = (
ECF3FD3B2C29A21200F5E62B /* Historic */,
646FA83D2C072340001466BA /* GameVM.swift */,
646FA8422C0730D6001466BA /* HistoricVM.swift */,
6437FF122C25846F009D0EAF /* PlayingGameVM.swift */,
);
path = Game;
@ -507,6 +509,15 @@
name = Products;
sourceTree = "<group>";
};
ECF3FD3B2C29A21200F5E62B /* Historic */ = {
isa = PBXGroup;
children = (
646FA8422C0730D6001466BA /* HistoricVM.swift */,
ECF3FD392C29A20D00F5E62B /* HistoricListVM.swift */,
);
path = Historic;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
@ -711,6 +722,7 @@
EC62C52D2C197ED10048CD0B /* Player.swift in Sources */,
EC62C5292C1974000048CD0B /* PlayersVM.swift in Sources */,
6437FF132C25846F009D0EAF /* PlayingGameVM.swift in Sources */,
ECF3FD3A2C29A20D00F5E62B /* HistoricListVM.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

@ -91,12 +91,12 @@ class CoreDataManager {
}
}
func saveHistorique(historique: Historique) {
func saveHistorique(historique: HistoricVM) {
let historiqueEntity = CDHistorique(context: context)
historiqueEntity.player1_name = historique.player1_name
historiqueEntity.player2_name = historique.player2_name
historiqueEntity.time = historique.time
historiqueEntity.result = historique.result
historiqueEntity.player1_name = historique.historic.player1_name
historiqueEntity.player2_name = historique.historic.player2_name
historiqueEntity.time = historique.historic.time
historiqueEntity.result = historique.historic.result
saveContext()
}

@ -13,25 +13,25 @@ import DouShouQiModel
struct GameResumeFrame: View {
// Players Params
let gameVM: GameVM
let historicVm: HistoricVM
var body: some View {
ZStack {
HStack(alignment: .center) {
VStack(alignment: .leading) {
Text("\(gameVM.player1Name) vs \(gameVM.player2Name)")
Text("\(historicVm.historic.player1_name) vs \(historicVm.historic.player2_name)")
.font(.headline)
Text(gameVM.gameStatus)
Text(historicVm.historic.result)
.font(.subheadline)
.foregroundColor(.gray)
Text(historicVm.historic.time)
.font(.subheadline)
.foregroundColor(.gray)
}
Spacer()
Text("Detail >")
.frame(width: 100)
.foregroundColor(.gray)
}
.padding(10)
@ -42,14 +42,3 @@ struct GameResumeFrame: View {
}.padding(2)
}
}
struct GameResumeFrame_Previews: PreviewProvider {
static var previews: some View {
do {
let game = try Game(withRules: VerySimpleRules(), andPlayer1: HumanPlayer(withName: "Rémi", andId: .player1)!, andPlayer2: HumanPlayer(withName: "Nathan", andId: .player2)!)
return AnyView(GameResumeFrame(gameVM: GameVM(withGame: game, time: "11:01")))
} catch {
return AnyView(Text("Erreur lors de la création du jeu : \(error.localizedDescription)"))
}
}
}

@ -92,6 +92,6 @@ struct RoundedCornersShape: Shape {
struct MainMenuButton_Previews: PreviewProvider {
static var previews: some View {
MainMenuButton(text: "test", destination: HistoricView(historicVM: HistoricVM()), sound: "TitleScreenButtonSound", horizontalAlignment: .leading, topRightCorner: 10, bottomRightCorner: 10)
MainMenuButton(text: "test", destination: HistoricView(historicListVM: HistoricListVM()), sound: "TitleScreenButtonSound", horizontalAlignment: .leading, topRightCorner: 10, bottomRightCorner: 10)
}
}

@ -0,0 +1,32 @@
//
// HistoricListVM.swift
// DouShouQi_App
//
// Created by étudiant on 24/06/2024.
//
import Foundation
import SwiftUI
import DouShouQiModel
class HistoricListVM: ObservableObject {
@Published var historicList: [HistoricVM]
public func SavePlayer(historic: HistoricVM){
historicList.append(historic)
CoreDataManager.shared.saveHistorique(historique: historic)
}
public func getAllHistoric() -> [HistoricVM]{
let coreDataPlayers = CoreDataManager.shared.fetchHistoriques()
self.historicList = coreDataPlayers.map { HistoricVM(historic: $0.toModel()) }
return historicList
}
init() {
historicList = []
historicList = getAllHistoric()
}
}

@ -0,0 +1,27 @@
//
// HistoricVM.swift
// DouShouQi_App
//
// Created by Rémi REGNAULT on 29/05/2024.
//
import Foundation
import SwiftUI
import DouShouQiModel
class HistoricVM: ObservableObject, Identifiable, Hashable{
public static func == (lhs: HistoricVM, rhs: HistoricVM) -> Bool {
return lhs.id == rhs.id
}
public func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
@Published var historic: Historique
init(historic: Historique) {
self.historic = historic
}
}

@ -1,26 +0,0 @@
//
// HistoricVM.swift
// DouShouQi_App
//
// Created by Rémi REGNAULT on 29/05/2024.
//
import Foundation
import SwiftUI
import DouShouQiModel
class HistoricVM: ObservableObject {
// Properties
public var gameVMs: [GameVM]
// Init
init() {
gameVMs = []
do {
let game = try Game(withRules: VerySimpleRules(), andPlayer1: HumanPlayer(withName: "Rémi", andId: .player1)!, andPlayer2: HumanPlayer(withName: "Nathan", andId: .player2)!)
gameVMs.append(GameVM(withGame: game, time: "11:01"))
} catch {
// Do nothing
}
}
}

@ -1,10 +1,3 @@
//
// MainMenuView.swift
// DouShouQi_App
//
// Created by Nathan Verdier on 26/05/2024.
//
import SwiftUI
import SpriteKit
import DouShouQiModel
@ -16,6 +9,10 @@ struct GameView: View {
@State private var showHome = false
var player1_name: String
var player2_name: String
@StateObject var historicVm = HistoricListVM()
var body: some View {
ZStack {
VStack {
@ -25,8 +22,6 @@ struct GameView: View {
.onAppear {
playSound(named: "Fight")
//MusicPlayer.shared.playBackgroundMusic(music: "GameMusic")
DispatchQueue.main.asyncAfter(deadline: .now() + 0.7) {
MusicPlayer.shared.playBackgroundMusic(music: "GameMusic")
}
@ -35,31 +30,40 @@ struct GameView: View {
MusicPlayer.shared.stopBackgroundMusic()
}
if (gameVM.game.isOver) {
HStack {
Spacer()
VStack(alignment: .center) {
Text("Game Over")
.font(.title2)
Text("Winner: \(gameVM.winner)")
}
.background(Colors.WinnerBackground)
if gameVM.game.isOver {
VStack(spacing: 20) {
Text("Game Over")
.font(.title)
.fontWeight(.bold)
Spacer()
Text("Winner: \(gameVM.winner)")
.font(.title2)
}
.padding()
.background(Colors.WinnerBackground)
.cornerRadius(15)
.shadow(radius: 10)
.padding()
.onAppear {
saveHistoric()
Timer.scheduledTimer(withTimeInterval: 10, repeats: false) { _ in
// Il faut retourner au home
withAnimation {
showHome = true
}
}
}
}
}
.onDisappear {
MusicPlayer.shared.stopBackgroundMusic()
}
.navigationBarHidden(true) // Hide the navigation bar
.navigationBarHidden(true)
}
private func saveHistoric() {
let newHistoric = HistoricVM(historic: Historique(player1_name: player1_name, player2_name: player2_name, time: "4:11", result: gameVM.winner))
historicVm.SavePlayer(historic: newHistoric)
}
}

@ -1,10 +1,3 @@
//
// HistoricView.swift
// DouShouQi_App
//
// Created by Rémi REGNAULT on 21/05/2024.
//
import SwiftUI
#if DEBUG
import DouShouQiModel
@ -12,16 +5,15 @@ import DouShouQiModel
struct HistoricView: View {
// GameVMs
var historicVM: HistoricVM
@ObservedObject var historicListVM: HistoricListVM
var body: some View {
VStack {
TitlePageFrame(Text: "Historic", ImageWidth: 200, ImageHeight: 200)
VStack {
List(historicVM.gameVMs) { gameVM in
GameResumeFrame(gameVM: gameVM)
List(historicListVM.historicList) { historicVM in
GameResumeFrame(historicVm: historicVM)
}
}
.padding(.horizontal, 10)
@ -34,6 +26,6 @@ struct HistoricView: View {
struct HistoricView_Previews: PreviewProvider {
static var previews: some View {
HistoricView(historicVM: HistoricVM())
HistoricView(historicListVM: HistoricListVM())
}
}

@ -14,6 +14,8 @@ struct MainMenuView: View {
@State private var timer: Timer?
@State private var showImage = false
@StateObject var historicList = HistoricListVM()
let images: [String] = [AppImages.SemiLion, AppImages.SemiDog, AppImages.SemiRat, AppImages.SemiWolf, AppImages.SemiLeopard, AppImages.SemiElephant, AppImages.SemiCat] // Add your image names here
var body: some View {
@ -38,7 +40,7 @@ struct MainMenuView: View {
HStack {
VStack(spacing: 25) {
MainMenuButton(text: "Play", destination: SelectPlayerView(), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10)
MainMenuButton(text: "Historique", destination: HistoricView(historicVM: HistoricVM()), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10)
MainMenuButton(text: "Historique", destination: HistoricView(historicListVM: historicList), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10)
MainMenuButton(text: "Best Scores", destination: ScoreBoardView(), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10)
MainMenuButton(text: "Players", destination: PlayersView(), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10)
MainMenuButton(text: "Settings", destination: SettingsView(), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10)

@ -27,7 +27,7 @@ struct SelectPlayerView: View {
HStack {
VStack(alignment: .trailing)
{
MainMenuButton(text: "Start", destination: GameView(gameVM: PlayingGameVM(withRules: ClassicRules(), andPlayer1: HumanPlayer(withName: player1.player.name, andId: .player1)!, andPlayer2: HumanPlayer(withName: player2.player.name, andId: .player2)!)!), sound: "Start", horizontalAlignment: .trailing, topLeftCorner: 10, bottomLeftCorner: 10)
MainMenuButton(text: "Start", destination: GameView(gameVM: PlayingGameVM(withRules: ClassicRules(), andPlayer1: HumanPlayer(withName: player1.player.name, andId: .player1)!, andPlayer2: HumanPlayer(withName: player2.player.name, andId: .player2)!)!, player1_name: player1.player.name, player2_name: player2.player.name), sound: "Start", horizontalAlignment: .trailing, topLeftCorner: 10, bottomLeftCorner: 10)
MainMenuButton(text: "Settings", destination: ContentView(), sound: "TitleScreenButtonSound", horizontalAlignment: .trailing, topLeftCorner: 10, bottomLeftCorner: 10)
}

Loading…
Cancel
Save