save last record - JSON

This commit is contained in:
HeshamTB 2018-08-12 02:21:12 +03:00
parent 38bb3bd30d
commit bba991d3bf
2 changed files with 37 additions and 12 deletions

View File

@ -1,6 +1,8 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Windows.Forms; using System.Windows.Forms;
using Newtonsoft.Json;
using System.IO;
namespace OWTrack namespace OWTrack
{ {
@ -13,20 +15,17 @@ namespace OWTrack
public Form1() public Form1()
{ {
InitializeComponent(); InitializeComponent();
loadSave();
checkStatus(); checkStatus();
label4.Text = Program.Version; label4.Text = Program.Version;
Text = "OWTrack " + Program.Version; Text = "OWTrack " + Program.Version;
} }
private void timer1_Tick(object sender, EventArgs e)
{
checkStatus();
}
private void checkStatus() private void checkStatus()
{ {
try try
{ {
File.WriteAllText(Directory.GetCurrentDirectory() + "/data.json", JsonConvert.SerializeObject(tr));
Time.Text = DateTime.Now.ToString("h:mm tt"); Time.Text = DateTime.Now.ToString("h:mm tt");
if (tr.owRunning()) if (tr.owRunning())
{ {
@ -45,6 +44,33 @@ namespace OWTrack
} }
} }
private void loadSave()
{
if (saveExist())
{
tr.wins = savedTracker().wins;
tr.losses = savedTracker().losses;
update();
}
else MessageBox.Show("no save");
}
private bool saveExist()
{
if (File.Exists(Directory.GetCurrentDirectory() + "/data.json")) { return true; }
else return false;
}
private Tracker savedTracker()
{
return JsonConvert.DeserializeObject<Tracker>(File.ReadAllText(Directory.GetCurrentDirectory() + "/data.json"));
}
private void timer1_Tick(object sender, EventArgs e)
{
checkStatus();
}
private void button1_Click(object sender, EventArgs e) private void button1_Click(object sender, EventArgs e)
{ {
tr.addWin(); tr.addWin();

View File

@ -1,20 +1,19 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using Newtonsoft.Json; using System.IO;
namespace OWTrack namespace OWTrack
{ {
class Tracker class Tracker
{ {
private int wins, losses = 0; public int wins, losses = 0;
public void Track() public void Track()
{ {
} }
public bool owRunning() public bool owRunning()
{ {
try try