C
C#6mo ago
dynastic

Design pattern for dynamic content for a browser game?

This is a bit of a niche question, so sorry if this isn’t much use to anyone else! I'm working on a webpage-based game. The idea is that the game is played by navigating through pages with different content on them. These are called "Scenes," and their metadata is stored in a database. Each Scene should have a list of Actions, and executing an Action returns a new scene to travel to, restarting the loop. I successfully have a database model for a Scene, but it's not much. Right now, it only has a Guid and a Name. So, to explicitly define it, a Scene should be able to: - Read game data, such as the active Pet and user. - Be viewable by a Razor page - Keep track of some sort of state instance (probably a simple string dictionary) - Have a few actions that the user can take by interacting with the page, such as through links and buttons - Optionally provide a URI for a new scene to go to I came up with several options: 1. For every scene, create a class and Razor page. Then, create classes for every action. In the DB, it could store the class name of the associated Scene (i.e. “sceneType”: “PetCareScene”). This could get unwieldy fast, because there are going to be quest lines with dozens of scenes. There could be hundreds of files in the codebase. They could be moved to another project(s) to mitigate this 2. Create some kind of serializable system for creating scenes and actions dynamically, and store this data in the database. This sounds like OVERKILL and id rather not lol. It would have to account for anything someone would want to do in a scene/action, without compromising security by being able to run arbitrary code. UPDATE: I am thinking this might be more viable if I were to have something like this:
public class ActionResult
{
public Guid? NextSceneId { get; set; }
public GameState UpdatedGameState { get; set; }
public string Message { get; set; }
}
public class Action
{
public Guid Id { get; set; }
// ...etc...
public string ActionType { get; set; } // e.g., "ChangeScene", "ModifyState", "Composite"
public Dictionary<string, string> Parameters { get; set; }
}
public class ActionResult
{
public Guid? NextSceneId { get; set; }
public GameState UpdatedGameState { get; set; }
public string Message { get; set; }
}
public class Action
{
public Guid Id { get; set; }
// ...etc...
public string ActionType { get; set; } // e.g., "ChangeScene", "ModifyState", "Composite"
public Dictionary<string, string> Parameters { get; set; }
}
And then do something along the lines of this, in some kind of manager/executor class.
switch (action.ActionType)
{
case "ChangeScene":
return new ActionResult
{
NextSceneId = Guid.Parse(action.Parameters["NextSceneId"]),
UpdatedGameState = gameState
};
// ...etc...
}
switch (action.ActionType)
{
case "ChangeScene":
return new ActionResult
{
NextSceneId = Guid.Parse(action.Parameters["NextSceneId"]),
UpdatedGameState = gameState
};
// ...etc...
}
3. Ditch the action system. Only keep track of the current scene URI. All scene/action logic is now handled by the Razor page code block, interacting with some kind of service to read/write game data. This would probably be the simplest and least pigeonholed option, but I worry that it may not scale well? I am leaning toward this option. Does anyone have any thoughts? I feel like none of these options are ideal, and would love to hear some other options. Thanks :)
3 Replies
Anton
Anton6mo ago
1. Doesn't sound like it won't scale. Sounds pretty reasonable. 2. Don't do this unless you need to do this. 3. That system is good for being able to record and replay things. But if you want that, your design probably won't work in the first place, because your game logic seems to be coupled to the web UI.
dynastic
dynasticOP6mo ago
I definitely agree about 2, and 3 is a really good point that I didn't consider. I think i'm trying to achieve something as decoupled from UI as possible, just using the pages as a means of interacting with the data. Soooo sounds like option 1 is the best way to go in that case? Also it may be a lot of files, but it's very explicit and doesn't split everything up.
HtmlCompiler
HtmlCompiler6mo ago
so is this a web version of a gamebook?
For every scene, create a class and Razor page Create some kind of serializable system for creating scenes
isn't every scene its own object anyway? with its text, its state in general, and such? i guess actions could be reused, but with scene it would happen rarely? or would parts of the scenes be reused because you are building them dynamically with the state of the game
Want results from more Discord servers?
Add your server