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

This commit is contained in:
Daniel Herrmann
2014-04-11 11:28:42 +02:00
6 changed files with 198 additions and 23 deletions

12
Message.cs Normal file
View File

@@ -0,0 +1,12 @@
using System;
namespace inf3
{
public class Message
{
public Message ()
{
}
}
}

View File

@@ -60,6 +60,8 @@
<Compile Include="src\Backend.cs" /> <Compile Include="src\Backend.cs" />
<Compile Include="src\StringUtils.cs" /> <Compile Include="src\StringUtils.cs" />
<Compile Include="src\ParsingException.cs" /> <Compile Include="src\ParsingException.cs" />
<Compile Include="src\Message.cs" />
<Compile Include="src\Opponent.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup /> <ItemGroup />
</Project> </Project>

View File

@@ -41,6 +41,16 @@ namespace Frontend
{ {
Players.Remove (player.GetId ()); Players.Remove (player.GetId ());
} }
public void clearPlayers ()
{
Players.Clear ();
}
public void clearDragons ()
{
Dragons.Clear ();
}
public Player getPlayer (int playerId) public Player getPlayer (int playerId)
{ {

19
src/Message.cs Normal file
View File

@@ -0,0 +1,19 @@
using System;
namespace inf3
{
public class Message
{
int sourceID;
string source;
string text;
public Message (int srcid, string src, string txt)
{
sourceID = srcid;
source = src;
text = txt;
}
}
}

21
src/Opponent.cs Normal file
View File

@@ -0,0 +1,21 @@
using System;
namespace inf3
{
public class Opponent
{
int id_;
int points_;
int total_;
enum Decision {DRAGON, STAGHUNT, SKIRMISH};
Decision d_;
public Opponent (int id, int points, int total, Decision d)
{
id_ = id;
points_ = points;
total_ = total;
d_ = d;
}
}
}

View File

@@ -30,6 +30,8 @@ namespace WorldOfPeacecraft
private Regex LastLineRegex; private Regex LastLineRegex;
private Backend backend; private Backend backend;
enum Decision {DRAGON, STAGHUNT, SKIRMISH};
public Parser () public Parser ()
{ {
ParserThread = new Thread (new ThreadStart (this.RunParser)); ParserThread = new Thread (new ThreadStart (this.RunParser));
@@ -56,15 +58,24 @@ namespace WorldOfPeacecraft
if (IsCompletePackage ()) { if (IsCompletePackage ()) {
Parse (); Parse ();
} }
// TODO Try-catch. IMPORTANT!
} }
} }
private void Parse () private void Parse ()
{ {
String[] aMessage = Enumerable.ToArray (Message); string[] aMessage = Enumerable.ToArray (Message);
Block mainBlock = new Block (aMessage, 0, aMessage.Length - 1); Message.Clear();
ProcessData (mainBlock); try {
Block mainBlock = new Block (aMessage, 0, aMessage.Length - 1);
ProcessData (mainBlock);
} catch (ParsingException e) {
string errormsg = "Error while parsing message: " + e.Message + "\n";
errormsg += "Message:";
foreach (string line in aMessage) {
errormsg += "\n\t" + line;
}
Console.WriteLine(errormsg);
}
} }
private void ProcessData (Block parentBlock) private void ProcessData (Block parentBlock)
@@ -153,6 +164,9 @@ namespace WorldOfPeacecraft
Dragon dragon = MapDragon (block); Dragon dragon = MapDragon (block);
backend.removeDragon (dragon); backend.removeDragon (dragon);
break; break;
default:
ThrowUnknownBlockException(deleteBlock, block);
break;
} }
} }
@@ -179,11 +193,13 @@ namespace WorldOfPeacecraft
int srcid = mesBlock.GetIntValue ("srcid"); int srcid = mesBlock.GetIntValue ("srcid");
string src = mesBlock.GetStringValue ("src"); string src = mesBlock.GetStringValue ("src");
string txt = mesBlock.GetStringValue ("txt"); string txt = mesBlock.GetStringValue ("txt");
//Message m = new Message (srcid, src, txt); Message m = new Message (srcid, src, txt);
//TODO Herausfinden wie wir das oben lösen (gilt für alle Funktionenstümpfe)
} }
private void ProcessAnswer (Block block) private void ProcessAnswer (Block block)
{ {
// TODO
} }
private void ProcessResult (Block procBlock) private void ProcessResult (Block procBlock)
@@ -192,19 +208,56 @@ namespace WorldOfPeacecraft
int round = procBlock.GetIntValue ("round"); int round = procBlock.GetIntValue ("round");
bool running = procBlock.GetBoolValue ("running"); bool running = procBlock.GetBoolValue ("running");
int delay = procBlock.GetIntValue ("delay"); int delay = procBlock.GetIntValue ("delay");
//ProcessOpponent (procBlock.GetBlocks ()); LinkedList <Block> block = procBlock.GetBlocks ();
ProcessOpponent (block.First.Value);
//Result r = new Result(round, running, delay); //Result r = new Result(round, running, delay);
} }
//"round:",INT,"running:",BOOLEAN,"delay:",INT,"begin:opponents:"OPPONENT,OPPONENT","end:opponents",
private void ProcessOpponent(Block block) private void ProcessOpponent(Block oppBlock)
{ {
//TODO int id = oppBlock.GetIntValue ("id");
int points = oppBlock.GetIntValue ("points");
int total = oppBlock.GetIntValue ("total");
LinkedList <string> unnamedValue = oppBlock.GetUnnamedValues ();
string stringValue = unnamedValue.First.Value;
Decision d;
if (stringValue == "DRAGON")
d = Decision.DRAGON;
if (stringValue == "STAGHUNT")
d = Decision.STAGHUNT;
if (stringValue == "SKIRMISH")
d = Decision.SKIRMISH;
}
//DRAGON|STAGHUNT|SKIRMISH
private void ProcessDecision(Block block)
{
//TODO -> Wafa
//OK das ist hier so'n bissle der gleiche scheiß wie "Answer" wie soll das gehn, ohne Block!?
//Maybe not TODO?
} }
private void ProcessChallenge (Block block) private void ProcessChallenge (Block challengeBlock)
{ {
//TODO -> Samed
int id = challengeBlock.GetIntValue("id");
String type;
switch (challengeBlock.GetStringValue())
{
case "DRAGON":
type = "Dragon";
break;
case "STAGHUNT":
type = "Staghunt";
break;
case "SKIRMISCH":
type = "Skirmisch";
break;
}
bool accepted = challengeBlock.GetBoolValue("accepted");
//Challenge c = new Challenge(id, type, accepted);
//CHALLENGE: "begin:challenge","id:",INT,"type:",("DRAGON"|"STAGHUNT"|"SKIRMISH"),"accepted:",BOOLEAN,"end:challenge"
} }
private void ProcessPlayer (Block playerBlock) private void ProcessPlayer (Block playerBlock)
@@ -217,8 +270,11 @@ namespace WorldOfPeacecraft
int y = playerBlock.GetIntValue ("y"); int y = playerBlock.GetIntValue ("y");
//Player p = new Player(points, id, busy, desc, x, y,); //Player p = new Player(points, id, busy, desc, x, y,);
} }
//"points:",INT, "id:",INT,"type:Player","busy:"BOOLEAN,"desc:"STRING,"x:",INT,"y:",INT,
private void ProcessMapcell (Block mapcellBlock)
{
backend.getMapObject().SetTile(MapMapcell(mapcellBlock));
}
private void ProcessYourid (Block yourIdBlock) private void ProcessYourid (Block yourIdBlock)
{ {
@@ -226,26 +282,55 @@ namespace WorldOfPeacecraft
string stringValue = unnamedValues.First.Value; string stringValue = unnamedValues.First.Value;
int intValue = int.Parse (stringValue); int intValue = int.Parse (stringValue);
//YourID id = new YourID (intValue); //YourID id = new YourID (intValue);
//TODO Herausfinden wie wir das oben lösen
} }
private void ProcessTime (Block block) private void ProcessTime (Block timeBlock)
{ {
LinkedList <string> unnamedValues = timeBlock.GetUnnamedValues ();
string stringValue = unnamedValues.First.Value;
long longValue = long.Parse (stringValue);
//Time t = new Time(longValue);
} }
private void ProcessOnline (Block block) private void ProcessOnline (Block onlineBlock)
{ {
LinkedList <string> unnamedValues = onlineBlock.GetUnnamedValues ();
string stringValue = unnamedValues.First.Value;
int intValue = int.Parse (stringValue);
//Online o = new Online(intValue);
} }
private void ProcessEntities (Block block) private void ProcessEntities (Block entitiesBlock)
{ {
backend.clearDragons ();
backend.clearPlayers ();
foreach (Block entityBlock in entitiesBlock.GetBlocks ()) {
switch (entityBlock.GetName()) {
case MessPlayer:
backend.SetPlayer(MapPlayer(entityBlock));
break;
case MessDragon:
backend.SetDragon(MapDragon(entityBlock));
break;
default:
ThrowUnknownBlockException(entitiesBlock, entityBlock);
break;
}
}
} }
private void ProcessPlayers (Block block) private void ProcessPlayers (Block playersBlock)
{ {
backend.clearPlayers ();
foreach (Block playerBlock in playersBlock.GetBlocks ()) {
backend.SetPlayer(MapPlayer(playerBlock));
}
} }
private void ProcessDragon (Block block) private void ProcessDragon (Block dragonBlock)
{ {
backend.SetDragon(MapDragon(dragonBlock));
} }
private Dragon MapDragon (Block dragonBlock) private Dragon MapDragon (Block dragonBlock)
@@ -298,7 +383,7 @@ namespace WorldOfPeacecraft
huntable = true; huntable = true;
break; break;
default: default:
throw new ParsingException("Unknown mapcell-property '" + prop + "'"); throw new ParsingException("Unknown mapcell property '" + prop + "'");
} }
} }
return new Tile(x,y,walkable, wall, forest, huntable, water); return new Tile(x,y,walkable, wall, forest, huntable, water);
@@ -401,22 +486,48 @@ namespace WorldOfPeacecraft
public string GetStringValue (string name) public string GetStringValue (string name)
{ {
return Values [name]; try {
return Values [name];
} catch (ArgumentOutOfRangeException e) {
throw new ParsingException("The parameter '" + name + "' does not exist in block '" + Name + "'", e);
}
} }
public int GetIntValue (string name) public int GetIntValue (string name)
{ {
return int.Parse (Values [name]); try {
return int.Parse (Values [name]);
} catch (ArgumentOutOfRangeException e) {
throw new ParsingException("The parameter '" + name + "' does not exist in block '" + Name + "'", e);
} catch (FormatException e) {
throw new ParsingException("The parameter '" + name + "' in block '" + Name + "' is not an integer (it was '" + Values [name] + "')", e);
} catch (OverflowException e) {
throw new ParsingException("The parameter '" + name + "' in blo '" + Name + "' does not fit into an integer (it was '" + Values [name] + "')", e);
}
} }
public long GetLongValue (string name) public long GetLongValue (string name)
{ {
return long.Parse (Values [name]); try {
return long.Parse (Values [name]);
} catch (ArgumentOutOfRangeException e) {
throw new ParsingException("The parameter '" + name + "' does not exist in block '" + Name + "'", e);
} catch (FormatException e) {
throw new ParsingException("The parameter '" + name + "' in block '" + Name + "' is not a long (it was '" + Values [name] + "')", e);
} catch (OverflowException e) {
throw new ParsingException("The parameter '" + name + "' in blo '" + Name + "' does not fit into a long (it was '" + Values [name] + "')", e);
}
} }
public bool GetBoolValue (string name) public bool GetBoolValue (string name)
{ {
return bool.Parse (Values [name]); try {
return bool.Parse (Values [name]);
} catch (ArgumentOutOfRangeException e) {
throw new ParsingException ("The parameter '" + name + "' does not exist in block '" + Name + "'", e);
} catch (FormatException e) {
throw new ParsingException ("The parameter '" + name + "' in block '" + Name + "' is not a bool (it was '" + Values [name] + "')", e);
}
} }
} }
} }