예제 #1
0
        public async Task Prepare()
        {
            //TODO: remove this later on
            var sampleUser = new User {
                FirstName = "John", LastName = "Doe", Email = "*****@*****.**", PasswordHash = "AQAAAAEAACcQAAAAEEI1y09DRnWeVEUmJfBSYoLkYp6Ps+yQZTdxGB3PKWzX/GNs/P8BxIyqGOc/VGIEDA==", IsEmailConfirmed = true
            };
            var user = await FindUserAsync(sampleUser.Email);

            if (user == null)
            {
                user = await RegisterUserAsync(sampleUser);
            }

            var keysUser = Builders <User> .IndexKeys.Ascending("Email");

            CreateIndexModel <User> userIndex = new CreateIndexModel <User>(keysUser);
            await Users.Indexes.CreateOneAsync(userIndex);

            var keysGame = Builders <GameState> .IndexKeys.Ascending("GameId");

            CreateIndexModel <GameState> gameIndex = new CreateIndexModel <GameState>(keysGame);
            await Games.Indexes.CreateOneAsync(gameIndex);

            var statistics = await GetClientStatisticsAsync();

            if (statistics == null)
            {
                await Statistics.InsertOneAsync(ClientStatistics.CreateNew());
            }
        }
예제 #2
0
 // File => Disconnect
 private void DisconnectToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (!ClientNetworkEngine.Status)
     {
         return;
     }
     foreach (TabPage tabPage in TabControlClient.TabPages)
     {
         if (TabPagePrivateChatReceiveClientEvent != null)
         {
             TabPagePrivateChatReceiveClientEvent.Invoke(tabPage.Name, null, null, TabPagePrivateChatClient.TabCommand.Disconnected);
         }
     }
     ClientNetworkEngine.Disconnect();
     ClientStatistics.Stop();
     Thread.Sleep(50);
     BtnPubSnd.Enabled = false;
     Text = @"Chat: " + Client.Name + @"[Disconnected]";
     RichTextClientPub.SelectionStart     = _CursorPosition;
     RichTextClientPub.SelectionColor     = Color.Black;
     RichTextClientPub.SelectionBackColor = Color.Crimson;
     RichTextClientPub.SelectedText       = @"You are disonnected now " + Environment.NewLine;
     _CursorPosition = RichTextClientPub.SelectionStart;
     ListBoxClientList.Items.Clear();
     _ClientChatHistoryList.Clear();
 }
예제 #3
0
        public virtual async Task <int> FiredCannonFromServer(string gameId)
        {
            GameState g = await FindActiveGameAsync(gameId);

            if (g == null)
            {
                return(-1);
            }

            ClientStatistics stat = null;

            if (_options.UseStatistics)
            {
                stat = await _gameStatistics.GetStatisticsAsync();
            }

            int i = _gameLogic.ChooseNextClientCell(g.ClientBoard, g.CurrentShip, stat);

            g.ClientBoard.Board[i] = (int)ClientCellState.CellFiredResultNotYetKnown;

            g.IsAwaitingServerTurn = false;

            await _storage.UpdateGameAsync(g);

            return(i);
        }
예제 #4
0
        public ClientStatisticsViewModelDesignData()
        {
            ClientStatistics stats = new ClientStatistics
            {
                PieceCount = Common.Helpers.EnumHelper.GetPieces(b => b).Select(piece => new
                {
                    Key   = piece,
                    Value = 1
                }).ToDictionary(x => x.Key, x => x.Value),
                SpecialCount = Common.Helpers.EnumHelper.GetSpecials(b => b).Select(special => new
                {
                    Key   = special,
                    Value = 1
                }).ToDictionary(x => x.Key, x => x.Value),
                SpecialUsed = Common.Helpers.EnumHelper.GetSpecials(b => b).Select(special => new
                {
                    Key   = special,
                    Value = 1
                }).ToDictionary(x => x.Key, x => x.Value),
                SpecialDiscarded = Common.Helpers.EnumHelper.GetSpecials(b => b).Select(special => new
                {
                    Key   = special,
                    Value = 1
                }).ToDictionary(x => x.Key, x => x.Value)
            };

            PieceCount       = BuildStatistics(stats.PieceCount);
            SpecialCount     = BuildStatistics(stats.SpecialCount);
            SpecialUsed      = BuildStatistics(stats.SpecialUsed);
            SpecialDiscarded = BuildStatistics(stats.SpecialDiscarded);

            Specials = BuildStatistics(stats);
        }
예제 #5
0
        public async Task UpdateStatisticsAsync(Dictionary <int, int> cellUsage)
        {
            ClientStatistics stat = await _storage.GetClientStatisticsAsync();

            cellUsage.Keys.ForEach(key =>
            {
                stat.CellHits[key] += cellUsage[key];
            });

            await _storage.UpdateClientStatisticsAsync(stat);
        }
예제 #6
0
        private void FrmChat_Load(object sender, EventArgs e)
        {
            FrmLogin frmLogin = new FrmLogin();

            frmLogin.ShowDialog();
            if (frmLogin.DialogResult == DialogResult.Cancel)
            {
                Close();
            }
            // Set window name
            Text = @"Chat: " + Client.Name;
            ClientStatistics.Start();
        }
        public Task Prepare()
        {
            var user = new User {
                FirstName = "John", LastName = "Doe", Email = "*****@*****.**", PasswordHash = "AQAAAAEAACcQAAAAEEI1y09DRnWeVEUmJfBSYoLkYp6Ps+yQZTdxGB3PKWzX/GNs/P8BxIyqGOc/VGIEDA==", IsEmailConfirmed = true
            };

            _users.AddOrUpdate(user.Email, user, (key, val) => user);

            ClientStatistics stat = ClientStatistics.CreateNew();

            _clientStatistics.GetOrAdd(typeof(ClientStatistics).ToString(), stat);

            return(Task.FromResult(0));
        }
예제 #8
0
        private int?GetStatisticalCellIndex(int[] board, ClientStatistics statistics)
        {
            //TODO: Add Games Count
            var sortedStat = statistics.CellHits.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);

            for (int i = 0; i < 20; i++)
            {
                int cellIdx = sortedStat.Keys.ElementAt(i);
                if (board[cellIdx] == (int)ClientCellState.CellNotFired)
                {
                    return(cellIdx);
                }
            }
            return(null);
        }
예제 #9
0
 // Disconnect
 private void Disconnect()
 {
     ClientStatistics.Stop();
     Invoke(new Action((delegate
     {
         if (TabPagePrivateChatReceiveClientEvent != null)
         {
             TabPagePrivateChatReceiveClientEvent.Invoke(null, null, null, TabPagePrivateChatClient.TabCommand.Disconnect);
         }
         BtnPubSnd.Enabled = false;
         ListBoxClientList.Items.Clear();
         RichTextClientPub.SelectionStart = _CursorPosition;
         RichTextClientPub.SelectionColor = Color.Black;
         RichTextClientPub.SelectionBackColor = Color.Tomato;
         RichTextClientPub.SelectedText = Time.NowTime() + " Disconnected from the server" + Environment.NewLine;
         _CursorPosition = RichTextClientPub.SelectionStart;
     })));
 }
예제 #10
0
        // Clients list
        private void ClientsList(string clientsList)
        {
            Invoke(new Action((delegate
            {
                //_ClientsColor.Add(new ClientChatProp(ClientConnection.ClientName)); //Add this Client to the ClientChatProp list
                ListBoxClientList.Items.AddRange(clientsList.Split(','));
                //remove the empty selection box in list view

                ListBoxClientList.Items.RemoveAt(ListBoxClientList.Items.Count - 1);
                // Add all the connected clients to ClientChatProp list
                foreach (object t in ListBoxClientList.Items)
                {
                    _ClientChatHistoryList.Add(new ClientChatHistory(t.ToString()));
                }
                RichTextClientPub.SelectionStart = _CursorPosition;
                RichTextClientPub.SelectionColor = Color.Black;
                RichTextClientPub.SelectionBackColor = Color.GreenYellow;
                RichTextClientPub.SelectedText = @"<<< You have joined the chat successfully >>>" + Environment.NewLine;
                _CursorPosition = RichTextClientPub.SelectionStart;
                _FrmStatistics.Start();
                ClientStatistics.Start();
            })));
        }
예제 #11
0
        public virtual int ChooseNextClientCell(BoardInfo clientBoard, List <int> currentShip, ClientStatistics statistics)
        {
            //if no current ship just fire random cell
            if (currentShip.Count == 0)
            {
                //TODO: This is a try to get most frequently used cells from statistics. Comment it for now.
                //if (statistics != null)
                //{
                //    int? cellIndex = GetStatisticalCellIndex(clientBoard.Board, statistics);
                //    if (cellIndex.HasValue)
                //        return cellIndex.Value;
                //}

                //try to guess next cell from most unexpplored space
                int?cell = GetCellIndexFromLongestSpace(clientBoard.Board);
                if (cell.HasValue)
                {
                    return(cell.Value);
                }

                return(GetRandomCellIndex(clientBoard.Board));
            }
            //if only one cell of ship is marked - try to find if it is horizontal or vertical
            else if (currentShip.Count == 1)
            {
                int i = currentShip[0];

                //add some random decision wheter to try vertical attack first
                bool isVerticalTryFirst = _random.Next(100) >= 50;

                int j;
                if (isVerticalTryFirst)
                {
                    if (TryAttackVertical(clientBoard.Board, i, out j))
                    {
                        return(j);
                    }
                    if (TryAttackHorizontal(clientBoard.Board, i, out j))
                    {
                        return(j);
                    }
                }
                else
                {
                    if (TryAttackHorizontal(clientBoard.Board, i, out j))
                    {
                        return(j);
                    }
                    if (TryAttackVertical(clientBoard.Board, i, out j))
                    {
                        return(j);
                    }
                }

                throw new InvalidOperationException("Either Vertical or Horizontal attack should be possible, as if we have ship not fully destroyed, it shoud be there.");
            }
            //we have more than one cell marked in this ship, so we know if it is horizontal or vertical
            else
            {
                int j;
                currentShip.Sort();
                int i = currentShip.Last();
                int k = currentShip.Skip(currentShip.Count - 2).Take(1).First();
                if (Math.Abs(i - k) == 10) //vertical
                {
                    if (TryAttackVertical(clientBoard.Board, currentShip.Last(), out j))
                    {
                        return(j);
                    }

                    if (TryAttackVertical(clientBoard.Board, currentShip.First(), out j))
                    {
                        return(j);
                    }

                    throw new InvalidOperationException("Vertical attack should be possible. check board");
                }
                else //horizontal
                {
                    if (TryAttackHorizontal(clientBoard.Board, currentShip.Last(), out j))
                    {
                        return(j);
                    }

                    if (TryAttackHorizontal(clientBoard.Board, currentShip.First(), out j))
                    {
                        return(j);
                    }

                    throw new InvalidOperationException("Horizontal attack should be possible. check board");
                }
            }
        }
예제 #12
0
 public async Task UpdateClientStatisticsAsync(ClientStatistics statistics)
 {
     await Statistics.FindOneAndReplaceAsync(_ => true, statistics);
 }
 public Task UpdateClientStatisticsAsync(ClientStatistics statistics)
 {
     _clientStatistics.AddOrUpdate(typeof(ClientStatistics).ToString(), statistics, (key, value) => statistics);
     return(Task.FromResult(0));
 }