Advertisement
Guest User

Untitled

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