Advertisement
Guest User

Untitled

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