Skip to content

Commit 193163d

Browse files
authored
Merge pull request #37 from Atralupus/feat/battle-controller
Implement dummy battle controller
2 parents 075cd5b + 2a82614 commit 193163d

File tree

8 files changed

+244
-9
lines changed

8 files changed

+244
-9
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
using System.Security.Claims;
2+
using ArenaService.Controllers;
3+
using ArenaService.Dtos;
4+
using ArenaService.Models;
5+
using ArenaService.Repositories;
6+
using ArenaService.Services;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.AspNetCore.Http.HttpResults;
9+
using Microsoft.AspNetCore.Mvc;
10+
using Moq;
11+
12+
namespace ArenaService.Tests.Controllers;
13+
14+
public class BattleControllerTests
15+
{
16+
private readonly BattleController _controller;
17+
private Mock<IAvailableOpponentRepository> _availableOpponentRepositoryMock;
18+
private AvailableOpponentService _availableOpponentService;
19+
private Mock<IParticipantRepository> _participantRepositoryMock;
20+
private Mock<IBattleLogRepository> _battleLogRepositoryMock;
21+
private ParticipantService _participantService;
22+
23+
public BattleControllerTests()
24+
{
25+
var availableOpponentRepositoryMock = new Mock<IAvailableOpponentRepository>();
26+
_availableOpponentRepositoryMock = availableOpponentRepositoryMock;
27+
_availableOpponentService = new AvailableOpponentService(
28+
_availableOpponentRepositoryMock.Object
29+
);
30+
var participantRepositoryMock = new Mock<IParticipantRepository>();
31+
_participantRepositoryMock = participantRepositoryMock;
32+
_participantService = new ParticipantService(_participantRepositoryMock.Object);
33+
var battleLogRepositoryMock = new Mock<IBattleLogRepository>();
34+
_battleLogRepositoryMock = battleLogRepositoryMock;
35+
_controller = new BattleController(
36+
_availableOpponentService,
37+
_participantService,
38+
_battleLogRepositoryMock.Object
39+
);
40+
}
41+
42+
[Fact]
43+
public async Task GetBattleToken_WithValidHeader_ReturnsOk()
44+
{
45+
var avatarAddress = "DDF1472fD5a79B8F46C28e7643eDEF045e36BD3d";
46+
47+
_participantRepositoryMock
48+
.Setup(repo => repo.GetParticipantByAvatarAddressAsync(1, avatarAddress))
49+
.ReturnsAsync(
50+
new Participant
51+
{
52+
Id = 1,
53+
AvatarAddress = avatarAddress,
54+
NameWithHash = "test",
55+
PortraitId = 1
56+
}
57+
);
58+
59+
_availableOpponentRepositoryMock
60+
.Setup(repo => repo.GetAvailableOpponents(1))
61+
.ReturnsAsync(
62+
[
63+
new AvailableOpponent
64+
{
65+
Id = 1,
66+
ParticipantId = 1,
67+
OpponentId = 2,
68+
Opponent = new Participant
69+
{
70+
Id = 2,
71+
AvatarAddress = "test",
72+
NameWithHash = "opponent1",
73+
PortraitId = 1
74+
},
75+
RefillBlockIndex = 1
76+
},
77+
new AvailableOpponent
78+
{
79+
Id = 2,
80+
ParticipantId = 1,
81+
OpponentId = 3,
82+
Opponent = new Participant
83+
{
84+
Id = 3,
85+
AvatarAddress = "test",
86+
NameWithHash = "opponent2",
87+
PortraitId = 1
88+
},
89+
RefillBlockIndex = 1
90+
}
91+
]
92+
);
93+
94+
_battleLogRepositoryMock
95+
.Setup(repo => repo.AddBattleLogAsync(1, 1, 1, "token"))
96+
.ReturnsAsync(
97+
new BattleLog
98+
{
99+
Id = 1,
100+
ParticipantId = 1,
101+
OpponentId = 1,
102+
SeasonId = 1,
103+
Token = "token"
104+
}
105+
);
106+
107+
_controller.ControllerContext = new ControllerContext
108+
{
109+
HttpContext = new DefaultHttpContext()
110+
};
111+
_controller.ControllerContext.HttpContext.User = new ClaimsPrincipal(
112+
new ClaimsIdentity([new Claim("avatar", avatarAddress)])
113+
);
114+
115+
var result = await _controller.CreateBattleToken(1, 1);
116+
117+
var okResult = Assert.IsType<Ok<string>>(result.Result);
118+
}
119+
}

ArenaService/Controllers/AvailableOpponentController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public async Task<
6262
);
6363
}
6464

65-
[HttpGet]
65+
[HttpPost]
6666
public async Task<Results<UnauthorizedHttpResult, NotFound<string>, Created>> ResetOpponents(
6767
int seasonId
6868
)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
namespace ArenaService.Controllers;
2+
3+
using System.Security.Claims;
4+
using ArenaService.Dtos;
5+
using ArenaService.Extensions;
6+
using ArenaService.Repositories;
7+
using ArenaService.Services;
8+
using Microsoft.AspNetCore.Http.HttpResults;
9+
using Microsoft.AspNetCore.Mvc;
10+
11+
[Route("seasons/{seasonId}/opponent/{opponentId}/battle")]
12+
[ApiController]
13+
public class BattleController : ControllerBase
14+
{
15+
private readonly AvailableOpponentService _availableOpponentService;
16+
private readonly ParticipantService _participantService;
17+
private readonly IBattleLogRepository _battleLogRepository;
18+
19+
public BattleController(
20+
AvailableOpponentService availableOpponentService,
21+
ParticipantService participantService,
22+
IBattleLogRepository battleLogRepository
23+
)
24+
{
25+
_availableOpponentService = availableOpponentService;
26+
_participantService = participantService;
27+
_battleLogRepository = battleLogRepository;
28+
}
29+
30+
private string? ExtractAvatarAddress()
31+
{
32+
if (HttpContext.User.Identity is ClaimsIdentity identity)
33+
{
34+
var claim = identity.FindFirst("avatar");
35+
return claim?.Value;
36+
}
37+
return null;
38+
}
39+
40+
[HttpPost]
41+
public async Task<
42+
Results<UnauthorizedHttpResult, NotFound<string>, Ok<string>>
43+
> CreateBattleToken(int seasonId, int opponentId)
44+
{
45+
var avatarAddress = ExtractAvatarAddress();
46+
47+
if (avatarAddress is null)
48+
{
49+
return TypedResults.Unauthorized();
50+
}
51+
52+
var participant = await _participantService.GetParticipantByAvatarAddressAsync(
53+
seasonId,
54+
avatarAddress
55+
);
56+
57+
if (participant is null)
58+
{
59+
return TypedResults.NotFound("Not participant user.");
60+
}
61+
62+
var opponents = await _availableOpponentService.GetAvailableOpponents(participant.Id);
63+
var battleLog = await _battleLogRepository.AddBattleLogAsync(
64+
participant.Id,
65+
opponentId,
66+
seasonId,
67+
"token"
68+
);
69+
70+
return TypedResults.Ok(battleLog.Token);
71+
}
72+
}

ArenaService/Models/BattleLog.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ public class BattleLog
1010

1111
[Required]
1212
public int ParticipantId { get; set; }
13-
public required Participant Participant { get; set; }
13+
public Participant Participant { get; set; } = null!;
1414

1515
[Required]
1616
public int OpponentId { get; set; }
17-
public required Participant Opponent { get; set; }
17+
public Participant Opponent { get; set; } = null!;
1818

1919
[Required]
2020
public int SeasonId { get; set; }
21-
public required Season Season { get; set; }
22-
23-
public long BattleBlockIndex { get; set; }
21+
public Season Season { get; set; } = null!;
2422

2523
[Required]
26-
public bool IsVictory { get; set; }
24+
public required string Token { get; set; }
2725

28-
public int ParticipantScoreChange { get; set; }
29-
public int OpponentScoreChange { get; set; }
26+
public bool? IsVictory { get; set; }
27+
public int? ParticipantScoreChange { get; set; }
28+
public int? OpponentScoreChange { get; set; }
29+
public long? BattleBlockIndex { get; set; }
3030
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
namespace ArenaService.Repositories;
2+
3+
using ArenaService.Data;
4+
using ArenaService.Models;
5+
6+
public interface IBattleLogRepository
7+
{
8+
Task<BattleLog> AddBattleLogAsync(
9+
int participantId,
10+
int opponentId,
11+
int seasonId,
12+
string token
13+
);
14+
}
15+
16+
public class BattleLogRepository : IBattleLogRepository
17+
{
18+
private readonly ArenaDbContext _context;
19+
20+
public BattleLogRepository(ArenaDbContext context)
21+
{
22+
_context = context;
23+
}
24+
25+
public async Task<BattleLog> AddBattleLogAsync(
26+
int participantId,
27+
int opponentId,
28+
int seasonId,
29+
string token
30+
)
31+
{
32+
var battleLog = await _context.BattleLogs.AddAsync(
33+
new BattleLog
34+
{
35+
ParticipantId = participantId,
36+
OpponentId = opponentId,
37+
SeasonId = seasonId,
38+
Token = token
39+
}
40+
);
41+
_context.SaveChanges();
42+
return battleLog.Entity;
43+
}
44+
}

0 commit comments

Comments
 (0)