2017-03-31 53 views
1

我有网站执行匹配操作。我正在编写代码来限制基于当前匹配阶段的某些操作。例如,如果前半场是现阶段,那么我应该限制直接结束比赛,而不是下半场。对于每个阶段,我有多个限制阶段。以下是我的舞台枚举。如何将列表与每个枚举项目关联在一起#

public enum MatchStage 
    { 
     FirstHalfAssumedStartTime = 1, 
     FirstHalfAssumedEndTime = 2, 
     SecondHalfAssumedStartTime = 3, 
     SecondHalfAssumedEndTime = 4, 
     ExtraFirstHalfAssumedStartTime = 5, 
     ExtraFirstHalfAssumedEndTime = 6, 
     ExtraSecondHalfAssumedStartTime = 7, 
     ExtraSecondHalfAssumedEndTime = 8, 
     PauseStartTime = 9, 
     PauseEndTime = 10, 
     EndMatchTime = 11 
    } 

我想使下面的方法工作。我想打电话给currentSiteCoreStage.RestrictedStages,它返回限制阶段列表。我怎样才能做到这一点?

public static bool IsProposedStageValid(MatchStage currentSiteCoreStage, MatchStage proposedStage) 
     { 
      if (currentSiteCoreStage.RestrictedStages.Contains(proposedStage)) // I am hoping to make this work 
       return false; 
     } 
+0

什么是'RestrictedStages'? –

+0

我会用更积极的方式 - 获得下一个可能的阶段,而不是你不感兴趣的阶段。如果你在'ExtraFirstHalfAssumedStartTime',那么它看起来像你只有一个选项来继续 –

+0

@ThomasAyoub什么都没有定义。我不确定即使是这样的可能或不可能。我也打开更好的解决方案。我在考虑这一行“currentSiteCoreStage.RestrictedStages.Contains”,因为它有意义并解释了目的。 – Akie

回答

3

你在这里实际上是什么状态机。您可以创建一个方法,为应用程序支持的每个阶段定义可能的阶段转换。例如。 (当然,你不必使用迭代器,你可以简单地从每一种情况下块返回阶段阵列):

public static IEnumerable<MatchStage> GetAvailableStageTransitions(MatchStage stage) 
{ 
    switch (stage) 
    { 
     case MatchStage.FirstHalfAssumedStartTime: 
      yield return MatchStage.FirstHalfAssumedEndTime; 
      yield return MatchStage.PauseStartTime; 
      yield break; 
     case MatchStage.SecondHalfAssumedStartTime: 
      yield return MatchStage.SecondHalfAssumedEndTime; 
      yield return MatchStage.PauseStartTime; 
      yield break; 
     // etc 
     default: 
      throw new NotSupportedException($"Stage {stage} is not supported."); 
    } 
} 

然后检查是否建议阶段可:根据复杂

GetAvailableStageTransitions(currentSiteCoreStage).Contains(proposedStage) 

你可以考虑将状态转换和逻辑转换为状态对象。欲了解更多详情,请阅读State Design Pattern

0

也许认为这样的事情: 1:您枚举

2:

public class Stage 
{ 
    public MatchStage MathStage { get; set; } 
    public List<MatchStage> PossibleStages { get; set; } 
} 

3.

public static bool IsProposedStageValid(Stage currentSiteCoreStage, MatchStage proposedStage) 
{ 
    if (currentSiteCoreStage.PossibleStages.Contains(proposedStage)) // I am hoping to make this work 
    return false; 

    return true; 
} 

例子:

Stage st1 = new Stage 
{ 
     MathStage = MatchStage.FirstHalfAssumedStartTime, 
     PossibleStages = new List<MatchStage> { MatchStage.FirstHalfAssumedEndTime } 
}; 

bool res = IsProposedStageValid(st1, MatchStage.PauseEndTime); 
0

一种方法是有一个知道你当前阶段的课程,并且可以根据当前阶段生成RestrictedStages。喜欢的东西:

public enum Stage 
{ 
    First, Second, Third 
} 

public class CoreStage 
{ 
    public Stage CurrentStage { get; private set; } 

    public IEnumerable<Stage> RestrictedStages { get; private set; } 

    public CoreStage(Stage currentStage) 
    { 
     CurrentStage = currentStage; 
     RestrictedStages = GenerateRestrictedStages(); 
    } 

    private IEnumerable<Stage> GenerateRestrictedStages() 
    { 
     //apply your logic to determine Restricted stages based on current stage. 
     return null; 
    } 
} 

现在,在您IsProposedStageValid方法看起来某事象下面这样:

public static bool IsProposedStageValid(Stage currentSiteCoreStage, Stage proposedStage) 
    { 
     var coreStage = new CoreStage(currentSiteCoreStage); 
     if (coreStage.RestrictedStages.Contains(proposedStage)) 
     { 
      //do whatever you want. 
     } 
     return false; 
    } 
相关问题