diff --git a/Bin/Hook/hook.ini b/Bin/Hook/hook.ini index 1e7254f..39060bd 100644 --- a/Bin/Hook/hook.ini +++ b/Bin/Hook/hook.ini @@ -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 \ No newline at end of file +webserviceurl=http://localhost:40147/sniper \ No newline at end of file diff --git a/Src/HM5.Hook/hook.ini b/Src/HM5.Hook/hook.ini index 1e7254f..39060bd 100644 --- a/Src/HM5.Hook/hook.ini +++ b/Src/HM5.Hook/hook.ini @@ -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 \ No newline at end of file +webserviceurl=http://localhost:40147/sniper \ No newline at end of file diff --git a/Src/HM5.Server/Controllers/ContractsController.cs b/Src/HM5.Server/Controllers/ContractsController.cs index 60a079d..eedd668 100644 --- a/Src/HM5.Server/Controllers/ContractsController.cs +++ b/Src/HM5.Server/Controllers/ContractsController.cs @@ -19,24 +19,25 @@ 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), @@ -44,7 +45,7 @@ public IActionResult GetContracts() .Select(y => y.Name) .ToList(), Difficulty = _contractsService.GetDifficultyName(x), - Score = x.UserScore + Score = x.HighestScoringFriendScore })); } @@ -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}")] diff --git a/Src/HM5.Server/Controllers/Hitman/HitmanController_GetScoreComparison.cs b/Src/HM5.Server/Controllers/Hitman/HitmanController_GetScoreComparison.cs index 87d4b50..2578698 100644 --- a/Src/HM5.Server/Controllers/Hitman/HitmanController_GetScoreComparison.cs +++ b/Src/HM5.Server/Controllers/Hitman/HitmanController_GetScoreComparison.cs @@ -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 @@ -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)); } } } diff --git a/Src/HM5.Server/HM5.Server.csproj b/Src/HM5.Server/HM5.Server.csproj index b24d6ac..5dce399 100644 --- a/Src/HM5.Server/HM5.Server.csproj +++ b/Src/HM5.Server/HM5.Server.csproj @@ -10,6 +10,7 @@ + diff --git a/Src/HM5.Server/Interfaces/IContractsService.cs b/Src/HM5.Server/Interfaces/IContractsService.cs index 2223f7f..e78ef5a 100644 --- a/Src/HM5.Server/Interfaces/IContractsService.cs +++ b/Src/HM5.Server/Interfaces/IContractsService.cs @@ -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 GetContracts(HitmanController.SearchForContracts2Request request, Func additionalFilter = null); string GetShareableContractLink(string contractId); diff --git a/Src/HM5.Server/Interfaces/IHitmanServer.cs b/Src/HM5.Server/Interfaces/IHitmanServer.cs index 031d1f9..2290619 100644 --- a/Src/HM5.Server/Interfaces/IHitmanServer.cs +++ b/Src/HM5.Server/Interfaces/IHitmanServer.cs @@ -21,5 +21,6 @@ public interface IHitmanServer List SearchForContracts2(HitmanController.SearchForContracts2Request request); void UpdateUserInfo(HitmanController.UpdateUserInfoRequest request); void UploadContract(HitmanController.UploadContractRequest request); + ScoreComparison GetScoreComparison(HitmanController.GetScoreComparisonRequest request); } } diff --git a/Src/HM5.Server/Properties/launchSettings.json b/Src/HM5.Server/Properties/launchSettings.json index 673bd65..5bce0d6 100644 --- a/Src/HM5.Server/Properties/launchSettings.json +++ b/Src/HM5.Server/Properties/launchSettings.json @@ -6,7 +6,7 @@ "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, - "applicationUrl": "http://localhost:80;https://localhost:443" + "applicationUrl": "http://localhost:40147" } } } \ No newline at end of file diff --git a/Src/HM5.Server/Services/ContractsService.cs b/Src/HM5.Server/Services/ContractsService.cs index 57baa0a..383371a 100644 --- a/Src/HM5.Server/Services/ContractsService.cs +++ b/Src/HM5.Server/Services/ContractsService.cs @@ -61,7 +61,7 @@ public void RebuildCache() } } - public void CreateContract(HitmanController.UploadContractRequest request) + public string CreateContract(HitmanController.UploadContractRequest request) { var simpleContract = new SimpleContract { @@ -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) @@ -95,7 +95,7 @@ public void CreateContract(string compressedBase64Contract) var simpleContract = JsonSerializer.Deserialize(outputStream.ToArray()); - CreateContract(simpleContract); + return CreateContract(simpleContract); } public void RemoveContract(string contractId) @@ -118,7 +118,11 @@ public IEnumerable 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) @@ -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(); @@ -221,7 +225,7 @@ private void CreateContract(SimpleContract simpleContract) if (File.Exists(contractsPath)) { - return; + return contractId; } var contract = MapSimpleContractToContract(contractId, simpleContract); @@ -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) @@ -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 diff --git a/Src/HM5.Server/Services/LocalHitmanServer.cs b/Src/HM5.Server/Services/LocalHitmanServer.cs index f9c2aa5..dca1fc6 100644 --- a/Src/HM5.Server/Services/LocalHitmanServer.cs +++ b/Src/HM5.Server/Services/LocalHitmanServer.cs @@ -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; } @@ -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++; }); @@ -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; @@ -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; @@ -204,7 +201,9 @@ public List SearchForContracts2(HitmanController.SearchForContracts2Re contracts.ForEach(x => { - if (!_userProfile.PlayedContracts.TryGetValue(x.Id, out var playedContract)) + var playedContract = GetPlayedContract(x.Id); + + if (playedContract == null) { return; } @@ -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) @@ -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; + } } } diff --git a/Src/HM5.Server/Services/MockedHitmanServer.cs b/Src/HM5.Server/Services/MockedHitmanServer.cs index d7a83e2..8896bd7 100644 --- a/Src/HM5.Server/Services/MockedHitmanServer.cs +++ b/Src/HM5.Server/Services/MockedHitmanServer.cs @@ -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 + }; + } } } diff --git a/Src/HM5.Server/Views/Contracts.cshtml b/Src/HM5.Server/Views/Contracts.cshtml index 8819d89..01ba243 100644 --- a/Src/HM5.Server/Views/Contracts.cshtml +++ b/Src/HM5.Server/Views/Contracts.cshtml @@ -6,43 +6,111 @@ Hitman Absolution -