From 7e9fbbcf156d928a41fdec1bfbc9e5e294ea6d71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Wed, 16 Apr 2014 14:46:26 +0200 Subject: [PATCH] Konstanten statt literale im source --- src/Parser.cs | 104 +++++++++++++++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 40 deletions(-) diff --git a/src/Parser.cs b/src/Parser.cs index be04f34..16d9052 100644 --- a/src/Parser.cs +++ b/src/Parser.cs @@ -9,6 +9,32 @@ namespace WorldOfPeacecraft { public class Parser { + public const string ValueAns = "ans"; + public const string ValueHeight = "height"; + public const string ValueWidth = "width"; + public const string ValueSource = "src"; + public const string ValueSourceId = "srcid"; + public const string ValueText = "txt"; + public const string ValueRound = "round"; + public const string ValueRunning = "running"; + public const string ValueDelay = "delay"; + public const string ValueId = "id"; + public const string ValuePoints = "points; + public const string ValueTotal = "total; + public const string ValueDescription = "desc"; + public const string ValueBusy = "busy"; + public const string ValueX = "x"; + public const string ValueY = "y"; + public const string ValueCol = "col"; + public const string ValueRow = "row"; + public const string EValueDragon = "DRAGON"; + public const string EValueStagunt = "STAGHUNT"; + public const string EValueSkirmish = "SKIRMISH"; + public const string EValueWalkable = "WALKABLE"; + public const string EValueWall = "WALL; + public const string EValueForest = "FOREST; + public const string EValueWater = "WATER"; + public const string EValueHuntable = "HUNTABLE"; public const string MessUpdate = "upd"; public const string MessDelete = "del"; public const string MessMap = "map"; @@ -29,7 +55,6 @@ namespace WorldOfPeacecraft private LinkedList Message; private Regex LastLineRegex; private Backend backend; - private Player DummyPlayer; public Parser () { @@ -85,7 +110,7 @@ namespace WorldOfPeacecraft private void ProcessData (Block parentBlock) { - if (parentBlock.ValueExists ("ans")) { + if (parentBlock.ValueExists (ValueAns)) { ProcessAnswer (parentBlock); return; } @@ -179,8 +204,8 @@ namespace WorldOfPeacecraft { CheckBlocksSize (mapBlock, 1, 1); Block cellsBlock = mapBlock.GetBlocks ().First.Value; - int height = mapBlock.GetIntValue ("height"); - int width = mapBlock.GetIntValue ("width"); + int height = mapBlock.GetIntValue (ValueHeight); + int width = mapBlock.GetIntValue (ValueWidth); try { CheckBlocksSize (cellsBlock, width * height, width * height); } catch (ParsingException e) { @@ -195,9 +220,9 @@ namespace WorldOfPeacecraft private void ProcessMessage (Block mesBlock) { - int srcid = mesBlock.GetIntValue ("srcid"); - string src = mesBlock.GetStringValue ("src"); - string txt = mesBlock.GetStringValue ("txt"); + int srcid = mesBlock.GetIntValue (ValueSourceId); + string src = mesBlock.GetStringValue (ValueSource); + string txt = mesBlock.GetStringValue (ValueText); Message m = new Message (srcid, src, txt); } @@ -209,9 +234,9 @@ namespace WorldOfPeacecraft private void ProcessResult (Block procBlock) { CheckBlocksSize (procBlock, 1, 1); - int round = procBlock.GetIntValue ("round"); - bool running = procBlock.GetBoolValue ("running"); - int delay = procBlock.GetIntValue ("delay"); + int round = procBlock.GetIntValue (ValueRound); + bool running = procBlock.GetBoolValue (ValueRunning); + int delay = procBlock.GetIntValue (ValueDelay); LinkedList block = procBlock.GetBlocks (); ProcessOpponent (block.First.Value); Result r = new Result(round, running, delay); @@ -219,17 +244,17 @@ namespace WorldOfPeacecraft private void ProcessOpponent(Block oppBlock) { - int id = oppBlock.GetIntValue ("id"); - int points = oppBlock.GetIntValue ("points"); - int total = oppBlock.GetIntValue ("total"); + int id = oppBlock.GetIntValue (ValueId); + int points = oppBlock.GetIntValue (ValuePoints); + int total = oppBlock.GetIntValue (ValueTotal); LinkedList unnamedValue = oppBlock.GetUnnamedValues (); string stringValue = unnamedValue.First.Value; Decision d; - if (stringValue == "DRAGON") + if (stringValue == EValueDragon) d = Decision.DRAGON; - else if (stringValue == "STAGHUNT") + else if (stringValue == EValueStagunt) d = Decision.STAGHUNT; - else if (stringValue == "SKIRMISH") + else if (stringValue == EValueSkirmish) d = Decision.SKIRMISH; else throw new ParsingException("Wrong param"); // TODO Better message @@ -244,19 +269,19 @@ namespace WorldOfPeacecraft private void ProcessChallenge (Block challengeBlock) { - int id = challengeBlock.GetIntValue("id"); + int id = challengeBlock.GetIntValue(ValueId); String type; LinkedList value = challengeBlock.GetUnnamedValues(); // TODO check value size, better name switch (value.First.Value) { - case "DRAGON": + case EValueDragon: type = "Dragon"; break; - case "STAGHUNT": + case EValueStagunt: type = "Staghunt"; break; - case "SKIRMISCH": + case EValueSkirmish: type = "Skirmisch"; break; default: @@ -266,7 +291,7 @@ namespace WorldOfPeacecraft bool accepted = challengeBlock.GetBoolValue("accepted"); Challenge c = new Challenge(id, type, accepted); } - + private void ProcessPlayer (Block playerBlock) { @@ -336,31 +361,30 @@ namespace WorldOfPeacecraft private Dragon MapDragon (Block dragonBlock) { - int id = dragonBlock.GetIntValue ("id"); - bool busy = dragonBlock.GetBoolValue ("busy"); - string desc = dragonBlock.GetStringValue ("desc"); - int x = dragonBlock.GetIntValue ("x"); - int y = dragonBlock.GetIntValue ("y"); + int id = dragonBlock.GetIntValue (ValueId); + bool busy = dragonBlock.GetBoolValue (ValueBusy); + string desc = dragonBlock.GetStringValue (ValueDescription); + int x = dragonBlock.GetIntValue (ValueX); + int y = dragonBlock.GetIntValue (ValueY); return new Dragon (id, x, y, desc, busy); } private Player MapPlayer (Block playerBlock) { - int id = playerBlock.GetIntValue ("id"); - int score = playerBlock.GetIntValue ("points"); - bool busy = playerBlock.GetBoolValue ("busy"); - string desc = playerBlock.GetStringValue ("desc"); - int x = playerBlock.GetIntValue ("x"); - int y = playerBlock.GetIntValue ("y"); - DummyPlayer = new Player(id, x, y, desc, busy, score); + int id = playerBlock.GetIntValue (ValueId); + int score = playerBlock.GetIntValue (ValuePoints); + bool busy = playerBlock.GetBoolValue (ValueBusy); + string desc = playerBlock.GetStringValue (ValueDescription); + int x = playerBlock.GetIntValue (ValueX); + int y = playerBlock.GetIntValue (ValueY); return new Player (id, x, y, desc, busy, score); } private Tile MapMapcell (Block cellBlock) { CheckBlocksSize (cellBlock, 1, 1); - int x = cellBlock.GetIntValue ("col"); - int y = cellBlock.GetIntValue ("row"); + int x = cellBlock.GetIntValue (ValueCol); + int y = cellBlock.GetIntValue (ValueRow); Block propsBlock = cellBlock.GetBlocks ().First.Value; bool walkable = false; bool wall = false; @@ -369,19 +393,19 @@ namespace WorldOfPeacecraft bool huntable = false; foreach (string prop in propsBlock.GetUnnamedValues()) { switch (prop) { - case "WALKABLE": + case EValueWalkable: walkable = true; break; - case "WALL": + case EValueWall: wall = true; break; - case "FOREST": + case EValueForest: forest = true; break; - case "WATER": + case EValueWater: water = true; break; - case "HUNTABLE": + case EValueHuntable: huntable = true; break; default: