// arranquem el queries form i el passem al Notification Worker public void LoginOk() { MessageBox.Show("Login OK!"); QueriesForm queriesForm = new QueriesForm(serverHandler, notificationWorker); notificationWorker.QueriesForm = queriesForm; queriesForm.ShowDialog(); }
//------------------------------------------------ // THREAD WORKER //------------------------------------------------ public void Start() { while (true) { // esperem a que el receiver ens indiqui ReceiverArgs.notificationSignal.WaitOne(); // agafem el tipus de missatge del Receiver int responseType = ReceiverArgs.responseType; // si hem rebut una update de dades (data grid), actualitzem el data grid if (responseType == DataGridUpdateRequested) { // Actualitzem la llista de connectats if (this.DataGridUpdateRequested == 6) { List <ConnectedUser> connectedList = ReceiverArgs.connectedList; ConnectedListDelegate gridDelegate = new ConnectedListDelegate(this.QueriesForm.UpdateConnectedList); QueriesForm.Invoke(gridDelegate, new object[] { connectedList }); connectedList = null; } // TODO: Actualitzem la taula de partides } // La resta de missatges de moment son les queries: // Mostra el temps jugat else if (responseType == 1) { string time = ReceiverArgs.responseStr; TimePlayedPopupDelegate timeDelegate = new TimePlayedPopupDelegate(this.QueriesForm.TimePlayedPopup); QueriesForm.Invoke(timeDelegate, new object[] { time }); } // TODO: la resta de queries //Mostra els personatges utilitzats en una partida else if (responseType == 3) { string CharacterList = ReceiverArgs.responseStr; CharacterListDelegate characterDelegate = new CharacterListDelegate(this.QueriesForm.UpdateCharactersList); int n_pairs = Convert.ToInt32(CharacterList.Split('/')[0]); string[] playerCharPairs = new string[n_pairs]; string[][] playerChars = new string[n_pairs][]; for (int i = 0; i < n_pairs; i++) { playerChars[i] = new string[2]; } if (n_pairs > 0) { CharacterList = CharacterList.Remove(0, CharacterList.IndexOf("/") + 1); //eliminem el n_chars de la resposta for (int i = 0; i < n_pairs; i++) { playerCharPairs = CharacterList.Split('/'); playerChars[i] = playerCharPairs[i].Split('*'); } } QueriesForm.Invoke(characterDelegate, new object[] { playerChars }); } //Mostra el ranking else if (responseType == 2) { string response = ReceiverArgs.responseStr; RankingDelegate rankingDelegate = new RankingDelegate(this.QueriesForm.UpdateRanking); int n_pairs = Convert.ToInt32(response.Split('/')[0]); string[] rankingPairs = new string[n_pairs]; string[][] ranking = new string[n_pairs][]; for (int i = 0; i < n_pairs; i++) { ranking[i] = new string[2]; } if (n_pairs > 0) { response = response.Remove(0, response.IndexOf("/") + 1); //eliminem el n_chars de la resposta for (int i = 0; i < n_pairs; i++) { rankingPairs = response.Split('/'); ranking[i] = rankingPairs[i].Split('*'); } } QueriesForm.Invoke(rankingDelegate, new object[] { ranking }); } //Crea un compte amb SignUp else if (responseType == 5) { string response = ReceiverArgs.responseStr; SignUpPopup SignUpDelegate = new SignUpPopup(this.QueriesForm.SignUpPopup); QueriesForm.Invoke(SignUpDelegate, new object[] { response }); } //Mostra la resposta a la partida creada else if (responseType == 7) { string response = ReceiverArgs.responseStr; CreatePartyPopup createPartyDelegate = new CreatePartyPopup(this.QueriesForm.CreatePartyPopup); QueriesForm.Invoke(createPartyDelegate, new object[] { response }); } //Notificació en invitacions else if (responseType == 9) { string response = ReceiverArgs.responseStr; string[] splitResponse = response.Split('/'); if (splitResponse[0] == "NOTIFY") { InvitationMessage invitationMessageDelegate = new InvitationMessage(this.QueriesForm.InvitationNotificationMessage); QueriesForm.Invoke(invitationMessageDelegate, new object[] { splitResponse[1], splitResponse[2] }); } else if (splitResponse[0] == "ACCEPTED") { InvitationMessageConfirmation invitationMessageDelegate = new InvitationMessageConfirmation(this.QueriesForm.AcceptedGamePopup); QueriesForm.Invoke(invitationMessageDelegate, new object[] { }); } else if (splitResponse[0] == "REJECTED") { InvitationMessageConfirmation invitationMessageDelegate = new InvitationMessageConfirmation(this.QueriesForm.RejectGamePopup); QueriesForm.Invoke(invitationMessageDelegate, new object[] { }); } else if (splitResponse[0] == "FAIL") { InvitationMessageConfirmation invitationMessageDelegate = new InvitationMessageConfirmation(this.QueriesForm.FailResponseGamePopup); QueriesForm.Invoke(invitationMessageDelegate, new object[] { }); } else if (splitResponse[0] == "LOST") { InvitationMessage invitationMessageDelegate = new InvitationMessage(this.QueriesForm.LoseInvitationPopup); QueriesForm.Invoke(invitationMessageDelegate, new object[] { splitResponse[1], splitResponse[2] }); } } //Notificació de l'actualització del GameStateUser else if (responseType == 10) { List <PreGameStateUser> gameState = ReceiverArgs.gameState; PreGameStateUserDelegate userStateDelegate = new PreGameStateUserDelegate(this.QueriesForm.GameStateUpdate); QueriesForm.Invoke(userStateDelegate, new object[] { gameState }); gameState = null; } //Missatges del Xat else if (responseType == 11) { string response = ReceiverArgs.responseStr; string[] splitResponse = response.Split('/'); ChatMessage ChatDelegate = new ChatMessage(this.QueriesForm.SentMessageChat); QueriesForm.Invoke(ChatDelegate, new object[] { splitResponse[0], splitResponse[1] }); } //Notificacions sobre la selecció de personatges, cancel.lar partides i l'inici d'aquestes else if (responseType == 12) { string response = ReceiverArgs.responseStr; string[] splitResponse = response.Split('/'); if (splitResponse[0] == "CHAROK") { InvitationMessageConfirmation invitationMessageDelegate = new InvitationMessageConfirmation(this.QueriesForm.AcceptCharacterPopup); QueriesForm.Invoke(invitationMessageDelegate, new object[] { }); } else if (splitResponse[0] == "CHARFAIL") { InvitationMessageConfirmation invitationMessageDelegate = new InvitationMessageConfirmation(this.QueriesForm.FailCharacterPopup); QueriesForm.Invoke(invitationMessageDelegate, new object[] { }); } else if (splitResponse[0] == "START") { InvitationMessageConfirmation invitationMessageDelegate = new InvitationMessageConfirmation(this.QueriesForm.StartGamePopup); QueriesForm.Invoke(invitationMessageDelegate, new object[] { }); } else if (splitResponse[0] == "NOTALLSELECTED") { InvitationMessageConfirmation invitationMessageDelegate = new InvitationMessageConfirmation(this.QueriesForm.NotAllSelectedPopup); QueriesForm.Invoke(invitationMessageDelegate, new object[] { }); } else if (splitResponse[0] == "ALONE") { InvitationMessageConfirmation invitationMessageDelegate = new InvitationMessageConfirmation(this.QueriesForm.AlonePlayerPopup); QueriesForm.Invoke(invitationMessageDelegate, new object[] { }); } else if (splitResponse[0] == "CANCEL") { InvitationMessage invitationMessageDelegate = new InvitationMessage(this.QueriesForm.CancelGamePopup); QueriesForm.Invoke(invitationMessageDelegate, new object[] { splitResponse[1], splitResponse[2] }); } } //Jugadors en qui ja he jugat else if (responseType == 13) { string OpponentList = ReceiverArgs.responseStr; CharacterListDelegate opponentDelegate = new CharacterListDelegate(this.QueriesForm.UpdateOpponent); int n_pairs = Convert.ToInt32(OpponentList.Split('/')[0]); string[] opponentPairs = new string[n_pairs]; string[][] opponents = new string[n_pairs][]; for (int i = 0; i < n_pairs; i++) { opponents[i] = new string[3]; } if (n_pairs > 0) { OpponentList = OpponentList.Remove(0, OpponentList.IndexOf("/") + 1); //eliminem el n_chars de la resposta for (int i = 0; i < n_pairs; i++) { opponentPairs = OpponentList.Split('/'); opponents[i] = opponentPairs[i].Split('*'); } } QueriesForm.Invoke(opponentDelegate, new object[] { opponents }); } //Resultat de partides jugades amb altres jugadors else if (responseType == 14) { string gameResultsList = ReceiverArgs.responseStr; CharacterListDelegate gameResultsDelegate = new CharacterListDelegate(this.QueriesForm.UpdateGameResultsWithOthers); int n_pairs = Convert.ToInt32(gameResultsList.Split('/')[0]); string[] gameResultsPairs = new string[n_pairs]; string[][] gameResults = new string[n_pairs][]; for (int i = 0; i < n_pairs; i++) { gameResults[i] = new string[3]; } if (n_pairs > 0) { gameResultsList = gameResultsList.Remove(0, gameResultsList.IndexOf("/") + 1); //eliminem el n_chars de la resposta for (int i = 0; i < n_pairs; i++) { gameResultsPairs = gameResultsList.Split('/'); gameResults[i] = gameResultsPairs[i].Split('*'); } } QueriesForm.Invoke(gameResultsDelegate, new object[] { gameResults }); } //Resultat de l'interval de partides else if (responseType == 15) { string gameResultsList = ReceiverArgs.responseStr; CharacterListDelegate gameResultsDelegate = new CharacterListDelegate(this.QueriesForm.UpdateGameInterval); int n_pairs = Convert.ToInt32(gameResultsList.Split('/')[0]); string[] gameResultsPairs = new string[n_pairs]; string[][] gameResults = new string[n_pairs][]; for (int i = 0; i < n_pairs; i++) { gameResults[i] = new string[3]; } if (n_pairs > 0) { gameResultsList = gameResultsList.Remove(0, gameResultsList.IndexOf("/") + 1); //eliminem el n_chars de la resposta for (int i = 0; i < n_pairs; i++) { gameResultsPairs = gameResultsList.Split('/'); gameResults[i] = gameResultsPairs[i].Split('*'); } } QueriesForm.Invoke(gameResultsDelegate, new object[] { gameResults }); } } }