Exemple #1
0
        /// <summary>
        /// Select good player based on 
        /// 1. CompletedSessions/TotalSessions
        /// 2. Currently returns the first from the list of good players
        /// </summary>
        /// <param name="neighbors"></param>
        /// <returns></returns>
        private string selectBestPlayer(List<string> neighbors)
        {
            List<string> goodPlayers = new List<string>();
            List<string> newPlayers = new List<string>();
            double maxFraction = 0;

            string discoveredNeighborStr = "Discovered Neighbors = ";

            //Following loop selects goodPlayers (players with nonzero game-playing history).
            //and newPlayers(players present in credibilityStore but have no game history => totalSessions == 0)
            foreach (string neighbor in neighbors)
            {
                discoveredNeighborStr += neighbor + " ";

                //consider pr only if it is in credibilityDataStore
                PlayerHistoryRecord pr = credibilityDataStore.Find(delegate(PlayerHistoryRecord player)
                {
                    return neighbor.Equals(player.PlayerDevID);
                });

                if (pr != null)
                {
                    if (pr.TotalSessions != 0 && maxFraction < (double)pr.CompletedSessions / (double)pr.TotalSessions)
                    {
                        maxFraction = (double)pr.CompletedSessions / (double)pr.TotalSessions;
                        goodPlayers.Clear();
                        goodPlayers.Add(pr.PlayerDevID);
                    }
                    else if (pr.TotalSessions != 0 && maxFraction == (double)pr.CompletedSessions / (double)pr.TotalSessions)
                        goodPlayers.Add(pr.PlayerDevID);
                    else if (pr.TotalSessions == 0) //TEST THIS
                        newPlayers.Add(pr.PlayerDevID);
                }
                else //found new neighbor playing with this App. Add this by default to good players.
                {
                    PlayerHistoryRecord newPlayer = new PlayerHistoryRecord(neighbor);
                    lock (credibilityDataStore)
                    {
                        credibilityDataStore.Add(newPlayer);
                    }
                    newPlayers.Add(newPlayer.PlayerDevID);
                }
            }

            logAndDisplay(discoveredNeighborStr);

            string goodNeighborsStr = "Good Neighbors = ";
            foreach (string player in goodPlayers)
                goodNeighborsStr += player + " ";
            logAndDisplay(goodNeighborsStr);

            if (goodPlayers.Count != 0)
            {
                Random generator = new Random();
                int index = generator.Next(0, goodPlayers.Count - 1);
                return goodPlayers.ToArray()[index];
            }
            else
                if (newPlayers.Count != 0)
                {
                    Random generator = new Random();
                    int index = generator.Next(0, newPlayers.Count - 1);
                    return newPlayers.ToArray()[index];
                }

            return null;
        }
Exemple #2
0
        /// <summary>
        /// Reads Playing History of the neighbors from a file and loads into credibilityDataStore.
        /// Called at application initialization
        /// </summary>
        private void ReadCredibilityHistory()
        {
            credibilityDataStore = new List<PlayerHistoryRecord>();

            StreamReader reader = null;
            if (File.Exists(credibilityFilePath) == false)
                return;

            reader = File.OpenText(credibilityFilePath);

            lock (credibilityDataStore)
            {
                while (reader.EndOfStream == false)
                {
                    string playerID = reader.ReadLine().Trim();
                    PlayerHistoryRecord player = new PlayerHistoryRecord(playerID);

                    int completedSessions = Int32.Parse(reader.ReadLine().Trim());
                    player.CompletedSessions = completedSessions;

                    int totalSessions = Int32.Parse(reader.ReadLine().Trim());
                    player.TotalSessions = totalSessions;

                    //It is important to read sessions line, even if it is blank.
                    string[] sessions = reader.ReadLine().Trim().Split(new char[] { ' ' });

                    if (totalSessions != 0)
                    {
                        foreach (string session in sessions)
                        {
                            bool status = session.Equals("1") ? true : false;
                            player.updateHistory(status);
                        }
                    }
                    credibilityDataStore.Add(player);
                }
            }
            reader.Close();
        }