Esempio n. 1
0
		public static void AddDialog(string login, Msg question, Msg[] answers, Flag flag, File[] files, long? broadcast = null)
		{
			Settings.ConnectionString.UsingConnection(conn => conn.UsingTransaction(tran =>
			{
				AddMessage(tran, question, login);
				Array.ForEach(answers, answer => AddMessage(tran, answer, login));
				AddFlag(tran, flag, login);
				AddFiles(tran, files, login);
				SetBroadcast(tran, broadcast, login);
			}));
		}
Esempio n. 2
0
		public static string GetAnswer(string question, Dictionary<string, Flag> state, out Flag flag, out File[] files, out DateTime timer)
		{
			flag = null;
			files = null;

			timer = HasBombTimer(state) ? Settings.BombTimerEnd : DateTime.MinValue;

			question = question.TrimToNull();
			if(question == null)
				return GetRandomAnswer(DefaultAnswers, string.Empty);

			var possibleFlag = question.SanitizeFlag();

			if(state.ContainsKey(possibleFlag))
				return GetRandomAnswer(OldFlagAnswers, possibleFlag);

			FlagType flagType;
			if(Flags.TryGetValue(possibleFlag, out flagType))
				flag = new Flag {Value = possibleFlag, Type = flagType};

			FlagFiles.TryGetValue(possibleFlag, out files);

			string answer;
			if(FlagAnswers.TryGetValue(possibleFlag, out answer))
				return answer + GetSuffix(flag, state, ref timer);

			if(possibleFlag.StartsWith(Settings.FlagPrefix, StringComparison.InvariantCultureIgnoreCase))
				return GetRandomAnswer(InvalidFlagAnswers, question);

			if(question.IndexOfAny(Settings.MartianChars) >= 0)
				return GetRandomAnswer(MartianAnswers, question);

			if(HintAnswers.TryGetValue(question, out answer))
				return answer;

			return GetRandomAnswer(DefaultAnswers, question);
		}
Esempio n. 3
0
		private static void AddFiles(DbTransaction tran, File[] files, string login)
		{
			if(files == null || files.Length == 0)
				return;
			Array.ForEach(files, file =>
			{
				tran.Connection.UsingCommand("insert into files ([login], [name], [ext], [url], [dt]) values (@login, @name, @ext, @url, @dt)", cmd =>
				{
					cmd.Transaction = tran;
					cmd.AddParam("login", login, DbType.String);
					cmd.AddParam("name", file.Name, DbType.String);
					cmd.AddParam("ext", file.Ext, DbType.String);
					cmd.AddParam("url", file.Url, DbType.String);
					cmd.AddParam("dt", DateTime.UtcNow, DbType.DateTime2);
					if(cmd.ExecuteNonQuery() > 0)
						Log.DebugFormat("Add file '{0}'", file.Name);
				});
			});
		}