diff --git a/zsirozas/Common.cs b/zsirozas/Common.cs index db51f0a86b8268c94b66ffb1e041a3dec7e3dcb0..906a844160a436f5974e565e4fea317cefbc065d 100644 --- a/zsirozas/Common.cs +++ b/zsirozas/Common.cs @@ -28,7 +28,7 @@ namespace zsirozas //szobák: létrehoz, belép, kilép, listáz (név, tagok, vezér, ), játékindít //játékosok: meghív, listáz void ParseNextMessage(Stream s, object param); - void GameAction(string gameID, string userID, PlayerAction action, int cardID); + object GameAction(string gameID, string userID, PlayerAction action, int cardID); } //ahogy a szerver látja (és kezeli) a klienst @@ -37,12 +37,18 @@ namespace zsirozas interface IAppClient { void SimpleNotify(ServerEvent @event, string param0); - void InGameNotify(GameEvent @event, object param0); + /// <summary> + /// Send a message specific to the game context. + /// </summary> + /// <param name="event">The GameEvent you want to broadcast.</param> + /// <param name="param0">The parameter of the call. It MUST be a JSON string</param> + void InGameNotify(GameEvent @event, string param0); /// <summary> /// Deprecated! Only use while transitioning from old! /// </summary> /// <param name="s">a message that will be written to the network stream</param> void SendMessage(string s); + void ParseNextMessage(Stream s, object param); bool ValidateConnection(object connInfo); } diff --git a/zsirozas/GameClient.cs b/zsirozas/GameClient.cs index e1e4271dcdc17a239ce2a3619ad9cd31a4fb2e3a..1aa94f4621e8ecd05a810bf8a896f2833bded447 100644 --- a/zsirozas/GameClient.cs +++ b/zsirozas/GameClient.cs @@ -10,7 +10,7 @@ using System.Web.Script.Serialization; namespace zsirozas { - class GameClient + class GameClient : IAppClient { public GameClient() { @@ -66,7 +66,7 @@ namespace zsirozas incomingListener.Start(); //if (SendRequest(ServerAction.Login, nickname + "\n" + ((IPEndPoint)incomingListener.Server.LocalEndPoint).Port)) userID = remoteServer.CreateUser(nickname, ConnectionType.RawTCP, ((IPEndPoint)incomingListener.Server.LocalEndPoint).Port); - if(!string.IsNullOrEmpty(userID)) + if (!string.IsNullOrEmpty(userID)) { messageStream = incomingListener.AcceptTcpClient().GetStream(); new Thread(ListenForServer) { IsBackground = true }.Start(); @@ -77,7 +77,7 @@ namespace zsirozas return true; } } - catch (ServerErrorException ex) + catch (ServerErrorException ex) { handleServerError(this, ex.Message); } @@ -121,70 +121,187 @@ namespace zsirozas void ListenForServer() { - using (var sr = new StreamReader(messageStream)) + try { - string line = ""; while (true) { - try + ParseNextMessage(messageStream, null); + } + } + catch (Exception ex) + { + return; + } + //using (var sr = new StreamReader(messageStream)) + //{ + // string line = ""; + // while (true) + // { + // try + // { + // while (string.IsNullOrEmpty(line)) + // { + // line = sr.ReadLine(); + // } + // var @event = (ServerEvent)Enum.Parse(typeof(ServerEvent), line); + // switch (@event) + // { + // case ServerEvent.GameEvent: + // //TODO: validálni + // line = sr.ReadLine(); + // GameEvent gameEvent; + // Enum.TryParse<GameEvent>(line, out gameEvent); + // switch (gameEvent) + // { + // case GameEvent.NewTurn: + // gameState = new JavaScriptSerializer().Deserialize<MidGameState>(sr.ReadLine()); + + // //TODO: ezt majd valahol máshol nézegetni... + // playerIDX = Array.IndexOf<string>(gameState.playersByOrder, userID); + + // gameStateCanged(this, gameEvent); + // //SendRequest(ServerAction.GameAction, PlayerAction.GetCards + "\n0"); + // remoteServer.GameAction(gameID, userID, PlayerAction.GetCards, 0); cardsChanged(); + // break; + // case GameEvent.CardAction: + // gameState = new JavaScriptSerializer().Deserialize<MidGameState>(sr.ReadLine()); + // gameStateCanged(this, gameEvent); + // break; + // case GameEvent.GameOver: + // string msg = sr.ReadLine(); + // var dummy = new { ID = "", score = 0 }; + // var scores = new JavaScriptSerializer() + // .Deserialize<IEnumerable<object>>(msg) + // .Select((arg) => CastByExample(dummy, arg)); + // msg = string.Join("\n", scores.Select((arg) => arg.ID + "\t" + arg.score).ToArray()); + // gameOver(this, "A játéknak vége!\n" + msg); + // break; + // default: + // break; + // } + // break; + // case ServerEvent.Logout: + // return; + + // default: + // break; + // } + // } + // catch (Exception) + // { + // line = sr.ReadLine(); + // continue; + // } + // } + //} + } + + + public void SimpleNotify(ServerEvent @event, string param0) + { + switch (@event) + { + case ServerEvent.GameEvent: + throw new NotSupportedException("You seem to be mistaken. GameEvents are handled by InGameNotify(), not SimpleNotify()"); + case ServerEvent.Logout: + throw new ThreadInterruptedException(); //TODO: ez-e a kivétel, amit használni akartam? + case ServerEvent.Invitation: + throw new NotImplementedException(); + case ServerEvent.Notice: + throw new NotImplementedException(); + case ServerEvent.Ping: + throw new NotImplementedException(); + default: + break; + } + throw new NotImplementedException(); + } + + public void ParseNextMessage(Stream s, object param) + { + using (var sr = new StreamReader(s)) + { + try + { + string line = ""; + while (string.IsNullOrEmpty(line)) { - while (string.IsNullOrEmpty(line)) - { - line = sr.ReadLine(); - } - var @event = (ServerEvent)Enum.Parse(typeof(ServerEvent), line); - switch (@event) - { - case ServerEvent.GameEvent: - //TODO: validálni - line = sr.ReadLine(); - GameEvent gameEvent; - Enum.TryParse<GameEvent>(line, out gameEvent); - switch (gameEvent) - { - case GameEvent.NewTurn: - gameState = new JavaScriptSerializer().Deserialize<MidGameState>(sr.ReadLine()); - - //TODO: ezt majd valahol máshol nézegetni... - playerIDX = Array.IndexOf<string>(gameState.playersByOrder, userID); - - gameStateCanged(this, gameEvent); - //SendRequest(ServerAction.GameAction, PlayerAction.GetCards + "\n0"); - remoteServer.GameAction(gameID, userID, PlayerAction.GetCards, 0); cardsChanged(); - break; - case GameEvent.CardAction: - gameState = new JavaScriptSerializer().Deserialize<MidGameState>(sr.ReadLine()); - gameStateCanged(this, gameEvent); - break; - case GameEvent.GameOver: - string msg = sr.ReadLine(); - var dummy = new { ID = "", score = 0 }; - var scores = new JavaScriptSerializer() - .Deserialize<IEnumerable<object>>(msg) - .Select((arg) => CastByExample(dummy, arg)); - msg = string.Join("\n", scores.Select((arg) => arg.ID + "\t" + arg.score).ToArray()); - gameOver(this, "A játéknak vége!\n" + msg); - break; - default: - break; - } - break; - case ServerEvent.Logout: - return; - - default: - break; - } + line = sr.ReadLine(); } - catch (Exception) + var @event = (ServerEvent)Enum.Parse(typeof(ServerEvent), line); + if (@event == ServerEvent.GameEvent) { - line = sr.ReadLine(); - continue; + ParseGameMessage(sr, param); + } + else + { + SimpleNotify(@event, sr.ReadLine()); //TODO: akarom-e az "egysorba mindent" formátumot? } } + catch (Exception ex) + { + sr.ReadLine(); + if (ex is ThreadInterruptedException) + { + throw; + } + else + { + handleServerError(this, ex.Message); + return; + } + } + } + } + private void ParseGameMessage(StreamReader sr, object param) + { + //TODO: validálni + string line = sr.ReadLine(); + GameEvent gameEvent; + Enum.TryParse(line, out gameEvent); + InGameNotify(gameEvent, sr.ReadLine()); + } + + public void InGameNotify(GameEvent @event, string param0) + { + var ser = new JavaScriptSerializer(); + switch (@event) + { + case GameEvent.NewTurn: + gameState = ser.Deserialize<MidGameState>(param0); + + //TODO: ezt majd valahol máshol nézegetni... + playerIDX = Array.IndexOf<string>(gameState.playersByOrder, userID); + + gameStateCanged(this, @event); + //SendRequest(ServerAction.GameAction, PlayerAction.GetCards + "\n0"); + Cards = remoteServer.GameAction(gameID, userID, PlayerAction.GetCards, 0) as Card[]; + cardsChanged(); + break; + case GameEvent.CardAction: + gameState = ser.Deserialize<MidGameState>(param0); + gameStateCanged(this, @event); + break; + case GameEvent.GameOver: + var dummy = new { ID = "", score = 0 }; + var scores = ser.Deserialize<IEnumerable<object>>(param0).Select((arg) => CastByExample(dummy, arg)); + string msg = string.Join("\n", scores.Select((arg) => arg.ID + "\t" + arg.score).ToArray()); + gameOver(this, "A játéknak vége!\n" + msg); + break; + default: + break; } } + public void SendMessage(string s) + { + throw new NotImplementedException(); + } + + public bool ValidateConnection(object connInfo) + { + return true; //We know who we are. + } // // in-game stuff @@ -245,6 +362,7 @@ namespace zsirozas // to infer the type to cast x to return (T)x; } + } diff --git a/zsirozas/GameServer.cs b/zsirozas/GameServer.cs index 5be62b7365ddc49f8bafa5eaaf14635531149aba..54d4db28866b0076813671c236f221b6e52b74b7 100644 --- a/zsirozas/GameServer.cs +++ b/zsirozas/GameServer.cs @@ -318,7 +318,7 @@ namespace zsirozas } } - public void GameAction(string gameID, string userID, PlayerAction getCards, int v) + public object GameAction(string gameID, string userID, PlayerAction action, int cardID) { //TODO: ezt rendesen throw new NotImplementedException(); diff --git a/zsirozas/RawTcpTransport.cs b/zsirozas/RawTcpTransport.cs index 2c3480e77232799ada2e6719f12434ae56063b8d..cbe77bf9d730e02e05ca0086e7393d59bb354e36 100644 --- a/zsirozas/RawTcpTransport.cs +++ b/zsirozas/RawTcpTransport.cs @@ -31,7 +31,7 @@ namespace zsirozas messageStreamW = new StreamWriter(msgConn.GetStream()); } - public void InGameNotify(GameEvent @event, object param0) + public void InGameNotify(GameEvent @event, string param0) { messageStreamW.WriteLine(ServerEvent.GameEvent); messageStreamW.WriteLine(@event); @@ -64,6 +64,10 @@ namespace zsirozas return false; } + public void ParseNextMessage(Stream s, object param) + { + throw new NotSupportedException("This is a proxy to the real app client. You shouldn't want to feed it message streams."); + } } // @@ -200,7 +204,8 @@ namespace zsirozas status = sr.ReadLine(); throw new ServerErrorException(status); } - throw new NotImplementedException(); + status = sr.ReadLine(); + return status.Split(' ').ToArray(); } } @@ -217,7 +222,7 @@ namespace zsirozas sw.WriteLine(ServerAction.StartGame); //sw.WriteLine(userID); //TODO: hoppá! erre nem gondoltam - sw.WriteLine(); + sw.WriteLine(roomID); sw.Flush(); if ((status = sr.ReadLine()) != "OK") { @@ -257,7 +262,7 @@ namespace zsirozas } } } - + //TODO: ezen ronda a control-flow public object GameAction(string gameID, string userID, PlayerAction action, int cardID) { var tempConn = new TcpClient(); @@ -359,25 +364,6 @@ namespace zsirozas } } - void IServerApiProvider.GameAction(string gameID, string userID, PlayerAction getCards, int v) - { - var tempConn = new TcpClient(); - tempConn.Connect(server); - using (var sw = new StreamWriter(tempConn.GetStream())) - using (var sr = new StreamReader(tempConn.GetStream())) - { - sw.WriteLine(); - string status = null; - sw.WriteLine(ServerAction.GameAction); - - sw.Flush(); - if ((status = sr.ReadLine()) != "OK") - { - status = sr.ReadLine(); - throw new ServerErrorException(status); - } - } - } //public bool SendRequest(string userID, ServerAction action, string @params = null) //{ // var tempConn = new TcpClient(); diff --git a/zsirozas/ServerWrapper.cs b/zsirozas/ServerWrapper.cs index aa2eccda721e16983a523268886ff7097d452cc5..01472a6dfb5be189babe3b6e4b2097ee27cc9f19 100644 --- a/zsirozas/ServerWrapper.cs +++ b/zsirozas/ServerWrapper.cs @@ -10,7 +10,6 @@ namespace zsirozas //TODO: biztosítani a szerver adatkonzisztenciáját class ServerWrapper { - //TODO: eleintézni hogy "szépen" megkapja a referenciát public IServerApiProvider serverInstance; public ServerWrapper(IServerApiProvider _serverInstance) diff --git a/zsirozas/zsirozas.csproj b/zsirozas/zsirozas.csproj index 7a9418eef06b46be398315786f287f32b2b255aa..28833b30387e2e5c90a842234513093042b81586 100644 --- a/zsirozas/zsirozas.csproj +++ b/zsirozas/zsirozas.csproj @@ -58,6 +58,7 @@ </ItemGroup> <ItemGroup> <None Include="app.config" /> + <None Include="ClassDiagram1.cd" /> </ItemGroup> <ItemGroup> <EmbeddedResource Include="Form1.resx">