Skip to content

Commit

Permalink
New XP and credits API and event system.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielWillett committed Oct 31, 2024
1 parent 87ec396 commit 4e5691c
Show file tree
Hide file tree
Showing 46 changed files with 4,805 additions and 2,504 deletions.
15 changes: 13 additions & 2 deletions UncreatedWarfare/Commands/GroupCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,20 @@ public async UniTask ExecuteAsync(CancellationToken token)
throw Context.Reply(_translations.GroupNotFound, groupId.ToString(Data.LocalLocale));
}

if (!Context.Player.UnturnedPlayer.quests.ServerAssignToGroup(groupInfo.groupID, EPlayerGroupRank.MEMBER, true))
if (newTeam.IsValid)
{
throw Context.Reply(_translations.GroupNotFound, groupId.ToString(Data.LocalLocale));
await _teamManager.JoinTeamAsync(Context.Player, newTeam, token);
if (Context.Player.Team != newTeam)
{
throw Context.Reply(_translations.GroupNotFound, groupId.ToString(Data.LocalLocale));
}
}
else
{
if (!Context.Player.UnturnedPlayer.quests.ServerAssignToGroup(groupInfo.groupID, EPlayerGroupRank.MEMBER, true))
{
throw Context.Reply(_translations.GroupNotFound, groupId.ToString(Data.LocalLocale));
}
}

if (newTeam != null && newTeam.IsValid)
Expand Down
106 changes: 106 additions & 0 deletions UncreatedWarfare/Commands/Migrate/MigrateLegacyPointsCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#if DEBUG
using Microsoft.EntityFrameworkCore;
using MySqlConnector;
using System.Collections.Generic;
using System.Globalization;
using Uncreated.Warfare.Database;
using Uncreated.Warfare.Database.Manual;
using Uncreated.Warfare.Interaction.Commands;
using Uncreated.Warfare.Models.Factions;
using Uncreated.Warfare.Stats;

namespace Uncreated.Warfare.Commands;

[Command("offenses"), HideFromHelp, SubCommandOf(typeof(MigrateCommand))]
public class MigrateLegacyPointsCommand : IExecutableCommand
{
private readonly IManualMySqlProvider _mySqlProvider;
private readonly IPointsStore _pointsSql;
private readonly WarfareDbContext _dbContext;
public CommandContext Context { get; set; }

public MigrateLegacyPointsCommand(IManualMySqlProvider mySqlProvider, IPointsStore pointsSql, WarfareDbContext dbContext)
{
_mySqlProvider = mySqlProvider;
_pointsSql = pointsSql;
_dbContext = dbContext;
}

public async UniTask ExecuteAsync(CancellationToken token)
{
Context.AssertRanByTerminal();

if (Context.TryGet(0, out int seasonId))
{
if (seasonId is < 1 or > 3)
throw Context.ReplyString("Only seasons 1, 2, 3 can be migrated.");

await MigrateSeason(seasonId, token);
return;
}

for (int i = 1; i <= 3; ++i)
{
await MigrateSeason(i, token);
}
}

private async Task MigrateSeason(int seasonId, CancellationToken token)
{
string tableName = seasonId == 1 ? "levels" : ("s" + seasonId.ToString(CultureInfo.InvariantCulture) + "_levels");
string creditsName = seasonId == 1 ? "OfficerPoints" : "Credits";
string xpName = seasonId == 1 ? "XP" : "Experience";

// s0 (no longer have this data), s1 (usa vs russia), s2 (usa vs russia), s3 (usa vs mec)
(string t1, string t2) = seasonId switch
{
3 => ("usa", "mec"),
_ => ("usa", "russia")
};

Faction t1Faction = await _dbContext.Factions.FirstAsync(x => x.InternalName == t1, token).ConfigureAwait(false);
Faction t2Faction = await _dbContext.Factions.FirstAsync(x => x.InternalName == t2, token).ConfigureAwait(false);

try
{
int rowCt = 0;
List<LegacyPointsInfo> points = new List<LegacyPointsInfo>(65536);

await _mySqlProvider.QueryAsync($"SELECT `Steam64`,`Team`,`{xpName}`,`{creditsName}` FROM `{tableName}`;", null, token, reader =>
{
LegacyPointsInfo pt = default;
pt.Steam64 = reader.GetUInt64(0);
pt.Team = reader.GetUInt64(1);
pt.Experience = reader.IsDBNull(2) ? 0 : reader.GetUInt32(2);
pt.Credits = reader.IsDBNull(3) ? (seasonId == 1 ? 0u : 500u /* starting credits */) : reader.GetUInt32(3);
points.Add(pt);
}).ConfigureAwait(false);

foreach (LegacyPointsInfo pt in points)
{
uint factionId;

if (pt.Team == 1)
factionId = t1Faction.Key;
else if (pt.Team == 2)
factionId = t2Faction.Key;
else continue;

await _pointsSql.SetPointsAsync(new CSteamID(pt.Steam64), factionId, seasonId, pt.Experience, pt.Credits, token);
}
}
catch (MySqlException ex)
{
Context.Logger.LogWarning(ex, "Table may not exist: {0}.", tableName);
}
}

private struct LegacyPointsInfo
{
public ulong Steam64;
public ulong Team;
public uint Experience;
public uint Credits;
}
}
#endif
22 changes: 14 additions & 8 deletions UncreatedWarfare/Components/SpottedComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ public class SpottedComponent : MonoBehaviour
#endif
public void Initialize(Spotted type, Team ownerTeam, IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
_playerService = serviceProvider.GetRequiredService<IPlayerService>();

_logger = serviceProvider.GetRequiredService<ILogger<SpottedComponent>>();

_ownerTeam = ownerTeam;
_vehicle = null;
VehicleType = null;
Expand All @@ -79,6 +74,8 @@ public void Initialize(Spotted type, Team ownerTeam, IServiceProvider servicePro
CurrentSpotter = null;
IsLaserTarget = type == Spotted.FOB;

InitializeCommon(serviceProvider);

AssetConfiguration assetConfig = serviceProvider.GetRequiredService<AssetConfiguration>();

IAssetLink<EffectAsset>? effect;
Expand Down Expand Up @@ -119,14 +116,14 @@ public void Initialize(Spotted type, Team ownerTeam, IServiceProvider servicePro
}
public void Initialize(VehicleType type, InteractableVehicle vehicle, IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
_playerService = serviceProvider.GetRequiredService<IPlayerService>();

CurrentSpotter = null;
IsLaserTarget = type.IsGroundVehicle();
_vehicle = vehicle;
VehicleType = type;

InitializeCommon(serviceProvider);

AssetConfiguration assetConfig = serviceProvider.GetRequiredService<AssetConfiguration>();
IAssetLink<EffectAsset>? effect;
switch (type)
Expand Down Expand Up @@ -252,6 +249,15 @@ public void Initialize(VehicleType type, InteractableVehicle vehicle, IServicePr

_logger.LogConditional("Spotter initialized: {0}.", this);
}

private void InitializeCommon(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
_playerService = serviceProvider.GetRequiredService<IPlayerService>();

_logger = serviceProvider.GetRequiredService<ILogger<SpottedComponent>>();
}

#if ENABLE_SPOTTED_BUFF
private static void OnExitVehicle(ExitVehicle e)
{
Expand Down Expand Up @@ -479,7 +485,7 @@ private void TryAnnounce(WarfarePlayer spotter, string targetName)
if (IsActive)
return;

ToastMessage.QueueMessage(spotter, new ToastMessage(ToastMessageStyle.Mini, T.SpottedToast.Translate(spotter)));
spotter.SendToast(new ToastMessage(ToastMessageStyle.Mini, T.SpottedToast.Translate(spotter)));

Team t = spotter.Team;
Color t1 = t.Faction.Color;
Expand Down
Loading

0 comments on commit 4e5691c

Please sign in to comment.