private void cardOut(CardControl card) { this.Dispatcher.Invoke(new Action(delegate { int direction = Convert.ToInt32(card.Owner); double left = 0; double top = 0; switch (direction) { case 0: left = Canvas.GetLeft(this.contractLabel) + this.contractLabel.ActualWidth / 2 - 50; top = Canvas.GetTop(this.contractLabel) + this.contractLabel.ActualHeight + 10; break; case 1: left = Canvas.GetLeft(this.contractLabel) - 110; top = Canvas.GetTop(this.contractLabel) + this.contractLabel.ActualHeight / 2 - 75; break; case 2: left = Canvas.GetLeft(this.contractLabel) + this.contractLabel.ActualWidth / 2 - 50; top = Canvas.GetTop(this.contractLabel) - 160; break; case 3: left = Canvas.GetLeft(this.contractLabel) + this.contractLabel.ActualWidth + 10; top = Canvas.GetTop(this.contractLabel) + this.contractLabel.ActualHeight / 2 - 75; break; } Storyboard s = new Storyboard(); DoubleAnimation da1 = new DoubleAnimation(Canvas.GetLeft(card), left, new Duration(TimeSpan.FromMilliseconds(500))); Storyboard.SetTarget(da1, card); Storyboard.SetTargetProperty(da1, new PropertyPath("(Canvas.Left)")); s.Children.Add(da1); DoubleAnimation da2 = new DoubleAnimation(Canvas.GetTop(card), top, new Duration(TimeSpan.FromMilliseconds(500))); Storyboard.SetTarget(da2, card); Storyboard.SetTargetProperty(da2, new PropertyPath("(Canvas.Top)")); s.Children.Add(da2); this.outCards.Add(card); s.Begin(); showCardInHand(direction); })); }
/// <summary> /// remove the card from player's hand and put it on the desk /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void selectCard(object sender, RoutedEventArgs e) { CardControl card = (CardControl)sender; this.Dispatcher.Invoke(new Action(delegate { this.tipsLabel.Content = ""; card.MouseDoubleClick -= selectCard; Play m = new Play(); m.Who = (Player)Convert.ToInt32(card.Owner); Card c = new Card(); c.Suit = (Suit)Suit2Encode(card.Suit); c.Rank = (uint)Number2Encode(card.Number); m.Card = c; try { this.socket.Send(m.ToByteArray()); } catch (Exception ex) { } this.ContinueButton.Click += Button_Click_1; for (int i = 0; i < 13; i++) { this.CardUI[Convert.ToInt32(card.Owner), i].MouseDoubleClick -= selectCard; } })); }
private static void OnOwnerChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d != null && d is CardControl) { CardControl cc = d as CardControl; cc.Owner = (string)e.NewValue; } }
/// <summary> /// <para>clear the card on the desk</para> /// <para>used in timer, to make sure the cards will keep on the desk for several seconds after one round is over</para> /// </summary> private void clearCardInThisTurn() { this.Dispatcher.Invoke(new Action(delegate { for (int i = 0; i < this.outCards.Count; i++) { CardControl c = this.outCards[i] as CardControl; this.canvas.Children.Remove(c); } this.outCards.Clear(); })); }
private static void OnSuitChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d != null && d is CardControl) { CardControl cc = d as CardControl; cc.tbSuit1.Text = (string)e.NewValue; cc.tbSuit2.Text = (string)e.NewValue; cc.tbSuit3.Text = (string)e.NewValue; if (string.Compare((string)e.NewValue, "\u2665") == 0 || string.Compare((string)e.NewValue, "\u2666") == 0) { cc.tbSuit1.Foreground = new SolidColorBrush(Colors.Red); cc.tbSuit2.Foreground = new SolidColorBrush(Colors.Red); cc.tbSuit3.Foreground = new SolidColorBrush(Colors.Red); } } }
private void sendPlay(object sender, RoutedEventArgs e) { this.Dispatcher.Invoke(new Action(delegate { CardControl card = sender as CardControl; UserEnable(Convert.ToInt32(card.Owner), false); Play m = new Play(); m.Who = (Player)this.seat; Card c = new Card(); c.Suit = (Suit)Convert.ToInt32(card.Suit); c.Rank = (uint)Convert.ToInt32(card.Number); m.Card = c; try { this.socket.Send(m.ToByteArray()); } catch (Exception ex) { } })); }
/// <summary> /// async receive data from server /// </summary> /// <param name="socket"></param> private void startReceive(Socket socket, int code) { byte[] data = new byte[1024]; try { socket.BeginReceive(data, 0, data.Length, SocketFlags.None, asyncResult => { try { int length = socket.EndReceive(asyncResult); Console.WriteLine("{0} bytes received", length); byte[] receivedData = data.Take(length).ToArray(); // inform ui what next information will be if (code == 1) { Hello m = new Hello(); m.MergeFrom(receivedData); code = (int)m.Code; // if received hello with code 4, means cards are distributed successfully. update ui then if (code == 4) { this.seat = 0; Console.WriteLine("card updated"); for (int i = 0; i < 4; i++) { updateCardUI(i); // update all } for (int i = 0; i < 4; i++) { showCardInHand(i); } code = 1; this.Dispatcher.Invoke(new Action(delegate { this.WaitAnimation = true; if (!this.watching) { this.watcherTimer.Start(); // when playing with models, start watcherTimer, to send continue message automatically } this.score = 0; // reset score this.round = 0; // reset round updateScoreLabel(); })); } sendContinue(); } // receive gamestate // ui will receive this only when the watcher is not playing, i.e. is watching, or at the beginning of a game when watching else if (code == 2) { GameState m = new GameState(); m.MergeFrom(receivedData); code = 1; int who = (int)m.Who; int toplay = (int)m.TableID; if (toplay == 0) { for (int i = 0; i < 13; i++) { if (i < m.Hand.Count) { this.Card[who, i, 0] = (int)m.Hand[i].Suit; this.Card[who, i, 1] = (int)m.Hand[i].Rank; this.inhand[who, i] = true; } else { this.Card[who, i, 0] = -1; this.Card[who, i, 1] = -1; this.inhand[who, i] = false; } } Console.WriteLine("{0} card received", who); sendContinue(); } else { for (int i = 0; i < 13; i++) { for (int j = 0; j < m.ValidPlays.Count; j++) { if (this.Card[who, i, 0] == (int)m.ValidPlays[j].Suit && this.Card[who, i, 1] == (int)m.ValidPlays[j].Rank) { this.Dispatcher.Invoke(new Action(delegate { this.CardUI[who, i].MouseDoubleClick += selectCard; })); break; } } } this.Dispatcher.Invoke(new Action(delegate { this.ContinueButton.Click -= Button_Click_1; if (who == 0) { this.tipsLabel.Content = "Your Turn!\nDeclarer"; } else { this.tipsLabel.Content = "Your Turn!\nDummy"; } })); } } // receive play message // add count -> when count reaches 4, means 1 round is over, clear desk // when watching, once a round is over, do not use watcher timer to continue but use continue button only. in other cases, just set the watchertimer to 2s // when playing(!watching), once a round is over, keep the desk for a while(2s or 3s). in other cases, set the watcher to 500ms else if (code == 3) { Play m = new Play(); m.MergeFrom(receivedData); this.count += 1; code = 1; int who = (int)m.Who; Card card = m.Card; CardControl sc = null; for (int i = 0; i < 13; i++) { if (this.Card[who, i, 0] == (int)card.Suit && this.Card[who, i, 1] == (int)card.Rank) { sc = this.CardUI[(4 + who - this.seat) % 4, i]; this.Dispatcher.Invoke(new Action(delegate { sc.Suit = Encode2Suit(this.Card[who, i, 0]); sc.Number = Encode2Number(this.Card[who, i, 1]); })); this.inhand[who, i] = false; cardOut(sc); break; } } if (this.count == 4) { this.count = 0; this.round += 1; this.needClear = true; } // when there will be button // when only watching, there will always be a button // when playing, only when 13 rounds is finished, there will be a continue button if (this.watching || this.round == 13) { this.WaitAnimation = true; this.Dispatcher.Invoke(new Action(delegate { this.ContinueButton.Visibility = Visibility.Visible; })); } if (!this.needClear) { if (this.watching) { this.watcherTimer.Interval = 2000; } else { this.watcherTimer.Interval = 500; } this.watcherTimer.Start(); } else if (!this.watching && this.needClear && this.round < 13) { this.watcherTimer.Interval = 2000; this.watcherTimer.Start(); } int score = (int)m.Score; this.Dispatcher.Invoke(new Action(delegate { Console.WriteLine("score:{0}", score); this.score = score; updateScoreLabel(); })); } else if (code == 5) { Console.WriteLine("received restart ok signal"); code = 1; } startReceive(socket, code); } catch (Exception ex) { } }, null); } catch (Exception ex) { } }