Having the clear locations endpoint only clear world locations, and adding endpoint to remove all items

This commit is contained in:
hz
2025-08-21 00:28:45 -05:00
parent 1e4b8463ee
commit b7c221d765
7 changed files with 194 additions and 1 deletions
+47
View File
@@ -1,10 +1,22 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using GameAuthApi.Models;
namespace GameAuthApi.Services;
public class AdminService
{
private readonly LocationService _locationService;
private readonly ItemService _itemService;
private readonly PlayerService _playerService;
public AdminService(LocationService locationService, ItemService itemService, PlayerService playerService)
{
_locationService = locationService;
_itemService = itemService;
_playerService = playerService;
}
public virtual Task<Dictionary<string, string>> GetStatusAsync()
{
var status = new Dictionary<string, string>
@@ -13,5 +25,40 @@ public class AdminService
};
return Task.FromResult(status);
}
public virtual async Task<Location> ResetLocationsAsync()
{
var worldLocations = await _locationService.GetByTypeAsync("World");
foreach (var loc in worldLocations)
{
await _itemService.DeleteByLocationAsync(loc.Id);
}
await _locationService.DeleteByTypeAsync("World");
var origin = new Location
{
LocationType = "World",
X = 0,
Y = 0,
Z = 0
};
await _locationService.CreateAsync(origin);
var players = await _playerService.GetAllAsync();
foreach (var player in players)
{
player.LocationId = origin.Id;
await _playerService.UpdateAsync(player);
await _locationService.AddPlayerToLocationAsync(origin.Id, player.Id);
}
return origin;
}
public virtual async Task ClearItemsAsync()
{
await _itemService.DeleteAllAsync();
await _locationService.ClearAllItemsAsync();
}
}
+10
View File
@@ -44,4 +44,14 @@ public class ItemService
{
await _items.DeleteOneAsync(i => i.Id == id);
}
public virtual async Task DeleteByLocationAsync(string locationId)
{
await _items.DeleteManyAsync(i => i.LocationId == locationId);
}
public virtual async Task DeleteAllAsync()
{
await _items.DeleteManyAsync(_ => true);
}
}
+17
View File
@@ -31,6 +31,17 @@ public class LocationService
public virtual async Task DeleteAsync(string id) =>
await _locations.DeleteOneAsync(loc => loc.Id == id);
public virtual async Task<List<Location>> GetByTypeAsync(string type)
{
return await _locations.Find(l => l.LocationType == type).ToListAsync();
}
public virtual async Task DeleteByTypeAsync(string type) =>
await _locations.DeleteManyAsync(l => l.LocationType == type);
public virtual async Task DeleteAllAsync() =>
await _locations.DeleteManyAsync(_ => true);
public virtual async Task UpdateAsync(Location loc) =>
await _locations.ReplaceOneAsync(l => l.Id == loc.Id, loc);
@@ -83,4 +94,10 @@ public class LocationService
var update = Builders<Location>.Update.Pull(l => l.Content, itemId);
await _locations.UpdateOneAsync(filter, update);
}
public virtual async Task ClearAllItemsAsync()
{
var update = Builders<Location>.Update.Set(l => l.Content, new List<string>());
await _locations.UpdateManyAsync(_ => true, update);
}
}
+5
View File
@@ -29,6 +29,11 @@ public class PlayerService
public virtual async Task<Player> GetByIdAsync(string id) =>
await _players.Find(p => p.Id == id).FirstOrDefaultAsync();
public virtual async Task<List<Player>> GetAllAsync()
{
return await _players.Find(_ => true).ToListAsync();
}
public virtual async Task CreateAsync(Player player) =>
await _players.InsertOneAsync(player);