Skip to content

Commit

Permalink
Change avatar/navigator/title select method
Browse files Browse the repository at this point in the history
Add the function to change player name
  • Loading branch information
asesidaa committed Jul 7, 2022
1 parent 4cd290a commit 14261c8
Show file tree
Hide file tree
Showing 19 changed files with 610 additions and 241 deletions.
14 changes: 14 additions & 0 deletions GC-local-server-rewrite/controllers/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ public List<User> GetUsers()
return result;
}

[Route(HttpVerbs.Post, "/Users/SetPlayerName")]
public bool SetPlayerName([JsonData] User data)
{
var existing = cardSqLiteConnection.Table<Card>().Where(card => card.CardId == data.CardId);
if (!existing.Any())
{
$"Trying to update non existing user's name! Card id {data.CardId}".Warn();
return false;
}
var user = existing.First();
user.PlayerName = data.PlayerName;
return cardSqLiteConnection.Update(user) == 1;
}

[Route(HttpVerbs.Post, "/UserDetail/SetMusicFavorite")]
// ReSharper disable once UnusedMember.Global
public bool SetFavorite([JsonData] MusicFavoriteData data)
Expand Down
13 changes: 13 additions & 0 deletions MudAdmin/MudAdmin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.4" PrivateAssets="all" />
<PackageReference Include="MudBlazor" Version="6.0.11-dev.4" />
<PackageReference Include="protobuf-net" Version="3.1.17" />
</ItemGroup>

<ItemGroup>
Expand All @@ -30,6 +31,18 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</Content>
<Content Update="wwwroot\data\avatar.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</Content>
<Content Update="wwwroot\data\navigator.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</Content>
<Content Update="wwwroot\data\title.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</Content>
<Content Update="wwwroot\news.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
Expand Down
73 changes: 73 additions & 0 deletions MudAdmin/Pages/ChangePlayerNameDialog.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
@using SharedProject.common
@using System.Text.RegularExpressions
@inject HttpClient Client
@inject ILogger<ChangePlayerNameDialog> Logger

<MudDialog>
<TitleContent>
<MudText Typo="Typo.h6">
Change Player Name
</MudText>
</TitleContent>
<DialogContent>
<MudForm @bind-IsValid="IsValid">
<MudTextField Value="@Data.CardId" Label="CardId" ReadOnly="true"/>
<MudTextField @bind-Value="Data.PlayerName"
Immediate="true"
Counter="SharedConstants.MAX_PLAYER_NAME_LENGTH"
MaxLength="SharedConstants.MAX_PLAYER_NAME_LENGTH"
Validation="ValidatePlayerName"/>
</MudForm>
</DialogContent>
<DialogActions>
<MudButton OnClick="Cancel">Cancel</MudButton>
<MudButton Color="Color.Primary" OnClick="Submit" Disabled="@(!IsValid)">Confirm</MudButton>
</DialogActions>
</MudDialog>

@code {

[CascadingParameter]
MudDialogInstance MudDialog { get; set; } = null!;

[Parameter]
public SharedProject.models.User Data { get; set; } = null!;

private string originalName = string.Empty;

private bool IsValid { get; set; }

protected override void OnInitialized()
{
base.OnInitialized();
originalName = new string(Data.PlayerName);
}

async Task Submit()
{
Logger.LogInformation("Data is {cardId}, {name}", Data.CardId, Data.PlayerName);
var response = await Client.PostAsJsonAsync("api/Users/SetPlayerName", Data);
var result = await response.Content.ReadFromJsonAsync<bool>();
Logger.LogInformation("SetPlayerName result is {result}", result);
MudDialog.Close(DialogResult.Ok(result));
}

void Cancel()
{
Data.PlayerName = originalName;
MudDialog.Cancel();
}

private static string? ValidatePlayerName(string playerName)
{
const string pattern = @"^[a-zA-Z0-9!?,./\-+:<>_\\@*#&=() ]{1,8}$";

return playerName.Length switch
{
0 => "Player name cannot be empty!",
> 8 => "Player name cannot be longer than 8 characters!",
_ => !Regex.IsMatch(playerName, pattern) ? "Player name contains invalid character!" : null
};

}
}
2 changes: 1 addition & 1 deletion MudAdmin/Pages/FavoriteDialog.razor
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
[Parameter]
public long CardId { get; set; }

async void Submit()
async Task Submit()
{
var postData = new MusicFavoriteData
{
Expand Down
Loading

0 comments on commit 14261c8

Please sign in to comment.