Mehr code für den Parser

This commit is contained in:
2014-04-09 22:07:18 +02:00
parent ad0776f25d
commit 2504f06e98

View File

@@ -23,7 +23,6 @@ namespace WorldOfPeacecraft
public const string MessPlayers = "players"; public const string MessPlayers = "players";
public const string MessDragon = "dragon"; public const string MessDragon = "dragon";
public const string MessMapcell = "cell"; public const string MessMapcell = "cell";
private Queue<string> Buffer = new Queue<string> (); private Queue<string> Buffer = new Queue<string> ();
private AutoResetEvent BufferFilledEvent = new AutoResetEvent (false); private AutoResetEvent BufferFilledEvent = new AutoResetEvent (false);
private Thread ParserThread; private Thread ParserThread;
@@ -72,7 +71,7 @@ namespace WorldOfPeacecraft
ProcessAnswer (parentBlock); ProcessAnswer (parentBlock);
return; return;
} }
CheckBlocksSize(parentBlock.GetBlocks(), 1, 1); CheckBlocksSize (parentBlock, 1, 1);
Block block = parentBlock.GetBlocks ().First.Value; Block block = parentBlock.GetBlocks ().First.Value;
switch (block.GetName ()) { switch (block.GetName ()) {
case MessUpdate: case MessUpdate:
@@ -119,8 +118,8 @@ namespace WorldOfPeacecraft
private void ProcessUpdate (Block updateBlock) private void ProcessUpdate (Block updateBlock)
{ {
LinkedList<Block> blocks = updateBlock.GetBlocks ();
CheckBlocksSize (updateBlock, 1, 1); CheckBlocksSize (updateBlock, 1, 1);
LinkedList<Block> blocks = updateBlock.GetBlocks ();
Block block = blocks.First.Value; Block block = blocks.First.Value;
switch (block.GetName ()) { switch (block.GetName ()) {
case MessDragon: case MessDragon:
@@ -140,24 +139,36 @@ namespace WorldOfPeacecraft
private void ProcessDelete (Block deleteBlock) private void ProcessDelete (Block deleteBlock)
{ {
LinkedList<Block> blocks = deleteBlock;
CheckBlocksSize (deleteBlock, 1, 1); CheckBlocksSize (deleteBlock, 1, 1);
LinkedList<Block> blocks = deleteBlock.GetBlocks ();
Block block = blocks.First.Value; Block block = blocks.First.Value;
switch (block.GetName ()) { switch (block.GetName ()) {
case MessPlayer: case MessPlayer:
Player player = MapPlayer(block); Player player = MapPlayer (block);
backend.removePlayer(player); backend.removePlayer (player);
break; break;
case MessDragon: case MessDragon:
Dragon dragon = MapDragon(block); Dragon dragon = MapDragon (block);
backend.removeDragon(dragon); backend.removeDragon (dragon);
break; break;
} }
} }
private void ProcessMap (Block block) private void ProcessMap (Block mapBlock)
{ {
CheckBlocksSize (mapBlock, 1, 1);
Block cellsBlock = mapBlock.GetBlocks ().First.Value;
int height = mapBlock.GetIntValue ("height");
int width = mapBlock.GetIntValue ("width");
try {
CheckBlocksSize (cellsBlock, width * height, width * height);
} catch (ParsingException e) {
throw new ParsingException ("The received map is " + width + "x" + height + "=" + (width * height) + " cells large, but the received cellblock contained " + cellsBlock.GetBlocks ().Count, e);
}
Map map = new Map(height, width);
foreach (Block cell in cellsBlock.GetBlocks()) {
// TODO Fertigstellen
}
} }
private void ProcessMessage (Block block) private void ProcessMessage (Block block)
@@ -200,39 +211,55 @@ namespace WorldOfPeacecraft
{ {
} }
private void ProcessMapcell (Block block)
{
}
private void ProcessDragon (Block block) private void ProcessDragon (Block block)
{ {
} }
private Dragon MapDragon (Block block) 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");
return new Dragon (id, x, y, desc, busy);
} }
private Player MapPlayer (Block block) 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");
return new Player (id, x, y, desc, busy, score);
}
private ITile MapMapcell (Block cellBlock)
{
CheckBlocksSize(cellBlock, 1, 1);
int x = cellBlock.GetIntValue("col");
int y = cellBlock.GetIntValue("row");
Block propsBlock = cellBlock.GetBlocks ().First.Value;
// TODO Fertigstellen
} }
private void ThrowUnknownBlockException (Block parentBlock, Block childBlock) private void ThrowUnknownBlockException (Block parentBlock, Block childBlock)
{ {
throw new ParsingException("Unknown Block: '" + childBlock.GetName() + "' (as subblock of '" + parentBlock.GetName() + "')"); throw new ParsingException ("Unknown Block: '" + childBlock.GetName () + "' (as subblock of '" + parentBlock.GetName () + "')");
} }
private void CheckBlocksSize (Block parentBlock, int min, int max) private void CheckBlocksSize (Block parentBlock, int min, int max)
{ {
LinkedList<Block> blocks = parentBlock.GetBlocks(); LinkedList<Block> blocks = parentBlock.GetBlocks ();
if (blocks.Count < min || (max >= 0 && blocks.Count > max)) { if (blocks.Count < min || (max >= 0 && blocks.Count > max)) {
if (min == max) { if (min == max) {
throw new ParsingException("The block '" + parentBlock.GetName() + "' has to contain exactly '" + min + "' Block, but contained '" + blocks.Count + "'"); throw new ParsingException ("The block '" + parentBlock.GetName () + "' has to contain exactly '" + min + "' Block, but contained '" + blocks.Count + "'");
} } else if (max < 0) {
else if (max < 0) { throw new ParsingException ("The block '" + parentBlock.GetName () + "' has to contain at least '" + min + "' Blocks, but contained '" + blocks.Count + "'");
throw new ParsingException("The block '" + parentBlock.GetName() + "' has to contain at least '" + min + "' Blocks, but contained '" + blocks.Count + "'"); } else {
} throw new ParsingException ("The block '" + parentBlock.GetName () + "' has to contain between '" + min + "' and '" + max + "' Blocks, but contained '" + blocks.Count + "'");
else {
throw new ParsingException("The block '" + parentBlock.GetName () + "' has to contain between '" + min + "' and '" + max + "' Blocks, but contained '" + blocks.Count + "'");
} }
} }
} }