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>
<Compile Include="DummyBackend.cs" />
<Compile Include="ParserTest.cs" />
<Compile Include="src\Buffer.cs" />
<Compile Include="src\Receiver.cs" />
<Compile Include="src\Sender.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 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<string> Buffer = new Queue<string> ();
private AutoResetEvent BufferFilledEvent = new AutoResetEvent (false);
private Buffer MonsterBuffer;
private Thread ParserThread;
private LinkedList<string> 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<string> ();
@@ -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> 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 <string> 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<string> 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
{

View File

@@ -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());
}
}