private void LClient_eGameHost(HostedGame game, bool unHosting, bool isGameListItem) { System.Threading.Thread thread = new System.Threading.Thread ( new System.Threading.ThreadStart ( delegate() { rtbChat.Dispatcher.Invoke ( System.Windows.Threading.DispatcherPriority.Normal, new Action ( delegate() { if(unHosting) { for(int i = 0; i < Program.LClient.HostedGames.Count; i++) { if(Program.LClient.HostedGames[i].intUGameNum == game.intUGameNum) Program.LClient.HostedGames.RemoveAt(i); } return; } if(isGameListItem) { Program.LClient.HostedGames.Add(game); } else { if(game.strHostName == Program.LClient.strUserName) { IPHostEntry host = Dns.GetHostEntry(game.getStrHost()[0]); // Addres of the host. IPAddress[] addressList = host.AddressList; ips = new String[addressList.Length]; int i = 0; foreach(IPAddress ip in addressList) { ips[i] = ip.ToString(); i++; } Join_Game(ips, game.getIntPort()); //Program.Client = new Networking.Client(addresslist[0], game.getIntPort()); //Program.Client.Connect(); //Program.LClient.isHosting = true; } Run r = new Run("#SYSTEM: "); Brush b = Brushes.Red; r.ToolTip = DateTime.Now.ToLongTimeString() + " " + DateTime.Now.ToLongDateString(); r.Foreground = b; r.Cursor = Cursors.Hand; r.Background = Brushes.White; Paragraph p = new Paragraph(); p.Inlines.Add(new Bold(r)); r = new Run(game.strHostName + " is hosting a " + game.strGameName + " "); p.Inlines.Add(r); r = getGameRun(game.strHostName, game); p.Inlines.Add(r); r = new Run(": " + game.strName); p.Inlines.Add(r); rtbChat.Document.Blocks.Add(p); rtbChat.ScrollToEnd(); if(Settings.Default.LobbySound) { System.Media.SoundPlayer sp = new System.Media.SoundPlayer(Properties.Resources.click); sp.Play(); } Program.LClient.HostedGames.Add(game); } } ) ); } ) ); thread.Start(); }
private void LobbyClient_onSocketMessageInput(object Sender, SocketMessage input) { String head = input.Header; String a = ""; String[] up; List<String> args = input.Arguments; String sPing = new String(new char[1] { (char)1 }); switch(head) { case "LOGSUCCESS": loggedIn = true; try { strUserName = args[0]; Settings.Default.NickName = Program.LClient.strUserName; Settings.Default.Save(); } catch(Exception e) { } eLogEvent.Invoke("LSUCC"); break; case "LOGERROR": MessageBox.Show("E-Mail or password is incorrect.", "Error"); eLogEvent.Invoke("LERR"); break; case "REGERR0": MessageBox.Show("Lobby server error. Please try again later."); eLogEvent.Invoke("RERR"); break; case "REGERR1": MessageBox.Show("Please enter a valid e-mail address."); eLogEvent.Invoke("RERR"); break; case "REGERR2": MessageBox.Show("Password must be at least 6 characters long."); eLogEvent.Invoke("RERR"); break; case "REGERR3": MessageBox.Show("Nickname already exists."); eLogEvent.Invoke("RERR"); break; case "REGERR4": MessageBox.Show("E-Mail already registered."); eLogEvent.Invoke("RERR"); break; case "REGSUCCESS": MessageBox.Show("Account successfully registered."); eLogEvent.Invoke("RSUCC"); break; default: if(head.Equals(sPing)) { if(Program.lwLobbyWindow == null) this.Close(true); else { if(!Program.lwLobbyWindow.IsActive) this.Close(true); } } if(loggedIn) { if(Status != PlayerStatus.Away) { TimeSpan ts = new TimeSpan(DateTime.Now.Ticks - lastMessageSent.Ticks); if(ts.TotalMinutes >= 5) { if(!isHosting && !isJoining) { Status = PlayerStatus.Away; } } } switch(head) { case "USERONLINE": //User[] temp = new User[args.Count]; try { a = args[0]; up = a.Split(new char[1] { ':' }); Boolean userAlreadyOnList = false; for(int i = 0; i < OnlineUsers.Count; i++) { if(OnlineUsers[i].Username.Equals(up[1])) { userAlreadyOnList = true; break; } } if(!userAlreadyOnList) OnlineUsers.Add(new User(up[0], up[1])); eUserEvent.Invoke(new User(up[0], up[1]), true); } catch(Exception e) { break; } break; case "USEROFFLINE": try { a = args[0]; up = a.Split(new char[1] { ':' }); for(int i = 0; i < OnlineUsers.Count; i++) { if(OnlineUsers[i].Email.Equals(up[0])) { User temp = OnlineUsers[i]; OnlineUsers.RemoveAt(i); eUserEvent.Invoke(temp, false); break; } } } catch(Exception e) { break; } break; case "ONLINELIST": try { OnlineUsers = new List<User>(0); for(int i = 0; i < args.Count; i++) { a = (String)args[i]; up = a.Split(new char[1] { ':' }); if(up.Length == 2) OnlineUsers.Add(new User(up[0], up[1])); else if(up.Length == 3) OnlineUsers.Add(new User(up[0], up[1], (PlayerStatus)Enum.Parse(typeof(PlayerStatus), up[2], true))); } eUserEvent.Invoke(new User("", ""), true); SocketMessage sm = new SocketMessage("GAMELIST"); writeMessage(sm); } catch(Exception e) { } break; case "LOBCHAT": try { eLobbyChat.Invoke(LobbyChatTypes.Global, (String)args[0], (String)args[1], false); } catch(Exception e) { } break; case "XAMLCHAT": try { eLobbyChat.Invoke(LobbyChatTypes.System, (String)args[0], (String)args[1], true); } catch(Exception e) { } break; case "LOBW": try { String[] s = args[0].Split(new char[1] { ':' }); if(s[0] != strUserName) strLastWhisperFrom = s[0]; eLobbyChat.Invoke(LobbyChatTypes.Whisper, (String)args[0], (String)args[1], false); } catch(Exception e) { }; break; case "CHATERROR": try { eLobbyChat.Invoke(LobbyChatTypes.Error, "ERROR", (String)args[0], false); } catch(Exception e) { }; break; case "CHATINFO": try { eLobbyChat.Invoke(LobbyChatTypes.System, "SYSTEM", (String)args[0], false); } catch(Exception e) { }; break; case "STATUS": String stat = args[0]; String usn = args[1]; if(usn.Equals(strUserName)) { Status = (PlayerStatus)Enum.Parse(typeof(PlayerStatus), stat, true); eUserStatusChanged.Invoke(new User("", strUserName), Status); } foreach(User u in OnlineUsers) { if(u.Username.Equals(usn)) { u.Status = (PlayerStatus)Enum.Parse(typeof(PlayerStatus), stat, true); eUserStatusChanged.Invoke(u, u.Status); break; } } break; case "HOST": try { String t = (string)args[6]; String[] ips = t.Split(new char[1] { '?' }); HostedGame hg = new HostedGame((string)args[3], (string)args[1], (string)args[0], (string)args[2], "", ips, int.Parse((string)args[7])); hg.strHostName = (string)args[4]; hg.intUGameNum = int.Parse((string)args[5]); eGameHost.Invoke(hg, false, false); } catch(Exception e) { }; break; case "UNHOST": try { HostedGame hg = new HostedGame(); hg.intUGameNum = int.Parse((String)args[0]); eGameHost.Invoke(hg, true, false); } catch(Exception e) { }; break; case "GAMELIST": try { String t = (string)args[1]; String[] ips = t.Split(new char[1] { '?' }); HostedGame hg = new HostedGame((string)args[7], (string)args[4], (string)args[3], (string)args[5], "", ips, int.Parse((string)args[2])); hg.strHostName = (string)args[6]; hg.intUGameNum = int.Parse((string)args[0]); eGameHost.Invoke(hg, false, true); } catch(Exception e) { }; break; default: #if (DEBUG) MessageBox.Show(input.getMessage(), "Input from sock"); #endif break; } } else { #if (DEBUG) MessageBox.Show(input.getMessage(), "Input from sock"); #endif } break; } }
private Run getGameRun(String user, HostedGame game) { Run r = new Run("game"); r.ToolTip = "Click to join " + user + "'s game"; r.Cursor = Cursors.Hand; r.Foreground = Brushes.Blue; r.Background = Brushes.White; r.MouseEnter += delegate(object sender, MouseEventArgs e) { r.Background = new RadialGradientBrush(Colors.DarkGray, Colors.WhiteSmoke); }; r.MouseLeave += delegate(object sender, MouseEventArgs e) { r.Background = Brushes.White; }; r.AddHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(delegate(object sender, MouseButtonEventArgs e) { if(user.Equals(Program.LClient.strUserName)) return; if(!Program.LClient.isHosting && !Program.LClient.isJoining) { if((Application.Current.MainWindow as Play.PlayWindow) != null) Application.Current.MainWindow.Close(); else if((Application.Current.MainWindow as DeckBuilder.DeckBuilderWindow) != null) Application.Current.MainWindow.Close(); Program.LClient.isJoining = true; if(SelectGame(game.getStrGUID())) { intIpTried = 0; port = game.getIntPort(); ips = game.getStrHost(); IPHostEntry host = Dns.GetHostEntry(ips[0]); // Addres of the host. IPAddress[] addressList = host.AddressList; ips[0] = addressList[0].ToString(); Join_Game(ips, game.getIntPort()); } else { //change_join_text("Join"); MessageBox.Show("You do not have the correct game installed."); Program.LClient.isJoining = false; } } else { if(Program.LClient.isHosting) { MessageBox.Show("Please stop hosting first."); } if(Program.LClient.isJoining) { MessageBox.Show("Please stop joining first."); } } })); return r; }