Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature of DLC handling #51

Open
wants to merge 3 commits into
base: Boderator3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 162 additions & 0 deletions ArmaForces.Boderator.BotService/Controllers/DLCController.cs
Original file line number Diff line number Diff line change
@@ -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<SingleUser.DLC> dlcList = new List<SingleUser.DLC>();

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");
}
}
}

26 changes: 26 additions & 0 deletions ArmaForces.Boderator.BotService/DataClasses/SingleUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;

namespace ArmaForces.Boderator.BotService.DataClasses
{
public class SingleUser
{
public ulong UserID;
public List<DLC> DLCList = new List<DLC>();

public enum DLC
{
Karts,
Helicopters,
Marksman,
Apex,
Jets,
LawsOfWar,
Tanks,
Contact,
GlobalMobilization,
SOGPrairieFire,
CSLA
}

}
}
10 changes: 10 additions & 0 deletions ArmaForces.Boderator.BotService/DataClasses/Users.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;

namespace ArmaForces.Boderator.BotService.DataClasses
{
public class Users
{
public List<SingleUser> UsersList = new List<SingleUser>();

}
}