attempt to add ShipBagMax

This commit is contained in:
raphaeIl
2024-04-03 01:46:39 -04:00
parent 2d7f83bafc
commit cea1a22103
4 changed files with 77 additions and 145 deletions

View File

@@ -6,10 +6,8 @@ using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace BLHX.Server.Common.Database
{
public sealed class PlayerContext : DbContext, IBLHXDBContext<PlayerContext>
{
namespace BLHX.Server.Common.Database {
public sealed class PlayerContext : DbContext, IBLHXDBContext<PlayerContext> {
SavingState savingState;
public static string DbPath => "Databases/players.db";
public DbSet<Player> Players { get; set; }
@@ -18,8 +16,7 @@ namespace BLHX.Server.Common.Database
public DbSet<PlayerShip> Ships { get; set; }
public DbSet<ChapterInfo> ChapterInfoes { get; set; }
public PlayerContext()
{
public PlayerContext() {
if (Database.GetPendingMigrations().Any())
Database.Migrate();
@@ -29,13 +26,11 @@ namespace BLHX.Server.Common.Database
}
// Thread-safe method pls
public void Save()
{
if (savingState == SavingState.Attempting)
public void Save() {
if (savingState == SavingState.Attempting)
return;
while (savingState != SavingState.None)
{
while (savingState != SavingState.None) {
savingState = SavingState.Attempting;
Task.Delay(1).Wait();
}
@@ -43,8 +38,7 @@ namespace BLHX.Server.Common.Database
SaveChanges();
}
public Player Init(string token, uint shipId, string name)
{
public Player Init(string token, uint shipId, string name) {
var player = new Player(token, new Displayinfo() { Icon = shipId }, name);
Players.Add(player);
@@ -66,8 +60,7 @@ namespace BLHX.Server.Common.Database
return player;
}
public void PlayerRoutine(Player player)
{
public void PlayerRoutine(Player player) {
if (!ResourceFields.Any(x => x.Type == ResourceFieldType.Gold))
ResourceFields.Add(new() { Type = ResourceFieldType.Gold, PlayerUid = player.Uid });
if (!ResourceFields.Any(x => x.Type == ResourceFieldType.Oil))
@@ -76,11 +69,9 @@ namespace BLHX.Server.Common.Database
Save();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
protected override void OnModelCreating(ModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Player>(e =>
{
modelBuilder.Entity<Player>(e => {
e.Property(b => b.DisplayInfo)
.HasJsonConversion();
e.Property(b => b.Fleets)
@@ -109,8 +100,7 @@ namespace BLHX.Server.Common.Database
.HasForeignKey(e => e.PlayerUid)
.IsRequired();
});
modelBuilder.Entity<PlayerShip>(e =>
{
modelBuilder.Entity<PlayerShip>(e => {
e.Property(b => b.Id)
.ValueGeneratedOnAdd();
e.Property(b => b.State)
@@ -128,8 +118,7 @@ namespace BLHX.Server.Common.Database
e.Property(b => b.CoreLists)
.HasJsonConversion();
});
modelBuilder.Entity<ChapterInfo>(e =>
{
modelBuilder.Entity<ChapterInfo>(e => {
e.Property(x => x.EscortLists)
.HasJsonConversion();
e.Property(x => x.AiLists)
@@ -163,14 +152,14 @@ namespace BLHX.Server.Common.Database
[PrimaryKey(nameof(Uid))]
[Index(nameof(Token), IsUnique = true)]
public class Player
{
public class Player {
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public uint Uid { get; set; }
public string Token { get; set; }
public string Name { get; set; }
// Aka. manifesto
public string Adv { get; set; } = string.Empty;
public uint ShipBagMax { get; set; } = 9990;
public uint Level { get; set; }
// TODO: Exp add setter to recalculate cap and set level
public uint Exp { get; set; }
@@ -186,8 +175,7 @@ namespace BLHX.Server.Common.Database
public virtual ICollection<PlayerShip> Ships { get; set; } = [];
public virtual ICollection<ChapterInfo> ChapterInfoes { get; set; } = [];
public Player(string token, Displayinfo displayInfo, string name)
{
public Player(string token, Displayinfo displayInfo, string name) {
DisplayInfo = displayInfo;
Token = token;
Name = name;
@@ -195,11 +183,9 @@ namespace BLHX.Server.Common.Database
CreatedAt = DateTime.Now;
}
public void DoResource(uint id, int num)
{
public void DoResource(uint id, int num) {
var res = Resources.SingleOrDefault(x => x.Id == id);
if (res is null)
{
if (res is null) {
res = new() { Id = id, PlayerUid = Uid };
DBManager.PlayerContext.Resources.Add(res);
}
@@ -210,13 +196,11 @@ namespace BLHX.Server.Common.Database
res.Num = Math.Min(res.Num + (uint)num, uint.MaxValue);
}
public void AddShip(uint shipTemplateId)
{
public void AddShip(uint shipTemplateId) {
if (!Data.Data.ShipDataTemplate.TryGetValue((int)shipTemplateId, out var shipTemplate))
throw new InvalidDataException($"Ship template {shipTemplateId} not found!");
var ship = new PlayerShip()
{
var ship = new PlayerShip() {
TemplateId = shipTemplateId,
Level = 1,
EquipInfoLists = [
@@ -238,15 +222,11 @@ namespace BLHX.Server.Common.Database
DBManager.PlayerContext.Ships.Add(ship);
}
public void HarvestResourceField(ResourceFieldType type)
{
foreach (var resourceField in ResourceFields)
{
if (resourceField.Type == type)
{
public void HarvestResourceField(ResourceFieldType type) {
foreach (var resourceField in ResourceFields) {
if (resourceField.Type == type) {
var amount = resourceField.Flush();
switch (type)
{
switch (type) {
case ResourceFieldType.Gold:
DoResource(1, (int)amount);
break;
@@ -262,8 +242,7 @@ namespace BLHX.Server.Common.Database
}
[PrimaryKey(nameof(Id), nameof(PlayerUid))]
public class PlayerResource
{
public class PlayerResource {
[Key]
public uint Id { get; set; }
public uint Num { get; set; }
@@ -274,8 +253,7 @@ namespace BLHX.Server.Common.Database
}
[PrimaryKey(nameof(Id))]
public class PlayerShip
{
public class PlayerShip {
[Key]
public uint Id { get; set; }
public uint TemplateId { get; set; }
@@ -307,10 +285,8 @@ namespace BLHX.Server.Common.Database
public DateTime CreatedAt { get; set; } = DateTime.Now;
public DateTime? LastChangeName { get; set; }
public Shipinfo ToProto()
{
return new()
{
public Shipinfo ToProto() {
return new() {
Id = Id,
TemplateId = TemplateId,
Level = Level,
@@ -340,15 +316,13 @@ namespace BLHX.Server.Common.Database
}
}
public enum ResourceFieldType
{
public enum ResourceFieldType {
Gold = 1,
Oil = 2
}
[PrimaryKey(nameof(Type), nameof(PlayerUid))]
public class ResourceField
{
public class ResourceField {
[Key]
public ResourceFieldType Type { get; set; }
public uint Level { get; set; } = 1;
@@ -359,14 +333,11 @@ namespace BLHX.Server.Common.Database
public uint PlayerUid { get; set; }
public virtual Player Player { get; set; } = null!;
public void CalculateYield()
{
public void CalculateYield() {
// TODO: Take UpgradeTime into acccount of the reward
switch (Type)
{
switch (Type) {
case ResourceFieldType.Gold:
if (Data.Data.GoldFieldTemplate.TryGetValue((int)Level, out var goldTemplate))
{
if (Data.Data.GoldFieldTemplate.TryGetValue((int)Level, out var goldTemplate)) {
var res = Player.Resources.FirstOrDefault(x => x.Id == 7);
var num = (goldTemplate.HourTime * goldTemplate.Production) / 3600f * LastHarvestTime.GetSecondsPassed();
@@ -375,8 +346,7 @@ namespace BLHX.Server.Common.Database
}
break;
case ResourceFieldType.Oil:
if (Data.Data.OilFieldTemplate.TryGetValue((int)Level, out var oilTemplate))
{
if (Data.Data.OilFieldTemplate.TryGetValue((int)Level, out var oilTemplate)) {
var res = Player.Resources.FirstOrDefault(x => x.Id == 5);
var num = (oilTemplate.HourTime * oilTemplate.Production) / 3600f * LastHarvestTime.GetSecondsPassed();
@@ -387,15 +357,12 @@ namespace BLHX.Server.Common.Database
}
}
public uint Flush()
{
public uint Flush() {
uint amount = 0;
// TODO: Take UpgradeTime into acccount of the reward
switch (Type)
{
switch (Type) {
case ResourceFieldType.Gold:
if (Data.Data.GoldFieldTemplate.TryGetValue((int)Level, out var goldTemplate))
{
if (Data.Data.GoldFieldTemplate.TryGetValue((int)Level, out var goldTemplate)) {
var goldField = Player.Resources.First(x => x.Id == 7);
amount = goldField.Num;
@@ -403,8 +370,7 @@ namespace BLHX.Server.Common.Database
}
break;
case ResourceFieldType.Oil:
if (Data.Data.OilFieldTemplate.TryGetValue((int)Level, out var oilTemplate))
{
if (Data.Data.OilFieldTemplate.TryGetValue((int)Level, out var oilTemplate)) {
var oilField = Player.Resources.First(x => x.Id == 5);
amount = oilField.Num;
@@ -419,8 +385,7 @@ namespace BLHX.Server.Common.Database
}
[PrimaryKey(nameof(Id), nameof(PlayerUid))]
public class ChapterInfo
{
public class ChapterInfo {
[Key]
public uint Id { get; set; }
public DateTime Time { get; set; }
@@ -449,17 +414,15 @@ namespace BLHX.Server.Common.Database
public uint PlayerUid { get; set; }
public virtual Player Player { get; set; } = null!;
public Currentchapterinfo ToProto()
{
return new Currentchapterinfo()
{
public Currentchapterinfo ToProto() {
return new Currentchapterinfo() {
Id = Id,
AiLists = AiLists,
BattleStatistics = BattleStatistics,
BuffLists = BuffLists,
CellFlagLists = CellFlagLists,
CellLists = CellLists,
ChapterHp = ChapterHp,
ChapterHp = ChapterHp,
ChapterStrategyLists = ChapterStrategyLists,
ContinuousKillCount = ContinuousKillCount,
EscortLists = EscortLists,
@@ -478,10 +441,8 @@ namespace BLHX.Server.Common.Database
};
}
public static ChapterInfo FromProto(Currentchapterinfo chapterInfo, uint uid)
{
return new()
{
public static ChapterInfo FromProto(Currentchapterinfo chapterInfo, uint uid) {
return new() {
Id = chapterInfo.Id,
AiLists = chapterInfo.AiLists,
BattleStatistics = chapterInfo.BattleStatistics,

View File

@@ -3,42 +3,36 @@ using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace BLHX.Server.Common.Migrations.Player
{
namespace BLHX.Server.Common.Migrations.Player {
/// <inheritdoc />
public partial class Init_PlayerContext : Migration
{
public partial class Init_PlayerContext : Migration {
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
protected override void Up(MigrationBuilder migrationBuilder) {
migrationBuilder.CreateTable(
name: "Players",
columns: table => new
{
columns: table => new {
Uid = table.Column<uint>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Token = table.Column<string>(type: "TEXT", nullable: false),
Name = table.Column<string>(type: "TEXT", nullable: false),
Level = table.Column<uint>(type: "INTEGER", nullable: false),
Exp = table.Column<uint>(type: "INTEGER", nullable: false),
ShipBagMax = table.Column<uint>(type: "INTEGER", nullable: false),
DisplayInfo = table.Column<string>(type: "jsonb", nullable: false),
CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
{
constraints: table => {
table.PrimaryKey("PK_Players", x => x.Uid);
});
migrationBuilder.CreateTable(
name: "Resources",
columns: table => new
{
columns: table => new {
Id = table.Column<uint>(type: "INTEGER", nullable: false),
PlayerUid = table.Column<uint>(type: "INTEGER", nullable: false),
Num = table.Column<uint>(type: "INTEGER", nullable: false)
},
constraints: table =>
{
constraints: table => {
table.PrimaryKey("PK_Resources", x => new { x.Id, x.PlayerUid });
table.ForeignKey(
name: "FK_Resources_Players_PlayerUid",
@@ -50,8 +44,7 @@ namespace BLHX.Server.Common.Migrations.Player
migrationBuilder.CreateTable(
name: "Ships",
columns: table => new
{
columns: table => new {
Id = table.Column<uint>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
TemplateId = table.Column<uint>(type: "INTEGER", nullable: false),
@@ -79,8 +72,7 @@ namespace BLHX.Server.Common.Migrations.Player
CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false),
LastChangeName = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
{
constraints: table => {
table.PrimaryKey("PK_Ships", x => x.Id);
table.ForeignKey(
name: "FK_Ships_Players_PlayerUid",
@@ -108,8 +100,7 @@ namespace BLHX.Server.Common.Migrations.Player
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
protected override void Down(MigrationBuilder migrationBuilder) {
migrationBuilder.DropTable(
name: "Resources");