Ejemplo n.º 1
0
        private void addGameExecute()
        {
            Game newGame = Game.CreateNewGame(White, Black, Winner);

            GameAdded?.Invoke(newGame);
            White = null;
            Black = null;
        }
Ejemplo n.º 2
0
        public void AddMatchAndSaveDatabase(bool addMatch, MatchStatistics ms)
        {
            // Skip adding stats if the game only had one player, make exception for co-op since it doesn't recognize pre-placed houses as players.
            if (ms.GetPlayerCount() <= 1 && !ms.MapIsCoop)
            {
                Logger.Log("Skipping adding match to statistics because game only had one player.");
                return;
            }

            if (ms.LengthInSeconds < 60)
            {
                Logger.Log("Skipping adding match to statistics because the game was cancelled.");
                return;
            }

            if (addMatch)
            {
                Statistics.Add(ms);
                GameAdded?.Invoke(this, EventArgs.Empty);
            }

            if (!File.Exists(ProgramConstants.GamePath + SCORE_FILE_PATH))
            {
                CreateDummyFile();
            }

            Logger.Log("Writing game info to statistics file.");

            using (FileStream fs = File.Open(ProgramConstants.GamePath + SCORE_FILE_PATH, FileMode.Open, FileAccess.ReadWrite))
            {
                fs.Position = 4; // First 4 bytes after the version mean the amount of games
                fs.WriteInt(Statistics.Count);

                fs.Position = fs.Length;
                ms.Write(fs);
            }

            Logger.Log("Finished writing statistics.");
        }
        public void AddMatchAndSaveDatabase(bool addMatch, MatchStatistics ms)
        {
            if (ms.LengthInSeconds < 60)
            {
                Logger.Log("Skipping adding match to statistics because the game was cancelled.");
                return;
            }

            if (addMatch)
            {
                Statistics.Add(ms);
                GameAdded?.Invoke(this, EventArgs.Empty);
            }

            if (!File.Exists(gamePath + SCORE_FILE_PATH))
            {
                CreateDummyFile();
            }

            Logger.Log("Writing game info to statistics file.");

            using (FileStream fs = File.Open(gamePath + SCORE_FILE_PATH, FileMode.Open, FileAccess.ReadWrite))
            {
                fs.Position = 4; // First 4 bytes after the version mean the amount of games
                byte[] writeBuffer = BitConverter.GetBytes(Statistics.Count);
                fs.Write(writeBuffer, 0, 4);

                fs.Position = fs.Length;

                // Game length
                writeBuffer = BitConverter.GetBytes(ms.LengthInSeconds);
                fs.Write(writeBuffer, 0, 4);
                // Game version, 8 bytes, ASCII
                writeBuffer = Encoding.ASCII.GetBytes(ms.GameVersion);
                if (writeBuffer.Length != 8)
                {
                    // If the game version's byte representation is not 8 bytes,
                    // let's resize the array
                    byte[] temp = writeBuffer;
                    writeBuffer = new byte[8];
                    for (int i = 0; i < temp.Length && i < writeBuffer.Length; i++)
                    {
                        writeBuffer[i] = temp[i];
                    }
                }
                fs.Write(writeBuffer, 0, 8);
                // Date and time, 8 bytes
                writeBuffer = BitConverter.GetBytes(ms.DateAndTime.ToBinary());
                fs.Write(writeBuffer, 0, 8);
                // SawCompletion, 1 byte
                fs.WriteByte(Convert.ToByte(ms.SawCompletion));
                // Number of players, 1 byte
                fs.WriteByte(Convert.ToByte(ms.GetPlayerCount()));
                // Average FPS, 4 bytes
                fs.Write(BitConverter.GetBytes(ms.AverageFPS), 0, 4);
                // Map name, 128 bytes (64 chars), Unicode
                writeBuffer = Encoding.Unicode.GetBytes(ms.MapName);
                if (writeBuffer.Length != 128)
                {
                    // If the map name's byte representation is shorter than 128 bytes,
                    // let's resize the array
                    byte[] temp = writeBuffer;
                    writeBuffer = new byte[128];
                    if (temp.Length < 129)
                    {
                        for (int i = 0; i < temp.Length; i++)
                        {
                            writeBuffer[i] = temp[i];
                        }
                    }
                    else
                    {
                        for (int i = 0; i < 128; i++)
                        {
                            writeBuffer[i] = temp[i];
                        }
                    }
                }
                fs.Write(writeBuffer, 0, 128);

                // Game mode, 64 bytes (32 chars), Unicode
                writeBuffer = Encoding.Unicode.GetBytes(ms.GameMode);
                if (writeBuffer.Length != 64)
                {
                    // If the game mode's byte representation is shorter than 64 bytes,
                    // let's resize the array
                    byte[] temp = writeBuffer;
                    writeBuffer = new byte[64];
                    for (int i = 0; i < temp.Length; i++)
                    {
                        writeBuffer[i] = temp[i];
                    }
                }
                fs.Write(writeBuffer, 0, 64);

                // Unique game ID, 4 bytes
                fs.Write(BitConverter.GetBytes(ms.GameID), 0, 4);

                // Write player info
                for (int i = 0; i < ms.GetPlayerCount(); i++)
                {
                    PlayerStatistics ps = ms.GetPlayer(i);
                    // 1 byte for economy
                    fs.WriteByte(Convert.ToByte(ps.Economy));
                    // 1 byte for IsAI
                    fs.WriteByte(Convert.ToByte(ps.IsAI));
                    // 1 byte for IsLocalPlayer
                    fs.WriteByte(Convert.ToByte(ps.IsLocalPlayer));
                    // 4 bytes for kills
                    fs.Write(BitConverter.GetBytes(ps.Kills), 0, 4);
                    // 4 bytes for losses
                    fs.Write(BitConverter.GetBytes(ps.Losses), 0, 4);
                    // Name takes 32 bytes
                    writeBuffer = Encoding.Unicode.GetBytes(ps.Name);
                    if (writeBuffer.Length != 32)
                    {
                        // If the name's byte presentation is shorter than 32 bytes,
                        // let's resize the array
                        byte[] temp = writeBuffer;
                        writeBuffer = new byte[32];
                        for (int j = 0; j < temp.Length; j++)
                        {
                            writeBuffer[j] = temp[j];
                        }
                    }
                    fs.Write(writeBuffer, 0, 32);
                    // 1 byte for SawEnd
                    fs.WriteByte(Convert.ToByte(ps.SawEnd));
                    // 4 bytes for Score
                    fs.Write(BitConverter.GetBytes(ps.Score), 0, 4);
                    // 1 byte for Side
                    fs.WriteByte(Convert.ToByte(ps.Side));
                    // 1 byte for Team
                    fs.WriteByte(Convert.ToByte(ps.Team));
                    // 1 byte color Color
                    fs.WriteByte(Convert.ToByte(ps.Color));
                    // 1 byte for WasSpectator
                    fs.WriteByte(Convert.ToByte(ps.WasSpectator));
                    // 1 byte for Won
                    fs.WriteByte(Convert.ToByte(ps.Won));
                    // 1 byte for AI level
                    fs.WriteByte(Convert.ToByte(ps.AILevel));
                }
            }

            Logger.Log("Finished writing statistics.");
        }
Ejemplo n.º 4
0
 public void AddGame(Game game)
 {
     games.Add(game);
     GameAdded?.Invoke(this, new GameEventArgs(game));
 }