Files
wwdpublic/Content.Server/Store/Systems/StoreSystem.Listings.cs
SimpleStation14 b1e3b79253 Mirror: Obsolete Logger cleanup for EntitySystems (#132)
## Mirror of PR #25941: [Obsolete `Logger` cleanup for
`EntitySystem`s](https://github.com/space-wizards/space-station-14/pull/25941)
from <img src="https://avatars.githubusercontent.com/u/10567778?v=4"
alt="space-wizards" width="22"/>
[space-wizards](https://github.com/space-wizards)/[space-station-14](https://github.com/space-wizards/space-station-14)

###### `aafe81512258b5a80776ada1f471b58e7507ca2d`

PR opened by <img
src="https://avatars.githubusercontent.com/u/27449516?v=4"
width="16"/><a href="https://github.com/LordCarve"> LordCarve</a> at
2024-03-09 12:19:14 UTC
PR merged by <img
src="https://avatars.githubusercontent.com/u/19864447?v=4"
width="16"/><a href="https://github.com/web-flow"> web-flow</a> at
2024-03-10 00:15:13 UTC

---

PR changed 25 files with 41 additions and 45 deletions.

The PR had the following labels:
- Status: Needs Review


---

<details open="true"><summary><h1>Original Body</h1></summary>

> <!-- Please read these guidelines before opening your PR:
https://docs.spacestation14.io/en/getting-started/pr-guideline -->
> <!-- The text between the arrows are comments - they will not be
visible on your PR. -->
> 
> ## About the PR
> <!-- What did you change in this PR? -->
> Changed almost all[^1] obsolete `Logger` calls in Content's
`EntitySystem`s to use `Log` instead. `Log` automatically selects the
system-specific `Sawmill`, so this isn't just a refactor - it makes logs
slightly more accurate (puts them in appropriate sawmill/context rather
than the root sawmill).
> 
> ## Why / Balance
> <!-- Why was it changed? Link any discussions or issues here. Please
discuss how this would affect game balance. -->
> Using `Logger` directly for logging is marked obsolete. Assumed this
is a desired change.
> 
> ## Technical details
> <!-- If this is a code change, summarize at high level how your new
code works. This makes it easier to review. -->
> This changes some log contexts, but generally in a desirable way:
> - For most, it put logs in `system.appropriate` `Sawmill` rather than
root.
> - For some that were forced into another `Sawmill` it changes it to a
more specific one, i.e. from `atmos` it becomes `system.gas_filter` or
`system.automatic_atmos` and `system.station` into
`system.station_jobs`.
> - For the rest it remains unchanged
> 
> **I assumed that all of the above was desirable because this seems to
be the standard convention** - I imagine that was the idea behind the
`Log` in all `EntitySystem`s coupled with `[Obsolete] Logger` methods -
**but if my assumptions are incorrect and/or there are exceptions,
please let me know and I will adjust.**
> 
> [^1]: There is only one `EntitySystem` that I didn't update -
`ExamineSystemShared`. That is because the `Logger` is in a `static`
method and refactoring that away causes a lot of cascading changes. May
do it as a separate PR to avoid overly diluting this one.
> 
> ## Media
> <!-- 
> PRs which make ingame changes (adding clothing, items, new features,
etc) are required to have media attached that showcase the changes.
> Small fixes/refactors are exempt.
> Any media may be used in SS14 progress reports, with clear credit
given.
> 
> If you're unsure whether your PR will require media, ask a maintainer.
> 
> Check the box below to confirm that you have in fact seen this (put an
X in the brackets, like [X]):
> -->
> 
> - [X] I have added screenshots/videos to this PR showcasing its
changes ingame, **or** this PR does not require an ingame showcase
> 
> ## Breaking changes
> <!--
> List any breaking changes, including namespace, public
class/method/field changes, prototype renames; and provide instructions
for fixing them. This will be pasted in #codebase-changes.
> -->
> Log output changes. `EntitySystem` logs now go to the expected
`Sawmill` for the system rather than hardcoded/root one.
> Any log analyzers anticipating these logs' context must be updated.


</details>

Co-authored-by: LordCarve <27449516+LordCarve@users.noreply.github.com>
2024-05-04 19:54:48 -04:00

130 lines
4.6 KiB
C#

using Content.Server.Store.Components;
using Content.Shared.Store;
namespace Content.Server.Store.Systems;
public sealed partial class StoreSystem
{
/// <summary>
/// Refreshes all listings on a store.
/// Do not use if you don't know what you're doing.
/// </summary>
/// <param name="component">The store to refresh</param>
public void RefreshAllListings(StoreComponent component)
{
component.Listings = GetAllListings();
}
/// <summary>
/// Gets all listings from a prototype.
/// </summary>
/// <returns>All the listings</returns>
public HashSet<ListingData> GetAllListings()
{
var allListings = _proto.EnumeratePrototypes<ListingPrototype>();
var allData = new HashSet<ListingData>();
foreach (var listing in allListings)
{
allData.Add((ListingData) listing.Clone());
}
return allData;
}
/// <summary>
/// Adds a listing from an Id to a store
/// </summary>
/// <param name="component">The store to add the listing to</param>
/// <param name="listingId">The id of the listing</param>
/// <returns>Whetehr or not the listing was added successfully</returns>
public bool TryAddListing(StoreComponent component, string listingId)
{
if (!_proto.TryIndex<ListingPrototype>(listingId, out var proto))
{
Log.Error("Attempted to add invalid listing.");
return false;
}
return TryAddListing(component, proto);
}
/// <summary>
/// Adds a listing to a store
/// </summary>
/// <param name="component">The store to add the listing to</param>
/// <param name="listing">The listing</param>
/// <returns>Whether or not the listing was add successfully</returns>
public bool TryAddListing(StoreComponent component, ListingData listing)
{
return component.Listings.Add(listing);
}
/// <summary>
/// Gets the available listings for a store
/// </summary>
/// <param name="buyer">Either the account owner, user, or an inanimate object (e.g., surplus bundle)</param>
/// <param name="store"></param>
/// <param name="component">The store the listings are coming from.</param>
/// <returns>The available listings.</returns>
public IEnumerable<ListingData> GetAvailableListings(EntityUid buyer, EntityUid store, StoreComponent component)
{
return GetAvailableListings(buyer, component.Listings, component.Categories, store);
}
/// <summary>
/// Gets the available listings for a user given an overall set of listings and categories to filter by.
/// </summary>
/// <param name="buyer">Either the account owner, user, or an inanimate object (e.g., surplus bundle)</param>
/// <param name="listings">All of the listings that are available. If null, will just get all listings from the prototypes.</param>
/// <param name="categories">What categories to filter by.</param>
/// <param name="storeEntity">The physial entity of the store. Can be null.</param>
/// <returns>The available listings.</returns>
public IEnumerable<ListingData> GetAvailableListings(EntityUid buyer, HashSet<ListingData>? listings, HashSet<string> categories, EntityUid? storeEntity = null)
{
listings ??= GetAllListings();
foreach (var listing in listings)
{
if (!ListingHasCategory(listing, categories))
continue;
if (listing.Conditions != null)
{
var args = new ListingConditionArgs(buyer, storeEntity, listing, EntityManager);
var conditionsMet = true;
foreach (var condition in listing.Conditions)
{
if (!condition.Condition(args))
{
conditionsMet = false;
break;
}
}
if (!conditionsMet)
continue;
}
yield return listing;
}
}
/// <summary>
/// Checks if a listing appears in a list of given categories
/// </summary>
/// <param name="listing">The listing itself.</param>
/// <param name="categories">The categories to check through.</param>
/// <returns>If the listing was present in one of the categories.</returns>
public bool ListingHasCategory(ListingData listing, HashSet<string> categories)
{
foreach (var cat in categories)
{
if (listing.Categories.Contains(cat))
return true;
}
return false;
}
}