├── .gitattributes ├── .gitignore ├── BlazorBattles.sln └── BlazorBattles ├── Client ├── App.razor ├── BlazorBattles.Client.csproj ├── CustomAuthStateProvider.cs ├── Pages │ ├── Army.razor │ ├── BattleLog.razor │ ├── Build.razor │ ├── Counter.razor │ ├── Counter.razor.cs │ ├── FetchData.razor │ ├── History.razor │ ├── Index.razor │ ├── Leaderboard.razor │ ├── Register.razor │ └── Register.razor.css ├── Program.cs ├── Properties │ └── launchSettings.json ├── Services │ ├── AuthService.cs │ ├── BananaService.cs │ ├── BattleService.cs │ ├── IAuthService.cs │ ├── IBananaService.cs │ ├── IBattleService.cs │ ├── ILeaderboardService.cs │ ├── IUnitService.cs │ ├── LeaderboardService.cs │ └── UnitService.cs ├── Shared │ ├── AddBananas.razor │ ├── Login.razor │ ├── MainLayout.razor │ ├── MainLayout.razor.css │ ├── NavMenu.razor │ ├── NavMenu.razor.css │ ├── SurveyPrompt.razor │ ├── TopMenu.razor │ └── TopMenu.razor.css ├── _Imports.razor └── wwwroot │ ├── css │ ├── app.css │ ├── bootstrap │ │ ├── bootstrap.min.css │ │ └── bootstrap.min.css.map │ └── open-iconic │ │ ├── FONT-LICENSE │ │ ├── ICON-LICENSE │ │ ├── README.md │ │ └── font │ │ ├── css │ │ └── open-iconic-bootstrap.min.css │ │ └── fonts │ │ ├── open-iconic.eot │ │ ├── open-iconic.otf │ │ ├── open-iconic.svg │ │ ├── open-iconic.ttf │ │ └── open-iconic.woff │ ├── favicon.ico │ ├── icons │ ├── archer.png │ ├── axe.png │ ├── banana.png │ ├── knight.png │ └── mage.png │ └── index.html ├── Server ├── BlazorBattles.Server.csproj ├── Controllers │ ├── AuthController.cs │ ├── BattleController.cs │ ├── UnitController.cs │ ├── UserController.cs │ ├── UserUnitController.cs │ └── WeatherForecastController.cs ├── Data │ ├── AuthRepository.cs │ ├── DataContext.cs │ └── IAuthRepository.cs ├── Migrations │ ├── 20210511193816_Initial.Designer.cs │ ├── 20210511193816_Initial.cs │ ├── 20210512173123_UserModel.Designer.cs │ ├── 20210512173123_UserModel.cs │ ├── 20210513084421_UserUnits.Designer.cs │ ├── 20210513084421_UserUnits.cs │ ├── 20210514081412_UserBattleStatistics.Designer.cs │ ├── 20210514081412_UserBattleStatistics.cs │ ├── 20210514195306_Battles.Designer.cs │ ├── 20210514195306_Battles.cs │ └── DataContextModelSnapshot.cs ├── Pages │ ├── Error.cshtml │ └── Error.cshtml.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── Services │ ├── IUtilityService.cs │ └── UtilityService.cs ├── Startup.cs ├── appsettings.Development.json └── appsettings.json └── Shared ├── Battle.cs ├── BattleHistoryEntry.cs ├── BattleResult.cs ├── BlazorBattles.Shared.csproj ├── ServiceResponse.cs ├── Unit.cs ├── User.cs ├── UserLogin.cs ├── UserRegister.cs ├── UserStatistic.cs ├── UserUnit.cs ├── UserUnitResponse.cs └── WeatherForecast.cs /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Oo]ut/ 33 | [Ll]og/ 34 | [Ll]ogs/ 35 | 36 | # Visual Studio 2015/2017 cache/options directory 37 | .vs/ 38 | # Uncomment if you have tasks that create the project's static files in wwwroot 39 | #wwwroot/ 40 | 41 | # Visual Studio 2017 auto generated files 42 | Generated\ Files/ 43 | 44 | # MSTest test Results 45 | [Tt]est[Rr]esult*/ 46 | [Bb]uild[Ll]og.* 47 | 48 | # NUnit 49 | *.VisualState.xml 50 | TestResult.xml 51 | nunit-*.xml 52 | 53 | # Build Results of an ATL Project 54 | [Dd]ebugPS/ 55 | [Rr]eleasePS/ 56 | dlldata.c 57 | 58 | # Benchmark Results 59 | BenchmarkDotNet.Artifacts/ 60 | 61 | # .NET Core 62 | project.lock.json 63 | project.fragment.lock.json 64 | artifacts/ 65 | 66 | # ASP.NET Scaffolding 67 | ScaffoldingReadMe.txt 68 | 69 | # StyleCop 70 | StyleCopReport.xml 71 | 72 | # Files built by Visual Studio 73 | *_i.c 74 | *_p.c 75 | *_h.h 76 | *.ilk 77 | *.meta 78 | *.obj 79 | *.iobj 80 | *.pch 81 | *.pdb 82 | *.ipdb 83 | *.pgc 84 | *.pgd 85 | *.rsp 86 | *.sbr 87 | *.tlb 88 | *.tli 89 | *.tlh 90 | *.tmp 91 | *.tmp_proj 92 | *_wpftmp.csproj 93 | *.log 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio LightSwitch build output 298 | **/*.HTMLClient/GeneratedArtifacts 299 | **/*.DesktopClient/GeneratedArtifacts 300 | **/*.DesktopClient/ModelManifest.xml 301 | **/*.Server/GeneratedArtifacts 302 | **/*.Server/ModelManifest.xml 303 | _Pvt_Extensions 304 | 305 | # Paket dependency manager 306 | .paket/paket.exe 307 | paket-files/ 308 | 309 | # FAKE - F# Make 310 | .fake/ 311 | 312 | # CodeRush personal settings 313 | .cr/personal 314 | 315 | # Python Tools for Visual Studio (PTVS) 316 | __pycache__/ 317 | *.pyc 318 | 319 | # Cake - Uncomment if you are using it 320 | # tools/** 321 | # !tools/packages.config 322 | 323 | # Tabs Studio 324 | *.tss 325 | 326 | # Telerik's JustMock configuration file 327 | *.jmconfig 328 | 329 | # BizTalk build output 330 | *.btp.cs 331 | *.btm.cs 332 | *.odx.cs 333 | *.xsd.cs 334 | 335 | # OpenCover UI analysis results 336 | OpenCover/ 337 | 338 | # Azure Stream Analytics local run output 339 | ASALocalRun/ 340 | 341 | # MSBuild Binary and Structured Log 342 | *.binlog 343 | 344 | # NVidia Nsight GPU debugger configuration file 345 | *.nvuser 346 | 347 | # MFractors (Xamarin productivity tool) working folder 348 | .mfractor/ 349 | 350 | # Local History for Visual Studio 351 | .localhistory/ 352 | 353 | # BeatPulse healthcheck temp database 354 | healthchecksdb 355 | 356 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 357 | MigrationBackup/ 358 | 359 | # Ionide (cross platform F# VS Code tools) working folder 360 | .ionide/ 361 | 362 | # Fody - auto-generated XML schema 363 | FodyWeavers.xsd -------------------------------------------------------------------------------- /BlazorBattles.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.31205.134 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorBattles.Server", "BlazorBattles\Server\BlazorBattles.Server.csproj", "{9408C4CF-2BDD-4B27-880D-4138DEF78D6B}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorBattles.Client", "BlazorBattles\Client\BlazorBattles.Client.csproj", "{90703545-2073-4FCD-90AA-7653A8E046E4}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorBattles.Shared", "BlazorBattles\Shared\BlazorBattles.Shared.csproj", "{48AB7A4E-B7BC-4C20-8C9D-888C0AAEDBCC}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Any CPU = Debug|Any CPU 15 | Release|Any CPU = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {9408C4CF-2BDD-4B27-880D-4138DEF78D6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {9408C4CF-2BDD-4B27-880D-4138DEF78D6B}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {9408C4CF-2BDD-4B27-880D-4138DEF78D6B}.Release|Any CPU.ActiveCfg = Release|Any CPU 21 | {9408C4CF-2BDD-4B27-880D-4138DEF78D6B}.Release|Any CPU.Build.0 = Release|Any CPU 22 | {90703545-2073-4FCD-90AA-7653A8E046E4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {90703545-2073-4FCD-90AA-7653A8E046E4}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {90703545-2073-4FCD-90AA-7653A8E046E4}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {90703545-2073-4FCD-90AA-7653A8E046E4}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {48AB7A4E-B7BC-4C20-8C9D-888C0AAEDBCC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {48AB7A4E-B7BC-4C20-8C9D-888C0AAEDBCC}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {48AB7A4E-B7BC-4C20-8C9D-888C0AAEDBCC}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {48AB7A4E-B7BC-4C20-8C9D-888C0AAEDBCC}.Release|Any CPU.Build.0 = Release|Any CPU 30 | EndGlobalSection 31 | GlobalSection(SolutionProperties) = preSolution 32 | HideSolutionNode = FALSE 33 | EndGlobalSection 34 | GlobalSection(ExtensibilityGlobals) = postSolution 35 | SolutionGuid = {70EFBC99-31D1-4D28-A5E5-1E3DD149B0A9} 36 | EndGlobalSection 37 | EndGlobal 38 | -------------------------------------------------------------------------------- /BlazorBattles/Client/App.razor: -------------------------------------------------------------------------------- 1 | @using BlazorBattles.Client.Shared; 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | > 11 | 12 | 13 | 14 |

Sorry, there's nothing at this address.

15 |
16 |
17 |
18 |
-------------------------------------------------------------------------------- /BlazorBattles/Client/BlazorBattles.Client.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /BlazorBattles/Client/CustomAuthStateProvider.cs: -------------------------------------------------------------------------------- 1 | using BlazorBattles.Client.Services; 2 | using Blazored.LocalStorage; 3 | using Microsoft.AspNetCore.Components.Authorization; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Net.Http; 8 | using System.Net.Http.Headers; 9 | using System.Security.Claims; 10 | using System.Text.Json; 11 | using System.Threading.Tasks; 12 | 13 | namespace BlazorBattles.Client 14 | { 15 | public class CustomAuthStateProvider : AuthenticationStateProvider 16 | { 17 | private readonly ILocalStorageService _localStorageService; 18 | private readonly HttpClient _http; 19 | private readonly IBananaService _bananaService; 20 | 21 | public CustomAuthStateProvider(ILocalStorageService localStorageService, HttpClient http, 22 | IBananaService bananaService) 23 | { 24 | _localStorageService = localStorageService; 25 | _http = http; 26 | _bananaService = bananaService; 27 | } 28 | 29 | public override async Task GetAuthenticationStateAsync() 30 | { 31 | string authToken = await _localStorageService.GetItemAsStringAsync("authToken"); 32 | 33 | var identity = new ClaimsIdentity(); 34 | _http.DefaultRequestHeaders.Authorization = null; 35 | 36 | if (!string.IsNullOrEmpty(authToken)) 37 | { 38 | try 39 | { 40 | identity = new ClaimsIdentity(ParseClaimsFromJwt(authToken), "jwt"); 41 | _http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken.Replace("\"", "")); 42 | await _bananaService.GetBananas(); 43 | } 44 | catch (Exception) 45 | { 46 | await _localStorageService.RemoveItemAsync("authToken"); 47 | identity = new ClaimsIdentity(); 48 | } 49 | } 50 | 51 | var user = new ClaimsPrincipal(identity); 52 | var state = new AuthenticationState(user); 53 | 54 | NotifyAuthenticationStateChanged(Task.FromResult(state)); 55 | 56 | return state; 57 | } 58 | 59 | private byte[] ParseBase64WithoutPadding(string base64) 60 | { 61 | switch (base64.Length % 4) 62 | { 63 | case 2: base64 += "=="; break; 64 | case 3: base64 += "="; break; 65 | } 66 | return Convert.FromBase64String(base64); 67 | } 68 | 69 | private IEnumerable ParseClaimsFromJwt(string jwt) 70 | { 71 | var payload = jwt.Split('.')[1]; 72 | var jsonBytes = ParseBase64WithoutPadding(payload); 73 | var keyValuePairs = JsonSerializer.Deserialize>(jsonBytes); 74 | var claims = keyValuePairs.Select(kvp => new Claim(kvp.Key, kvp.Value.ToString())); 75 | 76 | return claims; 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Pages/Army.razor: -------------------------------------------------------------------------------- 1 | @page "/army" 2 | @inject IUnitService UnitService 3 | @attribute [Authorize] 4 | 5 |

My Army

6 | 7 | @if (UnitService.Units.Count > 0) 8 | { 9 | 10 | @foreach (var userUnit in UnitService.MyUnits) 11 | { 12 | 13 | 14 | 15 | 23 | 26 | 29 | 30 | } 31 |
16 | @switch (userUnit.UnitId) 17 | { 18 | case 1:break; 19 | case 2: break; 20 | case 3: break; 21 | } 22 | 24 | @UnitService.Units.First(unit => unit.Id == userUnit.UnitId).Title 25 | 27 | @userUnit.HitPoints HP 28 |
32 |
33 | 34 |
35 | } 36 | else 37 | { 38 | Loading Units... 39 | } 40 | 41 | @code { 42 | protected override async Task OnInitializedAsync() 43 | { 44 | await UnitService.LoadUnitsAsync(); 45 | await UnitService.LoadUserUnitsAsync(); 46 | } 47 | 48 | private async Task ReviveArmy() 49 | { 50 | await UnitService.ReviveArmy(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Pages/BattleLog.razor: -------------------------------------------------------------------------------- 1 | @page "/battlelog" 2 | @inject IBattleService BattleService 3 | 4 |

Battle Log

5 | 6 |
    7 | @foreach (var round in BattleService.LastBattle.Log) 8 | { 9 |
  • @round
  • 10 | } 11 |
12 | 13 | @code { 14 | string GetClass(string round) 15 | { 16 | if (round.Contains("kills")) 17 | return "list-group-item list-group-item-danger"; 18 | else 19 | return "list-group-item"; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Pages/Build.razor: -------------------------------------------------------------------------------- 1 | @page "/build" 2 | @inject IBananaService BananaService 3 | @inject IUnitService UnitService 4 | @inject IToastService ToastService 5 | @attribute [Authorize] 6 | 7 |

Build

8 | 9 |
10 | 16 |
17 | 18 | 19 | 20 | @if (needMoreBananas) 21 | { 22 |
23 | Not enough bananas! :( 24 |
25 | } 26 | 27 | @code { 28 | int selectedUnitId = 1; 29 | bool needMoreBananas = false; 30 | 31 | protected override async Task OnInitializedAsync() 32 | { 33 | await UnitService.LoadUnitsAsync(); 34 | } 35 | 36 | public async Task BuildUnit() 37 | { 38 | await UnitService.AddUnit(selectedUnitId); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Pages/Counter.razor: -------------------------------------------------------------------------------- 1 | @page "/counter" 2 | 3 |

Counter

4 | 5 |

Current count: @currentCount

6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Pages/Counter.razor.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Components; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | 7 | namespace BlazorBattles.Client.Pages 8 | { 9 | public partial class Counter : ComponentBase 10 | { 11 | private int currentCount = 0; 12 | 13 | private void IncrementCount() 14 | { 15 | currentCount += 2; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Pages/FetchData.razor: -------------------------------------------------------------------------------- 1 | @page "/fetchdata" 2 | @using BlazorBattles.Shared 3 | @inject HttpClient Http 4 | 5 |

Weather forecast

6 | 7 |

This component demonstrates fetching data from the server.

8 | 9 | @if (forecasts == null) 10 | { 11 |

Loading...

12 | } 13 | else 14 | { 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | @foreach (var forecast in forecasts) 26 | { 27 | 28 | 29 | 30 | 31 | 32 | 33 | } 34 | 35 |
DateTemp. (C)Temp. (F)Summary
@forecast.Date.ToShortDateString()@forecast.TemperatureC@forecast.TemperatureF@forecast.Summary
36 | } 37 | 38 | @code { 39 | private WeatherForecast[] forecasts; 40 | 41 | protected override async Task OnInitializedAsync() 42 | { 43 | forecasts = await Http.GetFromJsonAsync("WeatherForecast"); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Pages/History.razor: -------------------------------------------------------------------------------- 1 | @page "/history" 2 | @inject IBattleService BattleService 3 | 4 |

History

5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | @foreach (var battle in BattleService.History) 19 | { 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | } 29 | 30 |
DateAttackerOpponentRoundsDamage
@battle.BattleDate@battle.AttackerName@battle.OpponentName@battle.RoundsFought@battle.WinnerDamageDealt@(battle.YouWon ? "You won!" : "You lost!")
31 | 32 | @code { 33 | protected override async Task OnInitializedAsync() 34 | { 35 | await BattleService.GetHistory(); 36 | } 37 | 38 | string GetHistoryStyle(BattleHistoryEntry entry) 39 | { 40 | if (entry.YouWon) 41 | return "color: green; font-weight: 600;"; 42 | else 43 | return string.Empty; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Pages/Index.razor: -------------------------------------------------------------------------------- 1 | @page "/" 2 | 3 | 4 | 5 |

Hi, @context.User.Identity.Name. Welcome back!

6 | Let's admire your beloved army. 7 |
8 | 9 |

Welcome to Blazor Battles!

10 | 11 |
12 |
13 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Pages/Leaderboard.razor: -------------------------------------------------------------------------------- 1 | @page "/leaderboard" 2 | @inject ILeaderboardService LeaderboardService 3 | @inject AuthenticationStateProvider AuthStateProvider 4 | @inject IBattleService BattleService 5 | @inject IBananaService BananaService 6 | @inject IToastService ToastService 7 | @inject NavigationManager NavigationManager 8 | 9 |

Leaderboard

10 | 11 | @if (LeaderboardService.Leaderboard == null) 12 | { 13 | Loading Leaderboard... 14 | } 15 | else 16 | { 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | @foreach (var entry in LeaderboardService.Leaderboard) 30 | { 31 | 32 | 33 | 34 | 35 | 36 | 37 | 45 | 46 | } 47 | 48 |
#UserVictoriesDefeatsBattles
@entry.Rank@entry.Username@entry.Victories@entry.Defeats@entry.Battles 38 | @if (entry.UserId != myUserId) 39 | { 40 | 43 | } 44 |
49 | } 50 | 51 | 52 | @code { 53 | int myUserId; 54 | 55 | protected override async Task OnInitializedAsync() 56 | { 57 | await LeaderboardService.GetLeaderboard(); 58 | 59 | var authState = await AuthStateProvider.GetAuthenticationStateAsync(); 60 | myUserId = int.Parse(authState.User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier).Value); 61 | } 62 | 63 | public string GetMyStyle(int userId) 64 | { 65 | if (userId == myUserId) 66 | return "color: green; font-weight: 600;"; 67 | else 68 | return string.Empty; 69 | } 70 | 71 | public async Task StartBattle(int opponentId) 72 | { 73 | Console.WriteLine($"StartBattle with {opponentId}."); 74 | var result = await BattleService.StartBattle(opponentId); 75 | if (result.RoundsFought <= 0) 76 | ToastService.ShowInfo("The battle did not take place."); 77 | else if (result.IsVictory) 78 | ToastService.ShowSuccess("You won the battle!"); 79 | else 80 | ToastService.ShowWarning("You have been destroyed!"); 81 | 82 | await LeaderboardService.GetLeaderboard(); 83 | await BananaService.GetBananas(); 84 | 85 | if (result.RoundsFought > 0) 86 | NavigationManager.NavigateTo("battlelog"); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Pages/Register.razor: -------------------------------------------------------------------------------- 1 | @page "/register" 2 | @inject IUnitService UnitService 3 | @inject NavigationManager NavigationManager 4 | @inject IAuthService AuthService 5 | @inject IToastService ToastService 6 | 7 |

Register

8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 |
16 |
17 | 18 | 19 | 20 |
21 |
22 | 23 | 24 | 25 |
26 |
27 | 28 | 29 | 30 |
31 |
32 | 33 | 34 |
35 |
36 |
37 | 38 | @foreach (var unit in UnitService.Units) 39 | { 40 | @unit.Title
41 | } 42 |
43 |
44 |
45 | 46 | 47 | 48 |
49 |
50 | 51 | 52 |
53 |
54 | 55 | 56 | 57 |
58 | 59 |
60 | 61 | @code { 62 | UserRegister user = new UserRegister(); 63 | 64 | protected override async Task OnInitializedAsync() 65 | { 66 | await UnitService.LoadUnitsAsync(); 67 | } 68 | 69 | async void HandleRegistration() 70 | { 71 | var result = await AuthService.Register(user); 72 | if (result.Success) 73 | { 74 | ToastService.ShowSuccess(result.Message); 75 | NavigationManager.NavigateTo("/"); 76 | } 77 | else 78 | { 79 | ToastService.ShowError(result.Message); 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Pages/Register.razor.css: -------------------------------------------------------------------------------- 1 | .required::after { 2 | content: " *"; 3 | font-weight: bold; 4 | color: red; 5 | } 6 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Program.cs: -------------------------------------------------------------------------------- 1 | using BlazorBattles.Client.Services; 2 | using Microsoft.AspNetCore.Components.WebAssembly.Hosting; 3 | using Microsoft.Extensions.Configuration; 4 | using Microsoft.Extensions.DependencyInjection; 5 | using Microsoft.Extensions.Logging; 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Net.Http; 9 | using System.Text; 10 | using System.Threading.Tasks; 11 | using Blazored.Toast; 12 | using Microsoft.AspNetCore.Components.Authorization; 13 | using Blazored.LocalStorage; 14 | 15 | namespace BlazorBattles.Client 16 | { 17 | public class Program 18 | { 19 | public static async Task Main(string[] args) 20 | { 21 | var builder = WebAssemblyHostBuilder.CreateDefault(args); 22 | builder.RootComponents.Add("#app"); 23 | 24 | builder.Services.AddBlazoredToast(); 25 | builder.Services.AddBlazoredLocalStorage(); 26 | builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); 27 | builder.Services.AddScoped(); 28 | builder.Services.AddScoped(); 29 | builder.Services.AddScoped(); 30 | builder.Services.AddOptions(); 31 | builder.Services.AddAuthorizationCore(); 32 | builder.Services.AddScoped(); 33 | builder.Services.AddScoped(); 34 | builder.Services.AddScoped(); 35 | 36 | await builder.Build().RunAsync(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:62414", 7 | "sslPort": 44394 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "BlazorBattles": { 20 | "commandName": "Project", 21 | "dotnetRunMessages": "true", 22 | "launchBrowser": true, 23 | "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", 24 | "applicationUrl": "https://localhost:5001;http://localhost:5000", 25 | "environmentVariables": { 26 | "ASPNETCORE_ENVIRONMENT": "Development" 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Services/AuthService.cs: -------------------------------------------------------------------------------- 1 | using BlazorBattles.Shared; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Net.Http; 6 | using System.Net.Http.Json; 7 | using System.Threading.Tasks; 8 | 9 | namespace BlazorBattles.Client.Services 10 | { 11 | public class AuthService : IAuthService 12 | { 13 | private readonly HttpClient _http; 14 | 15 | public AuthService(HttpClient http) 16 | { 17 | _http = http; 18 | } 19 | 20 | public async Task> Login(UserLogin request) 21 | { 22 | var result = await _http.PostAsJsonAsync("api/auth/login", request); 23 | 24 | return await result.Content.ReadFromJsonAsync>(); 25 | } 26 | 27 | public async Task> Register(UserRegister request) 28 | { 29 | var result = await _http.PostAsJsonAsync("api/auth/register", request); 30 | 31 | return await result.Content.ReadFromJsonAsync>(); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Services/BananaService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Net.Http; 5 | using System.Net.Http.Json; 6 | using System.Threading.Tasks; 7 | 8 | namespace BlazorBattles.Client.Services 9 | { 10 | public class BananaService : IBananaService 11 | { 12 | private readonly HttpClient _http; 13 | 14 | public event Action OnChange; 15 | public int Bananas { get; set; } = 0; 16 | 17 | public BananaService(HttpClient http) 18 | { 19 | _http = http; 20 | } 21 | 22 | public void EatBananas(int amount) 23 | { 24 | Bananas -= amount; 25 | BananasChanged(); 26 | } 27 | 28 | void BananasChanged() => OnChange.Invoke(); 29 | 30 | public async Task AddBananas(int amount) 31 | { 32 | var result = await _http.PutAsJsonAsync("api/user/addbananas", amount); 33 | Bananas = await result.Content.ReadFromJsonAsync(); 34 | BananasChanged(); 35 | } 36 | 37 | public async Task GetBananas() 38 | { 39 | Bananas = await _http.GetFromJsonAsync("api/user/getbananas"); 40 | BananasChanged(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Services/BattleService.cs: -------------------------------------------------------------------------------- 1 | using BlazorBattles.Shared; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Net.Http; 6 | using System.Net.Http.Json; 7 | using System.Threading.Tasks; 8 | 9 | namespace BlazorBattles.Client.Services 10 | { 11 | public class BattleService : IBattleService 12 | { 13 | private readonly HttpClient _http; 14 | 15 | public BattleService(HttpClient http) 16 | { 17 | _http = http; 18 | } 19 | 20 | public BattleResult LastBattle { get; set; } = new BattleResult(); 21 | public IList History { get; set; } = new List(); 22 | 23 | public async Task StartBattle(int opponentId) 24 | { 25 | var result = await _http.PostAsJsonAsync("api/battle", opponentId); 26 | LastBattle = await result.Content.ReadFromJsonAsync(); 27 | return LastBattle; 28 | } 29 | 30 | public async Task GetHistory() 31 | { 32 | History = await _http.GetFromJsonAsync("api/user/history"); 33 | Console.WriteLine(); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Services/IAuthService.cs: -------------------------------------------------------------------------------- 1 | using BlazorBattles.Shared; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | 7 | namespace BlazorBattles.Client.Services 8 | { 9 | public interface IAuthService 10 | { 11 | Task> Register(UserRegister request); 12 | Task> Login(UserLogin request); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Services/IBananaService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace BlazorBattles.Client.Services 7 | { 8 | public interface IBananaService 9 | { 10 | event Action OnChange; 11 | int Bananas { get; set; } 12 | void EatBananas(int amount); 13 | Task AddBananas(int amount); 14 | Task GetBananas(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Services/IBattleService.cs: -------------------------------------------------------------------------------- 1 | using BlazorBattles.Shared; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | 7 | namespace BlazorBattles.Client.Services 8 | { 9 | public interface IBattleService 10 | { 11 | BattleResult LastBattle { get; set; } 12 | IList History { get; set; } 13 | Task StartBattle(int opponentId); 14 | Task GetHistory(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Services/ILeaderboardService.cs: -------------------------------------------------------------------------------- 1 | using BlazorBattles.Shared; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | 7 | namespace BlazorBattles.Client.Services 8 | { 9 | public interface ILeaderboardService 10 | { 11 | IList Leaderboard { get; set; } 12 | Task GetLeaderboard(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Services/IUnitService.cs: -------------------------------------------------------------------------------- 1 | using BlazorBattles.Shared; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | 7 | namespace BlazorBattles.Client.Services 8 | { 9 | public interface IUnitService 10 | { 11 | IList Units { get; set; } 12 | IList MyUnits { get; set; } 13 | Task AddUnit(int unitId); 14 | Task LoadUnitsAsync(); 15 | Task LoadUserUnitsAsync(); 16 | Task ReviveArmy(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Services/LeaderboardService.cs: -------------------------------------------------------------------------------- 1 | using BlazorBattles.Shared; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Net.Http; 6 | using System.Net.Http.Json; 7 | using System.Threading.Tasks; 8 | 9 | namespace BlazorBattles.Client.Services 10 | { 11 | public class LeaderboardService : ILeaderboardService 12 | { 13 | private readonly HttpClient _http; 14 | 15 | public LeaderboardService(HttpClient http) 16 | { 17 | _http = http; 18 | } 19 | 20 | public IList Leaderboard { get; set; } 21 | 22 | public async Task GetLeaderboard() 23 | { 24 | Leaderboard = await _http.GetFromJsonAsync>("api/user/leaderboard"); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Services/UnitService.cs: -------------------------------------------------------------------------------- 1 | using BlazorBattles.Shared; 2 | using Blazored.Toast.Services; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Net.Http; 7 | using System.Net.Http.Json; 8 | using System.Threading.Tasks; 9 | 10 | namespace BlazorBattles.Client.Services 11 | { 12 | public class UnitService : IUnitService 13 | { 14 | private readonly IToastService _toastService; 15 | private readonly HttpClient _http; 16 | private readonly IBananaService _bananaService; 17 | 18 | public UnitService(IToastService toastService, HttpClient http, IBananaService bananaService) 19 | { 20 | _toastService = toastService; 21 | _http = http; 22 | _bananaService = bananaService; 23 | } 24 | 25 | public IList Units { get; set; } = new List(); 26 | 27 | public IList MyUnits { get; set; } = new List(); 28 | 29 | 30 | public async Task AddUnit(int unitId) 31 | { 32 | var unit = Units.First(unit => unit.Id == unitId); 33 | var result = await _http.PostAsJsonAsync("api/userunit", unitId); 34 | if (result.StatusCode != System.Net.HttpStatusCode.OK) 35 | { 36 | _toastService.ShowError(await result.Content.ReadAsStringAsync()); 37 | } 38 | else 39 | { 40 | await _bananaService.GetBananas(); 41 | _toastService.ShowSuccess($"Your {unit.Title} has been built!", "Unit built!"); 42 | } 43 | } 44 | 45 | public async Task LoadUnitsAsync() 46 | { 47 | if (Units.Count == 0) 48 | { 49 | Units = await _http.GetFromJsonAsync>("api/Unit"); 50 | } 51 | } 52 | 53 | public async Task LoadUserUnitsAsync() 54 | { 55 | MyUnits = await _http.GetFromJsonAsync>("api/userunit"); 56 | } 57 | 58 | public async Task ReviveArmy() 59 | { 60 | var result = await _http.PostAsJsonAsync("api/userunit/revive", null); 61 | if (result.StatusCode == System.Net.HttpStatusCode.OK) 62 | _toastService.ShowSuccess(await result.Content.ReadAsStringAsync()); 63 | else 64 | _toastService.ShowError(await result.Content.ReadAsStringAsync()); 65 | 66 | await LoadUserUnitsAsync(); 67 | await _bananaService.GetBananas(); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Shared/AddBananas.razor: -------------------------------------------------------------------------------- 1 | @inject IBananaService BananaService 2 | 3 | 4 | 5 | @code { 6 | 7 | public void IncreaseBananaCount() 8 | { 9 | BananaService.AddBananas(10); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Shared/Login.razor: -------------------------------------------------------------------------------- 1 | @inject ILocalStorageService LocalStorage 2 | @inject AuthenticationStateProvider AuthenticationStateProvider 3 | @inject IAuthService AuthService 4 | @inject IToastService ToastService 5 | 6 | Please log in below or register for a new account. 7 | 8 | 9 | 10 |
11 | 12 | 13 |
14 |
15 | 16 | 17 |
18 | 19 | 20 |
21 | 22 | @code { 23 | private UserLogin user = new UserLogin(); 24 | 25 | private async void HandleLogin() 26 | { 27 | var result = await AuthService.Login(user); 28 | if (result.Success) 29 | { 30 | await LocalStorage.SetItemAsync("authToken", result.Data); 31 | await AuthenticationStateProvider.GetAuthenticationStateAsync(); 32 | } 33 | else 34 | { 35 | ToastService.ShowError(result.Message); 36 | } 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Shared/MainLayout.razor: -------------------------------------------------------------------------------- 1 | @inherits LayoutComponentBase 2 | @using Blazored.Toast.Configuration 3 | 4 |
5 | 8 | 9 |
10 |
11 | 12 |
13 | 14 |
15 | @Body 16 |
17 |
18 |
19 | 20 | 21 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Shared/MainLayout.razor.css: -------------------------------------------------------------------------------- 1 | .page { 2 | position: relative; 3 | display: flex; 4 | flex-direction: column; 5 | } 6 | 7 | .main { 8 | flex: 1; 9 | } 10 | 11 | .sidebar { 12 | background-image: linear-gradient(180deg, rgb(5, 39, 103) 0%, #3a0647 70%); 13 | } 14 | 15 | .top-row { 16 | background-color: #f7f7f7; 17 | border-bottom: 1px solid #d6d5d5; 18 | justify-content: flex-end; 19 | height: 3.5rem; 20 | display: flex; 21 | align-items: center; 22 | } 23 | 24 | .top-row ::deep a, .top-row .btn-link { 25 | white-space: nowrap; 26 | margin-left: 1.5rem; 27 | } 28 | 29 | .top-row a:first-child { 30 | overflow: hidden; 31 | text-overflow: ellipsis; 32 | } 33 | 34 | @media (max-width: 640.98px) { 35 | .top-row:not(.auth) { 36 | /*display: none;*/ 37 | } 38 | 39 | .top-row.auth { 40 | justify-content: space-between; 41 | } 42 | 43 | .top-row a, .top-row .btn-link { 44 | margin-left: 0; 45 | } 46 | } 47 | 48 | @media (min-width: 641px) { 49 | .page { 50 | flex-direction: row; 51 | } 52 | 53 | .sidebar { 54 | width: 250px; 55 | height: 100vh; 56 | position: sticky; 57 | top: 0; 58 | } 59 | 60 | .top-row { 61 | position: sticky; 62 | top: 0; 63 | z-index: 1; 64 | } 65 | 66 | .main > div { 67 | padding-left: 2rem !important; 68 | padding-right: 1.5rem !important; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Shared/NavMenu.razor: -------------------------------------------------------------------------------- 1 | @inject ILocalStorageService LocalStorage 2 | @inject AuthenticationStateProvider AuthStateProvider 3 | @inject NavigationManager NavigationManager 4 | 5 | 11 | 12 |
13 | 56 |
57 | 58 | @code { 59 | private bool collapseNavMenu = true; 60 | 61 | private string NavMenuCssClass => collapseNavMenu ? "collapse" : null; 62 | 63 | private void ToggleNavMenu() 64 | { 65 | collapseNavMenu = !collapseNavMenu; 66 | } 67 | 68 | private async void Logout() 69 | { 70 | await LocalStorage.RemoveItemAsync("authToken"); 71 | await AuthStateProvider.GetAuthenticationStateAsync(); 72 | NavigationManager.NavigateTo("/"); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Shared/NavMenu.razor.css: -------------------------------------------------------------------------------- 1 | .navbar-toggler { 2 | background-color: rgba(255, 255, 255, 0.1); 3 | } 4 | 5 | .top-row { 6 | height: 3.5rem; 7 | background-color: rgba(0,0,0,0.4); 8 | } 9 | 10 | .navbar-brand { 11 | font-size: 1.1rem; 12 | } 13 | 14 | .oi { 15 | width: 2rem; 16 | font-size: 1.1rem; 17 | vertical-align: text-top; 18 | top: -2px; 19 | } 20 | 21 | .nav-item { 22 | font-size: 0.9rem; 23 | padding-bottom: 0.5rem; 24 | } 25 | 26 | .nav-item:first-of-type { 27 | padding-top: 1rem; 28 | } 29 | 30 | .nav-item:last-of-type { 31 | padding-bottom: 1rem; 32 | } 33 | 34 | .nav-item ::deep a { 35 | color: #d7d7d7; 36 | border-radius: 4px; 37 | height: 3rem; 38 | display: flex; 39 | align-items: center; 40 | line-height: 3rem; 41 | } 42 | 43 | .nav-item ::deep a.active { 44 | background-color: rgba(255,255,255,0.25); 45 | color: white; 46 | } 47 | 48 | .nav-item ::deep a:hover { 49 | background-color: rgba(255,255,255,0.1); 50 | color: white; 51 | } 52 | 53 | @media (min-width: 641px) { 54 | .navbar-toggler { 55 | display: none; 56 | } 57 | 58 | .collapse { 59 | /* Never collapse the sidebar for wide screens */ 60 | display: block; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Shared/SurveyPrompt.razor: -------------------------------------------------------------------------------- 1 |  11 | 12 | @code { 13 | // Demonstrates how a parent component can supply parameters 14 | [Parameter] 15 | public string Title { get; set; } 16 | } 17 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Shared/TopMenu.razor: -------------------------------------------------------------------------------- 1 | @inject IBananaService BananaService 2 | @implements IDisposable 3 | 4 | 5 | 6 |
7 | 8 | 9 | @BananaService.Bananas 10 | 11 | 12 |
13 |
14 | 15 |
16 | Login | 17 | Register 18 |
19 |
20 |
21 | 22 | @code { 23 | protected override void OnInitialized() 24 | { 25 | BananaService.OnChange += StateHasChanged; 26 | } 27 | 28 | public void Dispose() 29 | { 30 | BananaService.OnChange -= StateHasChanged; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /BlazorBattles/Client/Shared/TopMenu.razor.css: -------------------------------------------------------------------------------- 1 | .top-menu { 2 | display: flex; 3 | justify-content: center; 4 | width: 100% 5 | } 6 | -------------------------------------------------------------------------------- /BlazorBattles/Client/_Imports.razor: -------------------------------------------------------------------------------- 1 | @using System.Net.Http 2 | @using System.Net.Http.Json 3 | @using Microsoft.AspNetCore.Components.Forms 4 | @using Microsoft.AspNetCore.Components.Routing 5 | @using Microsoft.AspNetCore.Components.Web 6 | @using Microsoft.AspNetCore.Components.Web.Virtualization 7 | @using Microsoft.AspNetCore.Components.WebAssembly.Http 8 | @using Microsoft.JSInterop 9 | @using BlazorBattles.Client 10 | @using BlazorBattles.Client.Shared 11 | @using BlazorBattles.Client.Services 12 | @using Blazored.Toast 13 | @using Blazored.Toast.Services 14 | @using BlazorBattles.Shared 15 | @using Microsoft.AspNetCore.Components.Authorization 16 | @using Microsoft.AspNetCore.Authorization; 17 | @using Blazored.LocalStorage; 18 | -------------------------------------------------------------------------------- /BlazorBattles/Client/wwwroot/css/app.css: -------------------------------------------------------------------------------- 1 | @import url('open-iconic/font/css/open-iconic-bootstrap.min.css'); 2 | 3 | html, body { 4 | font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; 5 | } 6 | 7 | a, .btn-link { 8 | color: #0366d6; 9 | } 10 | 11 | .btn-primary { 12 | color: #fff; 13 | background-color: #1b6ec2; 14 | border-color: #1861ac; 15 | } 16 | 17 | .content { 18 | padding-top: 1.1rem; 19 | } 20 | 21 | .valid.modified:not([type=checkbox]) { 22 | outline: 1px solid #26b050; 23 | } 24 | 25 | .invalid { 26 | outline: 1px solid red; 27 | } 28 | 29 | .validation-message { 30 | color: red; 31 | } 32 | 33 | #blazor-error-ui { 34 | background: lightyellow; 35 | bottom: 0; 36 | box-shadow: 0 -1px 2px rgba(0, 0, 0, 0.2); 37 | display: none; 38 | left: 0; 39 | padding: 0.6rem 1.25rem 0.7rem 1.25rem; 40 | position: fixed; 41 | width: 100%; 42 | z-index: 1000; 43 | } 44 | 45 | #blazor-error-ui .dismiss { 46 | cursor: pointer; 47 | position: absolute; 48 | right: 0.75rem; 49 | top: 0.5rem; 50 | } 51 | -------------------------------------------------------------------------------- /BlazorBattles/Client/wwwroot/css/open-iconic/FONT-LICENSE: -------------------------------------------------------------------------------- 1 | SIL OPEN FONT LICENSE Version 1.1 2 | 3 | Copyright (c) 2014 Waybury 4 | 5 | PREAMBLE 6 | The goals of the Open Font License (OFL) are to stimulate worldwide 7 | development of collaborative font projects, to support the font creation 8 | efforts of academic and linguistic communities, and to provide a free and 9 | open framework in which fonts may be shared and improved in partnership 10 | with others. 11 | 12 | The OFL allows the licensed fonts to be used, studied, modified and 13 | redistributed freely as long as they are not sold by themselves. The 14 | fonts, including any derivative works, can be bundled, embedded, 15 | redistributed and/or sold with any software provided that any reserved 16 | names are not used by derivative works. The fonts and derivatives, 17 | however, cannot be released under any other type of license. The 18 | requirement for fonts to remain under this license does not apply 19 | to any document created using the fonts or their derivatives. 20 | 21 | DEFINITIONS 22 | "Font Software" refers to the set of files released by the Copyright 23 | Holder(s) under this license and clearly marked as such. This may 24 | include source files, build scripts and documentation. 25 | 26 | "Reserved Font Name" refers to any names specified as such after the 27 | copyright statement(s). 28 | 29 | "Original Version" refers to the collection of Font Software components as 30 | distributed by the Copyright Holder(s). 31 | 32 | "Modified Version" refers to any derivative made by adding to, deleting, 33 | or substituting -- in part or in whole -- any of the components of the 34 | Original Version, by changing formats or by porting the Font Software to a 35 | new environment. 36 | 37 | "Author" refers to any designer, engineer, programmer, technical 38 | writer or other person who contributed to the Font Software. 39 | 40 | PERMISSION & CONDITIONS 41 | Permission is hereby granted, free of charge, to any person obtaining 42 | a copy of the Font Software, to use, study, copy, merge, embed, modify, 43 | redistribute, and sell modified and unmodified copies of the Font 44 | Software, subject to the following conditions: 45 | 46 | 1) Neither the Font Software nor any of its individual components, 47 | in Original or Modified Versions, may be sold by itself. 48 | 49 | 2) Original or Modified Versions of the Font Software may be bundled, 50 | redistributed and/or sold with any software, provided that each copy 51 | contains the above copyright notice and this license. These can be 52 | included either as stand-alone text files, human-readable headers or 53 | in the appropriate machine-readable metadata fields within text or 54 | binary files as long as those fields can be easily viewed by the user. 55 | 56 | 3) No Modified Version of the Font Software may use the Reserved Font 57 | Name(s) unless explicit written permission is granted by the corresponding 58 | Copyright Holder. This restriction only applies to the primary font name as 59 | presented to the users. 60 | 61 | 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font 62 | Software shall not be used to promote, endorse or advertise any 63 | Modified Version, except to acknowledge the contribution(s) of the 64 | Copyright Holder(s) and the Author(s) or with their explicit written 65 | permission. 66 | 67 | 5) The Font Software, modified or unmodified, in part or in whole, 68 | must be distributed entirely under this license, and must not be 69 | distributed under any other license. The requirement for fonts to 70 | remain under this license does not apply to any document created 71 | using the Font Software. 72 | 73 | TERMINATION 74 | This license becomes null and void if any of the above conditions are 75 | not met. 76 | 77 | DISCLAIMER 78 | THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 79 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF 80 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 81 | OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE 82 | COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 83 | INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL 84 | DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 85 | FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM 86 | OTHER DEALINGS IN THE FONT SOFTWARE. 87 | -------------------------------------------------------------------------------- /BlazorBattles/Client/wwwroot/css/open-iconic/ICON-LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Waybury 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /BlazorBattles/Client/wwwroot/css/open-iconic/README.md: -------------------------------------------------------------------------------- 1 | [Open Iconic v1.1.1](http://useiconic.com/open) 2 | =========== 3 | 4 | ### Open Iconic is the open source sibling of [Iconic](http://useiconic.com). It is a hyper-legible collection of 223 icons with a tiny footprint—ready to use with Bootstrap and Foundation. [View the collection](http://useiconic.com/open#icons) 5 | 6 | 7 | 8 | ## What's in Open Iconic? 9 | 10 | * 223 icons designed to be legible down to 8 pixels 11 | * Super-light SVG files - 61.8 for the entire set 12 | * SVG sprite—the modern replacement for icon fonts 13 | * Webfont (EOT, OTF, SVG, TTF, WOFF), PNG and WebP formats 14 | * Webfont stylesheets (including versions for Bootstrap and Foundation) in CSS, LESS, SCSS and Stylus formats 15 | * PNG and WebP raster images in 8px, 16px, 24px, 32px, 48px and 64px. 16 | 17 | 18 | ## Getting Started 19 | 20 | #### For code samples and everything else you need to get started with Open Iconic, check out our [Icons](http://useiconic.com/open#icons) and [Reference](http://useiconic.com/open#reference) sections. 21 | 22 | ### General Usage 23 | 24 | #### Using Open Iconic's SVGs 25 | 26 | We like SVGs and we think they're the way to display icons on the web. Since Open Iconic are just basic SVGs, we suggest you display them like you would any other image (don't forget the `alt` attribute). 27 | 28 | ``` 29 | icon name 30 | ``` 31 | 32 | #### Using Open Iconic's SVG Sprite 33 | 34 | Open Iconic also comes in a SVG sprite which allows you to display all the icons in the set with a single request. It's like an icon font, without being a hack. 35 | 36 | Adding an icon from an SVG sprite is a little different than what you're used to, but it's still a piece of cake. *Tip: To make your icons easily style able, we suggest adding a general class to the* `` *tag and a unique class name for each different icon in the* `` *tag.* 37 | 38 | ``` 39 | 40 | 41 | 42 | ``` 43 | 44 | Sizing icons only needs basic CSS. All the icons are in a square format, so just set the `` tag with equal width and height dimensions. 45 | 46 | ``` 47 | .icon { 48 | width: 16px; 49 | height: 16px; 50 | } 51 | ``` 52 | 53 | Coloring icons is even easier. All you need to do is set the `fill` rule on the `` tag. 54 | 55 | ``` 56 | .icon-account-login { 57 | fill: #f00; 58 | } 59 | ``` 60 | 61 | To learn more about SVG Sprites, read [Chris Coyier's guide](http://css-tricks.com/svg-sprites-use-better-icon-fonts/). 62 | 63 | #### Using Open Iconic's Icon Font... 64 | 65 | 66 | ##### …with Bootstrap 67 | 68 | You can find our Bootstrap stylesheets in `font/css/open-iconic-bootstrap.{css, less, scss, styl}` 69 | 70 | 71 | ``` 72 | 73 | ``` 74 | 75 | 76 | ``` 77 | 78 | ``` 79 | 80 | ##### …with Foundation 81 | 82 | You can find our Foundation stylesheets in `font/css/open-iconic-foundation.{css, less, scss, styl}` 83 | 84 | ``` 85 | 86 | ``` 87 | 88 | 89 | ``` 90 | 91 | ``` 92 | 93 | ##### …on its own 94 | 95 | You can find our default stylesheets in `font/css/open-iconic.{css, less, scss, styl}` 96 | 97 | ``` 98 | 99 | ``` 100 | 101 | ``` 102 | 103 | ``` 104 | 105 | 106 | ## License 107 | 108 | ### Icons 109 | 110 | All code (including SVG markup) is under the [MIT License](http://opensource.org/licenses/MIT). 111 | 112 | ### Fonts 113 | 114 | All fonts are under the [SIL Licensed](http://scripts.sil.org/cms/scripts/page.php?item_id=OFL_web). 115 | -------------------------------------------------------------------------------- /BlazorBattles/Client/wwwroot/css/open-iconic/font/css/open-iconic-bootstrap.min.css: -------------------------------------------------------------------------------- 1 | @font-face{font-family:Icons;src:url(../fonts/open-iconic.eot);src:url(../fonts/open-iconic.eot?#iconic-sm) format('embedded-opentype'),url(../fonts/open-iconic.woff) format('woff'),url(../fonts/open-iconic.ttf) format('truetype'),url(../fonts/open-iconic.otf) format('opentype'),url(../fonts/open-iconic.svg#iconic-sm) format('svg');font-weight:400;font-style:normal}.oi{position:relative;top:1px;display:inline-block;speak:none;font-family:Icons;font-style:normal;font-weight:400;line-height:1;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}.oi:empty:before{width:1em;text-align:center;box-sizing:content-box}.oi.oi-align-center:before{text-align:center}.oi.oi-align-left:before{text-align:left}.oi.oi-align-right:before{text-align:right}.oi.oi-flip-horizontal:before{-webkit-transform:scale(-1,1);-ms-transform:scale(-1,1);transform:scale(-1,1)}.oi.oi-flip-vertical:before{-webkit-transform:scale(1,-1);-ms-transform:scale(-1,1);transform:scale(1,-1)}.oi.oi-flip-horizontal-vertical:before{-webkit-transform:scale(-1,-1);-ms-transform:scale(-1,1);transform:scale(-1,-1)}.oi-account-login:before{content:'\e000'}.oi-account-logout:before{content:'\e001'}.oi-action-redo:before{content:'\e002'}.oi-action-undo:before{content:'\e003'}.oi-align-center:before{content:'\e004'}.oi-align-left:before{content:'\e005'}.oi-align-right:before{content:'\e006'}.oi-aperture:before{content:'\e007'}.oi-arrow-bottom:before{content:'\e008'}.oi-arrow-circle-bottom:before{content:'\e009'}.oi-arrow-circle-left:before{content:'\e00a'}.oi-arrow-circle-right:before{content:'\e00b'}.oi-arrow-circle-top:before{content:'\e00c'}.oi-arrow-left:before{content:'\e00d'}.oi-arrow-right:before{content:'\e00e'}.oi-arrow-thick-bottom:before{content:'\e00f'}.oi-arrow-thick-left:before{content:'\e010'}.oi-arrow-thick-right:before{content:'\e011'}.oi-arrow-thick-top:before{content:'\e012'}.oi-arrow-top:before{content:'\e013'}.oi-audio-spectrum:before{content:'\e014'}.oi-audio:before{content:'\e015'}.oi-badge:before{content:'\e016'}.oi-ban:before{content:'\e017'}.oi-bar-chart:before{content:'\e018'}.oi-basket:before{content:'\e019'}.oi-battery-empty:before{content:'\e01a'}.oi-battery-full:before{content:'\e01b'}.oi-beaker:before{content:'\e01c'}.oi-bell:before{content:'\e01d'}.oi-bluetooth:before{content:'\e01e'}.oi-bold:before{content:'\e01f'}.oi-bolt:before{content:'\e020'}.oi-book:before{content:'\e021'}.oi-bookmark:before{content:'\e022'}.oi-box:before{content:'\e023'}.oi-briefcase:before{content:'\e024'}.oi-british-pound:before{content:'\e025'}.oi-browser:before{content:'\e026'}.oi-brush:before{content:'\e027'}.oi-bug:before{content:'\e028'}.oi-bullhorn:before{content:'\e029'}.oi-calculator:before{content:'\e02a'}.oi-calendar:before{content:'\e02b'}.oi-camera-slr:before{content:'\e02c'}.oi-caret-bottom:before{content:'\e02d'}.oi-caret-left:before{content:'\e02e'}.oi-caret-right:before{content:'\e02f'}.oi-caret-top:before{content:'\e030'}.oi-cart:before{content:'\e031'}.oi-chat:before{content:'\e032'}.oi-check:before{content:'\e033'}.oi-chevron-bottom:before{content:'\e034'}.oi-chevron-left:before{content:'\e035'}.oi-chevron-right:before{content:'\e036'}.oi-chevron-top:before{content:'\e037'}.oi-circle-check:before{content:'\e038'}.oi-circle-x:before{content:'\e039'}.oi-clipboard:before{content:'\e03a'}.oi-clock:before{content:'\e03b'}.oi-cloud-download:before{content:'\e03c'}.oi-cloud-upload:before{content:'\e03d'}.oi-cloud:before{content:'\e03e'}.oi-cloudy:before{content:'\e03f'}.oi-code:before{content:'\e040'}.oi-cog:before{content:'\e041'}.oi-collapse-down:before{content:'\e042'}.oi-collapse-left:before{content:'\e043'}.oi-collapse-right:before{content:'\e044'}.oi-collapse-up:before{content:'\e045'}.oi-command:before{content:'\e046'}.oi-comment-square:before{content:'\e047'}.oi-compass:before{content:'\e048'}.oi-contrast:before{content:'\e049'}.oi-copywriting:before{content:'\e04a'}.oi-credit-card:before{content:'\e04b'}.oi-crop:before{content:'\e04c'}.oi-dashboard:before{content:'\e04d'}.oi-data-transfer-download:before{content:'\e04e'}.oi-data-transfer-upload:before{content:'\e04f'}.oi-delete:before{content:'\e050'}.oi-dial:before{content:'\e051'}.oi-document:before{content:'\e052'}.oi-dollar:before{content:'\e053'}.oi-double-quote-sans-left:before{content:'\e054'}.oi-double-quote-sans-right:before{content:'\e055'}.oi-double-quote-serif-left:before{content:'\e056'}.oi-double-quote-serif-right:before{content:'\e057'}.oi-droplet:before{content:'\e058'}.oi-eject:before{content:'\e059'}.oi-elevator:before{content:'\e05a'}.oi-ellipses:before{content:'\e05b'}.oi-envelope-closed:before{content:'\e05c'}.oi-envelope-open:before{content:'\e05d'}.oi-euro:before{content:'\e05e'}.oi-excerpt:before{content:'\e05f'}.oi-expand-down:before{content:'\e060'}.oi-expand-left:before{content:'\e061'}.oi-expand-right:before{content:'\e062'}.oi-expand-up:before{content:'\e063'}.oi-external-link:before{content:'\e064'}.oi-eye:before{content:'\e065'}.oi-eyedropper:before{content:'\e066'}.oi-file:before{content:'\e067'}.oi-fire:before{content:'\e068'}.oi-flag:before{content:'\e069'}.oi-flash:before{content:'\e06a'}.oi-folder:before{content:'\e06b'}.oi-fork:before{content:'\e06c'}.oi-fullscreen-enter:before{content:'\e06d'}.oi-fullscreen-exit:before{content:'\e06e'}.oi-globe:before{content:'\e06f'}.oi-graph:before{content:'\e070'}.oi-grid-four-up:before{content:'\e071'}.oi-grid-three-up:before{content:'\e072'}.oi-grid-two-up:before{content:'\e073'}.oi-hard-drive:before{content:'\e074'}.oi-header:before{content:'\e075'}.oi-headphones:before{content:'\e076'}.oi-heart:before{content:'\e077'}.oi-home:before{content:'\e078'}.oi-image:before{content:'\e079'}.oi-inbox:before{content:'\e07a'}.oi-infinity:before{content:'\e07b'}.oi-info:before{content:'\e07c'}.oi-italic:before{content:'\e07d'}.oi-justify-center:before{content:'\e07e'}.oi-justify-left:before{content:'\e07f'}.oi-justify-right:before{content:'\e080'}.oi-key:before{content:'\e081'}.oi-laptop:before{content:'\e082'}.oi-layers:before{content:'\e083'}.oi-lightbulb:before{content:'\e084'}.oi-link-broken:before{content:'\e085'}.oi-link-intact:before{content:'\e086'}.oi-list-rich:before{content:'\e087'}.oi-list:before{content:'\e088'}.oi-location:before{content:'\e089'}.oi-lock-locked:before{content:'\e08a'}.oi-lock-unlocked:before{content:'\e08b'}.oi-loop-circular:before{content:'\e08c'}.oi-loop-square:before{content:'\e08d'}.oi-loop:before{content:'\e08e'}.oi-magnifying-glass:before{content:'\e08f'}.oi-map-marker:before{content:'\e090'}.oi-map:before{content:'\e091'}.oi-media-pause:before{content:'\e092'}.oi-media-play:before{content:'\e093'}.oi-media-record:before{content:'\e094'}.oi-media-skip-backward:before{content:'\e095'}.oi-media-skip-forward:before{content:'\e096'}.oi-media-step-backward:before{content:'\e097'}.oi-media-step-forward:before{content:'\e098'}.oi-media-stop:before{content:'\e099'}.oi-medical-cross:before{content:'\e09a'}.oi-menu:before{content:'\e09b'}.oi-microphone:before{content:'\e09c'}.oi-minus:before{content:'\e09d'}.oi-monitor:before{content:'\e09e'}.oi-moon:before{content:'\e09f'}.oi-move:before{content:'\e0a0'}.oi-musical-note:before{content:'\e0a1'}.oi-paperclip:before{content:'\e0a2'}.oi-pencil:before{content:'\e0a3'}.oi-people:before{content:'\e0a4'}.oi-person:before{content:'\e0a5'}.oi-phone:before{content:'\e0a6'}.oi-pie-chart:before{content:'\e0a7'}.oi-pin:before{content:'\e0a8'}.oi-play-circle:before{content:'\e0a9'}.oi-plus:before{content:'\e0aa'}.oi-power-standby:before{content:'\e0ab'}.oi-print:before{content:'\e0ac'}.oi-project:before{content:'\e0ad'}.oi-pulse:before{content:'\e0ae'}.oi-puzzle-piece:before{content:'\e0af'}.oi-question-mark:before{content:'\e0b0'}.oi-rain:before{content:'\e0b1'}.oi-random:before{content:'\e0b2'}.oi-reload:before{content:'\e0b3'}.oi-resize-both:before{content:'\e0b4'}.oi-resize-height:before{content:'\e0b5'}.oi-resize-width:before{content:'\e0b6'}.oi-rss-alt:before{content:'\e0b7'}.oi-rss:before{content:'\e0b8'}.oi-script:before{content:'\e0b9'}.oi-share-boxed:before{content:'\e0ba'}.oi-share:before{content:'\e0bb'}.oi-shield:before{content:'\e0bc'}.oi-signal:before{content:'\e0bd'}.oi-signpost:before{content:'\e0be'}.oi-sort-ascending:before{content:'\e0bf'}.oi-sort-descending:before{content:'\e0c0'}.oi-spreadsheet:before{content:'\e0c1'}.oi-star:before{content:'\e0c2'}.oi-sun:before{content:'\e0c3'}.oi-tablet:before{content:'\e0c4'}.oi-tag:before{content:'\e0c5'}.oi-tags:before{content:'\e0c6'}.oi-target:before{content:'\e0c7'}.oi-task:before{content:'\e0c8'}.oi-terminal:before{content:'\e0c9'}.oi-text:before{content:'\e0ca'}.oi-thumb-down:before{content:'\e0cb'}.oi-thumb-up:before{content:'\e0cc'}.oi-timer:before{content:'\e0cd'}.oi-transfer:before{content:'\e0ce'}.oi-trash:before{content:'\e0cf'}.oi-underline:before{content:'\e0d0'}.oi-vertical-align-bottom:before{content:'\e0d1'}.oi-vertical-align-center:before{content:'\e0d2'}.oi-vertical-align-top:before{content:'\e0d3'}.oi-video:before{content:'\e0d4'}.oi-volume-high:before{content:'\e0d5'}.oi-volume-low:before{content:'\e0d6'}.oi-volume-off:before{content:'\e0d7'}.oi-warning:before{content:'\e0d8'}.oi-wifi:before{content:'\e0d9'}.oi-wrench:before{content:'\e0da'}.oi-x:before{content:'\e0db'}.oi-yen:before{content:'\e0dc'}.oi-zoom-in:before{content:'\e0dd'}.oi-zoom-out:before{content:'\e0de'} -------------------------------------------------------------------------------- /BlazorBattles/Client/wwwroot/css/open-iconic/font/fonts/open-iconic.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/patrickgod/BlazorBattles-dotnet5/d28366ab6d4b56e715bfe42e7dcaa920227b3518/BlazorBattles/Client/wwwroot/css/open-iconic/font/fonts/open-iconic.eot -------------------------------------------------------------------------------- /BlazorBattles/Client/wwwroot/css/open-iconic/font/fonts/open-iconic.otf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/patrickgod/BlazorBattles-dotnet5/d28366ab6d4b56e715bfe42e7dcaa920227b3518/BlazorBattles/Client/wwwroot/css/open-iconic/font/fonts/open-iconic.otf -------------------------------------------------------------------------------- /BlazorBattles/Client/wwwroot/css/open-iconic/font/fonts/open-iconic.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 8 | Created by FontForge 20120731 at Tue Jul 1 20:39:22 2014 9 | By P.J. Onori 10 | Created by P.J. Onori with FontForge 2.0 (http://fontforge.sf.net) 11 | 12 | 13 | 14 | 27 | 28 | 30 | 32 | 34 | 36 | 38 | 40 | 42 | 45 | 47 | 49 | 51 | 53 | 55 | 57 | 59 | 61 | 63 | 65 | 67 | 69 | 71 | 74 | 76 | 79 | 81 | 84 | 86 | 88 | 91 | 93 | 95 | 98 | 100 | 102 | 104 | 106 | 109 | 112 | 115 | 117 | 121 | 123 | 125 | 127 | 130 | 132 | 134 | 136 | 138 | 141 | 143 | 145 | 147 | 149 | 151 | 153 | 155 | 157 | 159 | 162 | 165 | 167 | 169 | 172 | 174 | 177 | 179 | 181 | 183 | 185 | 189 | 191 | 194 | 196 | 198 | 200 | 202 | 205 | 207 | 209 | 211 | 213 | 215 | 218 | 220 | 222 | 224 | 226 | 228 | 230 | 232 | 234 | 236 | 238 | 241 | 243 | 245 | 247 | 249 | 251 | 253 | 256 | 259 | 261 | 263 | 265 | 267 | 269 | 272 | 274 | 276 | 280 | 282 | 285 | 287 | 289 | 292 | 295 | 298 | 300 | 302 | 304 | 306 | 309 | 312 | 314 | 316 | 318 | 320 | 322 | 324 | 326 | 330 | 334 | 338 | 340 | 343 | 345 | 347 | 349 | 351 | 353 | 355 | 358 | 360 | 363 | 365 | 367 | 369 | 371 | 373 | 375 | 377 | 379 | 381 | 383 | 386 | 388 | 390 | 392 | 394 | 396 | 399 | 401 | 404 | 406 | 408 | 410 | 412 | 414 | 416 | 419 | 421 | 423 | 425 | 428 | 431 | 435 | 438 | 440 | 442 | 444 | 446 | 448 | 451 | 453 | 455 | 457 | 460 | 462 | 464 | 466 | 468 | 471 | 473 | 477 | 479 | 481 | 483 | 486 | 488 | 490 | 492 | 494 | 496 | 499 | 501 | 504 | 506 | 509 | 512 | 515 | 517 | 520 | 522 | 524 | 526 | 529 | 532 | 534 | 536 | 539 | 542 | 543 | 544 | -------------------------------------------------------------------------------- /BlazorBattles/Client/wwwroot/css/open-iconic/font/fonts/open-iconic.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/patrickgod/BlazorBattles-dotnet5/d28366ab6d4b56e715bfe42e7dcaa920227b3518/BlazorBattles/Client/wwwroot/css/open-iconic/font/fonts/open-iconic.ttf -------------------------------------------------------------------------------- /BlazorBattles/Client/wwwroot/css/open-iconic/font/fonts/open-iconic.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/patrickgod/BlazorBattles-dotnet5/d28366ab6d4b56e715bfe42e7dcaa920227b3518/BlazorBattles/Client/wwwroot/css/open-iconic/font/fonts/open-iconic.woff -------------------------------------------------------------------------------- /BlazorBattles/Client/wwwroot/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/patrickgod/BlazorBattles-dotnet5/d28366ab6d4b56e715bfe42e7dcaa920227b3518/BlazorBattles/Client/wwwroot/favicon.ico -------------------------------------------------------------------------------- /BlazorBattles/Client/wwwroot/icons/archer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/patrickgod/BlazorBattles-dotnet5/d28366ab6d4b56e715bfe42e7dcaa920227b3518/BlazorBattles/Client/wwwroot/icons/archer.png -------------------------------------------------------------------------------- /BlazorBattles/Client/wwwroot/icons/axe.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/patrickgod/BlazorBattles-dotnet5/d28366ab6d4b56e715bfe42e7dcaa920227b3518/BlazorBattles/Client/wwwroot/icons/axe.png -------------------------------------------------------------------------------- /BlazorBattles/Client/wwwroot/icons/banana.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/patrickgod/BlazorBattles-dotnet5/d28366ab6d4b56e715bfe42e7dcaa920227b3518/BlazorBattles/Client/wwwroot/icons/banana.png -------------------------------------------------------------------------------- /BlazorBattles/Client/wwwroot/icons/knight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/patrickgod/BlazorBattles-dotnet5/d28366ab6d4b56e715bfe42e7dcaa920227b3518/BlazorBattles/Client/wwwroot/icons/knight.png -------------------------------------------------------------------------------- /BlazorBattles/Client/wwwroot/icons/mage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/patrickgod/BlazorBattles-dotnet5/d28366ab6d4b56e715bfe42e7dcaa920227b3518/BlazorBattles/Client/wwwroot/icons/mage.png -------------------------------------------------------------------------------- /BlazorBattles/Client/wwwroot/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Blazor Battles 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
Prepare to Fight!
18 | 19 |
20 | An unhandled error has occurred. 21 | Reload 22 | 🗙 23 |
24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /BlazorBattles/Server/BlazorBattles.Server.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | all 13 | runtime; build; native; contentfiles; analyzers; buildtransitive 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Controllers/AuthController.cs: -------------------------------------------------------------------------------- 1 | using BlazorBattles.Server.Data; 2 | using BlazorBattles.Shared; 3 | using Microsoft.AspNetCore.Http; 4 | using Microsoft.AspNetCore.Mvc; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Threading.Tasks; 9 | 10 | namespace BlazorBattles.Server.Controllers 11 | { 12 | [Route("api/[controller]")] 13 | [ApiController] 14 | public class AuthController : ControllerBase 15 | { 16 | private readonly IAuthRepository _authRepo; 17 | 18 | public AuthController(IAuthRepository authRepo) 19 | { 20 | _authRepo = authRepo; 21 | } 22 | 23 | [HttpPost("register")] 24 | public async Task Register(UserRegister request) 25 | { 26 | var response = await _authRepo.Register( 27 | new User 28 | { 29 | Username = request.Username, 30 | Email = request.Email, 31 | Bananas = request.Bananas, 32 | DateOfBirth = request.DateOfBirth, 33 | IsConfirmed = request.IsConfirmed 34 | }, 35 | request.Password, 36 | request.StartUnitId); 37 | 38 | if (!response.Success) 39 | { 40 | return BadRequest(response); 41 | } 42 | 43 | return Ok(response); 44 | } 45 | 46 | [HttpPost("login")] 47 | public async Task Login(UserLogin request) 48 | { 49 | var response = await _authRepo.Login( 50 | request.Email, request.Password); 51 | 52 | if (!response.Success) 53 | { 54 | return BadRequest(response); 55 | } 56 | 57 | return Ok(response); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Controllers/BattleController.cs: -------------------------------------------------------------------------------- 1 | using BlazorBattles.Server.Data; 2 | using BlazorBattles.Server.Services; 3 | using BlazorBattles.Shared; 4 | using Microsoft.AspNetCore.Authorization; 5 | using Microsoft.AspNetCore.Http; 6 | using Microsoft.AspNetCore.Mvc; 7 | using Microsoft.EntityFrameworkCore; 8 | using System; 9 | using System.Collections.Generic; 10 | using System.Linq; 11 | using System.Threading.Tasks; 12 | 13 | namespace BlazorBattles.Server.Controllers 14 | { 15 | [Route("api/[controller]")] 16 | [ApiController] 17 | [Authorize] 18 | public class BattleController : ControllerBase 19 | { 20 | private readonly DataContext _context; 21 | private readonly IUtilityService _utilityService; 22 | 23 | public BattleController(DataContext context, IUtilityService utilityService) 24 | { 25 | _context = context; 26 | _utilityService = utilityService; 27 | } 28 | 29 | [HttpPost] 30 | public async Task StartBattle([FromBody] int opponentId) 31 | { 32 | var attacker = await _utilityService.GetUser(); 33 | var opponent = await _context.Users.FindAsync(opponentId); 34 | if (opponent == null || opponent.IsDeleted) 35 | { 36 | return NotFound("Opponent not available."); 37 | } 38 | 39 | var result = new BattleResult(); 40 | await Fight(attacker, opponent, result); 41 | 42 | return Ok(result); 43 | } 44 | 45 | private async Task Fight(User attacker, User opponent, BattleResult result) 46 | { 47 | var attackerArmy = await _context.UserUnits 48 | .Where(u => u.UserId == attacker.Id && u.HitPoints > 0) 49 | .Include(u => u.Unit) 50 | .ToListAsync(); 51 | var opponentArmy = await _context.UserUnits 52 | .Where(u => u.UserId == opponent.Id && u.HitPoints > 0) 53 | .Include(u => u.Unit) 54 | .ToListAsync(); 55 | 56 | var attackerDamageSum = 0; 57 | var opponentDamageSum = 0; 58 | 59 | int currentRound = 0; 60 | while (attackerArmy.Count > 0 && opponentArmy.Count > 0) 61 | { 62 | currentRound++; 63 | 64 | if (currentRound % 2 != 0) 65 | attackerDamageSum += FightRound(attacker, opponent, attackerArmy, opponentArmy, result); 66 | else 67 | opponentDamageSum += FightRound(opponent, attacker, opponentArmy, attackerArmy, result); 68 | } 69 | 70 | result.IsVictory = opponentArmy.Count == 0; 71 | result.RoundsFought = currentRound; 72 | 73 | if (result.RoundsFought > 0) 74 | await FinishFight(attacker, opponent, result, attackerDamageSum, opponentDamageSum); 75 | } 76 | 77 | private int FightRound(User attacker, User opponent, 78 | List attackerArmy, List opponentArmy, BattleResult result) 79 | { 80 | int randomAttackerIndex = new Random().Next(attackerArmy.Count); 81 | int randomOpponentIndex = new Random().Next(opponentArmy.Count); 82 | 83 | var randomAttacker = attackerArmy[randomAttackerIndex]; 84 | var randomOpponent = opponentArmy[randomOpponentIndex]; 85 | 86 | var damage = 87 | new Random().Next(randomAttacker.Unit.Attack) - new Random().Next(randomOpponent.Unit.Defense); 88 | if (damage < 0) damage = 0; 89 | 90 | if (damage <= randomOpponent.HitPoints) 91 | { 92 | randomOpponent.HitPoints -= damage; 93 | result.Log.Add( 94 | $"{attacker.Username}'s {randomAttacker.Unit.Title} attacks " + 95 | $"{opponent.Username}'s {randomOpponent.Unit.Title} with {damage} damage."); 96 | return damage; 97 | } 98 | else 99 | { 100 | damage = randomOpponent.HitPoints; 101 | randomOpponent.HitPoints = 0; 102 | opponentArmy.Remove(randomOpponent); 103 | result.Log.Add( 104 | $"{attacker.Username}'s {randomAttacker.Unit.Title} kills " + 105 | $"{opponent.Username}'s {randomOpponent.Unit.Title}!"); 106 | return damage; 107 | } 108 | } 109 | 110 | private async Task FinishFight(User attacker, User opponent, BattleResult result, 111 | int attackerDamageSum, int opponentDamageSum) 112 | { 113 | result.AttackerDamageSum = attackerDamageSum; 114 | result.OpponentDamageSum = opponentDamageSum; 115 | 116 | attacker.Battles++; 117 | opponent.Battles++; 118 | 119 | if (result.IsVictory) 120 | { 121 | attacker.Victories++; 122 | opponent.Defeats++; 123 | attacker.Bananas += opponentDamageSum; 124 | opponent.Bananas += attackerDamageSum * 10; 125 | } 126 | else 127 | { 128 | attacker.Defeats++; 129 | opponent.Victories++; 130 | attacker.Bananas += opponentDamageSum * 10; 131 | opponent.Bananas += attackerDamageSum; 132 | } 133 | 134 | StoreBattleHistory(attacker, opponent, result); 135 | 136 | await _context.SaveChangesAsync(); 137 | } 138 | 139 | private void StoreBattleHistory(User attacker, User opponent, BattleResult result) 140 | { 141 | var battle = new Battle(); 142 | battle.Attacker = attacker; 143 | battle.Opponent = opponent; 144 | battle.RoundsFought = result.RoundsFought; 145 | battle.WinnerDamage = result.IsVictory ? result.AttackerDamageSum : result.OpponentDamageSum; 146 | battle.Winner = result.IsVictory ? attacker : opponent; 147 | 148 | _context.Battles.Add(battle); 149 | } 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Controllers/UnitController.cs: -------------------------------------------------------------------------------- 1 | using BlazorBattles.Server.Data; 2 | using BlazorBattles.Shared; 3 | using Microsoft.AspNetCore.Http; 4 | using Microsoft.AspNetCore.Mvc; 5 | using Microsoft.EntityFrameworkCore; 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Linq; 9 | using System.Threading.Tasks; 10 | 11 | namespace BlazorBattles.Server.Controllers 12 | { 13 | [Route("api/[controller]")] 14 | [ApiController] 15 | public class UnitController : ControllerBase 16 | { 17 | private readonly DataContext _context; 18 | 19 | public UnitController(DataContext context) 20 | { 21 | _context = context; 22 | } 23 | 24 | [HttpGet] 25 | public async Task GetUnits() 26 | { 27 | var units = await _context.Units.ToListAsync(); 28 | return Ok(units); 29 | } 30 | 31 | [HttpPost] 32 | public async Task AddUnit(Unit unit) 33 | { 34 | _context.Units.Add(unit); 35 | await _context.SaveChangesAsync(); 36 | return Ok(await _context.Units.ToListAsync()); 37 | } 38 | 39 | [HttpPut("{id}")] 40 | public async Task UpdateUnit(int id, Unit unit) 41 | { 42 | var dbUnit = await _context.Units.FirstOrDefaultAsync(u => u.Id == id); 43 | if (dbUnit == null) 44 | { 45 | return NotFound("Unit with the given Id doesn't exist."); 46 | } 47 | 48 | dbUnit.Title = unit.Title; 49 | dbUnit.Attack = unit.Attack; 50 | dbUnit.Defense = unit.Defense; 51 | dbUnit.BananaCost = unit.BananaCost; 52 | dbUnit.HitPoints = unit.HitPoints; 53 | 54 | await _context.SaveChangesAsync(); 55 | 56 | return Ok(dbUnit); 57 | } 58 | 59 | [HttpDelete("{id}")] 60 | public async Task DeleteUnit(int id) 61 | { 62 | var dbUnit = await _context.Units.FirstOrDefaultAsync(u => u.Id == id); 63 | if (dbUnit == null) 64 | { 65 | return NotFound("Unit with the given Id doesn't exist."); 66 | } 67 | 68 | _context.Units.Remove(dbUnit); 69 | await _context.SaveChangesAsync(); 70 | 71 | return Ok(await _context.Units.ToListAsync()); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Controllers/UserController.cs: -------------------------------------------------------------------------------- 1 | using BlazorBattles.Server.Data; 2 | using BlazorBattles.Server.Services; 3 | using BlazorBattles.Shared; 4 | using Microsoft.AspNetCore.Authorization; 5 | using Microsoft.AspNetCore.Http; 6 | using Microsoft.AspNetCore.Mvc; 7 | using Microsoft.EntityFrameworkCore; 8 | using System; 9 | using System.Collections.Generic; 10 | using System.Linq; 11 | using System.Security.Claims; 12 | using System.Threading.Tasks; 13 | 14 | namespace BlazorBattles.Server.Controllers 15 | { 16 | [Route("api/[controller]")] 17 | [ApiController] 18 | [Authorize] 19 | public class UserController : ControllerBase 20 | { 21 | private readonly DataContext _context; 22 | private readonly IUtilityService _utilityService; 23 | 24 | public UserController(DataContext context, IUtilityService utilityService) 25 | { 26 | _context = context; 27 | _utilityService = utilityService; 28 | } 29 | 30 | [HttpGet("getbananas")] 31 | public async Task GetBananas() 32 | { 33 | var user = await _utilityService.GetUser(); 34 | return Ok(user.Bananas); 35 | } 36 | 37 | [HttpPut("addbananas")] 38 | public async Task AddBananas([FromBody] int bananas) 39 | { 40 | var user = await _utilityService.GetUser(); 41 | user.Bananas += bananas; 42 | 43 | await _context.SaveChangesAsync(); 44 | return Ok(user.Bananas); 45 | } 46 | 47 | [HttpGet("leaderboard")] 48 | public async Task GetLeaderboard() 49 | { 50 | var users = await _context.Users.Where(user => !user.IsDeleted && user.IsConfirmed).ToListAsync(); 51 | 52 | users = users 53 | .OrderByDescending(u => u.Victories) 54 | .ThenBy(u => u.Defeats) 55 | .ThenBy(u => u.DateCreated) 56 | .ToList(); 57 | 58 | int rank = 1; 59 | var response = users.Select(user => new UserStatistic 60 | { 61 | Rank = rank++, 62 | UserId = user.Id, 63 | Username = user.Username, 64 | Battles = user.Battles, 65 | Victories = user.Victories, 66 | Defeats = user.Defeats 67 | }); 68 | 69 | return Ok(response); 70 | } 71 | 72 | [HttpGet("history")] 73 | public async Task GetHistory() 74 | { 75 | var user = await _utilityService.GetUser(); 76 | var battles = await _context.Battles 77 | .Where(battle => battle.AttackerId == user.Id || battle.OpponentId == user.Id) 78 | .Include(battle => battle.Attacker) 79 | .Include(battle => battle.Opponent) 80 | .Include(battle => battle.Winner) 81 | .ToListAsync(); 82 | var history = battles.Select(battle => new BattleHistoryEntry 83 | { 84 | BattleId = battle.Id, 85 | AttackerId = battle.AttackerId, 86 | OpponentId = battle.OpponentId, 87 | YouWon = battle.WinnerId == user.Id, 88 | AttackerName = battle.Attacker.Username, 89 | OpponentName = battle.Opponent.Username, 90 | RoundsFought = battle.RoundsFought, 91 | WinnerDamageDealt = battle.WinnerDamage, 92 | BattleDate = battle.BattleDate 93 | }); 94 | return Ok(history.OrderByDescending(h => h.BattleDate)); 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Controllers/UserUnitController.cs: -------------------------------------------------------------------------------- 1 | using BlazorBattles.Server.Data; 2 | using BlazorBattles.Server.Services; 3 | using BlazorBattles.Shared; 4 | using Microsoft.AspNetCore.Authorization; 5 | using Microsoft.AspNetCore.Http; 6 | using Microsoft.AspNetCore.Mvc; 7 | using Microsoft.EntityFrameworkCore; 8 | using System; 9 | using System.Collections.Generic; 10 | using System.Linq; 11 | using System.Threading.Tasks; 12 | 13 | namespace BlazorBattles.Server.Controllers 14 | { 15 | [Route("api/[controller]")] 16 | [ApiController] 17 | [Authorize] 18 | public class UserUnitController : ControllerBase 19 | { 20 | private readonly DataContext _context; 21 | private readonly IUtilityService _utilityService; 22 | 23 | public UserUnitController(DataContext context, IUtilityService utilityService) 24 | { 25 | _context = context; 26 | _utilityService = utilityService; 27 | } 28 | 29 | [HttpPost("revive")] 30 | public async Task ReviveArmy() 31 | { 32 | var user = await _utilityService.GetUser(); 33 | var userUnits = await _context.UserUnits 34 | .Where(unit => unit.UserId == user.Id) 35 | .Include(unit => unit.Unit) 36 | .ToListAsync(); 37 | 38 | int bananaCost = 1000; 39 | 40 | if (user.Bananas < bananaCost) 41 | { 42 | return BadRequest("Not enough bananas! You need 1000 bananas to revive your army."); 43 | } 44 | 45 | bool armyAlreadyAlive = true; 46 | foreach (var userUnit in userUnits) 47 | { 48 | if (userUnit.HitPoints <= 0) 49 | { 50 | armyAlreadyAlive = false; 51 | userUnit.HitPoints = new Random().Next(0, userUnit.Unit.HitPoints); 52 | } 53 | } 54 | 55 | if (armyAlreadyAlive) 56 | return Ok("Your army is already alive."); 57 | 58 | user.Bananas -= bananaCost; 59 | 60 | await _context.SaveChangesAsync(); 61 | 62 | return Ok("Army revived!"); 63 | } 64 | 65 | [HttpPost] 66 | public async Task BuildUserUnit([FromBody] int unitId) 67 | { 68 | var unit = await _context.Units.FirstOrDefaultAsync(u => u.Id == unitId); 69 | var user = await _utilityService.GetUser(); 70 | if (user.Bananas < unit.BananaCost) 71 | { 72 | return BadRequest("Not enough bananas!"); 73 | } 74 | 75 | user.Bananas -= unit.BananaCost; 76 | 77 | var newUserUnit = new UserUnit 78 | { 79 | UnitId = unit.Id, 80 | UserId = user.Id, 81 | HitPoints = unit.HitPoints 82 | }; 83 | 84 | _context.UserUnits.Add(newUserUnit); 85 | await _context.SaveChangesAsync(); 86 | return Ok(newUserUnit); 87 | } 88 | 89 | [HttpGet] 90 | public async Task GetUserUnits() 91 | { 92 | var user = await _utilityService.GetUser(); 93 | var userUnits = await _context.UserUnits.Where(unit => unit.UserId == user.Id).ToListAsync(); 94 | 95 | var response = userUnits.Select( 96 | unit => new UserUnitResponse 97 | { 98 | UnitId = unit.UnitId, 99 | HitPoints = unit.HitPoints 100 | }); 101 | 102 | return Ok(response); 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Controllers/WeatherForecastController.cs: -------------------------------------------------------------------------------- 1 | using BlazorBattles.Shared; 2 | using Microsoft.AspNetCore.Mvc; 3 | using Microsoft.Extensions.Logging; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Threading.Tasks; 8 | 9 | namespace BlazorBattles.Server.Controllers 10 | { 11 | [ApiController] 12 | [Route("[controller]")] 13 | public class WeatherForecastController : ControllerBase 14 | { 15 | private static readonly string[] Summaries = new[] 16 | { 17 | "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" 18 | }; 19 | 20 | private readonly ILogger _logger; 21 | 22 | public WeatherForecastController(ILogger logger) 23 | { 24 | _logger = logger; 25 | } 26 | 27 | [HttpGet] 28 | public IEnumerable Get() 29 | { 30 | var rng = new Random(); 31 | return Enumerable.Range(1, 6).Select(index => new WeatherForecast 32 | { 33 | Date = DateTime.Now.AddDays(index), 34 | TemperatureC = rng.Next(-20, 55), 35 | Summary = Summaries[rng.Next(Summaries.Length)] 36 | }) 37 | .ToArray(); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Data/AuthRepository.cs: -------------------------------------------------------------------------------- 1 | using BlazorBattles.Shared; 2 | using Microsoft.EntityFrameworkCore; 3 | using Microsoft.Extensions.Configuration; 4 | using Microsoft.IdentityModel.Tokens; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.IdentityModel.Tokens.Jwt; 8 | using System.Linq; 9 | using System.Security.Claims; 10 | using System.Security.Cryptography; 11 | using System.Threading.Tasks; 12 | 13 | namespace BlazorBattles.Server.Data 14 | { 15 | public class AuthRepository : IAuthRepository 16 | { 17 | private readonly DataContext _context; 18 | private readonly IConfiguration _configuration; 19 | 20 | public AuthRepository(DataContext context, IConfiguration configuration) 21 | { 22 | _context = context; 23 | _configuration = configuration; 24 | } 25 | 26 | public async Task> Login(string email, string password) 27 | { 28 | var response = new ServiceResponse(); 29 | var user = await _context.Users.FirstOrDefaultAsync(user => user.Email.ToLower().Equals(email.ToLower())); 30 | if (user == null) 31 | { 32 | response.Success = false; 33 | response.Message = "User not found."; 34 | } 35 | else if (!VerifyPasswordHash(password, user.PasswordHash, user.PasswordSalt)) 36 | { 37 | response.Success = false; 38 | response.Message = "Wrong password."; 39 | } 40 | else 41 | { 42 | response.Data = CreateToken(user); 43 | } 44 | 45 | return response; 46 | } 47 | 48 | public async Task> Register(User user, string password, int startUnitId) 49 | { 50 | if (await UserExists(user.Email)) 51 | { 52 | return new ServiceResponse { Success = false, Message = "User already exists." }; 53 | } 54 | 55 | CreatePasswordHash(password, out byte[] passwordHash, out byte[] passwordSalt); 56 | 57 | user.PasswordHash = passwordHash; 58 | user.PasswordSalt = passwordSalt; 59 | 60 | _context.Users.Add(user); 61 | await _context.SaveChangesAsync(); 62 | 63 | await AddStartingUnit(user, startUnitId); 64 | 65 | return new ServiceResponse { Data = user.Id, Message = "Registration successful!" }; 66 | } 67 | 68 | public async Task UserExists(string email) 69 | { 70 | if (await _context.Users.AnyAsync(user => user.Email.ToLower().Equals(email.ToLower()))) 71 | { 72 | return true; 73 | } 74 | return false; 75 | } 76 | 77 | private void CreatePasswordHash(string password, out byte[] passwordHash, out byte[] passwordSalt) 78 | { 79 | using (var hmac = new HMACSHA512()) 80 | { 81 | passwordSalt = hmac.Key; 82 | passwordHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password)); 83 | } 84 | } 85 | 86 | private bool VerifyPasswordHash(string password, byte[] passwordHash, byte[] passwordSalt) 87 | { 88 | using (var hmac = new HMACSHA512(passwordSalt)) 89 | { 90 | var computedHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password)); 91 | for (int i = 0; i < computedHash.Length; i++) 92 | { 93 | if (computedHash[i] != passwordHash[i]) 94 | { 95 | return false; 96 | } 97 | } 98 | return true; 99 | } 100 | } 101 | 102 | private string CreateToken(User user) 103 | { 104 | List claims = new List 105 | { 106 | new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()), 107 | new Claim(ClaimTypes.Name, user.Username) 108 | }; 109 | 110 | var key = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes( 111 | _configuration.GetSection("AppSettings:Token").Value)); 112 | 113 | var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature); 114 | 115 | var token = new JwtSecurityToken( 116 | claims: claims, 117 | expires: DateTime.Now.AddDays(1), 118 | signingCredentials: creds); 119 | 120 | var jwt = new JwtSecurityTokenHandler().WriteToken(token); 121 | 122 | return jwt; 123 | } 124 | 125 | private async Task AddStartingUnit(User user, int startUnitId) 126 | { 127 | var unit = await _context.Units.FirstOrDefaultAsync(u => u.Id == startUnitId); 128 | _context.UserUnits.Add(new UserUnit 129 | { 130 | UnitId = unit.Id, 131 | UserId = user.Id, 132 | HitPoints = unit.HitPoints 133 | }); 134 | await _context.SaveChangesAsync(); 135 | } 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Data/DataContext.cs: -------------------------------------------------------------------------------- 1 | using BlazorBattles.Shared; 2 | using Microsoft.EntityFrameworkCore; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Threading.Tasks; 7 | 8 | namespace BlazorBattles.Server.Data 9 | { 10 | public class DataContext : DbContext 11 | { 12 | public DataContext(DbContextOptions options) : base(options) 13 | { 14 | 15 | } 16 | 17 | protected override void OnModelCreating(ModelBuilder modelBuilder) 18 | { 19 | modelBuilder.Entity() 20 | .HasOne(b => b.Attacker) 21 | .WithMany() 22 | .OnDelete(DeleteBehavior.NoAction); 23 | 24 | modelBuilder.Entity() 25 | .HasOne(b => b.Opponent) 26 | .WithMany() 27 | .OnDelete(DeleteBehavior.NoAction); 28 | 29 | modelBuilder.Entity() 30 | .HasOne(b => b.Winner) 31 | .WithMany() 32 | .OnDelete(DeleteBehavior.NoAction); 33 | } 34 | 35 | public DbSet Units { get; set; } 36 | 37 | public DbSet Users { get; set; } 38 | 39 | public DbSet UserUnits { get; set; } 40 | 41 | public DbSet Battles { get; set; } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Data/IAuthRepository.cs: -------------------------------------------------------------------------------- 1 | using BlazorBattles.Shared; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | 7 | namespace BlazorBattles.Server.Data 8 | { 9 | public interface IAuthRepository 10 | { 11 | Task> Register(User user, string password, int startUnitId); 12 | Task> Login(string email, string password); 13 | Task UserExists(string email); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Migrations/20210511193816_Initial.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using BlazorBattles.Server.Data; 3 | using Microsoft.EntityFrameworkCore; 4 | using Microsoft.EntityFrameworkCore.Infrastructure; 5 | using Microsoft.EntityFrameworkCore.Metadata; 6 | using Microsoft.EntityFrameworkCore.Migrations; 7 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 8 | 9 | namespace BlazorBattles.Server.Migrations 10 | { 11 | [DbContext(typeof(DataContext))] 12 | [Migration("20210511193816_Initial")] 13 | partial class Initial 14 | { 15 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 16 | { 17 | #pragma warning disable 612, 618 18 | modelBuilder 19 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 20 | .HasAnnotation("ProductVersion", "5.0.5") 21 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 22 | 23 | modelBuilder.Entity("BlazorBattles.Shared.Unit", b => 24 | { 25 | b.Property("Id") 26 | .ValueGeneratedOnAdd() 27 | .HasColumnType("int") 28 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 29 | 30 | b.Property("Attack") 31 | .HasColumnType("int"); 32 | 33 | b.Property("BananaCost") 34 | .HasColumnType("int"); 35 | 36 | b.Property("Defense") 37 | .HasColumnType("int"); 38 | 39 | b.Property("HitPoints") 40 | .HasColumnType("int"); 41 | 42 | b.Property("Title") 43 | .HasColumnType("nvarchar(max)"); 44 | 45 | b.HasKey("Id"); 46 | 47 | b.ToTable("Units"); 48 | }); 49 | #pragma warning restore 612, 618 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Migrations/20210511193816_Initial.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore.Migrations; 2 | 3 | namespace BlazorBattles.Server.Migrations 4 | { 5 | public partial class Initial : Migration 6 | { 7 | protected override void Up(MigrationBuilder migrationBuilder) 8 | { 9 | migrationBuilder.CreateTable( 10 | name: "Units", 11 | columns: table => new 12 | { 13 | Id = table.Column(type: "int", nullable: false) 14 | .Annotation("SqlServer:Identity", "1, 1"), 15 | Title = table.Column(type: "nvarchar(max)", nullable: true), 16 | Attack = table.Column(type: "int", nullable: false), 17 | Defense = table.Column(type: "int", nullable: false), 18 | HitPoints = table.Column(type: "int", nullable: false), 19 | BananaCost = table.Column(type: "int", nullable: false) 20 | }, 21 | constraints: table => 22 | { 23 | table.PrimaryKey("PK_Units", x => x.Id); 24 | }); 25 | } 26 | 27 | protected override void Down(MigrationBuilder migrationBuilder) 28 | { 29 | migrationBuilder.DropTable( 30 | name: "Units"); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Migrations/20210512173123_UserModel.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using BlazorBattles.Server.Data; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.EntityFrameworkCore.Infrastructure; 6 | using Microsoft.EntityFrameworkCore.Metadata; 7 | using Microsoft.EntityFrameworkCore.Migrations; 8 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 9 | 10 | namespace BlazorBattles.Server.Migrations 11 | { 12 | [DbContext(typeof(DataContext))] 13 | [Migration("20210512173123_UserModel")] 14 | partial class UserModel 15 | { 16 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 17 | { 18 | #pragma warning disable 612, 618 19 | modelBuilder 20 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 21 | .HasAnnotation("ProductVersion", "5.0.5") 22 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 23 | 24 | modelBuilder.Entity("BlazorBattles.Shared.Unit", b => 25 | { 26 | b.Property("Id") 27 | .ValueGeneratedOnAdd() 28 | .HasColumnType("int") 29 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 30 | 31 | b.Property("Attack") 32 | .HasColumnType("int"); 33 | 34 | b.Property("BananaCost") 35 | .HasColumnType("int"); 36 | 37 | b.Property("Defense") 38 | .HasColumnType("int"); 39 | 40 | b.Property("HitPoints") 41 | .HasColumnType("int"); 42 | 43 | b.Property("Title") 44 | .HasColumnType("nvarchar(max)"); 45 | 46 | b.HasKey("Id"); 47 | 48 | b.ToTable("Units"); 49 | }); 50 | 51 | modelBuilder.Entity("BlazorBattles.Shared.User", b => 52 | { 53 | b.Property("Id") 54 | .ValueGeneratedOnAdd() 55 | .HasColumnType("int") 56 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 57 | 58 | b.Property("Bananas") 59 | .HasColumnType("int"); 60 | 61 | b.Property("DateCreated") 62 | .HasColumnType("datetime2"); 63 | 64 | b.Property("DateOfBirth") 65 | .HasColumnType("datetime2"); 66 | 67 | b.Property("Email") 68 | .HasColumnType("nvarchar(max)"); 69 | 70 | b.Property("IsConfirmed") 71 | .HasColumnType("bit"); 72 | 73 | b.Property("IsDeleted") 74 | .HasColumnType("bit"); 75 | 76 | b.Property("PasswordHash") 77 | .HasColumnType("varbinary(max)"); 78 | 79 | b.Property("PasswordSalt") 80 | .HasColumnType("varbinary(max)"); 81 | 82 | b.Property("Username") 83 | .HasColumnType("nvarchar(max)"); 84 | 85 | b.HasKey("Id"); 86 | 87 | b.ToTable("Users"); 88 | }); 89 | #pragma warning restore 612, 618 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Migrations/20210512173123_UserModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.EntityFrameworkCore.Migrations; 3 | 4 | namespace BlazorBattles.Server.Migrations 5 | { 6 | public partial class UserModel : Migration 7 | { 8 | protected override void Up(MigrationBuilder migrationBuilder) 9 | { 10 | migrationBuilder.CreateTable( 11 | name: "Users", 12 | columns: table => new 13 | { 14 | Id = table.Column(type: "int", nullable: false) 15 | .Annotation("SqlServer:Identity", "1, 1"), 16 | Email = table.Column(type: "nvarchar(max)", nullable: true), 17 | Username = table.Column(type: "nvarchar(max)", nullable: true), 18 | PasswordHash = table.Column(type: "varbinary(max)", nullable: true), 19 | PasswordSalt = table.Column(type: "varbinary(max)", nullable: true), 20 | Bananas = table.Column(type: "int", nullable: false), 21 | DateOfBirth = table.Column(type: "datetime2", nullable: false), 22 | IsConfirmed = table.Column(type: "bit", nullable: false), 23 | IsDeleted = table.Column(type: "bit", nullable: false), 24 | DateCreated = table.Column(type: "datetime2", nullable: false) 25 | }, 26 | constraints: table => 27 | { 28 | table.PrimaryKey("PK_Users", x => x.Id); 29 | }); 30 | } 31 | 32 | protected override void Down(MigrationBuilder migrationBuilder) 33 | { 34 | migrationBuilder.DropTable( 35 | name: "Users"); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Migrations/20210513084421_UserUnits.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using BlazorBattles.Server.Data; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.EntityFrameworkCore.Infrastructure; 6 | using Microsoft.EntityFrameworkCore.Metadata; 7 | using Microsoft.EntityFrameworkCore.Migrations; 8 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 9 | 10 | namespace BlazorBattles.Server.Migrations 11 | { 12 | [DbContext(typeof(DataContext))] 13 | [Migration("20210513084421_UserUnits")] 14 | partial class UserUnits 15 | { 16 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 17 | { 18 | #pragma warning disable 612, 618 19 | modelBuilder 20 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 21 | .HasAnnotation("ProductVersion", "5.0.5") 22 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 23 | 24 | modelBuilder.Entity("BlazorBattles.Shared.Unit", b => 25 | { 26 | b.Property("Id") 27 | .ValueGeneratedOnAdd() 28 | .HasColumnType("int") 29 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 30 | 31 | b.Property("Attack") 32 | .HasColumnType("int"); 33 | 34 | b.Property("BananaCost") 35 | .HasColumnType("int"); 36 | 37 | b.Property("Defense") 38 | .HasColumnType("int"); 39 | 40 | b.Property("HitPoints") 41 | .HasColumnType("int"); 42 | 43 | b.Property("Title") 44 | .HasColumnType("nvarchar(max)"); 45 | 46 | b.HasKey("Id"); 47 | 48 | b.ToTable("Units"); 49 | }); 50 | 51 | modelBuilder.Entity("BlazorBattles.Shared.User", b => 52 | { 53 | b.Property("Id") 54 | .ValueGeneratedOnAdd() 55 | .HasColumnType("int") 56 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 57 | 58 | b.Property("Bananas") 59 | .HasColumnType("int"); 60 | 61 | b.Property("DateCreated") 62 | .HasColumnType("datetime2"); 63 | 64 | b.Property("DateOfBirth") 65 | .HasColumnType("datetime2"); 66 | 67 | b.Property("Email") 68 | .HasColumnType("nvarchar(max)"); 69 | 70 | b.Property("IsConfirmed") 71 | .HasColumnType("bit"); 72 | 73 | b.Property("IsDeleted") 74 | .HasColumnType("bit"); 75 | 76 | b.Property("PasswordHash") 77 | .HasColumnType("varbinary(max)"); 78 | 79 | b.Property("PasswordSalt") 80 | .HasColumnType("varbinary(max)"); 81 | 82 | b.Property("Username") 83 | .HasColumnType("nvarchar(max)"); 84 | 85 | b.HasKey("Id"); 86 | 87 | b.ToTable("Users"); 88 | }); 89 | 90 | modelBuilder.Entity("BlazorBattles.Shared.UserUnit", b => 91 | { 92 | b.Property("Id") 93 | .ValueGeneratedOnAdd() 94 | .HasColumnType("int") 95 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 96 | 97 | b.Property("HitPoints") 98 | .HasColumnType("int"); 99 | 100 | b.Property("UnitId") 101 | .HasColumnType("int"); 102 | 103 | b.Property("UserId") 104 | .HasColumnType("int"); 105 | 106 | b.HasKey("Id"); 107 | 108 | b.HasIndex("UnitId"); 109 | 110 | b.HasIndex("UserId"); 111 | 112 | b.ToTable("UserUnits"); 113 | }); 114 | 115 | modelBuilder.Entity("BlazorBattles.Shared.UserUnit", b => 116 | { 117 | b.HasOne("BlazorBattles.Shared.Unit", "Unit") 118 | .WithMany() 119 | .HasForeignKey("UnitId") 120 | .OnDelete(DeleteBehavior.Cascade) 121 | .IsRequired(); 122 | 123 | b.HasOne("BlazorBattles.Shared.User", null) 124 | .WithMany("Units") 125 | .HasForeignKey("UserId") 126 | .OnDelete(DeleteBehavior.Cascade) 127 | .IsRequired(); 128 | 129 | b.Navigation("Unit"); 130 | }); 131 | 132 | modelBuilder.Entity("BlazorBattles.Shared.User", b => 133 | { 134 | b.Navigation("Units"); 135 | }); 136 | #pragma warning restore 612, 618 137 | } 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Migrations/20210513084421_UserUnits.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore.Migrations; 2 | 3 | namespace BlazorBattles.Server.Migrations 4 | { 5 | public partial class UserUnits : Migration 6 | { 7 | protected override void Up(MigrationBuilder migrationBuilder) 8 | { 9 | migrationBuilder.CreateTable( 10 | name: "UserUnits", 11 | columns: table => new 12 | { 13 | Id = table.Column(type: "int", nullable: false) 14 | .Annotation("SqlServer:Identity", "1, 1"), 15 | UserId = table.Column(type: "int", nullable: false), 16 | UnitId = table.Column(type: "int", nullable: false), 17 | HitPoints = table.Column(type: "int", nullable: false) 18 | }, 19 | constraints: table => 20 | { 21 | table.PrimaryKey("PK_UserUnits", x => x.Id); 22 | table.ForeignKey( 23 | name: "FK_UserUnits_Units_UnitId", 24 | column: x => x.UnitId, 25 | principalTable: "Units", 26 | principalColumn: "Id", 27 | onDelete: ReferentialAction.Cascade); 28 | table.ForeignKey( 29 | name: "FK_UserUnits_Users_UserId", 30 | column: x => x.UserId, 31 | principalTable: "Users", 32 | principalColumn: "Id", 33 | onDelete: ReferentialAction.Cascade); 34 | }); 35 | 36 | migrationBuilder.CreateIndex( 37 | name: "IX_UserUnits_UnitId", 38 | table: "UserUnits", 39 | column: "UnitId"); 40 | 41 | migrationBuilder.CreateIndex( 42 | name: "IX_UserUnits_UserId", 43 | table: "UserUnits", 44 | column: "UserId"); 45 | } 46 | 47 | protected override void Down(MigrationBuilder migrationBuilder) 48 | { 49 | migrationBuilder.DropTable( 50 | name: "UserUnits"); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Migrations/20210514081412_UserBattleStatistics.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using BlazorBattles.Server.Data; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.EntityFrameworkCore.Infrastructure; 6 | using Microsoft.EntityFrameworkCore.Metadata; 7 | using Microsoft.EntityFrameworkCore.Migrations; 8 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 9 | 10 | namespace BlazorBattles.Server.Migrations 11 | { 12 | [DbContext(typeof(DataContext))] 13 | [Migration("20210514081412_UserBattleStatistics")] 14 | partial class UserBattleStatistics 15 | { 16 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 17 | { 18 | #pragma warning disable 612, 618 19 | modelBuilder 20 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 21 | .HasAnnotation("ProductVersion", "5.0.5") 22 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 23 | 24 | modelBuilder.Entity("BlazorBattles.Shared.Unit", b => 25 | { 26 | b.Property("Id") 27 | .ValueGeneratedOnAdd() 28 | .HasColumnType("int") 29 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 30 | 31 | b.Property("Attack") 32 | .HasColumnType("int"); 33 | 34 | b.Property("BananaCost") 35 | .HasColumnType("int"); 36 | 37 | b.Property("Defense") 38 | .HasColumnType("int"); 39 | 40 | b.Property("HitPoints") 41 | .HasColumnType("int"); 42 | 43 | b.Property("Title") 44 | .HasColumnType("nvarchar(max)"); 45 | 46 | b.HasKey("Id"); 47 | 48 | b.ToTable("Units"); 49 | }); 50 | 51 | modelBuilder.Entity("BlazorBattles.Shared.User", b => 52 | { 53 | b.Property("Id") 54 | .ValueGeneratedOnAdd() 55 | .HasColumnType("int") 56 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 57 | 58 | b.Property("Bananas") 59 | .HasColumnType("int"); 60 | 61 | b.Property("Battles") 62 | .HasColumnType("int"); 63 | 64 | b.Property("DateCreated") 65 | .HasColumnType("datetime2"); 66 | 67 | b.Property("DateOfBirth") 68 | .HasColumnType("datetime2"); 69 | 70 | b.Property("Defeats") 71 | .HasColumnType("int"); 72 | 73 | b.Property("Email") 74 | .HasColumnType("nvarchar(max)"); 75 | 76 | b.Property("IsConfirmed") 77 | .HasColumnType("bit"); 78 | 79 | b.Property("IsDeleted") 80 | .HasColumnType("bit"); 81 | 82 | b.Property("PasswordHash") 83 | .HasColumnType("varbinary(max)"); 84 | 85 | b.Property("PasswordSalt") 86 | .HasColumnType("varbinary(max)"); 87 | 88 | b.Property("Username") 89 | .HasColumnType("nvarchar(max)"); 90 | 91 | b.Property("Victories") 92 | .HasColumnType("int"); 93 | 94 | b.HasKey("Id"); 95 | 96 | b.ToTable("Users"); 97 | }); 98 | 99 | modelBuilder.Entity("BlazorBattles.Shared.UserUnit", b => 100 | { 101 | b.Property("Id") 102 | .ValueGeneratedOnAdd() 103 | .HasColumnType("int") 104 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 105 | 106 | b.Property("HitPoints") 107 | .HasColumnType("int"); 108 | 109 | b.Property("UnitId") 110 | .HasColumnType("int"); 111 | 112 | b.Property("UserId") 113 | .HasColumnType("int"); 114 | 115 | b.HasKey("Id"); 116 | 117 | b.HasIndex("UnitId"); 118 | 119 | b.HasIndex("UserId"); 120 | 121 | b.ToTable("UserUnits"); 122 | }); 123 | 124 | modelBuilder.Entity("BlazorBattles.Shared.UserUnit", b => 125 | { 126 | b.HasOne("BlazorBattles.Shared.Unit", "Unit") 127 | .WithMany() 128 | .HasForeignKey("UnitId") 129 | .OnDelete(DeleteBehavior.Cascade) 130 | .IsRequired(); 131 | 132 | b.HasOne("BlazorBattles.Shared.User", null) 133 | .WithMany("Units") 134 | .HasForeignKey("UserId") 135 | .OnDelete(DeleteBehavior.Cascade) 136 | .IsRequired(); 137 | 138 | b.Navigation("Unit"); 139 | }); 140 | 141 | modelBuilder.Entity("BlazorBattles.Shared.User", b => 142 | { 143 | b.Navigation("Units"); 144 | }); 145 | #pragma warning restore 612, 618 146 | } 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Migrations/20210514081412_UserBattleStatistics.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore.Migrations; 2 | 3 | namespace BlazorBattles.Server.Migrations 4 | { 5 | public partial class UserBattleStatistics : Migration 6 | { 7 | protected override void Up(MigrationBuilder migrationBuilder) 8 | { 9 | migrationBuilder.AddColumn( 10 | name: "Battles", 11 | table: "Users", 12 | type: "int", 13 | nullable: false, 14 | defaultValue: 0); 15 | 16 | migrationBuilder.AddColumn( 17 | name: "Defeats", 18 | table: "Users", 19 | type: "int", 20 | nullable: false, 21 | defaultValue: 0); 22 | 23 | migrationBuilder.AddColumn( 24 | name: "Victories", 25 | table: "Users", 26 | type: "int", 27 | nullable: false, 28 | defaultValue: 0); 29 | } 30 | 31 | protected override void Down(MigrationBuilder migrationBuilder) 32 | { 33 | migrationBuilder.DropColumn( 34 | name: "Battles", 35 | table: "Users"); 36 | 37 | migrationBuilder.DropColumn( 38 | name: "Defeats", 39 | table: "Users"); 40 | 41 | migrationBuilder.DropColumn( 42 | name: "Victories", 43 | table: "Users"); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Migrations/20210514195306_Battles.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using BlazorBattles.Server.Data; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.EntityFrameworkCore.Infrastructure; 6 | using Microsoft.EntityFrameworkCore.Metadata; 7 | using Microsoft.EntityFrameworkCore.Migrations; 8 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 9 | 10 | namespace BlazorBattles.Server.Migrations 11 | { 12 | [DbContext(typeof(DataContext))] 13 | [Migration("20210514195306_Battles")] 14 | partial class Battles 15 | { 16 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 17 | { 18 | #pragma warning disable 612, 618 19 | modelBuilder 20 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 21 | .HasAnnotation("ProductVersion", "5.0.5") 22 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 23 | 24 | modelBuilder.Entity("BlazorBattles.Shared.Battle", b => 25 | { 26 | b.Property("Id") 27 | .ValueGeneratedOnAdd() 28 | .HasColumnType("int") 29 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 30 | 31 | b.Property("AttackerId") 32 | .HasColumnType("int"); 33 | 34 | b.Property("BattleDate") 35 | .HasColumnType("datetime2"); 36 | 37 | b.Property("OpponentId") 38 | .HasColumnType("int"); 39 | 40 | b.Property("RoundsFought") 41 | .HasColumnType("int"); 42 | 43 | b.Property("WinnerDamage") 44 | .HasColumnType("int"); 45 | 46 | b.Property("WinnerId") 47 | .HasColumnType("int"); 48 | 49 | b.HasKey("Id"); 50 | 51 | b.HasIndex("AttackerId"); 52 | 53 | b.HasIndex("OpponentId"); 54 | 55 | b.HasIndex("WinnerId"); 56 | 57 | b.ToTable("Battles"); 58 | }); 59 | 60 | modelBuilder.Entity("BlazorBattles.Shared.Unit", b => 61 | { 62 | b.Property("Id") 63 | .ValueGeneratedOnAdd() 64 | .HasColumnType("int") 65 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 66 | 67 | b.Property("Attack") 68 | .HasColumnType("int"); 69 | 70 | b.Property("BananaCost") 71 | .HasColumnType("int"); 72 | 73 | b.Property("Defense") 74 | .HasColumnType("int"); 75 | 76 | b.Property("HitPoints") 77 | .HasColumnType("int"); 78 | 79 | b.Property("Title") 80 | .HasColumnType("nvarchar(max)"); 81 | 82 | b.HasKey("Id"); 83 | 84 | b.ToTable("Units"); 85 | }); 86 | 87 | modelBuilder.Entity("BlazorBattles.Shared.User", b => 88 | { 89 | b.Property("Id") 90 | .ValueGeneratedOnAdd() 91 | .HasColumnType("int") 92 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 93 | 94 | b.Property("Bananas") 95 | .HasColumnType("int"); 96 | 97 | b.Property("Battles") 98 | .HasColumnType("int"); 99 | 100 | b.Property("DateCreated") 101 | .HasColumnType("datetime2"); 102 | 103 | b.Property("DateOfBirth") 104 | .HasColumnType("datetime2"); 105 | 106 | b.Property("Defeats") 107 | .HasColumnType("int"); 108 | 109 | b.Property("Email") 110 | .HasColumnType("nvarchar(max)"); 111 | 112 | b.Property("IsConfirmed") 113 | .HasColumnType("bit"); 114 | 115 | b.Property("IsDeleted") 116 | .HasColumnType("bit"); 117 | 118 | b.Property("PasswordHash") 119 | .HasColumnType("varbinary(max)"); 120 | 121 | b.Property("PasswordSalt") 122 | .HasColumnType("varbinary(max)"); 123 | 124 | b.Property("Username") 125 | .HasColumnType("nvarchar(max)"); 126 | 127 | b.Property("Victories") 128 | .HasColumnType("int"); 129 | 130 | b.HasKey("Id"); 131 | 132 | b.ToTable("Users"); 133 | }); 134 | 135 | modelBuilder.Entity("BlazorBattles.Shared.UserUnit", b => 136 | { 137 | b.Property("Id") 138 | .ValueGeneratedOnAdd() 139 | .HasColumnType("int") 140 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 141 | 142 | b.Property("HitPoints") 143 | .HasColumnType("int"); 144 | 145 | b.Property("UnitId") 146 | .HasColumnType("int"); 147 | 148 | b.Property("UserId") 149 | .HasColumnType("int"); 150 | 151 | b.HasKey("Id"); 152 | 153 | b.HasIndex("UnitId"); 154 | 155 | b.HasIndex("UserId"); 156 | 157 | b.ToTable("UserUnits"); 158 | }); 159 | 160 | modelBuilder.Entity("BlazorBattles.Shared.Battle", b => 161 | { 162 | b.HasOne("BlazorBattles.Shared.User", "Attacker") 163 | .WithMany() 164 | .HasForeignKey("AttackerId") 165 | .OnDelete(DeleteBehavior.NoAction) 166 | .IsRequired(); 167 | 168 | b.HasOne("BlazorBattles.Shared.User", "Opponent") 169 | .WithMany() 170 | .HasForeignKey("OpponentId") 171 | .OnDelete(DeleteBehavior.NoAction) 172 | .IsRequired(); 173 | 174 | b.HasOne("BlazorBattles.Shared.User", "Winner") 175 | .WithMany() 176 | .HasForeignKey("WinnerId") 177 | .OnDelete(DeleteBehavior.NoAction) 178 | .IsRequired(); 179 | 180 | b.Navigation("Attacker"); 181 | 182 | b.Navigation("Opponent"); 183 | 184 | b.Navigation("Winner"); 185 | }); 186 | 187 | modelBuilder.Entity("BlazorBattles.Shared.UserUnit", b => 188 | { 189 | b.HasOne("BlazorBattles.Shared.Unit", "Unit") 190 | .WithMany() 191 | .HasForeignKey("UnitId") 192 | .OnDelete(DeleteBehavior.Cascade) 193 | .IsRequired(); 194 | 195 | b.HasOne("BlazorBattles.Shared.User", null) 196 | .WithMany("Units") 197 | .HasForeignKey("UserId") 198 | .OnDelete(DeleteBehavior.Cascade) 199 | .IsRequired(); 200 | 201 | b.Navigation("Unit"); 202 | }); 203 | 204 | modelBuilder.Entity("BlazorBattles.Shared.User", b => 205 | { 206 | b.Navigation("Units"); 207 | }); 208 | #pragma warning restore 612, 618 209 | } 210 | } 211 | } 212 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Migrations/20210514195306_Battles.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.EntityFrameworkCore.Migrations; 3 | 4 | namespace BlazorBattles.Server.Migrations 5 | { 6 | public partial class Battles : Migration 7 | { 8 | protected override void Up(MigrationBuilder migrationBuilder) 9 | { 10 | migrationBuilder.CreateTable( 11 | name: "Battles", 12 | columns: table => new 13 | { 14 | Id = table.Column(type: "int", nullable: false) 15 | .Annotation("SqlServer:Identity", "1, 1"), 16 | AttackerId = table.Column(type: "int", nullable: false), 17 | OpponentId = table.Column(type: "int", nullable: false), 18 | WinnerId = table.Column(type: "int", nullable: false), 19 | WinnerDamage = table.Column(type: "int", nullable: false), 20 | RoundsFought = table.Column(type: "int", nullable: false), 21 | BattleDate = table.Column(type: "datetime2", nullable: false) 22 | }, 23 | constraints: table => 24 | { 25 | table.PrimaryKey("PK_Battles", x => x.Id); 26 | table.ForeignKey( 27 | name: "FK_Battles_Users_AttackerId", 28 | column: x => x.AttackerId, 29 | principalTable: "Users", 30 | principalColumn: "Id"); 31 | table.ForeignKey( 32 | name: "FK_Battles_Users_OpponentId", 33 | column: x => x.OpponentId, 34 | principalTable: "Users", 35 | principalColumn: "Id"); 36 | table.ForeignKey( 37 | name: "FK_Battles_Users_WinnerId", 38 | column: x => x.WinnerId, 39 | principalTable: "Users", 40 | principalColumn: "Id"); 41 | }); 42 | 43 | migrationBuilder.CreateIndex( 44 | name: "IX_Battles_AttackerId", 45 | table: "Battles", 46 | column: "AttackerId"); 47 | 48 | migrationBuilder.CreateIndex( 49 | name: "IX_Battles_OpponentId", 50 | table: "Battles", 51 | column: "OpponentId"); 52 | 53 | migrationBuilder.CreateIndex( 54 | name: "IX_Battles_WinnerId", 55 | table: "Battles", 56 | column: "WinnerId"); 57 | } 58 | 59 | protected override void Down(MigrationBuilder migrationBuilder) 60 | { 61 | migrationBuilder.DropTable( 62 | name: "Battles"); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Migrations/DataContextModelSnapshot.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using BlazorBattles.Server.Data; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.EntityFrameworkCore.Infrastructure; 6 | using Microsoft.EntityFrameworkCore.Metadata; 7 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 8 | 9 | namespace BlazorBattles.Server.Migrations 10 | { 11 | [DbContext(typeof(DataContext))] 12 | partial class DataContextModelSnapshot : ModelSnapshot 13 | { 14 | protected override void BuildModel(ModelBuilder modelBuilder) 15 | { 16 | #pragma warning disable 612, 618 17 | modelBuilder 18 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 19 | .HasAnnotation("ProductVersion", "5.0.5") 20 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 21 | 22 | modelBuilder.Entity("BlazorBattles.Shared.Battle", b => 23 | { 24 | b.Property("Id") 25 | .ValueGeneratedOnAdd() 26 | .HasColumnType("int") 27 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 28 | 29 | b.Property("AttackerId") 30 | .HasColumnType("int"); 31 | 32 | b.Property("BattleDate") 33 | .HasColumnType("datetime2"); 34 | 35 | b.Property("OpponentId") 36 | .HasColumnType("int"); 37 | 38 | b.Property("RoundsFought") 39 | .HasColumnType("int"); 40 | 41 | b.Property("WinnerDamage") 42 | .HasColumnType("int"); 43 | 44 | b.Property("WinnerId") 45 | .HasColumnType("int"); 46 | 47 | b.HasKey("Id"); 48 | 49 | b.HasIndex("AttackerId"); 50 | 51 | b.HasIndex("OpponentId"); 52 | 53 | b.HasIndex("WinnerId"); 54 | 55 | b.ToTable("Battles"); 56 | }); 57 | 58 | modelBuilder.Entity("BlazorBattles.Shared.Unit", b => 59 | { 60 | b.Property("Id") 61 | .ValueGeneratedOnAdd() 62 | .HasColumnType("int") 63 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 64 | 65 | b.Property("Attack") 66 | .HasColumnType("int"); 67 | 68 | b.Property("BananaCost") 69 | .HasColumnType("int"); 70 | 71 | b.Property("Defense") 72 | .HasColumnType("int"); 73 | 74 | b.Property("HitPoints") 75 | .HasColumnType("int"); 76 | 77 | b.Property("Title") 78 | .HasColumnType("nvarchar(max)"); 79 | 80 | b.HasKey("Id"); 81 | 82 | b.ToTable("Units"); 83 | }); 84 | 85 | modelBuilder.Entity("BlazorBattles.Shared.User", b => 86 | { 87 | b.Property("Id") 88 | .ValueGeneratedOnAdd() 89 | .HasColumnType("int") 90 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 91 | 92 | b.Property("Bananas") 93 | .HasColumnType("int"); 94 | 95 | b.Property("Battles") 96 | .HasColumnType("int"); 97 | 98 | b.Property("DateCreated") 99 | .HasColumnType("datetime2"); 100 | 101 | b.Property("DateOfBirth") 102 | .HasColumnType("datetime2"); 103 | 104 | b.Property("Defeats") 105 | .HasColumnType("int"); 106 | 107 | b.Property("Email") 108 | .HasColumnType("nvarchar(max)"); 109 | 110 | b.Property("IsConfirmed") 111 | .HasColumnType("bit"); 112 | 113 | b.Property("IsDeleted") 114 | .HasColumnType("bit"); 115 | 116 | b.Property("PasswordHash") 117 | .HasColumnType("varbinary(max)"); 118 | 119 | b.Property("PasswordSalt") 120 | .HasColumnType("varbinary(max)"); 121 | 122 | b.Property("Username") 123 | .HasColumnType("nvarchar(max)"); 124 | 125 | b.Property("Victories") 126 | .HasColumnType("int"); 127 | 128 | b.HasKey("Id"); 129 | 130 | b.ToTable("Users"); 131 | }); 132 | 133 | modelBuilder.Entity("BlazorBattles.Shared.UserUnit", b => 134 | { 135 | b.Property("Id") 136 | .ValueGeneratedOnAdd() 137 | .HasColumnType("int") 138 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 139 | 140 | b.Property("HitPoints") 141 | .HasColumnType("int"); 142 | 143 | b.Property("UnitId") 144 | .HasColumnType("int"); 145 | 146 | b.Property("UserId") 147 | .HasColumnType("int"); 148 | 149 | b.HasKey("Id"); 150 | 151 | b.HasIndex("UnitId"); 152 | 153 | b.HasIndex("UserId"); 154 | 155 | b.ToTable("UserUnits"); 156 | }); 157 | 158 | modelBuilder.Entity("BlazorBattles.Shared.Battle", b => 159 | { 160 | b.HasOne("BlazorBattles.Shared.User", "Attacker") 161 | .WithMany() 162 | .HasForeignKey("AttackerId") 163 | .OnDelete(DeleteBehavior.NoAction) 164 | .IsRequired(); 165 | 166 | b.HasOne("BlazorBattles.Shared.User", "Opponent") 167 | .WithMany() 168 | .HasForeignKey("OpponentId") 169 | .OnDelete(DeleteBehavior.NoAction) 170 | .IsRequired(); 171 | 172 | b.HasOne("BlazorBattles.Shared.User", "Winner") 173 | .WithMany() 174 | .HasForeignKey("WinnerId") 175 | .OnDelete(DeleteBehavior.NoAction) 176 | .IsRequired(); 177 | 178 | b.Navigation("Attacker"); 179 | 180 | b.Navigation("Opponent"); 181 | 182 | b.Navigation("Winner"); 183 | }); 184 | 185 | modelBuilder.Entity("BlazorBattles.Shared.UserUnit", b => 186 | { 187 | b.HasOne("BlazorBattles.Shared.Unit", "Unit") 188 | .WithMany() 189 | .HasForeignKey("UnitId") 190 | .OnDelete(DeleteBehavior.Cascade) 191 | .IsRequired(); 192 | 193 | b.HasOne("BlazorBattles.Shared.User", null) 194 | .WithMany("Units") 195 | .HasForeignKey("UserId") 196 | .OnDelete(DeleteBehavior.Cascade) 197 | .IsRequired(); 198 | 199 | b.Navigation("Unit"); 200 | }); 201 | 202 | modelBuilder.Entity("BlazorBattles.Shared.User", b => 203 | { 204 | b.Navigation("Units"); 205 | }); 206 | #pragma warning restore 612, 618 207 | } 208 | } 209 | } 210 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Pages/Error.cshtml: -------------------------------------------------------------------------------- 1 | @page 2 | @model BlazorBattles.Server.Pages.ErrorModel 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Error 11 | 12 | 13 | 14 | 15 | 16 |
17 |
18 |

Error.

19 |

An error occurred while processing your request.

20 | 21 | @if (Model.ShowRequestId) 22 | { 23 |

24 | Request ID: @Model.RequestId 25 |

26 | } 27 | 28 |

Development Mode

29 |

30 | Swapping to the Development environment displays detailed information about the error that occurred. 31 |

32 |

33 | The Development environment shouldn't be enabled for deployed applications. 34 | It can result in displaying sensitive information from exceptions to end users. 35 | For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development 36 | and restarting the app. 37 |

38 |
39 |
40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Pages/Error.cshtml.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc; 2 | using Microsoft.AspNetCore.Mvc.RazorPages; 3 | using Microsoft.Extensions.Logging; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Diagnostics; 7 | using System.Linq; 8 | using System.Threading.Tasks; 9 | 10 | namespace BlazorBattles.Server.Pages 11 | { 12 | [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] 13 | [IgnoreAntiforgeryToken] 14 | public class ErrorModel : PageModel 15 | { 16 | public string RequestId { get; set; } 17 | 18 | public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); 19 | 20 | private readonly ILogger _logger; 21 | 22 | public ErrorModel(ILogger logger) 23 | { 24 | _logger = logger; 25 | } 26 | 27 | public void OnGet() 28 | { 29 | RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Hosting; 2 | using Microsoft.Extensions.Configuration; 3 | using Microsoft.Extensions.Hosting; 4 | using Microsoft.Extensions.Logging; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Threading.Tasks; 9 | 10 | namespace BlazorBattles.Server 11 | { 12 | public class Program 13 | { 14 | public static void Main(string[] args) 15 | { 16 | CreateHostBuilder(args).Build().Run(); 17 | } 18 | 19 | public static IHostBuilder CreateHostBuilder(string[] args) => 20 | Host.CreateDefaultBuilder(args) 21 | .ConfigureWebHostDefaults(webBuilder => 22 | { 23 | webBuilder.UseStartup(); 24 | }); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:62414", 7 | "sslPort": 44394 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "BlazorBattles.Server": { 20 | "commandName": "Project", 21 | "dotnetRunMessages": "true", 22 | "launchBrowser": true, 23 | "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", 24 | "applicationUrl": "https://localhost:5001;http://localhost:5000", 25 | "environmentVariables": { 26 | "ASPNETCORE_ENVIRONMENT": "Development" 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Services/IUtilityService.cs: -------------------------------------------------------------------------------- 1 | using BlazorBattles.Shared; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | 7 | namespace BlazorBattles.Server.Services 8 | { 9 | public interface IUtilityService 10 | { 11 | Task GetUser(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Services/UtilityService.cs: -------------------------------------------------------------------------------- 1 | using BlazorBattles.Server.Data; 2 | using BlazorBattles.Shared; 3 | using Microsoft.AspNetCore.Http; 4 | using Microsoft.EntityFrameworkCore; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Security.Claims; 9 | using System.Threading.Tasks; 10 | 11 | namespace BlazorBattles.Server.Services 12 | { 13 | public class UtilityService : IUtilityService 14 | { 15 | private readonly DataContext _context; 16 | private readonly IHttpContextAccessor _httpContextAccessor; 17 | 18 | public UtilityService(DataContext context, IHttpContextAccessor httpContextAccessor) 19 | { 20 | _context = context; 21 | _httpContextAccessor = httpContextAccessor; 22 | } 23 | 24 | public async Task GetUser() 25 | { 26 | var userId = int.Parse(_httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier)); 27 | var user = await _context.Users.FirstOrDefaultAsync(u => u.Id == userId); 28 | return user; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /BlazorBattles/Server/Startup.cs: -------------------------------------------------------------------------------- 1 | using BlazorBattles.Server.Data; 2 | using BlazorBattles.Server.Services; 3 | using Microsoft.AspNetCore.Authentication.JwtBearer; 4 | using Microsoft.AspNetCore.Builder; 5 | using Microsoft.AspNetCore.Hosting; 6 | using Microsoft.AspNetCore.Http; 7 | using Microsoft.AspNetCore.HttpsPolicy; 8 | using Microsoft.AspNetCore.ResponseCompression; 9 | using Microsoft.EntityFrameworkCore; 10 | using Microsoft.Extensions.Configuration; 11 | using Microsoft.Extensions.DependencyInjection; 12 | using Microsoft.Extensions.Hosting; 13 | using Microsoft.IdentityModel.Tokens; 14 | using System.Linq; 15 | 16 | namespace BlazorBattles.Server 17 | { 18 | public class Startup 19 | { 20 | public Startup(IConfiguration configuration) 21 | { 22 | Configuration = configuration; 23 | } 24 | 25 | public IConfiguration Configuration { get; } 26 | 27 | // This method gets called by the runtime. Use this method to add services to the container. 28 | // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 29 | public void ConfigureServices(IServiceCollection services) 30 | { 31 | services.AddDbContext(options => 32 | options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); 33 | services.AddControllersWithViews(); 34 | services.AddRazorPages(); 35 | services.AddScoped(); 36 | services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) 37 | .AddJwtBearer(options => 38 | { 39 | options.TokenValidationParameters = new TokenValidationParameters 40 | { 41 | ValidateIssuerSigningKey = true, 42 | IssuerSigningKey = new SymmetricSecurityKey(System.Text.Encoding.ASCII.GetBytes( 43 | Configuration.GetSection("AppSettings:Token").Value)), 44 | ValidateIssuer = false, 45 | ValidateAudience = false 46 | }; 47 | }); 48 | services.AddScoped(); 49 | services.AddSingleton(); 50 | } 51 | 52 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 53 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 54 | { 55 | if (env.IsDevelopment()) 56 | { 57 | app.UseDeveloperExceptionPage(); 58 | app.UseWebAssemblyDebugging(); 59 | } 60 | else 61 | { 62 | app.UseExceptionHandler("/Error"); 63 | // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. 64 | app.UseHsts(); 65 | } 66 | 67 | app.UseHttpsRedirection(); 68 | app.UseBlazorFrameworkFiles(); 69 | app.UseStaticFiles(); 70 | 71 | app.UseRouting(); 72 | 73 | app.UseAuthentication(); 74 | app.UseAuthorization(); 75 | 76 | app.UseEndpoints(endpoints => 77 | { 78 | endpoints.MapRazorPages(); 79 | endpoints.MapControllers(); 80 | endpoints.MapFallbackToFile("index.html"); 81 | }); 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /BlazorBattles/Server/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft": "Warning", 6 | "Microsoft.Hosting.Lifetime": "Information" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /BlazorBattles/Server/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "ConnectionStrings": { 3 | "DefaultConnection": "Server=localhost\\SQLEXPRESS; Database=blazorbattles; Trusted_Connection=true;" 4 | }, 5 | "AppSettings": { 6 | "Token": "my top secret key" 7 | }, 8 | "Logging": { 9 | "LogLevel": { 10 | "Default": "Information", 11 | "Microsoft": "Warning", 12 | "Microsoft.Hosting.Lifetime": "Information" 13 | } 14 | }, 15 | "AllowedHosts": "*" 16 | } 17 | -------------------------------------------------------------------------------- /BlazorBattles/Shared/Battle.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace BlazorBattles.Shared 8 | { 9 | public class Battle 10 | { 11 | public int Id { get; set; } 12 | public User Attacker { get; set; } 13 | public int AttackerId { get; set; } 14 | public User Opponent { get; set; } 15 | public int OpponentId { get; set; } 16 | public User Winner { get; set; } 17 | public int WinnerId { get; set; } 18 | public int WinnerDamage { get; set; } 19 | public int RoundsFought { get; set; } 20 | public DateTime BattleDate { get; set; } = DateTime.Now; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /BlazorBattles/Shared/BattleHistoryEntry.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace BlazorBattles.Shared 8 | { 9 | public class BattleHistoryEntry 10 | { 11 | public int BattleId { get; set; } 12 | public DateTime BattleDate { get; set; } 13 | public int AttackerId { get; set; } 14 | public int OpponentId { get; set; } 15 | public bool YouWon { get; set; } 16 | public string AttackerName { get; set; } 17 | public string OpponentName { get; set; } 18 | public int RoundsFought { get; set; } 19 | public int WinnerDamageDealt { get; set; } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /BlazorBattles/Shared/BattleResult.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace BlazorBattles.Shared 8 | { 9 | public class BattleResult 10 | { 11 | public IList Log { get; set; } = new List(); 12 | public int AttackerDamageSum { get; set; } 13 | public int OpponentDamageSum { get; set; } 14 | public bool IsVictory { get; set; } 15 | public int RoundsFought { get; set; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /BlazorBattles/Shared/BlazorBattles.Shared.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /BlazorBattles/Shared/ServiceResponse.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace BlazorBattles.Shared 8 | { 9 | public class ServiceResponse 10 | { 11 | public T Data { get; set; } 12 | public bool Success { get; set; } = true; 13 | public string Message { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /BlazorBattles/Shared/Unit.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace BlazorBattles.Shared 8 | { 9 | public class Unit 10 | { 11 | public int Id { get; set; } 12 | public string Title { get; set; } 13 | public int Attack { get; set; } 14 | public int Defense { get; set; } 15 | public int HitPoints { get; set; } = 100; 16 | public int BananaCost { get; set; } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /BlazorBattles/Shared/User.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace BlazorBattles.Shared 8 | { 9 | public class User 10 | { 11 | public int Id { get; set; } 12 | public string Email { get; set; } 13 | public string Username { get; set; } 14 | public byte[] PasswordHash { get; set; } 15 | public byte[] PasswordSalt { get; set; } 16 | public int Bananas { get; set; } 17 | public DateTime DateOfBirth { get; set; } 18 | public bool IsConfirmed { get; set; } 19 | public bool IsDeleted { get; set; } 20 | public DateTime DateCreated { get; set; } = DateTime.Now; 21 | public List Units { get; set; } 22 | public int Battles { get; set; } 23 | public int Victories { get; set; } 24 | public int Defeats { get; set; } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /BlazorBattles/Shared/UserLogin.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace BlazorBattles.Shared 9 | { 10 | public class UserLogin 11 | { 12 | [Required(ErrorMessage = "Please enter an Email Address.")] 13 | public string Email { get; set; } 14 | [Required] 15 | public string Password { get; set; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /BlazorBattles/Shared/UserRegister.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace BlazorBattles.Shared 9 | { 10 | public class UserRegister 11 | { 12 | [Required, EmailAddress] 13 | public string Email { get; set; } 14 | [StringLength(16, ErrorMessage = "Your username is too long (16 characters max).")] 15 | public string Username { get; set; } 16 | public string Bio { get; set; } 17 | [Required, StringLength(100, MinimumLength = 6)] 18 | public string Password { get; set; } 19 | [Compare("Password", ErrorMessage = "The passwords do not match.")] 20 | public string ConfirmPassword { get; set; } 21 | public int StartUnitId { get; set; } = 1; 22 | [Range(0, 1000, ErrorMessage = "Please choose a number between 0 and 1000.")] 23 | public int Bananas { get; set; } = 100; 24 | public DateTime DateOfBirth { get; set; } = DateTime.Now; 25 | [Range(typeof(bool), "true", "true", ErrorMessage = "Only confirmed users can play!")] 26 | public bool IsConfirmed { get; set; } = true; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /BlazorBattles/Shared/UserStatistic.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace BlazorBattles.Shared 8 | { 9 | public class UserStatistic 10 | { 11 | public int Rank { get; set; } 12 | public int UserId { get; set; } 13 | public string Username { get; set; } 14 | public int Battles { get; set; } 15 | public int Victories { get; set; } 16 | public int Defeats { get; set; } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /BlazorBattles/Shared/UserUnit.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace BlazorBattles.Shared 8 | { 9 | public class UserUnit 10 | { 11 | public int Id { get; set; } 12 | public int UserId { get; set; } 13 | public Unit Unit { get; set; } 14 | public int UnitId { get; set; } 15 | public int HitPoints { get; set; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /BlazorBattles/Shared/UserUnitResponse.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace BlazorBattles.Shared 8 | { 9 | public class UserUnitResponse 10 | { 11 | public int UnitId { get; set; } 12 | public int HitPoints { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /BlazorBattles/Shared/WeatherForecast.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace BlazorBattles.Shared 6 | { 7 | public class WeatherForecast 8 | { 9 | public DateTime Date { get; set; } 10 | 11 | public int TemperatureC { get; set; } 12 | 13 | public string Summary { get; set; } 14 | 15 | public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); 16 | } 17 | } 18 | --------------------------------------------------------------------------------