Beispiel #1
0
 private void StartActualisation()
 {
     Task.Run(() =>
     {
         using (var channelFactory = new ChannelFactory <IBattleshipsService>("MyNetNamedPipeEndpoint"))
         {
             IBattleshipsService client = channelFactory.CreateChannel();
             while (true)
             {
                 try
                 {
                     GameInfo.Dispatcher.Invoke(() =>
                     {
                         GameInfo.Content = client.GetGameState(gameId, playerId);
                     });
                     updateMyBoard();
                 }
                 catch (EndpointNotFoundException)
                 {
                 }
                 Thread.Sleep(100);
             }
         }
     });
 }
Beispiel #2
0
 private void myBoardClick(int x, int y)
 {
     //place ships
     if (toPlace != null && !AllBoatsPlaced)
     {
         int  size;
         char orientation;
         GetBoatParameters(toPlace, out size, out orientation);
         using (var channelFactory = new ChannelFactory <IBattleshipsService>("MyNetNamedPipeEndpoint"))
         {
             IBattleshipsService client = channelFactory.CreateChannel();
             if (client.TryPutBoat(gameId, playerId, x, y, size, orientation))
             {
                 Rectangle friend = AvailableBoats.Children.Cast <Rectangle>()
                                    .Where(r => r.Width == toPlace.Height && r.Height == toPlace.Width && r.Visibility == Visibility.Visible)
                                    .First();
                 toPlace.Visibility = Visibility.Hidden;
                 friend.Visibility  = Visibility.Hidden;
                 toPlace            = null;
             }
             //all ships placed
             if (AllBoatsPlaced)
             {
                 client.PlayerReady(gameId, playerId);
                 opponentsBoard.Visibility = Visibility.Visible;
                 AvailableBoats.Visibility = Visibility.Hidden;
             }
         }
         updateMyBoard();
     }
 }
Beispiel #3
0
 private void FindGame()
 {
     using (var channelFactory = new ChannelFactory <IBattleshipsService>("MyNetNamedPipeEndpoint"))
     {
         IBattleshipsService client = channelFactory.CreateChannel();
         Game foundGame             = client.FindGame(me);
         MainMenuUc.Visibility = Visibility.Hidden;
         GameUc.StartGame(foundGame.GameId, me.PlayerId);
     }
 }
Beispiel #4
0
 internal void LeaveGame()
 {
     using (var channelFactory = new ChannelFactory <IBattleshipsService>("MyNetNamedPipeEndpoint"))
     {
         IBattleshipsService client = channelFactory.CreateChannel();
         client.LeaveGame(gameId, playerId);
     }
     this.Visibility = Visibility.Hidden;
     GameLeft();
 }
Beispiel #5
0
 private void OpponentBoardClick(int x, int y)
 {
     using (var channelFactory = new ChannelFactory <IBattleshipsService>("MyNetNamedPipeEndpoint"))
     {
         IBattleshipsService client     = channelFactory.CreateChannel();
         BlockState?         turnResult = client.MakeTurn(gameId, playerId, x, y);
         if (turnResult != null)
         {
             opponentsBoard.SetBlock(x, y, (BlockState)turnResult);
         }
     }
 }
Beispiel #6
0
 private void updateMyBoard()
 {
     using (var channelFactory = new ChannelFactory <IBattleshipsService>("MyNetNamedPipeEndpoint"))
     {
         IBattleshipsService client = channelFactory.CreateChannel();
         for (int x = 0; x < 10; x++)
         {
             for (int y = 0; y < 10; y++)
             {
                 //my board update
                 myBoard.SetBlock(x, y, client.GetMyBoardBlock(gameId, playerId, x, y));
             }
         }
     }
 }
Beispiel #7
0
        public void RegisterClick()
        {
            using (var channelFactory = new ChannelFactory <IBattleshipsService>("MyNetNamedPipeEndpoint"))
            {
                IBattleshipsService client = channelFactory.CreateChannel();
                player = client.Register(TbName.Text, TbPassword.Text);
            }

            if (player == null)
            {
                Info.Content = $"Player with name {TbName.Text} is already registered";
            }
            else
            {
                Close();
            }
        }
Beispiel #8
0
        public void LoginClick()
        {
            using (var channelFactory = new ChannelFactory <IBattleshipsService>("MyNetNamedPipeEndpoint"))
            {
                IBattleshipsService client = channelFactory.CreateChannel();
                player = client.Login(TbName.Text, TbPassword.Text);
            }

            if (player == null)
            {
                Info.Content = "Invalid name or password";
            }
            else
            {
                Close();
            }
        }
Beispiel #9
0
        public StatsModel(long myId)
        {
            Players = new List <PlayerModel>();
            using (var channelFactory = new ChannelFactory <IBattleshipsService>("MyNetNamedPipeEndpoint"))
            {
                IBattleshipsService client = channelFactory.CreateChannel();
                foreach (StatsContract pc in client.GetStats())
                {
                    Players.Add(new PlayerModel()
                    {
                        Id    = pc.Player.PlayerId,
                        Name  = pc.Player.Name,
                        Wins  = pc.Wins,
                        Loses = pc.Loses
                    });
                }
            }
            Players = Players.OrderByDescending(p => p.Loses == 0 ? p.Wins : (double)p.Wins / p.Loses).ToList();
            foreach (PlayerModel pm in Players)
            {
                pm.Rank = Players.IndexOf(pm) + 1;
            }
            Me = Players.Find(p => p.Id == myId);

            //init games
            Games = new List <GameModel>();
            using (var channelFactory = new ChannelFactory <IBattleshipsService>("MyNetNamedPipeEndpoint"))
            {
                IBattleshipsService client = channelFactory.CreateChannel();
                foreach (GameResult gc in client.GetMyGames(myId))
                {
                    Games.Add(new GameModel()
                    {
                        GameId   = gc.GameId,
                        End      = gc.End,
                        Start    = gc.Start,
                        Opponent = gc.Opponent,
                        Result   = gc.Result
                    });
                }
            }
        }
Beispiel #10
0
        private void JoinGame()
        {
            int gameId;

            if (!int.TryParse(Interaction.InputBox("Join Game", "Set game id number value"), out gameId))
            {
                MessageBox.Show("Game id must be a number...");
                return;
            }
            using (var channelFactory = new ChannelFactory <IBattleshipsService>("MyNetNamedPipeEndpoint"))
            {
                IBattleshipsService client = channelFactory.CreateChannel();
                Game foundGame             = client.JoinGame(me, gameId);
                if (foundGame == null)
                {
                    MessageBox.Show($"Game with id {gameId} not found");
                    return;
                }
                MainMenuUc.Visibility = Visibility.Hidden;
                GameUc.StartGame(foundGame.GameId, me.PlayerId);
            }
        }
Beispiel #11
0
 public void Run(long gameId)
 {
     Player1Board.Clear();
     Player2Board.Clear();
     moves.Clear();
     player1 = null;
     player2 = null;
     using (var channelFactory = new ChannelFactory <IBattleshipsService>("MyNetNamedPipeEndpoint"))
     {
         IBattleshipsService client = channelFactory.CreateChannel();
         GameReplay          gr     = client.GetGameReplay(gameId);
         if (gr == null)
         {
             return;
         }
         player1 = gr.Player1;
         player2 = gr.Player2;
         moves   = gr.Moves.OrderBy(m => m.Time).ToList();
     }
     updateButtons();
     Player1Button.MainText = $"Player {player1.Name} turn...";
     Player2Button.MainText = $"Player {player2.Name} turn...";
     Visibility             = Visibility.Visible;
 }
 public BattleshipsController(IBattleshipsService battleshipsService)
 {
     this.battleshipsService = battleshipsService;
 }