public static OperationParameters Parse(string[] commandParts) { OperationParameters opParams = new OperationParameters(); DoParse(opParams, commandParts); return(opParams); }
private static void DoParse(OperationParameters opParams, string[] commandParts) { for (int k = 1; k < commandParts.Length; k++) { ParsePart(opParams, commandParts[k].ToUpperInvariant()); } }
public static OperationParameters ParseWithDefault(string[] commandParts) { OperationParameters opParams = CreateDefault(); DoParse(opParams, commandParts); return(opParams); }
private static OperationParameters CreateDefault() { DateTime now = DateTime.Now; OperationParameters opParams = new OperationParameters() { OperationCode = "GF", Day = DateHelper.DayOfWeekToDay(now.DayOfWeek), Size = 8, Mode = "SM", Time = now.IsDaylightSavingTime() ? new TimeSpan(18, 30, 0) : new TimeSpan(19, 30, 0), }; return(opParams); }
public async Task <bool> UpdateOperation(int operationId, OperationParameters opParams) { Operation op; lock (this) { op = GetOperation(operationId); if (op == null) { return(false); } if (opParams.HasOperationCode) { op.OperationName = opParams.OperationCode; } if (opParams.HasTime) { op.Date = op.Date.Date + opParams.Time; } if (opParams.HasMode) { op.Mode = opParams.Mode; } if (opParams.HasSize) { op.Size = opParams.Size; } if (opParams.HasSide) { op.Side = opParams.Side; } if (opParams.HasDay) { DateTime newDate = DateHelper.GetDateForNextOccuranceOfDay(opParams.Day); op.Date = newDate + op.Date.TimeOfDay; } } await _operationUpdated.InvokeAsync(new OperationUpdatedEventArgs(op)); return(true); }
private static void ParsePart(OperationParameters opParams, string part) { if (DateHelper.IsDayName(part)) { opParams.Day = part; } else if (part == "GF" || Operation.IsValidOperationCode(part)) { opParams.OperationCode = part; } else if (Operation.IsValidOperationMode(part)) { opParams.Mode = part; } else if (Operation.IsValidSize(part)) { opParams.Size = int.Parse(part); } else if ("IMPERIAL".StartsWith(part)) { opParams.Side = "Imperial"; } else if ("REPUBLIC".StartsWith(part)) { opParams.Side = "Republic"; } else { TimeSpan time; if (!TimeSpan.TryParse(part, out time)) { throw new OpBotInvalidValueException($"Parameter \"{part}\" does not compute."); } if (time.TotalHours > 23) { throw new OpBotInvalidValueException($"{part} is not a valid time."); } opParams.Time = time; } }
private async Task CreateCommand(MessageCreateEventArgs e, ParsedCommand cmd) { try { OperationParameters opParams = OperationParameters.ParseWithDefault(cmd.CommandParts); Operation newOperation = new Operation() { Size = opParams.Size, Mode = opParams.Mode, Date = DateHelper.GetDateForNextOccuranceOfDay(opParams.Day) + opParams.Time, Side = opParams.Side, }; newOperation.OperationName = opParams.OperationCode == "GF" ? GroupFinder.OperationOn(newOperation.Date) : opParams.OperationCode; DiscordMessage newOpMessage = await e.Channel.SendMessageAsync("Creating event..."); newOperation.MessageId = newOpMessage.Id; string text = _ops.Add(newOperation).GetOperationMessageText(); await newOpMessage.ModifyAsync(text); await PinMessage(e, newOpMessage); await _repository.SaveAsync(_ops); SendAlerts(e, newOperation); } catch (OperationException ex) { await SendError(e, ex.Message); } catch (OpBotInvalidValueException opEx) { await SendError(e, $"I don't understand part of that create command.\n\n{opEx.Message}\n\nSo meat bag, try again and get it right this time or you will be terminated as an undesirable {DiscordText.StuckOutTongue}."); } }
private async Task EditCommand(MessageCreateEventArgs e, ParsedCommand cmd) { if (cmd.CommandParts.Length == 1) { await SendError(e, "What do you want me to edit?"); return; } try { OperationParameters opParams = OperationParameters.Parse(cmd.CommandParts); bool success = await _ops.UpdateOperation(cmd.OperationId, opParams); if (!success) { await SendOperationErrorMessage(e, cmd.OperationId); } } catch (OpBotInvalidValueException ex) { await SendError(e, $"That is an invalid edit command\n\n.{ex.Message}"); } }