Advertisement
Guest User

Untitled

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