fix: the correct questions should be sent to the correct players
This commit is contained in:
parent
7e4416824e
commit
8c825b0d0c
@ -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();
|
||||||
|
|
||||||
|
@ -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>();
|
||||||
|
if (storageManager != null)
|
||||||
|
{
|
||||||
|
storageManager.UploadPhoto(
|
||||||
gameManager.myRoom.code,
|
gameManager.myRoom.code,
|
||||||
gameManager.currentPlayer.id,
|
gameManager.currentPlayer.id,
|
||||||
currentQuestion,
|
questionsToAnswer[activeAnsweredQuestionIndex].index,
|
||||||
GetPropRef(gameManager.currentPlayer),
|
GetPropositionIndex(gameManager.currentPlayer.id),
|
||||||
succeed =>
|
succeed =>
|
||||||
{
|
{
|
||||||
if (!succeed)
|
if (!succeed)
|
||||||
{
|
{
|
||||||
Debug.LogError("Photo upload failed. please do something", this);
|
Debug.LogError("Photo upload failed. Please do something.", this);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currentQuestion == 1)
|
if (activeAnsweredQuestionIndex == SecondQuestionIndex)
|
||||||
{
|
{
|
||||||
gameManager.WaitForPlayers();
|
gameManager.WaitForPlayers();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
submitButton.interactable = true;
|
submitButton.interactable = true;
|
||||||
submitButton.GetComponent<TextMeshProUGUI>().text = "Submit";
|
submitButton.GetComponent<TextMeshProUGUI>().text = "Submit";
|
||||||
currentQuestion++;
|
activeAnsweredQuestionIndex = SecondQuestionIndex;
|
||||||
Redraw(currentQuestion);
|
Redraw(activeAnsweredQuestionIndex);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user