Advertisement
Guest User

Untitled

a guest
Jul 6th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 79.50 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.  
  111. // MyTxtBox Class:
  112. // the default width of the text box
  113. MyTxtBox._width = (this.Width / LOG_IN_FORM_TXT_X_SIZE);
  114.  
  115. // current class:
  116. // sets the close button
  117. PBClose = new MyPictureBox("PBClose", "close", new Size(this.Width / CLOSE_GAME_BUTTON_WIDTH, this.Height / CLOSE_GAME_BUTTON_HEIGHT));
  118. PBClose.Location = new Point(this.Width / 2 - PBClose.Width / 2, this.Height - PBClose.Height - this.Height / ALLIGMENT_FROM_TOP);
  119. PBClose.Click += PBClose_Click;
  120. this.Controls.Add(PBClose);
  121.  
  122. // sets the alligment from top of screen
  123. alligmentFromSubjectY = this.Height / ALLIGMENT_FROM_TOP;
  124.  
  125. uid = "tal";
  126. }
  127.  
  128. /// <summary>
  129. /// makes the login is screen
  130. /// </summary>
  131. public void InitLoginScreen(object sender, EventArgs args)
  132. {
  133. int reminingY = this.Height;
  134. int nextYPos = 0;
  135. int xOfTextBoxes = 0;
  136. int sizeOfPictureBox = 0;
  137. int alligmentFromPictureBox = 0;
  138. int alligmentFromTextBoxes = 0;
  139. int alligmentFromTextBoxesToLabels = this.Width / LOG_IN_FORM_ALLGIMENT_FROM_TEXT_TO_LABEL;
  140. int xOfPicBoxes = 0;
  141. List<Control> cont = new List<Control>();
  142.  
  143. // sets the background of the page
  144. this.BackgroundImage = Client.Properties.Resources.loginBG;
  145.  
  146. // sets the color of the letters
  147. this.ForeColor = Color.White;
  148.  
  149. // sets the subject label
  150. Label LBLSubject = new MyLabel("Please Log In", "LBLSubject"); // i create a label to know the width of its text
  151. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  152. cont.Add(LBLSubject);
  153.  
  154. nextYPos = LBLSubject.Location.Y + alligmentFromSubjectY + LBLSubject.Height; // begining of details section
  155. reminingY -= nextYPos; // sets the space of details section
  156.  
  157. // sets the text box of username
  158. TextBox TXTuserName = new MyTxtBox("TXTusername"); // i create a label to know the height of its font
  159. alligmentFromTextBoxes = ((reminingY / LOG_IN_FORM_TEXT_SECTOR_HEIGHT) - (TXTuserName.Height * 2)) / 3; // generates the needed alligment
  160. nextYPos += alligmentFromTextBoxes;
  161. reminingY -= reminingY / LOG_IN_FORM_TEXT_SECTOR_HEIGHT; // sets the space of buttons section
  162.  
  163. TXTuserName.Location = new Point((this.Width / 2) - (TXTuserName.Width / 2), nextYPos);
  164. cont.Add(TXTuserName);
  165.  
  166. // sets the label of username
  167. Label LBLusername = new MyLabel("UserName: ", "LBLusername"); // sets the label of the userName
  168. LBLusername.Location = new Point(TXTuserName.Location.X - LBLusername.Width - alligmentFromTextBoxesToLabels, nextYPos);
  169. cont.Add(LBLusername);
  170.  
  171. nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the position of the next textBox
  172. xOfTextBoxes = TXTuserName.Location.X;
  173.  
  174. // sets the text box of password
  175. TextBox TXTpsw = new MyTxtBox(new Point(xOfTextBoxes, nextYPos), "TXTpsw");
  176. cont.Add(TXTpsw);
  177.  
  178. // sets the label of password
  179. Label LBLpsw = new MyLabel("password: ", "LBLpsw"); // sets the password label
  180. LBLpsw.Location = new Point(xOfTextBoxes - LBLpsw.Width - alligmentFromTextBoxesToLabels, nextYPos);
  181. cont.Add(LBLpsw);
  182.  
  183. nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the begining of the buttons sector
  184.  
  185. // sets the buttons section
  186. sizeOfPictureBox = reminingY / LOG_IN_FORM_PICBOX_HEIGHT;
  187. reminingY -= (sizeOfPictureBox * 2);
  188. alligmentFromPictureBox = reminingY / 2;
  189.  
  190. // sets the sign in button
  191. PictureBox PBSignIn = new MyPictureBox("PBSignIn", "SignIn", new Size(TXTuserName.Width, sizeOfPictureBox));
  192. xOfPicBoxes = (this.Width / 2) - (PBSignIn.Width / 2); // sets the x of picture boxes
  193. PBSignIn.Location = new Point(xOfPicBoxes, nextYPos);
  194. PBSignIn.Click += (EventHandler)PBSign_In_Click;
  195. cont.Add(PBSignIn);
  196.  
  197. nextYPos += alligmentFromPictureBox;
  198.  
  199. // sets the signup button
  200. PictureBox PBSignUp = new MyPictureBox("PBSignUp", "SignUp", new Size(PBSignIn.Width, sizeOfPictureBox));
  201. PBSignUp.Location = new Point(xOfPicBoxes, nextYPos);
  202. PBSignUp.Click += (EventHandler)(CleanAllControlers);
  203. PBSignUp.Click += (EventHandler)InitSignUp;
  204. cont.Add(PBSignUp);
  205.  
  206. this.Controls.AddRange(cont.ToArray());
  207. }
  208.  
  209. /// <summary>
  210. /// sets the form to be sign up page
  211. /// </summary>
  212. public void InitSignUp(object sender, EventArgs args)
  213. {
  214. int reminingY = this.Height;
  215. int nextYPos = 0;
  216. int xOfTextBoxes = 0;
  217. int sizeOfPictureBox = 0;
  218. int alligmentFromPictureBox = 0;
  219. int alligmentFromTextBoxes = 0;
  220. int alligmentFromTextBoxesToLabels = this.Width / LOG_IN_FORM_ALLGIMENT_FROM_TEXT_TO_LABEL;
  221. int xOfPicBoxes = 0;
  222. List<Control> cont = new List<Control>();
  223.  
  224. // sets the background
  225. this.BackgroundImage = Properties.Resources.signUpBG;
  226.  
  227. // sets the color of the letters
  228. this.ForeColor = Color.Black;
  229.  
  230. // sets the subject label
  231. Label LBLSubject = new MyLabel("Please Sign Up", "LBLSubject"); // i create a label to know the width of its text
  232. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  233. cont.Add(LBLSubject);
  234.  
  235. nextYPos = LBLSubject.Location.Y + alligmentFromSubjectY + LBLSubject.Height; // begining of details section
  236. reminingY -= nextYPos; // sets the space of details section
  237.  
  238. // sets the text box of username
  239. TextBox TXTuserName = new MyTxtBox("TXTusername"); // i create a label to know the height of its font
  240. alligmentFromTextBoxes = ((reminingY / LOG_IN_FORM_TEXT_SECTOR_HEIGHT) - (TXTuserName.Height * 3)) / 4; // generates the needed alligment
  241. nextYPos += alligmentFromTextBoxes;
  242. reminingY -= reminingY / LOG_IN_FORM_TEXT_SECTOR_HEIGHT; // sets the space of buttons section
  243.  
  244. TXTuserName.Location = new Point((this.Width / 2) - (TXTuserName.Width / 2), nextYPos);
  245. cont.Add(TXTuserName);
  246.  
  247. // sets the label of username
  248. Label LBLusername = new MyLabel("UserName: ", "LBLusername"); // sets the label of the userName
  249. LBLusername.Location = new Point(TXTuserName.Location.X - LBLusername.Width - alligmentFromTextBoxesToLabels, nextYPos);
  250. cont.Add(LBLusername);
  251.  
  252. nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the position of the next textBox
  253. xOfTextBoxes = TXTuserName.Location.X;
  254.  
  255. // sets the text box of password
  256. TextBox TXTpsw = new MyTxtBox(new Point(xOfTextBoxes, nextYPos), "TXTpsw");
  257. cont.Add(TXTpsw);
  258.  
  259. // sets the label of password
  260. Label LBLpsw = new MyLabel("password: ", "LBLpsw"); // sets the password label
  261. LBLpsw.Location = new Point(xOfTextBoxes - LBLpsw.Width - alligmentFromTextBoxesToLabels, nextYPos);
  262. cont.Add(LBLpsw);
  263.  
  264. nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the position of the next textBox
  265.  
  266. // sets the text box of email
  267. TextBox TXTemail = new MyTxtBox(new Point(xOfTextBoxes, nextYPos), "TXTemail");
  268. cont.Add(TXTemail);
  269.  
  270. // sets the label of email
  271. Label LBLemail = new MyLabel("email: ", "LBLemail");
  272. LBLemail.Location = new Point(xOfTextBoxes - LBLemail.Width - alligmentFromTextBoxesToLabels, nextYPos);
  273. cont.Add(LBLemail);
  274.  
  275. nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the begining of the buttons sector
  276.  
  277. // sets the buttons section
  278. sizeOfPictureBox = reminingY / LOG_IN_FORM_PICBOX_HEIGHT;
  279. reminingY -= (sizeOfPictureBox * 2);
  280. alligmentFromPictureBox = reminingY / 2;
  281.  
  282. // sets the sign up button
  283. PictureBox PBSignUp = new MyPictureBox("PBSignUp", "SignUp", new Size(TXTemail.Width, sizeOfPictureBox));
  284. xOfPicBoxes = (this.Width / 2) - (PBSignUp.Width / 2); // sets the x of picture boxes
  285. PBSignUp.Location = new Point(xOfPicBoxes, nextYPos);
  286. PBSignUp.Click += (EventHandler)PBSign_Up_Click;
  287. //PBSignUp.Click += (EventHandler)CleanAllControlers;
  288. cont.Add(PBSignUp);
  289.  
  290. // sets the back button
  291. int PCHeight = this.Height / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  292. int PCWidth = this.Width / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  293. PictureBox PBBack = new MyPictureBox("PBBack", "back", new Size(PCWidth, PCHeight));
  294. PBBack.Location = new Point(this.Width - PCWidth, this.Height / ALLIGMENT_FROM_TOP);
  295. PBBack.Click += (EventHandler)CleanAllControlers;
  296. PBBack.Click += (EventHandler)InitLoginScreen;
  297. cont.Add(PBBack);
  298.  
  299. this.Controls.AddRange(cont.ToArray());
  300. }
  301.  
  302. /// <summary>
  303. /// builds the main screen
  304. /// </summary>
  305. /// <param name="sender"></param>
  306. /// <param name="args"></param>
  307. public void InitMainScreen(object sender, EventArgs args)
  308. {
  309. int nextYPos = 0;
  310. int reminingY = PBClose.Location.Y;
  311. int heightOfPB = this.Height / MAIN_FORM_PICBOX_HEIGHT;
  312. int widthOfPB = this.Width / MAIN_FORM_PICBOX_WIDTH;
  313. int alligmentFromButtonsY = 0;
  314. int alligmentFromButtonsX = 0;
  315.  
  316. // sets the background of the screen
  317. this.BackgroundImage = Properties.Resources.mainScreenBG;
  318.  
  319. // sets the font color to black
  320. this.ForeColor = Color.Black;
  321.  
  322. // sets the subject label
  323. Label LBLSubject = new MyLabel("Hello " + uid, "LBLSubject"); // i create a label to know the width of its text
  324. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  325. this.Controls.Add(LBLSubject);
  326.  
  327. // configures all the propeties needed for the button
  328. nextYPos = LBLSubject.Location.Y + alligmentFromSubjectY;
  329. reminingY -= (nextYPos - heightOfPB * 2);
  330. alligmentFromButtonsY = reminingY / 3;
  331. nextYPos += (alligmentFromButtonsY / 2);
  332. alligmentFromButtonsX = (this.Width - widthOfPB * 3) / 3;
  333.  
  334. // sets the Join room button
  335. Button BTNJoin = new MyButton("BTNJoin", new Size(widthOfPB, heightOfPB), "Join Room");
  336. BTNJoin.Location = new Point(alligmentFromButtonsX, nextYPos);
  337. BTNJoin.Click += (EventHandler)CleanAllControlers;
  338. BTNJoin.Click += (EventHandler)InitJoinRoom;
  339. BTNJoin.Click += (EventHandler)BTNJoin_Click;
  340. this.Controls.Add(BTNJoin);
  341.  
  342. // sets the Create room button
  343. Button BTNCreate = new MyButton("BTNCreate", new Size(widthOfPB, heightOfPB), "Create Room");
  344. BTNCreate.Location = new Point(this.Width - widthOfPB - alligmentFromButtonsX, nextYPos);
  345. BTNCreate.Click += (EventHandler)CleanAllControlers;
  346. BTNCreate.Click += (EventHandler)InitCreateRoom;
  347. this.Controls.Add(BTNCreate);
  348.  
  349. nextYPos += alligmentFromButtonsY;
  350.  
  351. // sets the best scores button
  352. Button BTNBest = new MyButton("BTNBest", new Size(widthOfPB, heightOfPB), "Best Scores");
  353. BTNBest.Location = new Point(alligmentFromButtonsX, nextYPos);
  354. BTNBest.Click += (EventHandler)CleanAllControlers;
  355. BTNBest.Click += (EventHandler)InitBestScores;
  356. BTNBest.Click += (EventHandler)BTNBest_Click;
  357. this.Controls.Add(BTNBest);
  358.  
  359. // sets the personal status
  360. Button BTNPersonal = new MyButton("BTNPersonal", new Size(widthOfPB, heightOfPB), "My Status");
  361. BTNPersonal.Location = new Point(this.Width - widthOfPB - alligmentFromButtonsX, nextYPos);
  362. BTNPersonal.Click += (EventHandler)CleanAllControlers;
  363. BTNPersonal.Click += (EventHandler)InitPersonalStatus;
  364. BTNPersonal.Click += (EventHandler)BTNPersonal_Click;
  365.  
  366. this.Controls.Add(BTNPersonal);
  367.  
  368. // sets the logout button
  369. PictureBox PBLogOut = new MyPictureBox("PBLogOut", "signOut", new Size(this.Height / MAIN_FORM_LOG_OUT_WIDTH, this.Width / MAIN_FORM_LOG_OUT_HEIGHT));
  370. PBLogOut.Location = new Point(this.Width / ALLIGMENT_FROM_LEFT, this.Height / ALLIGMENT_FROM_TOP);
  371. PBLogOut.Click += (EventHandler)CleanAllControlers;
  372. PBLogOut.Click += (EventHandler)InitLoginScreen;
  373. PBLogOut.Click += (EventHandler)PBLogOut_Click;
  374. this.Controls.Add(PBLogOut);
  375. }
  376.  
  377. /// <summary>
  378. /// inits the join room screen
  379. /// </summary>
  380. /// <param name="sender"></param>
  381. /// <param name="args"></param>
  382. private void InitJoinRoom(object sender, EventArgs args)
  383. {
  384. int nextYPos = 0;
  385. int alligmentFromClose = this.Height - this.PBClose.Location.Y - this.PBClose.Height;
  386. int alligmentFromButtonsY = 0;
  387. int alligmentFromButtonsX = 0;
  388. int buttonSectorY = 0;
  389. int alligmentFromListsX = 0;
  390. int refreshY = 0;
  391. int joinY = 0;
  392. int reminingY = this.PBClose.Location.Y;
  393.  
  394. // sets the form to join room bg
  395. this.BackgroundImage = Properties.Resources.joinRoomBG;
  396.  
  397. // changes the fore color to black
  398. this.ForeColor = Color.Black;
  399.  
  400. // sets the subject label
  401. Label LBLSubject = new MyLabel("Choose Room", "LBLSubject"); // i create a label to know the width of its text
  402. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  403. this.Controls.Add(LBLSubject);
  404.  
  405. nextYPos = LBLSubject.Location.Y + LBLSubject.Height + alligmentFromSubjectY;
  406. reminingY -= nextYPos;
  407.  
  408. // configures the button sector properties
  409. buttonSectorY = reminingY / JOIN_ROOM_FORM_BUTTON_SECTOR_Y;
  410. refreshY = buttonSectorY / JOIN_ROOM_FORM_REFRESH_HEIGHT;
  411. joinY = buttonSectorY / JOIN_ROOM_FORM_JOIN_HEIGHT;
  412. alligmentFromButtonsY = (buttonSectorY - refreshY - joinY) / 2;
  413. alligmentFromButtonsX = (this.Width - this.Width / JOIN_ROOM_FORM_REFRESH_WIDTH - this.Width / JOIN_ROOM_FORM_JOIN_WIDTH) / 3;
  414.  
  415. // configures and adds the refresh button
  416. PictureBox PBRefresh = new MyPictureBox("PBRefresh", "refresh", new Size(this.Width / JOIN_ROOM_FORM_REFRESH_WIDTH, refreshY));
  417. PBRefresh.Location = new Point(alligmentFromButtonsX, PBClose.Location.Y - alligmentFromButtonsY - refreshY);
  418. PBRefresh.Click += (EventHandler)PBRefresh_Click;
  419. this.Controls.Add(PBRefresh);
  420.  
  421. // configures and adds the join button
  422. PictureBox PBJoin = new MyPictureBox("PBJoin", "joinBTN", new Size(this.Width / JOIN_ROOM_FORM_JOIN_WIDTH, this.Height / JOIN_ROOM_FORM_JOIN_HEIGHT));
  423. PBJoin.Location = new Point(this.Width - PBJoin.Width - alligmentFromButtonsX, PBRefresh.Location.Y);
  424. PBJoin.Click += (EventHandler)PBJoin_Click;
  425. //PBJoin.Click += (EventHandler)CleanAllControlers;
  426. //PBJoin.Click += (EventHandler)InitInsideRoom;
  427. this.Controls.Add(PBJoin);
  428.  
  429. reminingY = PBRefresh.Location.Y - LBLSubject.Location.Y - LBLSubject.Height - alligmentFromSubjectY;
  430. ListBox LBRooms = new MyListBox("LBRooms", new Size(this.Width / JOIN_ROOM_FORM_ROOMS_LB_WIDTH, reminingY / 4));
  431. ListBox LBUsers = new MyListBox("LBUsers", new Size(this.Width / JOIN_ROOM_FORM_USERS_LB_WIDTH, reminingY / 4));
  432.  
  433. alligmentFromListsX = (this.Width - LBRooms.Width - LBUsers.Width) / 3;
  434.  
  435. Label LBLRooms = new MyLabel("Rooms:", "LBLRooms");
  436. Label LBLUsers = new MyLabel("Users:", "LBLUsers");
  437.  
  438. // adds the rooms list box
  439. LBRooms.Location = new Point(this.Width / 2 - LBRooms.Width / 2, nextYPos + LBLRooms.Height);
  440. LBRooms.SelectedIndexChanged += (EventHandler)LBRooms_SelectedIndexChanged;
  441. this.Controls.Add(LBRooms);
  442.  
  443. // sets the users list box
  444. LBUsers.Location = new Point(alligmentFromListsX, nextYPos + LBLUsers.Height);
  445. LBUsers.Visible = false;
  446. this.Controls.Add((LBUsers));
  447.  
  448. // configures and adds the label of rooms
  449. LBLRooms.Location = new Point(LBRooms.Location.X + LBRooms.Width / 2 - LBLRooms.Width / 2, nextYPos);
  450. this.Controls.Add(LBLRooms);
  451.  
  452. // configures and adds the label of users
  453. LBLUsers.Location = new Point(LBUsers.Location.X + LBUsers.Width / 2 - LBLRooms.Width / 2, nextYPos);
  454. LBLUsers.Visible = false;
  455. this.Controls.Add(LBLUsers);
  456.  
  457. // sets the back button
  458. int PCHeight = this.Height / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  459. int PCWidth = this.Width / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  460. PictureBox PBBack = new MyPictureBox("PBBack", "back", new Size(PCWidth, PCHeight));
  461. PBBack.Location = new Point(this.Width - PCWidth, this.Height / ALLIGMENT_FROM_TOP);
  462. PBBack.Click += (EventHandler)CleanAllControlers;
  463. PBBack.Click += (EventHandler)InitMainScreen;
  464. this.Controls.Add(PBBack);
  465. }
  466.  
  467. /// <summary>
  468. /// changes the room list in the join room form
  469. /// should be called when the join room button or refresh button were clicked
  470. /// </summary>
  471. /// <param name="roomsNames">the names of the rooms that will be shown on the list box</param>
  472. private void changeRoomsList(List<string> roomsNames)
  473. {
  474. if (roomsNames.Count < 1) // so that the list will still be shown even if there are no available rooms
  475. {
  476. roomsNames.Add("");
  477. roomsID.Add("-1"); // so the user cant join this fake room
  478. }
  479.  
  480. ListBox LBUsers = this.Controls.Find("LBUsers", false).FirstOrDefault() as ListBox;
  481. Label LBLUsers = this.Controls.Find("LBLUsers", false).FirstOrDefault() as Label;
  482.  
  483. // makes the users lists to disapear
  484. LBUsers.Visible = false;
  485. LBLUsers.Visible = false;
  486.  
  487. ListBox LBRooms = this.Controls.Find("LBRooms", false).FirstOrDefault() as ListBox;
  488. if (LBRooms != null)
  489. {
  490. LBRooms.Items.Clear();
  491. foreach (string name in roomsNames)
  492. {
  493. LBRooms.Items.Add(name);
  494. }
  495.  
  496. Label LBLRooms = this.Controls.Find("LBLRooms", false).FirstOrDefault() as Label;
  497. PictureBox PBRefresh = this.Controls.Find("PBRefresh", false).FirstOrDefault() as PictureBox;
  498.  
  499. // sets the height of the list box
  500. if (LBLRooms != null && PBRefresh != null)
  501. {
  502. int maxHeight = PBRefresh.Location.Y - LBLRooms.Location.Y - LBLRooms.Height - alligmentFromSubjectY;
  503. int height = LBRooms.ItemHeight;
  504. int maxObj = maxHeight / height;
  505.  
  506. LBRooms.Height = roomsNames.Count > maxObj ? height * maxObj : roomsNames.Count * height;
  507. }
  508.  
  509. // moves the list box to its proper place
  510. LBRooms.Location = new Point(this.Width / 2 - LBRooms.Width / 2, LBRooms.Location.Y);
  511. LBLRooms.Location = new Point(LBRooms.Location.X + LBRooms.Width / 2 - LBLRooms.Width / 2, LBLRooms.Location.Y);
  512.  
  513. this.Update();
  514. }
  515. }
  516.  
  517. /// <summary>
  518. /// changes the users in room list box
  519. /// should be called when an item on the rooms list is clicked
  520. /// </summary>
  521. /// <param name="users">the users that will be presented on the users list</param>
  522. private void changeUsersInRoom(List<string> users)
  523. {
  524. if (users.Count < 1)
  525. {
  526. users.Add("");
  527. }
  528.  
  529. ListBox LBUsers = this.Controls.Find("LBUsers", false).FirstOrDefault() as ListBox;
  530. Label LBLUsers = this.Controls.Find("LBLUsers", false).FirstOrDefault() as Label;
  531. Label LBLRooms = this.Controls.Find("LBLRooms", false).FirstOrDefault() as Label;
  532. PictureBox PBRefresh = this.Controls.Find("PBRefresh", false).FirstOrDefault() as PictureBox;
  533. ListBox LBRooms = this.Controls.Find("LBRooms", false).FirstOrDefault() as ListBox;
  534.  
  535. // sets the height of the list box
  536. if (LBLUsers != null && PBRefresh != null && LBUsers != null)
  537. {
  538. int maxHeight = PBRefresh.Location.Y - LBLUsers.Location.Y - LBLUsers.Height - alligmentFromSubjectY;
  539. int height = LBUsers.ItemHeight;
  540. int maxObj = maxHeight / height;
  541.  
  542. // changes the items in the list box
  543. LBUsers.Items.Clear();
  544. foreach (string user in users)
  545. {
  546. LBUsers.Items.Add(user);
  547. }
  548.  
  549. LBUsers.Height = users.Count > maxObj ? height * maxObj : users.Count * height;
  550.  
  551. // sets the rooms stuff to their proper place
  552. LBRooms.Location = new Point(this.Width - LBRooms.Width - LBUsers.Location.X, LBRooms.Location.Y);
  553. LBLRooms.Location = new Point(LBRooms.Location.X + LBRooms.Width / 2 - LBLRooms.Width / 2, LBLRooms.Location.Y);
  554.  
  555. LBUsers.Visible = true;
  556. LBLUsers.Visible = true;
  557. }
  558. }
  559.  
  560. private void InitCreateRoom(object sender, EventArgs args)
  561. {
  562. int alligmentFromCloseButton = this.Height - this.PBClose.Location.Y - this.PBClose.Height;
  563. int nextYPos = 0;
  564. int buttonHeight = this.Height / CREATE_ROOM_FORM_BUTTON_HEIGHT;
  565. int buttonWidth = this.Width / CREATE_ROOM_FORM_BUTTON_WIDTH;
  566. int reminingY = 0;
  567. int alligmentFromTextBoxes = 0;
  568.  
  569. this.BackgroundImage = Properties.Resources.createRoom;
  570. this.ForeColor = Color.Black;
  571.  
  572. // sets the subject label
  573. Label LBLSubject = new MyLabel("Create New Room:", "LBLSubject"); // i create a label to know the width of its text
  574. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  575. this.Controls.Add(LBLSubject);
  576.  
  577. // configures and adds the create room button
  578. Button BTNCreateRoom = new MyButton("BTNCreateRoom", new Size(buttonWidth, buttonHeight), "Create");
  579. BTNCreateRoom.Location = new Point(this.Width / 2 - buttonWidth / 2, this.PBClose.Location.Y - alligmentFromCloseButton - buttonHeight);
  580. BTNCreateRoom.Click += (EventHandler)BTNCreateRoom_Click;
  581. this.Controls.Add(BTNCreateRoom);
  582.  
  583. // sets all the required properties for textbox sector
  584. nextYPos = LBLSubject.Location.Y + alligmentFromSubjectY + LBLSubject.Height;
  585. reminingY = BTNCreateRoom.Location.Y - nextYPos;
  586. TextBox TXTRoomName = new MyTxtBox("TXTRoomName"); // i create a text box so i can know its height and width
  587. alligmentFromTextBoxes = (reminingY - TXTRoomName.Height * 4) / 4 + TXTRoomName.Height;
  588.  
  589. // configures and adds the room name textbox
  590. TXTRoomName.Location = new Point(this.Width / 2 - TXTRoomName.Width / 2, nextYPos);
  591. this.Controls.Add(TXTRoomName);
  592.  
  593. // configures and adds the room name label
  594. Label LBLRoomName = new MyLabel("Room Name: ", "LBLRoomName");
  595. LBLRoomName.Location = new Point(TXTRoomName.Location.X - LBLRoomName.Width, nextYPos);
  596. this.Controls.Add(LBLRoomName);
  597.  
  598. nextYPos += alligmentFromTextBoxes;
  599.  
  600. // configures and adds the number of players text box
  601. TextBox TXTNumOfPlayers = new MyTxtBox(new Point(TXTRoomName.Location.X, nextYPos), "TXTNumOfPlayers");
  602. this.Controls.Add(TXTNumOfPlayers);
  603.  
  604. // configures and adds the number of players label
  605. Label LBLNumOfPlayers = new MyLabel("Number of players: ", "LBLNumOfPlayers");
  606. LBLNumOfPlayers.Location = new Point(TXTNumOfPlayers.Location.X - LBLNumOfPlayers.Width, nextYPos);
  607. this.Controls.Add(LBLNumOfPlayers);
  608.  
  609. nextYPos += alligmentFromTextBoxes;
  610.  
  611. // configures and adds the number of questions text box
  612. TextBox TXTNumOfQuestions = new MyTxtBox(new Point(TXTRoomName.Location.X, nextYPos), "TXTNumOfQuestions");
  613. this.Controls.Add(TXTNumOfQuestions);
  614.  
  615. // configures and adds the number of question label
  616. Label LBLNumOfQuestions = new MyLabel("Number of questions: ", "LBLNumOfQuestions");
  617. LBLNumOfQuestions.Location = new Point(TXTNumOfQuestions.Location.X - LBLNumOfQuestions.Width, nextYPos);
  618. this.Controls.Add(LBLNumOfQuestions);
  619.  
  620. nextYPos += alligmentFromTextBoxes;
  621.  
  622. // configures and adds the number of questions text box
  623. TextBox TXTTimePerAns = new MyTxtBox(new Point(TXTRoomName.Location.X, nextYPos), "TXTTimePerAns");
  624. this.Controls.Add(TXTTimePerAns);
  625.  
  626. // configures and adds the time per answer label
  627. Label LBLTimePerAns = new MyLabel("Time Per answer: ", "LBLTimePerAns");
  628. LBLTimePerAns.Location = new Point(TXTTimePerAns.Location.X - LBLTimePerAns.Width, nextYPos);
  629. this.Controls.Add(LBLTimePerAns);
  630.  
  631. // sets the back button
  632. int PCHeight = this.Height / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  633. int PCWidth = this.Width / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  634. PictureBox PBBack = new MyPictureBox("PBBack", "back", new Size(PCWidth, PCHeight));
  635. PBBack.Location = new Point(this.Width - PCWidth, this.Height / ALLIGMENT_FROM_TOP);
  636. PBBack.Click += (EventHandler)CleanAllControlers;
  637. PBBack.Click += (EventHandler)InitMainScreen;
  638. this.Controls.Add(PBBack);
  639. }
  640.  
  641. /// <summary>
  642. /// changes the form to inside room performance
  643. /// </summary>
  644. /// <param name="sender"></param>
  645. /// <param name="args"></param>
  646. private void InitInsideRoom(object sender, EventArgs args)
  647. {
  648. int reminingY = this.Height;
  649. int nextYPos = 0;
  650. int buttonSectorY = 0;
  651. int buttonsHeight = this.Height / IN_ROOM_FORM_BUTTON_HEIGHT;
  652. int buttonWidth = this.Width / IN_ROOM_FORM_BUTTON_WIDTH;
  653. int alligmentFromButtonsY = 0;
  654. int alligmentFromLabelsY = 0;
  655. List<Label> labels = new List<Label>();
  656.  
  657. // sets the background to be inside room background
  658. this.BackgroundImage = Properties.Resources.inRoom;
  659.  
  660. // sets the font color to black
  661. this.ForeColor = Color.Black;
  662.  
  663. this.PBClose.Visible = false;
  664.  
  665. // sets the subject label
  666. Label LBLSubject = new MyLabel("You are in " + this.roomName + " room", "LBLSubject"); // i create a label to know the width of its text
  667. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  668. this.Controls.Add(LBLSubject);
  669.  
  670. nextYPos = LBLSubject.Location.Y + LBLSubject.Height + alligmentFromSubjectY;
  671. reminingY -= nextYPos;
  672. buttonSectorY = reminingY / IN_ROOM_FORM_BUTTON_SECTOR_Y;
  673. reminingY -= buttonSectorY;
  674. alligmentFromButtonsY = (buttonSectorY - buttonsHeight) / 2;
  675.  
  676. if (sender is PictureBox) // in case its a user presentation
  677. {
  678. Button BTNLeave = new MyButton("BTNLeave", new Size(buttonWidth, buttonsHeight), "Leave Room");
  679. BTNLeave.Location = new Point(this.Width / 2 - buttonWidth / 2, this.Height - buttonsHeight - alligmentFromButtonsY);
  680. BTNLeave.Click += (EventHandler)BTNLeave_Click;
  681. this.Controls.Add(BTNLeave);
  682. }
  683. else if (sender is Button) // in case its admin presentation
  684. {
  685. int alligmentFromButtonsX = (this.Width - buttonWidth * 2) / 3;
  686.  
  687. // configures and adds the close room button
  688. Button BTNCloseRoom = new MyButton("BTNCloseRoom", new Size(buttonWidth, buttonsHeight), "Close Room");
  689. BTNCloseRoom.Location = new Point(alligmentFromButtonsX, this.Height - BTNCloseRoom.Height - alligmentFromButtonsY);
  690. BTNCloseRoom.Click += (EventHandler)BTNCloseRoom_Click;
  691. this.Controls.Add(BTNCloseRoom);
  692.  
  693. // configures and adds the start game button
  694. Button BTNStartGame = new MyButton("BTNStartGame", new Size(buttonWidth, buttonsHeight), "Start Game");
  695. BTNStartGame.Location = new Point(this.Width - buttonWidth - alligmentFromButtonsX, BTNCloseRoom.Location.Y);
  696. BTNStartGame.Click += (EventHandler)BTNStartGame_Click;
  697. this.Controls.Add(BTNStartGame);
  698.  
  699. labels.Add(new MyLabel("Max number of users: ", "LBLMaxUsers"));
  700. }
  701.  
  702. Label LBLparticipates = new MyLabel("current participates are: ", "LBLparticipates");
  703.  
  704. // configures and adds the participates list box
  705. ListBox LBpart = new MyListBox("LBpart", new Size(this.Width / IN_ROOM_FORM_LB_WIDTH, reminingY / 4));
  706. LBpart.Location = new Point(this.Width / 2 - LBpart.Width / 2, nextYPos + LBLparticipates.Height);
  707. this.Controls.Add(LBpart);
  708.  
  709. // configures and adds the label of participates
  710. LBLparticipates.Location = new Point(LBpart.Location.X + LBpart.Width / 2 - LBLparticipates.Width / 2, nextYPos);
  711. this.Controls.Add(LBLparticipates);
  712.  
  713. // sets the room properties labels
  714. labels.Add(new MyLabel("Number of questions: ", "LBLNumOfQuestions"));
  715. labels.Add(new MyLabel("Time per question: ", "LBLTimePerQuestion"));
  716.  
  717. // calculates the needed alligment
  718. alligmentFromLabelsY = (reminingY - labels[0].Height * labels.Count) / (labels.Count + 1);
  719. nextYPos += alligmentFromLabelsY;
  720.  
  721. foreach (Label l in labels)
  722. {
  723. l.Location = new Point(0, nextYPos);
  724. nextYPos += alligmentFromLabelsY;
  725. this.Controls.Add(l);
  726. }
  727. }
  728.  
  729. /// <summary>
  730. /// changes the list box of participates
  731. /// </summary>
  732. /// <param name="users">the users to be added to the list box</param>
  733. private void ChangeLBpart(List<string> users)
  734. {
  735. Label LBLparticipates = this.Controls.Find("LBLparticipates", false).FirstOrDefault() as Label;
  736. ListBox LBpart = this.Controls.Find("LBpart", false).FirstOrDefault() as ListBox;
  737.  
  738. if (users.Count < 1)
  739. {
  740. users.Add("");
  741. }
  742.  
  743. if (LBLparticipates != null && LBpart != null)
  744. {
  745. int maxHeight = this.Height - this.Height / IN_ROOM_FORM_BUTTON_SECTOR_Y - LBLparticipates.Location.Y - LBLparticipates.Height;
  746. int height = LBpart.ItemHeight;
  747. int maxObj = maxHeight / height;
  748.  
  749. // changes the height of the list box according to the items in it
  750. LBpart.Height = users.Count > maxObj ? height * maxObj : height * users.Count;
  751.  
  752. LBpart.Items.Clear();
  753. foreach (string user in users)
  754. {
  755. LBpart.Items.Add(user);
  756. }
  757. }
  758. }
  759.  
  760. /// <summary>
  761. /// changes the labels of in room properties
  762. /// in case of user presentation the properties param should be made out of the string the server sent
  763. /// in case of admin presentation the properties param should be the global parameter roomProperties
  764. /// </summary>
  765. /// <param name="properties">the properties that would be in the labels, the order should be maxusers(if exists), numberOfQuestions, time per questions</param>
  766. private void ChangeLabels(List<string> properties)
  767. {
  768. Label[] labels = new Label[properties.Count];
  769.  
  770. if (properties.Count > 2)
  771. {
  772. labels[0] = this.Controls.Find("LBLMaxUsers", false).FirstOrDefault() as Label;
  773. }
  774.  
  775. labels[labels.Length - 2] = this.Controls.Find("LBLNumOfQuestions", false).FirstOrDefault() as Label;
  776. labels[labels.Length - 1] = this.Controls.Find("LBLTimePerQuestion", false).FirstOrDefault() as Label;
  777. MessageBox.Show("[1] = " + labels[labels.Length - 1].Text + "\n[2] = " + labels[labels.Length - 2].Text);
  778. this.questionTime = int.Parse(properties[properties.Count - 1]).ToString();
  779. this.maxQuestion = int.Parse(properties[properties.Count - 2]).ToString();
  780.  
  781. for (int i = 0; i < labels.Length; i++)
  782. {
  783. if (labels[i] != null)
  784. {
  785. labels[i].Text += (int.Parse(properties[i]).ToString());
  786. labels[i].Width = TextRenderer.MeasureText(labels[i].Text, labels[i].Font).Width;
  787. }
  788. }
  789. }
  790.  
  791. private void InitGameScreen(object sender, EventArgs args)
  792. {
  793. int nextYPos = 0;
  794. int reminingY = 0;
  795. int alligmentFromButtonsY = 0;
  796. int alligmentFromButtonsX = 0;
  797. int buttonHeight = this.Height / IN_GAME_FORM_ANSWER_BUTTON_HEIGHT;
  798. int buttonWidth = this.Width / IN_GAME_FORM_ANSWER_BUTTON_WIDTH;
  799.  
  800. this.PBClose.Visible = false;
  801.  
  802. this.BackgroundImage = Properties.Resources.gameBG;
  803. this.ForeColor = Color.Black;
  804.  
  805. // sets the subject label
  806. Label LBLSubject = new MyLabel("You are in game", "LBLSubject"); // i create a label to know the width of its text
  807. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  808. this.Controls.Add(LBLSubject);
  809.  
  810. // configures and sets a new button
  811. 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");
  812. BTNLeaveGame.Location = new Point(this.Width / 2 - BTNLeaveGame.Width / 2, this.Height - BTNLeaveGame.Height - this.Height / ALLIGMENT_FROM_TOP);
  813. BTNLeaveGame.Click += BTNLeaveGame_Click;
  814. this.Controls.Add(BTNLeaveGame);
  815.  
  816. nextYPos = LBLSubject.Location.Y + LBLSubject.Height + alligmentFromSubjectY;
  817. reminingY = BTNLeaveGame.Location.Y;
  818.  
  819. // configures and sets the question label
  820. Label LBLQuestion = new MyLabel("Question", "LBLQuestion");
  821. LBLQuestion.Location = new Point(0, nextYPos);
  822. this.Controls.Add(LBLQuestion);
  823.  
  824. nextYPos += LBLQuestion.Height;
  825.  
  826. reminingY -= nextYPos;
  827. alligmentFromButtonsY = (reminingY - buttonHeight * 2) / 3;
  828. alligmentFromButtonsX = (this.Width - buttonWidth * 2) / 3;
  829.  
  830. nextYPos += alligmentFromButtonsY;
  831.  
  832. // configures and sets the time left label
  833. Label LBLTime = new MyLabel(this.questionTime, "LBLTime");
  834. LBLTime.Location = new Point(this.Width / 2 - LBLTime.Width / 2, nextYPos - (nextYPos - LBLQuestion.Location.Y) / 2 - LBLTime.Height / 2);
  835. this.Controls.Add(LBLTime);
  836.  
  837. // configures and adds the first answer button
  838. Button BTN1st = new MyButton("BTN1st", new Size(buttonWidth, buttonHeight), "");
  839. BTN1st.Location = new Point(alligmentFromButtonsX, nextYPos);
  840. BTN1st.Click += BTN1st_Click;
  841. this.Controls.Add(BTN1st);
  842.  
  843. // configures and adds the second answer button
  844. Button BTN2nd = new MyButton("BTN2nd", new Size(buttonWidth, buttonHeight), "");
  845. BTN2nd.Location = new Point(this.Width - buttonWidth - alligmentFromButtonsX, nextYPos);
  846. BTN2nd.Click += BTN2nd_Click;
  847. this.Controls.Add(BTN2nd);
  848.  
  849. nextYPos += BTN2nd.Height + alligmentFromButtonsY;
  850.  
  851. // configures and adds the third answer button
  852. Button BTN3rd = new MyButton("BTN3rd", new Size(buttonWidth, buttonHeight), "");
  853. BTN3rd.Location = new Point(alligmentFromButtonsX, nextYPos);
  854. BTN3rd.Click += BTN3rd_Click;
  855. this.Controls.Add(BTN3rd);
  856.  
  857. // configures and adds the fourth answer button
  858. Button BTN4th = new MyButton("BTN4th", new Size(buttonWidth, buttonHeight), "");
  859. BTN4th.Location = new Point(BTN2nd.Location.X, nextYPos);
  860. BTN4th.Click += BTN4th_Click;
  861. this.Controls.Add(BTN4th);
  862.  
  863. // configures and adds the current answer count
  864. Label LBLCount = new MyLabel("5/6", "LBLCount");
  865. LBLCount.Location = new Point(this.Width / 2 - LBLCount.Width / 2, nextYPos + alligmentFromButtonsY / 2 - LBLCount.Height / 2 + BTN4th.Height);
  866. this.Controls.Add(LBLCount);
  867.  
  868. gameTimer.Interval = 1000; // 1 sec
  869. gameTimer.Tick += (EventHandler)delegate
  870. {
  871. Label time = this.Controls.Find("LBLTime", false).FirstOrDefault() as Label;
  872. //MessageBox.Show(time.Text);
  873. time.Text = ((int.Parse(time.Text) - 1).ToString());
  874. time.Width = TextRenderer.MeasureText(time.Text, time.Font).Width;
  875. time.Location = new Point(this.Width / 2 - time.Width / 2, time.Location.Y);
  876.  
  877. // in case half of the time passed the background of the time turns to red
  878. if(int.Parse(time.Text) <= int.Parse(this.questionTime) / 2)
  879. {
  880. time.BackColor = Color.Red;
  881. }
  882.  
  883. if (time.Text == "0")
  884. {
  885. Button[] buttons = new Button[4];
  886. buttons[0] = this.Controls.Find("BTN1st", false).FirstOrDefault() as Button;
  887. buttons[1] = this.Controls.Find("BTN2nd", false).FirstOrDefault() as Button;
  888. buttons[2] = this.Controls.Find("BTN3rd", false).FirstOrDefault() as Button;
  889. buttons[3] = this.Controls.Find("BTN4th", false).FirstOrDefault() as Button;
  890.  
  891. for (int i = 0; i < 4; i++ )
  892. {
  893. buttons[i].BackColor = Color.Red;
  894. }
  895.  
  896. stream.sendData(string.Format("2195{0:00}", this.questionTime));
  897.  
  898. gameTimer.Stop(); // the time is up
  899. }
  900. };
  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. }
  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. this.stream.sendData("299");
  1396. this.stream.CloseStream();
  1397. this.PBClose.Dispose();
  1398. }
  1399.  
  1400. /// <summary>
  1401. /// handles sign in request
  1402. /// </summary>
  1403. /// <param name="msg"> the message from the server </param>
  1404. private void HandleSignIn(string msg)
  1405. {
  1406. switch (int.Parse(msg))
  1407. {
  1408. case 0:
  1409. uid = ((TextBox)this.Controls.Find("TXTusername", false).FirstOrDefault()).Text; // to show his username in main screen
  1410. Invoke((MethodInvoker)(() => CleanAllControlers(null, EventArgs.Empty)));
  1411. Invoke((MethodInvoker)(() => InitMainScreen(null, EventArgs.Empty)));
  1412. break;
  1413. case 1:
  1414. MessageBox.Show("Wrong Details");
  1415. break;
  1416. case 2:
  1417. MessageBox.Show("User is already connected");
  1418. break;
  1419. }
  1420. }
  1421.  
  1422. /// <summary>
  1423. /// handles sign up request
  1424. /// </summary>
  1425. /// <param name="msg"> message from the server </param>
  1426. private void HandleSignUp(string msg)
  1427. {
  1428. switch (int.Parse(msg))
  1429. {
  1430. case 0:
  1431. Invoke((MethodInvoker)(() => CleanAllControlers(null, EventArgs.Empty)));
  1432. Invoke((MethodInvoker)(() => this.InitLoginScreen(null, EventArgs.Empty)));
  1433. break;
  1434. case 1:
  1435. MessageBox.Show("Pass illegal");
  1436. break;
  1437. case 2:
  1438. MessageBox.Show("Username is already exists");
  1439. break;
  1440. case 3:
  1441. MessageBox.Show("Username is illegal");
  1442. break;
  1443. case 4:
  1444. MessageBox.Show("Other");
  1445. break;
  1446. }
  1447. }
  1448.  
  1449. /// <summary>
  1450. /// handles best scores request
  1451. /// </summary>
  1452. /// <param name="msg"> message from the server </param>
  1453. private void HandleBestScores(string msg)
  1454. {
  1455. List<string> elem = new List<string>();
  1456.  
  1457. int[] arr = new int[6] { 2, -6, 2, -6, 2, -6 };
  1458.  
  1459. Helper.convertBytes(arr, elem, msg);
  1460.  
  1461. KeyValuePair<string, string>[] arrPairs = new KeyValuePair<string, string>[3];
  1462.  
  1463. if (elem.Count > 5)
  1464. {
  1465. for (int i = 0; i < 3; i++)
  1466. {
  1467. // arrPpairs is array of pairs that key is the name of the user and the value is his score
  1468. arrPairs[i] = new KeyValuePair<string, string>(elem[i * 2], (int.Parse(elem[i * 2 + 1])).ToString());
  1469. }
  1470. }
  1471.  
  1472. Invoke((MethodInvoker)(() => this.changeBestScores(arrPairs)));
  1473. }
  1474.  
  1475. /// <summary>
  1476. /// handles personal status request
  1477. /// </summary>
  1478. /// <param name="msg"> message from the server </param>
  1479. private void HandlePersonalStatus(string msg)
  1480. {
  1481. List<string> elem = new List<string>();
  1482.  
  1483. int[] arr = new int[4] { -4, -6, -6, -4 };
  1484.  
  1485. Helper.convertBytes(arr, elem, msg);
  1486.  
  1487. this.ChangePersonalStatus(elem.ToArray());
  1488. }
  1489.  
  1490. /// <summary>
  1491. /// handle get rooms
  1492. /// </summary>
  1493. /// <param name="msg"> the message from the server </param>
  1494. private void HandleGetRooms(string msg)
  1495. {
  1496. List<string> elem = new List<string>();
  1497.  
  1498. int numberOfRooms = int.Parse(msg.Substring(0, 4));
  1499.  
  1500. int[] arr = new int[numberOfRooms * 2]; // because there are 2 parameters of each room (id and name)
  1501.  
  1502. for (int i = 0; i < numberOfRooms; i++)
  1503. {
  1504. arr[i * 2] = -4;
  1505. arr[i * 2 + 1] = 2;
  1506. }
  1507.  
  1508. Helper.convertBytes(arr, elem, msg.Substring(4));
  1509.  
  1510. roomsID.Clear();
  1511. for (int i = 0; i < elem.Count; i += 2)
  1512. {
  1513. /* the variable elem consists of id and name, for exemple {5, Room1, 5, Room2} that is why i only added to
  1514. * the variable roomsID only the items in even places and remove them for the variable elem becase the
  1515. * function how get the variable elem needs only the names
  1516. */
  1517. this.roomsID.Add(elem[i]);
  1518. elem.RemoveAt(i);
  1519. }
  1520.  
  1521. Invoke((MethodInvoker)(() => this.changeRoomsList(elem)));
  1522. }
  1523.  
  1524. /// <summary>
  1525. /// handle get users
  1526. /// </summary>
  1527. /// <param name="msg"> the message from the server</param>
  1528. private void HandleGetUsers(string msg)
  1529. {
  1530. if (msg == "0") // in case of the game already start
  1531. {
  1532. MessageBox.Show("The game already start, please refresh");
  1533. return;
  1534. }
  1535.  
  1536. List<string> elem = new List<string>();
  1537.  
  1538. int numberOfUsers = int.Parse(msg.Substring(0, 1));
  1539.  
  1540. int[] arr = new int[numberOfUsers];
  1541.  
  1542. for (int i = 0; i < numberOfUsers; i++)
  1543. {
  1544. arr[i] = 2;
  1545. }
  1546.  
  1547. Helper.convertBytes(arr, elem, msg.Substring(1));
  1548.  
  1549. /* the can be called from choosing room and inside room that why the condition (the if)
  1550. * if the condition != null its mean that we are inside room and we souhld call the function ChangeLBpart
  1551. * else mean that we are chossing room and we should call function changeUsersInRoom
  1552. */
  1553.  
  1554. if (this.Controls.Find("LBLparticipates", false).FirstOrDefault() as Label != null)
  1555. {
  1556. Invoke((MethodInvoker)(() => this.ChangeLBpart(elem)));
  1557. }
  1558. else
  1559. {
  1560. Invoke((MethodInvoker)(() => this.changeUsersInRoom(elem)));
  1561. }
  1562. }
  1563.  
  1564. /// <summary>
  1565. /// handle join room
  1566. /// </summary>
  1567. /// <param name="msg"> the message from the server</param>
  1568. private void HandleJoinRoon(string msg)
  1569. {
  1570. if (msg.Substring(0, 1) == "0") // in case of succses
  1571. {
  1572. Invoke((MethodInvoker)(() => this.CleanAllControlers(null, EventArgs.Empty)));
  1573. // i create new button so i can identify whether the page should be admin or user presentation
  1574. Invoke((MethodInvoker)(() => this.InitInsideRoom(new PictureBox(), EventArgs.Empty)));
  1575.  
  1576. List<string> elem = new List<string>();
  1577.  
  1578. int[] arr = new int[2] { -2, -2 };
  1579.  
  1580. Helper.convertBytes(arr, elem, msg.Substring(1));
  1581.  
  1582. Invoke((MethodInvoker)(() => this.ChangeLabels(elem)));
  1583. }
  1584. else if (msg.Substring(0, 1) == "1") // in case of full room
  1585. {
  1586. MessageBox.Show("failed - room is full");
  1587. }
  1588. else // in case of room not exist or other reason
  1589. {
  1590. MessageBox.Show("failed - room not exist or other reason");
  1591. }
  1592. }
  1593.  
  1594. /// <summary>
  1595. /// handle leave room by clean all the controlers in the form and call InitMainScreen
  1596. /// </summary>
  1597. /// <param name="msg"> the message from the server</param>
  1598. private void HandleLeaveRoom(string msg)
  1599. {
  1600. if (msg == "0")
  1601. {
  1602. this.roomName = ""; // delete the name of room
  1603. Invoke((MethodInvoker)(() => CleanAllControlers(null, EventArgs.Empty)));
  1604. Invoke((MethodInvoker)(() => InitMainScreen(null, EventArgs.Empty)));
  1605. Invoke((MethodInvoker)(() => this.PBClose.Visible = true)); // turns on the close picture box
  1606. }
  1607. }
  1608.  
  1609. /// <summary>
  1610. /// handle close room by clean all the controlers in the form and call InitMainScreen
  1611. /// </summary>
  1612. private void HandleCloseRoom()
  1613. {
  1614. Button BTNCloseRoom = this.Controls.Find("BTNCloseRoom", false).FirstOrDefault() as Button;
  1615.  
  1616. if(BTNCloseRoom == null)
  1617. {
  1618. MessageBox.Show("The admin close the room");
  1619. }
  1620.  
  1621. this.roomName = ""; // delete the name of room
  1622. Invoke((MethodInvoker)(() => CleanAllControlers(null, EventArgs.Empty)));
  1623. Invoke((MethodInvoker)(() => InitMainScreen(null, EventArgs.Empty)));
  1624. Invoke((MethodInvoker)(() => this.PBClose.Visible = true)); // turns on the close picture box
  1625. }
  1626.  
  1627. /// <summary>
  1628. /// handle create room
  1629. /// </summary>
  1630. /// <param name="msg"> the message from the server </param>
  1631. private void handleCreateRoom(string msg)
  1632. {
  1633. if (msg == "0")
  1634. {
  1635. Invoke((MethodInvoker)(() => CleanAllControlers(null, EventArgs.Empty)));
  1636. Invoke((MethodInvoker)(() => InitInsideRoom(new Button(), EventArgs.Empty)));
  1637. Invoke((MethodInvoker)(() => ChangeLabels(this.roomProperties)));
  1638. Invoke((MethodInvoker)(() => ChangeLBpart(new List<string> { uid }))); // adds the current user to the users list
  1639. }
  1640. else
  1641. {
  1642. MessageBox.Show("Could not open the room");
  1643. }
  1644. }
  1645.  
  1646. /// <summary>
  1647. /// handle start game
  1648. /// </summary>
  1649. /// <param name="msg"> the message from the server </param>
  1650. private void HandleStartGame(string msg)
  1651. {
  1652. if (msg == "0")
  1653. {
  1654. MessageBox.Show("An error occurred");
  1655. }
  1656. else
  1657. {
  1658. this.roomName = "";
  1659.  
  1660. List<string> elem = new List<string>();
  1661.  
  1662. int[] arr = new int[5] { 3, 3, 3, 3, 3 };
  1663.  
  1664. Helper.convertBytes(arr, elem, msg);
  1665.  
  1666. Invoke((MethodInvoker)(() => this.CleanAllControlers(null, EventArgs.Empty)));
  1667. Invoke((MethodInvoker)(() => this.InitGameScreen(null, EventArgs.Empty)));
  1668. Invoke((MethodInvoker)(() => this.changeQuestion(elem)));
  1669. }
  1670. }
  1671.  
  1672. /// <summary>
  1673. /// handle answer from the user
  1674. /// </summary>
  1675. /// <param name="msg"> the message from the server </param>
  1676. private void HandleAnswer(string msg)
  1677. {
  1678. Button btnAnswer = this.Controls.Find(this.buttonAnswer, false).FirstOrDefault() as Button; // gets the button
  1679. // which i press on
  1680. if (msg == "0") // in case of wrong answer
  1681. {
  1682. btnAnswer.BackColor = Color.Red;
  1683. }
  1684. else
  1685. {
  1686. btnAnswer.BackColor = Color.LightGreen;
  1687. }
  1688. }
  1689.  
  1690. /// <summary>
  1691. /// handle finish game
  1692. /// </summary>
  1693. /// <param name="msg"> the message from the server </param>
  1694. private void HandleFinishGame(string msg)
  1695. {
  1696. string scores = "";
  1697. string line = "";
  1698.  
  1699. List<string> elem = new List<string>();
  1700.  
  1701. int numberOfUsers = int.Parse(msg.Substring(0, 1));
  1702.  
  1703. int[] arr = new int[numberOfUsers * 2];
  1704.  
  1705. for (int i = 0; i < numberOfUsers; i++)
  1706. {
  1707. arr[i * 2] = 2;
  1708. arr[i * 2 + 1] = -2;
  1709. }
  1710.  
  1711. Helper.convertBytes(arr, elem, msg.Substring(1));
  1712.  
  1713. for (int i = 0; i < elem.Count / 2; i+=2 )
  1714. {
  1715. line = "Name : " + elem[i] + ", " + "Score : " + elem[i + 1] + "\n";
  1716. scores += line;
  1717. }
  1718.  
  1719. MessageBox.Show(scores);
  1720.  
  1721. Invoke((MethodInvoker)(() => this.CleanAllControlers(null, EventArgs.Empty)));
  1722. Invoke((MethodInvoker)(() => this.InitMainScreen(null, EventArgs.Empty)));
  1723. Invoke((MethodInvoker)(() => this.PBClose.Visible = true));
  1724. }
  1725. }
  1726. }
  1727.  
  1728. // TODO:
  1729. // set font according to resulution
  1730. // remove the size of the Close button from the login and signup pages
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement