Advertisement
Guest User

Untitled

a guest
Jul 6th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 79.56 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}", int.Parse(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. 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. Invoke((MethodInvoker)(() => CleanAllControlers(null, EventArgs.Empty)));
  1321. Invoke((MethodInvoker)(() => InitMainScreen(null, EventArgs.Empty)));
  1322. Invoke((MethodInvoker)(() => this.PBClose.Visible = true));
  1323.  
  1324. stream.sendData("222");
  1325. }
  1326.  
  1327. private void BTNCloseRoom_Click(object sender, EventArgs args)
  1328. {
  1329. stream.sendData("215");
  1330. }
  1331.  
  1332. private void BTNStartGame_Click(object sender, EventArgs args)
  1333. {
  1334. stream.sendData("217");
  1335. }
  1336.  
  1337. /// <summary>
  1338. /// handles the messags from server
  1339. /// </summary>
  1340. private void handleMessage()
  1341. {
  1342. string msg = "";
  1343.  
  1344. while (true)
  1345. {
  1346. msg = this.stream.recvData(); // the message from the server
  1347.  
  1348. switch (int.Parse(msg.Substring(0, 3)))
  1349. {
  1350. case 102:
  1351. this.HandleSignIn(msg.Substring(3, 4)); // in case of sign in
  1352. break;
  1353. case 104:
  1354. this.HandleSignUp(msg.Substring(3, 4)); // in case of sign up
  1355. break;
  1356. case 124:
  1357. this.HandleBestScores(msg.Substring(3)); // in case of best scores
  1358. break;
  1359. case 126:
  1360. this.HandlePersonalStatus(msg.Substring(3)); // in case of personal status
  1361. break;
  1362. case 106:
  1363. this.HandleGetRooms(msg.Substring(3)); // in case of get rooms
  1364. break;
  1365. case 108:
  1366. this.HandleGetUsers(msg.Substring(3)); // in case of get users
  1367. break;
  1368. case 110:
  1369. this.HandleJoinRoon(msg.Substring(3)); // in case of join room
  1370. break;
  1371. case 112:
  1372. this.HandleLeaveRoom(msg.Substring(3, 1)); // in case of leave room
  1373. break;
  1374. case 116:
  1375. this.HandleCloseRoom(); // in case of close room
  1376. break;
  1377. case 118:
  1378. this.HandleStartGame(msg.Substring(3)); // in case of start game
  1379. break;
  1380. case 114:
  1381. this.handleCreateRoom(msg.Substring(3, 1)); // in case of create room
  1382. break;
  1383. case 120:
  1384. this.HandleAnswer(msg.Substring(3, 1)); // in case of gets answer
  1385. break;
  1386. case 121:
  1387. this.HandleFinishGame(msg.Substring(3)); // in case of finish game
  1388. break;
  1389. }
  1390. }
  1391. }
  1392.  
  1393. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  1394. {
  1395. this.t.Abort();
  1396. this.stream.sendData("299");
  1397. this.stream.CloseStream();
  1398. this.PBClose.Dispose();
  1399. }
  1400.  
  1401. /// <summary>
  1402. /// handles sign in request
  1403. /// </summary>
  1404. /// <param name="msg"> the message from the server </param>
  1405. private void HandleSignIn(string msg)
  1406. {
  1407. switch (int.Parse(msg))
  1408. {
  1409. case 0:
  1410. uid = ((TextBox)this.Controls.Find("TXTusername", false).FirstOrDefault()).Text; // to show his username in main screen
  1411. Invoke((MethodInvoker)(() => CleanAllControlers(null, EventArgs.Empty)));
  1412. Invoke((MethodInvoker)(() => InitMainScreen(null, EventArgs.Empty)));
  1413. break;
  1414. case 1:
  1415. MessageBox.Show("Wrong Details");
  1416. break;
  1417. case 2:
  1418. MessageBox.Show("User is already connected");
  1419. break;
  1420. }
  1421. }
  1422.  
  1423. /// <summary>
  1424. /// handles sign up request
  1425. /// </summary>
  1426. /// <param name="msg"> message from the server </param>
  1427. private void HandleSignUp(string msg)
  1428. {
  1429. switch (int.Parse(msg))
  1430. {
  1431. case 0:
  1432. Invoke((MethodInvoker)(() => CleanAllControlers(null, EventArgs.Empty)));
  1433. Invoke((MethodInvoker)(() => this.InitLoginScreen(null, EventArgs.Empty)));
  1434. break;
  1435. case 1:
  1436. MessageBox.Show("Pass illegal");
  1437. break;
  1438. case 2:
  1439. MessageBox.Show("Username is already exists");
  1440. break;
  1441. case 3:
  1442. MessageBox.Show("Username is illegal");
  1443. break;
  1444. case 4:
  1445. MessageBox.Show("Other");
  1446. break;
  1447. }
  1448. }
  1449.  
  1450. /// <summary>
  1451. /// handles best scores request
  1452. /// </summary>
  1453. /// <param name="msg"> message from the server </param>
  1454. private void HandleBestScores(string msg)
  1455. {
  1456. List<string> elem = new List<string>();
  1457.  
  1458. int[] arr = new int[6] { 2, -6, 2, -6, 2, -6 };
  1459.  
  1460. Helper.convertBytes(arr, elem, msg);
  1461.  
  1462. KeyValuePair<string, string>[] arrPairs = new KeyValuePair<string, string>[3];
  1463.  
  1464. if (elem.Count > 5)
  1465. {
  1466. for (int i = 0; i < 3; i++)
  1467. {
  1468. // arrPpairs is array of pairs that key is the name of the user and the value is his score
  1469. arrPairs[i] = new KeyValuePair<string, string>(elem[i * 2], (int.Parse(elem[i * 2 + 1])).ToString());
  1470. }
  1471. }
  1472.  
  1473. Invoke((MethodInvoker)(() => this.changeBestScores(arrPairs)));
  1474. }
  1475.  
  1476. /// <summary>
  1477. /// handles personal status request
  1478. /// </summary>
  1479. /// <param name="msg"> message from the server </param>
  1480. private void HandlePersonalStatus(string msg)
  1481. {
  1482. List<string> elem = new List<string>();
  1483.  
  1484. int[] arr = new int[4] { -4, -6, -6, -4 };
  1485.  
  1486. Helper.convertBytes(arr, elem, msg);
  1487.  
  1488. this.ChangePersonalStatus(elem.ToArray());
  1489. }
  1490.  
  1491. /// <summary>
  1492. /// handle get rooms
  1493. /// </summary>
  1494. /// <param name="msg"> the message from the server </param>
  1495. private void HandleGetRooms(string msg)
  1496. {
  1497. List<string> elem = new List<string>();
  1498.  
  1499. int numberOfRooms = int.Parse(msg.Substring(0, 4));
  1500.  
  1501. int[] arr = new int[numberOfRooms * 2]; // because there are 2 parameters of each room (id and name)
  1502.  
  1503. for (int i = 0; i < numberOfRooms; i++)
  1504. {
  1505. arr[i * 2] = -4;
  1506. arr[i * 2 + 1] = 2;
  1507. }
  1508.  
  1509. Helper.convertBytes(arr, elem, msg.Substring(4));
  1510.  
  1511. roomsID.Clear();
  1512. for (int i = 0; i < elem.Count; i += 2)
  1513. {
  1514. /* the variable elem consists of id and name, for exemple {5, Room1, 5, Room2} that is why i only added to
  1515. * the variable roomsID only the items in even places and remove them for the variable elem becase the
  1516. * function how get the variable elem needs only the names
  1517. */
  1518. this.roomsID.Add(elem[i]);
  1519. elem.RemoveAt(i);
  1520. }
  1521.  
  1522. Invoke((MethodInvoker)(() => this.changeRoomsList(elem)));
  1523. }
  1524.  
  1525. /// <summary>
  1526. /// handle get users
  1527. /// </summary>
  1528. /// <param name="msg"> the message from the server</param>
  1529. private void HandleGetUsers(string msg)
  1530. {
  1531. if (msg == "0") // in case of the game already start
  1532. {
  1533. MessageBox.Show("The game already start, please refresh");
  1534. return;
  1535. }
  1536.  
  1537. List<string> elem = new List<string>();
  1538.  
  1539. int numberOfUsers = int.Parse(msg.Substring(0, 1));
  1540.  
  1541. int[] arr = new int[numberOfUsers];
  1542.  
  1543. for (int i = 0; i < numberOfUsers; i++)
  1544. {
  1545. arr[i] = 2;
  1546. }
  1547.  
  1548. Helper.convertBytes(arr, elem, msg.Substring(1));
  1549.  
  1550. /* the can be called from choosing room and inside room that why the condition (the if)
  1551. * if the condition != null its mean that we are inside room and we souhld call the function ChangeLBpart
  1552. * else mean that we are chossing room and we should call function changeUsersInRoom
  1553. */
  1554.  
  1555. if (this.Controls.Find("LBLparticipates", false).FirstOrDefault() as Label != null)
  1556. {
  1557. Invoke((MethodInvoker)(() => this.ChangeLBpart(elem)));
  1558. }
  1559. else
  1560. {
  1561. Invoke((MethodInvoker)(() => this.changeUsersInRoom(elem)));
  1562. }
  1563. }
  1564.  
  1565. /// <summary>
  1566. /// handle join room
  1567. /// </summary>
  1568. /// <param name="msg"> the message from the server</param>
  1569. private void HandleJoinRoon(string msg)
  1570. {
  1571. if (msg.Substring(0, 1) == "0") // in case of succses
  1572. {
  1573. Invoke((MethodInvoker)(() => this.CleanAllControlers(null, EventArgs.Empty)));
  1574. // i create new button so i can identify whether the page should be admin or user presentation
  1575. Invoke((MethodInvoker)(() => this.InitInsideRoom(new PictureBox(), EventArgs.Empty)));
  1576.  
  1577. List<string> elem = new List<string>();
  1578.  
  1579. int[] arr = new int[2] { -2, -2 };
  1580.  
  1581. Helper.convertBytes(arr, elem, msg.Substring(1));
  1582.  
  1583. Invoke((MethodInvoker)(() => this.ChangeLabels(elem)));
  1584. }
  1585. else if (msg.Substring(0, 1) == "1") // in case of full room
  1586. {
  1587. MessageBox.Show("failed - room is full");
  1588. }
  1589. else // in case of room not exist or other reason
  1590. {
  1591. MessageBox.Show("failed - room not exist or other reason");
  1592. }
  1593. }
  1594.  
  1595. /// <summary>
  1596. /// handle leave room by clean all the controlers in the form and call InitMainScreen
  1597. /// </summary>
  1598. /// <param name="msg"> the message from the server</param>
  1599. private void HandleLeaveRoom(string msg)
  1600. {
  1601. if (msg == "0")
  1602. {
  1603. this.roomName = ""; // delete the name of room
  1604. Invoke((MethodInvoker)(() => CleanAllControlers(null, EventArgs.Empty)));
  1605. Invoke((MethodInvoker)(() => InitMainScreen(null, EventArgs.Empty)));
  1606. Invoke((MethodInvoker)(() => this.PBClose.Visible = true)); // turns on the close picture box
  1607. }
  1608. }
  1609.  
  1610. /// <summary>
  1611. /// handle close room by clean all the controlers in the form and call InitMainScreen
  1612. /// </summary>
  1613. private void HandleCloseRoom()
  1614. {
  1615. Button BTNCloseRoom = this.Controls.Find("BTNCloseRoom", false).FirstOrDefault() as Button;
  1616.  
  1617. if(BTNCloseRoom == null)
  1618. {
  1619. MessageBox.Show("The admin close the room");
  1620. }
  1621.  
  1622. this.roomName = ""; // delete the name of room
  1623. Invoke((MethodInvoker)(() => CleanAllControlers(null, EventArgs.Empty)));
  1624. Invoke((MethodInvoker)(() => InitMainScreen(null, EventArgs.Empty)));
  1625. Invoke((MethodInvoker)(() => this.PBClose.Visible = true)); // turns on the close picture box
  1626. }
  1627.  
  1628. /// <summary>
  1629. /// handle create room
  1630. /// </summary>
  1631. /// <param name="msg"> the message from the server </param>
  1632. private void handleCreateRoom(string msg)
  1633. {
  1634. if (msg == "0")
  1635. {
  1636. Invoke((MethodInvoker)(() => CleanAllControlers(null, EventArgs.Empty)));
  1637. Invoke((MethodInvoker)(() => InitInsideRoom(new Button(), EventArgs.Empty)));
  1638. Invoke((MethodInvoker)(() => ChangeLabels(this.roomProperties)));
  1639. Invoke((MethodInvoker)(() => ChangeLBpart(new List<string> { uid }))); // adds the current user to the users list
  1640. }
  1641. else
  1642. {
  1643. MessageBox.Show("Could not open the room");
  1644. }
  1645. }
  1646.  
  1647. /// <summary>
  1648. /// handle start game
  1649. /// </summary>
  1650. /// <param name="msg"> the message from the server </param>
  1651. private void HandleStartGame(string msg)
  1652. {
  1653. if (msg == "0")
  1654. {
  1655. MessageBox.Show("An error occurred");
  1656. }
  1657. else
  1658. {
  1659. this.roomName = "";
  1660.  
  1661. List<string> elem = new List<string>();
  1662.  
  1663. int[] arr = new int[5] { 3, 3, 3, 3, 3 };
  1664.  
  1665. Helper.convertBytes(arr, elem, msg);
  1666.  
  1667. Invoke((MethodInvoker)(() => this.CleanAllControlers(null, EventArgs.Empty)));
  1668. Invoke((MethodInvoker)(() => this.InitGameScreen(null, EventArgs.Empty)));
  1669. Invoke((MethodInvoker)(() => this.changeQuestion(elem)));
  1670. }
  1671. }
  1672.  
  1673. /// <summary>
  1674. /// handle answer from the user
  1675. /// </summary>
  1676. /// <param name="msg"> the message from the server </param>
  1677. private void HandleAnswer(string msg)
  1678. {
  1679. Button btnAnswer = this.Controls.Find(this.buttonAnswer, false).FirstOrDefault() as Button; // gets the button
  1680. // which i press on
  1681. if (msg == "0") // in case of wrong answer
  1682. {
  1683. btnAnswer.BackColor = Color.Red;
  1684. }
  1685. else
  1686. {
  1687. btnAnswer.BackColor = Color.LightGreen;
  1688. }
  1689. }
  1690.  
  1691. /// <summary>
  1692. /// handle finish game
  1693. /// </summary>
  1694. /// <param name="msg"> the message from the server </param>
  1695. private void HandleFinishGame(string msg)
  1696. {
  1697. string scores = "";
  1698. string line = "";
  1699.  
  1700. List<string> elem = new List<string>();
  1701.  
  1702. int numberOfUsers = int.Parse(msg.Substring(0, 1));
  1703.  
  1704. int[] arr = new int[numberOfUsers * 2];
  1705.  
  1706. for (int i = 0; i < numberOfUsers; i++)
  1707. {
  1708. arr[i * 2] = 2;
  1709. arr[i * 2 + 1] = -2;
  1710. }
  1711.  
  1712. Helper.convertBytes(arr, elem, msg.Substring(1));
  1713.  
  1714. for (int i = 0; i < elem.Count / 2; i+=2 )
  1715. {
  1716. line = "Name : " + elem[i] + ", " + "Score : " + elem[i + 1] + "\n";
  1717. scores += line;
  1718. }
  1719.  
  1720. MessageBox.Show(scores);
  1721.  
  1722. Invoke((MethodInvoker)(() => this.CleanAllControlers(null, EventArgs.Empty)));
  1723. Invoke((MethodInvoker)(() => this.InitMainScreen(null, EventArgs.Empty)));
  1724. Invoke((MethodInvoker)(() => this.PBClose.Visible = true));
  1725. }
  1726. }
  1727. }
  1728.  
  1729. // TODO:
  1730. // set font according to resulution
  1731. // remove the size of the Close button from the login and signup pages
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement