Ejemplo n.º 1
0
        private void Join_Button_Click(object sender, RoutedEventArgs e)
        {
            //create msg
            Dictionary <string, object> json = new Dictionary <string, object>();

            // make json
            json.Add("roomId", (int)allRooms[RoomsList.SelectedItem.ToString()].Id);
            json.Add("name", User.Username);
            string jsonString = JsonConvert.SerializeObject(json);

            byte[] arr = Helper.SerializeMsg(jsonString, 4);

            // send and get
            Communicator.SendMsg(arr, arr.Length);
            KeyValuePair <int, string> msg = Communicator.GetMsg();
            var v = JsonConvert.DeserializeObject <Dictionary <string, string> >(msg.Value);

            // get from the room list the selected room and save him

            if (v["status"] != "100")
            {
                User.UserRoom = allRooms[RoomsList.SelectedItem.ToString()];

                // close this window and open the room data window
                RoomDataWinow dataWinow = new RoomDataWinow();
                Communicator.EndCommunicate = false;
                Close();
                dataWinow.Show();
            }
            else
            {
                ErrorLabel.Content    = "room is full...";
                ErrorLabel.Visibility = Visibility.Visible;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// the user click on the create room button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CreateButton_Click(object sender, RoutedEventArgs e)
        {
            // define vars
            Dictionary <string, string> json1 = new Dictionary <string, string>();
            Dictionary <string, int>    json2 = new Dictionary <string, int>();
            bool validInput = true;
            bool num        = true;

            // make two dicts of msg, because we have strings and ints...
            // json1 is <string string>
            // json2 is <string int>

            //get inputs into json
            validInput &= Helper.AddToJson(json1, "roomName", this.RoomsInput.GetLineText(0));
            validInput &= AddToJson(json2, "questionCount", this.NumQuestionsInput.GetLineText(0), ref num);
            validInput &= AddToJson(json2, "maxUsers", this.NumPlayersInput.GetLineText(0), ref num);
            validInput &= AddToJson(json2, "answerTimeout", this.TimeInput.GetLineText(0), ref num);
            validInput &= Helper.AddToJson(json1, "username", User.Username);

            // all input is valid?
            if (validInput)
            {
                // hide error label
                this.ErrorLabel.Visibility = Visibility.Hidden;

                // create msg:
                // convert the dicts into json strings
                string jsonString1 = JsonConvert.SerializeObject(json1);
                string jsonString2 = JsonConvert.SerializeObject(json2);
                // add the two json strings
                string jsonString = Helper.AddTwoJson(jsonString1, jsonString2);
                // get byte array of json msg
                byte[] arr = Helper.SerializeMsg(jsonString, 5);

                // send and get res
                Communicator.SendMsg(arr, arr.Length);
                KeyValuePair <int, string> msg = Communicator.GetMsg();

                // error?
                if (msg.Key == 100)
                {
                    // print error msg
                    ErrorLabel.Content    = JsonConvert.DeserializeObject <Dictionary <string, string> >(msg.Value)["message"];
                    ErrorLabel.Visibility = Visibility.Visible;
                }
                else
                {
                    // get the room data
                    Dictionary <string, uint> resJson = JsonConvert.DeserializeObject <Dictionary <string, uint> >(msg.Value);

                    // make the user admin and init room
                    User.Is_admin = true;
                    User.initRoom(json1["roomName"], resJson["roomId"], (uint)json2["maxUsers"], (uint)json2["questionCount"], (uint)json2["answerTimeout"]);

                    // open room data window and close this
                    RoomDataWinow roomDataWinow = new RoomDataWinow();
                    Communicator.EndCommunicate = false;
                    Close();
                    roomDataWinow.Show();
                }
            }
            // invalid input
            else
            {
                // input of string and not int?
                if (!num)
                {
                    // show error label with this msg
                    this.ErrorLabel.Content    = "pls make sure all is in the good type";
                    this.ErrorLabel.Visibility = Visibility.Visible;
                }
                else
                {
                    // show error label with this msg
                    this.ErrorLabel.Content    = "pls fill all the filds";
                    this.ErrorLabel.Visibility = Visibility.Visible;
                }
            }
        }