Advertisement
Guest User

Untitled

a guest
Jul 5th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 55.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Threading;
  11.  
  12. namespace Client
  13. {
  14. public partial class Form1 : Form
  15. {
  16. // note !-> all the consts' values mean this.width (OR height) / value
  17. private const int ALLIGMENT_FROM_TOP = 20; // the alligment from the top of the screen
  18. private const int ALLIGMENT_FROM_LEFT = 20; // the alligment from the Left of the screen
  19. private const int LOG_IN_FORM_TXT_X_SIZE = 4; // the size of the text boxes in x axis which will be shown on the screen
  20. private const int LOG_IN_FORM_TEXT_SECTOR_HEIGHT = 2; // the height of the username password and email text boxes
  21. private const int LOG_IN_FORM_ALLGIMENT_FROM_TEXT_TO_LABEL = 50; // alligment from label to textbox
  22. private const int LOG_IN_FORM_PICBOX_HEIGHT = 4; // the height of the buttons (width / 4)
  23. private const int SIGN_OUT_FORM_BACK_HEIGHT_WIDTH = 6; // the height and width of the back button
  24. private const int CLOSE_GAME_BUTTON_WIDTH = 7; // the width of the close button
  25. private const int CLOSE_GAME_BUTTON_HEIGHT = 16; // the height of the close button
  26. private const int MAIN_FORM_PICBOX_HEIGHT = 10; // the height of the picture boxes in main form
  27. private const int MAIN_FORM_PICBOX_WIDTH = 5; // the width of the picture boxes in the main form
  28. private const int MAIN_FORM_LOG_OUT_HEIGHT = 12; // the height of the log out button
  29. private const int MAIN_FORM_LOG_OUT_WIDTH = 7; // the width of the log out button
  30. private const int BEST_SCORES_FORM_PB_HEIGHT = 4; // the height of the best scores pictures
  31. private const int BEST_SCORES_FORM_PB_WIDTH = 5; // the width of the best scores picture
  32. private const int JOIN_ROOM_FORM_ROOMS_LB_WIDTH = 4; // the width of the rooms list
  33. private const int JOIN_ROOM_FORM_USERS_LB_WIDTH = 5; // the width of the users list
  34. private const int JOIN_ROOM_FORM_BUTTON_SECTOR_Y = 4; // the height of the button sector
  35. private const int JOIN_ROOM_FORM_REFRESH_HEIGHT = 2; // the height of the refresh button
  36. private const int JOIN_ROOM_FORM_REFRESH_WIDTH = 8; // the width of the refresh button
  37. private const int JOIN_ROOM_FORM_JOIN_HEIGHT = 8; // the height of the join button
  38. private const int JOIN_ROOM_FORM_JOIN_WIDTH = 4; // the width of the join button
  39. private const int IN_ROOM_FORM_BUTTON_SECTOR_Y = 6; // the height of the button sector
  40. private const int IN_ROOM_FORM_BUTTON_HEIGHT = 8; // the height of the button
  41. private const int IN_ROOM_FORM_BUTTON_WIDTH = 3; // the width of the button
  42. private const int IN_ROOM_FORM_LB_WIDTH = 4; // the width of the list box
  43.  
  44. //private static int yAxisSpace; // the space remains after the alligment is substracted in y axis
  45. //private static int xAxisSpace; // the space remains after the alligment is substracted in x axis
  46.  
  47. private MyPictureBox PBClose; // the close button, <> needs to be disposed
  48. private Thread t; // Thread to handle messages from the server
  49. private Stream stream; // creates a connection with the server by ip and port
  50. private int alligmentFromSubjectY; // the alligment in y axis from the subject
  51. private string uid; // the username of the signed in user
  52. private string roomName; // the name of the room the user is currently at
  53. private List<string> roomProperties; // the properties of the room the user is currently at
  54. private List<string> roomsID; // all the rooms id which the user can connect them
  55.  
  56. public Form1()
  57. {
  58. this.stream = new Stream("127.0.0.1", 8686);
  59.  
  60. t = new Thread(handleMessage);
  61. t.Start();
  62.  
  63. InitializeComponent();
  64.  
  65. this.Width = Screen.PrimaryScreen.Bounds.Width;
  66. this.Height = Screen.PrimaryScreen.Bounds.Height;
  67.  
  68. InitStaticVars();
  69. //InitPersonalStatus(null, EventArgs.Empty);
  70. //ChangePersonalStatus(new string[4] { "4", "10", "5", "3" });
  71. InitLoginScreen(null, EventArgs.Empty);
  72. //InitSignUp(null, EventArgs.Empty);
  73. //InitMainScreen(null, EventArgs.Empty);
  74. //InitBestScores(null, EventArgs.Empty);
  75. //KeyValuePair<string, string>[] arr = new KeyValuePair<string, string>[3];
  76. //arr[0] = new KeyValuePair<string, string>("tal", "100");
  77. //arr[1] = new KeyValuePair<string, string>("Dolev", "0");
  78. //arr[2] = new KeyValuePair<string, string>("yarin", "10");
  79. //changeBestScores(arr);
  80. //CleanAllControlers();
  81. }
  82.  
  83. /// <summary>
  84. /// initializes all static variables of custom classes, and current class
  85. /// </summary>
  86. public void InitStaticVars()
  87. {
  88. this.roomsID = new List<string>();
  89.  
  90. // MyTxtBox Class:
  91. // the default width of the text box
  92. MyTxtBox._width = (this.Width / LOG_IN_FORM_TXT_X_SIZE);
  93.  
  94. // current class:
  95. // sets the close button
  96. PBClose = new MyPictureBox("PBClose", "close", new Size(this.Width / CLOSE_GAME_BUTTON_WIDTH, this.Height / CLOSE_GAME_BUTTON_HEIGHT));
  97. PBClose.Location = new Point(this.Width / 2 - PBClose.Width / 2, this.Height - PBClose.Height - this.Height / ALLIGMENT_FROM_TOP);
  98. PBClose.Click += PBClose_Click;
  99. this.Controls.Add(PBClose);
  100.  
  101. // sets the alligment from top of screen
  102. alligmentFromSubjectY = this.Height / ALLIGMENT_FROM_TOP;
  103.  
  104. uid = "tal";
  105. }
  106.  
  107. /// <summary>
  108. /// makes the login is screen
  109. /// </summary>
  110. public void InitLoginScreen(object sender, EventArgs args)
  111. {
  112. int reminingY = this.Height;
  113. int nextYPos = 0;
  114. int xOfTextBoxes = 0;
  115. int sizeOfPictureBox = 0;
  116. int alligmentFromPictureBox = 0;
  117. int alligmentFromTextBoxes = 0;
  118. int alligmentFromTextBoxesToLabels = this.Width / LOG_IN_FORM_ALLGIMENT_FROM_TEXT_TO_LABEL;
  119. int xOfPicBoxes = 0;
  120. List<Control> cont = new List<Control>();
  121.  
  122. // sets the background of the page
  123. this.BackgroundImage = Client.Properties.Resources.loginBG;
  124.  
  125. // sets the color of the letters
  126. this.ForeColor = Color.White;
  127.  
  128. // sets the subject label
  129. Label LBLSubject = new MyLabel("Please Log In", "LBLSubject"); // i create a label to know the width of its text
  130. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  131. cont.Add(LBLSubject);
  132.  
  133. nextYPos = LBLSubject.Location.Y + alligmentFromSubjectY + LBLSubject.Height; // begining of details section
  134. reminingY -= nextYPos; // sets the space of details section
  135.  
  136. // sets the text box of username
  137. TextBox TXTuserName = new MyTxtBox("TXTusername"); // i create a label to know the height of its font
  138. alligmentFromTextBoxes = ((reminingY / LOG_IN_FORM_TEXT_SECTOR_HEIGHT) - (TXTuserName.Height * 2)) / 3; // generates the needed alligment
  139. nextYPos += alligmentFromTextBoxes;
  140. reminingY -= reminingY / LOG_IN_FORM_TEXT_SECTOR_HEIGHT; // sets the space of buttons section
  141.  
  142. TXTuserName.Location = new Point((this.Width / 2) - (TXTuserName.Width / 2), nextYPos);
  143. cont.Add(TXTuserName);
  144.  
  145. // sets the label of username
  146. Label LBLusername = new MyLabel("UserName: ", "LBLusername"); // sets the label of the userName
  147. LBLusername.Location = new Point(TXTuserName.Location.X - LBLusername.Width - alligmentFromTextBoxesToLabels, nextYPos);
  148. cont.Add(LBLusername);
  149.  
  150. nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the position of the next textBox
  151. xOfTextBoxes = TXTuserName.Location.X;
  152.  
  153. // sets the text box of password
  154. TextBox TXTpsw = new MyTxtBox(new Point(xOfTextBoxes, nextYPos), "TXTpsw");
  155. cont.Add(TXTpsw);
  156.  
  157. // sets the label of password
  158. Label LBLpsw = new MyLabel("password: ", "LBLpsw"); // sets the password label
  159. LBLpsw.Location = new Point(xOfTextBoxes - LBLpsw.Width - alligmentFromTextBoxesToLabels, nextYPos);
  160. cont.Add(LBLpsw);
  161.  
  162. nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the begining of the buttons sector
  163.  
  164. // sets the buttons section
  165. sizeOfPictureBox = reminingY / LOG_IN_FORM_PICBOX_HEIGHT;
  166. reminingY -= (sizeOfPictureBox * 2);
  167. alligmentFromPictureBox = reminingY / 2;
  168.  
  169. // sets the sign in button
  170. PictureBox PBSignIn = new MyPictureBox("PBSignIn", "SignIn", new Size(TXTuserName.Width, sizeOfPictureBox));
  171. xOfPicBoxes = (this.Width / 2) - (PBSignIn.Width / 2); // sets the x of picture boxes
  172. PBSignIn.Location = new Point(xOfPicBoxes, nextYPos);
  173. PBSignIn.Click += (EventHandler)PBSign_In_Click;
  174. cont.Add(PBSignIn);
  175.  
  176. nextYPos += alligmentFromPictureBox;
  177.  
  178. // sets the signup button
  179. PictureBox PBSignUp = new MyPictureBox("PBSignUp", "SignUp", new Size(PBSignIn.Width, sizeOfPictureBox));
  180. PBSignUp.Location = new Point(xOfPicBoxes, nextYPos);
  181. PBSignUp.Click += (EventHandler)(CleanAllControlers);
  182. PBSignUp.Click += (EventHandler)InitSignUp;
  183. cont.Add(PBSignUp);
  184.  
  185. this.Controls.AddRange(cont.ToArray());
  186. }
  187.  
  188. /// <summary>
  189. /// sets the form to be sign up page
  190. /// </summary>
  191. public void InitSignUp(object sender, EventArgs args)
  192. {
  193. int reminingY = this.Height;
  194. int nextYPos = 0;
  195. int xOfTextBoxes = 0;
  196. int sizeOfPictureBox = 0;
  197. int alligmentFromPictureBox = 0;
  198. int alligmentFromTextBoxes = 0;
  199. int alligmentFromTextBoxesToLabels = this.Width / LOG_IN_FORM_ALLGIMENT_FROM_TEXT_TO_LABEL;
  200. int xOfPicBoxes = 0;
  201. List<Control> cont = new List<Control>();
  202.  
  203. // sets the background
  204. this.BackgroundImage = Properties.Resources.signUpBG;
  205.  
  206. // sets the color of the letters
  207. this.ForeColor = Color.Black;
  208.  
  209. // sets the subject label
  210. Label LBLSubject = new MyLabel("Please Sign Up", "LBLSubject"); // i create a label to know the width of its text
  211. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  212. cont.Add(LBLSubject);
  213.  
  214. nextYPos = LBLSubject.Location.Y + alligmentFromSubjectY + LBLSubject.Height; // begining of details section
  215. reminingY -= nextYPos; // sets the space of details section
  216.  
  217. // sets the text box of username
  218. TextBox TXTuserName = new MyTxtBox("TXTusername"); // i create a label to know the height of its font
  219. alligmentFromTextBoxes = ((reminingY / LOG_IN_FORM_TEXT_SECTOR_HEIGHT) - (TXTuserName.Height * 3)) / 4; // generates the needed alligment
  220. nextYPos += alligmentFromTextBoxes;
  221. reminingY -= reminingY / LOG_IN_FORM_TEXT_SECTOR_HEIGHT; // sets the space of buttons section
  222.  
  223. TXTuserName.Location = new Point((this.Width / 2) - (TXTuserName.Width / 2), nextYPos);
  224. cont.Add(TXTuserName);
  225.  
  226. // sets the label of username
  227. Label LBLusername = new MyLabel("UserName: ", "LBLusername"); // sets the label of the userName
  228. LBLusername.Location = new Point(TXTuserName.Location.X - LBLusername.Width - alligmentFromTextBoxesToLabels, nextYPos);
  229. cont.Add(LBLusername);
  230.  
  231. nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the position of the next textBox
  232. xOfTextBoxes = TXTuserName.Location.X;
  233.  
  234. // sets the text box of password
  235. TextBox TXTpsw = new MyTxtBox(new Point(xOfTextBoxes, nextYPos), "TXTpsw");
  236. cont.Add(TXTpsw);
  237.  
  238. // sets the label of password
  239. Label LBLpsw = new MyLabel("password: ", "LBLpsw"); // sets the password label
  240. LBLpsw.Location = new Point(xOfTextBoxes - LBLpsw.Width - alligmentFromTextBoxesToLabels, nextYPos);
  241. cont.Add(LBLpsw);
  242.  
  243. nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the position of the next textBox
  244.  
  245. // sets the text box of email
  246. TextBox TXTemail = new MyTxtBox(new Point(xOfTextBoxes, nextYPos), "TXTemail");
  247. cont.Add(TXTemail);
  248.  
  249. // sets the label of email
  250. Label LBLemail = new MyLabel("email: ", "LBLemail");
  251. LBLemail.Location = new Point(xOfTextBoxes - LBLemail.Width - alligmentFromTextBoxesToLabels, nextYPos);
  252. cont.Add(LBLemail);
  253.  
  254. nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the begining of the buttons sector
  255.  
  256. // sets the buttons section
  257. sizeOfPictureBox = reminingY / LOG_IN_FORM_PICBOX_HEIGHT;
  258. reminingY -= (sizeOfPictureBox * 2);
  259. alligmentFromPictureBox = reminingY / 2;
  260.  
  261. // sets the sign up button
  262. PictureBox PBSignUp = new MyPictureBox("PBSignUp", "SignUp", new Size(TXTemail.Width, sizeOfPictureBox));
  263. xOfPicBoxes = (this.Width / 2) - (PBSignUp.Width / 2); // sets the x of picture boxes
  264. PBSignUp.Location = new Point(xOfPicBoxes, nextYPos);
  265. PBSignUp.Click += (EventHandler)PBSign_Up_Click;
  266. //PBSignUp.Click += (EventHandler)CleanAllControlers;
  267. cont.Add(PBSignUp);
  268.  
  269. // sets the back button
  270. int PCHeight = this.Height / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  271. int PCWidth = this.Width / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  272. PictureBox PBBack = new MyPictureBox("PBBack", "back", new Size(PCWidth, PCHeight));
  273. PBBack.Location = new Point(this.Width - PCWidth, this.Height / ALLIGMENT_FROM_TOP);
  274. PBBack.Click += (EventHandler)CleanAllControlers;
  275. PBBack.Click += (EventHandler)InitLoginScreen;
  276. cont.Add(PBBack);
  277.  
  278. this.Controls.AddRange(cont.ToArray());
  279. }
  280.  
  281. /// <summary>
  282. /// builds the main screen
  283. /// </summary>
  284. /// <param name="sender"></param>
  285. /// <param name="args"></param>
  286. public void InitMainScreen(object sender, EventArgs args)
  287. {
  288. int nextYPos = 0;
  289. int reminingY = PBClose.Location.Y;
  290. int heightOfPB = this.Height / MAIN_FORM_PICBOX_HEIGHT;
  291. int widthOfPB = this.Width / MAIN_FORM_PICBOX_WIDTH;
  292. int alligmentFromButtonsY = 0;
  293. int alligmentFromButtonsX = 0;
  294.  
  295. // sets the background of the screen
  296. this.BackgroundImage = Properties.Resources.mainScreenBG;
  297.  
  298. // sets the font color to black
  299. this.ForeColor = Color.Black;
  300.  
  301. // sets the subject label
  302. Label LBLSubject = new MyLabel("Hello " + uid, "LBLSubject"); // i create a label to know the width of its text
  303. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  304. this.Controls.Add(LBLSubject);
  305.  
  306. // configures all the propeties needed for the button
  307. nextYPos = LBLSubject.Location.Y + alligmentFromSubjectY;
  308. reminingY -= (nextYPos - heightOfPB * 2);
  309. alligmentFromButtonsY = reminingY / 3;
  310. nextYPos += (alligmentFromButtonsY / 2);
  311. alligmentFromButtonsX = (this.Width - widthOfPB * 3) / 3;
  312.  
  313. // sets the Join room button
  314. Button BTNJoin = new MyButton("BTNJoin", new Size(widthOfPB, heightOfPB), "Join Room");
  315. BTNJoin.Location = new Point(alligmentFromButtonsX, nextYPos);
  316. BTNJoin.Click += (EventHandler)CleanAllControlers;
  317. BTNJoin.Click += (EventHandler)InitJoinRoom;
  318. BTNJoin.Click += (EventHandler)BTNJoin_Click;
  319. this.Controls.Add(BTNJoin);
  320.  
  321. // sets the Create room button
  322. Button BTNCreate = new MyButton("BTNCreate", new Size(widthOfPB, heightOfPB), "Create Room");
  323. BTNCreate.Location = new Point(this.Width - widthOfPB - alligmentFromButtonsX, nextYPos);
  324. this.Controls.Add(BTNCreate);
  325.  
  326. nextYPos += alligmentFromButtonsY;
  327.  
  328. // sets the best scores button
  329. Button BTNBest = new MyButton("BTNBest", new Size(widthOfPB, heightOfPB), "Best Scores");
  330. BTNBest.Location = new Point(alligmentFromButtonsX, nextYPos);
  331. BTNBest.Click += (EventHandler)CleanAllControlers;
  332. BTNBest.Click += (EventHandler)InitBestScores;
  333. BTNBest.Click += (EventHandler)BTNBest_Click;
  334. this.Controls.Add(BTNBest);
  335.  
  336. // sets the personal status
  337. Button BTNPersonal = new MyButton("BTNPersonal", new Size(widthOfPB, heightOfPB), "My Status");
  338. BTNPersonal.Location = new Point(this.Width - widthOfPB - alligmentFromButtonsX, nextYPos);
  339. BTNPersonal.Click += (EventHandler)CleanAllControlers;
  340. BTNPersonal.Click += (EventHandler)InitPersonalStatus;
  341. BTNPersonal.Click += (EventHandler)BTNPersonal_Click;
  342.  
  343. this.Controls.Add(BTNPersonal);
  344.  
  345. // sets the logout button
  346. PictureBox PBLogOut = new MyPictureBox("PBLogOut", "signOut", new Size(this.Height / MAIN_FORM_LOG_OUT_WIDTH, this.Width / MAIN_FORM_LOG_OUT_HEIGHT));
  347. PBLogOut.Location = new Point(this.Width / ALLIGMENT_FROM_LEFT, this.Height / ALLIGMENT_FROM_TOP);
  348. PBLogOut.Click += (EventHandler)CleanAllControlers;
  349. PBLogOut.Click += (EventHandler)InitLoginScreen;
  350. PBLogOut.Click += (EventHandler)PBLogOut_Click;
  351. this.Controls.Add(PBLogOut);
  352. }
  353.  
  354. /// <summary>
  355. /// inits the join room screen
  356. /// </summary>
  357. /// <param name="sender"></param>
  358. /// <param name="args"></param>
  359. private void InitJoinRoom(object sender, EventArgs args)
  360. {
  361. int nextYPos = 0;
  362. int alligmentFromClose = this.Height - this.PBClose.Location.Y - this.PBClose.Height;
  363. int alligmentFromButtonsY = 0;
  364. int alligmentFromButtonsX = 0;
  365. int buttonSectorY = 0;
  366. int alligmentFromListsX = 0;
  367. int refreshY = 0;
  368. int joinY = 0;
  369. int reminingY = this.PBClose.Location.Y;
  370.  
  371. // sets the form to join room bg
  372. this.BackgroundImage = Properties.Resources.joinRoomBG;
  373.  
  374. // changes the fore color to black
  375. this.ForeColor = Color.Black;
  376.  
  377. // sets the subject label
  378. Label LBLSubject = new MyLabel("Choose Room", "LBLSubject"); // i create a label to know the width of its text
  379. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  380. this.Controls.Add(LBLSubject);
  381.  
  382. nextYPos = LBLSubject.Location.Y + LBLSubject.Height + alligmentFromSubjectY;
  383. reminingY -= nextYPos;
  384.  
  385. // configures the button sector properties
  386. buttonSectorY = reminingY / JOIN_ROOM_FORM_BUTTON_SECTOR_Y;
  387. refreshY = buttonSectorY / JOIN_ROOM_FORM_REFRESH_HEIGHT;
  388. joinY = buttonSectorY / JOIN_ROOM_FORM_JOIN_HEIGHT;
  389. alligmentFromButtonsY = (buttonSectorY - refreshY - joinY) / 2;
  390. alligmentFromButtonsX = (this.Width - this.Width / JOIN_ROOM_FORM_REFRESH_WIDTH - this.Width / JOIN_ROOM_FORM_JOIN_WIDTH) / 3;
  391.  
  392. // configures and adds the refresh button
  393. PictureBox PBRefresh = new MyPictureBox("PBRefresh", "refresh", new Size(this.Width / JOIN_ROOM_FORM_REFRESH_WIDTH, refreshY));
  394. PBRefresh.Location = new Point(alligmentFromButtonsX, PBClose.Location.Y - alligmentFromButtonsY - refreshY);
  395. PBRefresh.Click += (EventHandler)PBRefresh_Click;
  396. this.Controls.Add(PBRefresh);
  397.  
  398. // configures and adds the join button
  399. PictureBox PBJoin = new MyPictureBox("PBJoin", "joinBTN", new Size(this.Width / JOIN_ROOM_FORM_JOIN_WIDTH, this.Height / JOIN_ROOM_FORM_JOIN_HEIGHT));
  400. PBJoin.Location = new Point(this.Width - PBJoin.Width - alligmentFromButtonsX, PBRefresh.Location.Y);
  401. PBJoin.Click += (EventHandler)PBJoin_Click;
  402. //PBJoin.Click += (EventHandler)CleanAllControlers;
  403. //PBJoin.Click += (EventHandler)InitInsideRoom;
  404. this.Controls.Add(PBJoin);
  405.  
  406. reminingY = PBRefresh.Location.Y - LBLSubject.Location.Y - LBLSubject.Height - alligmentFromSubjectY;
  407. ListBox LBRooms = new MyListBox("LBRooms", new Size(this.Width / JOIN_ROOM_FORM_ROOMS_LB_WIDTH, reminingY / 4));
  408. ListBox LBUsers = new MyListBox("LBUsers", new Size(this.Width / JOIN_ROOM_FORM_USERS_LB_WIDTH, reminingY / 4));
  409.  
  410. alligmentFromListsX = (this.Width - LBRooms.Width - LBUsers.Width) / 3;
  411.  
  412. Label LBLRooms = new MyLabel("Rooms:", "LBLRooms");
  413. Label LBLUsers = new MyLabel("Users:", "LBLUsers");
  414.  
  415. // adds the rooms list box
  416. LBRooms.Location = new Point(this.Width / 2 - LBRooms.Width / 2, nextYPos + LBLRooms.Height);
  417. LBRooms.SelectedIndexChanged += (EventHandler)LBRooms_SelectedIndexChanged;
  418. this.Controls.Add(LBRooms);
  419.  
  420. // sets the users list box
  421. LBUsers.Location = new Point(alligmentFromListsX, nextYPos + LBLUsers.Height);
  422. LBUsers.Visible = false;
  423. this.Controls.Add((LBUsers));
  424.  
  425. // configures and adds the label of rooms
  426. LBLRooms.Location = new Point(LBRooms.Location.X + LBRooms.Width / 2 - LBLRooms.Width / 2, nextYPos);
  427. this.Controls.Add(LBLRooms);
  428.  
  429. // configures and adds the label of users
  430. LBLUsers.Location = new Point(LBUsers.Location.X + LBUsers.Width / 2 - LBLRooms.Width / 2, nextYPos);
  431. LBLUsers.Visible = false;
  432. this.Controls.Add(LBLUsers);
  433.  
  434. // sets the back button
  435. int PCHeight = this.Height / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  436. int PCWidth = this.Width / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  437. PictureBox PBBack = new MyPictureBox("PBBack", "back", new Size(PCWidth, PCHeight));
  438. PBBack.Location = new Point(this.Width - PCWidth, this.Height / ALLIGMENT_FROM_TOP);
  439. PBBack.Click += (EventHandler)CleanAllControlers;
  440. PBBack.Click += (EventHandler)InitMainScreen;
  441. this.Controls.Add(PBBack);
  442. }
  443.  
  444. /// <summary>
  445. /// changes the room list in the join room form
  446. /// should be called when the join room button or refresh button were clicked
  447. /// </summary>
  448. /// <param name="roomsNames">the names of the rooms that will be shown on the list box</param>
  449. private void changeRoomsList(List<string> roomsNames)
  450. {
  451. if (roomsNames.Count < 1) // so that the list will still be shown even if there are no available rooms
  452. {
  453. roomsNames.Add("");
  454. }
  455.  
  456. ListBox LBUsers = this.Controls.Find("LBUsers", false).FirstOrDefault() as ListBox;
  457. Label LBLUsers = this.Controls.Find("LBLUsers", false).FirstOrDefault() as Label;
  458.  
  459. // makes the users lists to disapear
  460. LBUsers.Visible = false;
  461. LBLUsers.Visible = false;
  462.  
  463. ListBox LBRooms = this.Controls.Find("LBRooms", false).FirstOrDefault() as ListBox;
  464. if (LBRooms != null)
  465. {
  466. LBRooms.Items.Clear();
  467. foreach (string name in roomsNames)
  468. {
  469. LBRooms.Items.Add(name);
  470. }
  471.  
  472. Label LBLRooms = this.Controls.Find("LBLRooms", false).FirstOrDefault() as Label;
  473. PictureBox PBRefresh = this.Controls.Find("PBRefresh", false).FirstOrDefault() as PictureBox;
  474.  
  475. // sets the height of the list box
  476. if (LBLRooms != null && PBRefresh != null)
  477. {
  478. int maxHeight = PBRefresh.Location.Y - LBLRooms.Location.Y - LBLRooms.Height - alligmentFromSubjectY;
  479. int height = TextRenderer.MeasureText("k", this.Font).Height;
  480. int maxObj = maxHeight / height;
  481.  
  482. LBRooms.Height = roomsNames.Count > maxObj ? height * maxObj : roomsNames.Count * height;
  483. }
  484.  
  485. // moves the list box to its proper place
  486. LBRooms.Location = new Point(this.Width / 2 - LBRooms.Width / 2, LBRooms.Location.Y);
  487. LBLRooms.Location = new Point(LBRooms.Location.X + LBRooms.Width / 2 - LBLRooms.Width / 2, LBLRooms.Location.Y);
  488.  
  489. this.Update();
  490. }
  491. }
  492.  
  493. /// <summary>
  494. /// changes the users in room list box
  495. /// should be called when an item on the rooms list is clicked
  496. /// </summary>
  497. /// <param name="users">the users that will be presented on the users list</param>
  498. private void changeUsersInRoom(List<string> users)
  499. {
  500. if (users.Count < 1)
  501. {
  502. users.Add("");
  503. }
  504.  
  505. ListBox LBUsers = this.Controls.Find("LBUsers", false).FirstOrDefault() as ListBox;
  506. Label LBLUsers = this.Controls.Find("LBLUsers", false).FirstOrDefault() as Label;
  507. Label LBLRooms = this.Controls.Find("LBLRooms", false).FirstOrDefault() as Label;
  508. PictureBox PBRefresh = this.Controls.Find("PBRefresh", false).FirstOrDefault() as PictureBox;
  509. ListBox LBRooms = this.Controls.Find("LBRooms", false).FirstOrDefault() as ListBox;
  510.  
  511. // sets the height of the list box
  512. if (LBLUsers != null && PBRefresh != null && LBUsers != null)
  513. {
  514. int maxHeight = PBRefresh.Location.Y - LBLUsers.Location.Y - LBLUsers.Height - alligmentFromSubjectY;
  515. int height = TextRenderer.MeasureText("k", this.Font).Height;
  516. int maxObj = maxHeight / height;
  517.  
  518. // changes the items in the list box
  519. LBUsers.Items.Clear();
  520. foreach (string user in users)
  521. {
  522. LBUsers.Items.Add(user);
  523. }
  524.  
  525. LBUsers.Height = users.Count > maxObj ? height * maxObj : users.Count * height;
  526.  
  527. // sets the rooms stuff to their proper place
  528. LBRooms.Location = new Point(this.Width - LBRooms.Width - LBUsers.Location.X, LBRooms.Location.Y);
  529. LBLRooms.Location = new Point(LBRooms.Location.X + LBRooms.Width / 2 - LBLRooms.Width / 2, LBLRooms.Location.Y);
  530.  
  531. LBUsers.Visible = true;
  532. LBLUsers.Visible = true;
  533. }
  534. }
  535.  
  536. /// <summary>
  537. /// changes the form to inside room performance
  538. /// </summary>
  539. /// <param name="sender"></param>
  540. /// <param name="args"></param>
  541. private void InitInsideRoom(object sender, EventArgs args)
  542. {
  543. int reminingY = this.Height;
  544. int nextYPos = 0;
  545. int buttonSectorY = 0;
  546. int buttonsHeight = this.Height / IN_ROOM_FORM_BUTTON_HEIGHT;
  547. int buttonWidth = this.Width / IN_ROOM_FORM_BUTTON_WIDTH;
  548. int alligmentFromButtonsY = 0;
  549. int alligmentFromLabelsY = 0;
  550. List<Label> labels = new List<Label>();
  551.  
  552. this.PBClose.Visible = false;
  553.  
  554. // sets the subject label
  555. Label LBLSubject = new MyLabel("You are in " + this.roomName + " room" , "LBLSubject"); // i create a label to know the width of its text
  556. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  557. this.Controls.Add(LBLSubject);
  558.  
  559. nextYPos = LBLSubject.Location.Y + LBLSubject.Height + alligmentFromSubjectY;
  560. reminingY -= nextYPos;
  561. buttonSectorY = reminingY / IN_ROOM_FORM_BUTTON_SECTOR_Y;
  562. reminingY -= buttonSectorY;
  563. alligmentFromButtonsY = (buttonSectorY - buttonsHeight) / 2;
  564.  
  565. if (sender is PictureBox) // in case its a user presentation
  566. {
  567. Button BTNLeave = new MyButton("BTNLeave", new Size(buttonWidth, buttonsHeight), "Leave Room");
  568. BTNLeave.Location = new Point(this.Width / 2 - buttonWidth / 2, this.Height - buttonsHeight - alligmentFromButtonsY);
  569. BTNLeave.Click += (EventHandler)BTNLeave_Click;
  570. this.Controls.Add(BTNLeave);
  571. }
  572. else if (sender is Button) // in case its admin presentation
  573. {
  574. int alligmentFromButtonsX = (this.Width - buttonWidth * 2) / 3;
  575.  
  576. // configures and adds the close room button
  577. Button BTNCloseRoom = new MyButton("BTNCloseRoom", new Size(buttonWidth, buttonsHeight), "Close Room");
  578. BTNCloseRoom.Location = new Point(alligmentFromButtonsX, this.Height - BTNCloseRoom.Height - alligmentFromButtonsY);
  579. BTNCloseRoom.Click += (EventHandler)BTNCloseRoom_Click;
  580. this.Controls.Add(BTNCloseRoom);
  581.  
  582. // configures and adds the start game button
  583. Button BTNStartGame = new MyButton("BTNStartGame", new Size(buttonWidth, buttonsHeight), "Start Game");
  584. BTNStartGame.Location = new Point(this.Width - buttonWidth - alligmentFromButtonsX, BTNCloseRoom.Location.Y);
  585. BTNStartGame.Click += (EventHandler)BTNStartGame_Click;
  586. this.Controls.Add(BTNStartGame);
  587.  
  588. labels.Add(new MyLabel("Max number of users: ", "LBLMaxUsers"));
  589. }
  590.  
  591. Label LBLparticipates = new MyLabel("current participates are: ", "LBLparticipates");
  592.  
  593. // configures and adds the participates list box
  594. ListBox LBpart = new MyListBox("LBpart", new Size(this.Width / IN_ROOM_FORM_LB_WIDTH, reminingY / 4));
  595. LBpart.Location = new Point(this.Width / 2 - LBpart.Width / 2, nextYPos + LBLparticipates.Height);
  596. this.Controls.Add(LBpart);
  597.  
  598. // configures and adds the label of participates
  599. LBLparticipates.Location = new Point(LBpart.Location.X + LBpart.Width / 2 - LBLparticipates.Width / 2, nextYPos);
  600. this.Controls.Add(LBLparticipates);
  601.  
  602. // sets the room properties labels
  603. labels.Add(new MyLabel("Number of questions: ", "LBLNumOfQuestions"));
  604. labels.Add(new MyLabel("Time per question: ", "LBLTimePerQuestion"));
  605.  
  606. // calculates the needed alligment
  607. alligmentFromLabelsY = (reminingY - labels[0].Height * labels.Count) / (labels.Count + 1);
  608. nextYPos += alligmentFromLabelsY;
  609.  
  610. foreach (Label l in labels)
  611. {
  612. l.Location = new Point(0, nextYPos);
  613. nextYPos += alligmentFromLabelsY;
  614. this.Controls.Add(l);
  615. }
  616. }
  617.  
  618. /// <summary>
  619. /// changes the list box of participates
  620. /// </summary>
  621. /// <param name="users">the users to be added to the list box</param>
  622. private void ChangeLBpart(List<string> users)
  623. {
  624. Label LBLparticipates = this.Controls.Find("LBLparticipates", false).FirstOrDefault() as Label;
  625. ListBox LBpart = this.Controls.Find("LBpart", false).FirstOrDefault() as ListBox;
  626.  
  627. if(users.Count < 1)
  628. {
  629. users.Add("");
  630. }
  631.  
  632. if(LBLparticipates != null && LBpart != null)
  633. {
  634. int maxHeight = this.Height - this.Height / IN_ROOM_FORM_BUTTON_SECTOR_Y - LBLparticipates.Location.Y - LBLparticipates.Height;
  635. int height = TextRenderer.MeasureText("k", LBpart.Font).Height;
  636. int maxObj = maxHeight / height;
  637.  
  638. // changes the height of the list box according to the items in it
  639. LBpart.Height = users.Count > maxObj ? height * maxObj : height * users.Count;
  640.  
  641. LBpart.Items.Clear();
  642. foreach(string user in users)
  643. {
  644. LBpart.Items.Add(user);
  645. }
  646. }
  647. }
  648.  
  649. /// <summary>
  650. /// changes the labels of in room properties
  651. /// in case of user presentation the properties param should be made out of the string the server sent
  652. /// in case of admin presentation the properties param should be the global parameter roomProperties
  653. /// </summary>
  654. /// <param name="properties">the properties that would be in the labels, the order should be maxusers(if exists), numberOfQuestions, time per questions</param>
  655. private void ChangeLabels(List<string> properties)
  656. {
  657. Label[] labels = new Label[properties.Count];
  658.  
  659. if(properties.Count > 2)
  660. {
  661. labels[0] = this.Controls.Find("LBLMaxUsers", false).FirstOrDefault() as Label;
  662. }
  663.  
  664. labels[labels.Length - 2] = this.Controls.Find("LBLNumOfQuestions", false).FirstOrDefault() as Label;
  665. labels[labels.Length - 1] = this.Controls.Find("LBLTimePerQuestion", false).FirstOrDefault() as Label;
  666.  
  667. for (int i = 0; i < labels.Length; i++)
  668. {
  669. if(labels[i] != null)
  670. {
  671. labels[i].Text += properties[i];
  672. labels[i].Width = TextRenderer.MeasureText(labels[i].Text, labels[i].Font).Width;
  673. }
  674. }
  675. }
  676.  
  677. /// <summary>
  678. /// changes the form to personal status
  679. /// </summary>
  680. /// <param name="sender"></param>
  681. /// <param name="args"></param>
  682. private void InitPersonalStatus(object sender, EventArgs args)
  683. {
  684. int reminingY = this.PBClose.Location.Y;
  685. int nextYPos = 0;
  686. int alligmentFromLabelsY = 0;
  687.  
  688. // sets the background of the form to personal status background
  689. this.BackgroundImage = Properties.Resources.personalStatusBG;
  690.  
  691. // sets the font of the form to black
  692. this.ForeColor = Color.Black;
  693.  
  694. // sets the subject label
  695. Label LBLSubject = new MyLabel("My Performance: ", "LBLSubject"); // i create a label to know the width of its text
  696. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  697. this.Controls.Add(LBLSubject);
  698.  
  699. nextYPos = LBLSubject.Location.Y + LBLSubject.Height + alligmentFromSubjectY;
  700. reminingY -= nextYPos;
  701.  
  702. Label LBLnumOfGames = new MyLabel("number of Games: ", "LBLnumOfGames"); // i create a label so i can know the height of it
  703.  
  704. alligmentFromLabelsY = (reminingY - LBLnumOfGames.Height * 4) / 5;
  705. nextYPos += alligmentFromLabelsY;
  706.  
  707. // configures and adds the number of games label
  708. LBLnumOfGames.Location = new Point(0, nextYPos); // i put 0 in x of all labels because i change it to the middle when the real text arrives from server
  709. this.Controls.Add(LBLnumOfGames);
  710.  
  711. nextYPos += alligmentFromLabelsY;
  712.  
  713. // configures and adds the number of right answers label
  714. Label LBLnumOfRightAns = new MyLabel("number of right answers: ", "LBLnumOfRightAns");
  715. LBLnumOfRightAns.Location = new Point(0, nextYPos);
  716. this.Controls.Add(LBLnumOfRightAns);
  717.  
  718. nextYPos += alligmentFromLabelsY;
  719.  
  720. // configures and adds the number of wrong answers label
  721. Label LBLnumOfWrongAns = new MyLabel("number of wrong answers: ", "LBLnumOfWrongAns");
  722. LBLnumOfWrongAns.Location = new Point(0, nextYPos);
  723. this.Controls.Add(LBLnumOfWrongAns);
  724.  
  725. nextYPos += alligmentFromLabelsY;
  726.  
  727. // configures and adds the average time per answer
  728. Label LBLavgTimePerAns = new MyLabel("average time for answer: ", "LBLavgTimePerAns");
  729. LBLavgTimePerAns.Location = new Point(0, nextYPos);
  730. this.Controls.Add(LBLavgTimePerAns);
  731.  
  732. // sets the back button
  733. int PCHeight = this.Height / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  734. int PCWidth = this.Width / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  735. PictureBox PBBack = new MyPictureBox("PBBack", "back", new Size(PCWidth, PCHeight));
  736. PBBack.Location = new Point(this.Width - PCWidth, this.Height / ALLIGMENT_FROM_TOP);
  737. PBBack.Click += (EventHandler)CleanAllControlers;
  738. PBBack.Click += (EventHandler)InitMainScreen;
  739. this.Controls.Add(PBBack);
  740. }
  741.  
  742. /// <summary>
  743. /// changes the labels of personal status' form, has to be be used after InitPersonalStatus function used
  744. /// </summary>
  745. /// <param name="arr">an array that contains the values of the fields example { 4, 5, 10, 3 }</param>
  746. private void ChangePersonalStatus(string[] arr)
  747. {
  748. if(arr.Length > 3)
  749. {
  750. Label[] labels = new Label[4];
  751. labels[0] = this.Controls.Find("LBLnumOfGames", false).FirstOrDefault() as Label;
  752. labels[1] = this.Controls.Find("LBLnumOfRightAns", false).FirstOrDefault() as Label;
  753. labels[2] = this.Controls.Find("LBLnumOfWrongAns", false).FirstOrDefault() as Label;
  754. labels[3] = this.Controls.Find("LBLavgTimePerAns", false).FirstOrDefault() as Label;
  755.  
  756. for(int i = 0; i < 4; i++)
  757. {
  758. if(labels[i] != null)
  759. {
  760. if (i == 3)
  761. {
  762. arr[i] = String.Format("{0}.{1}", (int.Parse(arr[i]) / 100).ToString(), (int.Parse(arr[i]) % 100).ToString());
  763. Invoke((MethodInvoker)(() => labels[i].Text += arr[i]));
  764. }
  765. else
  766. {
  767. Invoke((MethodInvoker)(() => labels[i].Text += (int.Parse(arr[i])).ToString()));
  768. }
  769. Invoke((MethodInvoker)(() => labels[i].Width = TextRenderer.MeasureText(labels[i].Text, labels[i].Font).Width));
  770. Invoke((MethodInvoker)(() => labels[i].Location = new Point(this.Width / 2 - labels[i].Width / 2, labels[i].Location.Y)));
  771. }
  772. }
  773. }
  774. }
  775.  
  776. /// <summary>
  777. /// changes the form to show the best scores
  778. /// </summary>
  779. /// <param name="sender"></param>
  780. /// <param name="args"></param>
  781. private void InitBestScores(object sender, EventArgs args)
  782. {
  783. int reminingY = PBClose.Location.Y;
  784. int nextYPos = 0;
  785. int pictureBoxHeight = 0;
  786. int pictureBoxWidth = 0;
  787. int alligmentFromPicturesY = 0;
  788. int alligmentFromPictureX = 0;
  789.  
  790. // sets the background image to best scores BG
  791. this.BackgroundImage = Properties.Resources.winners;
  792.  
  793. // sets the font of the form to black
  794. this.ForeColor = Color.Black;
  795.  
  796. // sets the subject label
  797. Label LBLSubject = new MyLabel("Best Scores: ", "LBLSubject"); // i create a label to know the width of its text
  798. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  799. this.Controls.Add(LBLSubject);
  800.  
  801. // creates the label that contains the first player's scores
  802. Label LBL1st = new MyLabel("ABCD", "LBL1st"); // i put text in the label so i can know its properties
  803.  
  804. // sets the next position for the picture boxes sector
  805. nextYPos += (LBLSubject.Location.Y + LBLSubject.Height + alligmentFromSubjectY);
  806. reminingY -= nextYPos;
  807.  
  808. // configures the properties of the picture boxes
  809. pictureBoxHeight = reminingY / BEST_SCORES_FORM_PB_HEIGHT;
  810. pictureBoxWidth = this.Width / BEST_SCORES_FORM_PB_WIDTH;
  811.  
  812. // configures the picture box sector
  813. alligmentFromPicturesY = (reminingY - pictureBoxHeight * 2 - LBL1st.Height * 2) / 2;
  814. alligmentFromPictureX = (this.Width - pictureBoxWidth * 3) / 4;
  815. nextYPos += alligmentFromPicturesY;
  816.  
  817. // sets the first place picture box
  818. PictureBox PB1st = new MyPictureBox("PB1st", "_1st", new Size(pictureBoxWidth, pictureBoxHeight));
  819. PB1st.Location = new Point(alligmentFromPictureX * 2 + pictureBoxWidth, nextYPos);
  820. this.Controls.Add(PB1st);
  821.  
  822. LBL1st.Location = new Point(PB1st.Location.X + PB1st.Width / 2, nextYPos - LBL1st.Height);
  823. this.Controls.Add(LBL1st);
  824.  
  825. // sets the postion to the next picture box
  826. nextYPos += alligmentFromPicturesY;
  827.  
  828. // sets the second best score picture box
  829. PictureBox PB2nd = new MyPictureBox("PB2nd", "_2nd", new Size(pictureBoxWidth, pictureBoxHeight));
  830. PB2nd.Location = new Point(alligmentFromPictureX, nextYPos);
  831. this.Controls.Add(PB2nd);
  832.  
  833. // sets the label of the second best score
  834. Label LBL2nd = new MyLabel("ABCD", "LBL2nd"); // i put text in the label so i can know its properties
  835. LBL2nd.Location = new Point(PB2nd.Location.X + PB2nd.Width / 2, PB2nd.Location.Y - LBL2nd.Height);
  836. this.Controls.Add(LBL2nd);
  837.  
  838. // sets the picture box of the second best score
  839. PictureBox PB3rd = new MyPictureBox("PB3rd", "_3rd", new Size(pictureBoxWidth, pictureBoxHeight));
  840. PB3rd.Location = new Point(this.Width - alligmentFromPictureX - PB3rd.Width, nextYPos);
  841. this.Controls.Add(PB3rd);
  842.  
  843. // sets the label of th ethird best score
  844. Label LBL3rd = new MyLabel("ABCD", "LBL3rd"); // i put text in the label so i can know its properties
  845. LBL3rd.Location = new Point(PB3rd.Location.X + PB3rd.Width / 2, nextYPos - LBL3rd.Height);
  846. this.Controls.Add(LBL3rd);
  847.  
  848. // sets the back button
  849. int PCHeight = this.Height / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  850. int PCWidth = this.Width / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  851. PictureBox PBBack = new MyPictureBox("PBBack", "back", new Size(PCWidth, PCHeight));
  852. PBBack.Location = new Point(this.Width - PCWidth, this.Height / ALLIGMENT_FROM_TOP);
  853. PBBack.Click += (EventHandler)CleanAllControlers;
  854. PBBack.Click += (EventHandler)InitMainScreen;
  855. this.Controls.Add(PBBack);
  856. }
  857.  
  858. /// <summary>
  859. /// changes the best scores labels
  860. /// should be used when clicked on get best scores button, after InitBestScores was called
  861. /// </summary>
  862. /// <param name="arr">an array of pairs in which the key is the name of the user and the value is his score</param>
  863. private void changeBestScores(KeyValuePair<string, string>[] arr)
  864. {
  865. Label[] labels = new Label[3];
  866.  
  867. labels[0] = this.Controls.Find("LBL1st", false).FirstOrDefault() as Label;
  868. labels[1] = this.Controls.Find("LBL2nd", false).FirstOrDefault() as Label;
  869. labels[2] = this.Controls.Find("LBL3rd", false).FirstOrDefault() as Label;
  870.  
  871. for (int i = 0; i < 3; i++ )
  872. {
  873. if(labels[i] != null)
  874. {
  875. labels[i].Text = arr[i].Key + ": " + arr[i].Value;
  876. labels[i].Width = TextRenderer.MeasureText(labels[i].Text, labels[i].Font).Width;
  877. labels[i].Location = new Point(labels[i].Location.X - labels[i].Width / 2, labels[i].Location.Y);
  878. }
  879. }
  880. }
  881.  
  882. /// <summary>
  883. /// cleans the screen from all the controls on it
  884. /// </summary>
  885. private void CleanAllControlers(object sender, EventArgs args)
  886. {
  887. //foreach (Control c in this.Controls)
  888. //{
  889. // MessageBox.Show(c.GetType().ToString());
  890. // this.Controls.Remove(c);
  891. //}
  892.  
  893. this.Controls.Clear();
  894. this.Controls.Add(PBClose);
  895. }
  896.  
  897. private void PBClose_Click(object sender, EventArgs args)
  898. {
  899. this.Close();
  900. }
  901.  
  902. private void BTNBest_Click(object sender, EventArgs args)
  903. {
  904. stream.sendData("223");
  905. }
  906.  
  907. private void PBLogOut_Click(object sender, EventArgs args)
  908. {
  909. stream.sendData("201");
  910. }
  911.  
  912. private void BTNPersonal_Click(object sender, EventArgs args)
  913. {
  914. stream.sendData("225");
  915. }
  916.  
  917. private void BTNJoin_Click(object sender, EventArgs args)
  918. {
  919. stream.sendData("205");
  920. }
  921.  
  922. private void PBRefresh_Click(object sender, EventArgs args)
  923. {
  924. stream.sendData("205");
  925. }
  926.  
  927. private void PBJoin_Click(object sender, EventArgs args)
  928. {
  929. ListBox LBRooms = this.Controls.Find("LBRooms", false).FirstOrDefault() as ListBox;
  930.  
  931. int roomID = LBRooms.SelectedIndex;
  932.  
  933. this.stream.sendData("209" + this.roomsID[roomID]);
  934. }
  935.  
  936. private void LBRooms_SelectedIndexChanged(object sender, System.EventArgs e)
  937. {
  938. ListBox LBRooms = this.Controls.Find("LBRooms", false).FirstOrDefault() as ListBox;
  939.  
  940. int roomID = LBRooms.SelectedIndex;
  941.  
  942. this.stream.sendData("207" + this.roomsID[roomID]);
  943. }
  944.  
  945. /// <summary>
  946. /// sends the username, password and email to server according to the format for sign up
  947. /// </summary>
  948. private void PBSign_Up_Click(object sender, EventArgs args)
  949. {
  950. TextBox txtUserName = (TextBox)this.Controls.Find("TXTusername", false)[0];
  951. TextBox txtPassword = (TextBox)this.Controls.Find("TXTpsw", false)[0];
  952. TextBox txtEmail = (TextBox)this.Controls.Find("TXTemail", false)[0];
  953.  
  954. if (txtUserName != null && txtPassword != null && txtEmail != null)
  955. {
  956. this.stream.sendData(String.Format("203{0:00}{1}{2:00}{3}{4:00}{5}", txtUserName.Text.Length, txtUserName.Text, txtPassword.Text.Length, txtPassword.Text, txtEmail.Text.Length, txtEmail.Text));
  957. }
  958. }
  959.  
  960. /// <summary>
  961. /// sends the username and password to server according to the format for sign in
  962. /// </summary>
  963. private void PBSign_In_Click(object sender, EventArgs e)
  964. {
  965. TextBox txtUserName = (TextBox)this.Controls.Find("TXTusername", false)[0];
  966. TextBox txtPassword = (TextBox)this.Controls.Find("TXTpsw", false)[0];
  967.  
  968. if (txtUserName != null && txtPassword != null)
  969. {
  970. this.stream.sendData(String.Format("200{0:00}{1}{2:00}{3}", txtUserName.Text.Length, txtUserName.Text, txtPassword.Text.Length, txtPassword.Text));
  971. }
  972. }
  973.  
  974. private void BTNLeave_Click(object sender, EventArgs args)
  975. {
  976. // executed when leave button clicked ( in room form )
  977. this.PBClose.Visible = true;
  978. }
  979.  
  980. private void BTNCloseRoom_Click(object sender, EventArgs args)
  981. {
  982. // executed when close room button clicked ( in room form )
  983. this.PBClose.Visible = true;
  984. }
  985.  
  986. private void BTNStartGame_Click(object sender, EventArgs args)
  987. {
  988. // executed when start game button clicked ( in room form )
  989. }
  990.  
  991. /// <summary>
  992. /// handles the messags from server
  993. /// </summary>
  994. private void handleMessage()
  995. {
  996. string msg = "";
  997. while (true)
  998. {
  999. msg = this.stream.recvData(); // the message from the server
  1000.  
  1001. switch (int.Parse(msg.Substring(0, 3)))
  1002. {
  1003. case 102:
  1004. this.HandleSignIn(msg.Substring(3, 4)); // in case of sign in
  1005. break;
  1006. case 104:
  1007. this.HandleSignUp(msg.Substring(3, 4)); // in case of sign up
  1008. break;
  1009. case 124:
  1010. this.HandleBestScores(msg.Substring(3)); // in case of best scores
  1011. break;
  1012. case 126:
  1013. this.HandlePersonalStatus(msg.Substring(3)); // in case of personal status
  1014. break;
  1015. case 106:
  1016. this.HandleGetRooms(msg.Substring(3)); // in case of get rooms
  1017. break;
  1018. case 108:
  1019. this.HandleGetUsers(msg.Substring(3)); // in case of get users
  1020. break;
  1021. case 110:
  1022. this.HandleJoinRoon(msg.Substring(3)); // in case of join room
  1023. break;
  1024. }
  1025. }
  1026. }
  1027.  
  1028. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  1029. {
  1030. this.t.Abort();
  1031. this.stream.sendData("299");
  1032. this.stream.CloseStream();
  1033. this.PBClose.Dispose();
  1034. }
  1035.  
  1036. /// <summary>
  1037. /// handles sign in request
  1038. /// </summary>
  1039. /// <param name="msg"> the message from the server </param>
  1040. private void HandleSignIn(string msg)
  1041. {
  1042. switch (int.Parse(msg))
  1043. {
  1044. case 0:
  1045. uid = ((TextBox)this.Controls.Find("TXTusername", false).FirstOrDefault()).Text; // to show his username in main screen
  1046. Invoke((MethodInvoker)(() => CleanAllControlers(null, EventArgs.Empty)));
  1047. Invoke((MethodInvoker)(() => InitMainScreen(null, EventArgs.Empty)));
  1048. break;
  1049. case 1:
  1050. MessageBox.Show("Wrong Details");
  1051. break;
  1052. case 2:
  1053. MessageBox.Show("User is already connected");
  1054. break;
  1055. }
  1056. }
  1057.  
  1058. /// <summary>
  1059. /// handles sign up request
  1060. /// </summary>
  1061. /// <param name="msg"> message from the server </param>
  1062. private void HandleSignUp(string msg)
  1063. {
  1064. switch (int.Parse(msg))
  1065. {
  1066. case 0:
  1067. Invoke((MethodInvoker)(() => CleanAllControlers(null, EventArgs.Empty)));
  1068. Invoke((MethodInvoker)(() => this.InitLoginScreen(null, EventArgs.Empty)));
  1069. break;
  1070. case 1:
  1071. MessageBox.Show("Pass illegal");
  1072. break;
  1073. case 2:
  1074. MessageBox.Show("Username is already exists");
  1075. break;
  1076. case 3:
  1077. MessageBox.Show("Username is illegal");
  1078. break;
  1079. case 4:
  1080. MessageBox.Show("Other");
  1081. break;
  1082. }
  1083. }
  1084.  
  1085. /// <summary>
  1086. /// handles best scores request
  1087. /// </summary>
  1088. /// <param name="msg"> message from the server </param>
  1089. private void HandleBestScores(string msg)
  1090. {
  1091. List<string> elem = new List<string>();
  1092.  
  1093. int[] arr = new int[6] { 2, -6, 2, -6, 2, -6 };
  1094.  
  1095. Helper.convertBytes(arr, elem, msg);
  1096.  
  1097. KeyValuePair<string, string>[] arrPairs = new KeyValuePair<string, string>[3];
  1098.  
  1099. if (elem.Count > 5)
  1100. {
  1101. for (int i = 0; i < 3; i++)
  1102. {
  1103. arrPairs[i] = new KeyValuePair<string, string>(elem[i * 2], (int.Parse(elem[i * 2 + 1])).ToString());
  1104. }
  1105. }
  1106.  
  1107. Invoke((MethodInvoker)(() => this.changeBestScores(arrPairs)));
  1108. }
  1109.  
  1110. /// <summary>
  1111. /// handles personal status request
  1112. /// </summary>
  1113. /// <param name="msg"> message from the server </param>
  1114. private void HandlePersonalStatus(string msg)
  1115. {
  1116. List<string> elem = new List<string>();
  1117.  
  1118. int[] arr = new int[4] { -4, -6, -6, -4 };
  1119.  
  1120. Helper.convertBytes(arr, elem, msg);
  1121.  
  1122. this.ChangePersonalStatus(elem.ToArray());
  1123. }
  1124.  
  1125. private void HandleGetRooms(string msg)
  1126. {
  1127. msg = "0001000508OUR_ROOM";
  1128.  
  1129. List<string> elem = new List<string>();
  1130.  
  1131. int numberOfRooms = int.Parse(msg.Substring(0, 4));
  1132.  
  1133. int[] arr = new int[numberOfRooms * 2];
  1134.  
  1135. for (int i = 0; i < numberOfRooms; i++)
  1136. {
  1137. arr[i * 2] = -4;
  1138. arr[i * 2 + 1] = 2;
  1139. }
  1140.  
  1141. Helper.convertBytes(arr, elem, msg.Substring(4));
  1142.  
  1143. for (int i = 0; i < elem.Count; i += 2)
  1144. {
  1145. this.roomsID.Add(elem[i]);
  1146. elem.RemoveAt(i);
  1147. }
  1148.  
  1149. Invoke((MethodInvoker)(() => this.changeRoomsList(elem)));
  1150. }
  1151.  
  1152. private void HandleGetUsers(string msg)
  1153. {
  1154. msg = "205dolev04gali";
  1155.  
  1156. if (msg == "0") // in case of the game already start
  1157. {
  1158. MessageBox.Show("The game already start, please refresh");
  1159. return;
  1160. }
  1161.  
  1162. List<string> elem = new List<string>();
  1163.  
  1164. int numberOfUsers = int.Parse(msg.Substring(0, 1));
  1165.  
  1166. int[] arr = new int[numberOfUsers];
  1167.  
  1168. for (int i = 0; i < numberOfUsers; i++)
  1169. {
  1170. arr[i] = 2;
  1171. }
  1172.  
  1173. Helper.convertBytes(arr, elem, msg.Substring(1));
  1174.  
  1175. if(this.Controls.Find("LBLparticipates", false).FirstOrDefault() as Label != null)
  1176. {
  1177. Invoke((MethodInvoker)(() => this.ChangeLBpart(elem)));
  1178. }
  1179. else
  1180. {
  1181. Invoke((MethodInvoker)(() => this.changeUsersInRoom(elem)));
  1182. }
  1183. }
  1184.  
  1185. private void HandleJoinRoon(string msg)
  1186. {
  1187. msg = "02040";
  1188. if(msg.Substring(0,1) == "0")
  1189. {
  1190. Invoke((MethodInvoker)(() => this.CleanAllControlers(null, EventArgs.Empty)));
  1191. Invoke((MethodInvoker)(() => this.InitInsideRoom(null, EventArgs.Empty)));
  1192.  
  1193. List<string> elem = new List<string>();
  1194.  
  1195. int[] arr = new int[2] { -2, -2 };
  1196.  
  1197. Helper.convertBytes(arr, elem, msg.Substring(1));
  1198.  
  1199. Invoke((MethodInvoker)(() => this.ChangeLabels(elem)));
  1200. }
  1201. else if (msg.Substring(0,1) == "1")
  1202. {
  1203. MessageBox.Show("failed - room is full");
  1204. }
  1205. else
  1206. {
  1207. MessageBox.Show("failed - room not exist or other reason");
  1208. }
  1209. }
  1210. }
  1211. }
  1212.  
  1213.  
  1214. // TODO:
  1215. // set font according to resulution
  1216. // remove the size of the Close button from the login and signup pages
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement