Skip to content

Commit

Permalink
Add host controls, and list of questions in output
Browse files Browse the repository at this point in the history
  • Loading branch information
MetalMichael committed May 12, 2019
1 parent 1d701cc commit 05c03db
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Blazor20Questions.Client/Modals/NewGame.razor
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
{
isError = false;
var createdGame = await Http.PostJsonAsync<GameResponse>("api/Game/Create", game);
UriHelper.NavigateTo($"/game/{createdGame.Id}");
UriHelper.NavigateTo($"/host/{createdGame.Id}");
ModalService.Close();
}
catch (HttpRequestException e)
Expand Down
80 changes: 46 additions & 34 deletions Blazor20Questions.Client/Pages/Game.razor
Original file line number Diff line number Diff line change
Expand Up @@ -28,45 +28,57 @@ else if (_game != null)
else
{
<div class="game">
<span>Remaining Questions: @_game.QuestionsRemaining</span>
</div>
@if (_game.QuestionsRemaining > 0)
{
<h2>Ask a Question...</h2>
<EditForm onsubmit="@AskQuestion">
<div class="simple-form">
<div class="form-group">
<label>
Yes or No Question
<input type="text" bind="@Question" class="form-control" id="first-name" placeholder="Is it a...?" />
</label>
<h2>Asked questions</h2>
<div class="questions">
@foreach (var question in _game.Questions)
{
<div class="question">
<span class="question-text">@question.Question</span>
<span class="question-answer">@(question.HasAnswer ? (question.Answer.Value) ? "Yes" : "No" : "")</span>
</div>
</div>
</EditForm>
}
}
</div>

@if (!_game.GuessesCountAsQuestions || _game.QuestionsRemaining > 0)
{
<h2>Make a guess</h2>
@if (_game.GuessesCountAsQuestions)
<span>Remaining Questions: @_game.QuestionsRemaining</span>

@if (_game.QuestionsRemaining > 0)
{
<span>Each guess counts as a question, so be sure not to waste an oppertunity!</span>
<h2>Ask a Question...</h2>
<EditForm onsubmit="@AskQuestion">
<div class="simple-form">
<div class="form-group">
<label>
Yes or No Question
<input type="text" bind="@Question" class="form-control" id="first-name" placeholder="Is it a...?" />
</label>
</div>
</div>
</EditForm>
}
else

@if (!_game.GuessesCountAsQuestions || _game.QuestionsRemaining > 0)
{
<span>Guesses are free, and do not count as questions. Guess away!</span>
}
<EditForm onsubmit="@MakeGuess">
<div class="simple-form">
<div class="form-group">
<label>
Guess the Subject
<input type="text" bind="@Guess" class="form-control" id="first-name" placeholder="house" />
</label>
<h2>Make a guess</h2>
@if (_game.GuessesCountAsQuestions)
{
<span>Each guess counts as a question, so be sure not to waste an oppertunity!</span>
}
else
{
<span>Guesses are free, and do not count as questions. Guess away!</span>
}
<EditForm onsubmit="@MakeGuess">
<div class="simple-form">
<div class="form-group">
<label>
Guess the Subject
<input type="text" bind="@Guess" class="form-control" id="first-name" placeholder="house" />
</label>
</div>
</div>
</div>
</EditForm>
}
</EditForm>
}
</div>
}
}
else
Expand Down Expand Up @@ -110,7 +122,7 @@ else

var content = await res.Content.ReadAsStringAsync();
_game = JsonConvert.DeserializeObject<GameResponse>(content);

}
catch (Exception e)
{
Expand Down
123 changes: 123 additions & 0 deletions Blazor20Questions.Client/Pages/Host.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
@page "/host/{Id:guid}"
@using System.Net.Http
@using Newtonsoft.Json
@using Blazor20Questions.Shared
@inject HttpClient Http
@inject IUriHelper UriHelper

<h1>20 Questions - Host Controls</h1>

<div class="link">
<div>Send this link to your friends to join in the fun!</div>
<input type="text" readonly value="@UriHelper.GetBaseUri()game/@Id" onclick="this.select()" size="80" />
</div>

@if (_error)
{
<div class="error">
Error loading game: @_errorMessage
</div>
}
else if (_game != null)
{
if (_game.Complete)
{
if (_game.Won)
{
<div class="success">The guesser(s) guessed correctly!</div>
}
else
{
<div class="fail">The guesser(s) did not guess "@_game.Subject" in time</div>
}
}
else
{
<div class="host game">
<span>Remaining Questions: @_game.QuestionsRemaining</span>

<h2>Questions</h2>
<div class="questions">
@foreach (var question in _game.Questions)
{
<div class="question">
<span class="question-text">@question.Question</span>
@if (question.HasAnswer)
{
<span class="question-answer">@((question.Answer.Value) ? "Yes" : "No")</span>
}
else
{
<span class="question-no"><button onclick="@AnswerNo">NO</button></span>
<span class="question-no"><button onclick="@AnswerYes">YES</button></span>
}

</div>
}
</div>
<span>Remaining Questions: @_game.QuestionsRemaining</span>
</div>
}
}
else
{
<div>Loading...</div>
}

@functions {

[Parameter]
public Guid Id { get; set; }

private string Question { get; set; }
private string Guess { get; set; }

private GameResponse _game;
private bool _error;
private string _errorMessage;

protected override async Task OnInitAsync()
{
try
{
_error = false;
_game = await Http.GetJsonAsync<GameResponse>($"api/Game/{Id}");
}
catch (Exception e)
{
_error = true;
_errorMessage = e.Message;
}
}

private async Task AnswerYes()
{
await AnswerQuestion(true);
}

private async Task AnswerNo()
{
await AnswerQuestion(false);
}

private async Task AnswerQuestion(bool answer)
{
try
{
var res = await Http.PostAsync($"api/Game/{Id}/answer", new StringContent(answer.ToString()));

res.EnsureSuccessStatusCode();

var content = await res.Content.ReadAsStringAsync();
_game = JsonConvert.DeserializeObject<GameResponse>(content);

}
catch (Exception e)
{
_error = true;
_errorMessage = e.Message;
}
}


}
6 changes: 6 additions & 0 deletions Blazor20Questions.Shared/GameResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

namespace Blazor20Questions.Shared
{
Expand Down Expand Up @@ -46,5 +47,10 @@ public class GameResponse
/// Only populated if game is over
/// </remarks>
public string Subject { get; set; }

/// <summary>
/// List of questions that have been asked, with answers (if given)
/// </summary>
public IList<QuestionResponse> Questions { get; set; }
}
}
20 changes: 20 additions & 0 deletions Blazor20Questions.Shared/QuestionResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Blazor20Questions.Shared
{
public class QuestionResponse
{
/// <summary>
/// The question that was asked
/// </summary>
public string Question { get; set; }

/// <summary>
/// The answer provided by the host, if any
/// </summary>
public bool? Answer { get; set; }

/// <summary>
/// Whether the question has been answered
/// </summary>
public bool HasAnswer => Answer.HasValue;
}
}

0 comments on commit 05c03db

Please sign in to comment.