From 2504f06e98cb64b2e1706fbfe4668c390d04bdab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Wed, 9 Apr 2014 22:07:18 +0200 Subject: [PATCH] =?UTF-8?q?Mehr=20code=20f=C3=BCr=20den=20Parser?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Parser.cs | 77 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 25 deletions(-) diff --git a/src/Parser.cs b/src/Parser.cs index 28a1830..bb473b8 100644 --- a/src/Parser.cs +++ b/src/Parser.cs @@ -23,7 +23,6 @@ namespace WorldOfPeacecraft public const string MessPlayers = "players"; public const string MessDragon = "dragon"; public const string MessMapcell = "cell"; - private Queue Buffer = new Queue (); private AutoResetEvent BufferFilledEvent = new AutoResetEvent (false); private Thread ParserThread; @@ -72,7 +71,7 @@ namespace WorldOfPeacecraft ProcessAnswer (parentBlock); return; } - CheckBlocksSize(parentBlock.GetBlocks(), 1, 1); + CheckBlocksSize (parentBlock, 1, 1); Block block = parentBlock.GetBlocks ().First.Value; switch (block.GetName ()) { case MessUpdate: @@ -119,8 +118,8 @@ namespace WorldOfPeacecraft private void ProcessUpdate (Block updateBlock) { - LinkedList blocks = updateBlock.GetBlocks (); CheckBlocksSize (updateBlock, 1, 1); + LinkedList blocks = updateBlock.GetBlocks (); Block block = blocks.First.Value; switch (block.GetName ()) { case MessDragon: @@ -140,24 +139,36 @@ namespace WorldOfPeacecraft private void ProcessDelete (Block deleteBlock) { - LinkedList blocks = deleteBlock; CheckBlocksSize (deleteBlock, 1, 1); + LinkedList blocks = deleteBlock.GetBlocks (); Block block = blocks.First.Value; switch (block.GetName ()) { case MessPlayer: - Player player = MapPlayer(block); - backend.removePlayer(player); + Player player = MapPlayer (block); + backend.removePlayer (player); break; case MessDragon: - Dragon dragon = MapDragon(block); - backend.removeDragon(dragon); + Dragon dragon = MapDragon (block); + backend.removeDragon (dragon); 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) @@ -200,39 +211,55 @@ namespace WorldOfPeacecraft { } - private void ProcessMapcell (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) { - 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) { - LinkedList blocks = parentBlock.GetBlocks(); + LinkedList blocks = parentBlock.GetBlocks (); if (blocks.Count < min || (max >= 0 && blocks.Count > max)) { if (min == max) { - throw new ParsingException("The block '" + parentBlock.GetName() + "' has to contain exactly '" + min + "' Block, but contained '" + blocks.Count + "'"); - } - else if (max < 0) { - 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 + "'"); + throw new ParsingException ("The block '" + parentBlock.GetName () + "' has to contain exactly '" + min + "' Block, but contained '" + blocks.Count + "'"); + } else if (max < 0) { + 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 + "'"); } } }