refactor, saveManeger

This commit is contained in:
HeshamTB 2018-09-20 11:23:47 +03:00
parent 5ce37c59c0
commit d2cc397c69
4 changed files with 49 additions and 90 deletions

View File

@ -17,7 +17,7 @@ namespace OWTrack
public MainForm()
{
InitializeComponent();
InitializeComponent();
loadSave();
checkStatus();
update();
@ -47,80 +47,59 @@ namespace OWTrack
private void loadSave()
{
Directory.CreateDirectory("saves");
if (saveExist())
if (saveManeger.saveExist())
{
tr.wins = savedTracker().wins;
tr.losses = savedTracker().losses;
tr.newSR = savedTracker().newSR;
tr.startSR = savedTracker().startSR;
tr.gamePath = savedTracker().gamePath;
update();
}
}
private bool saveExist()
{
try
{
if (File.Exists(savesPath))
{
try
{
using (StreamReader st = new StreamReader(savesPath))
{
string line = st.ReadLine();
if (line.Contains("Overwatch.exe"))
{
st.Close();
return true;
tr = saveManeger.GetSavedTracker();
st.Close();
}
else
{
if (!tr.LoacteOW())
{
st.Close();
{
tr.gamePath = getGamePath();
}
return true;
st.Close();
}
}
}
else
{
if (!tr.LoacteOW())
{
tr.gamePath = getGamePath();
}
return false;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
return false;
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
update();
}
else if (!tr.LoacteOW())
{
tr.gamePath = getGamePath();
}
}
private string getGamePath()
{
openFileDialog1.Title = "Select Overwatch.exe";
openFileDialog1.DefaultExt = "exe";
openFileDialog1.Filter = "exe Files (*.exe)|*.exe|All files (*.*)|*.*";
//openFileDialog1.CheckFileExists = true;
//openFileDialog1.CheckPathExists = true;
openFileDialog1.Filter = "exe Files (*.exe)|*.exe|All files (*.*)|*.*";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
//tr.gamePath = openFileDialog1.FileName;
MessageBox.Show("Saved Overwatch.exe location.\nPress Clear to rest\n" + openFileDialog1.FileName );
return openFileDialog1.FileName;
}
else
{
update();
return null;
//Close();
}
//Show();
//update();
}
}
private Tracker savedTracker()

View File

@ -18,7 +18,7 @@ namespace OWTrack
Application.Run(new MainForm());
}
public static string Version { get; } = "1.4.0a1";
public static string Version { get; } = "1.4.0a2";
//public static string Version = Application.ProductVersion;
//public static Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

View File

@ -13,7 +13,7 @@ namespace OWTrack
public string gamePath;
public void Track() { }
public void reset() { wins = 0; losses = 0; startSR = 0; newSR = 0; }
public void reset() { wins = 0; losses = 0; startSR = 0; newSR = 0; gamePath = null; }
public void addWin() { wins++; }
public void addLoss() { losses++; }
public void reduceWin() { wins--; }

View File

@ -10,11 +10,13 @@ namespace OWTrack
{
class saveManeger
{
private static string savesPath = Directory.GetCurrentDirectory() + "/saves/data.json";
public static Tracker GetSavedTracker()
{
try
{
return JsonConvert.DeserializeObject<Tracker>(File.ReadAllText(Directory.GetCurrentDirectory() + "/data.json"));
return JsonConvert.DeserializeObject<Tracker>(File.ReadAllText(savesPath));
}
catch (Exception e)
{
@ -48,45 +50,23 @@ namespace OWTrack
}
}
//private bool saveExist()
//{
// try
// {
// if (File.Exists(Directory.GetCurrentDirectory() + "/data.json"))
// {
// using (StreamReader st = new StreamReader(Directory.GetCurrentDirectory() + "/data.json"))
// {
// string line = st.ReadLine();
// if (line.Contains("Overwatch.exe"))
// {
// st.Close();
// return true;
// }
// else
// {
// if (!tr.LoacteOW())
// {
// st.Close();
// getGamePath();
// }
// return true;
// }
// }
// }
// else
// {
// if (!tr.LoacteOW())
// {
// getGamePath();
// }
// return false;
// }
// }
// catch (Exception e)
// {
// MessageBox.Show(e.Message);
// return false;
// }
//}
public static bool saveExist()
{
try
{
if (File.Exists(savesPath))
{
return true;
}
else
{
return false;
}
}
catch (Exception e)
{
throw e;
}
}
}
}