Skip to content

Commit 1882a4d

Browse files
committed
feat: add game when user navigated to 404
1 parent 5a7e04c commit 1882a4d

File tree

3 files changed

+123
-15
lines changed

3 files changed

+123
-15
lines changed

src/LinkDotNet.Blog.Web/App.razor

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@
55
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"/>
66
</Found>
77
<NotFound>
8-
<LayoutView Layout="@typeof(MainLayout)">
9-
<div class="m-auto text-center">
10-
<h1 class="fs-1">404 - o((⊙﹏⊙))o</h1>
11-
<br/>
12-
<p>I really looked hard but I couldn't find the page you are looking for.</p>
13-
<p>Go back to <a href="/">safety</a></p>
14-
</div>
15-
</LayoutView>
8+
<LayoutView Layout="@typeof(MainLayout)">
9+
<ObjectNotFound></ObjectNotFound>
10+
</LayoutView>
1611
</NotFound>
1712
</Router>
1813
</CascadingAuthenticationState>
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<div class="m-auto text-center">
2+
<h1 class="fs-1">404 - o((⊙﹏⊙))o</h1>
3+
<br />
4+
<p>I really looked hard but I couldn't find the page you are looking for.</p>
5+
<p>Go back to <a href="/">safety</a></p>
6+
<hr />
7+
<h3>Play a Number Guessing Game!</h3>
8+
<p>Since you are here, why not play a number guessing game?</p>
9+
10+
@if (!isGameStarted)
11+
{
12+
<p>Select a difficulty level to start:</p>
13+
<div class="btn-group mb-3" role="group">
14+
<button class="btn btn-success" @onclick="() => StartGame(DifficultyLevel.Easy)">Easy</button>
15+
<button class="btn btn-warning" @onclick="() => StartGame(DifficultyLevel.Medium)">Medium</button>
16+
<button class="btn btn-danger" @onclick="() => StartGame(DifficultyLevel.Hard)">Hard</button>
17+
</div>
18+
}
19+
else
20+
{
21+
<p>I'm thinking of a number between 1 and @maxNumber. Can you guess it?</p>
22+
<div class="input-group mb-3 w-25 mx-auto">
23+
<input type="number" class="form-control" @bind="userGuess" min="1" max="@maxNumber" @onkeyup="HandleKeyPress" />
24+
<button class="btn btn-primary" @onclick="CheckGuess">Guess</button>
25+
</div>
26+
<p>Total guesses: <strong>@guessCount</strong></p>
27+
@if (!string.IsNullOrEmpty(message))
28+
{
29+
<p>@message</p>
30+
@if (gameDone)
31+
{
32+
<button class="btn btn-success" @onclick="ResetGame">Play Again</button>
33+
}
34+
}
35+
}
36+
</div>
37+
38+
@code {
39+
private int targetNumber;
40+
private int userGuess;
41+
private int guessCount;
42+
private int maxNumber;
43+
private string message = string.Empty;
44+
private bool gameDone = false;
45+
private bool isGameStarted = false;
46+
47+
private enum DifficultyLevel
48+
{
49+
Easy,
50+
Medium,
51+
Hard
52+
}
53+
54+
private void StartGame(DifficultyLevel difficulty)
55+
{
56+
isGameStarted = true;
57+
gameDone = false;
58+
guessCount = 0;
59+
message = string.Empty;
60+
userGuess = 0;
61+
62+
switch (difficulty)
63+
{
64+
case DifficultyLevel.Easy:
65+
maxNumber = 10;
66+
break;
67+
case DifficultyLevel.Medium:
68+
maxNumber = 100;
69+
break;
70+
case DifficultyLevel.Hard:
71+
maxNumber = 1000;
72+
break;
73+
}
74+
75+
var random = new Random();
76+
targetNumber = random.Next(1, maxNumber + 1);
77+
}
78+
79+
private void CheckGuess()
80+
{
81+
if (gameDone || !isGameStarted)
82+
return;
83+
84+
if (userGuess < targetNumber)
85+
{
86+
guessCount++;
87+
message = "Too low, try again!";
88+
}
89+
else if (userGuess > targetNumber)
90+
{
91+
guessCount++;
92+
message = "Too high, try again!";
93+
}
94+
else
95+
{
96+
guessCount++;
97+
gameDone = true;
98+
message = "🎉 Congratulations! You guessed the number!";
99+
}
100+
}
101+
102+
private void ResetGame()
103+
{
104+
isGameStarted = false;
105+
gameDone = false;
106+
message = string.Empty;
107+
userGuess = 0;
108+
guessCount = 0;
109+
}
110+
111+
private void HandleKeyPress(KeyboardEventArgs e)
112+
{
113+
if (e.Key == "Enter" && !gameDone && isGameStarted)
114+
{
115+
CheckGuess();
116+
}
117+
}
118+
}

src/LinkDotNet.Blog.Web/Features/ShowBlogPost/ShowBlogPostPage.razor

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@
2121
}
2222
else if (!isLoading && BlogPost is null)
2323
{
24-
<div class="m-auto text-center" id="no-blog-post-error">
25-
<h1 class="fs-1">404 - o((⊙﹏⊙))o</h1>
26-
<br/>
27-
<p>I really looked hard but I couldn't find the page you are looking for.</p>
28-
<p>Go back to <a href="/">safety</a></p>
29-
</div>
24+
<ObjectNotFound></ObjectNotFound>
3025
}
3126
else if (BlogPost is not null)
3227
{
@@ -87,7 +82,7 @@ else if (BlogPost is not null)
8782
</div>
8883
@if (SupportConfiguration.Value.ShowUnderBlogPost)
8984
{
90-
<DonationSection />
85+
<DonationSection />
9186
}
9287
@if (AppConfiguration.Value.ShowSimilarPosts)
9388
{

0 commit comments

Comments
 (0)