public ConcurrentDictionary<string, object[]> GameStates(ConcurrentDictionary<string, Player> players, ConcurrentDictionary<string, Ship> ships, List<Bullet> bullets) { ConcurrentDictionary<string, object[]> gameStates = new ConcurrentDictionary<string, object[]>(); Parallel.ForEach(players, player => { GameState gameState = new GameState(); Parallel.ForEach(ships, ship => { gameState.Ships.Add(Compress(ship.Value)); }); lock (bullets) { foreach (Bullet bullet in bullets) { object[] compressedBullet = Compress(bullet); gameState.Bullets.Add(compressedBullet); } } gameStates.TryAdd(player.Key, Compress(gameState)); }); return gameStates; }
private object[] Compress(GameState gameState) { object[] compressedGameState = new object[2]; compressedGameState[0] = gameState.Ships; compressedGameState[1] = gameState.Bullets; return compressedGameState; }