Aeon Vex
Aeon Vex
CC#
Created by Aeon Vex on 5/1/2023 in #help
❔ Catching an error status code in HTML agility pack when using LoadFromWebAsync
I'm using HTML agility pack to request multiple web pages simultaneously. I'm struggling to see how I can safely do error handling when using LoadFromWebAsync() however, given that the returned result does not include any status code, and the only way to check the http response code is via a callback on the HtmlWeb instance or via a public property. Is there a way I can catch and handle http error codes in a thread safe way?
private HtmlWeb _htmlWeb = new HtmlWeb();

async Task<SomeData> GetDataAsync()
{
// _htmlWeb.PostResponse += ... ?
// Can the callback be used to handle status codes for these page loads?

var firstPageLoad = _htmlWeb.LoadFromWebAsync(firstUrl);
var secondPageLoad = _htmlWeb.LoadFromWebAsync(secondUrl);

await Task.WhenAll(firstPageLoad, secondPageLoad);

// can't check _htmlWeb.StatusCode here because another thread might have already modified it, or whichever page loaded first might have gotten an error code, which was then overwritten with 200 OK by the second page load

// if either returned anything but 200 OK, handle it or throw an exception...

var firstPage = await firstPageLoad;
var secondPage = await secondPageLoad;

// etc ...
}
private HtmlWeb _htmlWeb = new HtmlWeb();

async Task<SomeData> GetDataAsync()
{
// _htmlWeb.PostResponse += ... ?
// Can the callback be used to handle status codes for these page loads?

var firstPageLoad = _htmlWeb.LoadFromWebAsync(firstUrl);
var secondPageLoad = _htmlWeb.LoadFromWebAsync(secondUrl);

await Task.WhenAll(firstPageLoad, secondPageLoad);

// can't check _htmlWeb.StatusCode here because another thread might have already modified it, or whichever page loaded first might have gotten an error code, which was then overwritten with 200 OK by the second page load

// if either returned anything but 200 OK, handle it or throw an exception...

var firstPage = await firstPageLoad;
var secondPage = await secondPageLoad;

// etc ...
}
The only option I see would be to use the HtmlWeb.PostResponse callback, but I'm not sure how I could use that to cleanly handle error status codes in those specific tasks.
2 replies
CC#
Created by Aeon Vex on 9/22/2022 in #help
Implementing an inventory system with right click actions
This is really a hypothetical question as I'm not working on anything like this at the moment, but I've wonder in the past what the best way to approach this would be and haven't been able to come up with any good resources. Let's say I'm making a game, and the game design required an inventory system. The main thing this system should allow for is the following: 1. It should be able to keep track of items, naturally. For simplicity's sake, no item stacking is allowed, so singular items only. 2. Items should have actions that you can execute via a right click menu, e.g. "Consume" for consumables, "Equip" for equippables, and "Drop" for all. The first solution that comes to mind is creating a base class / interface for Item, and for each type of item having a derived type, e.g. Consumable and Equippable. These are then kept in a List<Item>, for example. The drop action could then be added to the right click menu (exact implementation not important) as it would apply to all items, and would perhaps be implemented in the Inventory class itself. With this setup, what would be the "right" way to implement actions that are specific to certain types of items? Presumably the right click menu would be populated like this:
var menu = new RightClickMenu();
menu.AddAction("Drop", () => Drop(item));
// OR
menu.AddAction(new DropAction(item));
var menu = new RightClickMenu();
menu.AddAction("Drop", () => Drop(item));
// OR
menu.AddAction(new DropAction(item));
Would it make sense to have the classes deriving from item define which actions should be added to the menu? If not, how would you go about fetching valid actions for any given Item? Alternatively, does all I've mentioned above make sense, or would an entirely different structure be preferred here? Any hints on where to start looking for this kind of design would also be appreciated.
20 replies