167 lines
4.5 KiB
C#
167 lines
4.5 KiB
C#
using System;
|
|
using Avalonia;
|
|
using Splat;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
using CsvHelper.Configuration;
|
|
using CsvHelper;
|
|
using StarsAssistant.Model;
|
|
using System.Globalization;
|
|
using Avalonia.ReactiveUI;
|
|
|
|
|
|
namespace StarsAssistant;
|
|
|
|
sealed class Program
|
|
{
|
|
// Initialization code. Don't use any Avalonia, third-party APIs or any
|
|
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
|
|
// yet and stuff might break.
|
|
[STAThread]
|
|
public static void Main(string[] args)
|
|
{
|
|
ModeDetector.OverrideModeDetector(Splat.ModeDetection.Mode.Run);
|
|
Locator.CurrentMutable.Register(
|
|
() => new StarsDatabase("stars.sqlite"),
|
|
typeof(StarsDatabase)
|
|
);
|
|
|
|
__createTestData();
|
|
|
|
BuildAvaloniaApp()
|
|
.StartWithClassicDesktopLifetime(args);
|
|
}
|
|
|
|
// Avalonia configuration, don't remove; also used by visual designer.
|
|
public static AppBuilder BuildAvaloniaApp()
|
|
=> AppBuilder.Configure<App>()
|
|
.UsePlatformDetect()
|
|
.WithInterFont()
|
|
.LogToTrace()
|
|
.UseReactiveUI();
|
|
|
|
|
|
public static void __createTestData ()
|
|
{
|
|
using var db = Locator.Current.GetService<StarsDatabase>()!;
|
|
|
|
db.Database.EnsureDeleted();
|
|
db.Database.EnsureCreated();
|
|
|
|
// Note: This sample requires the database to be created before running.
|
|
// Console.WriteLine($"Database path: {db.DbPath}.");
|
|
|
|
Race r = new()
|
|
{
|
|
Name = "Atlantis",
|
|
PlayerRace = true,
|
|
ColonistsPerResource = 1000,
|
|
GrowthRatePercent = 19,
|
|
PRT = PRT.Other,
|
|
HasOBRM = true,
|
|
FactoryCost3 = false,
|
|
FactoryNumberPer10k = 8,
|
|
FactoryResCost = 8,
|
|
FactoryResPer10 = 15,
|
|
MineResCost = 3,
|
|
MineMineralsPer10 = 10,
|
|
MineNumberPer10k = 10
|
|
};
|
|
db.Add(r);
|
|
db.SaveChanges();
|
|
Race.Player = r;
|
|
|
|
var config = CsvConfiguration.FromAttributes<Planet>(CultureInfo.InvariantCulture);
|
|
config.Delimiter = "\t";
|
|
config.Escape = '\0';
|
|
config.MissingFieldFound = null;
|
|
|
|
using (var reader = new StreamReader("/home/torben/goingth/GOINGTH.p1", System.Text.Encoding.Latin1))
|
|
using (var csv = new CsvReader(reader, config))
|
|
{
|
|
List<CSV.Planet> records = csv.GetRecords<CSV.Planet>().ToList();
|
|
var planetByPlayer = from csvp in records
|
|
group csvp by csvp.Owner into byOwners
|
|
select byOwners;
|
|
|
|
foreach (var owner in planetByPlayer)
|
|
{
|
|
if (owner.Key != "Atlantis")
|
|
{
|
|
r = new()
|
|
{
|
|
Name = owner.Key
|
|
};
|
|
db.Add(r);
|
|
db.SaveChanges();
|
|
}
|
|
|
|
foreach (CSV.Planet csvp in owner)
|
|
{
|
|
Planet p = new Planet{ Name = csvp.Name };
|
|
csvp.UpdateDbPlanet(p);
|
|
db.Add(p);
|
|
}
|
|
db.SaveChanges();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
using Microsoft.EntityFrameworkCore;
|
|
using CsvHelper.Configuration;
|
|
using CsvHelper;
|
|
using StarsAssistant.model;
|
|
|
|
|
|
using var db = new StarsDatabase("stars.sqlite");
|
|
|
|
db.Database.EnsureDeleted();
|
|
db.Database.EnsureCreated();
|
|
|
|
// Note: This sample requires the database to be created before running.
|
|
Console.WriteLine($"Database path: {db.DbPath}.");
|
|
|
|
var config = CsvConfiguration.FromAttributes<Planet>(CultureInfo.InvariantCulture);
|
|
config.Delimiter = "\t";
|
|
config.Escape = '\0';
|
|
config.MissingFieldFound = null;
|
|
|
|
using (var reader = new StreamReader("/home/torben/goingth/GOINGTH.p1", Encoding.Latin1))
|
|
using (var csv = new CsvReader(reader, config))
|
|
{
|
|
List<Planet> records = csv.GetRecords<Planet>().ToList();
|
|
foreach (Planet p in records)
|
|
{
|
|
db.Add(p);
|
|
}
|
|
db.SaveChanges();
|
|
}
|
|
*/
|
|
|
|
/*
|
|
// Create
|
|
Console.WriteLine("Inserting a new blog");
|
|
db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
|
|
db.SaveChanges();
|
|
|
|
// Read
|
|
Console.WriteLine("Querying for a blog");
|
|
var blog = db.Blogs
|
|
.OrderBy(b => b.BlogId)
|
|
.First();
|
|
|
|
// Update
|
|
Console.WriteLine("Updating the blog and adding a post");
|
|
blog.Url = "https://devblogs.microsoft.com/dotnet";
|
|
blog.Posts.Add(
|
|
new Post { Title = "Hello World", Content = "I wrote an app using EF Core!" });
|
|
db.SaveChanges();
|
|
|
|
// Delete
|
|
// Console.WriteLine("Delete the blog");
|
|
// db.Remove(blog);
|
|
// db.SaveChanges();
|
|
*/ |