Events added for ploegen.cs (reset json files for next boot)

This commit is contained in:
Beppe
2019-08-03 21:29:23 +02:00
parent b3a4a7441a
commit 1fb0e5801b
5 changed files with 68 additions and 16 deletions

View File

@@ -21,7 +21,7 @@ namespace Data
List<Bet> list = new List<Bet>(); List<Bet> list = new List<Bet>();
try try
{ {
using (StreamReader file = File.OpenText(@"Data/bets.json")) using (StreamReader file = File.OpenText(@"../../Data/bets.json"))
{ {
JsonSerializer serializer = new JsonSerializer(); JsonSerializer serializer = new JsonSerializer();
@@ -42,7 +42,7 @@ namespace Data
try try
{ {
using (StreamReader file = File.OpenText(@"Data/games.json")) using (StreamReader file = File.OpenText(@"../../Data/games.json"))
{ {
JsonSerializer serializer = new JsonSerializer(); JsonSerializer serializer = new JsonSerializer();
list = (List<Game>)serializer.Deserialize(file, typeof(List<Game>)); list = (List<Game>)serializer.Deserialize(file, typeof(List<Game>));
@@ -61,7 +61,7 @@ namespace Data
List<Person> list = new List<Person>(); List<Person> list = new List<Person>();
try { try {
using (StreamReader file = File.OpenText(@"Data/persons.json")) using (StreamReader file = File.OpenText(@"../../Data/persons.json"))
{ {
JsonSerializer serializer = new JsonSerializer(); JsonSerializer serializer = new JsonSerializer();
list = (List<Person>)serializer.Deserialize(file, typeof(List<Person>)); list = (List<Person>)serializer.Deserialize(file, typeof(List<Person>));
@@ -80,7 +80,7 @@ namespace Data
List<Ploeg> list = new List<Ploeg>(); List<Ploeg> list = new List<Ploeg>();
try try
{ {
using (StreamReader file = File.OpenText(@"Data/ploegen.json")) using (StreamReader file = File.OpenText(@"../../Data/ploegen.json"))
{ {
JsonSerializer serializer = new JsonSerializer(); JsonSerializer serializer = new JsonSerializer();
list = (List<Ploeg>)serializer.Deserialize(file, typeof(List<Ploeg>)); list = (List<Ploeg>)serializer.Deserialize(file, typeof(List<Ploeg>));
@@ -96,7 +96,7 @@ namespace Data
public void saveBets(List<Bet> b) public void saveBets(List<Bet> b)
{ {
using (StreamWriter file = File.CreateText(@"Data/bets.json")) using (StreamWriter file = File.CreateText(@"../../Data/bets.json"))
{ {
JsonSerializer serializer = new JsonSerializer(); JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, b); serializer.Serialize(file, b);
@@ -105,7 +105,7 @@ namespace Data
public void saveGames(List<Game> g) public void saveGames(List<Game> g)
{ {
using (StreamWriter file = File.CreateText(@"Data/games.json")) using (StreamWriter file = File.CreateText(@"../../Data/games.json"))
{ {
JsonSerializer serializer = new JsonSerializer(); JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, g); serializer.Serialize(file, g);
@@ -114,7 +114,7 @@ namespace Data
public void savePersons(List<Person> p) public void savePersons(List<Person> p)
{ {
using (StreamWriter file = File.CreateText(@"Data/persons.json")) using (StreamWriter file = File.CreateText(@"../../Data/persons.json"))
{ {
JsonSerializer serializer = new JsonSerializer(); JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, p); serializer.Serialize(file, p);
@@ -123,7 +123,7 @@ namespace Data
public void savePloegen(List<Ploeg> p) public void savePloegen(List<Ploeg> p)
{ {
using (StreamWriter file = File.CreateText(@"Data/ploegen.json")) using (StreamWriter file = File.CreateText(@"../../Data/ploegen.json"))
{ {
JsonSerializer serializer = new JsonSerializer(); JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, p); serializer.Serialize(file, p);

View File

@@ -11,38 +11,62 @@ namespace Globals.classes
[JsonConstructor] [JsonConstructor]
public Game(int Id, Ploeg home, Ploeg away, DateTime date) public Game(int Id, Ploeg home, Ploeg away, DateTime date)
{ {
homeSetBool = false;
awaySetBool = false;
this.Id = Id; this.Id = Id;
this.home = home; this.home = home;
this.away = away; this.away = away;
this.home.scoreChanged += homeSet;
this.away.scoreChanged += awaySet;
this.date = date; this.date = date;
} }
public Game(Ploeg home, Ploeg away, DateTime d) public Game(Ploeg home, Ploeg away, DateTime d)
{ {
homeSetBool = false;
awaySetBool = false;
this.home = home; this.home = home;
this.away = away; this.away = away;
this.home.scoreChanged += homeSet;
this.away.scoreChanged += awaySet;
this.date = d; this.date = d;
} }
public Game(Ploeg home, Ploeg away, DateTime d, int scoreHome, int scoreAway) public Game(Ploeg home, Ploeg away, DateTime d, int scoreHome, int scoreAway)
{ {
homeSetBool = false;
awaySetBool = false;
this.home = home; this.home = home;
this.away = away; this.away = away;
this.home.score = scoreHome; this.home.scoreChanged += homeSet;
this.away.score = scoreAway; this.away.scoreChanged += awaySet;
this.home.setScore(scoreHome);
this.away.setScore(scoreAway);
this.date = d; this.date = d;
} }
public Game(int id, Ploeg home, Ploeg away) public Game(int id, Ploeg home, Ploeg away)
{ {
homeSetBool = false;
awaySetBool = false;
Id = id; Id = id;
this.home = home ?? throw new ArgumentNullException(nameof(home)); this.home = home ?? throw new ArgumentNullException(nameof(home));
this.away = away ?? throw new ArgumentNullException(nameof(away)); this.away = away ?? throw new ArgumentNullException(nameof(away));
this.home.scoreChanged += homeSet;
this.away.scoreChanged += awaySet;
} }
public Game(int id, Ploeg home, Ploeg away, int scoreHome, int scoreAway) public Game(int id, Ploeg home, Ploeg away, int scoreHome, int scoreAway)
{ {
homeSetBool = false;
awaySetBool = false;
Id = id; Id = id;
this.home = home ?? throw new ArgumentNullException(nameof(home)); this.home = home ?? throw new ArgumentNullException(nameof(home));
this.away = away ?? throw new ArgumentNullException(nameof(away)); this.away = away ?? throw new ArgumentNullException(nameof(away));
this.home.score = scoreHome; this.home.scoreChanged += homeSet;
this.away.score = scoreAway; this.away.scoreChanged += awaySet;
this.home.setScore(scoreHome);
this.away.setScore(scoreAway);
} }
[JsonProperty("id")] [JsonProperty("id")]
public int Id { get; set; } public int Id { get; set; }
@@ -53,6 +77,18 @@ namespace Globals.classes
[JsonProperty("Date")] [JsonProperty("Date")]
public DateTime date { get; set; } //y m d public DateTime date { get; set; } //y m d
private bool homeSetBool;
private bool awaySetBool;
private void homeSet (object s, EventArgs e)
{
homeSetBool = true;
}
private void awaySet(object s, EventArgs e)
{
awaySetBool = true;
}
public state getWinner() public state getWinner()
{ {
if (!this.home.scoreSet || !this.away.scoreSet) if (!this.home.scoreSet || !this.away.scoreSet)

View File

@@ -34,9 +34,11 @@ namespace Globals.classes
public int Id { get; set; } public int Id { get; set; }
public string naam { get; set; } public string naam { get; set; }
public int score { get; set; } public int score { get; private set; }
public bool scoreSet { get => this.score != int.MinValue; } public bool scoreSet { get; }
public event EventHandler scoreChanged;
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
try try
@@ -66,5 +68,15 @@ namespace Globals.classes
return this.Id + ": " + this.naam + score; return this.Id + ": " + this.naam + score;
} }
public void setScore(int i)
{
EventHandler h = scoreChanged;
if (null != h) h(this, EventArgs.Empty);
this.score = i;
}
public int getScore()
{
return this.score;
}
} }
} }

View File

@@ -11,5 +11,6 @@ namespace Globals.Interfaces
Ploeg away { get; set; } Ploeg away { get; set; }
DateTime date { get; set; } DateTime date { get; set; }
state getWinner(); state getWinner();
} }
} }

View File

@@ -5,7 +5,10 @@ namespace Globals.Interfaces
{ {
int Id { get; set; } int Id { get; set; }
string naam { get; set; } string naam { get; set; }
int score { get; set; } int score { get; }
bool scoreSet { get; } bool scoreSet { get; }
event EventHandler scoreChanged;
void setScore(int i);
int getScore();
} }
} }