async Task Specific(CommandContext commandContext) { var topic = await _slackApiService.GetChannelTopic(commandContext.ChannelName); var train = Train.Parse(topic); var specificCarriageNumber = int.Parse(commandContext.Arguments); if (specificCarriageNumber >= train.Carriages.Count) { await _slackApiService.PostMessage(commandContext.ChannelName, $"{commandContext.UserName}: The carriage you asked for doesn't exist (it's zero-based, remember?)."); return; } var rider = train.Carriages[specificCarriageNumber].Riders.FirstOrDefault(r => r.Name == commandContext.UserName); if (rider == null) { await _slackApiService.PostMessage(commandContext.ChannelName, $"{commandContext.UserName}: You aren't on that carriage"); return; } if (specificCarriageNumber == 0 && train.Phase != Phase.Rollcall) { await _slackApiService.PostMessage(commandContext.ChannelName, $"{commandContext.UserName}: You can't get off the train after its left the station."); return; } _carriageManipulationService.RemoveRiderFromCarriage(train, rider, train.Carriages[specificCarriageNumber]); await _slackApiService.SetChannelTopic(commandContext.ChannelName, train.ToString()); }
public async Task HandleMessage(CommandContext commandContext) { var topic = await _slackApiService.GetChannelTopic(commandContext.ChannelName); var train = Train.Parse(topic); var targetUser = await _slackApiService.GetUserFromId(commandContext.Arguments); var riders = train.Carriages .SelectMany(c => c.Riders, (c, rider) => new { Rider = rider, Carriage = c }) .Where(r => r.Rider.Name == targetUser) .ToList(); if (riders.Count != 1) { if (riders.Count == 0) { await _slackApiService.PostMessage(commandContext.ChannelName, $"{commandContext.UserName}: Can't kick {targetUser}, they don't seem to be on the train"); } else { await _slackApiService.PostMessage(commandContext.ChannelName, $"{commandContext.UserName}: Can't kick {targetUser}, they're on multiple carriages, which one did you mean? Remember it's zero-based."); } return; } var rider = riders.First(); _carriageManipulationService.RemoveRiderFromCarriage(train, rider.Rider, rider.Carriage); await _slackApiService.SetChannelTopic(commandContext.ChannelName, train.ToString()); }