diff --git a/zsirozas/Deck.cs b/zsirozas/Deck.cs index 9fea4f26c0aa7fedcb1353a76db09df628c06236..7d101a424d3edeb47870520eb507a7e37ed70dde 100644 --- a/zsirozas/Deck.cs +++ b/zsirozas/Deck.cs @@ -103,7 +103,7 @@ namespace zsirozas } enum CardValue { - VI, VII, IX, X, A, F, K, I + VII, VIII, IX, X, A, F, K, I } struct Card @@ -113,7 +113,7 @@ namespace zsirozas public bool Hits(Card c) { - return this.value == CardValue.VI || this.value == c.value; + return this.value == CardValue.VII || this.value == c.value; } public override string ToString() { diff --git a/zsirozas/Form1.Designer.cs b/zsirozas/Form1.Designer.cs index c6c1f072d7614361bacc260ef6684404ca01619d..b3c655b30a7b3ecb1fdabc02df2bd13e67358752 100644 --- a/zsirozas/Form1.Designer.cs +++ b/zsirozas/Form1.Designer.cs @@ -30,6 +30,10 @@ { this.tabControl1 = new System.Windows.Forms.TabControl(); this.tabPageWelcome = new System.Windows.Forms.TabPage(); + this.label8 = new System.Windows.Forms.Label(); + this.label7 = new System.Windows.Forms.Label(); + this.textBoxIncomingPort = new System.Windows.Forms.TextBox(); + this.textBoxUserNickname = new System.Windows.Forms.TextBox(); this.buttonSearchForServers = new System.Windows.Forms.Button(); this.buttonConnect = new System.Windows.Forms.Button(); this.listBoxServers = new System.Windows.Forms.ListBox(); @@ -43,15 +47,16 @@ this.radioButtonLocalServer = new System.Windows.Forms.RadioButton(); this.radioButtonManual = new System.Windows.Forms.RadioButton(); this.tabPageServer = new System.Windows.Forms.TabPage(); + this.label6 = new System.Windows.Forms.Label(); this.buttonStartGame = new System.Windows.Forms.Button(); this.tabPageGame = new System.Windows.Forms.TabPage(); this.panelPlayers = new System.Windows.Forms.Panel(); this.panel2 = new System.Windows.Forms.Panel(); - this.label4 = new System.Windows.Forms.Label(); - this.label5 = new System.Windows.Forms.Label(); + this.labelPlayerCards2 = new System.Windows.Forms.Label(); + this.labelPlayerName2 = new System.Windows.Forms.Label(); this.panel1 = new System.Windows.Forms.Panel(); - this.label1 = new System.Windows.Forms.Label(); - this.label2 = new System.Windows.Forms.Label(); + this.labelPlayerCards1 = new System.Windows.Forms.Label(); + this.labelPlayerName1 = new System.Windows.Forms.Label(); this.panelPlayer1 = new System.Windows.Forms.Panel(); this.labelPlayerCards0 = new System.Windows.Forms.Label(); this.labelPlayerName0 = new System.Windows.Forms.Label(); @@ -66,11 +71,6 @@ this.buttonCard1 = new System.Windows.Forms.Button(); this.buttonCard0 = new System.Windows.Forms.Button(); this.buttonEndTurn = new System.Windows.Forms.Button(); - this.label6 = new System.Windows.Forms.Label(); - this.textBoxUserNickname = new System.Windows.Forms.TextBox(); - this.textBoxIncomingPort = new System.Windows.Forms.TextBox(); - this.label7 = new System.Windows.Forms.Label(); - this.label8 = new System.Windows.Forms.Label(); this.tabControl1.SuspendLayout(); this.tabPageWelcome.SuspendLayout(); this.tabPageServer.SuspendLayout(); @@ -121,6 +121,44 @@ this.tabPageWelcome.Text = "Welcome"; this.tabPageWelcome.UseVisualStyleBackColor = true; // + // label8 + // + this.label8.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.label8.AutoSize = true; + this.label8.Location = new System.Drawing.Point(441, 213); + this.label8.Name = "label8"; + this.label8.Size = new System.Drawing.Size(53, 13); + this.label8.TabIndex = 13; + this.label8.Text = "Becenév:"; + // + // label7 + // + this.label7.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.label7.AutoSize = true; + this.label7.Location = new System.Drawing.Point(441, 249); + this.label7.Name = "label7"; + this.label7.Size = new System.Drawing.Size(98, 13); + this.label7.TabIndex = 12; + this.label7.Text = "Üzenetfogadó port:"; + // + // textBoxIncomingPort + // + this.textBoxIncomingPort.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.textBoxIncomingPort.Location = new System.Drawing.Point(564, 246); + this.textBoxIncomingPort.Name = "textBoxIncomingPort"; + this.textBoxIncomingPort.Size = new System.Drawing.Size(41, 20); + this.textBoxIncomingPort.TabIndex = 11; + this.textBoxIncomingPort.Text = "6501"; + // + // textBoxUserNickname + // + this.textBoxUserNickname.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.textBoxUserNickname.Location = new System.Drawing.Point(505, 210); + this.textBoxUserNickname.Name = "textBoxUserNickname"; + this.textBoxUserNickname.Size = new System.Drawing.Size(100, 20); + this.textBoxUserNickname.TabIndex = 10; + this.textBoxUserNickname.Text = "pistike"; + // // buttonSearchForServers // this.buttonSearchForServers.Enabled = false; @@ -263,6 +301,15 @@ this.tabPageServer.Text = "Server"; this.tabPageServer.UseVisualStyleBackColor = true; // + // label6 + // + this.label6.AutoSize = true; + this.label6.Location = new System.Drawing.Point(251, 45); + this.label6.Name = "label6"; + this.label6.Size = new System.Drawing.Size(219, 26); + this.label6.TabIndex = 1; + this.label6.Text = "Jelenleg nincsnek külön szobák a szerveren.\r\nMindenki a RoomZero szoba tagja."; + // // buttonStartGame // this.buttonStartGame.Enabled = false; @@ -300,57 +347,57 @@ // // panel2 // - this.panel2.Controls.Add(this.label4); - this.panel2.Controls.Add(this.label5); + this.panel2.Controls.Add(this.labelPlayerCards2); + this.panel2.Controls.Add(this.labelPlayerName2); this.panel2.Location = new System.Drawing.Point(411, 23); this.panel2.Name = "panel2"; this.panel2.Size = new System.Drawing.Size(128, 32); this.panel2.TabIndex = 1; // - // label4 + // labelplayerCards2 // - this.label4.AutoSize = true; - this.label4.Location = new System.Drawing.Point(98, 9); - this.label4.Name = "label4"; - this.label4.Size = new System.Drawing.Size(13, 13); - this.label4.TabIndex = 0; - this.label4.Text = "0"; + this.labelPlayerCards2.AutoSize = true; + this.labelPlayerCards2.Location = new System.Drawing.Point(98, 9); + this.labelPlayerCards2.Name = "labelplayerCards2"; + this.labelPlayerCards2.Size = new System.Drawing.Size(13, 13); + this.labelPlayerCards2.TabIndex = 0; + this.labelPlayerCards2.Text = "0"; // - // label5 + // labelPlayerName2 // - this.label5.AutoSize = true; - this.label5.Location = new System.Drawing.Point(15, 9); - this.label5.Name = "label5"; - this.label5.Size = new System.Drawing.Size(45, 13); - this.label5.TabIndex = 0; - this.label5.Text = "noname"; + this.labelPlayerName2.AutoSize = true; + this.labelPlayerName2.Location = new System.Drawing.Point(15, 9); + this.labelPlayerName2.Name = "labelPlayerName2"; + this.labelPlayerName2.Size = new System.Drawing.Size(45, 13); + this.labelPlayerName2.TabIndex = 0; + this.labelPlayerName2.Text = "noname"; // // panel1 // - this.panel1.Controls.Add(this.label1); - this.panel1.Controls.Add(this.label2); + this.panel1.Controls.Add(this.labelPlayerCards1); + this.panel1.Controls.Add(this.labelPlayerName1); this.panel1.Location = new System.Drawing.Point(221, 23); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(128, 32); this.panel1.TabIndex = 1; // - // label1 + // labelPlayerCards1 // - this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(98, 9); - this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(13, 13); - this.label1.TabIndex = 0; - this.label1.Text = "0"; + this.labelPlayerCards1.AutoSize = true; + this.labelPlayerCards1.Location = new System.Drawing.Point(98, 9); + this.labelPlayerCards1.Name = "labelPlayerCards1"; + this.labelPlayerCards1.Size = new System.Drawing.Size(13, 13); + this.labelPlayerCards1.TabIndex = 0; + this.labelPlayerCards1.Text = "0"; // - // label2 + // labelPlayerName1 // - this.label2.AutoSize = true; - this.label2.Location = new System.Drawing.Point(15, 9); - this.label2.Name = "label2"; - this.label2.Size = new System.Drawing.Size(45, 13); - this.label2.TabIndex = 0; - this.label2.Text = "noname"; + this.labelPlayerName1.AutoSize = true; + this.labelPlayerName1.Location = new System.Drawing.Point(15, 9); + this.labelPlayerName1.Name = "labelPlayerName1"; + this.labelPlayerName1.Size = new System.Drawing.Size(45, 13); + this.labelPlayerName1.TabIndex = 0; + this.labelPlayerName1.Text = "noname"; // // panelPlayer1 // @@ -507,53 +554,6 @@ this.buttonEndTurn.UseVisualStyleBackColor = true; this.buttonEndTurn.Click += new System.EventHandler(this.buttonEndTurn_Click); // - // label6 - // - this.label6.AutoSize = true; - this.label6.Location = new System.Drawing.Point(251, 45); - this.label6.Name = "label6"; - this.label6.Size = new System.Drawing.Size(219, 26); - this.label6.TabIndex = 1; - this.label6.Text = "Jelenleg nincsnek külön szobák a szerveren.\r\nMindenki a RoomZero szoba tagja."; - // - // textBoxUserNickname - // - this.textBoxUserNickname.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.textBoxUserNickname.Location = new System.Drawing.Point(505, 210); - this.textBoxUserNickname.Name = "textBoxUserNickname"; - this.textBoxUserNickname.Size = new System.Drawing.Size(100, 20); - this.textBoxUserNickname.TabIndex = 10; - this.textBoxUserNickname.Text = "pistike"; - // - // textBoxIncomingPort - // - this.textBoxIncomingPort.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.textBoxIncomingPort.Location = new System.Drawing.Point(564, 246); - this.textBoxIncomingPort.Name = "textBoxIncomingPort"; - this.textBoxIncomingPort.Size = new System.Drawing.Size(41, 20); - this.textBoxIncomingPort.TabIndex = 11; - this.textBoxIncomingPort.Text = "6501"; - // - // label7 - // - this.label7.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.label7.AutoSize = true; - this.label7.Location = new System.Drawing.Point(441, 249); - this.label7.Name = "label7"; - this.label7.Size = new System.Drawing.Size(98, 13); - this.label7.TabIndex = 12; - this.label7.Text = "Üzenetfogadó port:"; - // - // label8 - // - this.label8.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.label8.AutoSize = true; - this.label8.Location = new System.Drawing.Point(441, 213); - this.label8.Name = "label8"; - this.label8.Size = new System.Drawing.Size(53, 13); - this.label8.TabIndex = 13; - this.label8.Text = "Becenév:"; - // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -618,11 +618,11 @@ private System.Windows.Forms.Label labelPlayerCards0; private System.Windows.Forms.Label labelPlayerName0; private System.Windows.Forms.Panel panel2; - private System.Windows.Forms.Label label4; - private System.Windows.Forms.Label label5; + private System.Windows.Forms.Label labelPlayerCards2; + private System.Windows.Forms.Label labelPlayerName2; private System.Windows.Forms.Panel panel1; - private System.Windows.Forms.Label label1; - private System.Windows.Forms.Label label2; + private System.Windows.Forms.Label labelPlayerCards1; + private System.Windows.Forms.Label labelPlayerName1; private System.Windows.Forms.Button buttonStartGame; private System.Windows.Forms.Label label6; private System.Windows.Forms.Label label8; diff --git a/zsirozas/Form1.cs b/zsirozas/Form1.cs index 4f7d29ac8e64e78c8ae1ef14b21d914dd1da8d2b..5d75b36ffd8352ccd81e3b310d51bb6c22445eb4 100644 --- a/zsirozas/Form1.cs +++ b/zsirozas/Form1.cs @@ -16,27 +16,44 @@ namespace zsirozas { InitializeComponent(); CardButtons = new Button[] { buttonCard0, buttonCard1, buttonCard2, buttonCard3 }; + playerNameLabels = new Label[] { labelPlayerName0, labelPlayerName1, labelPlayerName2, }; + playerCardLabels = new Label[] { labelPlayerCards0, labelPlayerCards1, labelPlayerCards2, }; client = new GameClient(); } private void buttonEndTurn_Click(object sender, EventArgs e) { - ; + if (client.EndTurn()) + { + //TODO: itt csinálok valamit? + } } private void buttonCard3_Click(object sender, EventArgs e) { - + if (client.UseCard(3)) + { + buttonCard0.Enabled = false; + buttonCard0.Visible = false; + } } private void buttonCard2_Click(object sender, EventArgs e) { - + if (client.UseCard(2)) + { + buttonCard0.Enabled = false; + buttonCard0.Visible = false; + } } private void buttonCard1_Click(object sender, EventArgs e) { - + if (client.UseCard(1)) + { + buttonCard0.Enabled = false; + buttonCard0.Visible = false; + } } private void buttonCard0_Click(object sender, EventArgs e) @@ -70,32 +87,37 @@ namespace zsirozas private void buttonStartLocalServer_Click(object sender, EventArgs e) { - throw new NotImplementedException(); + throw new NotImplementedException(); //Not in this version. } private void buttonStopLocalServer_Click(object sender, EventArgs e) { - throw new NotImplementedException(); + throw new NotImplementedException(); //Not in this version. } - private void radioButtonManual_CheckedChanged(object sender, EventArgs e) - { - + private void radioButtonManual_CheckedChanged(object sender, EventArgs e) + { + throw new NotImplementedException(); //Not in this version. } - private void radioButtonFindServers_CheckedChanged(object sender, EventArgs e) - { + private void radioButtonFindServers_CheckedChanged(object sender, EventArgs e) + { + throw new NotImplementedException(); //Not in this version. } - private void radioButtonLocalServer_CheckedChanged(object sender, EventArgs e) - { + private void radioButtonLocalServer_CheckedChanged(object sender, EventArgs e) + { + throw new NotImplementedException(); //Not in this version. } private void buttonConnect_Click(object sender, EventArgs e) - { + { + //Obtain some preferences client.nickname = textBoxUserNickname.Text; if (!int.TryParse(textBoxIncomingPort.Text, out client.serverIncoming)) { MessageBox.Show("Halihó! Ez sem egy portszám."); } + + string server_IP = null; int server_port = 0; @@ -111,11 +133,11 @@ namespace zsirozas } else if (radioButtonLocalServer.Checked) { - throw new NotImplementedException(); + throw new NotImplementedException(); //Not in this version. } else if(radioButtonFindServers.Checked) { - throw new NotImplementedException(); + throw new NotImplementedException(); //Not in this version. } //connect @@ -134,6 +156,9 @@ namespace zsirozas } } + public Label[] playerNameLabels; + public Label[] playerCardLabels; + public Button[] CardButtons; GameClient client; private void onCardsChanged() @@ -156,10 +181,15 @@ namespace zsirozas MessageBox.Show("Hoppá! Valami hibát dobott a szerver:\n" + e); } + void GameOver(object sender, string s) + { + MessageBox.Show(s); + } + void GameStateCanged(object sender, GameEvent e) { labelCardsDealt.Text = string.Join(", ", client.gameState.cardsOnTable); - labelDeckCnt.Text = client.gameState.remainsInDeck.ToString(); + labelDeckCnt.Text = client.gameState.remainsInDeck + "lap"; if (client.gameState.cardsOnTable.Length > 0) { @@ -169,10 +199,39 @@ namespace zsirozas { labelTurnCard.Text = ""; } - for (int i = 0; i < client.gameState.numofCardsInHands.Length; i++) + for (int i = 1; i < client.gameState.numofCardsInHands.Length; i++) { int idx = (i + client.playerIDX) % client.gameState.numofCardsInHands.Length; - //TODO: játékosokhoz tartozó címkék beállítása + playerNameLabels[idx].Visible = true; + playerNameLabels[idx].Visible = true; + + playerNameLabels[idx].Text = client.gameState.playersByOrder[i]; + playerCardLabels[idx].Text = client.gameState.numofCardsInHands.Length.ToString(); + if (client.gameState.startingPlayer == i) + { + playerNameLabels[idx].BackColor = Color.Cyan; + playerNameLabels[idx].BackColor = Color.Cyan; + } + else + { + playerNameLabels[idx].BackColor = Color.Transparent; + playerNameLabels[idx].BackColor = Color.Transparent; + } + if (client.gameState.lastToHit == i) + { + playerNameLabels[idx].BackColor = Color.Yellow; + playerNameLabels[idx].BackColor = Color.Yellow; + } + else + { + playerNameLabels[idx].BackColor = Color.Transparent; + playerNameLabels[idx].BackColor = Color.Transparent; + } + } + for (int i = 0; i < playerNameLabels.Length; i++) + { + playerNameLabels[i].Visible = false; + playerCardLabels[i].Visible = false; } MessageBox.Show(e.ToString()); } @@ -182,14 +241,26 @@ namespace zsirozas //TODO: ezt majd inkább neeeee...... client.roomID = "RoomZero"; + + client.gameOver += GameOver; buttonStartGame.Enabled = false; client.StartGame(); ; - } - - private void Form1_FormClosing(object sender, FormClosingEventArgs e) - { - client.SendRequest(ServerAction.Logout); - } + } + + private void Form1_FormClosing(object sender, FormClosingEventArgs e) + { + if (client.server != null) + { + try + { + client.SendRequest(ServerAction.Logout); + } + catch (Exception) + { + + } + } + } } } diff --git a/zsirozas/Game.cs b/zsirozas/Game.cs index 0ced6e76ba1b75b73938261db23fe0c8903218c4..592e5bb5b168b12547aeda295c15e78fa2347072 100644 --- a/zsirozas/Game.cs +++ b/zsirozas/Game.cs @@ -7,6 +7,7 @@ namespace zsirozas { class Game { + public string gameID; public Server gameServer; public Dictionary<string, Player> playersByID; public string[] playersByOrder; @@ -24,7 +25,6 @@ namespace zsirozas message += "\n" + PublicState().ToString(); break; case GameEvent.GameOver: - //TODO: compose GameOver message var zsir_lapok = new CardValue[] { CardValue.X, CardValue.I }; var zsir_cnt = new int[playersByOrder.Length]; for (int i = 0; i < playersByOrder.Length; i++) @@ -37,7 +37,11 @@ namespace zsirozas } } } - var izteurdsa = Enumerable.Range(0, playersByOrder.Length).OrderByDescending((arg) => zsir_cnt[arg]); + var playersByScore = Enumerable.Range(0, playersByOrder.Length) + .OrderByDescending((arg) => zsir_cnt[arg]) + .Select((arg) => new { ID = playersByOrder[arg], score = zsir_cnt[arg] }) + .ToArray(); + message += (new JavaScriptSerializer().Serialize(playersByScore)); break; default: break; @@ -48,11 +52,14 @@ namespace zsirozas } } - public Game(Server _gameServer, IEnumerable<string> _playerIDs) + public Game(Server _gameServer, IEnumerable<string> _playerIDs, string _gameID) { //reference the server gameServer = _gameServer; + //reference self + gameID = _gameID; + //create player indices playersByOrder = _playerIDs.ToArray(); playersByID = new Dictionary<string, Player>(); @@ -104,7 +111,7 @@ namespace zsirozas //determine next up // int nextUp = Array.IndexOf(playersByOrder, playerID) + 1 % playersByOrder.Length; - while (playersByID[playersByOrder[currentRound.nextUp]].cardsInHand.Count == playersByID[playerID].cardsInHand.Count) + while (playersByID[playersByOrder[currentRound.nextUp]].cardsInHand.Count == playersByID[playerID].cardsInHand.Count && playersByOrder[currentRound.nextUp] != playerID) { currentRound.nextUp = (currentRound.nextUp + 1) % playersByOrder.Length; NotifyPlayers(GameEvent.CardAction); @@ -123,7 +130,7 @@ namespace zsirozas if (playersByOrder[currentRound.nextUp] == playerID) { //Has this turn tecnically ended? - if (playersByOrder[currentRound.lastToHit] == playerID) + if (playersByOrder[currentRound.lastToHit] == playerID && currentRound.playedCards.Count > 0) { Complain("Objection! You already won this round!"); } @@ -151,14 +158,14 @@ namespace zsirozas { if (playerID == playersByOrder[currentRound.startingPlayer]) { - + //transfer playedCards to winner playersByID[playersByOrder[currentRound.lastToHit]].cardsWon.AddRange(currentRound.playedCards); //check for endGame condition if (deck.cardsDealtSoFar == deck.IDsByOrder.Count) { - NotifyPlayers(GameEvent.GameOver); + EndGame(); } else { @@ -212,10 +219,11 @@ namespace zsirozas public void EndGame() { NotifyPlayers(GameEvent.GameOver); - throw new NotImplementedException(); + + throw new GameOverException(this.gameID); } - public MidGameState PublicState() + public MidGameState PublicState() { return new MidGameState() { @@ -224,9 +232,9 @@ namespace zsirozas nextUp = currentRound.nextUp, lastToHit = currentRound.lastToHit, startingPlayer = currentRound.startingPlayer, - remainsInDeck = deck.IDsByOrder.Count - deck.cardsDealtSoFar - - }; + remainsInDeck = deck.IDsByOrder.Count - deck.cardsDealtSoFar, + playersByOrder = playersByOrder, + }; } public struct MidGameState @@ -239,27 +247,23 @@ namespace zsirozas public int remainsInDeck; public override string ToString() { - //TODO: ennek itt a kimeneti formátuma micsoda? return new JavaScriptSerializer().Serialize(this); } public static MidGameState Parse(string s) { - //TODO: string-ből, vagy Stream-ről egyszerűbb olvasni? return new JavaScriptSerializer().Deserialize<MidGameState>(s); } + //TODO: ezt a részt átdobni majd a játékszobás logikához + public string[] playersByOrder; } - public class InvalidActionException : Exception - { - public InvalidActionException(string message) : base(message) { } - } - //public class ServerNotifiyingException:Exception - //{ - // public GameEvent _gameEvent; - // public string _userID; - // public ServerNotifiyingException(GameEvent @event, string userID, string message) - // { + } + public class InvalidActionException : Exception + { + public InvalidActionException(string message) : base(message) { } + } - // } - //} + public class GameOverException : Exception + { + public GameOverException(string message) : base(message) { } } } \ No newline at end of file diff --git a/zsirozas/GameClient.cs b/zsirozas/GameClient.cs index 36a90e1231a3a9e3d56499ab06ea6cb1d84b2a2b..51c170da741272896d68bfa9e8613e46a32b87d8 100644 --- a/zsirozas/GameClient.cs +++ b/zsirozas/GameClient.cs @@ -38,7 +38,7 @@ namespace zsirozas } return true; } - catch (Exception ex) + catch (Exception) { return false; } @@ -70,7 +70,7 @@ namespace zsirozas return true; } } - catch (Exception ex) { } + catch (Exception) { } } return false; @@ -88,15 +88,15 @@ namespace zsirozas public string gameID; public int playerIDX; - TcpListener incomingListener = null; + TcpListener incomingListener = null; Stream messageStream = null; - public bool SendRequest(ServerAction action, string @params = null) + public bool SendRequest(ServerAction action, string @params = null) { var tempConn = new TcpClient(); tempConn.Connect(server); using (var sw = new StreamWriter(tempConn.GetStream())) - using (var sr = new StreamReader(tempConn.GetStream())) + using (var sr = new StreamReader(tempConn.GetStream())) { sw.WriteLine(); string status = null; @@ -108,6 +108,7 @@ namespace zsirozas sw.Flush(); if ((status = sr.ReadLine()) != "OK") { + status = sr.ReadLine(); handleServerError(this, status); return false; } @@ -122,6 +123,7 @@ namespace zsirozas sw.Flush(); if ((status = sr.ReadLine()) != "OK") { + status = sr.ReadLine(); handleServerError(this, status); return false; } @@ -135,6 +137,7 @@ namespace zsirozas sw.Flush(); if ((status = sr.ReadLine()) != "OK") { + status = sr.ReadLine(); handleServerError(this, status); return false; } @@ -149,6 +152,7 @@ namespace zsirozas //TODO: ez a getCards idegesítően különc... if ((status = sr.ReadLine()) != "OK") { + status = sr.ReadLine(); handleServerError(this, status); return false; } @@ -170,6 +174,7 @@ namespace zsirozas sw.Flush(); if ((status = sr.ReadLine()) != "OK") { + status = sr.ReadLine(); handleServerError(this, status); return false; } @@ -183,6 +188,7 @@ namespace zsirozas sw.Flush(); if ((status = sr.ReadLine()) != "OK") { + status = sr.ReadLine(); handleServerError(this, status); return false; } @@ -202,9 +208,9 @@ namespace zsirozas return true; } - public void StartGame() + public void StartGame() { - SendRequest(ServerAction.StartGame, roomID); + SendRequest(ServerAction.StartGame, roomID); } public void JoinRoom(string roomID) @@ -212,7 +218,7 @@ namespace zsirozas SendRequest(ServerAction.JoinRoom, roomID); } - void ListenForServer() + void ListenForServer() { using (var sr = new StreamReader(messageStream)) { @@ -221,7 +227,7 @@ namespace zsirozas { try { - while (line == "") + while (string.IsNullOrEmpty(line)) { line = sr.ReadLine(); } @@ -237,27 +243,41 @@ namespace zsirozas { case GameEvent.NewTurn: gameState = new JavaScriptSerializer().Deserialize<Game.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 + "\n"); + SendRequest(ServerAction.GameAction, PlayerAction.GetCards + "\n0"); break; case GameEvent.CardAction: gameState = new JavaScriptSerializer().Deserialize<Game.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 ex) + catch (Exception) { - ; + line = sr.ReadLine(); + continue; } } } @@ -274,6 +294,8 @@ namespace zsirozas /// </summary> public event Action cardsChanged; + public event EventHandler<string> gameOver; + public void updateCards() { //TODO: elkérni a szervertől a kártyákat @@ -288,13 +310,19 @@ namespace zsirozas public bool EndTurn() { - return SendRequest(ServerAction.GameAction, PlayerAction.EndTurn + "\n"); + return SendRequest(ServerAction.GameAction, PlayerAction.EndTurn + "\n0"); } public Game.MidGameState gameState; public event EventHandler<GameEvent> gameStateCanged; - + //névtelen típusra cast-oláshoz + private static T CastByExample<T>(T typeHolder, Object x) + { + // typeHolder above is just for compiler magic + // to infer the type to cast x to + return (T)x; + } } -} \ No newline at end of file +} diff --git a/zsirozas/Round.cs b/zsirozas/Round.cs index 789ed647b62e57397146a2acd9792664aeab5605..fe2c279ebbe5e44b4508e5e4a1696675a44853b3 100644 --- a/zsirozas/Round.cs +++ b/zsirozas/Round.cs @@ -3,10 +3,6 @@ using System.Collections.Generic; namespace zsirozas { - //enum PlayerState - //{ - // Hitting, NonHitting - //} enum PlayerAction { Card, EndTurn, GetCards //players should query their cards themselves @@ -35,16 +31,6 @@ namespace zsirozas { playerID = _playerID; } - - /// <summary> - /// DEPRECATED! - /// </summary> - public void Invoke() - { - //jelzést küld valahogy.... - Console.WriteLine(playerID + ", you're up"); - } - } struct Round diff --git a/zsirozas/Server.cs b/zsirozas/Server.cs index 5f58ccd5205eb2362db30f45a91f3f6d4ef7db20..d075da43f9a815a99ad2485f6f12bd46176cf1c5 100644 --- a/zsirozas/Server.cs +++ b/zsirozas/Server.cs @@ -54,61 +54,88 @@ namespace zsirozas public void ParseNextMessage(Stream s, object param) { - //TODO: eztet rendesen - using (var sr = new StreamReader(s)) using (var sw = new StreamWriter(s)) { - string line = ""; while (line == "") { line = sr.ReadLine(); } - var action = (ServerAction)Enum.Parse(typeof(ServerAction), line); + if (!Enum.TryParse<ServerAction>(line, out ServerAction action)) + { + action = ServerAction.Invalid; + } switch (action) { case ServerAction.GameAction: //a következő sor a gameID line = sr.ReadLine(); - //TODO: rájönni, miért nullt ad a kliens gameID helyett + + //TODO: rájönni, miért néha nullt ad a kliens gameID helyett if (line == "") line = "G~RoomZero"; if (Games.Contains(line)) { //TODO: validálni? vagy kivételt elkapni? - string playerID = sr.ReadLine(); - string playerAction = sr.ReadLine(); - string cardID = sr.ReadLine(); - //TODO: rájönni hogy miért ad néha null-t cardID helyett - if (cardID == "") cardID = "0"; - string response = GamesByID[line].Action( - //playerID - playerID, - //action - (PlayerAction)Enum.Parse(typeof(PlayerAction), playerAction), - //cardID, nem hagyható el (de nem mindig értelmes) - int.Parse(cardID) - ); - sw.WriteLine("OK"); - sw.WriteLine(response); + try + { + string playerID = sr.ReadLine(); + string playerAction = sr.ReadLine(); + string cardID = sr.ReadLine(); + //TODO: rájönni hogy miért ad néha null-t cardID helyett + if (cardID == "") cardID = "0"; + string response = GamesByID[line].Action( + //playerID + playerID, + //action + (PlayerAction)Enum.Parse(typeof(PlayerAction), playerAction), + //cardID, nem hagyható el (de nem mindig értelmes) + int.Parse(cardID) + ); + sw.WriteLine("OK"); + sw.WriteLine(response); + } + catch (FormatException) + { + sw.WriteLine("ERR"); + } + catch (InvalidActionException ex) + { + sw.WriteLine("ERR"); + sw.WriteLine(ex.Message); + } + catch (GameOverException ex) + { + sw.WriteLine("OK"); + Console.WriteLine("[LOG] Game " + ex.Message + "ended."); + GamesByID.Remove(ex.Message); + Games.Remove(ex.Message); + //TODO: ezt majd kivennim amikor teljes lesz a játékszobás rész + Console.WriteLine("First game has ended, shutting down..."); + Shutdown(); + } } break; case ServerAction.Ping: - //TODO: egyéb ellenőrzés ping során? sw.WriteLine("OK"); break; case ServerAction.ListRooms: - //TODO: szobákat kilistázni - throw new NotImplementedException(); + throw new NotImplementedException(); //Not in this version. case ServerAction.Login: line = sr.ReadLine(); - //TODO: validálni! - string ID = CreateUser(line, (IPAddress)param, int.Parse(sr.ReadLine())); - sw.WriteLine("OK"); - sw.WriteLine(ID); + try + { + string ID = CreateUser(line, (IPAddress)param, int.Parse(sr.ReadLine())); + sw.WriteLine("OK"); + sw.WriteLine(ID); + } + catch (Exception) + { + sw.WriteLine("ERR"); + } break; case ServerAction.Logout: line = sr.ReadLine(); @@ -132,12 +159,22 @@ namespace zsirozas sw.WriteLine("OK"); break; case ServerAction.InviteUser: - //nem része az alapnak - throw new NotSupportedException(); + throw new NotImplementedException(); //Not in this version. case ServerAction.StartGame: - var newgame = NewGame(sr.ReadLine()); - sw.WriteLine("OK"); - sw.WriteLine(newgame); + try + { + var newgame = NewGame(sr.ReadLine()); + sw.WriteLine("OK"); + sw.WriteLine(newgame); + } + catch (Exception) + { + sw.WriteLine("ERR"); + } + break; + case ServerAction.Invalid: + sw.WriteLine("ERR"); + sw.WriteLine("Hey! \"" + line + "\" is not a valid request"); break; default: break; @@ -147,7 +184,13 @@ namespace zsirozas } - // public void MessageUser(string userID, ServerAction action, string message) { } + private void Shutdown() + { + Console.WriteLine("Server shut down succesfully."); + System.Environment.Exit(0); + } + + //different kinds of actions /// <summary> @@ -162,6 +205,7 @@ namespace zsirozas RoomsByID.Add(r.roomID, r); Rooms.Add(r.roomID); UsersByID[userID].roomID = r.roomID; + Console.WriteLine("[LOG] Room "+r.roomID+" has been created."); return r.roomID; } @@ -188,11 +232,12 @@ namespace zsirozas U.msgConn.Connect(IP, port); U.messageStreamW = new StreamWriter(U.msgConn.GetStream()); + Console.WriteLine("[LOG] " + U.userID + " logged in"); + if (bigRoom) { JoinRoom(U.userID, Rooms[0]); } - return U.userID; } @@ -204,6 +249,7 @@ namespace zsirozas UsersByID[userID].msgConn.Close(); UsersByID.Remove(userID); Users.Remove(userID); + Console.WriteLine("[LOG] " + userID + " logged out"); } public void LeaveRoom(string userID) { @@ -211,10 +257,12 @@ namespace zsirozas { //TODO: validálni ezt a mindent RoomsByID[UsersByID[userID].roomID].userList.Remove(userID); + Console.WriteLine("[LOG] " + userID + " left room " + UsersByID[userID].roomID); if (!bigRoom && RoomsByID[UsersByID[userID].roomID].userList.Count == 0) { RoomsByID.Remove(UsersByID[userID].roomID); Rooms.Remove(UsersByID[userID].roomID); + Console.WriteLine("[LOG] Empty room "+UsersByID[userID].roomID+" has been destroyed"); } UsersByID[userID].roomID = null; } @@ -227,6 +275,7 @@ namespace zsirozas { UsersByID[userID].roomID = roomID; RoomsByID[roomID].userList.Add(userID); + Console.WriteLine("[LOG] " + userID + " joined room " + UsersByID[userID].roomID); //TODO: akarok-e erről üzenni? } else throw new NotImplementedException(); @@ -235,18 +284,18 @@ namespace zsirozas } public string NewGame(string roomID) { - var G = new Game(this, RoomsByID[roomID].userList); string gameID = "G~" + roomID; + var G = new Game(this, RoomsByID[roomID].userList, gameID); GamesByID.Add(gameID, G); Games.Add(gameID); + Console.WriteLine("[LOG] New game " + gameID); return gameID; - //TODO: üzenetsorrend hogy történik? } } enum ServerAction { - GameAction, StartGame, Ping, ListRooms, Login, Logout, CreateRoom, LeaveRoom, JoinRoom, InviteUser + Invalid = -1, GameAction, StartGame, Ping, ListRooms, Login, Logout, CreateRoom, LeaveRoom, JoinRoom, InviteUser } enum ServerEvent