Dunkleosteus - UlozeniVysledku.cs
C# .NET
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.IO;
using System.Windows;
using Microsoft.Win32;
namespace Dunkleosteus
{
public class UlozeniVysledku
{
public int Skore { get; private set; } // Bodové skóre
public int Pocitadlo { get; private set; } // Počítadlo řešitelů
public string Cesta { get; private set; } // Cesta k souboru
public string NovyZapis { get; private set; } // Nový zápis do žebříčku
public string Retezec { get; private set; } // Výpis řešitelů
public List<Hrac> SeznamHracu { get; private set; } // List na řešitele
public UlozeniVysledku()
{
SeznamHracu = new List<Hrac>();
Skore = 0;
NovyZapis = "";
Cesta = "vysledky.xml";
Nacti();
}
// Přidání nového zápisu
public void Pridej(string jmeno, int skore, int vzdalenost, int uroven)
{
Hrac novyHrac = new Hrac(jmeno, skore, vzdalenost, uroven);
SeznamHracu.Add(novyHrac);
Uloz();
}
public void Seradit()
{
SeznamHracu = SeznamHracu.OrderBy(o => o.Skore).Reverse().ToList();
}
// Uložíme dokument
public void Uloz()
{
XmlSerializer serializer = new XmlSerializer(SeznamHracu.GetType());
using (StreamWriter sw = new StreamWriter(Cesta))
{
serializer.Serialize(sw, SeznamHracu);
}
}
// Načteme dokument
public void Nacti()
{
XmlSerializer serializer = new XmlSerializer(SeznamHracu.GetType());
if (File.Exists(Cesta))
{
using (StreamReader sr = new StreamReader(Cesta))
{
// Když jsme něco načetli, aktualizujeme SeznamHracu
SeznamHracu = (List<Hrac>)serializer.Deserialize(sr);
}
}
else
{
// Když jsme nic nenačetli, vytvoříme jej jako prázdný
SeznamHracu = new List<Hrac>();
}
}
}
}
Neformátovaný
Přidáno: 4.8.2021
Expirace: Neuvedeno