├── .gitignore ├── .vscode ├── launch.json └── tasks.json ├── AutoMapperProfile.cs ├── Controllers ├── AuthController.cs ├── CharacterController.cs ├── FightController.cs ├── WeaponController.cs └── WeatherForecastController.cs ├── Data ├── AuthRepository.cs ├── DataContext.cs └── IAuthRepository.cs ├── Dtos ├── Character │ ├── AddCharacterDto.cs │ ├── AddCharacterSkillDto.cs │ ├── GetCharacterDto.cs │ └── UpdateCharacterDto.cs ├── Fight │ ├── AttackResultDto.cs │ ├── FightRequestDto.cs │ ├── FightResultDto.cs │ ├── HighscoreDto.cs │ ├── SkillAttackDto.cs │ └── WeaponAttackDto.cs ├── Skill │ └── GetSkillDto.cs ├── User │ ├── UserLoginDto.cs │ └── UserRegisterDto.cs └── Weapon │ ├── AddWeaponDto.cs │ └── GetWeaponDto.cs ├── Migrations ├── 20220315145521_InitialCreate.Designer.cs ├── 20220315145521_InitialCreate.cs ├── 20220316151920_UserModel.Designer.cs ├── 20220316151920_UserModel.cs ├── 20220317173055_UserCharacterRelationship.Designer.cs ├── 20220317173055_UserCharacterRelationship.cs ├── 20220624203034_Weapon.Designer.cs ├── 20220624203034_Weapon.cs ├── 20220624211428_Skill.Designer.cs ├── 20220624211428_Skill.cs ├── 20220624212109_SkillSeeding.Designer.cs ├── 20220624212109_SkillSeeding.cs ├── 20220624231340_FightProperties.Designer.cs ├── 20220624231340_FightProperties.cs └── DataContextModelSnapshot.cs ├── Models ├── Character.cs ├── RpgClass.cs ├── ServiceResponse.cs ├── Skill.cs ├── User.cs └── Weapon.cs ├── Program.cs ├── Properties └── launchSettings.json ├── Services ├── CharacterService │ ├── CharacterService.cs │ └── ICharacterService.cs ├── FightService │ ├── FightService.cs │ └── IFightService.cs └── WeaponService │ ├── IWeaponService.cs │ └── WeaponService.cs ├── WeatherForecast.cs ├── appsettings.Development.json ├── appsettings.json └── dotnet-rpg.csproj /.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 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # Tye 66 | .tye/ 67 | 68 | # ASP.NET Scaffolding 69 | ScaffoldingReadMe.txt 70 | 71 | # StyleCop 72 | StyleCopReport.xml 73 | 74 | # Files built by Visual Studio 75 | *_i.c 76 | *_p.c 77 | *_h.h 78 | *.ilk 79 | *.meta 80 | *.obj 81 | *.iobj 82 | *.pch 83 | *.pdb 84 | *.ipdb 85 | *.pgc 86 | *.pgd 87 | *.rsp 88 | *.sbr 89 | *.tlb 90 | *.tli 91 | *.tlh 92 | *.tmp 93 | *.tmp_proj 94 | *_wpftmp.csproj 95 | *.log 96 | *.vspscc 97 | *.vssscc 98 | .builds 99 | *.pidb 100 | *.svclog 101 | *.scc 102 | 103 | # Chutzpah Test files 104 | _Chutzpah* 105 | 106 | # Visual C++ cache files 107 | ipch/ 108 | *.aps 109 | *.ncb 110 | *.opendb 111 | *.opensdf 112 | *.sdf 113 | *.cachefile 114 | *.VC.db 115 | *.VC.VC.opendb 116 | 117 | # Visual Studio profiler 118 | *.psess 119 | *.vsp 120 | *.vspx 121 | *.sap 122 | 123 | # Visual Studio Trace Files 124 | *.e2e 125 | 126 | # TFS 2012 Local Workspace 127 | $tf/ 128 | 129 | # Guidance Automation Toolkit 130 | *.gpState 131 | 132 | # ReSharper is a .NET coding add-in 133 | _ReSharper*/ 134 | *.[Rr]e[Ss]harper 135 | *.DotSettings.user 136 | 137 | # TeamCity is a build add-in 138 | _TeamCity* 139 | 140 | # DotCover is a Code Coverage Tool 141 | *.dotCover 142 | 143 | # AxoCover is a Code Coverage Tool 144 | .axoCover/* 145 | !.axoCover/settings.json 146 | 147 | # Coverlet is a free, cross platform Code Coverage Tool 148 | coverage*.json 149 | coverage*.xml 150 | coverage*.info 151 | 152 | # Visual Studio code coverage results 153 | *.coverage 154 | *.coveragexml 155 | 156 | # NCrunch 157 | _NCrunch_* 158 | .*crunch*.local.xml 159 | nCrunchTemp_* 160 | 161 | # MightyMoose 162 | *.mm.* 163 | AutoTest.Net/ 164 | 165 | # Web workbench (sass) 166 | .sass-cache/ 167 | 168 | # Installshield output folder 169 | [Ee]xpress/ 170 | 171 | # DocProject is a documentation generator add-in 172 | DocProject/buildhelp/ 173 | DocProject/Help/*.HxT 174 | DocProject/Help/*.HxC 175 | DocProject/Help/*.hhc 176 | DocProject/Help/*.hhk 177 | DocProject/Help/*.hhp 178 | DocProject/Help/Html2 179 | DocProject/Help/html 180 | 181 | # Click-Once directory 182 | publish/ 183 | 184 | # Publish Web Output 185 | *.[Pp]ublish.xml 186 | *.azurePubxml 187 | # Note: Comment the next line if you want to checkin your web deploy settings, 188 | # but database connection strings (with potential passwords) will be unencrypted 189 | *.pubxml 190 | *.publishproj 191 | 192 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 193 | # checkin your Azure Web App publish settings, but sensitive information contained 194 | # in these scripts will be unencrypted 195 | PublishScripts/ 196 | 197 | # NuGet Packages 198 | *.nupkg 199 | # NuGet Symbol Packages 200 | *.snupkg 201 | # The packages folder can be ignored because of Package Restore 202 | **/[Pp]ackages/* 203 | # except build/, which is used as an MSBuild target. 204 | !**/[Pp]ackages/build/ 205 | # Uncomment if necessary however generally it will be regenerated when needed 206 | #!**/[Pp]ackages/repositories.config 207 | # NuGet v3's project.json files produces more ignorable files 208 | *.nuget.props 209 | *.nuget.targets 210 | 211 | # Microsoft Azure Build Output 212 | csx/ 213 | *.build.csdef 214 | 215 | # Microsoft Azure Emulator 216 | ecf/ 217 | rcf/ 218 | 219 | # Windows Store app package directories and files 220 | AppPackages/ 221 | BundleArtifacts/ 222 | Package.StoreAssociation.xml 223 | _pkginfo.txt 224 | *.appx 225 | *.appxbundle 226 | *.appxupload 227 | 228 | # Visual Studio cache files 229 | # files ending in .cache can be ignored 230 | *.[Cc]ache 231 | # but keep track of directories ending in .cache 232 | !?*.[Cc]ache/ 233 | 234 | # Others 235 | ClientBin/ 236 | ~$* 237 | *~ 238 | *.dbmdl 239 | *.dbproj.schemaview 240 | *.jfm 241 | *.pfx 242 | *.publishsettings 243 | orleans.codegen.cs 244 | 245 | # Including strong name files can present a security risk 246 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 247 | #*.snk 248 | 249 | # Since there are multiple workflows, uncomment next line to ignore bower_components 250 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 251 | #bower_components/ 252 | 253 | # RIA/Silverlight projects 254 | Generated_Code/ 255 | 256 | # Backup & report files from converting an old project file 257 | # to a newer Visual Studio version. Backup files are not needed, 258 | # because we have git ;-) 259 | _UpgradeReport_Files/ 260 | Backup*/ 261 | UpgradeLog*.XML 262 | UpgradeLog*.htm 263 | ServiceFabricBackup/ 264 | *.rptproj.bak 265 | 266 | # SQL Server files 267 | *.mdf 268 | *.ldf 269 | *.ndf 270 | 271 | # Business Intelligence projects 272 | *.rdl.data 273 | *.bim.layout 274 | *.bim_*.settings 275 | *.rptproj.rsuser 276 | *- [Bb]ackup.rdl 277 | *- [Bb]ackup ([0-9]).rdl 278 | *- [Bb]ackup ([0-9][0-9]).rdl 279 | 280 | # Microsoft Fakes 281 | FakesAssemblies/ 282 | 283 | # GhostDoc plugin setting file 284 | *.GhostDoc.xml 285 | 286 | # Node.js Tools for Visual Studio 287 | .ntvs_analysis.dat 288 | node_modules/ 289 | 290 | # Visual Studio 6 build log 291 | *.plg 292 | 293 | # Visual Studio 6 workspace options file 294 | *.opt 295 | 296 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 297 | *.vbw 298 | 299 | # Visual Studio LightSwitch build output 300 | **/*.HTMLClient/GeneratedArtifacts 301 | **/*.DesktopClient/GeneratedArtifacts 302 | **/*.DesktopClient/ModelManifest.xml 303 | **/*.Server/GeneratedArtifacts 304 | **/*.Server/ModelManifest.xml 305 | _Pvt_Extensions 306 | 307 | # Paket dependency manager 308 | .paket/paket.exe 309 | paket-files/ 310 | 311 | # FAKE - F# Make 312 | .fake/ 313 | 314 | # CodeRush personal settings 315 | .cr/personal 316 | 317 | # Python Tools for Visual Studio (PTVS) 318 | __pycache__/ 319 | *.pyc 320 | 321 | # Cake - Uncomment if you are using it 322 | # tools/** 323 | # !tools/packages.config 324 | 325 | # Tabs Studio 326 | *.tss 327 | 328 | # Telerik's JustMock configuration file 329 | *.jmconfig 330 | 331 | # BizTalk build output 332 | *.btp.cs 333 | *.btm.cs 334 | *.odx.cs 335 | *.xsd.cs 336 | 337 | # OpenCover UI analysis results 338 | OpenCover/ 339 | 340 | # Azure Stream Analytics local run output 341 | ASALocalRun/ 342 | 343 | # MSBuild Binary and Structured Log 344 | *.binlog 345 | 346 | # NVidia Nsight GPU debugger configuration file 347 | *.nvuser 348 | 349 | # MFractors (Xamarin productivity tool) working folder 350 | .mfractor/ 351 | 352 | # Local History for Visual Studio 353 | .localhistory/ 354 | 355 | # BeatPulse healthcheck temp database 356 | healthchecksdb 357 | 358 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 359 | MigrationBackup/ 360 | 361 | # Ionide (cross platform F# VS Code tools) working folder 362 | .ionide/ 363 | 364 | # Fody - auto-generated XML schema 365 | FodyWeavers.xsd 366 | 367 | ## 368 | ## Visual studio for Mac 369 | ## 370 | 371 | 372 | # globs 373 | Makefile.in 374 | *.userprefs 375 | *.usertasks 376 | config.make 377 | config.status 378 | aclocal.m4 379 | install-sh 380 | autom4te.cache/ 381 | *.tar.gz 382 | tarballs/ 383 | test-results/ 384 | 385 | # Mac bundle stuff 386 | *.dmg 387 | *.app 388 | 389 | # content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore 390 | # General 391 | .DS_Store 392 | .AppleDouble 393 | .LSOverride 394 | 395 | # Icon must end with two \r 396 | Icon 397 | 398 | 399 | # Thumbnails 400 | ._* 401 | 402 | # Files that might appear in the root of a volume 403 | .DocumentRevisions-V100 404 | .fseventsd 405 | .Spotlight-V100 406 | .TemporaryItems 407 | .Trashes 408 | .VolumeIcon.icns 409 | .com.apple.timemachine.donotpresent 410 | 411 | # Directories potentially created on remote AFP share 412 | .AppleDB 413 | .AppleDesktop 414 | Network Trash Folder 415 | Temporary Items 416 | .apdisk 417 | 418 | # content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore 419 | # Windows thumbnail cache files 420 | Thumbs.db 421 | ehthumbs.db 422 | ehthumbs_vista.db 423 | 424 | # Dump file 425 | *.stackdump 426 | 427 | # Folder config file 428 | [Dd]esktop.ini 429 | 430 | # Recycle Bin used on file shares 431 | $RECYCLE.BIN/ 432 | 433 | # Windows Installer files 434 | *.cab 435 | *.msi 436 | *.msix 437 | *.msm 438 | *.msp 439 | 440 | # Windows shortcuts 441 | *.lnk 442 | 443 | # JetBrains Rider 444 | .idea/ 445 | *.sln.iml 446 | 447 | ## 448 | ## Visual Studio Code 449 | ## 450 | .vscode/* 451 | !.vscode/settings.json 452 | !.vscode/tasks.json 453 | !.vscode/launch.json 454 | !.vscode/extensions.json 455 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | // Use IntelliSense to find out which attributes exist for C# debugging 6 | // Use hover for the description of the existing attributes 7 | // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md 8 | "name": ".NET Core Launch (web)", 9 | "type": "coreclr", 10 | "request": "launch", 11 | "preLaunchTask": "build", 12 | // If you have changed target frameworks, make sure to update the program path. 13 | "program": "${workspaceFolder}/bin/Debug/net6.0/dotnet-rpg.dll", 14 | "args": [], 15 | "cwd": "${workspaceFolder}", 16 | "stopAtEntry": false, 17 | // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser 18 | "serverReadyAction": { 19 | "action": "openExternally", 20 | "pattern": "\\bNow listening on:\\s+(https?://\\S+)" 21 | }, 22 | "env": { 23 | "ASPNETCORE_ENVIRONMENT": "Development" 24 | }, 25 | "sourceFileMap": { 26 | "/Views": "${workspaceFolder}/Views" 27 | } 28 | }, 29 | { 30 | "name": ".NET Core Attach", 31 | "type": "coreclr", 32 | "request": "attach" 33 | } 34 | ] 35 | } -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "label": "build", 6 | "command": "dotnet", 7 | "type": "process", 8 | "args": [ 9 | "build", 10 | "${workspaceFolder}/dotnet-rpg.csproj", 11 | "/property:GenerateFullPaths=true", 12 | "/consoleloggerparameters:NoSummary" 13 | ], 14 | "problemMatcher": "$msCompile" 15 | }, 16 | { 17 | "label": "publish", 18 | "command": "dotnet", 19 | "type": "process", 20 | "args": [ 21 | "publish", 22 | "${workspaceFolder}/dotnet-rpg.csproj", 23 | "/property:GenerateFullPaths=true", 24 | "/consoleloggerparameters:NoSummary" 25 | ], 26 | "problemMatcher": "$msCompile" 27 | }, 28 | { 29 | "label": "watch", 30 | "command": "dotnet", 31 | "type": "process", 32 | "args": [ 33 | "watch", 34 | "run", 35 | "--project", 36 | "${workspaceFolder}/dotnet-rpg.csproj" 37 | ], 38 | "problemMatcher": "$msCompile" 39 | } 40 | ] 41 | } -------------------------------------------------------------------------------- /AutoMapperProfile.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using AutoMapper; 6 | using dotnet_rpg.Dtos.Character; 7 | using dotnet_rpg.Dtos.Fight; 8 | using dotnet_rpg.Dtos.Skill; 9 | using dotnet_rpg.Dtos.Weapon; 10 | 11 | namespace dotnet_rpg 12 | { 13 | public class AutoMapperProfile : Profile 14 | { 15 | public AutoMapperProfile() 16 | { 17 | CreateMap(); 18 | CreateMap(); 19 | CreateMap(); 20 | CreateMap(); 21 | CreateMap(); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /Controllers/AuthController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using dotnet_rpg.Data; 6 | using dotnet_rpg.Dtos.User; 7 | using Microsoft.AspNetCore.Mvc; 8 | 9 | namespace dotnet_rpg.Controllers 10 | { 11 | [ApiController] 12 | [Route("[controller]")] 13 | public class AuthController : ControllerBase 14 | { 15 | private readonly IAuthRepository _authRepo; 16 | 17 | public AuthController(IAuthRepository authRepo) 18 | { 19 | _authRepo = authRepo; 20 | } 21 | 22 | [HttpPost("register")] 23 | public async Task>> Register(UserRegisterDto request) 24 | { 25 | var response = await _authRepo.Register( 26 | new User { Username = request.Username }, request.Password 27 | ); 28 | if (!response.Success) 29 | { 30 | return BadRequest(response); 31 | } 32 | return Ok(response); 33 | } 34 | 35 | [HttpPost("login")] 36 | public async Task>> Login(UserLoginDto request) 37 | { 38 | var response = await _authRepo.Login(request.Username, request.Password); 39 | if (!response.Success) 40 | { 41 | return BadRequest(response); 42 | } 43 | return Ok(response); 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /Controllers/CharacterController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Security.Claims; 5 | using System.Threading.Tasks; 6 | using dotnet_rpg.Dtos.Character; 7 | using dotnet_rpg.Services.CharacterService; 8 | using Microsoft.AspNetCore.Authorization; 9 | using Microsoft.AspNetCore.Mvc; 10 | 11 | namespace dotnet_rpg.Controllers 12 | { 13 | [Authorize] 14 | [ApiController] 15 | [Route("api/[controller]")] 16 | public class CharacterController : ControllerBase 17 | { 18 | private readonly ICharacterService _characterService; 19 | public CharacterController(ICharacterService characterService) 20 | { 21 | _characterService = characterService; 22 | } 23 | 24 | [HttpGet("GetAll")] 25 | public async Task>>> Get() 26 | { 27 | return Ok(await _characterService.GetAllCharacters()); 28 | } 29 | 30 | [HttpDelete("{id}")] 31 | public async Task>>> Delete(int id) 32 | { 33 | var response = await _characterService.DeleteCharacter(id); 34 | if (response.Data == null) 35 | { 36 | return NotFound(response); 37 | } 38 | return Ok(response); 39 | } 40 | 41 | [HttpGet("{id}")] 42 | public async Task>> GetSingle(int id) 43 | { 44 | return Ok(await _characterService.GetCharacterById(id)); 45 | } 46 | 47 | [HttpPost] 48 | public async Task>>> AddCharacter(AddCharacterDto newCharacter) 49 | { 50 | return Ok(await _characterService.AddCharacter(newCharacter)); 51 | } 52 | 53 | [HttpPut] 54 | public async Task>> UpdateCharacter(UpdateCharacterDto updatedCharacter) 55 | { 56 | var response = await _characterService.UpdateCharacter(updatedCharacter); 57 | if (response.Data == null) 58 | { 59 | return NotFound(response); 60 | } 61 | return Ok(response); 62 | } 63 | 64 | 65 | [HttpPost("Skill")] 66 | public async Task>> AddCharacterSkill(AddCharacterSkillDto newCharacterSkill) 67 | { 68 | return Ok(await _characterService.AddCharacterSkill(newCharacterSkill)); 69 | } 70 | } 71 | } -------------------------------------------------------------------------------- /Controllers/FightController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using dotnet_rpg.Dtos.Fight; 6 | using dotnet_rpg.Services.FightService; 7 | using Microsoft.AspNetCore.Mvc; 8 | 9 | namespace dotnet_rpg.Controllers 10 | { 11 | [ApiController] 12 | [Route("[controller]")] 13 | public class FightController : ControllerBase 14 | { 15 | private readonly IFightService _fightService; 16 | 17 | public FightController(IFightService fightService) 18 | { 19 | _fightService = fightService; 20 | } 21 | 22 | [HttpPost("Weapon")] 23 | public async Task>> WeaponAttack(WeaponAttackDto request) 24 | { 25 | return Ok(await _fightService.WeaponAttack(request)); 26 | } 27 | 28 | [HttpPost("Skill")] 29 | public async Task>> SkillAttack(SkillAttackDto request) 30 | { 31 | return Ok(await _fightService.SkillAttack(request)); 32 | } 33 | 34 | [HttpPost] 35 | public async Task>> Fight(FightRequestDto request) 36 | { 37 | return Ok(await _fightService.Fight(request)); 38 | } 39 | 40 | [HttpGet] 41 | public async Task>>> GetHighscore() 42 | { 43 | return Ok(await _fightService.GetHighscore()); 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /Controllers/WeaponController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using dotnet_rpg.Dtos.Character; 6 | using dotnet_rpg.Dtos.Weapon; 7 | using dotnet_rpg.Services.WeaponService; 8 | using Microsoft.AspNetCore.Authorization; 9 | using Microsoft.AspNetCore.Mvc; 10 | 11 | namespace dotnet_rpg.Controllers 12 | { 13 | [Authorize] 14 | [ApiController] 15 | [Route("[controller]")] 16 | public class WeaponController : ControllerBase 17 | { 18 | private readonly IWeaponService _weaponService; 19 | 20 | public WeaponController(IWeaponService weaponService) 21 | { 22 | _weaponService = weaponService; 23 | } 24 | 25 | [HttpPost] 26 | public async Task>> AddWeapon(AddWeaponDto newWeapon) 27 | { 28 | return Ok(await _weaponService.AddWeapon(newWeapon)); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /Controllers/WeatherForecastController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc; 2 | 3 | namespace dotnet_rpg.Controllers; 4 | 5 | [ApiController] 6 | [Route("[controller]")] 7 | public class WeatherForecastController : ControllerBase 8 | { 9 | private static readonly string[] Summaries = new[] 10 | { 11 | "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" 12 | }; 13 | 14 | private readonly ILogger _logger; 15 | 16 | public WeatherForecastController(ILogger logger) 17 | { 18 | _logger = logger; 19 | } 20 | 21 | [HttpGet(Name = "GetWeatherForecast")] 22 | public IEnumerable Get() 23 | { 24 | return Enumerable.Range(1, 5).Select(index => new WeatherForecast 25 | { 26 | Date = DateTime.Now.AddDays(index), 27 | TemperatureC = Random.Shared.Next(-20, 55), 28 | Summary = Summaries[Random.Shared.Next(Summaries.Length)] 29 | }) 30 | .ToArray(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Data/AuthRepository.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IdentityModel.Tokens.Jwt; 4 | using System.Linq; 5 | using System.Security.Claims; 6 | using System.Threading.Tasks; 7 | using Microsoft.EntityFrameworkCore; 8 | using Microsoft.IdentityModel.Tokens; 9 | 10 | namespace dotnet_rpg.Data 11 | { 12 | public class AuthRepository : IAuthRepository 13 | { 14 | private readonly DataContext _context; 15 | private readonly IConfiguration _configuration; 16 | 17 | public AuthRepository(DataContext context, IConfiguration configuration) 18 | { 19 | _context = context; 20 | _configuration = configuration; 21 | } 22 | 23 | public async Task> Login(string username, string password) 24 | { 25 | var response = new ServiceResponse(); 26 | var user = await _context.Users 27 | .FirstOrDefaultAsync(u => u.Username.ToLower().Equals(username.ToLower())); 28 | 29 | if (user == null) 30 | { 31 | response.Success = false; 32 | response.Message = "User not found."; 33 | } 34 | else if (!VerifyPasswordHash(password, user.PasswordHash, user.PasswordSalt)) 35 | { 36 | response.Success = false; 37 | response.Message = "Wrong password."; 38 | } 39 | else 40 | { 41 | response.Data = CreateToken(user); 42 | } 43 | 44 | return response; 45 | } 46 | 47 | public async Task> Register(User user, string password) 48 | { 49 | ServiceResponse response = new ServiceResponse(); 50 | if (await UserExists(user.Username)) 51 | { 52 | response.Success = false; 53 | response.Message = "User already exists."; 54 | return response; 55 | } 56 | 57 | CreatePasswordHash(password, out byte[] passwordHash, out byte[] passwordSalt); 58 | 59 | user.PasswordHash = passwordHash; 60 | user.PasswordSalt = passwordSalt; 61 | 62 | _context.Users.Add(user); 63 | await _context.SaveChangesAsync(); 64 | response.Data = user.Id; 65 | return response; 66 | } 67 | 68 | public async Task UserExists(string username) 69 | { 70 | if (await _context.Users.AnyAsync(u => u.Username.ToLower() == username.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 System.Security.Cryptography.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 System.Security.Cryptography.HMACSHA512(passwordSalt)) 89 | { 90 | var computeHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password)); 91 | return computeHash.SequenceEqual(passwordHash); 92 | } 93 | } 94 | 95 | private string CreateToken(User user) 96 | { 97 | List claims = new List { 98 | new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()), 99 | new Claim(ClaimTypes.Name, user.Username) 100 | }; 101 | 102 | SymmetricSecurityKey key = new SymmetricSecurityKey(System.Text.Encoding.UTF8 103 | .GetBytes(_configuration.GetSection("AppSettings:Token").Value)); 104 | 105 | SigningCredentials creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature); 106 | 107 | SecurityTokenDescriptor tokenDescriptor = new SecurityTokenDescriptor 108 | { 109 | Subject = new ClaimsIdentity(claims), 110 | Expires = DateTime.Now.AddDays(1), 111 | SigningCredentials = creds 112 | }; 113 | 114 | JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler(); 115 | SecurityToken token = tokenHandler.CreateToken(tokenDescriptor); 116 | 117 | return tokenHandler.WriteToken(token); 118 | } 119 | } 120 | } -------------------------------------------------------------------------------- /Data/DataContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.EntityFrameworkCore; 6 | 7 | namespace dotnet_rpg.Data 8 | { 9 | public class DataContext : DbContext 10 | { 11 | public DataContext(DbContextOptions options) : base(options) 12 | { 13 | 14 | } 15 | 16 | protected override void OnModelCreating(ModelBuilder modelBuilder) 17 | { 18 | modelBuilder.Entity().HasData( 19 | new Skill { Id = 1, Name = "Fireball", Damage = 30 }, 20 | new Skill { Id = 2, Name = "Frenzy", Damage = 20 }, 21 | new Skill { Id = 3, Name = "Blizzard", Damage = 50 } 22 | ); 23 | } 24 | 25 | public DbSet Characters { get; set; } 26 | public DbSet Users { get; set; } 27 | public DbSet Weapons { get; set; } 28 | public DbSet Skills { get; set; } 29 | } 30 | } -------------------------------------------------------------------------------- /Data/IAuthRepository.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace dotnet_rpg.Data 7 | { 8 | public interface IAuthRepository 9 | { 10 | Task> Register(User user, string password); 11 | Task> Login(string username, string password); 12 | Task UserExists(string username); 13 | } 14 | } -------------------------------------------------------------------------------- /Dtos/Character/AddCharacterDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace dotnet_rpg.Dtos.Character 7 | { 8 | public class AddCharacterDto 9 | { 10 | public string Name { get; set; } = "Frodo"; 11 | public int HitPoints { get; set; } = 100; 12 | public int Strength { get; set; } = 10; 13 | public int Defense { get; set; } = 10; 14 | public int Intelligence { get; set; } = 10; 15 | public RpgClass Class { get; set; } = RpgClass.Knight; 16 | } 17 | } -------------------------------------------------------------------------------- /Dtos/Character/AddCharacterSkillDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace dotnet_rpg.Dtos.Character 7 | { 8 | public class AddCharacterSkillDto 9 | { 10 | public int CharacterId { get; set; } 11 | public int SkillId { get; set; } 12 | } 13 | } -------------------------------------------------------------------------------- /Dtos/Character/GetCharacterDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using dotnet_rpg.Dtos.Skill; 6 | using dotnet_rpg.Dtos.Weapon; 7 | 8 | namespace dotnet_rpg.Dtos.Character 9 | { 10 | public class GetCharacterDto 11 | { 12 | public int Id { get; set; } 13 | public string Name { get; set; } = "Frodo"; 14 | public int HitPoints { get; set; } = 100; 15 | public int Strength { get; set; } = 10; 16 | public int Defense { get; set; } = 10; 17 | public int Intelligence { get; set; } = 10; 18 | public RpgClass Class { get; set; } = RpgClass.Knight; 19 | public GetWeaponDto Weapon { get; set; } 20 | public List Skills { get; set; } 21 | public int Fights { get; set; } 22 | public int Victories { get; set; } 23 | public int Defeats { get; set; } 24 | } 25 | } -------------------------------------------------------------------------------- /Dtos/Character/UpdateCharacterDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace dotnet_rpg.Dtos.Character 7 | { 8 | public class UpdateCharacterDto 9 | { 10 | public int Id { get; set; } 11 | public string Name { get; set; } = "Frodo"; 12 | public int HitPoints { get; set; } = 100; 13 | public int Strength { get; set; } = 10; 14 | public int Defense { get; set; } = 10; 15 | public int Intelligence { get; set; } = 10; 16 | public RpgClass Class { get; set; } = RpgClass.Knight; 17 | } 18 | } -------------------------------------------------------------------------------- /Dtos/Fight/AttackResultDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace dotnet_rpg.Dtos.Fight 7 | { 8 | public class AttackResultDto 9 | { 10 | public string Attacker { get; set; } = string.Empty; 11 | public string Opponent { get; set; } = string.Empty; 12 | public int AttackerHP { get; set; } 13 | public int OpponentHP { get; set; } 14 | public int Damage { get; set; } 15 | } 16 | } -------------------------------------------------------------------------------- /Dtos/Fight/FightRequestDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace dotnet_rpg.Dtos.Fight 7 | { 8 | public class FightRequestDto 9 | { 10 | public List CharacterIds { get; set; } 11 | } 12 | } -------------------------------------------------------------------------------- /Dtos/Fight/FightResultDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace dotnet_rpg.Dtos.Fight 7 | { 8 | public class FightResultDto 9 | { 10 | public List Log { get; set; } = new List(); 11 | } 12 | } -------------------------------------------------------------------------------- /Dtos/Fight/HighscoreDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace dotnet_rpg.Dtos.Fight 7 | { 8 | public class HighscoreDto 9 | { 10 | public int Id { get; set; } 11 | public string Name { get; set; } = string.Empty; 12 | public int Fights { get; set; } 13 | public int Victories { get; set; } 14 | public int Defeats { get; set; } 15 | } 16 | } -------------------------------------------------------------------------------- /Dtos/Fight/SkillAttackDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace dotnet_rpg.Dtos.Fight 7 | { 8 | public class SkillAttackDto 9 | { 10 | public int AttackerId { get; set; } 11 | public int OpponentId { get; set; } 12 | public int SkillId { get; set; } 13 | } 14 | } -------------------------------------------------------------------------------- /Dtos/Fight/WeaponAttackDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace dotnet_rpg.Dtos.Fight 7 | { 8 | public class WeaponAttackDto 9 | { 10 | public int AttackerId { get; set; } 11 | public int OpponentId { get; set; } 12 | } 13 | } -------------------------------------------------------------------------------- /Dtos/Skill/GetSkillDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace dotnet_rpg.Dtos.Skill 7 | { 8 | public class GetSkillDto 9 | { 10 | public string Name { get; set; } = string.Empty; 11 | public int Damage { get; set; } 12 | } 13 | } -------------------------------------------------------------------------------- /Dtos/User/UserLoginDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace dotnet_rpg.Dtos.User 7 | { 8 | public class UserLoginDto 9 | { 10 | public string Username { get; set; } = string.Empty; 11 | public string Password { get; set; } = string.Empty; 12 | } 13 | } -------------------------------------------------------------------------------- /Dtos/User/UserRegisterDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace dotnet_rpg.Dtos.User 7 | { 8 | public class UserRegisterDto 9 | { 10 | public string Username { get; set; } = string.Empty; 11 | public string Password { get; set; } = string.Empty; 12 | } 13 | } -------------------------------------------------------------------------------- /Dtos/Weapon/AddWeaponDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace dotnet_rpg.Dtos.Weapon 7 | { 8 | public class AddWeaponDto 9 | { 10 | public string Name { get; set; } = string.Empty; 11 | public int Damage { get; set; } 12 | public int CharacterId { get; set; } 13 | } 14 | } -------------------------------------------------------------------------------- /Dtos/Weapon/GetWeaponDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace dotnet_rpg.Dtos.Weapon 7 | { 8 | public class GetWeaponDto 9 | { 10 | public string Name { get; set; } = string.Empty; 11 | public int Damage { get; set; } 12 | } 13 | } -------------------------------------------------------------------------------- /Migrations/20220315145521_InitialCreate.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using Microsoft.EntityFrameworkCore; 3 | using Microsoft.EntityFrameworkCore.Infrastructure; 4 | using Microsoft.EntityFrameworkCore.Metadata; 5 | using Microsoft.EntityFrameworkCore.Migrations; 6 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 7 | using dotnet_rpg.Data; 8 | 9 | #nullable disable 10 | 11 | namespace dotnet_rpg.Migrations 12 | { 13 | [DbContext(typeof(DataContext))] 14 | [Migration("20220315145521_InitialCreate")] 15 | partial class InitialCreate 16 | { 17 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 18 | { 19 | #pragma warning disable 612, 618 20 | modelBuilder 21 | .HasAnnotation("ProductVersion", "6.0.3") 22 | .HasAnnotation("Relational:MaxIdentifierLength", 128); 23 | 24 | SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); 25 | 26 | modelBuilder.Entity("dotnet_rpg.Models.Character", b => 27 | { 28 | b.Property("Id") 29 | .ValueGeneratedOnAdd() 30 | .HasColumnType("int"); 31 | 32 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 33 | 34 | b.Property("Class") 35 | .HasColumnType("int"); 36 | 37 | b.Property("Defense") 38 | .HasColumnType("int"); 39 | 40 | b.Property("HitPoints") 41 | .HasColumnType("int"); 42 | 43 | b.Property("Intelligence") 44 | .HasColumnType("int"); 45 | 46 | b.Property("Name") 47 | .IsRequired() 48 | .HasColumnType("nvarchar(max)"); 49 | 50 | b.Property("Strength") 51 | .HasColumnType("int"); 52 | 53 | b.HasKey("Id"); 54 | 55 | b.ToTable("Characters"); 56 | }); 57 | #pragma warning restore 612, 618 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Migrations/20220315145521_InitialCreate.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore.Migrations; 2 | 3 | #nullable disable 4 | 5 | namespace dotnet_rpg.Migrations 6 | { 7 | public partial class InitialCreate : Migration 8 | { 9 | protected override void Up(MigrationBuilder migrationBuilder) 10 | { 11 | migrationBuilder.CreateTable( 12 | name: "Characters", 13 | columns: table => new 14 | { 15 | Id = table.Column(type: "int", nullable: false) 16 | .Annotation("SqlServer:Identity", "1, 1"), 17 | Name = table.Column(type: "nvarchar(max)", nullable: false), 18 | HitPoints = table.Column(type: "int", nullable: false), 19 | Strength = table.Column(type: "int", nullable: false), 20 | Defense = table.Column(type: "int", nullable: false), 21 | Intelligence = table.Column(type: "int", nullable: false), 22 | Class = table.Column(type: "int", nullable: false) 23 | }, 24 | constraints: table => 25 | { 26 | table.PrimaryKey("PK_Characters", x => x.Id); 27 | }); 28 | } 29 | 30 | protected override void Down(MigrationBuilder migrationBuilder) 31 | { 32 | migrationBuilder.DropTable( 33 | name: "Characters"); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Migrations/20220316151920_UserModel.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 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 | using dotnet_rpg.Data; 9 | 10 | #nullable disable 11 | 12 | namespace dotnet_rpg.Migrations 13 | { 14 | [DbContext(typeof(DataContext))] 15 | [Migration("20220316151920_UserModel")] 16 | partial class UserModel 17 | { 18 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 19 | { 20 | #pragma warning disable 612, 618 21 | modelBuilder 22 | .HasAnnotation("ProductVersion", "6.0.3") 23 | .HasAnnotation("Relational:MaxIdentifierLength", 128); 24 | 25 | SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); 26 | 27 | modelBuilder.Entity("dotnet_rpg.Models.Character", b => 28 | { 29 | b.Property("Id") 30 | .ValueGeneratedOnAdd() 31 | .HasColumnType("int"); 32 | 33 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 34 | 35 | b.Property("Class") 36 | .HasColumnType("int"); 37 | 38 | b.Property("Defense") 39 | .HasColumnType("int"); 40 | 41 | b.Property("HitPoints") 42 | .HasColumnType("int"); 43 | 44 | b.Property("Intelligence") 45 | .HasColumnType("int"); 46 | 47 | b.Property("Name") 48 | .IsRequired() 49 | .HasColumnType("nvarchar(max)"); 50 | 51 | b.Property("Strength") 52 | .HasColumnType("int"); 53 | 54 | b.HasKey("Id"); 55 | 56 | b.ToTable("Characters"); 57 | }); 58 | 59 | modelBuilder.Entity("dotnet_rpg.Models.User", b => 60 | { 61 | b.Property("Id") 62 | .ValueGeneratedOnAdd() 63 | .HasColumnType("int"); 64 | 65 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 66 | 67 | b.Property("PasswordHash") 68 | .IsRequired() 69 | .HasColumnType("varbinary(max)"); 70 | 71 | b.Property("PasswordSalt") 72 | .IsRequired() 73 | .HasColumnType("varbinary(max)"); 74 | 75 | b.Property("Username") 76 | .IsRequired() 77 | .HasColumnType("nvarchar(max)"); 78 | 79 | b.HasKey("Id"); 80 | 81 | b.ToTable("Users"); 82 | }); 83 | #pragma warning restore 612, 618 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /Migrations/20220316151920_UserModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.EntityFrameworkCore.Migrations; 3 | 4 | #nullable disable 5 | 6 | namespace dotnet_rpg.Migrations 7 | { 8 | public partial class UserModel : Migration 9 | { 10 | protected override void Up(MigrationBuilder migrationBuilder) 11 | { 12 | migrationBuilder.CreateTable( 13 | name: "Users", 14 | columns: table => new 15 | { 16 | Id = table.Column(type: "int", nullable: false) 17 | .Annotation("SqlServer:Identity", "1, 1"), 18 | Username = table.Column(type: "nvarchar(max)", nullable: false), 19 | PasswordHash = table.Column(type: "varbinary(max)", nullable: false), 20 | PasswordSalt = table.Column(type: "varbinary(max)", nullable: false) 21 | }, 22 | constraints: table => 23 | { 24 | table.PrimaryKey("PK_Users", x => x.Id); 25 | }); 26 | } 27 | 28 | protected override void Down(MigrationBuilder migrationBuilder) 29 | { 30 | migrationBuilder.DropTable( 31 | name: "Users"); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Migrations/20220317173055_UserCharacterRelationship.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 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 | using dotnet_rpg.Data; 9 | 10 | #nullable disable 11 | 12 | namespace dotnet_rpg.Migrations 13 | { 14 | [DbContext(typeof(DataContext))] 15 | [Migration("20220317173055_UserCharacterRelationship")] 16 | partial class UserCharacterRelationship 17 | { 18 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 19 | { 20 | #pragma warning disable 612, 618 21 | modelBuilder 22 | .HasAnnotation("ProductVersion", "6.0.3") 23 | .HasAnnotation("Relational:MaxIdentifierLength", 128); 24 | 25 | SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); 26 | 27 | modelBuilder.Entity("dotnet_rpg.Models.Character", b => 28 | { 29 | b.Property("Id") 30 | .ValueGeneratedOnAdd() 31 | .HasColumnType("int"); 32 | 33 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 34 | 35 | b.Property("Class") 36 | .HasColumnType("int"); 37 | 38 | b.Property("Defense") 39 | .HasColumnType("int"); 40 | 41 | b.Property("HitPoints") 42 | .HasColumnType("int"); 43 | 44 | b.Property("Intelligence") 45 | .HasColumnType("int"); 46 | 47 | b.Property("Name") 48 | .IsRequired() 49 | .HasColumnType("nvarchar(max)"); 50 | 51 | b.Property("Strength") 52 | .HasColumnType("int"); 53 | 54 | b.Property("UserId") 55 | .HasColumnType("int"); 56 | 57 | b.HasKey("Id"); 58 | 59 | b.HasIndex("UserId"); 60 | 61 | b.ToTable("Characters"); 62 | }); 63 | 64 | modelBuilder.Entity("dotnet_rpg.Models.User", b => 65 | { 66 | b.Property("Id") 67 | .ValueGeneratedOnAdd() 68 | .HasColumnType("int"); 69 | 70 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 71 | 72 | b.Property("PasswordHash") 73 | .IsRequired() 74 | .HasColumnType("varbinary(max)"); 75 | 76 | b.Property("PasswordSalt") 77 | .IsRequired() 78 | .HasColumnType("varbinary(max)"); 79 | 80 | b.Property("Username") 81 | .IsRequired() 82 | .HasColumnType("nvarchar(max)"); 83 | 84 | b.HasKey("Id"); 85 | 86 | b.ToTable("Users"); 87 | }); 88 | 89 | modelBuilder.Entity("dotnet_rpg.Models.Character", b => 90 | { 91 | b.HasOne("dotnet_rpg.Models.User", "User") 92 | .WithMany("Characters") 93 | .HasForeignKey("UserId"); 94 | 95 | b.Navigation("User"); 96 | }); 97 | 98 | modelBuilder.Entity("dotnet_rpg.Models.User", b => 99 | { 100 | b.Navigation("Characters"); 101 | }); 102 | #pragma warning restore 612, 618 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /Migrations/20220317173055_UserCharacterRelationship.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore.Migrations; 2 | 3 | #nullable disable 4 | 5 | namespace dotnet_rpg.Migrations 6 | { 7 | public partial class UserCharacterRelationship : Migration 8 | { 9 | protected override void Up(MigrationBuilder migrationBuilder) 10 | { 11 | migrationBuilder.AddColumn( 12 | name: "UserId", 13 | table: "Characters", 14 | type: "int", 15 | nullable: true); 16 | 17 | migrationBuilder.CreateIndex( 18 | name: "IX_Characters_UserId", 19 | table: "Characters", 20 | column: "UserId"); 21 | 22 | migrationBuilder.AddForeignKey( 23 | name: "FK_Characters_Users_UserId", 24 | table: "Characters", 25 | column: "UserId", 26 | principalTable: "Users", 27 | principalColumn: "Id"); 28 | } 29 | 30 | protected override void Down(MigrationBuilder migrationBuilder) 31 | { 32 | migrationBuilder.DropForeignKey( 33 | name: "FK_Characters_Users_UserId", 34 | table: "Characters"); 35 | 36 | migrationBuilder.DropIndex( 37 | name: "IX_Characters_UserId", 38 | table: "Characters"); 39 | 40 | migrationBuilder.DropColumn( 41 | name: "UserId", 42 | table: "Characters"); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Migrations/20220624203034_Weapon.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 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 | using dotnet_rpg.Data; 9 | 10 | #nullable disable 11 | 12 | namespace dotnet_rpg.Migrations 13 | { 14 | [DbContext(typeof(DataContext))] 15 | [Migration("20220624203034_Weapon")] 16 | partial class Weapon 17 | { 18 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 19 | { 20 | #pragma warning disable 612, 618 21 | modelBuilder 22 | .HasAnnotation("ProductVersion", "6.0.3") 23 | .HasAnnotation("Relational:MaxIdentifierLength", 128); 24 | 25 | SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); 26 | 27 | modelBuilder.Entity("dotnet_rpg.Models.Character", b => 28 | { 29 | b.Property("Id") 30 | .ValueGeneratedOnAdd() 31 | .HasColumnType("int"); 32 | 33 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 34 | 35 | b.Property("Class") 36 | .HasColumnType("int"); 37 | 38 | b.Property("Defense") 39 | .HasColumnType("int"); 40 | 41 | b.Property("HitPoints") 42 | .HasColumnType("int"); 43 | 44 | b.Property("Intelligence") 45 | .HasColumnType("int"); 46 | 47 | b.Property("Name") 48 | .IsRequired() 49 | .HasColumnType("nvarchar(max)"); 50 | 51 | b.Property("Strength") 52 | .HasColumnType("int"); 53 | 54 | b.Property("UserId") 55 | .HasColumnType("int"); 56 | 57 | b.HasKey("Id"); 58 | 59 | b.HasIndex("UserId"); 60 | 61 | b.ToTable("Characters"); 62 | }); 63 | 64 | modelBuilder.Entity("dotnet_rpg.Models.User", b => 65 | { 66 | b.Property("Id") 67 | .ValueGeneratedOnAdd() 68 | .HasColumnType("int"); 69 | 70 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 71 | 72 | b.Property("PasswordHash") 73 | .IsRequired() 74 | .HasColumnType("varbinary(max)"); 75 | 76 | b.Property("PasswordSalt") 77 | .IsRequired() 78 | .HasColumnType("varbinary(max)"); 79 | 80 | b.Property("Username") 81 | .IsRequired() 82 | .HasColumnType("nvarchar(max)"); 83 | 84 | b.HasKey("Id"); 85 | 86 | b.ToTable("Users"); 87 | }); 88 | 89 | modelBuilder.Entity("dotnet_rpg.Models.Weapon", b => 90 | { 91 | b.Property("Id") 92 | .ValueGeneratedOnAdd() 93 | .HasColumnType("int"); 94 | 95 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 96 | 97 | b.Property("CharacterId") 98 | .HasColumnType("int"); 99 | 100 | b.Property("Damage") 101 | .HasColumnType("int"); 102 | 103 | b.Property("Name") 104 | .IsRequired() 105 | .HasColumnType("nvarchar(max)"); 106 | 107 | b.HasKey("Id"); 108 | 109 | b.HasIndex("CharacterId") 110 | .IsUnique(); 111 | 112 | b.ToTable("Weapons"); 113 | }); 114 | 115 | modelBuilder.Entity("dotnet_rpg.Models.Character", b => 116 | { 117 | b.HasOne("dotnet_rpg.Models.User", "User") 118 | .WithMany("Characters") 119 | .HasForeignKey("UserId"); 120 | 121 | b.Navigation("User"); 122 | }); 123 | 124 | modelBuilder.Entity("dotnet_rpg.Models.Weapon", b => 125 | { 126 | b.HasOne("dotnet_rpg.Models.Character", "Character") 127 | .WithOne("Weapon") 128 | .HasForeignKey("dotnet_rpg.Models.Weapon", "CharacterId") 129 | .OnDelete(DeleteBehavior.Cascade) 130 | .IsRequired(); 131 | 132 | b.Navigation("Character"); 133 | }); 134 | 135 | modelBuilder.Entity("dotnet_rpg.Models.Character", b => 136 | { 137 | b.Navigation("Weapon") 138 | .IsRequired(); 139 | }); 140 | 141 | modelBuilder.Entity("dotnet_rpg.Models.User", b => 142 | { 143 | b.Navigation("Characters"); 144 | }); 145 | #pragma warning restore 612, 618 146 | } 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /Migrations/20220624203034_Weapon.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore.Migrations; 2 | 3 | #nullable disable 4 | 5 | namespace dotnet_rpg.Migrations 6 | { 7 | public partial class Weapon : Migration 8 | { 9 | protected override void Up(MigrationBuilder migrationBuilder) 10 | { 11 | migrationBuilder.CreateTable( 12 | name: "Weapons", 13 | columns: table => new 14 | { 15 | Id = table.Column(type: "int", nullable: false) 16 | .Annotation("SqlServer:Identity", "1, 1"), 17 | Name = table.Column(type: "nvarchar(max)", nullable: false), 18 | Damage = table.Column(type: "int", nullable: false), 19 | CharacterId = table.Column(type: "int", nullable: false) 20 | }, 21 | constraints: table => 22 | { 23 | table.PrimaryKey("PK_Weapons", x => x.Id); 24 | table.ForeignKey( 25 | name: "FK_Weapons_Characters_CharacterId", 26 | column: x => x.CharacterId, 27 | principalTable: "Characters", 28 | principalColumn: "Id", 29 | onDelete: ReferentialAction.Cascade); 30 | }); 31 | 32 | migrationBuilder.CreateIndex( 33 | name: "IX_Weapons_CharacterId", 34 | table: "Weapons", 35 | column: "CharacterId", 36 | unique: true); 37 | } 38 | 39 | protected override void Down(MigrationBuilder migrationBuilder) 40 | { 41 | migrationBuilder.DropTable( 42 | name: "Weapons"); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Migrations/20220624211428_Skill.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 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 | using dotnet_rpg.Data; 9 | 10 | #nullable disable 11 | 12 | namespace dotnet_rpg.Migrations 13 | { 14 | [DbContext(typeof(DataContext))] 15 | [Migration("20220624211428_Skill")] 16 | partial class Skill 17 | { 18 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 19 | { 20 | #pragma warning disable 612, 618 21 | modelBuilder 22 | .HasAnnotation("ProductVersion", "6.0.3") 23 | .HasAnnotation("Relational:MaxIdentifierLength", 128); 24 | 25 | SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); 26 | 27 | modelBuilder.Entity("CharacterSkill", b => 28 | { 29 | b.Property("CharactersId") 30 | .HasColumnType("int"); 31 | 32 | b.Property("SkillsId") 33 | .HasColumnType("int"); 34 | 35 | b.HasKey("CharactersId", "SkillsId"); 36 | 37 | b.HasIndex("SkillsId"); 38 | 39 | b.ToTable("CharacterSkill"); 40 | }); 41 | 42 | modelBuilder.Entity("dotnet_rpg.Models.Character", b => 43 | { 44 | b.Property("Id") 45 | .ValueGeneratedOnAdd() 46 | .HasColumnType("int"); 47 | 48 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 49 | 50 | b.Property("Class") 51 | .HasColumnType("int"); 52 | 53 | b.Property("Defense") 54 | .HasColumnType("int"); 55 | 56 | b.Property("HitPoints") 57 | .HasColumnType("int"); 58 | 59 | b.Property("Intelligence") 60 | .HasColumnType("int"); 61 | 62 | b.Property("Name") 63 | .IsRequired() 64 | .HasColumnType("nvarchar(max)"); 65 | 66 | b.Property("Strength") 67 | .HasColumnType("int"); 68 | 69 | b.Property("UserId") 70 | .HasColumnType("int"); 71 | 72 | b.HasKey("Id"); 73 | 74 | b.HasIndex("UserId"); 75 | 76 | b.ToTable("Characters"); 77 | }); 78 | 79 | modelBuilder.Entity("dotnet_rpg.Models.Skill", b => 80 | { 81 | b.Property("Id") 82 | .ValueGeneratedOnAdd() 83 | .HasColumnType("int"); 84 | 85 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 86 | 87 | b.Property("Damage") 88 | .HasColumnType("int"); 89 | 90 | b.Property("Name") 91 | .IsRequired() 92 | .HasColumnType("nvarchar(max)"); 93 | 94 | b.HasKey("Id"); 95 | 96 | b.ToTable("Skills"); 97 | }); 98 | 99 | modelBuilder.Entity("dotnet_rpg.Models.User", b => 100 | { 101 | b.Property("Id") 102 | .ValueGeneratedOnAdd() 103 | .HasColumnType("int"); 104 | 105 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 106 | 107 | b.Property("PasswordHash") 108 | .IsRequired() 109 | .HasColumnType("varbinary(max)"); 110 | 111 | b.Property("PasswordSalt") 112 | .IsRequired() 113 | .HasColumnType("varbinary(max)"); 114 | 115 | b.Property("Username") 116 | .IsRequired() 117 | .HasColumnType("nvarchar(max)"); 118 | 119 | b.HasKey("Id"); 120 | 121 | b.ToTable("Users"); 122 | }); 123 | 124 | modelBuilder.Entity("dotnet_rpg.Models.Weapon", b => 125 | { 126 | b.Property("Id") 127 | .ValueGeneratedOnAdd() 128 | .HasColumnType("int"); 129 | 130 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 131 | 132 | b.Property("CharacterId") 133 | .HasColumnType("int"); 134 | 135 | b.Property("Damage") 136 | .HasColumnType("int"); 137 | 138 | b.Property("Name") 139 | .IsRequired() 140 | .HasColumnType("nvarchar(max)"); 141 | 142 | b.HasKey("Id"); 143 | 144 | b.HasIndex("CharacterId") 145 | .IsUnique(); 146 | 147 | b.ToTable("Weapons"); 148 | }); 149 | 150 | modelBuilder.Entity("CharacterSkill", b => 151 | { 152 | b.HasOne("dotnet_rpg.Models.Character", null) 153 | .WithMany() 154 | .HasForeignKey("CharactersId") 155 | .OnDelete(DeleteBehavior.Cascade) 156 | .IsRequired(); 157 | 158 | b.HasOne("dotnet_rpg.Models.Skill", null) 159 | .WithMany() 160 | .HasForeignKey("SkillsId") 161 | .OnDelete(DeleteBehavior.Cascade) 162 | .IsRequired(); 163 | }); 164 | 165 | modelBuilder.Entity("dotnet_rpg.Models.Character", b => 166 | { 167 | b.HasOne("dotnet_rpg.Models.User", "User") 168 | .WithMany("Characters") 169 | .HasForeignKey("UserId"); 170 | 171 | b.Navigation("User"); 172 | }); 173 | 174 | modelBuilder.Entity("dotnet_rpg.Models.Weapon", b => 175 | { 176 | b.HasOne("dotnet_rpg.Models.Character", "Character") 177 | .WithOne("Weapon") 178 | .HasForeignKey("dotnet_rpg.Models.Weapon", "CharacterId") 179 | .OnDelete(DeleteBehavior.Cascade) 180 | .IsRequired(); 181 | 182 | b.Navigation("Character"); 183 | }); 184 | 185 | modelBuilder.Entity("dotnet_rpg.Models.Character", b => 186 | { 187 | b.Navigation("Weapon") 188 | .IsRequired(); 189 | }); 190 | 191 | modelBuilder.Entity("dotnet_rpg.Models.User", b => 192 | { 193 | b.Navigation("Characters"); 194 | }); 195 | #pragma warning restore 612, 618 196 | } 197 | } 198 | } 199 | -------------------------------------------------------------------------------- /Migrations/20220624211428_Skill.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore.Migrations; 2 | 3 | #nullable disable 4 | 5 | namespace dotnet_rpg.Migrations 6 | { 7 | public partial class Skill : Migration 8 | { 9 | protected override void Up(MigrationBuilder migrationBuilder) 10 | { 11 | migrationBuilder.CreateTable( 12 | name: "Skills", 13 | columns: table => new 14 | { 15 | Id = table.Column(type: "int", nullable: false) 16 | .Annotation("SqlServer:Identity", "1, 1"), 17 | Name = table.Column(type: "nvarchar(max)", nullable: false), 18 | Damage = table.Column(type: "int", nullable: false) 19 | }, 20 | constraints: table => 21 | { 22 | table.PrimaryKey("PK_Skills", x => x.Id); 23 | }); 24 | 25 | migrationBuilder.CreateTable( 26 | name: "CharacterSkill", 27 | columns: table => new 28 | { 29 | CharactersId = table.Column(type: "int", nullable: false), 30 | SkillsId = table.Column(type: "int", nullable: false) 31 | }, 32 | constraints: table => 33 | { 34 | table.PrimaryKey("PK_CharacterSkill", x => new { x.CharactersId, x.SkillsId }); 35 | table.ForeignKey( 36 | name: "FK_CharacterSkill_Characters_CharactersId", 37 | column: x => x.CharactersId, 38 | principalTable: "Characters", 39 | principalColumn: "Id", 40 | onDelete: ReferentialAction.Cascade); 41 | table.ForeignKey( 42 | name: "FK_CharacterSkill_Skills_SkillsId", 43 | column: x => x.SkillsId, 44 | principalTable: "Skills", 45 | principalColumn: "Id", 46 | onDelete: ReferentialAction.Cascade); 47 | }); 48 | 49 | migrationBuilder.CreateIndex( 50 | name: "IX_CharacterSkill_SkillsId", 51 | table: "CharacterSkill", 52 | column: "SkillsId"); 53 | } 54 | 55 | protected override void Down(MigrationBuilder migrationBuilder) 56 | { 57 | migrationBuilder.DropTable( 58 | name: "CharacterSkill"); 59 | 60 | migrationBuilder.DropTable( 61 | name: "Skills"); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Migrations/20220624212109_SkillSeeding.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 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 | using dotnet_rpg.Data; 9 | 10 | #nullable disable 11 | 12 | namespace dotnet_rpg.Migrations 13 | { 14 | [DbContext(typeof(DataContext))] 15 | [Migration("20220624212109_SkillSeeding")] 16 | partial class SkillSeeding 17 | { 18 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 19 | { 20 | #pragma warning disable 612, 618 21 | modelBuilder 22 | .HasAnnotation("ProductVersion", "6.0.3") 23 | .HasAnnotation("Relational:MaxIdentifierLength", 128); 24 | 25 | SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); 26 | 27 | modelBuilder.Entity("CharacterSkill", b => 28 | { 29 | b.Property("CharactersId") 30 | .HasColumnType("int"); 31 | 32 | b.Property("SkillsId") 33 | .HasColumnType("int"); 34 | 35 | b.HasKey("CharactersId", "SkillsId"); 36 | 37 | b.HasIndex("SkillsId"); 38 | 39 | b.ToTable("CharacterSkill"); 40 | }); 41 | 42 | modelBuilder.Entity("dotnet_rpg.Models.Character", b => 43 | { 44 | b.Property("Id") 45 | .ValueGeneratedOnAdd() 46 | .HasColumnType("int"); 47 | 48 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 49 | 50 | b.Property("Class") 51 | .HasColumnType("int"); 52 | 53 | b.Property("Defense") 54 | .HasColumnType("int"); 55 | 56 | b.Property("HitPoints") 57 | .HasColumnType("int"); 58 | 59 | b.Property("Intelligence") 60 | .HasColumnType("int"); 61 | 62 | b.Property("Name") 63 | .IsRequired() 64 | .HasColumnType("nvarchar(max)"); 65 | 66 | b.Property("Strength") 67 | .HasColumnType("int"); 68 | 69 | b.Property("UserId") 70 | .HasColumnType("int"); 71 | 72 | b.HasKey("Id"); 73 | 74 | b.HasIndex("UserId"); 75 | 76 | b.ToTable("Characters"); 77 | }); 78 | 79 | modelBuilder.Entity("dotnet_rpg.Models.Skill", b => 80 | { 81 | b.Property("Id") 82 | .ValueGeneratedOnAdd() 83 | .HasColumnType("int"); 84 | 85 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 86 | 87 | b.Property("Damage") 88 | .HasColumnType("int"); 89 | 90 | b.Property("Name") 91 | .IsRequired() 92 | .HasColumnType("nvarchar(max)"); 93 | 94 | b.HasKey("Id"); 95 | 96 | b.ToTable("Skills"); 97 | 98 | b.HasData( 99 | new 100 | { 101 | Id = 1, 102 | Damage = 30, 103 | Name = "Fireball" 104 | }, 105 | new 106 | { 107 | Id = 2, 108 | Damage = 20, 109 | Name = "Frenzy" 110 | }, 111 | new 112 | { 113 | Id = 3, 114 | Damage = 50, 115 | Name = "Blizzard" 116 | }); 117 | }); 118 | 119 | modelBuilder.Entity("dotnet_rpg.Models.User", b => 120 | { 121 | b.Property("Id") 122 | .ValueGeneratedOnAdd() 123 | .HasColumnType("int"); 124 | 125 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 126 | 127 | b.Property("PasswordHash") 128 | .IsRequired() 129 | .HasColumnType("varbinary(max)"); 130 | 131 | b.Property("PasswordSalt") 132 | .IsRequired() 133 | .HasColumnType("varbinary(max)"); 134 | 135 | b.Property("Username") 136 | .IsRequired() 137 | .HasColumnType("nvarchar(max)"); 138 | 139 | b.HasKey("Id"); 140 | 141 | b.ToTable("Users"); 142 | }); 143 | 144 | modelBuilder.Entity("dotnet_rpg.Models.Weapon", b => 145 | { 146 | b.Property("Id") 147 | .ValueGeneratedOnAdd() 148 | .HasColumnType("int"); 149 | 150 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 151 | 152 | b.Property("CharacterId") 153 | .HasColumnType("int"); 154 | 155 | b.Property("Damage") 156 | .HasColumnType("int"); 157 | 158 | b.Property("Name") 159 | .IsRequired() 160 | .HasColumnType("nvarchar(max)"); 161 | 162 | b.HasKey("Id"); 163 | 164 | b.HasIndex("CharacterId") 165 | .IsUnique(); 166 | 167 | b.ToTable("Weapons"); 168 | }); 169 | 170 | modelBuilder.Entity("CharacterSkill", b => 171 | { 172 | b.HasOne("dotnet_rpg.Models.Character", null) 173 | .WithMany() 174 | .HasForeignKey("CharactersId") 175 | .OnDelete(DeleteBehavior.Cascade) 176 | .IsRequired(); 177 | 178 | b.HasOne("dotnet_rpg.Models.Skill", null) 179 | .WithMany() 180 | .HasForeignKey("SkillsId") 181 | .OnDelete(DeleteBehavior.Cascade) 182 | .IsRequired(); 183 | }); 184 | 185 | modelBuilder.Entity("dotnet_rpg.Models.Character", b => 186 | { 187 | b.HasOne("dotnet_rpg.Models.User", "User") 188 | .WithMany("Characters") 189 | .HasForeignKey("UserId"); 190 | 191 | b.Navigation("User"); 192 | }); 193 | 194 | modelBuilder.Entity("dotnet_rpg.Models.Weapon", b => 195 | { 196 | b.HasOne("dotnet_rpg.Models.Character", "Character") 197 | .WithOne("Weapon") 198 | .HasForeignKey("dotnet_rpg.Models.Weapon", "CharacterId") 199 | .OnDelete(DeleteBehavior.Cascade) 200 | .IsRequired(); 201 | 202 | b.Navigation("Character"); 203 | }); 204 | 205 | modelBuilder.Entity("dotnet_rpg.Models.Character", b => 206 | { 207 | b.Navigation("Weapon") 208 | .IsRequired(); 209 | }); 210 | 211 | modelBuilder.Entity("dotnet_rpg.Models.User", b => 212 | { 213 | b.Navigation("Characters"); 214 | }); 215 | #pragma warning restore 612, 618 216 | } 217 | } 218 | } 219 | -------------------------------------------------------------------------------- /Migrations/20220624212109_SkillSeeding.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore.Migrations; 2 | 3 | #nullable disable 4 | 5 | namespace dotnet_rpg.Migrations 6 | { 7 | public partial class SkillSeeding : Migration 8 | { 9 | protected override void Up(MigrationBuilder migrationBuilder) 10 | { 11 | migrationBuilder.InsertData( 12 | table: "Skills", 13 | columns: new[] { "Id", "Damage", "Name" }, 14 | values: new object[] { 1, 30, "Fireball" }); 15 | 16 | migrationBuilder.InsertData( 17 | table: "Skills", 18 | columns: new[] { "Id", "Damage", "Name" }, 19 | values: new object[] { 2, 20, "Frenzy" }); 20 | 21 | migrationBuilder.InsertData( 22 | table: "Skills", 23 | columns: new[] { "Id", "Damage", "Name" }, 24 | values: new object[] { 3, 50, "Blizzard" }); 25 | } 26 | 27 | protected override void Down(MigrationBuilder migrationBuilder) 28 | { 29 | migrationBuilder.DeleteData( 30 | table: "Skills", 31 | keyColumn: "Id", 32 | keyValue: 1); 33 | 34 | migrationBuilder.DeleteData( 35 | table: "Skills", 36 | keyColumn: "Id", 37 | keyValue: 2); 38 | 39 | migrationBuilder.DeleteData( 40 | table: "Skills", 41 | keyColumn: "Id", 42 | keyValue: 3); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Migrations/20220624231340_FightProperties.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 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 | using dotnet_rpg.Data; 9 | 10 | #nullable disable 11 | 12 | namespace dotnet_rpg.Migrations 13 | { 14 | [DbContext(typeof(DataContext))] 15 | [Migration("20220624231340_FightProperties")] 16 | partial class FightProperties 17 | { 18 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 19 | { 20 | #pragma warning disable 612, 618 21 | modelBuilder 22 | .HasAnnotation("ProductVersion", "6.0.3") 23 | .HasAnnotation("Relational:MaxIdentifierLength", 128); 24 | 25 | SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); 26 | 27 | modelBuilder.Entity("CharacterSkill", b => 28 | { 29 | b.Property("CharactersId") 30 | .HasColumnType("int"); 31 | 32 | b.Property("SkillsId") 33 | .HasColumnType("int"); 34 | 35 | b.HasKey("CharactersId", "SkillsId"); 36 | 37 | b.HasIndex("SkillsId"); 38 | 39 | b.ToTable("CharacterSkill"); 40 | }); 41 | 42 | modelBuilder.Entity("dotnet_rpg.Models.Character", b => 43 | { 44 | b.Property("Id") 45 | .ValueGeneratedOnAdd() 46 | .HasColumnType("int"); 47 | 48 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 49 | 50 | b.Property("Class") 51 | .HasColumnType("int"); 52 | 53 | b.Property("Defeats") 54 | .HasColumnType("int"); 55 | 56 | b.Property("Defense") 57 | .HasColumnType("int"); 58 | 59 | b.Property("Fights") 60 | .HasColumnType("int"); 61 | 62 | b.Property("HitPoints") 63 | .HasColumnType("int"); 64 | 65 | b.Property("Intelligence") 66 | .HasColumnType("int"); 67 | 68 | b.Property("Name") 69 | .IsRequired() 70 | .HasColumnType("nvarchar(max)"); 71 | 72 | b.Property("Strength") 73 | .HasColumnType("int"); 74 | 75 | b.Property("UserId") 76 | .HasColumnType("int"); 77 | 78 | b.Property("Victories") 79 | .HasColumnType("int"); 80 | 81 | b.HasKey("Id"); 82 | 83 | b.HasIndex("UserId"); 84 | 85 | b.ToTable("Characters"); 86 | }); 87 | 88 | modelBuilder.Entity("dotnet_rpg.Models.Skill", b => 89 | { 90 | b.Property("Id") 91 | .ValueGeneratedOnAdd() 92 | .HasColumnType("int"); 93 | 94 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 95 | 96 | b.Property("Damage") 97 | .HasColumnType("int"); 98 | 99 | b.Property("Name") 100 | .IsRequired() 101 | .HasColumnType("nvarchar(max)"); 102 | 103 | b.HasKey("Id"); 104 | 105 | b.ToTable("Skills"); 106 | 107 | b.HasData( 108 | new 109 | { 110 | Id = 1, 111 | Damage = 30, 112 | Name = "Fireball" 113 | }, 114 | new 115 | { 116 | Id = 2, 117 | Damage = 20, 118 | Name = "Frenzy" 119 | }, 120 | new 121 | { 122 | Id = 3, 123 | Damage = 50, 124 | Name = "Blizzard" 125 | }); 126 | }); 127 | 128 | modelBuilder.Entity("dotnet_rpg.Models.User", b => 129 | { 130 | b.Property("Id") 131 | .ValueGeneratedOnAdd() 132 | .HasColumnType("int"); 133 | 134 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 135 | 136 | b.Property("PasswordHash") 137 | .IsRequired() 138 | .HasColumnType("varbinary(max)"); 139 | 140 | b.Property("PasswordSalt") 141 | .IsRequired() 142 | .HasColumnType("varbinary(max)"); 143 | 144 | b.Property("Username") 145 | .IsRequired() 146 | .HasColumnType("nvarchar(max)"); 147 | 148 | b.HasKey("Id"); 149 | 150 | b.ToTable("Users"); 151 | }); 152 | 153 | modelBuilder.Entity("dotnet_rpg.Models.Weapon", b => 154 | { 155 | b.Property("Id") 156 | .ValueGeneratedOnAdd() 157 | .HasColumnType("int"); 158 | 159 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 160 | 161 | b.Property("CharacterId") 162 | .HasColumnType("int"); 163 | 164 | b.Property("Damage") 165 | .HasColumnType("int"); 166 | 167 | b.Property("Name") 168 | .IsRequired() 169 | .HasColumnType("nvarchar(max)"); 170 | 171 | b.HasKey("Id"); 172 | 173 | b.HasIndex("CharacterId") 174 | .IsUnique(); 175 | 176 | b.ToTable("Weapons"); 177 | }); 178 | 179 | modelBuilder.Entity("CharacterSkill", b => 180 | { 181 | b.HasOne("dotnet_rpg.Models.Character", null) 182 | .WithMany() 183 | .HasForeignKey("CharactersId") 184 | .OnDelete(DeleteBehavior.Cascade) 185 | .IsRequired(); 186 | 187 | b.HasOne("dotnet_rpg.Models.Skill", null) 188 | .WithMany() 189 | .HasForeignKey("SkillsId") 190 | .OnDelete(DeleteBehavior.Cascade) 191 | .IsRequired(); 192 | }); 193 | 194 | modelBuilder.Entity("dotnet_rpg.Models.Character", b => 195 | { 196 | b.HasOne("dotnet_rpg.Models.User", "User") 197 | .WithMany("Characters") 198 | .HasForeignKey("UserId"); 199 | 200 | b.Navigation("User"); 201 | }); 202 | 203 | modelBuilder.Entity("dotnet_rpg.Models.Weapon", b => 204 | { 205 | b.HasOne("dotnet_rpg.Models.Character", "Character") 206 | .WithOne("Weapon") 207 | .HasForeignKey("dotnet_rpg.Models.Weapon", "CharacterId") 208 | .OnDelete(DeleteBehavior.Cascade) 209 | .IsRequired(); 210 | 211 | b.Navigation("Character"); 212 | }); 213 | 214 | modelBuilder.Entity("dotnet_rpg.Models.Character", b => 215 | { 216 | b.Navigation("Weapon") 217 | .IsRequired(); 218 | }); 219 | 220 | modelBuilder.Entity("dotnet_rpg.Models.User", b => 221 | { 222 | b.Navigation("Characters"); 223 | }); 224 | #pragma warning restore 612, 618 225 | } 226 | } 227 | } 228 | -------------------------------------------------------------------------------- /Migrations/20220624231340_FightProperties.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore.Migrations; 2 | 3 | #nullable disable 4 | 5 | namespace dotnet_rpg.Migrations 6 | { 7 | public partial class FightProperties : Migration 8 | { 9 | protected override void Up(MigrationBuilder migrationBuilder) 10 | { 11 | migrationBuilder.AddColumn( 12 | name: "Defeats", 13 | table: "Characters", 14 | type: "int", 15 | nullable: false, 16 | defaultValue: 0); 17 | 18 | migrationBuilder.AddColumn( 19 | name: "Fights", 20 | table: "Characters", 21 | type: "int", 22 | nullable: false, 23 | defaultValue: 0); 24 | 25 | migrationBuilder.AddColumn( 26 | name: "Victories", 27 | table: "Characters", 28 | type: "int", 29 | nullable: false, 30 | defaultValue: 0); 31 | } 32 | 33 | protected override void Down(MigrationBuilder migrationBuilder) 34 | { 35 | migrationBuilder.DropColumn( 36 | name: "Defeats", 37 | table: "Characters"); 38 | 39 | migrationBuilder.DropColumn( 40 | name: "Fights", 41 | table: "Characters"); 42 | 43 | migrationBuilder.DropColumn( 44 | name: "Victories", 45 | table: "Characters"); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Migrations/DataContextModelSnapshot.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Microsoft.EntityFrameworkCore; 4 | using Microsoft.EntityFrameworkCore.Infrastructure; 5 | using Microsoft.EntityFrameworkCore.Metadata; 6 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 7 | using dotnet_rpg.Data; 8 | 9 | #nullable disable 10 | 11 | namespace dotnet_rpg.Migrations 12 | { 13 | [DbContext(typeof(DataContext))] 14 | partial class DataContextModelSnapshot : ModelSnapshot 15 | { 16 | protected override void BuildModel(ModelBuilder modelBuilder) 17 | { 18 | #pragma warning disable 612, 618 19 | modelBuilder 20 | .HasAnnotation("ProductVersion", "6.0.3") 21 | .HasAnnotation("Relational:MaxIdentifierLength", 128); 22 | 23 | SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); 24 | 25 | modelBuilder.Entity("CharacterSkill", b => 26 | { 27 | b.Property("CharactersId") 28 | .HasColumnType("int"); 29 | 30 | b.Property("SkillsId") 31 | .HasColumnType("int"); 32 | 33 | b.HasKey("CharactersId", "SkillsId"); 34 | 35 | b.HasIndex("SkillsId"); 36 | 37 | b.ToTable("CharacterSkill"); 38 | }); 39 | 40 | modelBuilder.Entity("dotnet_rpg.Models.Character", b => 41 | { 42 | b.Property("Id") 43 | .ValueGeneratedOnAdd() 44 | .HasColumnType("int"); 45 | 46 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 47 | 48 | b.Property("Class") 49 | .HasColumnType("int"); 50 | 51 | b.Property("Defeats") 52 | .HasColumnType("int"); 53 | 54 | b.Property("Defense") 55 | .HasColumnType("int"); 56 | 57 | b.Property("Fights") 58 | .HasColumnType("int"); 59 | 60 | b.Property("HitPoints") 61 | .HasColumnType("int"); 62 | 63 | b.Property("Intelligence") 64 | .HasColumnType("int"); 65 | 66 | b.Property("Name") 67 | .IsRequired() 68 | .HasColumnType("nvarchar(max)"); 69 | 70 | b.Property("Strength") 71 | .HasColumnType("int"); 72 | 73 | b.Property("UserId") 74 | .HasColumnType("int"); 75 | 76 | b.Property("Victories") 77 | .HasColumnType("int"); 78 | 79 | b.HasKey("Id"); 80 | 81 | b.HasIndex("UserId"); 82 | 83 | b.ToTable("Characters"); 84 | }); 85 | 86 | modelBuilder.Entity("dotnet_rpg.Models.Skill", b => 87 | { 88 | b.Property("Id") 89 | .ValueGeneratedOnAdd() 90 | .HasColumnType("int"); 91 | 92 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 93 | 94 | b.Property("Damage") 95 | .HasColumnType("int"); 96 | 97 | b.Property("Name") 98 | .IsRequired() 99 | .HasColumnType("nvarchar(max)"); 100 | 101 | b.HasKey("Id"); 102 | 103 | b.ToTable("Skills"); 104 | 105 | b.HasData( 106 | new 107 | { 108 | Id = 1, 109 | Damage = 30, 110 | Name = "Fireball" 111 | }, 112 | new 113 | { 114 | Id = 2, 115 | Damage = 20, 116 | Name = "Frenzy" 117 | }, 118 | new 119 | { 120 | Id = 3, 121 | Damage = 50, 122 | Name = "Blizzard" 123 | }); 124 | }); 125 | 126 | modelBuilder.Entity("dotnet_rpg.Models.User", b => 127 | { 128 | b.Property("Id") 129 | .ValueGeneratedOnAdd() 130 | .HasColumnType("int"); 131 | 132 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 133 | 134 | b.Property("PasswordHash") 135 | .IsRequired() 136 | .HasColumnType("varbinary(max)"); 137 | 138 | b.Property("PasswordSalt") 139 | .IsRequired() 140 | .HasColumnType("varbinary(max)"); 141 | 142 | b.Property("Username") 143 | .IsRequired() 144 | .HasColumnType("nvarchar(max)"); 145 | 146 | b.HasKey("Id"); 147 | 148 | b.ToTable("Users"); 149 | }); 150 | 151 | modelBuilder.Entity("dotnet_rpg.Models.Weapon", b => 152 | { 153 | b.Property("Id") 154 | .ValueGeneratedOnAdd() 155 | .HasColumnType("int"); 156 | 157 | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); 158 | 159 | b.Property("CharacterId") 160 | .HasColumnType("int"); 161 | 162 | b.Property("Damage") 163 | .HasColumnType("int"); 164 | 165 | b.Property("Name") 166 | .IsRequired() 167 | .HasColumnType("nvarchar(max)"); 168 | 169 | b.HasKey("Id"); 170 | 171 | b.HasIndex("CharacterId") 172 | .IsUnique(); 173 | 174 | b.ToTable("Weapons"); 175 | }); 176 | 177 | modelBuilder.Entity("CharacterSkill", b => 178 | { 179 | b.HasOne("dotnet_rpg.Models.Character", null) 180 | .WithMany() 181 | .HasForeignKey("CharactersId") 182 | .OnDelete(DeleteBehavior.Cascade) 183 | .IsRequired(); 184 | 185 | b.HasOne("dotnet_rpg.Models.Skill", null) 186 | .WithMany() 187 | .HasForeignKey("SkillsId") 188 | .OnDelete(DeleteBehavior.Cascade) 189 | .IsRequired(); 190 | }); 191 | 192 | modelBuilder.Entity("dotnet_rpg.Models.Character", b => 193 | { 194 | b.HasOne("dotnet_rpg.Models.User", "User") 195 | .WithMany("Characters") 196 | .HasForeignKey("UserId"); 197 | 198 | b.Navigation("User"); 199 | }); 200 | 201 | modelBuilder.Entity("dotnet_rpg.Models.Weapon", b => 202 | { 203 | b.HasOne("dotnet_rpg.Models.Character", "Character") 204 | .WithOne("Weapon") 205 | .HasForeignKey("dotnet_rpg.Models.Weapon", "CharacterId") 206 | .OnDelete(DeleteBehavior.Cascade) 207 | .IsRequired(); 208 | 209 | b.Navigation("Character"); 210 | }); 211 | 212 | modelBuilder.Entity("dotnet_rpg.Models.Character", b => 213 | { 214 | b.Navigation("Weapon") 215 | .IsRequired(); 216 | }); 217 | 218 | modelBuilder.Entity("dotnet_rpg.Models.User", b => 219 | { 220 | b.Navigation("Characters"); 221 | }); 222 | #pragma warning restore 612, 618 223 | } 224 | } 225 | } 226 | -------------------------------------------------------------------------------- /Models/Character.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace dotnet_rpg.Models 7 | { 8 | public class Character 9 | { 10 | public int Id { get; set; } 11 | public string Name { get; set; } = "Frodo"; 12 | public int HitPoints { get; set; } = 100; 13 | public int Strength { get; set; } = 10; 14 | public int Defense { get; set; } = 10; 15 | public int Intelligence { get; set; } = 10; 16 | public RpgClass Class { get; set; } = RpgClass.Knight; 17 | public User? User { get; set; } 18 | public Weapon Weapon { get; set; } 19 | public List Skills { get; set; } 20 | public int Fights { get; set; } 21 | public int Victories { get; set; } 22 | public int Defeats { get; set; } 23 | } 24 | } -------------------------------------------------------------------------------- /Models/RpgClass.cs: -------------------------------------------------------------------------------- 1 | using System.Text.Json.Serialization; 2 | 3 | namespace dotnet_rpg.Models 4 | { 5 | [JsonConverter(typeof(JsonStringEnumConverter))] 6 | public enum RpgClass 7 | { 8 | Knight = 1, 9 | Mage = 2, 10 | Cleric = 3 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Models/ServiceResponse.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace dotnet_rpg.Models 7 | { 8 | public class ServiceResponse 9 | { 10 | public T? Data { get; set; } 11 | public bool Success { get; set; } = true; 12 | public string Message { get; set; } = string.Empty; 13 | } 14 | } -------------------------------------------------------------------------------- /Models/Skill.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace dotnet_rpg.Models 7 | { 8 | public class Skill 9 | { 10 | public int Id { get; set; } 11 | public string Name { get; set; } = string.Empty; 12 | public int Damage { get; set; } 13 | public List Characters { get; set; } 14 | } 15 | } -------------------------------------------------------------------------------- /Models/User.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace dotnet_rpg.Models 7 | { 8 | public class User 9 | { 10 | public int Id { get; set; } 11 | public string Username { get; set; } = string.Empty; 12 | public byte[] PasswordHash { get; set; } 13 | public byte[] PasswordSalt { get; set; } 14 | public List? Characters { get; set; } 15 | } 16 | } -------------------------------------------------------------------------------- /Models/Weapon.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace dotnet_rpg.Models 7 | { 8 | public class Weapon 9 | { 10 | public int Id { get; set; } 11 | public string Name { get; set; } = string.Empty; 12 | public int Damage { get; set; } 13 | public Character Character { get; set; } 14 | public int CharacterId { get; set; } 15 | } 16 | } -------------------------------------------------------------------------------- /Program.cs: -------------------------------------------------------------------------------- 1 | global using dotnet_rpg.Models; 2 | using dotnet_rpg.Data; 3 | using dotnet_rpg.Services.CharacterService; 4 | using dotnet_rpg.Services.FightService; 5 | using dotnet_rpg.Services.WeaponService; 6 | using Microsoft.AspNetCore.Authentication.JwtBearer; 7 | using Microsoft.EntityFrameworkCore; 8 | using Microsoft.IdentityModel.Tokens; 9 | using Microsoft.OpenApi.Models; 10 | using Swashbuckle.AspNetCore.Filters; 11 | 12 | var builder = WebApplication.CreateBuilder(args); 13 | 14 | // Add services to the container. 15 | builder.Services.AddDbContext(options => 16 | options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))); 17 | builder.Services.AddControllers(); 18 | // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle 19 | builder.Services.AddEndpointsApiExplorer(); 20 | builder.Services.AddSwaggerGen(c => { 21 | c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme { 22 | Description = "Standard Authorization header using the Bearer scheme, e.g. \"bearer {token} \"", 23 | In = ParameterLocation.Header, 24 | Name = "Authorization", 25 | Type = SecuritySchemeType.ApiKey 26 | }); 27 | 28 | c.OperationFilter(); 29 | }); 30 | builder.Services.AddAutoMapper(typeof(Program).Assembly); 31 | 32 | 33 | 34 | 35 | builder.Services.AddScoped(); 36 | 37 | 38 | 39 | 40 | 41 | builder.Services.AddScoped(); 42 | builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) 43 | .AddJwtBearer(options => 44 | { 45 | options.TokenValidationParameters = new TokenValidationParameters 46 | { 47 | ValidateIssuerSigningKey = true, 48 | IssuerSigningKey = new SymmetricSecurityKey(System.Text.Encoding.UTF8 49 | .GetBytes(builder.Configuration.GetSection("AppSettings:Token").Value)), 50 | ValidateIssuer = false, 51 | ValidateAudience = false 52 | }; 53 | }); 54 | builder.Services.AddHttpContextAccessor(); 55 | builder.Services.AddScoped(); 56 | builder.Services.AddScoped(); 57 | 58 | var app = builder.Build(); 59 | 60 | // Configure the HTTP request pipeline. 61 | if (app.Environment.IsDevelopment()) 62 | { 63 | app.UseSwagger(); 64 | app.UseSwaggerUI(); 65 | } 66 | 67 | app.UseHttpsRedirection(); 68 | 69 | app.UseAuthentication(); 70 | 71 | app.UseAuthorization(); 72 | 73 | app.MapControllers(); 74 | 75 | app.Run(); 76 | -------------------------------------------------------------------------------- /Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/launchsettings.json", 3 | "iisSettings": { 4 | "windowsAuthentication": false, 5 | "anonymousAuthentication": true, 6 | "iisExpress": { 7 | "applicationUrl": "http://localhost:60577", 8 | "sslPort": 44355 9 | } 10 | }, 11 | "profiles": { 12 | "dotnet_rpg": { 13 | "commandName": "Project", 14 | "dotnetRunMessages": true, 15 | "launchBrowser": true, 16 | "launchUrl": "swagger", 17 | "applicationUrl": "https://localhost:7103;http://localhost:5001", 18 | "environmentVariables": { 19 | "ASPNETCORE_ENVIRONMENT": "Development" 20 | } 21 | }, 22 | "IIS Express": { 23 | "commandName": "IISExpress", 24 | "launchBrowser": true, 25 | "launchUrl": "swagger", 26 | "environmentVariables": { 27 | "ASPNETCORE_ENVIRONMENT": "Development" 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Services/CharacterService/CharacterService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Security.Claims; 5 | using System.Threading.Tasks; 6 | using AutoMapper; 7 | using dotnet_rpg.Data; 8 | using dotnet_rpg.Dtos.Character; 9 | using Microsoft.EntityFrameworkCore; 10 | 11 | namespace dotnet_rpg.Services.CharacterService 12 | { 13 | public class CharacterService : ICharacterService 14 | { 15 | private readonly IMapper _mapper; 16 | private readonly DataContext _context; 17 | private readonly IHttpContextAccessor _httpContextAccessor; 18 | 19 | public CharacterService(IMapper mapper, DataContext context, IHttpContextAccessor httpContextAccessor) 20 | { 21 | _mapper = mapper; 22 | _context = context; 23 | _httpContextAccessor = httpContextAccessor; 24 | } 25 | 26 | private int GetUserId() => int.Parse(_httpContextAccessor.HttpContext.User 27 | .FindFirstValue(ClaimTypes.NameIdentifier)); 28 | 29 | public async Task>> AddCharacter(AddCharacterDto newCharacter) 30 | { 31 | var serviceResponse = new ServiceResponse>(); 32 | Character character = _mapper.Map(newCharacter); 33 | character.User = await _context.Users.FirstOrDefaultAsync(u => u.Id == GetUserId()); 34 | 35 | _context.Characters.Add(character); 36 | await _context.SaveChangesAsync(); 37 | serviceResponse.Data = await _context.Characters 38 | .Where(c => c.User.Id == GetUserId()) 39 | .Select(c => _mapper.Map(c)) 40 | .ToListAsync(); 41 | return serviceResponse; 42 | } 43 | 44 | public async Task>> DeleteCharacter(int id) 45 | { 46 | ServiceResponse> response = new ServiceResponse>(); 47 | 48 | try 49 | { 50 | Character character = await _context.Characters 51 | .FirstOrDefaultAsync(c => c.Id == id && c.User.Id == GetUserId()); 52 | if (character != null) 53 | { 54 | _context.Characters.Remove(character); 55 | await _context.SaveChangesAsync(); 56 | response.Data = _context.Characters 57 | .Where(c => c.User.Id == GetUserId()) 58 | .Select(c => _mapper.Map(c)).ToList(); 59 | } 60 | else 61 | { 62 | response.Success = false; 63 | response.Message = "Character not found"; 64 | } 65 | } 66 | catch (Exception ex) 67 | { 68 | response.Success = false; 69 | response.Message = ex.Message; 70 | } 71 | 72 | return response; 73 | } 74 | 75 | public async Task>> GetAllCharacters() 76 | { 77 | var response = new ServiceResponse>(); 78 | var dbCharacters = await _context.Characters 79 | .Include(c => c.Weapon) 80 | .Include(c => c.Skills) 81 | .Where(c => c.User.Id == GetUserId()) 82 | .ToListAsync(); 83 | response.Data = dbCharacters.Select(c => _mapper.Map(c)).ToList(); 84 | return response; 85 | } 86 | 87 | public async Task> GetCharacterById(int id) 88 | { 89 | var serviceResponse = new ServiceResponse(); 90 | var dbCharacter = await _context.Characters 91 | .Include(c => c.Weapon) 92 | .Include(c => c.Skills) 93 | .FirstOrDefaultAsync(c => c.Id == id && c.User.Id == GetUserId()); 94 | serviceResponse.Data = _mapper.Map(dbCharacter); 95 | return serviceResponse; 96 | } 97 | 98 | public async Task> UpdateCharacter(UpdateCharacterDto updatedCharacter) 99 | { 100 | ServiceResponse response = new ServiceResponse(); 101 | 102 | try 103 | { 104 | var character = await _context.Characters 105 | .Include(c => c.User) 106 | .FirstOrDefaultAsync(c => c.Id == updatedCharacter.Id); 107 | 108 | if (character.User.Id == GetUserId()) 109 | { 110 | character.Name = updatedCharacter.Name; 111 | character.HitPoints = updatedCharacter.HitPoints; 112 | character.Strength = updatedCharacter.Strength; 113 | character.Defense = updatedCharacter.Defense; 114 | character.Intelligence = updatedCharacter.Intelligence; 115 | character.Class = updatedCharacter.Class; 116 | 117 | await _context.SaveChangesAsync(); 118 | 119 | response.Data = _mapper.Map(character); 120 | } 121 | else 122 | { 123 | response.Success = false; 124 | response.Message = "Character not found."; 125 | } 126 | } 127 | catch (Exception ex) 128 | { 129 | response.Success = false; 130 | response.Message = ex.Message; 131 | } 132 | 133 | return response; 134 | } 135 | 136 | public async Task> AddCharacterSkill(AddCharacterSkillDto newCharacterSkill) 137 | { 138 | var response = new ServiceResponse(); 139 | try 140 | { 141 | var character = await _context.Characters 142 | .Include(c => c.Weapon) 143 | .Include(c => c.Skills) 144 | .FirstOrDefaultAsync(c => c.Id == newCharacterSkill.CharacterId && 145 | c.User.Id == GetUserId()); 146 | 147 | if (character == null) 148 | { 149 | response.Success = false; 150 | response.Message = "Character not found."; 151 | return response; 152 | } 153 | 154 | var skill = await _context.Skills.FirstOrDefaultAsync(s => s.Id == newCharacterSkill.SkillId); 155 | if (skill == null) 156 | { 157 | response.Success = false; 158 | response.Message = "Skill not found."; 159 | return response; 160 | } 161 | 162 | character.Skills.Add(skill); 163 | await _context.SaveChangesAsync(); 164 | response.Data = _mapper.Map(character); 165 | 166 | } 167 | catch (Exception ex) 168 | { 169 | response.Success = false; 170 | response.Message = ex.Message; 171 | } 172 | return response; 173 | } 174 | } 175 | } -------------------------------------------------------------------------------- /Services/CharacterService/ICharacterService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using dotnet_rpg.Dtos.Character; 6 | 7 | namespace dotnet_rpg.Services.CharacterService 8 | { 9 | public interface ICharacterService 10 | { 11 | Task>> GetAllCharacters(); 12 | Task> GetCharacterById(int id); 13 | Task>> AddCharacter(AddCharacterDto newCharacter); 14 | Task> UpdateCharacter(UpdateCharacterDto updatedCharacter); 15 | Task>> DeleteCharacter(int id); 16 | Task> AddCharacterSkill(AddCharacterSkillDto newCharacterSkill); 17 | } 18 | } -------------------------------------------------------------------------------- /Services/FightService/FightService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using AutoMapper; 6 | using dotnet_rpg.Data; 7 | using dotnet_rpg.Dtos.Fight; 8 | using Microsoft.EntityFrameworkCore; 9 | 10 | namespace dotnet_rpg.Services.FightService 11 | { 12 | public class FightService : IFightService 13 | { 14 | private readonly DataContext _context; 15 | private readonly IMapper _mapper; 16 | public FightService(DataContext context, IMapper mapper) 17 | { 18 | _mapper = mapper; 19 | _context = context; 20 | } 21 | 22 | public async Task> Fight(FightRequestDto request) 23 | { 24 | var response = new ServiceResponse 25 | { 26 | Data = new FightResultDto() 27 | }; 28 | 29 | try 30 | { 31 | var characters = await _context.Characters 32 | .Include(c => c.Weapon) 33 | .Include(c => c.Skills) 34 | .Where(c => request.CharacterIds.Contains(c.Id)).ToListAsync(); 35 | 36 | bool defeated = false; 37 | while (!defeated) 38 | { 39 | foreach (Character attacker in characters) 40 | { 41 | var opponents = characters.Where(c => c.Id != attacker.Id).ToList(); 42 | var opponent = opponents[new Random().Next(opponents.Count)]; 43 | 44 | int damage = 0; 45 | string attackUsed = string.Empty; 46 | 47 | bool useWeapon = new Random().Next(2) == 0; 48 | if (useWeapon) 49 | { 50 | attackUsed = attacker.Weapon.Name; 51 | damage = DoWeaponAttack(attacker, opponent); 52 | } 53 | else 54 | { 55 | var skill = attacker.Skills[new Random().Next(attacker.Skills.Count)]; 56 | attackUsed = skill.Name; 57 | damage = DoSkillAttack(attacker, opponent, skill); 58 | } 59 | 60 | response.Data.Log 61 | .Add($"{attacker.Name} attacks {opponent.Name} using {attackUsed} with {(damage >= 0 ? damage : 0)} damage."); 62 | 63 | if (opponent.HitPoints <= 0) 64 | { 65 | defeated = true; 66 | attacker.Victories++; 67 | opponent.Defeats++; 68 | response.Data.Log.Add($"{opponent.Name} has been defeated!"); 69 | response.Data.Log.Add($"{attacker.Name} wins with {attacker.HitPoints} HP left!"); 70 | break; 71 | } 72 | } 73 | } 74 | 75 | characters.ForEach(c => 76 | { 77 | c.Fights++; 78 | c.HitPoints = 100; 79 | }); 80 | 81 | await _context.SaveChangesAsync(); 82 | } 83 | catch (Exception ex) 84 | { 85 | response.Success = false; 86 | response.Message = ex.Message; 87 | } 88 | return response; 89 | } 90 | 91 | public async Task> SkillAttack(SkillAttackDto request) 92 | { 93 | var response = new ServiceResponse(); 94 | try 95 | { 96 | var attacker = await _context.Characters 97 | .Include(c => c.Skills) 98 | .FirstOrDefaultAsync(c => c.Id == request.AttackerId); 99 | var opponent = await _context.Characters 100 | .FirstOrDefaultAsync(c => c.Id == request.OpponentId); 101 | 102 | var skill = attacker.Skills.FirstOrDefault(s => s.Id == request.SkillId); 103 | if (skill == null) 104 | { 105 | response.Success = false; 106 | response.Message = $"{attacker.Name} doesn't know that skill."; 107 | return response; 108 | } 109 | 110 | int damage = DoSkillAttack(attacker, opponent, skill); 111 | 112 | if (opponent.HitPoints <= 0) 113 | response.Message = $"{opponent.Name} has been defeated!"; 114 | 115 | await _context.SaveChangesAsync(); 116 | 117 | response.Data = new AttackResultDto 118 | { 119 | Attacker = attacker.Name, 120 | Opponent = opponent.Name, 121 | AttackerHP = attacker.HitPoints, 122 | OpponentHP = opponent.HitPoints, 123 | Damage = damage 124 | }; 125 | } 126 | catch (Exception ex) 127 | { 128 | response.Success = false; 129 | response.Message = ex.Message; 130 | } 131 | return response; 132 | } 133 | 134 | private static int DoSkillAttack(Character? attacker, Character? opponent, Skill? skill) 135 | { 136 | int damage = skill.Damage + (new Random().Next(attacker.Intelligence)); 137 | damage -= new Random().Next(opponent.Defense); 138 | 139 | if (damage > 0) 140 | opponent.HitPoints -= damage; 141 | return damage; 142 | } 143 | 144 | public async Task> WeaponAttack(WeaponAttackDto request) 145 | { 146 | var response = new ServiceResponse(); 147 | try 148 | { 149 | var attacker = await _context.Characters 150 | .Include(c => c.Weapon) 151 | .FirstOrDefaultAsync(c => c.Id == request.AttackerId); 152 | var opponent = await _context.Characters 153 | .FirstOrDefaultAsync(c => c.Id == request.OpponentId); 154 | int damage = DoWeaponAttack(attacker, opponent); 155 | 156 | if (opponent.HitPoints <= 0) 157 | response.Message = $"{opponent.Name} has been defeated!"; 158 | 159 | await _context.SaveChangesAsync(); 160 | 161 | response.Data = new AttackResultDto 162 | { 163 | Attacker = attacker.Name, 164 | Opponent = opponent.Name, 165 | AttackerHP = attacker.HitPoints, 166 | OpponentHP = opponent.HitPoints, 167 | Damage = damage 168 | }; 169 | } 170 | catch (Exception ex) 171 | { 172 | response.Success = false; 173 | response.Message = ex.Message; 174 | } 175 | return response; 176 | } 177 | 178 | private static int DoWeaponAttack(Character? attacker, Character? opponent) 179 | { 180 | int damage = attacker.Weapon.Damage + (new Random().Next(attacker.Strength)); 181 | damage -= new Random().Next(opponent.Defense); 182 | 183 | if (damage > 0) 184 | opponent.HitPoints -= damage; 185 | return damage; 186 | } 187 | 188 | public async Task>> GetHighscore() 189 | { 190 | var characters = await _context.Characters 191 | .Where(c => c.Fights > 0) 192 | .OrderByDescending(c => c.Victories) 193 | .ThenBy(c => c.Defeats) 194 | .ToListAsync(); 195 | 196 | var response = new ServiceResponse> 197 | { 198 | Data = characters.Select(c => _mapper.Map(c)).ToList() 199 | }; 200 | 201 | return response; 202 | } 203 | } 204 | } -------------------------------------------------------------------------------- /Services/FightService/IFightService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using dotnet_rpg.Dtos.Fight; 6 | 7 | namespace dotnet_rpg.Services.FightService 8 | { 9 | public interface IFightService 10 | { 11 | Task> WeaponAttack(WeaponAttackDto request); 12 | Task> SkillAttack(SkillAttackDto request); 13 | Task> Fight(FightRequestDto request); 14 | Task>> GetHighscore(); 15 | } 16 | } -------------------------------------------------------------------------------- /Services/WeaponService/IWeaponService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using dotnet_rpg.Dtos.Character; 6 | using dotnet_rpg.Dtos.Weapon; 7 | 8 | namespace dotnet_rpg.Services.WeaponService 9 | { 10 | public interface IWeaponService 11 | { 12 | Task> AddWeapon(AddWeaponDto newWeapon); 13 | } 14 | } -------------------------------------------------------------------------------- /Services/WeaponService/WeaponService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Security.Claims; 5 | using System.Threading.Tasks; 6 | using AutoMapper; 7 | using dotnet_rpg.Data; 8 | using dotnet_rpg.Dtos.Character; 9 | using dotnet_rpg.Dtos.Weapon; 10 | using Microsoft.EntityFrameworkCore; 11 | 12 | namespace dotnet_rpg.Services.WeaponService 13 | { 14 | public class WeaponService : IWeaponService 15 | { 16 | private readonly DataContext _context; 17 | private readonly IHttpContextAccessor _httpContextAccessor; 18 | private readonly IMapper _mapper; 19 | 20 | public WeaponService(DataContext context, IHttpContextAccessor httpContextAccessor, IMapper mapper) 21 | { 22 | _context = context; 23 | _httpContextAccessor = httpContextAccessor; 24 | _mapper = mapper; 25 | } 26 | 27 | public async Task> AddWeapon(AddWeaponDto newWeapon) 28 | { 29 | ServiceResponse response = new ServiceResponse(); 30 | try 31 | { 32 | Character character = await _context.Characters 33 | .FirstOrDefaultAsync(c => c.Id == newWeapon.CharacterId && 34 | c.User.Id == int.Parse(_httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier))); 35 | if (character == null) 36 | { 37 | response.Success = false; 38 | response.Message = "Character not found."; 39 | return response; 40 | } 41 | 42 | Weapon weapon = new Weapon 43 | { 44 | Name = newWeapon.Name, 45 | Damage = newWeapon.Damage, 46 | Character = character 47 | }; 48 | 49 | _context.Weapons.Add(weapon); 50 | await _context.SaveChangesAsync(); 51 | response.Data = _mapper.Map(character); 52 | } 53 | catch (Exception ex) 54 | { 55 | response.Success = false; 56 | response.Message = ex.Message; 57 | } 58 | return response; 59 | } 60 | } 61 | } -------------------------------------------------------------------------------- /WeatherForecast.cs: -------------------------------------------------------------------------------- 1 | namespace dotnet_rpg; 2 | 3 | public class WeatherForecast 4 | { 5 | public DateTime Date { get; set; } 6 | 7 | public int TemperatureC { get; set; } 8 | 9 | public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); 10 | 11 | public string? Summary { get; set; } 12 | } 13 | -------------------------------------------------------------------------------- /appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "ConnectionStrings": { 3 | "DefaultConnection": "Server=localhost\\SQLEXPRESS; Database=dotnet-rpg; Trusted_Connection=true;" 4 | }, 5 | "AppSettings": { 6 | "Token": "my top secret key" 7 | }, 8 | "Logging": { 9 | "LogLevel": { 10 | "Default": "Information", 11 | "Microsoft.AspNetCore": "Warning" 12 | } 13 | }, 14 | "AllowedHosts": "*" 15 | } 16 | -------------------------------------------------------------------------------- /dotnet-rpg.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | enable 6 | enable 7 | dotnet_rpg 8 | 9 | 10 | 11 | 12 | 13 | 14 | runtime; build; native; contentfiles; analyzers; buildtransitive 15 | all 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | --------------------------------------------------------------------------------