Displaying a Wikipedia article page
The player has read the starting article and clicked on a link within the content. We need to add the logic for rendering the linked article. We'll have to fetch the article (or read it from the database if it was already fetched), display it, and update the game's state.
Here is the code:
const articlepage = HTTP.HandlerFunction() do req uri_parts = parseuri(req.target) game = gamesession(uri_parts[1]) article_uri = "/wiki/$(uri_parts[end])" existing_articles = Articles.find(article_uri) article = isempty(existing_articles) ? persistedarticle(fetchpage(article_uri)...) : existing_articles[1] push!(game.history, article) game.steps_taken += 1 puzzlesolved(game, article) && destroygamesession(game.id) HTTP.Messages.Response(200, wikiarticle(game, article)) end
We start by parsing the Request
URI to extract all the values sent via GET. It is a string with the format /$session_id/wiki/$article_name
, for example...