From 85f8a5db207ed2edec7f33ed62c5ad9cfd7c12ef Mon Sep 17 00:00:00 2001 From: Baptiste ARNAUD Date: Tue, 26 Nov 2019 09:05:58 +0100 Subject: [PATCH] serialisation valider --- TheGameExtreme/IO/IOGamePreparation.cs | 60 +++++++++++++++++-- .../view/GamePreparationPage.xaml.cs | 35 +++++++++-- TheGameExtreme/view/Settings.xaml.cs | 6 ++ 3 files changed, 92 insertions(+), 9 deletions(-) diff --git a/TheGameExtreme/IO/IOGamePreparation.cs b/TheGameExtreme/IO/IOGamePreparation.cs index 399bdbb..fa02f11 100644 --- a/TheGameExtreme/IO/IOGamePreparation.cs +++ b/TheGameExtreme/IO/IOGamePreparation.cs @@ -9,15 +9,17 @@ namespace TheGameExtreme.IO class IOGamePreparation { static string pathPlayers = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "NbPlayers.xml"); + static string pathGameModeValue = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "GameModeValue.xml"); + static string pathNbStacks = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "NbStacks.xml"); - public static void SaveParamaterGamePreparation(int nbPlayers) + public static void SaveParamaterGamePreparationNbPlayers(int nbPlayers) { XmlSerializer ser = new XmlSerializer(typeof(int)); TextWriter tw = new StreamWriter(pathPlayers); ser.Serialize(tw, nbPlayers); tw.Close(); } - public static int LoadParameterGamePreparation() + public static int LoadParameterGamePreparationNbPlayers() { XmlSerializer xs = new XmlSerializer(typeof(int)); @@ -27,13 +29,63 @@ namespace TheGameExtreme.IO { int players = (int)xs.Deserialize(fs); return players; - } - + } } catch { return 0; } } + + public static void SaveParameterGamePreparationGameModeValue(int mode) + { + XmlSerializer xs = new XmlSerializer(typeof(int)); + TextWriter tw = new StreamWriter(pathGameModeValue); + xs.Serialize(tw, mode); + tw.Close(); + } + + public static int LoadParameterGamePreparationGameModeValue() + { + XmlSerializer xs = new XmlSerializer(typeof(int)); + try + { + using (FileStream fs = new FileStream(pathGameModeValue, FileMode.Open)) + { + int gameMode = (int)xs.Deserialize(fs); + return gameMode; + } + } + catch + { + + return 0; + } + } + + public static void SaveParameterGamePreparationNbStacks(int nbStack) + { + XmlSerializer xs = new XmlSerializer(typeof(int)); + TextWriter tw = new StreamWriter(pathNbStacks); + xs.Serialize(tw, nbStack); + tw.Close(); + } + + public static int LoadParamaterGamePreparationNbStacks() + { + XmlSerializer xs = new XmlSerializer(typeof(int)); + try + { + using (FileStream fs = new FileStream(pathNbStacks, FileMode.Open)) + { + int nbstacks = (int)xs.Deserialize(fs); + return nbstacks; + } + } + catch + { + return 0; + } + } } } diff --git a/TheGameExtreme/view/GamePreparationPage.xaml.cs b/TheGameExtreme/view/GamePreparationPage.xaml.cs index 3adcccb..0dfa340 100644 --- a/TheGameExtreme/view/GamePreparationPage.xaml.cs +++ b/TheGameExtreme/view/GamePreparationPage.xaml.cs @@ -21,18 +21,29 @@ namespace TheGameExtreme.view LoadParameterNbPlayerGamePreparation(); SelectMode.ItemsSource = listGameMode; - SelectMode.SelectedIndex = 0; + LoadParameterGameModeValueGamePreparation(); SelectNbStack.ItemsSource = listNbStack; - SelectNbStack.SelectedIndex = 0; + LoadParameterNbStacksGamePreparation(); + + - } private async void Back(object sender, EventArgs e) { + IOGamePreparation.SaveParamaterGamePreparationNbPlayers(PlayerSelecter.SelectedIndex); + IOGamePreparation.SaveParameterGamePreparationGameModeValue(SelectMode.SelectedIndex); + IOGamePreparation.SaveParameterGamePreparationNbStacks(SelectNbStack.SelectedIndex); await Navigation.PopAsync(); } + protected override bool OnBackButtonPressed() + { + IOGamePreparation.SaveParamaterGamePreparationNbPlayers(PlayerSelecter.SelectedIndex); + IOGamePreparation.SaveParameterGamePreparationGameModeValue(SelectMode.SelectedIndex); + IOGamePreparation.SaveParameterGamePreparationNbStacks(SelectNbStack.SelectedIndex); + return base.OnBackButtonPressed(); + } private async void Play(object sender, EventArgs args) { @@ -48,7 +59,9 @@ namespace TheGameExtreme.view return; } } - IOGamePreparation.SaveParamaterGamePreparation(PlayerSelecter.SelectedIndex); + IOGamePreparation.SaveParamaterGamePreparationNbPlayers(PlayerSelecter.SelectedIndex); + IOGamePreparation.SaveParameterGamePreparationGameModeValue(SelectMode.SelectedIndex); + IOGamePreparation.SaveParameterGamePreparationNbStacks(SelectNbStack.SelectedIndex); await Navigation.PushAsync(new MainPage(playersNames, (int)SelectNbStack.SelectedItem, (int)SelectMode.SelectedIndex)); } @@ -80,10 +93,22 @@ namespace TheGameExtreme.view } public void LoadParameterNbPlayerGamePreparation() { - int nbJoueurs = IOGamePreparation.LoadParameterGamePreparation(); + int nbJoueurs = IOGamePreparation.LoadParameterGamePreparationNbPlayers(); PlayerSelecter.SelectedIndex = nbJoueurs; } + public void LoadParameterGameModeValueGamePreparation() + { + int gameModeValue = IOGamePreparation.LoadParameterGamePreparationGameModeValue(); + SelectMode.SelectedIndex = gameModeValue; + } + + public void LoadParameterNbStacksGamePreparation() + { + int nbStackSer = IOGamePreparation.LoadParamaterGamePreparationNbStacks(); + SelectNbStack.SelectedIndex = nbStackSer; + } + } diff --git a/TheGameExtreme/view/Settings.xaml.cs b/TheGameExtreme/view/Settings.xaml.cs index 7621477..a627d36 100644 --- a/TheGameExtreme/view/Settings.xaml.cs +++ b/TheGameExtreme/view/Settings.xaml.cs @@ -87,5 +87,11 @@ namespace TheGameExtreme.view //serialisationSwitch(); await Navigation.PopAsync(); } + + protected override bool OnBackButtonPressed() + { + IOOptions.SaveOptionParameter(swTheme.IsToggled); + return base.OnBackButtonPressed(); + } } }