FormRoles.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using DeepCore;
  2. using DeepMMO.Data;
  3. using DeepMMO.Protocol;
  4. using DeepMMO.Protocol.Client;
  5. using System;
  6. using System.Windows.Forms;
  7. namespace DeepMMO.Client.Win32.Forms
  8. {
  9. public partial class FormRoles : Form
  10. {
  11. private readonly RPGClient client;
  12. public RoleTemplateData SelectedRoleTemplate
  13. {
  14. get { return combo_Pro.SelectedItem as RoleTemplateData; }
  15. }
  16. public RoleSnap SelectedRole
  17. {
  18. get
  19. {
  20. if (list_Roles.SelectedItems.Count > 0)
  21. {
  22. return list_Roles.SelectedItems[0].Tag as RoleSnap;
  23. }
  24. return null;
  25. }
  26. }
  27. public FormRoles(RPGClient client)
  28. {
  29. this.client = client;
  30. InitializeComponent();
  31. this.DialogResult = DialogResult.Ignore;
  32. init_role_templates();
  33. }
  34. protected override void OnLoad(EventArgs e)
  35. {
  36. base.OnLoad(e);
  37. do_random_name();
  38. do_get_role_list();
  39. }
  40. //-----------------------------------------------------------------------------------------------------------
  41. #region Operation
  42. protected virtual void init_role_templates()
  43. {
  44. var prolist = RPGClientTemplateManager.Instance.AllRoleTemplates;
  45. foreach (var rt in prolist)
  46. {
  47. combo_Pro.Items.Add(rt);
  48. }
  49. var pro = new Random().GetRandomInArray(prolist);
  50. combo_Pro.SelectedItem = pro;
  51. }
  52. private void do_random_name()
  53. {
  54. var pro = SelectedRoleTemplate;
  55. if (pro != null)
  56. {
  57. this.client.GameClient.Request<ClientGetRandomNameResponse>(new ClientGetRandomNameRequest() { c2s_role_template_id = pro.id }, (err, rsp) =>
  58. {
  59. if (Response.CheckSuccess(rsp))
  60. {
  61. txt_RoleName.Text = rsp.s2c_name;
  62. }
  63. else
  64. {
  65. MessageBox.Show("ClientGetRandomNameRequest : " + rsp);
  66. }
  67. });
  68. }
  69. }
  70. protected virtual void do_get_role_list()
  71. {
  72. list_Roles.Items.Clear();
  73. this.client.GameClient.Request<ClientGetRolesResponse>(new ClientGetRolesRequest() { }, (err, rsp) =>
  74. {
  75. if (Response.CheckSuccess(rsp))
  76. {
  77. if (rsp.s2c_roles == null || rsp.s2c_roles.Count == 0)
  78. {
  79. tabControl1.SelectedTab = tabPage_CreateRole;
  80. }
  81. else
  82. {
  83. tabControl1.SelectedTab = tabPage_SelectRole;
  84. rsp.s2c_roles.Sort((a, b) =>
  85. {
  86. return (a.last_login_time - b.last_login_time).Seconds;
  87. });
  88. foreach (var p in rsp.s2c_roles)
  89. {
  90. if (p != null)
  91. {
  92. add_role_to_list(p);
  93. }
  94. }
  95. }
  96. }
  97. else
  98. {
  99. MessageBox.Show("ClientGetRolesRequest : " + rsp);
  100. }
  101. });
  102. }
  103. protected virtual void add_role_to_list(RoleSnap p)
  104. {
  105. var rt = RPGClientTemplateManager.Instance.GetRoleTemplate(p.role_template_id, 0);
  106. var item = list_Roles.Items.Add(p.name);
  107. item.SubItems.Add(p.level.ToString());
  108. item.SubItems.Add(rt != null ? rt.name : "");
  109. item.SubItems.Add(p.last_login_time.ToString());
  110. item.SubItems.Add((p.uuid == client.last_EnterGateResponse.s2c_lastLoginRoleID) ? "True" : "");
  111. item.SubItems.Add(p.server_id + "");
  112. item.Tag = p;
  113. item.Selected = true;
  114. }
  115. protected virtual void do_create_role()
  116. {
  117. var pro = SelectedRoleTemplate;
  118. if (pro != null)
  119. {
  120. this.client.GameClient.Request<ClientCreateRoleResponse>(new ClientCreateRoleRequest()
  121. {
  122. c2s_name = txt_RoleName.Text,
  123. c2s_template_id = pro.id,
  124. }, (err, rsp) =>
  125. {
  126. if (Response.CheckSuccess(rsp))
  127. {
  128. tabControl1.SelectedTab = tabPage_SelectRole;
  129. add_role_to_list(rsp.s2c_role);
  130. }
  131. else
  132. {
  133. MessageBox.Show("ClientCreateRoleResponse : " + rsp);
  134. }
  135. });
  136. }
  137. else
  138. {
  139. MessageBox.Show("未选择职业");
  140. }
  141. }
  142. protected virtual void do_enter_game()
  143. {
  144. var role = SelectedRole;
  145. if (role != null)
  146. {
  147. var last_roleID = client.last_EnterGateResponse.s2c_lastLoginRoleID;
  148. if (last_roleID != null && role.uuid != last_roleID)
  149. {
  150. }
  151. this.client.GameClient.Request<ClientEnterGameResponse>(new ClientEnterGameRequest()
  152. {
  153. c2s_roleUUID = role.uuid
  154. }, (err, rsp) =>
  155. {
  156. if (Response.CheckSuccess(rsp))
  157. {
  158. this.DialogResult = DialogResult.OK;
  159. this.Close();
  160. }
  161. else
  162. {
  163. MessageBox.Show("ClientEnterGameResponse : " + rsp);
  164. }
  165. });
  166. }
  167. else
  168. {
  169. MessageBox.Show("未选择角色");
  170. }
  171. }
  172. #endregion
  173. //-----------------------------------------------------------------------------------------------------------
  174. #region FormEvent
  175. private void btn_RandomName_Click(object sender, EventArgs e)
  176. {
  177. do_random_name();
  178. }
  179. private void btn_CreateRole_Click(object sender, EventArgs e)
  180. {
  181. do_create_role();
  182. }
  183. private void btn_Enter_Click(object sender, EventArgs e)
  184. {
  185. do_enter_game();
  186. }
  187. #endregion
  188. }
  189. }