Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Item templates #102

Merged
merged 7 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions Fuyu.Backend.BSG/DTO/Services/ItemFactoryService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Fuyu.Backend.BSG.ItemTemplates;
using Fuyu.Backend.BSG.Models.Items;
using Fuyu.Backend.BSG.Models.Responses;
using Fuyu.Common.Hashing;
using Fuyu.Common.IO;
using Fuyu.Common.Serialization;

namespace Fuyu.Backend.BSG.DTO.Services
{
// TODO: split UPD factory and Item Factory
public static class ItemFactoryService
{
public static Dictionary<MongoId, ItemTemplate> ItemTemplates { get; private set; }

public static void Load()
{
var itemsText = Resx.GetText("eft", "database.client.items.json");
ItemTemplates = Json.Parse<ResponseBody<Dictionary<MongoId, ItemTemplate>>>(itemsText).data;
}

public static ItemInstance CreateItem(MongoId tpl, MongoId? id = null)
{
var template = ItemTemplates[tpl];
var itemId = id.GetValueOrDefault(new MongoId(true));
ItemUpdatable upd = CreateItemUpdatable(template);

var item = new ItemInstance
{
Id = itemId,
TemplateId = tpl,
Updatable = upd
};

return item;
}

public static ItemUpdatable CreateItemUpdatable(ItemTemplate template)
{
ItemUpdatable upd = null;
var updProperties = typeof(ItemUpdatable).GetProperties();

foreach (var updProperty in updProperties)
{
var component = CreateItemComponent(template, updProperty.PropertyType, false);
if (component != null)
{
if (upd == null)
{
upd = new ItemUpdatable();
}

updProperty.SetValue(upd, component);
}
}

return upd;
}

public static object CreateItemComponent(ItemTemplate template, Type componentType, bool createDefault)
{
var isItemComponentInterface = typeof(IItemComponent).IsAssignableFrom(componentType);
if (!isItemComponentInterface)
{
return null;
}

var bindingFlags = BindingFlags.Public | BindingFlags.Static;
var createComponentMethodName = nameof(IItemComponent.CreateComponent);
var createComponentMethod = componentType.GetMethod(createComponentMethodName, bindingFlags);
if (createComponentMethod == null)
{
return null;
}

var result = createComponentMethod.Invoke(null, [template.Props]);
if (result == null && createDefault)
{
result = Activator.CreateInstance(componentType);
}

return result;
}

public static ItemUpdatable CreateItemUpdatable(MongoId tpl)
{
return CreateItemUpdatable(ItemTemplates[tpl]);
}

public static object CreateItemComponent(MongoId tpl, Type componentType, bool createDefault)
{
return CreateItemComponent(ItemTemplates[tpl], componentType, createDefault);
}
}
}
9 changes: 9 additions & 0 deletions Fuyu.Backend.BSG/IItemComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Newtonsoft.Json.Linq;

namespace Fuyu.Backend.BSG
{
public interface IItemComponent
{
static abstract object CreateComponent(JObject templateProperties);
}
}
14 changes: 14 additions & 0 deletions Fuyu.Backend.BSG/ItemTemplates/AmmoBoxItemProperties.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Runtime.Serialization;

namespace Fuyu.Backend.BSG.ItemTemplates
{
[DataContract]
public class AmmoBoxItemProperties : StackableItemItemProperties
{
[DataMember(Name = "magAnimationIndex")]
public int magAnimationIndex;

[DataMember(Name = "StackSlots")]
public object[] StackSlots = [];
}
}
198 changes: 198 additions & 0 deletions Fuyu.Backend.BSG/ItemTemplates/AmmoItemProperties.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
using System.Runtime.Serialization;
using Fuyu.Backend.BSG.DTO.Common;
using Fuyu.Backend.BSG.Models.Common;

namespace Fuyu.Backend.BSG.ItemTemplates
{
[DataContract]
public class AmmoItemProperties : StackableItemItemProperties
{
[DataMember(Name = "ammoType")]
public string ammoType;

[DataMember(Name = "Damage")]
public int Damage;

[DataMember(Name = "ammoAccr")]
public int ammoAccr;

[DataMember(Name = "ammoRec")]
public int ammoRec;

[DataMember(Name = "ammoDist")]
public int ammoDist;

[DataMember(Name = "buckshotBullets")]
public int buckshotBullets;

[DataMember(Name = "PenetrationPower")]
public int PenetrationPower = 40;

[DataMember(Name = "PenetrationPowerDiviation")]
public float PenetrationPowerDiviation;

[DataMember(Name = "ammoHear")]
public int ammoHear;

[DataMember(Name = "ammoSfx")]
public string ammoSfx;

[DataMember(Name = "MisfireChance")]
public float MisfireChance;

[DataMember(Name = "MinFragmentsCount")]
public int MinFragmentsCount = 2;

[DataMember(Name = "MaxFragmentsCount")]
public int MaxFragmentsCount = 3;

[DataMember(Name = "ammoShiftChance")]
public int ammoShiftChance;

[DataMember(Name = "casingName")]
public string casingName;

[DataMember(Name = "casingEjectPower")]
public float casingEjectPower;

[DataMember(Name = "casingMass")]
public float casingMass;

[DataMember(Name = "casingSounds")]
public string casingSounds;

[DataMember(Name = "ProjectileCount")]
public int ProjectileCount = 1;

[DataMember(Name = "InitialSpeed")]
public float InitialSpeed = 700f;

[DataMember(Name = "PenetrationDamageMod")]
public float PenetrationDamageMod = 0.1f;

[DataMember(Name = "PenetrationChanceObstacle")]
public float PenetrationChanceObstacle = 0.2f;

[DataMember(Name = "RicochetChance")]
public float RicochetChance = 0.1f;

[DataMember(Name = "FragmentationChance")]
public float FragmentationChance = 0.03f;

[DataMember(Name = "BallisticCoeficient")]
public float BallisticCoeficient = 1f;

[DataMember(Name = "Tracer")]
public bool Tracer;

[DataMember(Name = "TracerColor")]
public ETaxonomyColor TracerColor;

[DataMember(Name = "TracerDistance")]
public float TracerDistance;

[DataMember(Name = "ArmorDamage")]
public int ArmorDamage;

[DataMember(Name = "Caliber")]
public string Caliber;

[DataMember(Name = "StaminaBurnPerDamage")]
public float StaminaBurnPerDamage;

[DataMember(Name = "HasGrenaderComponent")]
public bool HasGrenaderComponent;

[DataMember(Name = "FuzeArmTimeSec")]
public float FuzeArmTimeSec;

[DataMember(Name = "MinExplosionDistance")]
public float MinExplosionDistance;

[DataMember(Name = "MaxExplosionDistance")]
public float MaxExplosionDistance;

[DataMember(Name = "FragmentsCount")]
public int FragmentsCount;

[DataMember(Name = "FragmentType")]
public string FragmentType;

[DataMember(Name = "ExplosionType")]
public string ExplosionType;

[DataMember(Name = "ShowHitEffectOnExplode")]
public bool ShowHitEffectOnExplode;

[DataMember(Name = "ExplosionStrength")]
public float ExplosionStrength;

[DataMember(Name = "ShowBullet")]
public bool ShowBullet;

[DataMember(Name = "AmmoLifeTimeSec")]
public float AmmoLifeTimeSec = 2f;

[DataMember(Name = "MalfMisfireChance")]
public float MalfMisfireChance;

[DataMember(Name = "MalfFeedChance")]
public float MalfFeedChance;

[DataMember(Name = "ArmorDistanceDistanceDamage")]
public Vector3 ArmorDistanceDistanceDamage;

[DataMember(Name = "Contusion")]
public Vector3 Contusion;

[DataMember(Name = "Blindness")]
public Vector3 Blindness;

[DataMember(Name = "LightBleedingDelta")]
public float LightBleedingDelta;

[DataMember(Name = "HeavyBleedingDelta")]
public float HeavyBleedingDelta;

[DataMember(Name = "IsLightAndSoundShot")]
public bool IsLightAndSoundShot;

[DataMember(Name = "LightAndSoundShotAngle")]
public float LightAndSoundShotAngle;

[DataMember(Name = "LightAndSoundShotSelfContusionTime")]
public float LightAndSoundShotSelfContusionTime;

[DataMember(Name = "LightAndSoundShotSelfContusionStrength")]
public float LightAndSoundShotSelfContusionStrength;

[DataMember(Name = "DurabilityBurnModificator")]
public float DurabilityBurnModificator = 1f;

[DataMember(Name = "HeatFactor")]
public float HeatFactor = 1f;

[DataMember(Name = "BulletMassGram")]
public float BulletMassGram;

[DataMember(Name = "BulletDiameterMilimeters")]
public float BulletDiameterMilimeters;

[DataMember(Name = "RemoveShellAfterFire")]
public bool RemoveShellAfterFire;

[DataMember(Name = "airDropTemplateId")]
public string airDropTemplateId;

[DataMember(Name = "FlareTypes")]
public EFlareEventType[] FlareTypes;
}

public enum EFlareEventType
{
Airdrop,
ExitActivate,
AIFollowEvent,
CallArtilleryOnMyself
}
}
9 changes: 9 additions & 0 deletions Fuyu.Backend.BSG/ItemTemplates/ArmBandItemProperties.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Runtime.Serialization;

namespace Fuyu.Backend.BSG.ItemTemplates
{
[DataContract]
public class ArmBandItemProperties : EquipmentItemProperties
{
}
}
9 changes: 9 additions & 0 deletions Fuyu.Backend.BSG/ItemTemplates/ArmorItemProperties.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Runtime.Serialization;

namespace Fuyu.Backend.BSG.ItemTemplates
{
[DataContract]
public class ArmorItemProperties : ArmoredEquipmentItemProperties
{
}
}
16 changes: 16 additions & 0 deletions Fuyu.Backend.BSG/ItemTemplates/ArmorPlateItemProperties.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Runtime.Serialization;
using Fuyu.Backend.BSG.DTO.Common;
using Fuyu.Backend.BSG.Models.Common;

namespace Fuyu.Backend.BSG.ItemTemplates
{
[DataContract]
public class ArmorPlateItemProperties : ArmoredEquipmentItemProperties
{
[DataMember(Name = "Lower75Prefab")]
public ResourceKey Lower75Prefab;

[DataMember(Name = "Lower40Prefab")]
public ResourceKey Lower40Prefab;
}
}
Loading