Пример #1
0
        // Add the newly-created player in the database.
        public bool saveNewPlayer(Player player)
        {
            FileStream f;
            byte[] playerAsBytes;

            if (!File.Exists(this.fullPath))
                f = File.Create(this.fullPath);
            else
                f = File.OpenWrite(this.fullPath);

            playerAsBytes = player.ToBytes();
            f.Seek(0, SeekOrigin.End);
            f.Write(playerAsBytes, 0, playerAsBytes.Length);

            return true;
        }
Пример #2
0
        // Update the changeable stats of the player objects passed in
        public bool updatePlayerRatings(Player p1, Player p2)
        {
            bool error = false;
            int recUpdated = 0;
            int currIndex = 0;
            Player currPlayer;
            byte[] source;
            byte[] playerRec;

            // Check for file existence
            if (File.Exists(this.fullPath))
            {
                source = File.ReadAllBytes(this.fullPath);

                while (!error && recUpdated < 2 && currIndex < source.Length)
                {
                    currPlayer = Player.FromBytes(source, ref currIndex);

                    if (currPlayer == null)
                        error = true;
                    else if (currPlayer.ID == p1.ID)
                    {
                        playerRec = p1.ToBytes();
                        Array.Copy(playerRec, 0, source, currIndex - playerRec.Length, playerRec.Length);
                        recUpdated++;
                    }
                    else if (currPlayer.ID == p2.ID)
                    {
                        playerRec = p2.ToBytes();
                        Array.Copy(playerRec, 0, source, currIndex - playerRec.Length, playerRec.Length);
                        recUpdated++;
                    }
                }

                if (!error && recUpdated == 2)
                    File.WriteAllBytes(this.fullPath, source);
            }

            return recUpdated == 2 && !error;
        }