private void newWindow_RaiseCustomEvent(object sender, menu.CustomEventArgs e) { if (e.Title == 1) { Player1 = e.Message1; Player2 = e.Message2; Index = e.Title; } if (e.Title == 2 || e.Title == 3 || e.Title == 4) { Player1 = e.Message1; Player1 = e.Message1; Index = e.Title; } }
// int PointWin = -1; public MainWindow() { AI = new AIGomoku(); InitializeComponent(); menu mn = new menu(); mn.RaiseCustomEvent += new EventHandler<menu.CustomEventArgs>(newWindow_RaiseCustomEvent); mn.ShowDialog(); string chat = Name; if (Index == 2) { AI.InitiIsBlack(); cb = new ChessBoard(AI._IsBlack,Player1); cb.Click += ViewModels_Click; cb.ClickChat += ViewModels_ClickChat; cb.ClickChange += ViewModel_ClickChange; cb.message += ViewModels_Message; worker.DoWork += worker_DoWork; worker.RunWorkerCompleted += worker_RunWorkerCompleted; DataContext = cb; } if (Index == 1) { AI.InitiIsBlack(); TBName.Visibility = Visibility.Hidden; BTChangeName.Visibility = Visibility.Hidden; LBName.Visibility = Visibility.Hidden; Chattb.Visibility = Visibility.Hidden; Chat.Visibility = Visibility.Hidden; cb = new ChessBoard(AI._IsBlack, Player1); cb.Click += ViewModels_Click; cb.ClickChat += ViewModels_ClickChat; cb.ClickChange += ViewModel_ClickChange; cb.message += ViewModels_Message; isPlayer = 4; DataContext = cb; } if (Index == 3) { AI.InitiIsBlack(); cb = new ChessBoard(AI._IsBlack, Player1); cb.Click += ViewModels_Click; cb.ClickChat += ViewModels_ClickChat; cb.ClickChange += ViewModel_ClickChange; cb.message += ViewModels_Message; worker.DoWork += worker_Net; worker.RunWorkerCompleted += worker_RunWorkerNetCompleted; DataContext = cb; } if (Index == 4) { AI.InitiIsBlack(); cb = new ChessBoard(AI._IsBlack, Player1); cb.Click += ViewModels_Click; cb.ClickChat += ViewModels_ClickChat; cb.ClickChange += ViewModel_ClickChange; cb.message += ViewModels_Message; worker.DoWork += worker_DoWork_Net; worker.RunWorkerCompleted += worker_RunWorkerCompleted_Net; DataContext = cb; } if (Index == 0) this.Close(); if (Index == 3 || Index == 4) { //var ConnStr = ConfigurationSettings.AppSettings["IPAddress"].ToString(); socket = IO.Socket(ConfigurationSettings.AppSettings["IPAddress"].ToString()); socket.On(Socket.EVENT_CONNECT, () => { }); socket.On(Socket.EVENT_MESSAGE, (data) => { Thread t = new Thread(() => ChatNeWork(data.ToString())); t.SetApartmentState(ApartmentState.STA); t.Start(); }); socket.On(Socket.EVENT_CONNECT_ERROR, (data) => { Thread t = new Thread(() => ChatNeWork(data.ToString())); t.SetApartmentState(ApartmentState.STA); t.Start(); }); socket.On("ChatMessage", (data) => { Thread t = new Thread(() => ChatNeWork(data.ToString())); t.SetApartmentState(ApartmentState.STA); t.Start(); if (((Newtonsoft.Json.Linq.JObject)data)["message"].ToString() == "Welcome!") { socket.Emit("MyNameIs", Player1); socket.Emit("ConnectToOtherPlayer"); } string s = data.ToString(); if (s.Contains("is now called")) { int n = s.IndexOf("is now called"); s = s.Substring(n + 14); n = s.IndexOf("\"\r\n}"); s = s.Substring(0, n); if (IsChangeName == true) { Player1 = s; } else { Player2 = s; } } if (s.Contains("You are the first player!")) { isNext = true; string TName = data.ToString(); int n = TName.IndexOf(Player1); TName = TName.Substring(n + 5 + Player1.Length); n = TName.IndexOf("started a new game."); TName = TName.Substring(0, n); Player2 = TName; if (Index == 4) { worker.RunWorkerAsync(); } } if (s.Contains("You are the second player!")) { isNext = false; string TName = data.ToString(); int n = TName.IndexOf("and " + Player1 + " started a new game."); TName = TName.Substring(0, n - 1); TName = TName.Substring(17); Player2 = TName; } }); socket.On(Socket.EVENT_ERROR, (data) => { Thread t = new Thread(() => ChatNeWork(data.ToString())); t.SetApartmentState(ApartmentState.STA); t.Start(); }); socket.On("NextStepIs", (data) => { Thread t = new Thread(() => NextStep("NextStepIs: " + data.ToString())); t.SetApartmentState(ApartmentState.STA); t.Start(); }); } else isNext = true; }