<commit_before>using System;
using System.Collections.Generic;
using SnakeBattleNet.Core.Battlefield;
using SnakeBattleNet.Core.Common;

namespace SnakeBattleNet.Core.BattleReplay
{
    public class ReplayRecorder : IReplayRecorder
    {
        private string[,] field;
        private Size fieldSize;
        private int randomSeed;
        private Dictionary<string, int> snakes = new Dictionary<string, int>();
        private List<FieldEvent> events = new List<FieldEvent>();


        public void InitBattleField(IBattleField battleField)
        {
            fieldSize = battleField.Size;
            field = new string[battleField.Size.X, battleField.Size.Y];
            for (int y = 0; y < battleField.Size.Y; y++)
            {
                for (int x = 0; x < battleField.Size.X; x++)
                {
                    var fieldRow = battleField[x, y];
                    switch (fieldRow.FieldRowContent)
                    {
                        case FieldRowContent.Empty:
                            field[x, y] = "E";
                            break;
                        case FieldRowContent.Wall:
                            field[x, y] = "W";
                            break;
                        case FieldRowContent.Head:
                            field[x, y] = "H" + GetSnake(fieldRow.Guid);
                            break;
                        case FieldRowContent.Body:
                            field[x, y] = "B" + GetSnake(fieldRow.Guid);
                            break;
                        case FieldRowContent.Tail:
                            field[x, y] = "T" + GetSnake(fieldRow.Guid);
                            break;
                    }
                }
            }
        }

        public void InitSeed(int randomSeed)
        {
            this.randomSeed = randomSeed;
        }

        public void InitSnakes(IEnumerable<ISnake> snakes)
        {
            foreach (var snake in snakes)
                GetSnake(snake.Id);
        }

        public void AddEvent(Guid? id, int x, int y, Command command)
        {
            switch (command)
            {
                case Command.PutHead:
                    events.Add(new FieldEvent(x, y, "H" + GetSnake(id.Value)));
                    break;
                case Command.PutBody:
                    events.Add(new FieldEvent(x, y, "B" + GetSnake(id.Value)));
                    break;
                case Command.PutTail:
                    events.Add(new FieldEvent(x, y, "T" + GetSnake(id.Value)));
                    break;
                case Command.PutEmpty:
                    events.Add(new FieldEvent(x, y, "E"));
                    break;
                case Command.PutGateway:
                    events.Add(new FieldEvent(x, y, "G"));
                    break;
            }
        }

        public Dictionary<string, object> GetReplay()
        {
            var objects = new Dictionary<string, object>();
            objects.Add("snakes", snakes);
            objects.Add("field", field);
            objects.Add("fieldSize", fieldSize);
            objects.Add("randomSeed", randomSeed);
            objects.Add("events", events);
            return objects;
        }

        private int GetSnake(Guid id)
        {
            var key = id.ToString();

            if (snakes.ContainsKey(key))
                return snakes[key];

            int c = snakes.Count + 1;
            snakes.Add(key, c);

            return c;
        }
    }

    class FieldEvent
    {
        public int X { get; set; }
        public int Y { get; set; }
        public string T { get; set; }
        public FieldEvent(int x, int y, string t)
        {
            X = x;
            Y = y;
            T = t;
        }
    }
}<commit_msg>ReplayRecorder now make from long snake id's short id, for better serialization<commit_after>using System;
using System.Collections.Generic;
using System.Linq;
using SnakeBattleNet.Core.Battlefield;
using SnakeBattleNet.Core.Common;

namespace SnakeBattleNet.Core.BattleReplay
{
    public class ReplayRecorder : IReplayRecorder
    {
        private string[,] field;
        private Size fieldSize;
        private int randomSeed;
        private List<SnakeShort> snakes = new List<SnakeShort>();
        private List<FieldEvent> events = new List<FieldEvent>();


        public void InitBattleField(IBattleField battleField)
        {
            fieldSize = battleField.Size;
            field = new string[battleField.Size.X, battleField.Size.Y];
            for (int y = 0; y < battleField.Size.Y; y++)
            {
                for (int x = 0; x < battleField.Size.X; x++)
                {
                    var fieldRow = battleField[x, y];
                    switch (fieldRow.FieldRowContent)
                    {
                        case FieldRowContent.Empty:
                            field[x, y] = "E";
                            break;
                        case FieldRowContent.Wall:
                            field[x, y] = "W";
                            break;
                        case FieldRowContent.Head:
                            field[x, y] = "H" + GetSnake(fieldRow.Guid);
                            break;
                        case FieldRowContent.Body:
                            field[x, y] = "B" + GetSnake(fieldRow.Guid);
                            break;
                        case FieldRowContent.Tail:
                            field[x, y] = "T" + GetSnake(fieldRow.Guid);
                            break;
                    }
                }
            }
        }

        public void InitSeed(int randomSeed)
        {
            this.randomSeed = randomSeed;
        }

        public void InitSnakes(IEnumerable<ISnake> snakes)
        {
            foreach (var snake in snakes)
                GetSnake(snake.Id);
        }

        public void AddEvent(Guid? id, int x, int y, Command command)
        {
            switch (command)
            {
                case Command.PutHead:
                    events.Add(new FieldEvent(x, y, "H" + GetSnake(id.Value)));
                    break;
                case Command.PutBody:
                    events.Add(new FieldEvent(x, y, "B" + GetSnake(id.Value)));
                    break;
                case Command.PutTail:
                    events.Add(new FieldEvent(x, y, "T" + GetSnake(id.Value)));
                    break;
                case Command.PutEmpty:
                    events.Add(new FieldEvent(x, y, "E"));
                    break;
                case Command.PutGateway:
                    events.Add(new FieldEvent(x, y, "G"));
                    break;
            }
        }

        public Dictionary<string, object> GetReplay()
        {
            var objects = new Dictionary<string, object>();
            objects.Add("snakes", snakes);
            objects.Add("field", field);
            objects.Add("fieldSize", fieldSize);
            objects.Add("randomSeed", randomSeed);
            objects.Add("events", events);
            return objects;
        }

        private int GetSnake(Guid id)
        {
            var key = id.ToString();

            foreach (var snakeShort in this.snakes.Where(snake => snake.LongId == key))
                return snakeShort.ShortId;

            int c = snakes.Count + 1;
            snakes.Add(new SnakeShort(key, c));
            return c;
        }
    }

    class FieldEvent
    {
        public int X { get; set; }
        public int Y { get; set; }
        public string T { get; set; }
        public FieldEvent(int x, int y, string t)
        {
            X = x;
            Y = y;
            T = t;
        }
    }

    class SnakeShort
    {
        public string LongId { get; set; }
        public int ShortId { get; set; }

        public SnakeShort(string longId, int shortId)
        {
            LongId = longId;
            ShortId = shortId;
        }
    }
}
{'fraction_non_alphanumeric': 0.09523809523809523, 'fraction_numerical': 0.0007499062617172854, 'mean_word_length': 1.1983516483516483, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3827409', 'n_tokens_mistral': 2156, 'n_tokens_neox': 2068, 'n_words': 506}