Skip to content
This repository has been archived by the owner on Oct 22, 2023. It is now read-only.

Commit

Permalink
Added support for custom contracts
Browse files Browse the repository at this point in the history
Added UI to manage and share custom contracts
  • Loading branch information
LennardF1989 committed Jul 17, 2023
1 parent 860b934 commit b17ebfa
Show file tree
Hide file tree
Showing 15 changed files with 536 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,6 +5,7 @@ obj
*.user

# Server
/Src/HM5.Server/Contracts
/Src/HM5.Server/Logs

# Other
Expand Down
75 changes: 75 additions & 0 deletions Src/HM5.Server/Controllers/ContractsController.cs
@@ -0,0 +1,75 @@
using HM5.Server.Controllers.Hitman;
using HM5.Server.Interfaces;
using Microsoft.AspNetCore.Mvc;

namespace HM5.Server.Controllers
{
[Route("contracts")]
public class ContractsController : Controller
{
private readonly IContractsService _contractsService;

public ContractsController(IContractsService contractsService)
{
_contractsService = contractsService;
}

public IActionResult Index()
{
return View("~/Views/Contracts.cshtml");
}

[HttpGet("all")]
public IActionResult GetContracts()
{
const int pageSize = 1000;

var contracts = _contractsService.GetContracts(new HitmanController.SearchForContracts2Request
{
LevelIndex = -1,
CheckpointId = -1,
Difficulty = -1,
ContractId = string.Empty,
StartIndex = 0,
Range = pageSize
});

return Json(contracts.Select(x => new
{
x.Id,
Name = x.DisplayId,
x.Description,
Level = _contractsService.GetLevelName(x),
Targets = x.Targets.Targets
.Select(y => y.Name)
.ToList(),
Difficulty = _contractsService.GetDifficultyName(x),
Score = x.UserScore
}));
}

[HttpGet("share/{contractId}")]
public IActionResult GetShareLink(string contractId)
{
var contractLink = _contractsService.GetShareableContractLink(contractId);

return Json($"http://localhost/contracts/create/{contractLink}");
}

[HttpGet("create/{compressedBase64Contract}")]
public IActionResult CreateContract(string compressedBase64Contract)
{
_contractsService.CreateContract(compressedBase64Contract);

return RedirectToAction("Index");
}

[HttpGet("delete/{contractId}")]
public IActionResult DeleteContract(string contractId)
{
_contractsService.RemoveContract(contractId);

return RedirectToAction("Index");
}
}
}
9 changes: 6 additions & 3 deletions Src/HM5.Server/Controllers/Hitman/HitmanController.cs
Expand Up @@ -86,7 +86,7 @@ public abstract class BaseGetAverageScoresRequest : IEdmFunctionImport

private static readonly Contract _mockedContractWithoutCompetition = new()
{
Id = 1,
Id = "1",
DisplayId = "FakeContract47",
UserId = "76561198161220058",
UserName = "Wingz of Death",
Expand Down Expand Up @@ -138,7 +138,7 @@ public abstract class BaseGetAverageScoresRequest : IEdmFunctionImport

private static readonly Contract _mockedContractWithCompetition = new()
{
Id = 2,
Id = "2",
DisplayId = "FakeContract48",
UserId = "76561198161220058",
UserName = "Wingz of Death",
Expand Down Expand Up @@ -206,16 +206,19 @@ public abstract class BaseGetAverageScoresRequest : IEdmFunctionImport

private readonly IMetadataService _metadataService;
private readonly Options _options;
private readonly IContractsService _contractsService;

public HitmanController(
ISimpleLogger simpleLogger,
IMetadataServiceForHitman metadataService,
Options options
Options options,
IContractsService contractsService
)
: base(simpleLogger)
{
_metadataService = metadataService;
_options = options;
_contractsService = contractsService;

//Apply options to the mocked contracts
_mockedContractWithoutCompetition.UserId = options.MockedContractSteamId;
Expand Down
Expand Up @@ -102,7 +102,7 @@ public IActionResult GetMessages([FromQuery] GetMessagesRequest request)
return JsonFeedResponse(messages);
}

private string MasterCraftedSilverballer(string title, string body, int? contractId = null)
private string MasterCraftedSilverballer(string title, string body, string contractId = null)
{
return contractId == null
? $"Silverballer|||{title}||{{baller}}|||{{baller}}||||{body}"
Expand Down
Expand Up @@ -57,6 +57,13 @@ public class SearchForContracts2Request : IEdmFunctionImport
[Route("SearchForContracts2")]
public IActionResult SearchForContracts2([FromQuery] SearchForContracts2Request request)
{
if (_options.UseCustomContracts)
{
var contracts = _contractsService.GetContracts(request);

return JsonFeedResponse(contracts.ToList());
}

return JsonFeedResponse(new List<Contract>
{
_mockedContractWithoutCompetition,
Expand Down
Expand Up @@ -75,6 +75,11 @@ public class UploadContractRequest : IEdmFunctionImport
[Route("UploadContract")]
public IActionResult UploadContract([FromQuery] UploadContractRequest request)
{
if (_options.UseCustomContracts)
{
_contractsService.CreateContract(request);
}

return Ok();
}
}
Expand Down
3 changes: 2 additions & 1 deletion Src/HM5.Server/HM5.Server.csproj
Expand Up @@ -8,7 +8,8 @@
</PropertyGroup>

<ItemGroup>
<Folder Include="Logs\" />
<Content Remove="Contracts\**" />
<None Remove="Logs\**" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions Src/HM5.Server/Interfaces/IContractsService.cs
@@ -0,0 +1,17 @@
using HM5.Server.Controllers.Hitman;
using HM5.Server.Models;

namespace HM5.Server.Interfaces
{
public interface IContractsService
{
void RebuildCache();
void CreateContract(HitmanController.UploadContractRequest request);
void CreateContract(string compressedBase64Contract);
void RemoveContract(string contractId);
IEnumerable<Contract> GetContracts(HitmanController.SearchForContracts2Request request);
string GetShareableContractLink(string contractId);
string GetLevelName(Contract contract);
string GetDifficultyName(Contract contract);
}
}
2 changes: 1 addition & 1 deletion Src/HM5.Server/Json/AnyToJsonStringConverter.cs
Expand Up @@ -7,7 +7,7 @@ public class AnyToJsonStringConverter<T> : JsonConverter<T>
{
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
return JsonSerializer.Deserialize<T>(reader.GetString());
}

public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
Expand Down
3 changes: 1 addition & 2 deletions Src/HM5.Server/Models/Contract.cs
Expand Up @@ -9,10 +9,9 @@ namespace HM5.Server.Models
[EdmEntity("Contract")]
public class Contract : IEdmEntity
{
//NOTE: Even though this is treated as a String in-game, we always assign an Integer.
[JsonPropertyName("_id")]
[EdmProperty("_id", EdmTypes.String, false)]
public int Id { get; set; }
public string Id { get; set; }

[EdmProperty("DisplayId", EdmTypes.String, false)]
public string DisplayId { get; set; }
Expand Down
1 change: 1 addition & 0 deletions Src/HM5.Server/Options.cs
Expand Up @@ -8,5 +8,6 @@ public class Options
public bool EnableResponseBodyLogging { get; set; } = false;
public string MockedContractSteamId { get; set; } = "76561198161220058";
public int WalletAmount { get; set; } = 1337;
public bool UseCustomContracts { get; set; } = false;
}
}

0 comments on commit b17ebfa

Please sign in to comment.