fix: dictionnary int

This commit is contained in:
Michel Roux 2024-01-28 15:42:32 +01:00
parent d84d18ba63
commit 59d7c14af0
6 changed files with 24 additions and 30 deletions

View File

@ -8,16 +8,15 @@ public class Question
{ {
public string id; public string id;
public string promptId; public string promptId;
public Dictionary<string, Proposition> propositions; public Dictionary<int, Proposition> propositions;
public double creationDate; public double creationDate;
public Proposition GetFirstProposition() public Proposition GetFirstProposition()
{ {
return propositions["0"]; return propositions[0];
} }
public Proposition GetSecondProposition() public Proposition GetSecondProposition()
{ {
return propositions["1"]; return propositions[1];
} }
} }

View File

@ -8,7 +8,7 @@
public class Room public class Room
{ {
public string code; public string code;
public Dictionary<string, Question> questions; public Dictionary<int, Question> questions;
public Dictionary<string, Player> players; public Dictionary<string, Player> players;
public string currentQuestionId; public string currentQuestionId;
public double creationDate; public double creationDate;
@ -19,7 +19,7 @@ public Room(string _code)
code = _code; code = _code;
creationDate = DateTime.Now.ToOADate(); creationDate = DateTime.Now.ToOADate();
players = new Dictionary<string, Player>(); players = new Dictionary<string, Player>();
questions = new Dictionary<string, Question>(); questions = new Dictionary<int, Question>();
currentQuestionId = ""; currentQuestionId = "";
currentState = 1; //default by PC currentState = 1; //default by PC
} }
@ -47,28 +47,23 @@ public List<Player> GetOrderedPlayerList()
return players.Values.OrderBy(x => x.creationDate).ToList(); return players.Values.OrderBy(x => x.creationDate).ToList();
} }
public List<Question> GetQuestionList()
{
return new List<Question>(questions.Values);
}
public List<Question> GetQuestionsByPlayer(Player player) public List<Question> GetQuestionsByPlayer(Player player)
{ {
List<Question> questions = new(); List<Question> playerQuestions = new();
foreach (Question question in GetQuestionList()) foreach (Question question in new List<Question>(questions.Values))
{ {
foreach (Proposition proposition in new List<Proposition>(question.propositions.Values)) foreach (Proposition proposition in new List<Proposition>(question.propositions.Values))
{ {
if (proposition.owner.id == player.id) if (proposition.owner.id == player.id)
{ {
questions.Add(question); playerQuestions.Add(question);
break; break;
} }
} }
} }
return questions; return playerQuestions;
} }
@ -79,21 +74,21 @@ public void SetPlayersAreReady(int _state)
public List<Proposition> GetPropositionsByPlayer(Player player) public List<Proposition> GetPropositionsByPlayer(Player player)
{ {
List<Proposition> propositions = new(); List<Proposition> playerPropositions = new();
foreach (Question question in GetQuestionList()) foreach (Question question in new List<Question>(questions.Values))
{ {
foreach (Proposition proposition in new List<Proposition>(question.propositions.Values)) foreach (Proposition proposition in new List<Proposition>(question.propositions.Values))
{ {
if (proposition.owner.id == player.id) if (proposition.owner.id == player.id)
{ {
propositions.Add(proposition); playerPropositions.Add(proposition);
break; break;
} }
} }
} }
return propositions; return playerPropositions;
} }
} }

View File

@ -32,9 +32,9 @@ void Redraw(int currentQuestion)
cameraManager.WebcamResume(); cameraManager.WebcamResume();
} }
string GetPropRef(string playerId) int GetPropRef(Player player)
{ {
return gameManager.myRoom.questions[(currentQuestion - 1).ToString()].propositions.First(x => x.Value.owner.id == playerId).Key; return gameManager.myRoom.questions[currentQuestion - 1].propositions.First(x => x.Value.owner.id == player.id).Key;
} }
public void OnSubmitButton() public void OnSubmitButton()
@ -44,7 +44,7 @@ public void OnSubmitButton()
gameManager.myRoom.code, gameManager.myRoom.code,
gameManager.currentPlayer.id, gameManager.currentPlayer.id,
currentQuestion, currentQuestion,
GetPropRef(gameManager.currentPlayer.id)); GetPropRef(gameManager.currentPlayer));
if (currentQuestion < 2) { if (currentQuestion < 2) {
Redraw(++currentQuestion); Redraw(++currentQuestion);

View File

@ -287,22 +287,22 @@ public void GeneratePrompts()
System.Random rnd = new(); System.Random rnd = new();
List<Prompt> prompts = promptList.prompts.OrderBy(x => rnd.Next()).Take(myRoom.players.Count()).ToList(); List<Prompt> prompts = promptList.prompts.OrderBy(x => rnd.Next()).Take(myRoom.players.Count()).ToList();
List<Player> players = myRoom.players.Values.ToList().OrderBy(x => rnd.Next()).ToList(); List<Player> players = myRoom.players.Values.ToList().OrderBy(x => rnd.Next()).ToList();
Dictionary<string, Question> questions = new(); Dictionary<int, Question> questions = new();
for (int i = 0; i < players.Count(); i++) for (int i = 0; i < players.Count(); i++)
{ {
Dictionary<string, Proposition> propositions = new(); Dictionary<int, Proposition> propositions = new();
for (int j = 0; j < 2; j++) for (int j = 0; j < 2; j++)
{ {
propositions.Add(j.ToString(), new Proposition() propositions.Add(j, new Proposition()
{ {
owner = players[i + j < players.Count() ? i + j : 0], owner = players[i + j < players.Count() ? i + j : 0],
creationDate = DateTime.Now.ToOADate() creationDate = DateTime.Now.ToOADate()
}); });
} }
questions.Add(i.ToString(), new Question() questions.Add(i, new Question()
{ {
id = Guid.NewGuid().ToString(), id = Guid.NewGuid().ToString(),
promptId = prompts[i].id, promptId = prompts[i].id,

View File

@ -33,7 +33,7 @@ void Update()
} }
public void UploadPhoto(string roomCode, string playerId, int question, string proposition) public void UploadPhoto(string roomCode, string playerId, int question, int proposition)
{ {
Texture2D photo = gameObject.GetComponent<CameraManager>().GetPhoto(); Texture2D photo = gameObject.GetComponent<CameraManager>().GetPhoto();
byte[] photoBytes = ImageConversion.EncodeToJPG(photo); byte[] photoBytes = ImageConversion.EncodeToJPG(photo);
@ -58,7 +58,7 @@ public void UploadPhoto(string roomCode, string playerId, int question, string p
.Child("questions") .Child("questions")
.Child(question.ToString()) .Child(question.ToString())
.Child("propositions") .Child("propositions")
.Child(proposition) .Child(proposition.ToString())
.Child("photoUrl") .Child("photoUrl")
.SetValueAsync(imageRef.Path); .SetValueAsync(imageRef.Path);
} }

View File

@ -58,7 +58,7 @@ private void Update()
// Start is called before the first frame update // Start is called before the first frame update
public void ShowVotingPage(DatabaseReference _roomRef, Dictionary<string, Player> _currentPlayers, Dictionary<string, Question> _questions, Action _callback_OnVoteEnded) public void ShowVotingPage(DatabaseReference _roomRef, Dictionary<string, Player> _currentPlayers, Dictionary<int, Question> _questions, Action _callback_OnVoteEnded)
{ {
Debug.Log("Initializing voting page"); Debug.Log("Initializing voting page");
this.gameObject.SetActive(true); this.gameObject.SetActive(true);
@ -66,7 +66,7 @@ public void ShowVotingPage(DatabaseReference _roomRef, Dictionary<string, Player
currentPlayers = _currentPlayers; currentPlayers = _currentPlayers;
OnVoteEnded = _callback_OnVoteEnded; OnVoteEnded = _callback_OnVoteEnded;
foreach (var QuestionByID in _questions) foreach (Dictionary<int, Question> QuestionByID in _questions)
{ {
remainingQuestions.Enqueue(QuestionByID.Value); remainingQuestions.Enqueue(QuestionByID.Value);
roomRef.Child("questions").Child(QuestionByID.Key).ValueChanged += OnQuestionChanged; roomRef.Child("questions").Child(QuestionByID.Key).ValueChanged += OnQuestionChanged;