/// <summary> /// Handles <see cref="NetPlayer.GameQuitted"/> event. /// </summary> /// <param name="sender">Event source.</param> /// <param name="e">An <see cref="EventArgs"/> containing event data.</param> private void NetPlayer_GameQuitted(object sender, EventArgs e) { MainWindow.BeginInvoke(new EventHandler((snd, args) => { controller.Timing.Stop(); MessageBox.Show("Przeciwnik opuścił grę", "Niestety...", MessageBoxButtons.OK, MessageBoxIcon.Information); mapView.MapViewClick -= MapView_MapViewClick; })); }
/// <summary> /// Initializes a new instance of <see cref="GameplayMPHost"/> with specified size of map and /// <see cref="TcpClient"/> to use for communication with client. /// </summary> /// <param name="mapSize">Size of the map to play with.</param> /// <param name="client"><see cref="TcpClient"/> to use for communication with client.</param> public GameplayMPHost(Size mapSize, TcpClient client) : base(mapSize, Mark.Cross) { NetPlayer remote = new NetPlayer(); connection = new Connection(client); controller = new HostController(mapSize, connection, 10) { SyncConext = SynchronizationContext.Current }; controller.AddPlayers(new User(), remote); HostTimer timing = controller.Timing as HostTimer; connection.DataProcessor.RegisterProcessingUnit(NetPlayer.MarkCode, remote); connection.DataProcessor.RegisterProcessingUnit(NetPlayer.QuitCode, remote); connection.DataProcessor.RegisterProcessingUnit(ClientTimer.TimeCode, timing); connection.DataProcessor.RegisterProcessingUnit(ClientTimer.TimePassedCode, timing); controller.Timing.Tick += (sender, e) => { TimeSpan time = TimeSpan.FromSeconds(controller.Timing.TimeLeft); connection.SendInformation(ClientTimer.TimeCode, time.ToString()); }; controller.Timing.TimePassed += (sender, e) => { connection.SendInformation(ClientTimer.TimePassedCode, null); }; controller.State.CurrentChanged += (sender, e) => { MainWindow.BeginInvoke(new MethodInvoker(() => { controller.Timing.Restart(); })); }; }
private void HostButtonClick(object sender, EventArgs e) { var ret = MessageBox.Show("Postawić na hoście lokalnym?", "...", MessageBoxButtons.YesNo, MessageBoxIcon.Question); IPAddress ip; if (ret == DialogResult.Yes) { ip = IPAddress.Parse("127.0.0.1"); } else { ip = IPAddress.Any; } TcpClient client = null; string title = MainWindow.Text; MainWindow.Text = "Oczekiwanie na połączenie..."; Thread listeningThread = null; TcpListener listen = null; listeningThread = new Thread(() => { listen = new TcpListener(ip, 11000); try { listen.Start(); client = listen.AcceptTcpClient(); } catch (SocketException) { } finally { listen.Stop(); MainWindow.BeginInvoke(new MethodInvoker(() => { MainWindow.Text = title; listeningThread.Join(); if (client != null) { CtrlInstance = new GameplayMPHost(new Size(100, 100), client); } })); } }); btnHost.Click -= HostButtonClick; btnHost.Text = "Przerwij"; void NewEventHandler(object sender2, EventArgs e2) { if (listen != null) { listen.Stop(); } btnHost.Text = "Postaw"; btnHost.Click -= NewEventHandler; btnHost.Click += HostButtonClick; } btnHost.Click += NewEventHandler; listeningThread.Start(); }
/// <summary> /// Initializes main window. /// </summary> public override void InitializeWindow() { Label lblCurrentInfo = new Label { Text = "Aktualny gracz: ", Name = CurrentInfo, AutoSize = true, Font = SystemFonts.CaptionFont, ForeColor = Color.Blue }; MarkDisplay markDisplay = new MarkDisplay { Current = Mark.Cross, Size = new Size(40, 40), Name = CurrentDisplay }; Label lblTimeLeftInfo = null; CountingDownLabel lblTimeLeft = null; if (controller.Timing != null) { lblTimeLeftInfo = new Label { AutoSize = true, Font = SystemFonts.CaptionFont, ForeColor = Color.Blue, Name = TimeLeftInfo, Text = "Czas do wykonania ruchu:" }; lblTimeLeft = new CountingDownLabel { AutoSize = true, Font = SystemFonts.CaptionFont, ForeColor = Color.Blue, Name = TimeLeft, Time = TimeSpan.FromSeconds(controller.Timing.MoveTime) }; } mapView.Location = new Point(0, 50); mapView.Size = new Size(MainWindow.ClientSize.Width, MainWindow.ClientSize.Height - 50); mapView.MapViewClick += MapView_MapViewClick; upperPanel = new CenteringPanel { Size = new Size(MainWindow.ClientSize.Width - 120, 50), Location = new Point(0, 0), DefaultControlsDistance = 10 }; upperPanel.SuspendLayout(); upperPanel.AddToPanel(lblCurrentInfo); upperPanel.AddToPanel(markDisplay); if (lblTimeLeft != null) { upperPanel.AddToPanel(lblTimeLeftInfo, 20); upperPanel.AddToPanel(lblTimeLeft, 0); } upperPanel.ResumeLayout(); optionsBtn = new Button { Text = "Opcje", Size = new Size(100, 35), BackColor = Color.Green, ForeColor = Color.White, Location = new Point(MainWindow.ClientSize.Width - 110, 7), FlatStyle = FlatStyle.Flat }; optionsBtn.Click += OptionsButton_Click; MainWindow.Controls.Add(mapView); MainWindow.Controls.Add(upperPanel); MainWindow.Controls.Add(optionsBtn); controller.State.TicTacToeMap.MarksChanged += (sender, e) => { MainWindow.BeginInvoke(new MarksChangedEventHandler(Map_MarksChanged), sender, e); }; controller.WinConditionMet += (sender, e) => { MainWindow.BeginInvoke(new WinConditionMetEventHandler(Controller_WinConditionMet), sender, e); }; controller.State.CurrentChanged += (sender, e) => { MainWindow.BeginInvoke(new EventHandler(State_CurrentChanged), sender, e); }; if (controller.Timing != null) { controller.Timing.Tick += (sender, e) => { MainWindow.BeginInvoke(new EventHandler(Timing_Tick), sender, e); }; controller.Timing.Start(); } }