예제 #1
0
        private void initAndCreateGameRoom()
        {
            gameRoom gr = new gameRoom();

            //Initialize "7" Required Game Room Attributes.
            gr.gameState = GAME_STATE.Wait;
            gr.host_ID   = globalVars.player.id;
            gr.host_IP   = globalVars.userIp;
            if (LevelBox.SelectedIndex == -1)
            {
                gr.level = 1;                               //default = 1
            }
            else
            {
                gr.level = LevelBox.SelectedIndex + 1;
            }
            if ((numOfPlayer_txt.Text).Length == 0)
            {
                gr.maxPlayers = 2;                                     //default = 2
            }
            else
            {
                gr.maxPlayers = int.Parse(numOfPlayer_txt.Text);
            }
            if ((RoomName_txt.Text).Length == 0)
            {
                gr.name = "Unnamed";                                  //default = "Unnamed"
            }
            else
            {
                gr.name = RoomName_txt.Text;
            }
            if ((numOfPlays_txt.Text).Length == 0)
            {
                gr.numOfPlays = 3;                                    //default
            }
            else
            {
                gr.numOfPlays = int.Parse(numOfPlays_txt.Text);
            }
            //Add The Room To DB
            try
            {
                int roomId_or_Zero = globalVars.proxy.openRoom(gr);
                if (roomId_or_Zero != 0)
                {
                    globalVars.roomId = roomId_or_Zero;
                }
                else
                {
                    throw new Exception("Cannot Open The Game Room :(");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }
예제 #2
0
        private void RoomList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (RoomList.SelectedIndex == -1)
            {
                return;
            }

            //사용자가 방 클릭 시 서버에 메시지 보냄
            char[] sendMessage = new char[128];
//방번호 받기용
            string createRoomNum = ((sender as ListBox).SelectedItem as string);

            //SelectedItem as string);
            if (createRoomNum == String.Empty)
            {
                return;
            }
            sendMessage[0]  = 'A';
            sendMessage[1]  = 'M';
            sendMessage[2]  = '_';
            sendMessage[3]  = 'E';
            sendMessage[4]  = 'N';
            sendMessage[5]  = 'T';
            sendMessage[6]  = 'E';
            sendMessage[7]  = 'R';
            sendMessage[8]  = '_';
            sendMessage[9]  = 'R';
            sendMessage[10] = 'O';
            sendMessage[11] = 'O';
            sendMessage[12] = 'M';
            sendMessage[13] = '#';
            sendMessage[21] = createRoomNum[0];
            sendMessage[22] = '#';

            CheckNoChat = true;
            Socket.SendMessage(sendMessage);



            while (message[3] != 'E' && message[4] != 'N' && message[5] != 'T')
            {
                /*
                 * for (; ; )
                 * {
                 * if (message[3] == 'E' && message[4] == 'N' && message[5] == 'T')
                 * {
                 *  break;
                 * }
                 * if (Socket.hasMessage())
                 * {
                 *  message = Socket.GetMessage();
                 *  if (message[3] == 'E' && message[4] == 'N' && message[5] == 'T')
                 *      break;
                 * }
                 * }
                 */
                if (message[21] == '0')
                {
                    return;
                }
            }
            Login.SetLobby(this);
            StopSound();
            Exit = true;
            gameRoom room = new gameRoom(Socket, Login);

            m_hThread.Join();
            //this.Kill_Thread();
            room.SetInfoData(InfoData);
            room.Owner    = Login;
            room.TopLevel = false;
            //Login.Visible = true;
            Login.Controls.Add(room);
            Login.IsMdiContainer = true;
            room.Show();
            room.GoThread();
            this.Close();
        }
예제 #3
0
 private async void Connect_btn_Click(object sender, RoutedEventArgs e)
 {
     await Task.Run(() =>
     {
         disableGraphicsAndWait();
         bool invalidRoomID = false;
         //Validiate Room ID
         this.Dispatcher.Invoke((Action)(() =>
         {
             try
             {
                 globalVars.roomId = Convert.ToInt32(RoomId_txt.Text);
             }
             catch (Exception)
             {
                 invalidRoomID = true;
                 globalVars.roomId = 0;
                 MessageBox.Show("Invalid Room ID!", "Invalid Info.",
                                 MessageBoxButton.OK, MessageBoxImage.Error);
                 resetGraphics();
             }
         }));
         if (invalidRoomID)
         {
             return;                //Don't Make Progress just return
         }
         //Get The Room Data By ID
         gameRoom gr = new gameRoom(globalVars.roomId);
         if (gr.id == 0) //Room Not Found!
         {
             globalVars.roomId = 0;
             MessageBox.Show("Room Not Found!", "Invalid Info.",
                             MessageBoxButton.OK, MessageBoxImage.Error);
             resetGraphics();
             return;
         }
         else
         {
             //Trying To Establish a connection to the host:
             try
             {
                 string MemoryGameServiceHostUri = "http://" + gr.host_IP + ":8888/MemoryGameService/MemoryGameService/";
                 EndpointAddress MemoryGameServiceEndpointAddress = new EndpointAddress(MemoryGameServiceHostUri);
                 BasicHttpBinding _BasicHttpBinding = new BasicHttpBinding();
                 ChannelFactory <IMemoryGameService> _ChannelFactory =
                     new ChannelFactory <IMemoryGameService>(_BasicHttpBinding, MemoryGameServiceEndpointAddress);
                 globalVars.proxy = _ChannelFactory.CreateChannel();
             }
             catch (Exception ex)
             {
                 globalVars.roomId = 0;
                 MessageBox.Show(ex.Message, "Cannot Establish a Connection.",
                                 MessageBoxButton.OK, MessageBoxImage.Error);
                 resetGraphics();
                 return;
             }
             //Trying To Join The Game Room:
             bool isJoined = false;
             try
             {
                 isJoined = globalVars.proxy.joinRoom(globalVars.roomId,
                                                      globalVars.player.id, globalVars.userIp);
             }
             catch (Exception ex)
             {
                 globalVars.roomId = 0;
                 MessageBox.Show(ex.Message, "Connection Lost :(",
                                 MessageBoxButton.OK, MessageBoxImage.Error);
                 resetGraphics();
                 return;
             }
             //If Joined Successfully -> Switch To The Next Window:
             if (isJoined)
             {
                 Application.Current.Dispatcher.Invoke((Action) delegate
                 {
                     //switch to next window
                     ChatReadyWindow _ChatReadyWindow = new ChatReadyWindow();
                     App.Current.MainWindow           = _ChatReadyWindow;
                     _ChatReadyWindow.Show();
                     this.Close();
                 });
             }
             else
             {
                 globalVars.roomId = 0;
                 MessageBox.Show("Sorry, No Late Join :(", "Cannot Join This Room.",
                                 MessageBoxButton.OK, MessageBoxImage.Error);
                 resetGraphics();
                 return;
             }
         }
     });
 }