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");

View File

@@ -19,9 +19,9 @@ namespace BLHX.Server.Game.Commands {
}
if (Unlock.Equals("all", StringComparison.CurrentCultureIgnoreCase)) {
int amount = 500; // not sure why but if you add more than this amount the client crashes
int amount = 585; // not sure why but if you add more than this amount the client crashes
List<int> all_ship_ids = Data.ShipDataTemplate.Where(x => x.Value.Star == x.Value.StarMax && x.Value.Star >= 5).ToDictionary().Keys.ToList();
List<PlayerShip> all_ships = all_ship_ids.Select(ship_id => CreateShipFromId((uint)ship_id, connection.player.Uid)).Take(amount).ToList();
List<PlayerShip> all_ships = all_ship_ids.Select(ship_id => CreateShipFromId((uint)ship_id, connection.player.Uid)).ToList();
all_ships.AddRange(GetDefaultShips(connection.player.Ships)); // add the defaults
connection.player.Ships = all_ships;

View File

@@ -2,16 +2,12 @@
using BLHX.Server.Common.Proto;
using BLHX.Server.Common.Proto.p11;
namespace BLHX.Server.Game.Handlers
{
internal static class P11
{
namespace BLHX.Server.Game.Handlers {
internal static class P11 {
[PacketHandler(Command.Cs11001)]
static void ServerTimeHandler(Connection connection, Packet packet)
{
static void ServerTimeHandler(Connection connection, Packet packet) {
connection.InitClientData();
connection.Send(new Sc11002()
{
connection.Send(new Sc11002() {
Timestamp = (uint)DateTimeOffset.Now.ToUnixTimeSeconds(),
Monday0oclockTimestamp = Connection.Monday0oclockTimestamp,
ShipCount = connection.player is null ? 0 : (uint)connection.player.Ships.Count
@@ -20,8 +16,7 @@ namespace BLHX.Server.Game.Handlers
}
[PacketHandler(Command.Cs11009, SaveDataAfterRun = true)]
static void ChangeManifestoHandler(Connection connection, Packet packet)
{
static void ChangeManifestoHandler(Connection connection, Packet packet) {
var req = packet.Decode<Cs11009>();
connection.player.Adv = req.Adv;
@@ -29,8 +24,7 @@ namespace BLHX.Server.Game.Handlers
}
[PacketHandler(Command.Cs11013, SaveDataAfterRun = true)]
static void HarvestResourceHandler(Connection connection, Packet packet)
{
static void HarvestResourceHandler(Connection connection, Packet packet) {
var req = packet.Decode<Cs11013>();
connection.player.HarvestResourceField((ResourceFieldType)req.Type);
@@ -38,55 +32,43 @@ namespace BLHX.Server.Game.Handlers
}
[PacketHandler(Command.Cs11601)]
static void GetEmojiInfoHandler(Connection connection, Packet packet)
{
static void GetEmojiInfoHandler(Connection connection, Packet packet) {
connection.Send(new Sc11602());
}
[PacketHandler(Command.Cs11603)]
static void FetchSecondaryPasswordHandler(Connection connection, Packet packet)
{
static void FetchSecondaryPasswordHandler(Connection connection, Packet packet) {
connection.Send(new Sc11604());
}
[PacketHandler(Command.Cs11017)]
static void StageDropListHandler(Connection connection, Packet packet)
{
static void StageDropListHandler(Connection connection, Packet packet) {
connection.Send(new Sc11018());
}
[PacketHandler(Command.Cs11401)]
static void ChangeChatRoomHandler(Connection connection, Packet packet)
{
static void ChangeChatRoomHandler(Connection connection, Packet packet) {
var req = packet.Decode<Cs11401>();
connection.Send(new Sc11402()
{
connection.Send(new Sc11402() {
Result = 0,
RoomId = req.RoomId
});
}
}
static class P11ConnectionNotifyExtensions
{
public static void NotifyResourceList(this Connection connection)
{
if (connection.player is not null)
{
connection.Send(new Sc11004()
{
static class P11ConnectionNotifyExtensions {
public static void NotifyResourceList(this Connection connection) {
if (connection.player is not null) {
connection.Send(new Sc11004() {
ResourceLists = connection.player.Resources.Select(x => new Resource() { Num = x.Num, Type = x.Id }).ToList()
});
}
}
public static void NotifyPlayerData(this Connection connection)
{
if (connection.player is not null)
{
connection.Send(new Sc11003()
{
public static void NotifyPlayerData(this Connection connection) {
if (connection.player is not null) {
connection.Send(new Sc11003() {
Id = connection.player.Uid,
Name = connection.player.Name,
Level = connection.player.Level,
@@ -94,7 +76,7 @@ namespace BLHX.Server.Game.Handlers
Adv = connection.player.Adv,
ResourceLists = connection.player.Resources.Select(x => new Resource() { Num = x.Num, Type = x.Id }).ToList(),
Characters = [1],
ShipBagMax = 150,
ShipBagMax = connection.player.ShipBagMax,
EquipBagMax = 350,
GmFlag = 1,
Rank = 1,
@@ -109,13 +91,11 @@ namespace BLHX.Server.Game.Handlers
}
}
public static void NotifyRefluxData(this Connection connection)
{
public static void NotifyRefluxData(this Connection connection) {
connection.Send(new Sc11752());
}
public static void NotifyActivityData(this Connection connection)
{
public static void NotifyActivityData(this Connection connection) {
connection.Send(new Sc11200());
}
}