Merge branch 'dev_vm_saveHistorique'

Conflicts:
	DouShouQi_App/DouShouQi_App/Components/MainMenuButton.swift
	DouShouQi_App/DouShouQi_App/Views/Game/GameView.swift
	DouShouQi_App/DouShouQi_App/Views/Menu/MainMenuView.swift
	DouShouQi_App/DouShouQi_App/Views/Player/SelectPlayerView.swift
dev_ARKit^2
Rayhân HASSOU 10 months ago
commit f5428fdb18

@ -70,6 +70,8 @@
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 */; };
ECF3FD3D2C29B64100F5E62B /* ChronoVM.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECF3FD3C2C29B64100F5E62B /* ChronoVM.swift */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@ -168,6 +170,8 @@
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>"; };
ECF3FD3C2C29B64100F5E62B /* ChronoVM.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChronoVM.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -314,6 +318,7 @@
EC62C5262C11E24B0048CD0B /* Players */,
646F04BC2C0F5491003C8600 /* Settings */,
646FA83F2C0730B4001466BA /* Game */,
ECF3FD3C2C29B64100F5E62B /* ChronoVM.swift */,
);
path = ViewModel;
sourceTree = "<group>";
@ -321,8 +326,8 @@
646FA83F2C0730B4001466BA /* Game */ = {
isa = PBXGroup;
children = (
ECF3FD3B2C29A21200F5E62B /* Historic */,
646FA83D2C072340001466BA /* GameVM.swift */,
646FA8422C0730D6001466BA /* HistoricVM.swift */,
6437FF122C25846F009D0EAF /* PlayingGameVM.swift */,
);
path = Game;
@ -507,6 +512,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 */
@ -680,6 +694,7 @@
EC0540C42C08A13E0032E9EF /* GameView.swift in Sources */,
EC05BFC42C04C3C4000F7B19 /* SettingsView.swift in Sources */,
ECF3FD312C2722C600F5E62B /* CDHistoriqueExtension.swift in Sources */,
ECF3FD3D2C29B64100F5E62B /* ChronoVM.swift in Sources */,
EC62C53D2C1C69200048CD0B /* DouShouQi_App.xcdatamodeld in Sources */,
EC62C50F2C05D06A0048CD0B /* AddPlayerView.swift in Sources */,
EC62C53F2C1C6D1A0048CD0B /* CDPlayerExtension.swift in Sources */,
@ -711,6 +726,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)"))
}
}
}

@ -10,6 +10,7 @@ import DouShouQiModel
struct TopGameBoard: View {
@ObservedObject var gameVM: PlayingGameVM
@ObservedObject var chronoVM: ChronometerViewModel
var body: some View {
VStack {
@ -32,7 +33,7 @@ struct TopGameBoard: View {
Image(systemName: "clock")
.resizable()
.frame(width: 30, height: 30)
Text("4:11")
Text(chronoVM.formattedTime())
.font(.title)
.foregroundColor(.black)
}
@ -43,9 +44,6 @@ struct TopGameBoard: View {
Text("Last move :")
.font(.title2)
.foregroundColor(.black)
/*Image(systemName: "cheetah")
.resizable()
.frame(width: 30, height: 30)*/
Text(gameVM.lastPieceMoved)
Text(gameVM.lastMove)
.font(.title2)
@ -62,9 +60,3 @@ struct TopGameBoard: View {
.background(Color.red.opacity(0.5))
}
}
//struct TopGameBoard_Previews: PreviewProvider {
// static var previews: some View {
// TopGameBoard(gameVM: vm)
// }
//}

@ -92,6 +92,6 @@ struct RoundedCornersShape: Shape {
struct MainMenuButton_Previews: PreviewProvider {
static var previews: some View {
MainMenuButton(text: "test", 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 @@
//
// ChronoVM.swift
// DouShouQi_App
//
// Created by étudiant on 24/06/2024.
//
import SwiftUI
import Combine
class ChronometerViewModel: ObservableObject {
@Published var elapsedTime: TimeInterval = 0
private var timer: AnyCancellable?
func startTimer() {
let startDate = Date()
timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect().sink { _ in
self.elapsedTime = Date().timeIntervalSince(startDate)
}
}
func stopTimer() {
timer?.cancel()
}
func formattedTime() -> String {
let minutes = Int(elapsedTime) / 60
let seconds = Int(elapsedTime) % 60
return String(format: "%02d:%02d", minutes, seconds)
}
}

@ -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
@ -13,20 +6,24 @@ struct GameView: View {
var gameScene: GameScene = GameScene(size: CGSize(width: 700, height: 900))
@StateObject var gameVM: PlayingGameVM
@StateObject var chronoVM = ChronometerViewModel()
@Binding var isDisplayed: Bool
@Binding var parentIsDisplayed: Bool
var player1_name: String
var player2_name: String
@StateObject var historicVm = HistoricListVM()
var body: some View {
ZStack {
VStack {
TopGameBoard(gameVM: gameVM).frame(maxHeight: 200)
TopGameBoard(gameVM: gameVM, chronoVM: chronoVM).frame(maxHeight: 200)
SpriteView(scene: GameScene(size: CGSize(width: 700, height: 900), gameVM: gameVM))
}
.onAppear {
playSound(named: "Fight")
//MusicPlayer.shared.playBackgroundMusic(music: "GameMusic")
chronoVM.startTimer()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.7) {
MusicPlayer.shared.playBackgroundMusic(music: "GameMusic")
@ -36,20 +33,23 @@ struct GameView: View {
MusicPlayer.shared.stopBackgroundMusic()
}
if (gameVM.game.isOver) {
HStack {
Spacer()
if gameVM.game.isOver {
VStack(spacing: 20) {
Text("Game Over")
.font(.title)
.fontWeight(.bold)
VStack(alignment: .center) {
Text("Game Over")
.font(.title2)
Text("Winner: \(gameVM.winner)")
}
.background(Colors.WinnerBackground)
Spacer()
Text("Winner: \(gameVM.winner)")
.font(.title2)
}
.padding()
.background(Colors.WinnerBackground)
.cornerRadius(15)
.shadow(radius: 10)
.padding()
.onAppear {
chronoVM.stopTimer()
saveHistoric()
Timer.scheduledTimer(withTimeInterval: 10, repeats: false) { _ in
isDisplayed = false
@ -63,10 +63,9 @@ struct GameView: View {
}
.navigationBarHidden(true)
}
}
/*struct GameView_Previews: PreviewProvider {
static var previews: some View {
GameView()
private func saveHistoric() {
let newHistoric = HistoricVM(historic: Historique(player1_name: player1_name, player2_name: player2_name, time: chronoVM.formattedTime(), 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())
}
}

@ -19,6 +19,7 @@ struct MainMenuView: View {
@State var showBestScores = false
@State var showPlayers = false
@State var showSettings = 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

Loading…
Cancel
Save