Merge branch 'master' of manuel-voegele.de:inf3

This commit is contained in:
Wafa Sadri
2014-04-16 14:54:41 +02:00
4 changed files with 113 additions and 59 deletions

View File

@@ -41,6 +41,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="DummyBackend.cs" /> <Compile Include="DummyBackend.cs" />
<Compile Include="ParserTest.cs" /> <Compile Include="ParserTest.cs" />
<Compile Include="src\Buffer.cs" />
<Compile Include="src\Receiver.cs" /> <Compile Include="src\Receiver.cs" />
<Compile Include="src\Sender.cs" /> <Compile Include="src\Sender.cs" />
<Compile Include="src\DefaultGui\DefaultGui.Designer.cs" /> <Compile Include="src\DefaultGui\DefaultGui.Designer.cs" />

35
src/Buffer.cs Normal file
View File

@@ -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<string> RawBuffer = new Queue<string>();
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 ();
}
}
}
}
}

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";
@@ -23,16 +49,15 @@ 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 Buffer MonsterBuffer;
private AutoResetEvent BufferFilledEvent = new AutoResetEvent (false);
private Thread ParserThread; private Thread ParserThread;
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 (Buffer b)
{ {
MonsterBuffer = b;
ParserThread = new Thread (new ThreadStart (this.RunParser)); ParserThread = new Thread (new ThreadStart (this.RunParser));
ParserThread.Start(); ParserThread.Start();
Message = new LinkedList<string> (); Message = new LinkedList<string> ();
@@ -40,11 +65,11 @@ namespace WorldOfPeacecraft
} }
private void RunParser () private void RunParser ()
{ {/*
while (true) { while (true) {
bool waitRequired = false; bool waitRequired = false;
lock (Buffer) { lock (MonsterBuffer) {
if (Buffer.Count == 0) { if (MonsterBuffer.Count == 0) {
waitRequired = true; waitRequired = true;
BufferFilledEvent.Reset (); BufferFilledEvent.Reset ();
} }
@@ -58,7 +83,7 @@ namespace WorldOfPeacecraft
if (IsCompletePackage ()) { if (IsCompletePackage ()) {
Parse (); Parse ();
} }
} }*/
} }
public Player getDummyPlayer() public Player getDummyPlayer()
@@ -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:
@@ -421,13 +445,7 @@ namespace WorldOfPeacecraft
ParserThread.Abort (); ParserThread.Abort ();
} }
public void AddToBuffer (string s)
{
lock (Buffer) {
Buffer.Enqueue (s);
BufferFilledEvent.Set ();
}
}
private class Block private class Block
{ {

View File

@@ -9,13 +9,13 @@ namespace WorldOfPeacecraft
{ {
private TcpClient Client; private TcpClient Client;
private StreamReader Reader; private StreamReader Reader;
private Parser Parser; private Buffer KillerBuffer;
private Thread ReceiverThread; private Thread ReceiverThread;
public Receiver (TcpClient client, Parser parser) public Receiver (TcpClient client, Buffer buffer)
{ {
this.Client = client; this.Client = client;
this.Parser = parser; this.KillerBuffer = buffer;
ReceiverThread = new Thread(new ThreadStart(this.doReceive)); ReceiverThread = new Thread(new ThreadStart(this.doReceive));
ReceiverThread.Start(); ReceiverThread.Start();
} }
@@ -29,7 +29,7 @@ namespace WorldOfPeacecraft
{ {
this.Reader = new StreamReader (Client.GetStream ()); this.Reader = new StreamReader (Client.GetStream ());
while (true) { while (true) {
Parser.AddToBuffer(Receive()); KillerBuffer.AddToBuffer(Receive());
} }
} }