Advertisement
Guest User

Untitled

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