Skip to content

Commit

Permalink
[API] Added funtionality and efficiency (#153)
Browse files Browse the repository at this point in the history
* [API] Added funtionality and efficiency

- userOauth token (with specified permissions)
- avatarinfo
  get avatars from specific user (req user auth)
  get avatars from multiple ids
  get all avatars now can get up to 500 avatars per page
  getting all neighborhood avatars is more efficient
  online avatars can also be requested as compact now (only online count)
- lotinfo
  json objects now include lot_id (more consistent/reliable value than location)
  json objects now include admit_mode
  get lots from multiple ids
  online lots can also be requested as compact now (only online count)
  added funtion for getting all the lots by pages, and max 500 lots per page

* [API] userApiFixes

- compact now works more efficient
- stress sql bandwidth fixed on getAllAvatars and GetAllLots
  • Loading branch information
Cowplant-Simmer-Collin authored and riperiperi committed Aug 19, 2019
1 parent 6651acc commit 01a08e7
Show file tree
Hide file tree
Showing 10 changed files with 467 additions and 71 deletions.
165 changes: 124 additions & 41 deletions TSOClient/FSO.Server.Api.Core/Controllers/AvatarInfoController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,37 @@ namespace FSO.Server.Api.Core.Controllers
[ApiController]
public class AvatarInfoController : ControllerBase
{
//get the avatars by user_id
[Route("userapi/user/avatars")]
public IActionResult GetByUser()
{
var api = Api.INSTANCE;
var user = api.RequireAuthentication(Request);
if (!user.Claims.Contains("userReadPermissions")) return ApiResponse.Json(HttpStatusCode.OK, new JSONAvatarError("No read premissions found."));

using (var da = api.DAFactory.Get())
{
var avatars = da.Avatars.GetByUserId(user.UserID);
List<JSONAvatar> avatarJson = new List<JSONAvatar>();
foreach (var avatar in avatars)
{
avatarJson.Add(new JSONAvatar
{
avatar_id = avatar.avatar_id,
shard_id = avatar.shard_id,
name = avatar.name,
gender = avatar.gender,
date = avatar.date,
description = avatar.description,
current_job = avatar.current_job,
mayor_nhood = avatar.mayor_nhood
});
}
var avatarsJson = new JSONAvatars();
avatarsJson.avatars = avatarJson;
return ApiResponse.Json(HttpStatusCode.OK, avatarsJson);
}
}
//get the avatar by id
[HttpGet]
[Route("userapi/avatars/{avartarId}")]
Expand Down Expand Up @@ -44,22 +75,60 @@ public IActionResult GetByID(uint avartarId)
return ApiResponse.Json(HttpStatusCode.OK, avatarJson);
}
}
//get the avatars by ids
[Route("userapi/avatars")]
public IActionResult GetByIDs([FromQuery(Name = "ids")]string idsString)
{
var api = Api.INSTANCE;
try
{
uint[] ids = Array.ConvertAll(idsString.Split(","), uint.Parse);
using (var da = api.DAFactory.Get())
{
var avatars = da.Avatars.GetMultiple(ids);
List<JSONAvatar> avatarJson = new List<JSONAvatar>();
foreach (var avatar in avatars)
{
avatarJson.Add(new JSONAvatar
{
avatar_id = avatar.avatar_id,
shard_id = avatar.shard_id,
name = avatar.name,
gender = avatar.gender,
date = avatar.date,
description = avatar.description,
current_job = avatar.current_job,
mayor_nhood = avatar.mayor_nhood
});
}
var avatarsJson = new JSONAvatars();
avatarsJson.avatars = avatarJson;
return ApiResponse.Json(HttpStatusCode.OK, avatarsJson);
}
}
catch
{
return ApiResponse.Json(HttpStatusCode.NotFound, new JSONAvatarError("Error during cast. (invalid_value)"));
}
}
//gets all the avatars from one city
[HttpGet]
[Route("userapi/city/{shardId}/avatars/page/{pageNum}")]
public IActionResult GetAll(int shardId,int pageNum)
public IActionResult GetAll(int shardId,int pageNum, [FromQuery(Name = "avatars_on_page")]int perPage)
{
var api = Api.INSTANCE;

if(perPage == 0)
{
perPage = 100;
}
if (perPage > 500) return ApiResponse.Json(HttpStatusCode.NotFound, new JSONLotError("The max amount of avatars per page is 500"));
using (var da = api.DAFactory.Get())
{
pageNum = pageNum - 1;

var avatars = da.Avatars.All(shardId);
var avatarCount = avatars.Count();
var totalPages = (avatars.Count() - 1)/100 + 1;
avatars = avatars.Skip(pageNum * 100);
avatars = avatars.Take(100);
var avatars = da.Avatars.AllByPage(shardId, pageNum * perPage, perPage,"avatar_id");
var avatarCount = avatars.Total;
var totalPages = (avatars.Total - 1)/perPage + 1;

var pageAvatarsJson = new JSONAvatarsPage();
pageAvatarsJson.total_avatars = avatarCount;
Expand Down Expand Up @@ -135,22 +204,26 @@ public IActionResult GetByNhood(int shardId, uint nhoodId)
List<JSONAvatar> avatarJson = new List<JSONAvatar>();
foreach (var lot in lots)
{
var roomies = da.Roommates.GetLotRoommates(lot.lot_id).Where(x => x.is_pending == 0).Select(x => x.avatar_id);
foreach (var roomie in roomies)
if(lot.category != FSO.Common.Enum.LotCategory.community)
{
var roomieAvatar = da.Avatars.Get(roomie);
avatarJson.Add(new JSONAvatar
var roomies = da.Roommates.GetLotRoommates(lot.lot_id).Where(x => x.is_pending == 0).Select(x => x.avatar_id);
var avatars = da.Avatars.GetMultiple(roomies.ToArray());
foreach (var avatar in avatars)
{
avatar_id = roomieAvatar.avatar_id,
shard_id = roomieAvatar.shard_id,
name = roomieAvatar.name,
gender = roomieAvatar.gender,
date = roomieAvatar.date,
description = roomieAvatar.description,
current_job = roomieAvatar.current_job,
mayor_nhood = roomieAvatar.mayor_nhood
});
avatarJson.Add(new JSONAvatar
{
avatar_id = avatar.avatar_id,
shard_id = avatar.shard_id,
name = avatar.name,
gender = avatar.gender,
date = avatar.date,
description = avatar.description,
current_job = avatar.current_job,
mayor_nhood = avatar.mayor_nhood
});
}
}

}
var avatarsJson = new JSONAvatars();
avatarsJson.avatars = avatarJson;
Expand All @@ -160,36 +233,46 @@ public IActionResult GetByNhood(int shardId, uint nhoodId)
//get all online Avatars
[HttpGet]
[Route("userapi/avatars/online")]
public IActionResult GetOnline()
public IActionResult GetOnline([FromQuery(Name = "compact")]bool compact)
{
var api = Api.INSTANCE;

using (var da = api.DAFactory.Get())
{
var avatarStatus = da.AvatarClaims.GetAllActiveAvatars();
if (avatarStatus == null) return ApiResponse.Json(HttpStatusCode.NotFound, new JSONAvatarError("Avatars not found"));


List<JSONAvatarSmall> avatarSmallJson = new List<JSONAvatarSmall>();

foreach (var avatar in avatarStatus)
var avatarJson = new JSONAvatarOnline();
if (compact)
{
uint location = 0;
if (avatar.privacy_mode == 0)
{
location = avatar.location;
}
avatarSmallJson.Add(new JSONAvatarSmall
var avatarStatus = da.AvatarClaims.GetAllActiveAvatarsCount();
if (avatarStatus == null) return ApiResponse.Json(HttpStatusCode.NotFound, new JSONAvatarError("ammount not found"));
avatarJson.avatars_online_count = avatarStatus;
}


if (!compact)
{
var avatarStatus = da.AvatarClaims.GetAllActiveAvatars();
if (avatarStatus == null) return ApiResponse.Json(HttpStatusCode.NotFound, new JSONAvatarError("Avatars not found"));

foreach (var avatar in avatarStatus)
{
avatar_id = avatar.avatar_id,
name = avatar.name,
privacy_mode = avatar.privacy_mode,
location = location
});
uint location = 0;
if (avatar.privacy_mode == 0)
{
location = avatar.location;
}
avatarSmallJson.Add(new JSONAvatarSmall
{
avatar_id = avatar.avatar_id,
name = avatar.name,
privacy_mode = avatar.privacy_mode,
location = location
});

}
avatarJson.avatars_online_count = avatarStatus.Count();
}
var avatarJson = new JSONAvatarOnline();
avatarJson.avatars_online_count = avatarStatus.Count();

avatarJson.avatars = avatarSmallJson;
return ApiResponse.Json(HttpStatusCode.OK, avatarJson);
}
Expand All @@ -213,7 +296,7 @@ public class JSONAvatarsPage
}
public class JSONAvatarOnline
{
public int avatars_online_count { get; set; }
public int? avatars_online_count { get; set; }
public List<JSONAvatarSmall> avatars { get; set; }
}
public class JSONAvatarSmall
Expand Down
Loading

0 comments on commit 01a08e7

Please sign in to comment.