fix: the correct questions should be sent to the correct players

This commit is contained in:
Fangh 2024-01-31 22:34:50 +01:00
parent 7e4416824e
commit 8c825b0d0c
3 changed files with 68 additions and 38 deletions

View File

@ -76,7 +76,12 @@ public void SetPlayersAreReady(int _state)
currentState = _state; currentState = _state;
} }
public List<Proposition> GetPropositionsByPlayer(Player player) /// <summary>
/// Return all the propositions linked to a specific player.
/// </summary>
/// <param name="player"></param>
/// <returns></returns>
public List<Proposition> GetPropositionsForPlayer(Player player)
{ {
List<Proposition> playerPropositions = new(); List<Proposition> playerPropositions = new();

View File

@ -12,69 +12,94 @@ public class QuestionHandler : MonoBehaviour
public PromptList promptList; public PromptList promptList;
private GameManager gameManager; private GameManager gameManager;
private List<Question> player2questions; private List<Question> questionsToAnswer;
private int currentQuestion = 0; private int activeAnsweredQuestionIndex = 0;
private const int FirstQuestionIndex = 0;
private const int SecondQuestionIndex = 1;
/// <summary>
/// Called when the script is enabled.
/// Initializes the GameManager and questionsToAnswer list, and updates the UI.
/// </summary>
void OnEnable() void OnEnable()
{ {
gameManager = managers.GetComponent<GameManager>(); gameManager = managers.GetComponent<GameManager>();
player2questions = gameManager.myRoom.GetQuestionsByPlayer(gameManager.currentPlayer); questionsToAnswer = gameManager.myRoom.GetQuestionsByPlayer(gameManager.currentPlayer);
Redraw(currentQuestion); Redraw(activeAnsweredQuestionIndex);
} }
/// <summary>
/// Called when the script is disabled.
/// Cleans up resources and resets the activeAnsweredQuestionIndex.
/// </summary>
void OnDisable() void OnDisable()
{ {
player2questions = null; questionsToAnswer = null;
currentQuestion = 0; activeAnsweredQuestionIndex = FirstQuestionIndex;
} }
/// <summary>
/// Redraws the UI with the information from the current question.
/// </summary>
/// <param name="currentQuestion">The index of the current question to display.</param>
void Redraw(int currentQuestion) void Redraw(int currentQuestion)
{ {
Prompt prompt = promptList.prompts.Find(x => x.id == player2questions[currentQuestion].promptId); Prompt prompt = promptList.prompts.Find(x => x.id == questionsToAnswer[currentQuestion].promptId);
//Debug.Log(JsonUtility.ToJson(prompt));
explainText.SetText(prompt.en); explainText.SetText(prompt.en);
CameraManager cameraManager = gameObject.GetComponent<CameraManager>(); CameraManager cameraManager = gameObject.GetComponent<CameraManager>();
cameraManager.WebcamResume(); cameraManager.WebcamResume();
} }
int GetPropRef(Player player) /// <summary>
/// Gets the index of the proposition owned by a player for the current question.
/// </summary>
/// <param name="_playerId">The player's ID for whom the proposition index is retrieved.</param>
/// <returns>The index of the proposition for the player.</returns>
int GetPropositionIndex(string _playerId)
{ {
return gameManager.myRoom.questions[currentQuestion].propositions.First(x => x.Value.owner.id == player.id).Key; return questionsToAnswer[activeAnsweredQuestionIndex].propositions.First(x => x.Value.owner.id == _playerId).Key;
} }
/// <summary>
/// Handles the click event of the submit button.
/// Uploads a photo and manages the UI accordingly.
/// </summary>
public void OnSubmitButton() public void OnSubmitButton()
{ {
submitButton.interactable = false; submitButton.interactable = false;
submitButton.GetComponent<TextMeshProUGUI>().text = "Uploading..."; submitButton.GetComponent<TextMeshProUGUI>().text = "Uploading...";
managers.GetComponent<StorageManager>().UploadPhoto( StorageManager storageManager = managers.GetComponent<StorageManager>();
gameManager.myRoom.code, if (storageManager != null)
gameManager.currentPlayer.id, {
currentQuestion, storageManager.UploadPhoto(
GetPropRef(gameManager.currentPlayer), gameManager.myRoom.code,
succeed => gameManager.currentPlayer.id,
{ questionsToAnswer[activeAnsweredQuestionIndex].index,
if(!succeed) GetPropositionIndex(gameManager.currentPlayer.id),
succeed =>
{ {
Debug.LogError("Photo upload failed. please do something", this); if (!succeed)
return; {
} Debug.LogError("Photo upload failed. Please do something.", this);
return;
if (currentQuestion == 1) }
{
gameManager.WaitForPlayers();
return;
}
else
{
submitButton.interactable = true;
submitButton.GetComponent<TextMeshProUGUI>().text = "Submit";
currentQuestion++;
Redraw(currentQuestion);
}
});
if (activeAnsweredQuestionIndex == SecondQuestionIndex)
{
gameManager.WaitForPlayers();
}
else
{
submitButton.interactable = true;
submitButton.GetComponent<TextMeshProUGUI>().text = "Submit";
activeAnsweredQuestionIndex = SecondQuestionIndex;
Redraw(activeAnsweredQuestionIndex);
}
});
}
} }
} }

View File

@ -81,7 +81,7 @@ private void CheckPlayersWhoHaveProposed()
continue; continue;
bool playerHasAnsweredBothProposition = true; bool playerHasAnsweredBothProposition = true;
Proposition[] propositions = myRoom.GetPropositionsByPlayer(player).ToArray(); Proposition[] propositions = myRoom.GetPropositionsForPlayer(player).ToArray();
//Debug.Log($"I found {propositions.Length} propositions for {player.name}", this); //Debug.Log($"I found {propositions.Length} propositions for {player.name}", this);
if (propositions.Length < 2) if (propositions.Length < 2)