From 46d96c591fb98d09536d1b9ab87cfb1fc0260bfa Mon Sep 17 00:00:00 2001 From: Smieszkokoleszko <63968615+Smieszkokoleszko@users.noreply.github.com> Date: Sat, 22 May 2021 23:11:58 +0200 Subject: [PATCH 1/2] New feature of DLC handling Added basic class for single user and class with a list of users. Added API controller to check users and their DLCs. --- ArmaForces.Boderator/.gitignore | 3 +- .../Controllers/DLCController.cs | 162 ++++++++++++++++++ .../DataClasses/SingleUser.cs | 26 +++ .../DataClasses/Users.cs | 10 ++ 4 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 ArmaForces.Boderator/ArmaForces.Boderator.BotService/Controllers/DLCController.cs create mode 100644 ArmaForces.Boderator/ArmaForces.Boderator.BotService/DataClasses/SingleUser.cs create mode 100644 ArmaForces.Boderator/ArmaForces.Boderator.BotService/DataClasses/Users.cs diff --git a/ArmaForces.Boderator/.gitignore b/ArmaForces.Boderator/.gitignore index b6984ff..43c515b 100644 --- a/ArmaForces.Boderator/.gitignore +++ b/ArmaForces.Boderator/.gitignore @@ -13,4 +13,5 @@ Boderator/* BoderatorTest/* BoderatorWeb/* -BoderatorWebTest/* \ No newline at end of file +BoderatorWebTest/* +log*.txt \ No newline at end of file diff --git a/ArmaForces.Boderator/ArmaForces.Boderator.BotService/Controllers/DLCController.cs b/ArmaForces.Boderator/ArmaForces.Boderator.BotService/Controllers/DLCController.cs new file mode 100644 index 0000000..aa8e366 --- /dev/null +++ b/ArmaForces.Boderator/ArmaForces.Boderator.BotService/Controllers/DLCController.cs @@ -0,0 +1,162 @@ +using System; +using System.Linq; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json.Linq; +using System.Collections.Generic; +using System.IO; +using ArmaForces.Boderator.BotService.DataClasses; + +namespace ArmaForces.Boderator.BotService.Controllers +{ + [Route("users/")] + [ApiController] + [Produces("application/json")] + public class DLCController : ControllerBase + { + private readonly Users _users; + + public DLCController(Users users) + { + _users = users; + } + + //Adds DLC to user + [HttpPut("{userID}/dlcadd/{dlcName}")] + public void AddDlcToUser(ulong userID, SingleUser.DLC dlcName) + { + if (!(_users.UsersList.Any(x => x.UserID == userID))) + { + Response.StatusCode = 404; + Response.WriteAsync($"User not found"); + return; + } + + if (_users.UsersList.Where(x => x.UserID == userID).Any(x => x.DLCList.Contains(dlcName))) + { + Response.WriteAsync($"DLC already added"); + return; + } + + _users.UsersList.Find(x => x.UserID == userID).DLCList.Add(dlcName); + + Response.WriteAsync($"DLC added"); + return; + } + + //List user with given ID. "0" lists all users + [HttpGet("{userID}")] + public void UserOnServer(ulong userID) + { + JArray usersArray = new JArray(); + var usersOnServer = _users.UsersList; + + if (userID != 0) + { + usersOnServer = usersOnServer + .FindAll(x => x.UserID == userID); + } + + if (usersOnServer.Count == 0) + { + Response.StatusCode = 404; + Response.WriteAsync("User not found"); + return; + } + + foreach (var user in usersOnServer) + { + var objUser = new JObject(); + objUser.Add("UserID", user.UserID); + + var dlcArray = new JArray(); + + foreach (var dlc in user.DLCList) + { + var objDLC = new JObject(); + objDLC.Add("DLC", dlc.ToString()); + dlcArray.Add(objDLC); + } + objUser.Add("DLC List", dlcArray); + + usersArray.Add(objUser); + } + + Response.WriteAsync($"{usersArray.ToString()}"); + } + + //Finds users wiht given DLC + [HttpGet("{dlcName}/users")] + public void UsersWithDLC(String dlcName) + { + var usersArray = new JArray(); + var usersOnServer = _users.UsersList; + + SingleUser.DLC dlcClass; + + try + { + dlcClass = (SingleUser.DLC)Enum.Parse(typeof(SingleUser.DLC), dlcName); + } + catch + { + Response.StatusCode = 404; + Response.WriteAsync("DLC not found"); + return; + } + + foreach (var user in usersOnServer) + { + if (user.DLCList.Contains(dlcClass)) + { + var objUser = new JObject(); + objUser.Add("UserID", user.UserID); + usersArray.Add(objUser); + } + } + + Response.WriteAsync($"{usersArray.ToString()}"); + } + + //Check if user has all DLCs + [HttpGet("DLC")] + public IActionResult UserhasDLC(ulong userID, String dlcs) + { + if (dlcs == null) + { + return (IActionResult)NotFound("No given DLC"); + } + + List dlcList = new List(); + + foreach (string singleDlc in dlcs.Split(',')) + { + + try + { + var dlcClass = (SingleUser.DLC)Enum.Parse(typeof(SingleUser.DLC), singleDlc); + dlcList.Add(dlcClass); + } + catch + { + return (IActionResult)BadRequest("Wrong DLC name"); + } + + } + + var user = _users.UsersList.SingleOrDefault(x => x.UserID == userID); + + if (user is null) + { + return (IActionResult)NotFound("User not found"); + } + + var dlcFound = !dlcList.Except(user.DLCList).Any(); + + return dlcFound + ? Ok("User has all given DLCs") + : (IActionResult)NotFound("User doesn't have all given DLCs"); + } + } +} + diff --git a/ArmaForces.Boderator/ArmaForces.Boderator.BotService/DataClasses/SingleUser.cs b/ArmaForces.Boderator/ArmaForces.Boderator.BotService/DataClasses/SingleUser.cs new file mode 100644 index 0000000..54a557f --- /dev/null +++ b/ArmaForces.Boderator/ArmaForces.Boderator.BotService/DataClasses/SingleUser.cs @@ -0,0 +1,26 @@ +using System.Collections.Generic; + +namespace ArmaForces.Boderator.BotService.DataClasses +{ + public class SingleUser + { + public ulong UserID; + public List DLCList = new List(); + + public enum DLC + { + Karts, + Helicopters, + Marksman, + Apex, + Jets, + LawsOfWar, + Tanks, + Contact, + GlobalMobilization, + SOGPrairieFire, + CSLA + } + + } +} diff --git a/ArmaForces.Boderator/ArmaForces.Boderator.BotService/DataClasses/Users.cs b/ArmaForces.Boderator/ArmaForces.Boderator.BotService/DataClasses/Users.cs new file mode 100644 index 0000000..826766f --- /dev/null +++ b/ArmaForces.Boderator/ArmaForces.Boderator.BotService/DataClasses/Users.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; + +namespace ArmaForces.Boderator.BotService.DataClasses +{ + public class Users + { + public List UsersList = new List(); + + } +} From f26186e74572e9d113e54a18388b0cd585ba11ad Mon Sep 17 00:00:00 2001 From: 3Mydlo3 Date: Thu, 9 Sep 2021 19:00:15 +0200 Subject: [PATCH 2/2] Move new files --- .../Controllers/DLCController.cs | 0 .../DataClasses/SingleUser.cs | 0 .../DataClasses/Users.cs | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename {ArmaForces.Boderator/ArmaForces.Boderator.BotService => ArmaForces.Boderator.BotService}/Controllers/DLCController.cs (100%) rename {ArmaForces.Boderator/ArmaForces.Boderator.BotService => ArmaForces.Boderator.BotService}/DataClasses/SingleUser.cs (100%) rename {ArmaForces.Boderator/ArmaForces.Boderator.BotService => ArmaForces.Boderator.BotService}/DataClasses/Users.cs (100%) diff --git a/ArmaForces.Boderator/ArmaForces.Boderator.BotService/Controllers/DLCController.cs b/ArmaForces.Boderator.BotService/Controllers/DLCController.cs similarity index 100% rename from ArmaForces.Boderator/ArmaForces.Boderator.BotService/Controllers/DLCController.cs rename to ArmaForces.Boderator.BotService/Controllers/DLCController.cs diff --git a/ArmaForces.Boderator/ArmaForces.Boderator.BotService/DataClasses/SingleUser.cs b/ArmaForces.Boderator.BotService/DataClasses/SingleUser.cs similarity index 100% rename from ArmaForces.Boderator/ArmaForces.Boderator.BotService/DataClasses/SingleUser.cs rename to ArmaForces.Boderator.BotService/DataClasses/SingleUser.cs diff --git a/ArmaForces.Boderator/ArmaForces.Boderator.BotService/DataClasses/Users.cs b/ArmaForces.Boderator.BotService/DataClasses/Users.cs similarity index 100% rename from ArmaForces.Boderator/ArmaForces.Boderator.BotService/DataClasses/Users.cs rename to ArmaForces.Boderator.BotService/DataClasses/Users.cs