public void SearchBattle() { var opt = new ParallelOptions(); opt.MaxDegreeOfParallelism = 4; Parallel.ForEach(Players.Where(p => !p.isBattle).OrderBy(p=>GB.Random.NextDouble()).ToList(), opt, () => new List<Battle>(), (player, pls, battles) => { if (Battles.Find(p => p != null && p.CanJoin(player.Udemae) && p.Join(player)) == null) { if (battles.Find(p => p != null && p.CanJoin(player.Udemae) && p.Join(player)) == null) { var battle = new Battle(player.Udemae); lock (this) { battles.Add(battle); battle.Join(player); } } } return battles; } , (battles) => { lock (this) { Battles.AddRange(battles); } } ); }
public void LeaveBattle(Battle battle) { isBattle = false; }
public void JoinBattle(Battle battle) { isBattle = true; }