public StartBattleVM(PokemonLobbyClient client, User rival, GameSettings settings, bool isPassive) { this.client = client; Rival = rival; this.isPassive = isPassive; RivalAvatar = AvatarVM.GetAvatar(rival.Avatar); Teams = Helper.DataMainInstance.PokemonData.Teams.Folders; chosenTeam = Teams.FirstOrDefault(); GameSettings = settings; if (isPassive) { OkCommand = new MenuCommand("接受", Accept); CancelCommand = new MenuCommand("拒绝", Refuse); client.ChallengeCanceled += OnProcessed; PlaySound(); } else { OkCommand = new MenuCommand("挑战", Challenge); CancelCommand = new MenuCommand("取消", Cancel); client.ChallengeAccepted += OnProcessed; client.ChallengeRefused += OnProcessed; } client.EnterSucceed += OnProcessed; OkCommand.IsEnabled = ChosenTeam != null; timer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(10) }; timer.Tick += (sender, e) => CancelCommand.IsEnabled = true; }
public UserVM(PokemonLobbyClient client, User user) : base(user, false) { this.client = client; if (client.User.Id != user.Id) { commands.Add(new MenuCommand("私聊", Chat)); commands.Add(new MenuCommand("挑战", Challenge)); } }
private void client_LoginFailed() { lock (this) { UIDispatcher.Invoke(() => { MessageBox.Show("Login Failed"); currentClient = null; IsEnabled = true; }); } }
private void client_LoginComplete() //not in UI thread { lock (this) { UIDispatcher.Invoke(() => { currentClient.LoginCompleted -= client_LoginComplete; LoginComplete(currentClient); currentClient = null; button.IsEnabled = true; }); } }
public LobbyVM(PokemonLobbyClient model) { this.Model = model; model.UserChanged += model_UserChanged; //if it's possible to relogin, better Disconnected+=()=>Model.Dispose(); var _us = model.Users; usersDictionary = new Dictionary<int, UserVM>(_us.Count()); users = new ObservableCollection<UserVM>(); foreach (User u in _us) AddUser(u); User = new UserVM(model, Model.User); usersDictionary.Add(User.Id, User); users.Add(User); readonlyUsers = new ReadOnlyObservableCollection<UserVM>(users); UsersView = CollectionViewSource.GetDefaultView(Users); UsersView.SortDescriptions.Add(new SortDescription("State", ListSortDirection.Descending)); }
internal void Init(PokemonLobbyClient client) { if (controller != null) { //那个Intern的string岂不是悲剧了 放不出来了 controller.ChatMessageReceived -= controller_ChatMessageReceived; } if (client != null) { speaking.Clear(); chat.Inlines.Clear(); userName = client.User.Name; //userName = String.Intern(client.User.Name); //还是算了 controller = client; controller.BroadcastReceived += controller_BroadcastReceived; controller.ChatMessageReceived += controller_ChatMessageReceived; } else { IsEnabled = false; } }
/// <param name="settings">主动的话这个不应该是null么</param> public StartBattle(PokemonLobbyClient client, User rival, GameSettings settings, bool isPassitive) { InitializeComponent(); DataContext = vm = new StartBattleVM(client, rival, settings, isPassitive); vm.Processed += () => Close(); }
private void button_Click(object sender, RoutedEventArgs e) { //Is it neccessary to make UserData multi-instances? string addr = servers.Text.Trim(); if (currentClient != null || string.IsNullOrWhiteSpace(addr) || string.IsNullOrWhiteSpace(name.Text)) return; System.Net.IPAddress ip; if (!System.Net.IPAddress.TryParse(addr, out ip)) try { var ips = System.Net.Dns.GetHostAddresses(addr); foreach(var i in ips) if (i.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { ip = i; break; } } catch { } if (ip != null) { lock (this) { currentClient = new PokemonLobbyClient(ip, PORT); currentClient.LoginFailed += client_LoginFailed; currentClient.LoginCompleted += client_LoginComplete; currentClient.Login(name.Text.Trim(), avatarVM.InnerAvatarId, avatarUrl.Text);//"http://tb.himg.baidu.com/sys/portrait/item/f543c7aec9f1b2bbcac76c6f6c69bfd85603" } IsEnabled = false; } }