Skip to content
Permalink
Browse files

Replace ugly switch statement in hub with dictionary

  • Loading branch information
nelsonwellswku committed Feb 15, 2015
1 parent 991aa37 commit 4baca0298e0ff6955e0248156eb295a2949bd046
Showing with 25 additions and 33 deletions.
  1. +20 −0 Website/HubHelpers/LowerSectionScorer.cs
  2. +1 −0 Website/Website.csproj
  3. +4 −33 Website/YahtzeeHub.cs
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using Yahtzee.Framework;

namespace Website.HubHelpers
{
public static class LowerSectionScorer
{
public static Dictionary<string, Func<IScoreSheet, IDiceCup, int?>> Score = new Dictionary<string, Func<IScoreSheet, IDiceCup, int?>>
{
{"threeofakind", (scoreSheet, diceCup) => scoreSheet.RecordThreeOfAKind(diceCup)},
{"fourofakind", (scoreSheet, diceCup) =>scoreSheet.RecordFourOfAKind(diceCup)},
{"fullhouse", (scoreSheet, diceCup)=> scoreSheet.RecordFullHouse(diceCup)},
{"smallstraight", (scoreSheet, diceCup) => scoreSheet.RecordSmallStraight(diceCup)},
{"largestraight", (scoreSheet, diceCup) => scoreSheet.RecordLargeStraight(diceCup)},
{"yahtzee", (scoreSheet, diceCup)=> scoreSheet.RecordYahtzee(diceCup)},
{"chance", (scoreSheet, diceCup) => scoreSheet.RecordChance(diceCup)}
};
}
}
@@ -180,6 +180,7 @@
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="HubHelpers\LowerSectionScorer.cs" />
<Compile Include="Models\AccountViewModels.cs" />
<Compile Include="Models\GameStateModel.cs" />
<Compile Include="Models\IdentityModels.cs" />
@@ -5,6 +5,8 @@
using Autofac;
using Website.Models;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Website.HubHelpers;

namespace Website
{
@@ -112,38 +114,7 @@ public void TakeLower(string name)
return;
}

int score = 0;
switch (name)
{
case "threeofakind":
state.ScoreSheet.RecordThreeOfAKind(state.CurrentDiceCup);
score = state.ScoreSheet.ThreeOfAKind.Value;
break;
case "fourofakind":
state.ScoreSheet.RecordFourOfAKind(state.CurrentDiceCup);
score = state.ScoreSheet.FourOfAKind.Value;
break;
case "fullhouse":
state.ScoreSheet.RecordFullHouse(state.CurrentDiceCup);
score = state.ScoreSheet.FullHouse.Value;
break;
case "smallstraight":
state.ScoreSheet.RecordSmallStraight(state.CurrentDiceCup);
score = state.ScoreSheet.SmallStraight.Value;
break;
case "largestraight":
state.ScoreSheet.RecordLargeStraight(state.CurrentDiceCup);
score = state.ScoreSheet.LargeStraight.Value;
break;
case "yahtzee":
state.ScoreSheet.RecordYahtzee(state.CurrentDiceCup);
score = state.ScoreSheet.Yahtzee.Value;
break;
case "chance":
state.ScoreSheet.RecordChance(state.CurrentDiceCup);
score = state.ScoreSheet.Chance.Value;
break;
}
int? score = LowerSectionScorer.Score[name](state.ScoreSheet, state.CurrentDiceCup);

state.CurrentDiceCup = _diceCupFactory();
bool isLowerSectionComplete = state.ScoreSheet.IsLowerSectionComplete;
@@ -162,7 +133,7 @@ public void TakeLower(string name)
Clients.Caller.setLower(new
{
name = name,
score = score,
score = score.Value,
isLowerSectionComplete = isLowerSectionComplete,
lowerSectionTotal = lowerSectionTotal,
isScoreSheetComplete = isScoreSheetComplete,

0 comments on commit 4baca02

Please sign in to comment.
You can’t perform that action at this time.