Advertisement
Guest User

Untitled

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