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

Commit

Permalink
Added support paging and filtering contract Web UI
Browse files Browse the repository at this point in the history
Added Creator Steam ID to the contract Web UI
Added styling to the contract Web UI
Added support for singleplayer scores
Changed server port from 80 to 40147 to prevent issues for most users
Fixed issue where Web UI wouldn't show any contracts
Fixed exploit where self-made contracts could be replayed and earn you too much money
  • Loading branch information
LennardF1989 committed Jul 18, 2023
1 parent 8c6a678 commit 266cbe8
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 72 deletions.
4 changes: 2 additions & 2 deletions Bin/Hook/hook.ini
Expand Up @@ -6,8 +6,8 @@ log=true
game=hm5

[hm5]
webserviceurl=http://localhost/hm5
webserviceurl=http://localhost:40147/hm5
skiplauncher=true

[sniper]
webserviceurl=http://localhost/sniper
webserviceurl=http://localhost:40147/sniper
4 changes: 2 additions & 2 deletions Src/HM5.Hook/hook.ini
Expand Up @@ -6,8 +6,8 @@ log=true
game=hm5

[hm5]
webserviceurl=http://localhost/hm5
webserviceurl=http://localhost:40147/hm5
skiplauncher=true

[sniper]
webserviceurl=http://localhost/sniper
webserviceurl=http://localhost:40147/sniper
15 changes: 8 additions & 7 deletions Src/HM5.Server/Controllers/ContractsController.cs
Expand Up @@ -19,32 +19,33 @@ public IActionResult Index()
return View("~/Views/Contracts.cshtml");
}

[HttpGet("all")]
public IActionResult GetContracts()
[HttpGet("all/{page}/{filter=}")]
public IActionResult GetContracts(int page, string filter = null)
{
const int pageSize = 1000;
const int pageSize = 20;

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

return Json(contracts.Select(x => new
{
x.Id,
x.UserId,
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
Score = x.HighestScoringFriendScore
}));
}

Expand All @@ -53,7 +54,7 @@ public IActionResult GetShareLink(string contractId)
{
var contractLink = _contractsService.GetShareableContractLink(contractId);

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

[HttpGet("create/{compressedBase64Contract}")]
Expand Down
@@ -1,7 +1,6 @@
using HM5.Server.Attributes;
using HM5.Server.Enums;
using HM5.Server.Interfaces;
using HM5.Server.Models;
using Microsoft.AspNetCore.Mvc;

namespace HM5.Server.Controllers.Hitman
Expand Down Expand Up @@ -36,14 +35,7 @@ public class GetScoreComparisonRequest : IEdmFunctionImport
[Route("GetScoreComparison")]
public IActionResult GetScoreComparison([FromQuery] GetScoreComparisonRequest request)
{
return JsonEntryResponse(new ScoreComparison
{
//NOTE: Has to be a SteamID
FriendName = "76561198161220058",
FriendScore = 1337,
CountryAverage = 101,
WorldAverage = 101
});
return JsonEntryResponse(_hitmanServer.GetScoreComparison(request));
}
}
}
1 change: 1 addition & 0 deletions Src/HM5.Server/HM5.Server.csproj
Expand Up @@ -10,6 +10,7 @@
<ItemGroup>
<Content Remove="Contracts\**" />
<None Remove="Logs\**" />
<Content Remove="userprofile.json" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions Src/HM5.Server/Interfaces/IContractsService.cs
Expand Up @@ -6,8 +6,8 @@ namespace HM5.Server.Interfaces
public interface IContractsService
{
void RebuildCache();
void CreateContract(HitmanController.UploadContractRequest request);
void CreateContract(string compressedBase64Contract);
string CreateContract(HitmanController.UploadContractRequest request);
string CreateContract(string compressedBase64Contract);
void RemoveContract(string contractId);
IEnumerable<Contract> GetContracts(HitmanController.SearchForContracts2Request request, Func<Contract, bool> additionalFilter = null);
string GetShareableContractLink(string contractId);
Expand Down
1 change: 1 addition & 0 deletions Src/HM5.Server/Interfaces/IHitmanServer.cs
Expand Up @@ -21,5 +21,6 @@ public interface IHitmanServer
List<Contract> SearchForContracts2(HitmanController.SearchForContracts2Request request);
void UpdateUserInfo(HitmanController.UpdateUserInfoRequest request);
void UploadContract(HitmanController.UploadContractRequest request);
ScoreComparison GetScoreComparison(HitmanController.GetScoreComparisonRequest request);
}
}
2 changes: 1 addition & 1 deletion Src/HM5.Server/Properties/launchSettings.json
Expand Up @@ -6,7 +6,7 @@
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:80;https://localhost:443"
"applicationUrl": "http://localhost:40147"
}
}
}
22 changes: 14 additions & 8 deletions Src/HM5.Server/Services/ContractsService.cs
Expand Up @@ -61,7 +61,7 @@ public void RebuildCache()
}
}

public void CreateContract(HitmanController.UploadContractRequest request)
public string CreateContract(HitmanController.UploadContractRequest request)
{
var simpleContract = new SimpleContract
{
Expand All @@ -79,10 +79,10 @@ public void CreateContract(HitmanController.UploadContractRequest request)
Score = request.Score
};

CreateContract(simpleContract);
return CreateContract(simpleContract);
}

public void CreateContract(string compressedBase64Contract)
public string CreateContract(string compressedBase64Contract)
{
using var inputStream = new MemoryStream(
Base64UrlTextEncoder.Decode(compressedBase64Contract)
Expand All @@ -95,7 +95,7 @@ public void CreateContract(string compressedBase64Contract)

var simpleContract = JsonSerializer.Deserialize<SimpleContract>(outputStream.ToArray());

CreateContract(simpleContract);
return CreateContract(simpleContract);
}

public void RemoveContract(string contractId)
Expand All @@ -118,7 +118,11 @@ public IEnumerable<Contract> GetContracts(HitmanController.SearchForContracts2Re
(request.LevelIndex == -1 || x.LevelIndex == request.LevelIndex) &&
(request.CheckpointId == -1 || x.CheckpointIndex == request.CheckpointId) &&
(request.Difficulty == -1 || x.Difficulty == request.Difficulty) &&
(request.ContractName == string.Empty || x.DisplayId.Contains(request.ContractName) || x.Title.Contains(request.ContractName, StringComparison.InvariantCultureIgnoreCase)) &&
(
string.IsNullOrWhiteSpace(request.ContractName) ||
x.DisplayId.Contains(request.ContractName) ||
x.Title.Contains(request.ContractName, StringComparison.InvariantCultureIgnoreCase)
) &&
(additionalFilter == null || additionalFilter(x))
)
.OrderBy(x => x.DisplayId)
Expand Down Expand Up @@ -210,7 +214,7 @@ public string GetDifficultyName(Contract contract)
};
}

private void CreateContract(SimpleContract simpleContract)
private string CreateContract(SimpleContract simpleContract)
{
var requestBytes = JsonSerializer.SerializeToUtf8Bytes(simpleContract);
using var sha1 = SHA1.Create();
Expand All @@ -221,7 +225,7 @@ private void CreateContract(SimpleContract simpleContract)

if (File.Exists(contractsPath))
{
return;
return contractId;
}

var contract = MapSimpleContractToContract(contractId, simpleContract);
Expand All @@ -230,6 +234,8 @@ private void CreateContract(SimpleContract simpleContract)
var contractJson = JsonSerializer.Serialize(simpleContract);

File.WriteAllTextAsync(contractsPath, contractJson);

return contractId;
}

private Contract MapSimpleContractToContract(string contractId, SimpleContract simpleContract)
Expand All @@ -239,7 +245,7 @@ private Contract MapSimpleContractToContract(string contractId, SimpleContract s
Id = contractId,
DisplayId = simpleContract.Title,
UserId = simpleContract.UserId,
UserName = string.Empty,
UserName = simpleContract.UserId,
Title = simpleContract.Title,
Description = simpleContract.Description,
Targets = new TargetsWrapper
Expand Down
77 changes: 60 additions & 17 deletions Src/HM5.Server/Services/LocalHitmanServer.cs
Expand Up @@ -98,10 +98,17 @@ public Contract GetFeaturedContract(HitmanController.GetFeaturedContractRequest
.GetContracts(new HitmanController.SearchForContracts2Request
{
LevelIndex = request.LevelIndex,
CheckpointId = -1,
Difficulty = -1,
StartIndex = 0,
Range = 1
}, contract => !_userProfile.PlayedContracts.ContainsKey(contract.Id))
.FirstOrDefault();
}, contract => GetPlayedContract(contract.Id)?.Plays == 0)
.FirstOrDefault();

if (contract != null)
{
contract.UserScore = GetPlayedContract(contract.Id)?.Score ?? 0;
}

return contract;
}
Expand Down Expand Up @@ -130,12 +137,7 @@ public void MarkContractAsPlayed(HitmanController.MarkContractAsPlayedRequest re
{
SaveUserProfile(() =>
{
if (!_userProfile.PlayedContracts.TryGetValue(request.ContractId, out var playedContract))
{
playedContract = new UserProfile.PlayedContract();
_userProfile.PlayedContracts[request.ContractId] = playedContract;
}
var playedContract = GetOrAddPlayedContract(request.ContractId);
playedContract.Plays++;
});
Expand All @@ -147,12 +149,7 @@ public int PutScore(HitmanController.PutScoreRequest request)

SaveUserProfile(() =>
{
if (!_userProfile.PlayedContracts.TryGetValue(request.LeaderboardId, out var playedContract))
{
playedContract = new UserProfile.PlayedContract();
_userProfile.PlayedContracts[request.LeaderboardId] = playedContract;
}
var playedContract = GetOrAddPlayedContract(request.LeaderboardId);
difference = request.Score - playedContract.Score;
Expand All @@ -162,7 +159,7 @@ public int PutScore(HitmanController.PutScoreRequest request)
_userProfile.WalletAmount += difference;
}
_userProfile.PlayedContracts[request.LeaderboardId].Score = Math.Max(request.Score, playedContract.Score);
playedContract.Score = Math.Max(request.Score, playedContract.Score);
});

return difference;
Expand Down Expand Up @@ -204,7 +201,9 @@ public List<Contract> SearchForContracts2(HitmanController.SearchForContracts2Re

contracts.ForEach(x =>
{
if (!_userProfile.PlayedContracts.TryGetValue(x.Id, out var playedContract))
var playedContract = GetPlayedContract(x.Id);
if (playedContract == null)
{
return;
}
Expand All @@ -229,14 +228,35 @@ public void UpdateUserInfo(HitmanController.UpdateUserInfoRequest request)

public void UploadContract(HitmanController.UploadContractRequest request)
{
_contractsService.CreateContract(request);
var contractId = _contractsService.CreateContract(request);

SaveUserProfile(() =>
{
var playedContract = GetOrAddPlayedContract(contractId);
playedContract.Score = request.Score;
_userProfile.ContractsCreated++;
});
}

public ScoreComparison GetScoreComparison(HitmanController.GetScoreComparisonRequest request)
{
var playedContract = GetPlayedContract(request.LeaderboardId);

if (playedContract == null)
{
return null;
}

return new ScoreComparison
{
FriendName = _userProfile.UserId,
FriendScore = playedContract.Score,
CountryAverage = 0,
WorldAverage = 0
};
}

private UserProfile LoadUserProfile()
{
lock (_profileLock)
Expand Down Expand Up @@ -276,5 +296,28 @@ private void SaveUserProfile(Action scope)
File.WriteAllText(USERPROFILE_PATH, JsonSerializer.Serialize(_userProfile));
}
}

private UserProfile.PlayedContract GetPlayedContract(string contractId)
{
return _userProfile.PlayedContracts.TryGetValue(contractId, out var playedContract)
? playedContract
: null;
}

private UserProfile.PlayedContract GetOrAddPlayedContract(string contractId)
{
var playedContract = GetPlayedContract(contractId);

if (playedContract != null)
{
return playedContract;
}

playedContract = new UserProfile.PlayedContract();

_userProfile.PlayedContracts[contractId] = playedContract;

return playedContract;
}
}
}
12 changes: 12 additions & 0 deletions Src/HM5.Server/Services/MockedHitmanServer.cs
Expand Up @@ -341,5 +341,17 @@ public void UploadContract(HitmanController.UploadContractRequest request)
{
//Do nothing
}

public ScoreComparison GetScoreComparison(HitmanController.GetScoreComparisonRequest request)
{
return new ScoreComparison
{
//NOTE: Has to be a SteamID
FriendName = "76561198161220058",
FriendScore = 1337,
CountryAverage = 101,
WorldAverage = 101
};
}
}
}
178 changes: 155 additions & 23 deletions Src/HM5.Server/Views/Contracts.cshtml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Src/HM5.Server/appsettings.json
Expand Up @@ -2,7 +2,7 @@
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://localhost:80"
"Url": "http://localhost:40147"
}
}
},
Expand Down

0 comments on commit 266cbe8

Please sign in to comment.