예제 #1
0
        public LobbyViewModel(GTTcpClient client, PlayerListViewModel playerList)
        {
            SelectedGameStage = GameStage.First;
            _playerList       = playerList;
            IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());

            HostIp            = ipHostInfo.AddressList.Where(ip => ip.AddressFamily == AddressFamily.InterNetwork).First().ToString();
            ConnectInProgress = false;
            Port    = DefaultPort;
            Ip      = HostIp;
            _client = client;
            _client.BuildingBegun      += Client_BuildingBegun;
            _playerList.LostConnection += PlayerList_LostConnection;

            HostCommand = new DelegateCommand(param => Server == null, param =>
            {
                try
                {
                    Server = new GTTcpListener(Port, SelectedGameStage);
                    Task.Factory.StartNew(() => Server.Start(), TaskCreationOptions.RunContinuationsAsynchronously | TaskCreationOptions.LongRunning);
                    Ip = "127.0.0.1";
                    ConnectCommand.Execute(null);
                }
                catch (SocketException)
                {
                    Error = "Az adott port már használatban van!";
                }
            });

            ConnectCommand = new DelegateCommand(param => !ConnectInProgress && !IsConnected, param => Connect());

            ReadyCommand = new DelegateCommand(param =>
            {
                try
                {
                    _client.ToggleReady(ServerStage.Lobby);
                }
                catch (Exception e)
                {
                    Error = $"Hiba a szerverrel való kommunikáció közben:\n{e.Message}";
                }
            });

            BackToMenuCommand = new DelegateCommand(param =>
            {
                UnsubscribeFromEvents();
                BackToMenu?.Invoke(this, Server != null);
            });

            StartBuildingCommand = new DelegateCommand(param => Server != null && !Server.NotReadyPlayers.Any() && _client.PlayerInfos.Count > 1,
                                                       param =>
            {
                Task.Factory.StartNew(() => Server.StartBuildStage(), TaskCreationOptions.LongRunning);
            });
        }
예제 #2
0
 public PlayerListViewModel(GTTcpClient client)
 {
     _lock                           = new object();
     ConnectedPlayers                = new ObservableCollection <PlayerInfoViewModel>();
     _client                         = client;
     _client.PlayerConnected        += Client_PlayerConnected;
     _client.PlayerReadied          += Client_PlayerReadied;
     _client.ThisPlayerReadied      += Client_ThisPlayerReadied;
     _client.PlayerDisconnected     += Client_PlayerDisconnected;
     _client.ThisPlayerDisconnected += Client_ThisPlayerDisconnected;
 }
예제 #3
0
        private void Menu_JoinGame(object sender, EventArgs e)
        {
            _client = new GTTcpClient();
            _playerListViewModel = new PlayerListViewModel(_client);
            _playerListViewModel.LostConnection += PlayerListViewModel_LostConnection;
            _lobbyViewModel                  = new LobbyViewModel(_client, _playerListViewModel);
            _lobbyViewModel.BackToMenu      += LobbyViewModel_BackToMenu;
            _lobbyViewModel.BuildingStarted += LobbyViewModel_BuildingStarted;
            ConnectControl connectControl = new ConnectControl
            {
                DataContext = _lobbyViewModel
            };

            _mainWindow.Content = connectControl;
        }
예제 #4
0
        public FlightViewModel(GTTcpClient client, PlayerListViewModel playerList, Ship ship)
        {
            ShipCash           = 0;
            _shipPartsLock     = new object();
            _playerOrderLock   = new object();
            _optionsLock       = new object();
            _popupLock         = new object();
            _client            = client;
            _playerList        = playerList;
            _ship              = ship;
            _isWaiting         = false;
            _gameOver          = false;
            CurrentAttributes  = new PlayerAttributes();
            OptionsOrSubEvents = new ObservableCollection <OptionOrSubEventViewModel>();
            ShipParts          = new ObservableCollection <FlightPartViewModel>();
            PopupMessages      = new ObservableCollection <string>();

            foreach (Part p in _ship.Parts)
            {
                ShipParts.Add(new FlightPartViewModel(p));
            }
            foreach (FlightPartViewModel part in ShipParts)
            {
                part.PartClickCommand = new DelegateCommand(param =>
                {
                    if (!_client.Crashed && part.Highlighted && !_gameOver)
                    {
                        _ship.ActivatePartAt(part.Row, part.Column);
                        foreach (FlightPartViewModel item in ShipParts)
                        {
                            if (item.Highlighted)
                            {
                                item.Part.Highlight();
                            }
                        }
                    }
                });
            }

            _client.CardPicked         += Client_CardPicked;
            _client.PlayerCrashed      += Client_PlayerCrashed;
            _client.PlayerTargeted     += Client_PlayerTargeted;
            _client.OtherTargeted      += Client_OtherTargeted;
            _client.CardOver           += Client_CardOver;
            _client.OptionRemoved      += Client_OptionRemoved;
            _client.OptionPicked       += Client_OptionPicked;
            _client.FlightEnded        += Client_FlightEnded;
            _client.GameEnded          += Client_GameEnded;
            _playerList.LostConnection += PlayerList_LostConnection;
            _ship.PartRemoved          += Ship_PartRemoved;
            _ship.Wrecked += Ship_Wrecked;
            _ship.FlightAttributesChanged += Ship_FlightAttributesChanged;
            _ship.CashChanged             += Ship_CashChanged;
            _client.PlacesChanged         += (sender, e) => RefreshTokens();

            SendAttributesCommand = new DelegateCommand(param => !_client.Crashed && RequiresAttributes && !_gameOver, param =>
            {
                RequiresAttributes = false;
                _client.UpdateAttributes(_ship.Firepower, _ship.Enginepower, _ship.CrewCount, _ship.StorageSize, _ship.Batteries);
            });

            CrashCommand = new DelegateCommand(param => !_client.Crashed && !_gameOver, param =>
            {
                MessageBoxResult result = MessageBox.Show("Biztos ki akarsz szállni?",
                                                          "Megerősítés",
                                                          MessageBoxButton.YesNo,
                                                          MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                {
                    _client.CrashPlayer();
                }
            });

            ActivatePartCommand = new DelegateCommand(param => !_client.Crashed && _ship.Batteries > 0 && !_gameOver, param =>
            {
                bool any = _ship.HighlightActivatables();
                if (!any)
                {
                    AddPopUpMessage("Nincs aktiválható elem!");
                }
            });

            ReadyCommand = new DelegateCommand(param => !_client.Crashed && _roundResolved && _client.Card.IsResolved() && !_gameOver && !_isWaiting, param =>
            {
                _client.ToggleReady(ServerStage.Flight);
                _roundResolved = false;
                _ship.ResetActivatables();
            });

            ContinueCommand = new DelegateCommand(param => !_client.Crashed && _isWaiting && !_gameOver, param =>
            {
                _isWaiting    = false;
                StatusMessage = "";
                _client.Card.ProceedCurrent();
            });

            Ship_FlightAttributesChanged(null, null);
            InitializeOrderFields();
        }
예제 #5
0
        public BuildViewModel(GTTcpClient client, PlayerListViewModel playerList, ShipLayout layout)
        {
            _playerList = playerList;
            _playerList.SynchronizeListWithClient();
            _playerList.LostConnection += PlayerList_LostConnection;

            _lastPickResolved      = true;
            _currentAlien          = Personnel.None;
            BuildingEnded          = false;
            _pickablePartsLock     = new object();
            _client                = client;
            _client.PartPicked    += Client_PartPicked;
            _client.PartPutBack   += Client_PartPutBack;
            _client.PartTaken     += Client_PartTaken;
            _client.BuildingEnded += Client_BuildingEnded;
            _client.FlightBegun   += Client_FlightBegun;

            ToggleReadyContent = "Építkezés befejezése";
            _ship     = new Ship(layout, _client.Player);
            ShipParts = new ObservableCollection <BuildPartViewModel>();

            _ship.PartRemoved += Ship_PartRemoved;

            for (int i = 0; i < 11; ++i)
            {
                for (int j = 0; j < 11; ++j)
                {
                    ShipParts.Add(new BuildPartViewModel
                    {
                        ShipRow      = i,
                        ShipColumn   = j,
                        IsValidField = _ship.IsValidField(i, j)
                    });
                }
            }

            foreach (BuildPartViewModel item in ShipParts)
            {
                item.PartClickCommand = new DelegateCommand(param => !_client.IsReady, param => ShipPartClick(param as BuildPartViewModel));
            }

            Part cockpit = _ship.GetCockpit();
            BuildPartViewModel cockpitViewModel = ShipParts.First(p => p.ShipRow == cockpit.Row && p.ShipColumn == cockpit.Column);

            cockpitViewModel.Part             = cockpit;
            cockpitViewModel.PartImage        = PartBuilder.GetPartImage(cockpit);
            cockpitViewModel.PartClickCommand = new DelegateCommand(param => false, param => { });

            PickableParts = new ObservableCollection <BuildPartViewModel>();
            for (int i = 0; i < 10; ++i)
            {
                for (int j = 0; j < 14; ++j)
                {
                    PickableParts.Add(new BuildPartViewModel
                    {
                        BuildRow     = i,
                        BuildColumn  = j,
                        IsValidField = true
                    });
                }
            }

            foreach (BuildPartViewModel item in PickableParts)
            {
                item.PartClickCommand = new DelegateCommand(param => !_client.IsReady && !_buildingEnded, param =>
                {
                    if (!_lastPickResolved)
                    {
                        return;
                    }
                    _lastPickResolved = false;
                    if (SelectedPart != null)
                    {
                        PutBackSelected();
                    }
                    BuildPartViewModel pickedPart = param as BuildPartViewModel;
                    _client.PickPart(pickedPart.BuildRow, pickedPart.BuildColumn);
                    SelectedPart = pickedPart;
                });
            }

            PutBackSelectedCommand = new DelegateCommand(param => SelectedPart != null && !_client.IsReady, param => PutBackSelected());

            RotateSelectedCommand = new DelegateCommand(param => SelectedPart != null && !_client.IsReady && !(SelectedPart.Part is Engine), param => RotateSelected(int.Parse(param as string)));

            ToggleReadyCommand = new DelegateCommand(param => !_buildingEnded || !_client.IsReady, param =>
            {
                try
                {
                    if (SelectedPart != null)
                    {
                        PutBackSelected();
                    }
                    if (!_client.IsReady && _buildingEnded)
                    {
                        _ship.FillCabins();
                        _client.StartFlightStage(_ship.Firepower, _ship.Enginepower, _ship.CrewCount, _ship.StorageSize, _ship.Batteries);
                        return;
                    }

                    _client.ToggleReady(ServerStage.Build);
                    ToggleReadyContent = (_client.IsReady, _buildingEnded) switch
                    {
                        (true, _) => "Mégsem kész",
                        (false, true) => "Tovább a repülésre",
                        (false, false) => "Építkezés befejezése"
                    };
                }