public AnimalRace(ulong serverId, ITextChannel ch) { this._log = LogManager.GetCurrentClassLogger(); this.serverId = serverId; this.raceChannel = ch; if (!AnimalRaces.TryAdd(serverId, this)) { Fail = true; return; } using (var uow = DbHandler.UnitOfWork()) { animals = new ConcurrentQueue <string>(uow.BotConfig.GetOrCreate().RaceAnimals.Select(ra => ra.Icon).Shuffle()); } var cancelSource = new CancellationTokenSource(); var token = cancelSource.Token; var fullgame = CheckForFullGameAsync(token); Task.Run(async() => { try { try { await raceChannel.SendMessageAsync($"π`Race is starting in 20 seconds or when the room is full. Type {NadekoBot.ModulePrefixes[typeof(Gambling).Name]}jr to join the race.`"); } catch (Exception ex) { _log.Warn(ex); } var t = await Task.WhenAny(Task.Delay(20000, token), fullgame); Started = true; cancelSource.Cancel(); if (t == fullgame) { try { await raceChannel.SendMessageAsync("π`Race full, starting right now!`"); } catch (Exception ex) { _log.Warn(ex); } } else if (participants.Count > 1) { try { await raceChannel.SendMessageAsync("π`Game starting with " + participants.Count + " participants.`"); } catch (Exception ex) { _log.Warn(ex); } } else { try { await raceChannel.SendMessageAsync("π`Race failed to start since there was not enough participants.`"); } catch (Exception ex) { _log.Warn(ex); } var p = participants.FirstOrDefault(); if (p != null) { await CurrencyHandler.AddCurrencyAsync(p.User, "BetRace", p.AmountBet, true).ConfigureAwait(false); } End(); return; } await Task.Run(StartRace); End(); } catch { } }); }
public AnimalRace(ulong serverId, Channel ch) { this.serverId = serverId; this.raceChannel = ch; if (!AnimalRaces.TryAdd(serverId, this)) { Fail = true; return; } var cancelSource = new CancellationTokenSource(); var token = cancelSource.Token; var fullgame = CheckForFullGameAsync(token); Task.Run(async() => { try { await raceChannel.SendMessage($"π`Race is starting in 20 seconds or when the room is full. Type {NadekoBot.Config.CommandPrefixes.Gambling}jr to join the race.`"); var t = await Task.WhenAny(Task.Delay(20000, token), fullgame); Started = true; cancelSource.Cancel(); if (t == fullgame) { await raceChannel.SendMessage("π`Race full, starting right now!`"); } else if (participants.Count > 1) { await raceChannel.SendMessage("π`Game starting with " + participants.Count + " participants.`"); } else { await raceChannel.SendMessage("π`Race failed to start since there was not enough participants.`"); var p = participants.FirstOrDefault(); if (p != null) { await FlowersHandler.AddFlowersAsync(p.User, "BetRace", p.AmountBet, true).ConfigureAwait(false); } End(); return; } await Task.Run(StartRace); End(); } catch { } }); }
public AnimalRace(ulong serverId, Channel ch) { this.serverId = serverId; this.raceChannel = ch; if (!AnimalRaces.TryAdd(serverId, this)) { Fail = true; return; } var cancelSource = new CancellationTokenSource(); var token = cancelSource.Token; var fullgame = CheckForFullGameAsync(token); Task.Run(async() => { try { await raceChannel.SendMessage($"π`Rennen startet in 20 Sekunden oder wenn der Raum voll ist. Gib {MidnightBot.Config.CommandPrefixes.Gambling}jr ein, um dem Rennen beizutreten.`"); var t = await Task.WhenAny(Task.Delay(20000, token), fullgame); Started = true; cancelSource.Cancel(); if (t == fullgame) { await raceChannel.SendMessage("π`Rennen ist voll, wird gestartet!`"); } else if (participants.Count > 1) { await raceChannel.SendMessage("π`Spiel startet mit " + participants.Count + " Teilnehmern.`"); } else { await raceChannel.SendMessage("π`Rennen konnte nicht starten da es nicht genug Teilnehmer gab.`"); var p = participants.FirstOrDefault(); if (p != null) { await FlowersHandler.AddFlowersAsync(p.User, "BetRace", p.AmountBet, true).ConfigureAwait(false); } End(); return; } await Task.Run(StartRace); End(); } catch { } }); }
private void End() { AnimalRace throwaway; AnimalRaces.TryRemove(serverId, out throwaway); }
public AnimalRace(ulong serverId, ITextChannel ch, string prefix) { _prefix = prefix; _log = LogManager.GetCurrentClassLogger(); _serverId = serverId; _raceChannel = ch; if (!AnimalRaces.TryAdd(serverId, this)) { Fail = true; return; } animals = new ConcurrentQueue <string>(NadekoBot.BotConfig.RaceAnimals.Select(ra => ra.Icon).Shuffle()); var cancelSource = new CancellationTokenSource(); var token = cancelSource.Token; var fullgame = CheckForFullGameAsync(token); Task.Run(async() => { try { try { await _raceChannel.SendConfirmAsync(GetText("animal_race"), GetText("animal_race_starting"), footer: GetText("animal_race_join_instr", _prefix)); } catch (Exception ex) { _log.Warn(ex); } var t = await Task.WhenAny(Task.Delay(20000, token), fullgame); Started = true; cancelSource.Cancel(); if (t == fullgame) { try { await _raceChannel.SendConfirmAsync(GetText("animal_race"), GetText("animal_race_full")); } catch (Exception ex) { _log.Warn(ex); } } else if (_participants.Count > 1) { try { await _raceChannel.SendConfirmAsync(GetText("animal_race"), GetText("animal_race_starting_with_x", _participants.Count)); } catch (Exception ex) { _log.Warn(ex); } } else { try { await _raceChannel.SendErrorAsync(GetText("animal_race"), GetText("animal_race_failed")); } catch (Exception ex) { _log.Warn(ex); } var p = _participants.FirstOrDefault(); if (p != null && p.AmountBet > 0) { await CurrencyHandler.AddCurrencyAsync(p.User, "BetRace", p.AmountBet, false).ConfigureAwait(false); } End(); return; } await Task.Run(StartRace); End(); } catch { try { End(); } catch { } } }); }