Skip to content

Commit

Permalink
Выкачена работа с котом в мешке и исправление багов при неверном ответе
Browse files Browse the repository at this point in the history
  • Loading branch information
Fooxboy committed Nov 4, 2023
1 parent ad86a66 commit deab803
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 24 deletions.
5 changes: 5 additions & 0 deletions MyOwnGame.Backend/Domain/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ public int ChangeStateToQuestion(int countQuestions)

return seconds;
}

public void ChangeStateToQuestion()
{
State = SessionState.Question;
}

public void ChangeStateToAnswer()
{
Expand Down
7 changes: 6 additions & 1 deletion MyOwnGame.Backend/Helpers/SessionEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,10 @@ public enum SessionEvents
/// <summary>
/// Пользователь выбирает кому передать вопрос
/// </summary>
PlayerChoosesQuestionPlayer
PlayerChoosesQuestionPlayer,

/// <summary>
/// Вопрос перенаправлен ебать
/// </summary>
QuestionForwarded
}
4 changes: 2 additions & 2 deletions MyOwnGame.Backend/Hubs/SessionHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,11 @@ public async Task SetQuestionPrice(int price)
}
}

public async Task ForwardQuestion()
public async Task ForwardQuestion(int playerId)
{
try
{

await _sessionService.ForwardQuestion(Context.ConnectionId, playerId);
}
catch (Exception ex)
{
Expand Down
6 changes: 6 additions & 0 deletions MyOwnGame.Backend/Services/SessionCallbackService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,10 @@ public async Task NeedForwardQuestion(string connectionId, long sessionId)
await _hubContext.Clients.Client(connectionId).SendAsync(SessionEvents.NeedForwardQuestion.ToString());
await _hubContext.Clients.Group(sessionId.ToString()).SendAsync(SessionEvents.PlayerChoosesQuestionPlayer.ToString());
}

public async Task QuestionForwarded(long sessionId, Player forwardedPlayer)
{
await _hubContext.Clients.Group(sessionId.ToString()).SendAsync(SessionEvents.QuestionForwarded.ToString(), PlayerDto.Create(forwardedPlayer));

}
}
124 changes: 103 additions & 21 deletions MyOwnGame.Backend/Services/SessionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using MyOwnGame.Backend.Models.Answers;
using MyOwnGame.Backend.Models.Questions;
using MyOwnGame.Backend.Models.QuestionsAdditionalInfo;
using MyOwnGame.Backend.Models.SiqPackage;
using MyOwnGame.Backend.Parsers;

namespace MyOwnGame.Backend.Services;
Expand Down Expand Up @@ -360,6 +361,8 @@ public async Task GetQuestionInfo(int themeNumber, int priceNumber, string conne
{
throw new Exception("Не найден текущий пользователь што");
}

session.ResetAuctionPrice();

session.SelectCurrentQuestion(questionInfo);

Expand Down Expand Up @@ -426,7 +429,7 @@ public async Task GiveAnswer(DateTime time, string connectionId)
throw new Exception("Не найден игрок лол");
}

if (session.State != SessionState.Question || session.ReadyToAnswerTime > DateTime.UtcNow || session.RespondingPlayer is not null)
if (session.State != SessionState.Question || session.RespondingPlayer is not null)
{
await _callbackService.PlayerTriedAnswer(player.SessionId, player);
return;
Expand All @@ -450,7 +453,8 @@ public async Task AcceptAnswer(string connectionId)
if (!answerData.Session.CurrentRound.IsFinal)
{
if (answerData.QuestionInfo.QuestionPackInfo != null &&
answerData.QuestionInfo.QuestionPackInfo.Type == QuestionPackType.Auction)
answerData.QuestionInfo.QuestionPackInfo.Type == QuestionPackType.Auction
|| answerData.QuestionInfo.QuestionPackInfo.Type == QuestionPackType.Cat)
{
var price = session.AuctionPrices.FirstOrDefault(x => x.Player.Id == player.Id);

Expand Down Expand Up @@ -488,7 +492,6 @@ public async Task RejectAnswer(string connectionId)

if (!answerData.Session.CurrentRound.IsFinal)
{

if (answerData.QuestionInfo.QuestionPackInfo != null &&
answerData.QuestionInfo.QuestionPackInfo.Type == QuestionPackType.Auction)
{
Expand All @@ -502,7 +505,7 @@ public async Task RejectAnswer(string connectionId)

session.ResetRespondingPlayer();

session.ChangeStateToAnswer();
session.ChangeStateToQuestion();

await _callbackService.RejectAnswer(player.SessionId, player, player.Score);
}
Expand Down Expand Up @@ -788,34 +791,78 @@ public async Task SetQuestionPrice(int price, string userConnectionId)
{
throw new Exception("Не найдена сессия в которой находится игрок");
}

session.AddAuctionPrice(player, price);

var playersWithoutInstallPrices = session.Players.Where(p => !session.AuctionPrices!.Exists(x => x.Player.Id == p.Id) && !p.IsAdmin);
if (session.CurrentQuestion.QuestionPackInfo.Type == QuestionPackType.Auction)
{
session.AddAuctionPrice(player, price);

var nextPlayer = playersWithoutInstallPrices.FirstOrDefault();
var playersWithoutInstallPrices = session.Players.Where(p => !session.AuctionPrices!.Exists(x => x.Player.Id == p.Id) && !p.IsAdmin);

if (nextPlayer is null)
{
var questionPlayer = session.AuctionPrices!.MaxBy(price => price.Price).Player;
var nextPlayer = playersWithoutInstallPrices.FirstOrDefault();

if (nextPlayer is null)
{
var questionPlayer = session.AuctionPrices!.MaxBy(price => price.Price).Player;

session.SetSelectQuestionPlayer(questionPlayer);
session.SetSelectQuestionPlayer(questionPlayer);

await _callbackService.ChangeSelectQuestionPlayer(questionPlayer.SessionId, questionPlayer);
await _callbackService.ChangeSelectQuestionPlayer(questionPlayer.SessionId, questionPlayer);

var questionInfo = session.CurrentQuestion;
var questionInfo = session.CurrentQuestion;

session.CurrentRound.Themes[questionInfo.ThemeNumber].Prices[questionInfo.PriceNumber].IsAnswered = true;
session.CurrentRound.Themes[questionInfo.ThemeNumber].Prices[questionInfo.PriceNumber].IsAnswered = true;

await _callbackService.QuestionSelected(questionPlayer.SessionId, questionInfo.Questions,
questionInfo.QuestionPackInfo, -1, questionInfo.ThemeNumber, questionInfo.PriceNumber);
await _callbackService.QuestionSelected(questionPlayer.SessionId, questionInfo.Questions,
questionInfo.QuestionPackInfo, -1, questionInfo.ThemeNumber, questionInfo.PriceNumber);

return;
return;
}

await _callbackService.NeedSetQuestionPrice(nextPlayer.SessionId, nextPlayer, 0, nextPlayer.Score, 1);

}else if (session.CurrentQuestion.QuestionPackInfo.Type == QuestionPackType.Cat)
{
session.AddAuctionPrice(player, price);

await _callbackService.QuestionSelected(player.SessionId, session.CurrentQuestion.Questions,
session.CurrentQuestion.QuestionPackInfo, -1, session.CurrentQuestion.ThemeNumber, session.CurrentQuestion.PriceNumber);
}


await _callbackService.QuestionPriceInstalled(player.SessionId, player, price);
}

await _callbackService.NeedSetQuestionPrice(nextPlayer.SessionId, nextPlayer, 0, nextPlayer.Score, 1);
private (int Min, int Max, int Step) GetPricesByQuestion(QuestionInfo question, Session session)
{
var max = 0;
var min = 0;
var step = 0;

switch (question.QuestionPackInfo.CatInfo.PriceType)
{
case QuestionPackPriceType.Fixed:
max = question.QuestionPackInfo.CatInfo.FixedPrice.Value;
min = question.QuestionPackInfo.CatInfo.FixedPrice.Value;
step = 0;
break;
case QuestionPackPriceType.Select:
max = question.QuestionPackInfo.CatInfo.SelectPrice.To;
min = question.QuestionPackInfo.CatInfo.SelectPrice.From;
step = 1;
break;
case QuestionPackPriceType.MaxOrMin:
max = session.CurrentRound.Themes[question.ThemeNumber].Prices.Max(x => x.Price);
min = session.CurrentRound.Themes[question.ThemeNumber].Prices.Min(x => x.Price);
step = 0;
break;
case QuestionPackPriceType.SelectWithStep:
max = question.QuestionPackInfo.CatInfo.SelectPriceWithStep.To;
min = question.QuestionPackInfo.CatInfo.SelectPriceWithStep.From;
step = question.QuestionPackInfo.CatInfo.SelectPriceWithStep.Step.Value;
break;
}

return (min, max, step);
}

public async Task ForwardQuestion(string connectionId, int playerId)
Expand All @@ -841,10 +888,45 @@ public async Task ForwardQuestion(string connectionId, int playerId)
throw new Exception("Не найден игрок, которому передают вопрос");
}


session.ChangeStateToAnswer();

session.ChangeRespondingPlayer(forwardPlayer);

var question = session.CurrentQuestion;

await _callbackService.PlayerAnswer(forwardPlayer.SessionId, player);
var max = 0;
var min = 0;
var step = 0;

switch (question.QuestionPackInfo.CatInfo.PriceType)
{
case QuestionPackPriceType.Fixed:
max = question.QuestionPackInfo.CatInfo.FixedPrice.Value;
min = question.QuestionPackInfo.CatInfo.FixedPrice.Value;
step = 0;
break;
case QuestionPackPriceType.Select:
max = question.QuestionPackInfo.CatInfo.SelectPrice.To;
min = question.QuestionPackInfo.CatInfo.SelectPrice.From;
step = 1;
break;
case QuestionPackPriceType.MaxOrMin:
max = session.CurrentRound.Themes[question.ThemeNumber].Prices.Max(x => x.Price);
min = session.CurrentRound.Themes[question.ThemeNumber].Prices.Min(x => x.Price);
step = 0;
break;
case QuestionPackPriceType.SelectWithStep:
max = question.QuestionPackInfo.CatInfo.SelectPriceWithStep.To;
min = question.QuestionPackInfo.CatInfo.SelectPriceWithStep.From;
step = question.QuestionPackInfo.CatInfo.SelectPriceWithStep.Step.Value;
break;
}

await _callbackService.NeedSetQuestionPrice(forwardPlayer.SessionId, forwardPlayer, min, max, step);

await _callbackService.QuestionForwarded(forwardPlayer.SessionId, forwardPlayer);

session.ChangeRespondingPlayer(forwardPlayer);
}

private (Player Player, QuestionInfo QuestionInfo, Session Session) ValidateAnswerData(string connectionId)
Expand Down

0 comments on commit deab803

Please sign in to comment.