From 6c989e79236f9e2bd983742d188b8215815188fa Mon Sep 17 00:00:00 2001 From: Josh Date: Thu, 4 Aug 2022 14:07:52 -0500 Subject: [PATCH] Implement story mode player counts endpoint (#400) --- .../Controllers/Slots/SlotsController.cs | 25 +++++++++++++++++++ ProjectLighthouse/Levels/Slot.cs | 19 +++++++++----- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/ProjectLighthouse.Servers.GameServer/Controllers/Slots/SlotsController.cs b/ProjectLighthouse.Servers.GameServer/Controllers/Slots/SlotsController.cs index 54e105e4..fc421375 100644 --- a/ProjectLighthouse.Servers.GameServer/Controllers/Slots/SlotsController.cs +++ b/ProjectLighthouse.Servers.GameServer/Controllers/Slots/SlotsController.cs @@ -90,6 +90,31 @@ public class SlotsController : ControllerBase return this.Ok(LbpSerializer.TaggedStringElement("slots", serialized, "total", serializedSlots.Count)); } + [HttpGet("slots/developer")] + public async Task StoryPlayers() + { + User? user = await this.database.UserFromGameRequest(this.Request); + if (user == null) return this.StatusCode(403, ""); + + GameToken? token = await this.database.GameTokenFromRequest(this.Request); + if (token == null) return this.StatusCode(403, ""); + + List activeSlotIds = RoomHelper.Rooms.Where(r => r.Slot.SlotType == SlotType.Developer).Select(r => r.Slot.SlotId).ToList(); + + List serializedSlots = new(); + + foreach (int id in activeSlotIds) + { + int placeholderSlotId = await SlotHelper.GetPlaceholderSlotId(this.database, id, SlotType.Developer); + Slot slot = await this.database.Slots.FirstAsync(s => s.SlotId == placeholderSlotId); + serializedSlots.Add(slot.SerializeDevSlot(false)); + } + + string serialized = serializedSlots.Aggregate(string.Empty, (current, slot) => current + slot); + + return this.Ok(LbpSerializer.StringElement("slots", serialized)); + } + [HttpGet("s/developer/{id:int}")] public async Task SDev(int id) { diff --git a/ProjectLighthouse/Levels/Slot.cs b/ProjectLighthouse/Levels/Slot.cs index c9491858..04a31e32 100644 --- a/ProjectLighthouse/Levels/Slot.cs +++ b/ProjectLighthouse/Levels/Slot.cs @@ -242,20 +242,27 @@ public class Slot LbpSerializer.StringElement("sizeOfResources", this.Resources.Sum(FileHelper.ResourceSize)); } - public string SerializeDevSlot() + public string SerializeDevSlot(bool includeExtras = true) { - int comments = this.database.Comments.Count(c => c.Type == CommentType.Level && c.TargetId == this.SlotId); - int photos = this.database.Photos.Count(c => c.SlotId == this.SlotId); + int comments = 0, photos = 0; + if (includeExtras) + { + comments = this.database.Comments.Count(c => c.Type == CommentType.Level && c.TargetId == this.SlotId); + photos = this.database.Photos.Count(c => c.SlotId == this.SlotId); + } + int players = RoomHelper.Rooms .Where(r => r.Slot.SlotType == SlotType.Developer && r.Slot.SlotId == this.InternalSlotId) .Sum(r => r.PlayerIds.Count); string slotData = LbpSerializer.StringElement("id", this.InternalSlotId) + - LbpSerializer.StringElement("playerCount", players) + - LbpSerializer.StringElement("commentCount", comments) + - LbpSerializer.StringElement("photoCount", photos); + LbpSerializer.StringElement("playerCount", players); + + if(includeExtras) + slotData += LbpSerializer.StringElement("commentCount", comments) + + LbpSerializer.StringElement("photoCount", photos); return LbpSerializer.TaggedStringElement("slot", slotData, "type", "developer"); }