diff --git a/Message.cs b/Message.cs new file mode 100644 index 0000000..d300666 --- /dev/null +++ b/Message.cs @@ -0,0 +1,12 @@ +using System; + +namespace inf3 +{ + public class Message + { + public Message () + { + } + } +} + diff --git a/inf3.csproj b/inf3.csproj index d772698..4cd9bd5 100644 --- a/inf3.csproj +++ b/inf3.csproj @@ -60,6 +60,8 @@ + + \ No newline at end of file diff --git a/src/Backend.cs b/src/Backend.cs index cae9716..f78799a 100644 --- a/src/Backend.cs +++ b/src/Backend.cs @@ -41,6 +41,16 @@ namespace Frontend { Players.Remove (player.GetId ()); } + + public void clearPlayers () + { + Players.Clear (); + } + + public void clearDragons () + { + Dragons.Clear (); + } public Player getPlayer (int playerId) { diff --git a/src/Message.cs b/src/Message.cs new file mode 100644 index 0000000..277dc88 --- /dev/null +++ b/src/Message.cs @@ -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; + } + } +} + diff --git a/src/Opponent.cs b/src/Opponent.cs new file mode 100644 index 0000000..56f1b18 --- /dev/null +++ b/src/Opponent.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/src/Parser.cs b/src/Parser.cs index 228f633..fbb3bd2 100644 --- a/src/Parser.cs +++ b/src/Parser.cs @@ -30,6 +30,8 @@ namespace WorldOfPeacecraft private Regex LastLineRegex; private Backend backend; + enum Decision {DRAGON, STAGHUNT, SKIRMISH}; + public Parser () { ParserThread = new Thread (new ThreadStart (this.RunParser)); @@ -56,15 +58,24 @@ namespace WorldOfPeacecraft if (IsCompletePackage ()) { Parse (); } - // TODO Try-catch. IMPORTANT! } } private void Parse () { - String[] aMessage = Enumerable.ToArray (Message); - Block mainBlock = new Block (aMessage, 0, aMessage.Length - 1); - ProcessData (mainBlock); + string[] aMessage = Enumerable.ToArray (Message); + Message.Clear(); + 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) @@ -153,6 +164,9 @@ namespace WorldOfPeacecraft Dragon dragon = MapDragon (block); backend.removeDragon (dragon); break; + default: + ThrowUnknownBlockException(deleteBlock, block); + break; } } @@ -179,11 +193,13 @@ namespace WorldOfPeacecraft int srcid = mesBlock.GetIntValue ("srcid"); string src = mesBlock.GetStringValue ("src"); 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) { + // TODO } private void ProcessResult (Block procBlock) @@ -192,19 +208,56 @@ namespace WorldOfPeacecraft int round = procBlock.GetIntValue ("round"); bool running = procBlock.GetBoolValue ("running"); int delay = procBlock.GetIntValue ("delay"); - //ProcessOpponent (procBlock.GetBlocks ()); + LinkedList block = procBlock.GetBlocks (); + ProcessOpponent (block.First.Value); //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 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) @@ -217,8 +270,11 @@ namespace WorldOfPeacecraft int y = playerBlock.GetIntValue ("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) { @@ -226,26 +282,55 @@ namespace WorldOfPeacecraft string stringValue = unnamedValues.First.Value; int intValue = int.Parse (stringValue); //YourID id = new YourID (intValue); + //TODO Herausfinden wie wir das oben lösen } - private void ProcessTime (Block block) + private void ProcessTime (Block timeBlock) { + LinkedList 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 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) @@ -298,7 +383,7 @@ namespace WorldOfPeacecraft huntable = true; break; 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); @@ -401,22 +486,48 @@ namespace WorldOfPeacecraft 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) { - 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) { - 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) { - 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); + } } } }