Skip to content

Commit

Permalink
make it possible to set a cooldown on item collection from skills. Th…
Browse files Browse the repository at this point in the history
…is acts as a minimal interval between gains
  • Loading branch information
zerratar committed Aug 1, 2023
1 parent 39196ea commit 1ea186e
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 33 deletions.
34 changes: 23 additions & 11 deletions GameDataSimulation/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,32 @@
using GMath = RavenNest.BusinessLogic.GameMath;

var villages = new List<UserVillage>();
for (var j = 0; j < 7; j++)
//for (var j = 0; j < 7; j++)
//{
// var level = j == 0 ? 1 : (j * 50) - 1;
// for (var i = 0; i < 4; i++)
// {
// var population = (i % 4) * 10;//100;
// villages.Add(new UserVillage
// {
// Level = level,
// Name = "Village " + j + "#" + i,
// Population = population
// });
// }
//}

for (var i = 0; i < 15; i++)
{
var level = j == 0 ? 1 : (j * 50) - 1;
for (var i = 0; i < 4; i++)
var population = i * 10;//100;
villages.Add(new UserVillage
{
var population = (i % 4) * 10;//100;
villages.Add(new UserVillage
{
Level = level,
Name = "Village " + j + "#" + i,
Population = population
});
}
Level = 1,
Name = "Village " + 1 + "#" + i,
Population = population
});
}

//var villages = new UserVillage[]
//{
// new UserVillage { Level = 10, Population = 25, Name = "Someone" },
Expand Down
4 changes: 2 additions & 2 deletions GameDataSimulation/Simulations/MiningDropSimulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private void UpdateResourceGain(ResourceContext ctx, Action onUpdate)
}
}

private void AddDrop(int miningLevel, Action<ResourceDrop> onDrop)
private void AddDrop(int skillLevel, Action<ResourceDrop> onDrop)
{
var multiDrop = Random.NextDouble();
var isMultiDrop = multiDrop <= 0.1;
Expand All @@ -135,7 +135,7 @@ private void AddDrop(int miningLevel, Action<ResourceDrop> onDrop)
foreach (var res in ResourceTaskProcessor.DefaultDroppableResources.OrderByDescending(x => x.SkillLevel))
{
chance = Random.NextDouble();
if (miningLevel >= res.SkillLevel && (chance <= res.GetDropChance(miningLevel)))
if (skillLevel >= res.SkillLevel && (chance <= res.GetDropChance(skillLevel)))
{
onDrop(res);

Expand Down
20 changes: 19 additions & 1 deletion src/RavenNest.Blazor/Pages/Admin/ItemDropManagement.razor
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
<InputText class="modal-input" @bind-Value="dataModel.DropChance" />
</div>

<div class="form-row">
<label>Cooldown</label>
<InputText class="modal-input" @bind-Value="dataModel.Cooldown" />
</div>

<div class="form-row">
<label>Required Skill Level</label>
<InputNumber class="modal-input" @bind-Value="dataModel.SkillLevel" />
Expand Down Expand Up @@ -118,6 +123,7 @@
<th>Skill</th>
<th>Level Req</th>
<th>Drop Chance</th>
<th>Cooldown</th>
<th></th>
</tr>
</thead>
Expand All @@ -130,6 +136,7 @@
<td class='item'>@(item.Skill != null ? RavenNest.DataModels.Skills.SkillNames[item.Skill.Value] : "")</td>
<td class='item'>@item.LevelRequirement</td>
<td class='item'>@item.DropChance</td>
<td class='item'>@item.Cooldown</td>
<td class='item'>
<button class="link-button" @onclick="() => ShowConfirmDelete(item)"><i class="fa-solid fa-trash-can"></i></button>
<button class="link-button" @onclick="() => ShowModal(item)"><i class="fas fa-pencil-alt"></i></button>
Expand Down Expand Up @@ -269,6 +276,7 @@
if (d != null)
{
DropChance = d.DropChance.ToString();
Cooldown = d.Cooldown.GetValueOrDefault().ToString();
ItemName = d.ItemName;
ItemId = d.ItemId.ToString();

Expand All @@ -282,6 +290,7 @@
return;
}

Cooldown = 0.ToString();
DropChance = 0.01.ToString();
SkillLevel = 1;
}
Expand All @@ -293,6 +302,7 @@
Drop.ItemId = toCopy.ItemId;
Drop.ItemName = toCopy.ItemName;
Drop.DropChance = toCopy.DropChance;
Drop.Cooldown = toCopy.Cooldown;
Drop.LevelRequirement = toCopy.LevelRequirement;
Drop.Skill = toCopy.Skill;
return true;
Expand Down Expand Up @@ -340,6 +350,12 @@
double.TryParse(DropChance, out dropChance);
}

var cooldown = 0.0;
if (!string.IsNullOrEmpty(Cooldown))
{
double.TryParse(Cooldown, out cooldown);
}

if (TargetItem != null)
{
return new RavenNest.DataModels.ResourceItemDrop
Expand All @@ -349,7 +365,8 @@
ItemName = TargetItem.Name,
DropChance = dropChance,
LevelRequirement = SkillLevel,
Skill = index
Skill = index,
Cooldown = cooldown
};
}

Expand All @@ -362,6 +379,7 @@
public string ItemId { get; set; }
public string Skill { get; set; }
public string DropChance { get; set; }
public string Cooldown { get; set; }
public int SkillLevel { get; set; }
}
}
4 changes: 4 additions & 0 deletions src/RavenNest.BusinessLogic/Data/GameData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public class GameData : IDisposable

private readonly EntitySet<VendorItem> vendorItems;

private readonly EntitySet<Achievement> achievements;
private readonly EntitySet<UserAchievement> userAchievements;
private readonly EntitySet<CharacterAchievement> characterAchievements;

private readonly EntitySet<CharacterClanInvite> clanInvites;
private readonly EntitySet<Clan> clans;
private readonly EntitySet<ClanRole> clanRoles;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using RavenNest.BusinessLogic.Data;
using RavenNest.BusinessLogic.Providers;
using RavenNest.DataModels;
using System;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ static ResourceTaskProcessor()
{
DefaultDroppableResources = new List<ResourceDrop>()
{
new ResourceDrop(Guid.Parse("49D53A1E-55F7-4537-9A5B-0560B1C0F465"), "Ethereum", 0.003, 280, 6),
new ResourceDrop(Guid.Parse("BA6ED0AD-2FE6-46BF-9A99-5528657FF40E"), "Lionite", 0.005, 240, 6),
new ResourceDrop(Guid.Parse("17c3f9b1-57d6-4219-bbc7-9e929757babf"), "Phantom Core", 0.01, 200, 6),
new ResourceDrop(Guid.Parse("f9b7e6a3-4e4a-4e4a-b79d-42a3cf2a16c8"), "Abraxas Spirit", 0.02, 170, 6),
new ResourceDrop(Guid.Parse("0dc620c2-b726-4928-9f1c-fcf61aaa2542"), "Dragon Scale", 0.025, 130, 6),
new ResourceDrop(Guid.Parse("40781EB8-1EBF-4C0C-9A11-6E8033C9953C"), "Rune Nugget", 0.075, 70, 6),
new ResourceDrop(Guid.Parse("E32A6F17-653C-4AF3-A3A1-D0C6674FE4D5"), "Adamantite Nugget", 0.1, 50, 6),
new ResourceDrop(Guid.Parse("FEE5E07E-4397-44A9-9E3A-ED0465CE29FC"), "Gold Nugget", 0.135, 30, 6),
new ResourceDrop(Guid.Parse("B3411B33-59F6-4443-A70C-6576B6EC74EC"), "Mithril Nugget", 0.135, 30, 6),
new ResourceDrop(Guid.Parse("F5A6063F-CC99-48BF-BC79-F764CD87373A"), "Ruby", 0.135, 25, 6),
new ResourceDrop(Guid.Parse("48C94F6C-6119-48A2-88EA-F7649F816DA4"), "Emerald", 0.135, 20, 6),
new ResourceDrop(Guid.Parse("723A48A0-E3CB-4EBD-9966-EE8323B11DC0"), "Sapphire", 0.15, 10, 6),
new ResourceDrop(Guid.Parse("EF674846-817E-41B7-B378-85E64D2CCF5D"), "Steel Nugget", 0.185, 10, 6),
new ResourceDrop(Guid.Parse("CC61E4A3-B00E-4FD4-9160-16A6466787E6"), "Iron Nugget", 0.2, 1, 6),
new ResourceDrop(Guid.Parse("49D53A1E-55F7-4537-9A5B-0560B1C0F465"), "Ethereum", 0.003, 0, 280, 6),
new ResourceDrop(Guid.Parse("BA6ED0AD-2FE6-46BF-9A99-5528657FF40E"), "Lionite", 0.005, 0,240, 6),
new ResourceDrop(Guid.Parse("17c3f9b1-57d6-4219-bbc7-9e929757babf"), "Phantom Core", 0.01, 0, 200, 6),
new ResourceDrop(Guid.Parse("f9b7e6a3-4e4a-4e4a-b79d-42a3cf2a16c8"), "Abraxas Spirit", 0.02, 0, 170, 6),
new ResourceDrop(Guid.Parse("0dc620c2-b726-4928-9f1c-fcf61aaa2542"), "Dragon Scale", 0.025, 0, 130, 6),
new ResourceDrop(Guid.Parse("40781EB8-1EBF-4C0C-9A11-6E8033C9953C"), "Rune Nugget", 0.075, 0, 70, 6),
new ResourceDrop(Guid.Parse("E32A6F17-653C-4AF3-A3A1-D0C6674FE4D5"), "Adamantite Nugget", 0.1, 0, 50, 6),
new ResourceDrop(Guid.Parse("FEE5E07E-4397-44A9-9E3A-ED0465CE29FC"), "Gold Nugget", 0.135, 0, 30, 6),
new ResourceDrop(Guid.Parse("B3411B33-59F6-4443-A70C-6576B6EC74EC"), "Mithril Nugget", 0.135, 0, 30, 6),
new ResourceDrop(Guid.Parse("F5A6063F-CC99-48BF-BC79-F764CD87373A"), "Ruby", 0.135, 0, 25, 6),
new ResourceDrop(Guid.Parse("48C94F6C-6119-48A2-88EA-F7649F816DA4"), "Emerald", 0.135, 0, 20, 6),
new ResourceDrop(Guid.Parse("723A48A0-E3CB-4EBD-9966-EE8323B11DC0"), "Sapphire", 0.15, 0, 10, 6),
new ResourceDrop(Guid.Parse("EF674846-817E-41B7-B378-85E64D2CCF5D"), "Steel Nugget", 0.185, 0, 10, 6),
new ResourceDrop(Guid.Parse("CC61E4A3-B00E-4FD4-9160-16A6466787E6"), "Iron Nugget", 0.2, 0, 1, 6),
};
}

Expand Down Expand Up @@ -114,14 +114,16 @@ public class ResourceDrop
public Guid Id { get; }
public string Name { get; }
public double DropChance { get; }
public double Cooldown { get; }
public int SkillLevel { get; set; }
public int? SkillIndex { get; set; }

public ResourceDrop(Guid id, string name, double dropChance, int skillLevel, int? skillIndex)
public ResourceDrop(Guid id, string name, double dropChance, double cooldown, int skillLevel, int? skillIndex)
{
Id = id;
Name = name;
DropChance = dropChance;
Cooldown = cooldown;
SkillLevel = skillLevel;
SkillIndex = skillIndex;
}
Expand All @@ -133,7 +135,7 @@ public double GetDropChance(int playerSkillLevel)

public static implicit operator ResourceDrop(ResourceItemDrop source)
{
return new ResourceDrop(source.ItemId, source.ItemName, source.DropChance, source.LevelRequirement, source.Skill);
return new ResourceDrop(source.ItemId, source.ItemName, source.DropChance, source.Cooldown ?? 0, source.LevelRequirement, source.Skill);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using RavenNest.BusinessLogic.Data;
using RavenNest.BusinessLogic.Providers;
using RavenNest.DataModels;
using System;
using System.Collections.Generic;
Expand All @@ -11,6 +10,7 @@ public class SimpleDropHandler
{
private readonly string skill;
private readonly List<ResourceDrop> drops = new List<ResourceDrop>();
private readonly Dictionary<string, DateTime> dropTimes = new Dictionary<string, DateTime>();

private bool initialized;
public SimpleDropHandler(string skill)
Expand All @@ -30,7 +30,14 @@ public void LoadDrops(GameData gameData)
}
}

public bool TryDropItem(ResourceTaskProcessor resProcessor, GameData gameData, PlayerInventoryProvider inventoryProvider, GameSession session, Character character, int skillLevel, Func<ResourceDrop, bool> canDrop = null)
public bool TryDropItem(
ResourceTaskProcessor resProcessor,
GameData gameData,
PlayerInventoryProvider inventoryProvider,
GameSession session,
Character character,
int skillLevel,
Func<ResourceDrop, bool> canDrop = null)
{
var chance = resProcessor.Random.NextDouble();
if (chance > ItemDropRateSettings.InitDropChance)
Expand All @@ -47,6 +54,23 @@ public bool TryDropItem(ResourceTaskProcessor resProcessor, GameData gameData, P
{
if (canDrop == null || canDrop(res))
{
// check for cooldown
var cooldownKey = character.Id + "_" + res.Id;
if (res.Cooldown > 0)
{
if (dropTimes.TryGetValue(cooldownKey, out var lastDrop))
{
if (DateTime.UtcNow - lastDrop < TimeSpan.FromSeconds(res.Cooldown))
{
return false;
}
}
else
{
dropTimes[cooldownKey] = DateTime.UtcNow;
}
}

resProcessor.IncrementItemStack(gameData, inventoryProvider, session, character, res.Id);
return true;
}
Expand Down
8 changes: 8 additions & 0 deletions src/RavenNest.DataModels/Achievement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace RavenNest.DataModels
{
public partial class Achievement : Entity<Achievement>
{
private string name; public string Name { get => name; set => Set(ref name, value); }
private string description; public string Description { get => description; set => Set(ref description, value); }
}
}
11 changes: 11 additions & 0 deletions src/RavenNest.DataModels/AchievementReward.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace RavenNest.DataModels
{
public partial class AchievementReward : Entity<Achievement>
{
private Guid achievementId; public Guid AchievementId { get => achievementId; set => Set(ref achievementId, value); }
private int rewardType; public int RewardType { get => rewardType; set => Set(ref rewardType, value); }
private long rewardAmount; public long RewardAmount { get => rewardAmount; set => Set(ref rewardAmount, value); }
}
}
11 changes: 11 additions & 0 deletions src/RavenNest.DataModels/CharacterAchievement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace RavenNest.DataModels
{
public partial class CharacterAchievement : Entity<CharacterAchievement>
{
private Guid achievementId; public Guid AchievementId { get => achievementId; set => Set(ref achievementId, value); }
private Guid characterId; public Guid CharacterId { get => characterId; set => Set(ref characterId, value); }
private DateTime achieved; public DateTime Achieved { get => achieved; set => Set(ref achieved, value); }
}
}
1 change: 1 addition & 0 deletions src/RavenNest.DataModels/ResourceItemDrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public partial class ResourceItemDrop : Entity<ResourceItemDrop>
private double dropChance; public double DropChance { get => dropChance; set => Set(ref dropChance, value); }
private int levelRequirement; public int LevelRequirement { get => levelRequirement; set => Set(ref levelRequirement, value); }
private int? skill; public int? Skill { get => skill; set => Set(ref skill, value); }
private double? cooldown; public double? Cooldown { get => cooldown; set => Set(ref cooldown, value); }
}
}
11 changes: 11 additions & 0 deletions src/RavenNest.DataModels/UserAchievement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace RavenNest.DataModels
{
public partial class UserAchievement : Entity<UserAchievement>
{
private Guid achievementId; public Guid AchievementId { get => achievementId; set => Set(ref achievementId, value); }
private Guid userId; public Guid UserId { get => userId; set => Set(ref userId, value); }
private DateTime achieved; public DateTime Achieved { get => achieved; set => Set(ref achieved, value); }
}
}

0 comments on commit 1ea186e

Please sign in to comment.