Light12306/Web12306/Servers/TrainSuggestion/RequestInfo.cs
2015-03-13 19:25:08 +08:00

73 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

namespace Web12306.Servers.TrainSuggestion
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
public class RequestInfo
{
public string Key { get; set; }
public string From { get; set; }
public string To { get; set; }
public DateTime Date { get; set; }
[JsonProperty("tt")]
public string[] SelectedTrain { get; set; }
[JsonProperty("ts")]
public string[] SelectedSeats { get; set; }
public Dictionary<string, TrainInfoItem> Stops { get; set; }
/// <summary>
/// 检测是否有非法请求有则返回true
/// </summary>
/// <returns></returns>
public bool CheckIllegal()
{
if (Key != "stupid360" || Stops == null)
return true;
if (Stops.Values.Any(s => (s.TrainInfo.code == s.TrainInfo.from.code && s.TrainInfo.to.code == s.TrainInfo.end.code)))
return true;
//add 2014年9月4日 - 检测非法席别
if (SelectedSeats == null)
return true;
if (SelectedSeats.Length > 0)
{
if (Stops.Values.Any(s => !SelectedSeats.Any(x => s.TrainInfo.ticketMap.ContainsKey(x[0]))))
return true;
}
//added 2014-9-9 检测非法车次
if (SelectedTrain != null && SelectedTrain.Length > 0)
{
var reg = new Regex("^(" + string.Join("|", SelectedTrain.Select(s => TrainSuggestionCommonData.FrequencyTrainCodeMap.GetValue(s).DefaultForEmpty(s))) + ")$", RegexOptions.IgnoreCase);
if (Stops.Keys.Any(s => !reg.IsMatch(s)))
return true;
}
return false;
}
public void PreprocessData()
{
foreach (var trainInfoItem in Stops.ToArray())
{
if (trainInfoItem.Value.StopInfos == null)
{
Stops.Remove(trainInfoItem.Key);
}
else
{
trainInfoItem.Value.PreprocessData(Date);
}
}
}
}
}