Ejemplo n.º 1
0
        private void GameStartButtonClicked(object sender, EventArgs e)
        {
            GameStartButton.IsEnabled = false;
            using var socket          = ConnectServer.CreateSocket((IPAddress)Application.Current.Properties["serverIpAddress"],
                                                                   (int)Application.Current.Properties["serverPort"]);
            var requestMethod = new CloseCreateMessage(_myPlayerInfo.MatchKey);
            var requestText   = JsonConvert.SerializeObject(requestMethod);

            var(_, result, msg) = Connection.SendAndRecvMessage(requestText, socket, true);

            if (!result)
            {
                var failed = JsonConvert.DeserializeObject <FailedMessage>(msg);
                throw new Exception($"CloseCreate Error: {failed.Message}");
            }

            _hostPlayersPageMoved = true;
            Device.BeginInvokeOnMainThread(async() =>
                                           await Navigation.PushAsync(new PlayPage(_myPlayerInfo, _matchInfo)));
        }
Ejemplo n.º 2
0
        private bool PlayerInfoUpdate()
        {
            using var socket = ConnectServer.CreateSocket((IPAddress)Application.Current.Properties["serverIpAddress"],
                                                          (int)Application.Current.Properties["serverPort"]);
            var requestMethod = new GetMatchInfoMessage(_myPlayerInfo.MatchKey);
            var requestText   = JsonConvert.SerializeObject(requestMethod);

            var(_, result, msg) = Connection.SendAndRecvMessage(requestText, socket, true);

            if (!result)
            {
                var failed = JsonConvert.DeserializeObject <FailedMessage>(msg);
                throw new Exception($"MatchKey Error: {failed.Message}");
            }

            if (_hostPlayersPageMoved)
            {
                return(false);
            }

            _matchInfo = JsonConvert.DeserializeObject <MatchInfo>(msg);
            if (_matchInfo.CreatePlayerClosed)
            {
                Device.BeginInvokeOnMainThread(async() =>
                                               await Navigation.PushAsync(new PlayPage(_myPlayerInfo, _matchInfo)));
                return(false);
            }

            var viewModels = _matchInfo.Players.Select(p => new WaitOtherPlayerViewModel
            {
                IdInfo      = $"{(p.PlayerID == _myPlayerInfo.PlayerID ? "自分 " : "")}ID: {p.PlayerID}",
                PlayerName  = p.PlayerName,
                ImageSource = _playerImages[p.PlayerID - 1]
            });

            PlayersView.ItemsSource = viewModels;
            return(true);
        }
Ejemplo n.º 3
0
        private async void OnStartButtonClicked(object sender, EventArgs e)
        {
            StartButton.IsEnabled = false;
            var serverIpAddress = ServerIpAddress.Text;
            var serverPort      = ServerPort.Text;
            var playerName      = PlayerNameEditor.Text;
            var roomName        = RoomNameEditor.Text;

            if (string.IsNullOrEmpty(serverIpAddress) || string.IsNullOrEmpty(serverPort) ||
                string.IsNullOrEmpty(playerName) || string.IsNullOrEmpty(roomName))
            {
                Device.BeginInvokeOnMainThread(async() =>
                                               await DisplayAlert("エラー", "サーバーのIPアドレス、ポート番号、プレイヤー名、部屋名のいずれかが不足しています", "OK"));
                return;
            }

            if (!IPAddress.TryParse(serverIpAddress, out var serverIp) || !int.TryParse(serverPort, out var port))
            {
                Device.BeginInvokeOnMainThread(async() =>
                                               await DisplayAlert("エラー", "IPアドレス、ポート番号の形式が正しくありません", "OK"));
                return;
            }

            Application.Current.Properties["serverIpAddress"] = serverIp;
            Application.Current.Properties["serverPort"]      = port;

            Player playerData = null;

            try
            {
                using var socket = ConnectServer.CreateSocket(serverIp, port);
                var createPlayerMessage = new CreatePlayerMessage(playerName, roomName);
                var msg = JsonConvert.SerializeObject(createPlayerMessage);
                var(_, result, recvMsg) = Connection.SendAndRecvMessage(msg, socket, true);

                if (!result)
                {
                    var failMessage = JsonConvert.DeserializeObject <FailedMessage>(recvMsg);
                    Device.BeginInvokeOnMainThread(
                        async() => await DisplayAlert("部屋の作成に失敗", failMessage.Message, "OK"));
                    return;
                }

                playerData = JsonConvert.DeserializeObject <Player>(recvMsg);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
                Device.BeginInvokeOnMainThread(async() =>
                                               await DisplayAlert("エラー", "ソケットのConnectで例外が発生しました。やり直してください。", "OK"));
            }
            finally
            {
                if (playerData != null)
                {
                    await Navigation.PushAsync(new WaitOtherPlayerPage(playerData));
                }

                StartButton.IsEnabled = true;
            }
        }