RobotViewModel.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.ComponentModel.Composition;
  3. using System.Windows.Threading;
  4. using ENet;
  5. using Log;
  6. using Microsoft.Practices.Prism.ViewModel;
  7. using Robot;
  8. namespace Modules.Robot
  9. {
  10. [Export(contractType: typeof (RobotViewModel)),
  11. PartCreationPolicy(creationPolicy: CreationPolicy.NonShared)]
  12. internal sealed class RobotViewModel: NotificationObject, IDisposable
  13. {
  14. private readonly ClientHost clientHost;
  15. private string loginIP = "192.168.11.95";
  16. private ushort loginPort = 8888;
  17. private string account = "egametang";
  18. private string password = "163bio1";
  19. private readonly DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Normal)
  20. { Interval = new TimeSpan(0, 0, 0, 0, 50) };
  21. public string LoginIP
  22. {
  23. get
  24. {
  25. return this.loginIP;
  26. }
  27. set
  28. {
  29. if (this.loginIP == value)
  30. {
  31. return;
  32. }
  33. this.loginIP = value;
  34. this.RaisePropertyChanged("LoginIp");
  35. }
  36. }
  37. public ushort LoginPort
  38. {
  39. get
  40. {
  41. return this.loginPort;
  42. }
  43. set
  44. {
  45. if (this.loginPort == value)
  46. {
  47. return;
  48. }
  49. this.loginPort = value;
  50. this.RaisePropertyChanged("LoginPort");
  51. }
  52. }
  53. public string Account
  54. {
  55. get
  56. {
  57. return this.account;
  58. }
  59. set
  60. {
  61. if (this.account == value)
  62. {
  63. return;
  64. }
  65. this.account = value;
  66. this.RaisePropertyChanged("Account");
  67. }
  68. }
  69. public string Password
  70. {
  71. get
  72. {
  73. return this.password;
  74. }
  75. set
  76. {
  77. if (this.password == value)
  78. {
  79. return;
  80. }
  81. this.password = value;
  82. this.RaisePropertyChanged("Password");
  83. }
  84. }
  85. public RobotViewModel()
  86. {
  87. this.clientHost = new ClientHost();
  88. this.timer.Tick += delegate { this.clientHost.RunOnce(); };
  89. this.timer.Start();
  90. }
  91. ~RobotViewModel()
  92. {
  93. this.Disposing(false);
  94. }
  95. public void Dispose()
  96. {
  97. this.Disposing(true);
  98. GC.SuppressFinalize(this);
  99. }
  100. private void Disposing(bool disposing)
  101. {
  102. this.clientHost.Dispose();
  103. }
  104. public void Login()
  105. {
  106. var session = new RealmSession(this.LoginIP, this.LoginPort);
  107. try
  108. {
  109. session.Login(this.Account, this.Password);
  110. }
  111. catch (Exception e)
  112. {
  113. Logger.Trace("realm exception: {0}, {1}", e.Message, e.StackTrace);
  114. return;
  115. }
  116. Logger.Trace("session login success!");
  117. }
  118. }
  119. }