Konstanten statt literale im source

This commit is contained in:
2014-04-16 14:46:26 +02:00
parent 32048c3d84
commit 7e9fbbcf15

View File

@@ -9,6 +9,32 @@ namespace WorldOfPeacecraft
{ {
public class Parser 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 MessUpdate = "upd";
public const string MessDelete = "del"; public const string MessDelete = "del";
public const string MessMap = "map"; public const string MessMap = "map";
@@ -29,7 +55,6 @@ namespace WorldOfPeacecraft
private LinkedList<string> Message; private LinkedList<string> Message;
private Regex LastLineRegex; private Regex LastLineRegex;
private Backend backend; private Backend backend;
private Player DummyPlayer;
public Parser () public Parser ()
{ {
@@ -85,7 +110,7 @@ namespace WorldOfPeacecraft
private void ProcessData (Block parentBlock) private void ProcessData (Block parentBlock)
{ {
if (parentBlock.ValueExists ("ans")) { if (parentBlock.ValueExists (ValueAns)) {
ProcessAnswer (parentBlock); ProcessAnswer (parentBlock);
return; return;
} }
@@ -179,8 +204,8 @@ namespace WorldOfPeacecraft
{ {
CheckBlocksSize (mapBlock, 1, 1); CheckBlocksSize (mapBlock, 1, 1);
Block cellsBlock = mapBlock.GetBlocks ().First.Value; Block cellsBlock = mapBlock.GetBlocks ().First.Value;
int height = mapBlock.GetIntValue ("height"); int height = mapBlock.GetIntValue (ValueHeight);
int width = mapBlock.GetIntValue ("width"); int width = mapBlock.GetIntValue (ValueWidth);
try { try {
CheckBlocksSize (cellsBlock, width * height, width * height); CheckBlocksSize (cellsBlock, width * height, width * height);
} catch (ParsingException e) { } catch (ParsingException e) {
@@ -195,9 +220,9 @@ namespace WorldOfPeacecraft
private void ProcessMessage (Block mesBlock) private void ProcessMessage (Block mesBlock)
{ {
int srcid = mesBlock.GetIntValue ("srcid"); int srcid = mesBlock.GetIntValue (ValueSourceId);
string src = mesBlock.GetStringValue ("src"); string src = mesBlock.GetStringValue (ValueSource);
string txt = mesBlock.GetStringValue ("txt"); string txt = mesBlock.GetStringValue (ValueText);
Message m = new Message (srcid, src, txt); Message m = new Message (srcid, src, txt);
} }
@@ -209,9 +234,9 @@ namespace WorldOfPeacecraft
private void ProcessResult (Block procBlock) private void ProcessResult (Block procBlock)
{ {
CheckBlocksSize (procBlock, 1, 1); CheckBlocksSize (procBlock, 1, 1);
int round = procBlock.GetIntValue ("round"); int round = procBlock.GetIntValue (ValueRound);
bool running = procBlock.GetBoolValue ("running"); bool running = procBlock.GetBoolValue (ValueRunning);
int delay = procBlock.GetIntValue ("delay"); int delay = procBlock.GetIntValue (ValueDelay);
LinkedList <Block> block = procBlock.GetBlocks (); LinkedList <Block> block = procBlock.GetBlocks ();
ProcessOpponent (block.First.Value); ProcessOpponent (block.First.Value);
Result r = new Result(round, running, delay); Result r = new Result(round, running, delay);
@@ -219,17 +244,17 @@ namespace WorldOfPeacecraft
private void ProcessOpponent(Block oppBlock) private void ProcessOpponent(Block oppBlock)
{ {
int id = oppBlock.GetIntValue ("id"); int id = oppBlock.GetIntValue (ValueId);
int points = oppBlock.GetIntValue ("points"); int points = oppBlock.GetIntValue (ValuePoints);
int total = oppBlock.GetIntValue ("total"); int total = oppBlock.GetIntValue (ValueTotal);
LinkedList <string> unnamedValue = oppBlock.GetUnnamedValues (); LinkedList <string> unnamedValue = oppBlock.GetUnnamedValues ();
string stringValue = unnamedValue.First.Value; string stringValue = unnamedValue.First.Value;
Decision d; Decision d;
if (stringValue == "DRAGON") if (stringValue == EValueDragon)
d = Decision.DRAGON; d = Decision.DRAGON;
else if (stringValue == "STAGHUNT") else if (stringValue == EValueStagunt)
d = Decision.STAGHUNT; d = Decision.STAGHUNT;
else if (stringValue == "SKIRMISH") else if (stringValue == EValueSkirmish)
d = Decision.SKIRMISH; d = Decision.SKIRMISH;
else else
throw new ParsingException("Wrong param"); // TODO Better message throw new ParsingException("Wrong param"); // TODO Better message
@@ -244,19 +269,19 @@ namespace WorldOfPeacecraft
private void ProcessChallenge (Block challengeBlock) private void ProcessChallenge (Block challengeBlock)
{ {
int id = challengeBlock.GetIntValue("id"); int id = challengeBlock.GetIntValue(ValueId);
String type; String type;
LinkedList<string> value = challengeBlock.GetUnnamedValues(); LinkedList<string> value = challengeBlock.GetUnnamedValues();
// TODO check value size, better name // TODO check value size, better name
switch (value.First.Value) switch (value.First.Value)
{ {
case "DRAGON": case EValueDragon:
type = "Dragon"; type = "Dragon";
break; break;
case "STAGHUNT": case EValueStagunt:
type = "Staghunt"; type = "Staghunt";
break; break;
case "SKIRMISCH": case EValueSkirmish:
type = "Skirmisch"; type = "Skirmisch";
break; break;
default: default:
@@ -336,31 +361,30 @@ namespace WorldOfPeacecraft
private Dragon MapDragon (Block dragonBlock) private Dragon MapDragon (Block dragonBlock)
{ {
int id = dragonBlock.GetIntValue ("id"); int id = dragonBlock.GetIntValue (ValueId);
bool busy = dragonBlock.GetBoolValue ("busy"); bool busy = dragonBlock.GetBoolValue (ValueBusy);
string desc = dragonBlock.GetStringValue ("desc"); string desc = dragonBlock.GetStringValue (ValueDescription);
int x = dragonBlock.GetIntValue ("x"); int x = dragonBlock.GetIntValue (ValueX);
int y = dragonBlock.GetIntValue ("y"); int y = dragonBlock.GetIntValue (ValueY);
return new Dragon (id, x, y, desc, busy); return new Dragon (id, x, y, desc, busy);
} }
private Player MapPlayer (Block playerBlock) private Player MapPlayer (Block playerBlock)
{ {
int id = playerBlock.GetIntValue ("id"); int id = playerBlock.GetIntValue (ValueId);
int score = playerBlock.GetIntValue ("points"); int score = playerBlock.GetIntValue (ValuePoints);
bool busy = playerBlock.GetBoolValue ("busy"); bool busy = playerBlock.GetBoolValue (ValueBusy);
string desc = playerBlock.GetStringValue ("desc"); string desc = playerBlock.GetStringValue (ValueDescription);
int x = playerBlock.GetIntValue ("x"); int x = playerBlock.GetIntValue (ValueX);
int y = playerBlock.GetIntValue ("y"); int y = playerBlock.GetIntValue (ValueY);
DummyPlayer = new Player(id, x, y, desc, busy, score);
return new Player (id, x, y, desc, busy, score); return new Player (id, x, y, desc, busy, score);
} }
private Tile MapMapcell (Block cellBlock) private Tile MapMapcell (Block cellBlock)
{ {
CheckBlocksSize (cellBlock, 1, 1); CheckBlocksSize (cellBlock, 1, 1);
int x = cellBlock.GetIntValue ("col"); int x = cellBlock.GetIntValue (ValueCol);
int y = cellBlock.GetIntValue ("row"); int y = cellBlock.GetIntValue (ValueRow);
Block propsBlock = cellBlock.GetBlocks ().First.Value; Block propsBlock = cellBlock.GetBlocks ().First.Value;
bool walkable = false; bool walkable = false;
bool wall = false; bool wall = false;
@@ -369,19 +393,19 @@ namespace WorldOfPeacecraft
bool huntable = false; bool huntable = false;
foreach (string prop in propsBlock.GetUnnamedValues()) { foreach (string prop in propsBlock.GetUnnamedValues()) {
switch (prop) { switch (prop) {
case "WALKABLE": case EValueWalkable:
walkable = true; walkable = true;
break; break;
case "WALL": case EValueWall:
wall = true; wall = true;
break; break;
case "FOREST": case EValueForest:
forest = true; forest = true;
break; break;
case "WATER": case EValueWater:
water = true; water = true;
break; break;
case "HUNTABLE": case EValueHuntable:
huntable = true; huntable = true;
break; break;
default: default: