diff --git a/inf3.csproj b/inf3.csproj index 9544d7b..52bbf88 100644 --- a/inf3.csproj +++ b/inf3.csproj @@ -41,6 +41,7 @@ + diff --git a/src/Buffer.cs b/src/Buffer.cs new file mode 100644 index 0000000..a10d9a7 --- /dev/null +++ b/src/Buffer.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; + +namespace WorldOfPeacecraft +{ + public class Buffer + { + private Queue RawBuffer = new Queue(); + private AutoResetEvent BufferFilledEvent = new AutoResetEvent (false); + int size; + + public Buffer(int size) + { + this.size=size; + } + + + public void AddToBuffer (string s) + { + if (RawBuffer.Count >= size){ + + } + else{ + lock (RawBuffer) { + RawBuffer.Enqueue (s); + BufferFilledEvent.Set (); + } + } + + } + } +} diff --git a/src/Parser.cs b/src/Parser.cs index be04f34..b470f3f 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"; @@ -23,16 +49,15 @@ 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 Buffer MonsterBuffer; private Thread ParserThread; private LinkedList Message; private Regex LastLineRegex; private Backend backend; - private Player DummyPlayer; - public Parser () + public Parser (Buffer b) { + MonsterBuffer = b; ParserThread = new Thread (new ThreadStart (this.RunParser)); ParserThread.Start(); Message = new LinkedList (); @@ -40,11 +65,11 @@ namespace WorldOfPeacecraft } private void RunParser () - { + {/* while (true) { bool waitRequired = false; - lock (Buffer) { - if (Buffer.Count == 0) { + lock (MonsterBuffer) { + if (MonsterBuffer.Count == 0) { waitRequired = true; BufferFilledEvent.Reset (); } @@ -58,9 +83,9 @@ namespace WorldOfPeacecraft if (IsCompletePackage ()) { Parse (); } - } + }*/ } - + public Player getDummyPlayer() { return DummyPlayer; @@ -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: @@ -421,13 +445,7 @@ namespace WorldOfPeacecraft ParserThread.Abort (); } - public void AddToBuffer (string s) - { - lock (Buffer) { - Buffer.Enqueue (s); - BufferFilledEvent.Set (); - } - } + private class Block { diff --git a/src/Receiver.cs b/src/Receiver.cs index 9e0b5e9..f2428ff 100644 --- a/src/Receiver.cs +++ b/src/Receiver.cs @@ -9,13 +9,13 @@ namespace WorldOfPeacecraft { private TcpClient Client; private StreamReader Reader; - private Parser Parser; + private Buffer KillerBuffer; private Thread ReceiverThread; - public Receiver (TcpClient client, Parser parser) + public Receiver (TcpClient client, Buffer buffer) { this.Client = client; - this.Parser = parser; + this.KillerBuffer = buffer; ReceiverThread = new Thread(new ThreadStart(this.doReceive)); ReceiverThread.Start(); } @@ -29,7 +29,7 @@ namespace WorldOfPeacecraft { this.Reader = new StreamReader (Client.GetStream ()); while (true) { - Parser.AddToBuffer(Receive()); + KillerBuffer.AddToBuffer(Receive()); } }