Move player to game engine.

This commit is contained in:
Torben Nehmer 2024-09-21 14:40:13 +02:00
parent 63761001fa
commit e01a50ec9c
No known key found for this signature in database
5 changed files with 30 additions and 21 deletions

View File

@ -99,23 +99,23 @@ public class Planet
public int EffectiveValue() => EffectiveValue(Value);
public static int MaxPopOnPlanet (int? value) {
return Race.Player.MaxPopOnPlanet * EffectiveValue(value) / 100;
return Services.Game.Player.MaxPopOnPlanet * EffectiveValue(value) / 100;
}
public int MaxPopOnPlanet() => MaxPopOnPlanet(Value);
public int PopGrowth(int currentPop, int maxPop, int value) {
// What to do with non player races?
if (OwnerId != Race.Player.Name) return 0;
if (OwnerId != Services.Game.Player.Name) return 0;
double popRatio = currentPop / maxPop;
int growth = 0;
if (value < 0)
growth = currentPop * value / 10;
else if (popRatio <= 0.25)
growth = currentPop * value * (Race.Player.GrowthRatePercent ?? 0) / 100;
growth = currentPop * value * (Services.Game.Player.GrowthRatePercent ?? 0) / 100;
else if (popRatio <= 1)
growth = currentPop * value * (Race.Player.GrowthRatePercent ?? 0) /100 * 16 / 9 * ((int) Math.Pow(1 - popRatio, 2));
growth = currentPop * value * (Services.Game.Player.GrowthRatePercent ?? 0) /100 * 16 / 9 * ((int) Math.Pow(1 - popRatio, 2));
else
growth = (int) (currentPop * (popRatio - 1) * 0.04);
@ -126,7 +126,7 @@ public class Planet
public int MaxFact(int currentPop) {
int effectivePop = Math.Min(currentPop, MaxPopOnPlanet());
double tmp = (double) effectivePop / 10000 * Race.Player.FactoryNumberPer10k ?? 0;
double tmp = (double) effectivePop / 10000 * Services.Game.Player.FactoryNumberPer10k ?? 0;
return (int) tmp;
}
@ -134,15 +134,15 @@ public class Planet
public int MaxMines(int currentPop) {
int effectivePop = Math.Min(currentPop, MaxPopOnPlanet());
double tmp = (double) effectivePop / 10000 * Race.Player.MineNumberPer10k ?? 0;
double tmp = (double) effectivePop / 10000 * Services.Game.Player.MineNumberPer10k ?? 0;
return (int) tmp;
}
public int MaxMines() => MaxMines(Population ?? 0);
public static int ResourcesFromPop(int currentPop, int value) {
if (Race.Player.ColonistsPerResource == null
|| Race.Player.ColonistsPerResource == 0)
if (Services.Game.Player.ColonistsPerResource == null
|| Services.Game.Player.ColonistsPerResource == 0)
{
return 0;
}
@ -162,7 +162,7 @@ public class Planet
popHalfEfficiency = 2 * maxPop;
}
int popEffective = popFullEfficiency + (popHalfEfficiency / 2);
return (popEffective / Race.Player.ColonistsPerResource) ?? 0;
return (popEffective / Services.Game.Player.ColonistsPerResource) ?? 0;
}
public int ResourcesFromPop() => ResourcesFromPop(Population ?? 0, Value ?? 0);
@ -170,10 +170,10 @@ public class Planet
public static int ResourcesFromFact(int currentPop, int factories, int value) {
int maxPop = MaxPopOnPlanet(value);
int effectivePop = Math.Min(currentPop, maxPop);
double tmp = (double) effectivePop / 10000 * Race.Player.FactoryNumberPer10k ?? 0;
double tmp = (double) effectivePop / 10000 * Services.Game.Player.FactoryNumberPer10k ?? 0;
int maxOperableFactories = (int) tmp;
int operableFactories = Math.Min(factories, maxOperableFactories);
tmp = (double) operableFactories / 10 * Race.Player.FactoryResPer10 ?? 0;
tmp = (double) operableFactories / 10 * Services.Game.Player.FactoryResPer10 ?? 0;
return (int) tmp;
}

View File

@ -85,13 +85,6 @@ public class Race
public ICollection<Planet> Planets { get; } = new List<Planet>();
// TODO: Implement Lazy Loader, currently hardcoded in testing startup code
public static Race _player = null!;
public static Race Player {
get => _player;
set => _player = value;
}
#endregion
#region Public Helpers

View File

@ -95,10 +95,9 @@ sealed class Program
};
db.Add(r);
db.SaveChanges();
Race.Player = r;
var loader = new CSV.PlanetLoader();
loader.ImportForRace(Race.Player);
loader.ImportForRace(Services.Game.Player);
}
}

View File

@ -53,6 +53,6 @@ public partial class CSVDataLoader : IEnableLogger
this.Log().Debug($"Got file type {type} for player {player}");
var loader = new CSV.PlanetLoader();
loader.ImportForRace(Model.Race.Player);
loader.ImportForRace(Services.Game.Player);
}
}

View File

@ -1,3 +1,4 @@
using System.Reflection.PortableExecutable;
using Splat;
namespace StarsAssistant.Services;
@ -64,6 +65,22 @@ public class Game
/// </summary>
public string PlanetFileSearchPattern => $"{BaseName}.p*";
/// <summary>
/// Internal helper to lazily load the current player from the DB.
/// </summary>
private readonly static Lazy<Model.Race>LazyPlayer = new ( () => {
using var db = Locator.Current.GetService<Model.StarsDatabase>()!;
Model.Race result = db.Race
.Where(r => r.PlayerRace == true)
.First();
return result;
});
/// <summary>
/// Get the Race object for the current player, lazy initialized.
/// </summary>
public static Model.Race Player => LazyPlayer.Value;
/// <summary>
/// Get the name of a planet file for a given race.
/// </summary>