Advertisement
Guest User

Untitled

a guest
Jul 5th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 56.13 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. roomsID.Add("-1"); // so the user cant join this fake room
  458. }
  459.  
  460. ListBox LBUsers = this.Controls.Find("LBUsers", false).FirstOrDefault() as ListBox;
  461. Label LBLUsers = this.Controls.Find("LBLUsers", false).FirstOrDefault() as Label;
  462.  
  463. // makes the users lists to disapear
  464. LBUsers.Visible = false;
  465. LBLUsers.Visible = false;
  466.  
  467. ListBox LBRooms = this.Controls.Find("LBRooms", false).FirstOrDefault() as ListBox;
  468. if (LBRooms != null)
  469. {
  470. LBRooms.Items.Clear();
  471. foreach (string name in roomsNames)
  472. {
  473. LBRooms.Items.Add(name);
  474. }
  475.  
  476. Label LBLRooms = this.Controls.Find("LBLRooms", false).FirstOrDefault() as Label;
  477. PictureBox PBRefresh = this.Controls.Find("PBRefresh", false).FirstOrDefault() as PictureBox;
  478.  
  479. // sets the height of the list box
  480. if (LBLRooms != null && PBRefresh != null)
  481. {
  482. int maxHeight = PBRefresh.Location.Y - LBLRooms.Location.Y - LBLRooms.Height - alligmentFromSubjectY;
  483. int height = LBRooms.ItemHeight;
  484. int maxObj = maxHeight / height;
  485.  
  486. LBRooms.Height = roomsNames.Count > maxObj ? height * maxObj : roomsNames.Count * height;
  487. }
  488.  
  489. // moves the list box to its proper place
  490. LBRooms.Location = new Point(this.Width / 2 - LBRooms.Width / 2, LBRooms.Location.Y);
  491. LBLRooms.Location = new Point(LBRooms.Location.X + LBRooms.Width / 2 - LBLRooms.Width / 2, LBLRooms.Location.Y);
  492.  
  493. this.Update();
  494. }
  495. }
  496.  
  497. /// <summary>
  498. /// changes the users in room list box
  499. /// should be called when an item on the rooms list is clicked
  500. /// </summary>
  501. /// <param name="users">the users that will be presented on the users list</param>
  502. private void changeUsersInRoom(List<string> users)
  503. {
  504. if (users.Count < 1)
  505. {
  506. users.Add("");
  507. }
  508.  
  509. ListBox LBUsers = this.Controls.Find("LBUsers", false).FirstOrDefault() as ListBox;
  510. Label LBLUsers = this.Controls.Find("LBLUsers", false).FirstOrDefault() as Label;
  511. Label LBLRooms = this.Controls.Find("LBLRooms", false).FirstOrDefault() as Label;
  512. PictureBox PBRefresh = this.Controls.Find("PBRefresh", false).FirstOrDefault() as PictureBox;
  513. ListBox LBRooms = this.Controls.Find("LBRooms", false).FirstOrDefault() as ListBox;
  514.  
  515. // sets the height of the list box
  516. if (LBLUsers != null && PBRefresh != null && LBUsers != null)
  517. {
  518. int maxHeight = PBRefresh.Location.Y - LBLUsers.Location.Y - LBLUsers.Height - alligmentFromSubjectY;
  519. int height = LBUsers.ItemHeight;
  520. int maxObj = maxHeight / height;
  521.  
  522. // changes the items in the list box
  523. LBUsers.Items.Clear();
  524. foreach (string user in users)
  525. {
  526. LBUsers.Items.Add(user);
  527. }
  528.  
  529. LBUsers.Height = users.Count > maxObj ? height * maxObj : users.Count * height;
  530.  
  531. // sets the rooms stuff to their proper place
  532. LBRooms.Location = new Point(this.Width - LBRooms.Width - LBUsers.Location.X, LBRooms.Location.Y);
  533. LBLRooms.Location = new Point(LBRooms.Location.X + LBRooms.Width / 2 - LBLRooms.Width / 2, LBLRooms.Location.Y);
  534.  
  535. LBUsers.Visible = true;
  536. LBLUsers.Visible = true;
  537. }
  538. }
  539.  
  540. /// <summary>
  541. /// changes the form to inside room performance
  542. /// </summary>
  543. /// <param name="sender"></param>
  544. /// <param name="args"></param>
  545. private void InitInsideRoom(object sender, EventArgs args)
  546. {
  547. int reminingY = this.Height;
  548. int nextYPos = 0;
  549. int buttonSectorY = 0;
  550. int buttonsHeight = this.Height / IN_ROOM_FORM_BUTTON_HEIGHT;
  551. int buttonWidth = this.Width / IN_ROOM_FORM_BUTTON_WIDTH;
  552. int alligmentFromButtonsY = 0;
  553. int alligmentFromLabelsY = 0;
  554. List<Label> labels = new List<Label>();
  555.  
  556. this.PBClose.Visible = false;
  557.  
  558. // sets the subject label
  559. Label LBLSubject = new MyLabel("You are in " + this.roomName + " room", "LBLSubject"); // i create a label to know the width of its text
  560. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  561. this.Controls.Add(LBLSubject);
  562.  
  563. nextYPos = LBLSubject.Location.Y + LBLSubject.Height + alligmentFromSubjectY;
  564. reminingY -= nextYPos;
  565. buttonSectorY = reminingY / IN_ROOM_FORM_BUTTON_SECTOR_Y;
  566. reminingY -= buttonSectorY;
  567. alligmentFromButtonsY = (buttonSectorY - buttonsHeight) / 2;
  568.  
  569. MessageBox.Show("SENDER = " + sender.GetType().ToString());
  570.  
  571. if (sender is PictureBox) // in case its a user presentation
  572. {
  573. Button BTNLeave = new MyButton("BTNLeave", new Size(buttonWidth, buttonsHeight), "Leave Room");
  574. BTNLeave.Location = new Point(this.Width / 2 - buttonWidth / 2, this.Height - buttonsHeight - alligmentFromButtonsY);
  575. BTNLeave.Click += (EventHandler)BTNLeave_Click;
  576. this.Controls.Add(BTNLeave);
  577. }
  578. else if (sender is Button) // in case its admin presentation
  579. {
  580. int alligmentFromButtonsX = (this.Width - buttonWidth * 2) / 3;
  581.  
  582. // configures and adds the close room button
  583. Button BTNCloseRoom = new MyButton("BTNCloseRoom", new Size(buttonWidth, buttonsHeight), "Close Room");
  584. BTNCloseRoom.Location = new Point(alligmentFromButtonsX, this.Height - BTNCloseRoom.Height - alligmentFromButtonsY);
  585. BTNCloseRoom.Click += (EventHandler)BTNCloseRoom_Click;
  586. this.Controls.Add(BTNCloseRoom);
  587.  
  588. // configures and adds the start game button
  589. Button BTNStartGame = new MyButton("BTNStartGame", new Size(buttonWidth, buttonsHeight), "Start Game");
  590. BTNStartGame.Location = new Point(this.Width - buttonWidth - alligmentFromButtonsX, BTNCloseRoom.Location.Y);
  591. BTNStartGame.Click += (EventHandler)BTNStartGame_Click;
  592. this.Controls.Add(BTNStartGame);
  593.  
  594. labels.Add(new MyLabel("Max number of users: ", "LBLMaxUsers"));
  595. }
  596.  
  597. Label LBLparticipates = new MyLabel("current participates are: ", "LBLparticipates");
  598.  
  599. // configures and adds the participates list box
  600. ListBox LBpart = new MyListBox("LBpart", new Size(this.Width / IN_ROOM_FORM_LB_WIDTH, reminingY / 4));
  601. LBpart.Location = new Point(this.Width / 2 - LBpart.Width / 2, nextYPos + LBLparticipates.Height);
  602. this.Controls.Add(LBpart);
  603.  
  604. // configures and adds the label of participates
  605. LBLparticipates.Location = new Point(LBpart.Location.X + LBpart.Width / 2 - LBLparticipates.Width / 2, nextYPos);
  606. this.Controls.Add(LBLparticipates);
  607.  
  608. // sets the room properties labels
  609. labels.Add(new MyLabel("Number of questions: ", "LBLNumOfQuestions"));
  610. labels.Add(new MyLabel("Time per question: ", "LBLTimePerQuestion"));
  611.  
  612. // calculates the needed alligment
  613. alligmentFromLabelsY = (reminingY - labels[0].Height * labels.Count) / (labels.Count + 1);
  614. nextYPos += alligmentFromLabelsY;
  615.  
  616. foreach (Label l in labels)
  617. {
  618. l.Location = new Point(0, nextYPos);
  619. nextYPos += alligmentFromLabelsY;
  620. this.Controls.Add(l);
  621. }
  622. }
  623.  
  624. /// <summary>
  625. /// changes the list box of participates
  626. /// </summary>
  627. /// <param name="users">the users to be added to the list box</param>
  628. private void ChangeLBpart(List<string> users)
  629. {
  630. Label LBLparticipates = this.Controls.Find("LBLparticipates", false).FirstOrDefault() as Label;
  631. ListBox LBpart = this.Controls.Find("LBpart", false).FirstOrDefault() as ListBox;
  632.  
  633. if (users.Count < 1)
  634. {
  635. users.Add("");
  636. }
  637.  
  638. if (LBLparticipates != null && LBpart != null)
  639. {
  640. int maxHeight = this.Height - this.Height / IN_ROOM_FORM_BUTTON_SECTOR_Y - LBLparticipates.Location.Y - LBLparticipates.Height;
  641. int height = LBpart.ItemHeight;
  642. int maxObj = maxHeight / height;
  643.  
  644. // changes the height of the list box according to the items in it
  645. LBpart.Height = users.Count > maxObj ? height * maxObj : height * users.Count;
  646.  
  647. LBpart.Items.Clear();
  648. foreach (string user in users)
  649. {
  650. LBpart.Items.Add(user);
  651. }
  652. }
  653. }
  654.  
  655. /// <summary>
  656. /// changes the labels of in room properties
  657. /// in case of user presentation the properties param should be made out of the string the server sent
  658. /// in case of admin presentation the properties param should be the global parameter roomProperties
  659. /// </summary>
  660. /// <param name="properties">the properties that would be in the labels, the order should be maxusers(if exists), numberOfQuestions, time per questions</param>
  661. private void ChangeLabels(List<string> properties)
  662. {
  663. Label[] labels = new Label[properties.Count];
  664.  
  665. if (properties.Count > 2)
  666. {
  667. labels[0] = this.Controls.Find("LBLMaxUsers", false).FirstOrDefault() as Label;
  668. }
  669.  
  670. labels[labels.Length - 2] = this.Controls.Find("LBLNumOfQuestions", false).FirstOrDefault() as Label;
  671. labels[labels.Length - 1] = this.Controls.Find("LBLTimePerQuestion", false).FirstOrDefault() as Label;
  672.  
  673. for (int i = 0; i < labels.Length; i++)
  674. {
  675. if (labels[i] != null)
  676. {
  677. labels[i].Text += (int.Parse(properties[i]).ToString());
  678. labels[i].Width = TextRenderer.MeasureText(labels[i].Text, labels[i].Font).Width;
  679. }
  680. }
  681. }
  682.  
  683. /// <summary>
  684. /// changes the form to personal status
  685. /// </summary>
  686. /// <param name="sender"></param>
  687. /// <param name="args"></param>
  688. private void InitPersonalStatus(object sender, EventArgs args)
  689. {
  690. int reminingY = this.PBClose.Location.Y;
  691. int nextYPos = 0;
  692. int alligmentFromLabelsY = 0;
  693.  
  694. // sets the background of the form to personal status background
  695. this.BackgroundImage = Properties.Resources.personalStatusBG;
  696.  
  697. // sets the font of the form to black
  698. this.ForeColor = Color.Black;
  699.  
  700. // sets the subject label
  701. Label LBLSubject = new MyLabel("My Performance: ", "LBLSubject"); // i create a label to know the width of its text
  702. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  703. this.Controls.Add(LBLSubject);
  704.  
  705. nextYPos = LBLSubject.Location.Y + LBLSubject.Height + alligmentFromSubjectY;
  706. reminingY -= nextYPos;
  707.  
  708. Label LBLnumOfGames = new MyLabel("number of Games: ", "LBLnumOfGames"); // i create a label so i can know the height of it
  709.  
  710. alligmentFromLabelsY = (reminingY - LBLnumOfGames.Height * 4) / 5;
  711. nextYPos += alligmentFromLabelsY;
  712.  
  713. // configures and adds the number of games label
  714. 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
  715. this.Controls.Add(LBLnumOfGames);
  716.  
  717. nextYPos += alligmentFromLabelsY;
  718.  
  719. // configures and adds the number of right answers label
  720. Label LBLnumOfRightAns = new MyLabel("number of right answers: ", "LBLnumOfRightAns");
  721. LBLnumOfRightAns.Location = new Point(0, nextYPos);
  722. this.Controls.Add(LBLnumOfRightAns);
  723.  
  724. nextYPos += alligmentFromLabelsY;
  725.  
  726. // configures and adds the number of wrong answers label
  727. Label LBLnumOfWrongAns = new MyLabel("number of wrong answers: ", "LBLnumOfWrongAns");
  728. LBLnumOfWrongAns.Location = new Point(0, nextYPos);
  729. this.Controls.Add(LBLnumOfWrongAns);
  730.  
  731. nextYPos += alligmentFromLabelsY;
  732.  
  733. // configures and adds the average time per answer
  734. Label LBLavgTimePerAns = new MyLabel("average time for answer: ", "LBLavgTimePerAns");
  735. LBLavgTimePerAns.Location = new Point(0, nextYPos);
  736. this.Controls.Add(LBLavgTimePerAns);
  737.  
  738. // sets the back button
  739. int PCHeight = this.Height / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  740. int PCWidth = this.Width / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  741. PictureBox PBBack = new MyPictureBox("PBBack", "back", new Size(PCWidth, PCHeight));
  742. PBBack.Location = new Point(this.Width - PCWidth, this.Height / ALLIGMENT_FROM_TOP);
  743. PBBack.Click += (EventHandler)CleanAllControlers;
  744. PBBack.Click += (EventHandler)InitMainScreen;
  745. this.Controls.Add(PBBack);
  746. }
  747.  
  748. /// <summary>
  749. /// changes the labels of personal status' form, has to be be used after InitPersonalStatus function used
  750. /// </summary>
  751. /// <param name="arr">an array that contains the values of the fields example { 4, 5, 10, 3 }</param>
  752. private void ChangePersonalStatus(string[] arr)
  753. {
  754. if (arr.Length > 3)
  755. {
  756. Label[] labels = new Label[4];
  757. labels[0] = this.Controls.Find("LBLnumOfGames", false).FirstOrDefault() as Label;
  758. labels[1] = this.Controls.Find("LBLnumOfRightAns", false).FirstOrDefault() as Label;
  759. labels[2] = this.Controls.Find("LBLnumOfWrongAns", false).FirstOrDefault() as Label;
  760. labels[3] = this.Controls.Find("LBLavgTimePerAns", false).FirstOrDefault() as Label;
  761.  
  762. for (int i = 0; i < 4; i++)
  763. {
  764. if (labels[i] != null)
  765. {
  766. if (i == 3)
  767. {
  768. arr[i] = String.Format("{0}.{1}", (int.Parse(arr[i]) / 100).ToString(), (int.Parse(arr[i]) % 100).ToString());
  769. Invoke((MethodInvoker)(() => labels[i].Text += arr[i]));
  770. }
  771. else
  772. {
  773. Invoke((MethodInvoker)(() => labels[i].Text += (int.Parse(arr[i])).ToString()));
  774. }
  775. Invoke((MethodInvoker)(() => labels[i].Width = TextRenderer.MeasureText(labels[i].Text, labels[i].Font).Width));
  776. Invoke((MethodInvoker)(() => labels[i].Location = new Point(this.Width / 2 - labels[i].Width / 2, labels[i].Location.Y)));
  777. }
  778. }
  779. }
  780. }
  781.  
  782. /// <summary>
  783. /// changes the form to show the best scores
  784. /// </summary>
  785. /// <param name="sender"></param>
  786. /// <param name="args"></param>
  787. private void InitBestScores(object sender, EventArgs args)
  788. {
  789. int reminingY = PBClose.Location.Y;
  790. int nextYPos = 0;
  791. int pictureBoxHeight = 0;
  792. int pictureBoxWidth = 0;
  793. int alligmentFromPicturesY = 0;
  794. int alligmentFromPictureX = 0;
  795.  
  796. // sets the background image to best scores BG
  797. this.BackgroundImage = Properties.Resources.winners;
  798.  
  799. // sets the font of the form to black
  800. this.ForeColor = Color.Black;
  801.  
  802. // sets the subject label
  803. Label LBLSubject = new MyLabel("Best Scores: ", "LBLSubject"); // i create a label to know the width of its text
  804. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  805. this.Controls.Add(LBLSubject);
  806.  
  807. // creates the label that contains the first player's scores
  808. Label LBL1st = new MyLabel("ABCD", "LBL1st"); // i put text in the label so i can know its properties
  809.  
  810. // sets the next position for the picture boxes sector
  811. nextYPos += (LBLSubject.Location.Y + LBLSubject.Height + alligmentFromSubjectY);
  812. reminingY -= nextYPos;
  813.  
  814. // configures the properties of the picture boxes
  815. pictureBoxHeight = reminingY / BEST_SCORES_FORM_PB_HEIGHT;
  816. pictureBoxWidth = this.Width / BEST_SCORES_FORM_PB_WIDTH;
  817.  
  818. // configures the picture box sector
  819. alligmentFromPicturesY = (reminingY - pictureBoxHeight * 2 - LBL1st.Height * 2) / 2;
  820. alligmentFromPictureX = (this.Width - pictureBoxWidth * 3) / 4;
  821. nextYPos += alligmentFromPicturesY;
  822.  
  823. // sets the first place picture box
  824. PictureBox PB1st = new MyPictureBox("PB1st", "_1st", new Size(pictureBoxWidth, pictureBoxHeight));
  825. PB1st.Location = new Point(alligmentFromPictureX * 2 + pictureBoxWidth, nextYPos);
  826. this.Controls.Add(PB1st);
  827.  
  828. LBL1st.Location = new Point(PB1st.Location.X + PB1st.Width / 2, nextYPos - LBL1st.Height);
  829. this.Controls.Add(LBL1st);
  830.  
  831. // sets the postion to the next picture box
  832. nextYPos += alligmentFromPicturesY;
  833.  
  834. // sets the second best score picture box
  835. PictureBox PB2nd = new MyPictureBox("PB2nd", "_2nd", new Size(pictureBoxWidth, pictureBoxHeight));
  836. PB2nd.Location = new Point(alligmentFromPictureX, nextYPos);
  837. this.Controls.Add(PB2nd);
  838.  
  839. // sets the label of the second best score
  840. Label LBL2nd = new MyLabel("ABCD", "LBL2nd"); // i put text in the label so i can know its properties
  841. LBL2nd.Location = new Point(PB2nd.Location.X + PB2nd.Width / 2, PB2nd.Location.Y - LBL2nd.Height);
  842. this.Controls.Add(LBL2nd);
  843.  
  844. // sets the picture box of the second best score
  845. PictureBox PB3rd = new MyPictureBox("PB3rd", "_3rd", new Size(pictureBoxWidth, pictureBoxHeight));
  846. PB3rd.Location = new Point(this.Width - alligmentFromPictureX - PB3rd.Width, nextYPos);
  847. this.Controls.Add(PB3rd);
  848.  
  849. // sets the label of th ethird best score
  850. Label LBL3rd = new MyLabel("ABCD", "LBL3rd"); // i put text in the label so i can know its properties
  851. LBL3rd.Location = new Point(PB3rd.Location.X + PB3rd.Width / 2, nextYPos - LBL3rd.Height);
  852. this.Controls.Add(LBL3rd);
  853.  
  854. // sets the back button
  855. int PCHeight = this.Height / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  856. int PCWidth = this.Width / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  857. PictureBox PBBack = new MyPictureBox("PBBack", "back", new Size(PCWidth, PCHeight));
  858. PBBack.Location = new Point(this.Width - PCWidth, this.Height / ALLIGMENT_FROM_TOP);
  859. PBBack.Click += (EventHandler)CleanAllControlers;
  860. PBBack.Click += (EventHandler)InitMainScreen;
  861. this.Controls.Add(PBBack);
  862. }
  863.  
  864. /// <summary>
  865. /// changes the best scores labels
  866. /// should be used when clicked on get best scores button, after InitBestScores was called
  867. /// </summary>
  868. /// <param name="arr">an array of pairs in which the key is the name of the user and the value is his score</param>
  869. private void changeBestScores(KeyValuePair<string, string>[] arr)
  870. {
  871. Label[] labels = new Label[3];
  872.  
  873. labels[0] = this.Controls.Find("LBL1st", false).FirstOrDefault() as Label;
  874. labels[1] = this.Controls.Find("LBL2nd", false).FirstOrDefault() as Label;
  875. labels[2] = this.Controls.Find("LBL3rd", false).FirstOrDefault() as Label;
  876.  
  877. for (int i = 0; i < 3; i++)
  878. {
  879. if (labels[i] != null)
  880. {
  881. labels[i].Text = arr[i].Key + ": " + arr[i].Value;
  882. labels[i].Width = TextRenderer.MeasureText(labels[i].Text, labels[i].Font).Width;
  883. labels[i].Location = new Point(labels[i].Location.X - labels[i].Width / 2, labels[i].Location.Y);
  884. }
  885. }
  886. }
  887.  
  888. /// <summary>
  889. /// cleans the screen from all the controls on it
  890. /// </summary>
  891. private void CleanAllControlers(object sender, EventArgs args)
  892. {
  893. //foreach (Control c in this.Controls)
  894. //{
  895. // MessageBox.Show(c.GetType().ToString());
  896. // this.Controls.Remove(c);
  897. //}
  898.  
  899. this.Controls.Clear();
  900. this.Controls.Add(PBClose);
  901. }
  902.  
  903. private void PBClose_Click(object sender, EventArgs args)
  904. {
  905. this.Close();
  906. }
  907.  
  908. private void BTNBest_Click(object sender, EventArgs args)
  909. {
  910. stream.sendData("223");
  911. }
  912.  
  913. private void PBLogOut_Click(object sender, EventArgs args)
  914. {
  915. stream.sendData("201");
  916. }
  917.  
  918. private void BTNPersonal_Click(object sender, EventArgs args)
  919. {
  920. stream.sendData("225");
  921. }
  922.  
  923. private void BTNJoin_Click(object sender, EventArgs args)
  924. {
  925. stream.sendData("205");
  926. }
  927.  
  928. private void PBRefresh_Click(object sender, EventArgs args)
  929. {
  930. stream.sendData("205");
  931. }
  932.  
  933. private void PBJoin_Click(object sender, EventArgs args)
  934. {
  935. ListBox LBRooms = this.Controls.Find("LBRooms", false).FirstOrDefault() as ListBox;
  936.  
  937. if(LBRooms.SelectedIndex >= 0 && int.Parse(roomsID[LBRooms.SelectedIndex]) >= 0)
  938. {
  939. this.roomName = LBRooms.Items[LBRooms.SelectedIndex] as string;
  940. this.stream.sendData("209" + this.roomsID[LBRooms.SelectedIndex]);
  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. if(int.Parse(this.roomsID[LBRooms.SelectedIndex]) >= 0)
  949. {
  950. this.stream.sendData("207" + this.roomsID[LBRooms.SelectedIndex]);
  951. }
  952. }
  953.  
  954. /// <summary>
  955. /// sends the username, password and email to server according to the format for sign up
  956. /// </summary>
  957. private void PBSign_Up_Click(object sender, EventArgs args)
  958. {
  959. TextBox txtUserName = (TextBox)this.Controls.Find("TXTusername", false)[0];
  960. TextBox txtPassword = (TextBox)this.Controls.Find("TXTpsw", false)[0];
  961. TextBox txtEmail = (TextBox)this.Controls.Find("TXTemail", false)[0];
  962.  
  963. if (txtUserName != null && txtPassword != null && txtEmail != null)
  964. {
  965. 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));
  966. }
  967. }
  968.  
  969. /// <summary>
  970. /// sends the username and password to server according to the format for sign in
  971. /// </summary>
  972. private void PBSign_In_Click(object sender, EventArgs e)
  973. {
  974. TextBox txtUserName = (TextBox)this.Controls.Find("TXTusername", false)[0];
  975. TextBox txtPassword = (TextBox)this.Controls.Find("TXTpsw", false)[0];
  976.  
  977. if (txtUserName != null && txtPassword != null)
  978. {
  979. this.stream.sendData(String.Format("200{0:00}{1}{2:00}{3}", txtUserName.Text.Length, txtUserName.Text, txtPassword.Text.Length, txtPassword.Text));
  980. }
  981. }
  982.  
  983. private void BTNLeave_Click(object sender, EventArgs args)
  984. {
  985. // executed when leave button clicked ( in room form )
  986. this.PBClose.Visible = true;
  987. }
  988.  
  989. private void BTNCloseRoom_Click(object sender, EventArgs args)
  990. {
  991. // executed when close room button clicked ( in room form )
  992. this.PBClose.Visible = true;
  993. }
  994.  
  995. private void BTNStartGame_Click(object sender, EventArgs args)
  996. {
  997. // executed when start game button clicked ( in room form )
  998. }
  999.  
  1000. /// <summary>
  1001. /// handles the messags from server
  1002. /// </summary>
  1003. private void handleMessage()
  1004. {
  1005. string msg = "";
  1006. while (true)
  1007. {
  1008. msg = this.stream.recvData(); // the message from the server
  1009.  
  1010. switch (int.Parse(msg.Substring(0, 3)))
  1011. {
  1012. case 102:
  1013. this.HandleSignIn(msg.Substring(3, 4)); // in case of sign in
  1014. break;
  1015. case 104:
  1016. this.HandleSignUp(msg.Substring(3, 4)); // in case of sign up
  1017. break;
  1018. case 124:
  1019. this.HandleBestScores(msg.Substring(3)); // in case of best scores
  1020. break;
  1021. case 126:
  1022. this.HandlePersonalStatus(msg.Substring(3)); // in case of personal status
  1023. break;
  1024. case 106:
  1025. this.HandleGetRooms(msg.Substring(3)); // in case of get rooms
  1026. break;
  1027. case 108:
  1028. this.HandleGetUsers(msg.Substring(3)); // in case of get users
  1029. break;
  1030. case 110:
  1031. this.HandleJoinRoon(msg.Substring(3)); // in case of join room
  1032. break;
  1033. }
  1034. }
  1035. }
  1036.  
  1037. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  1038. {
  1039. this.t.Abort();
  1040. this.stream.sendData("299");
  1041. this.stream.CloseStream();
  1042. this.PBClose.Dispose();
  1043. }
  1044.  
  1045. /// <summary>
  1046. /// handles sign in request
  1047. /// </summary>
  1048. /// <param name="msg"> the message from the server </param>
  1049. private void HandleSignIn(string msg)
  1050. {
  1051. switch (int.Parse(msg))
  1052. {
  1053. case 0:
  1054. uid = ((TextBox)this.Controls.Find("TXTusername", false).FirstOrDefault()).Text; // to show his username in main screen
  1055. Invoke((MethodInvoker)(() => CleanAllControlers(null, EventArgs.Empty)));
  1056. Invoke((MethodInvoker)(() => InitMainScreen(null, EventArgs.Empty)));
  1057. break;
  1058. case 1:
  1059. MessageBox.Show("Wrong Details");
  1060. break;
  1061. case 2:
  1062. MessageBox.Show("User is already connected");
  1063. break;
  1064. }
  1065. }
  1066.  
  1067. /// <summary>
  1068. /// handles sign up request
  1069. /// </summary>
  1070. /// <param name="msg"> message from the server </param>
  1071. private void HandleSignUp(string msg)
  1072. {
  1073. switch (int.Parse(msg))
  1074. {
  1075. case 0:
  1076. Invoke((MethodInvoker)(() => CleanAllControlers(null, EventArgs.Empty)));
  1077. Invoke((MethodInvoker)(() => this.InitLoginScreen(null, EventArgs.Empty)));
  1078. break;
  1079. case 1:
  1080. MessageBox.Show("Pass illegal");
  1081. break;
  1082. case 2:
  1083. MessageBox.Show("Username is already exists");
  1084. break;
  1085. case 3:
  1086. MessageBox.Show("Username is illegal");
  1087. break;
  1088. case 4:
  1089. MessageBox.Show("Other");
  1090. break;
  1091. }
  1092. }
  1093.  
  1094. /// <summary>
  1095. /// handles best scores request
  1096. /// </summary>
  1097. /// <param name="msg"> message from the server </param>
  1098. private void HandleBestScores(string msg)
  1099. {
  1100. List<string> elem = new List<string>();
  1101.  
  1102. int[] arr = new int[6] { 2, -6, 2, -6, 2, -6 };
  1103.  
  1104. Helper.convertBytes(arr, elem, msg);
  1105.  
  1106. KeyValuePair<string, string>[] arrPairs = new KeyValuePair<string, string>[3];
  1107.  
  1108. if (elem.Count > 5)
  1109. {
  1110. for (int i = 0; i < 3; i++)
  1111. {
  1112. arrPairs[i] = new KeyValuePair<string, string>(elem[i * 2], (int.Parse(elem[i * 2 + 1])).ToString());
  1113. }
  1114. }
  1115.  
  1116. Invoke((MethodInvoker)(() => this.changeBestScores(arrPairs)));
  1117. }
  1118.  
  1119. /// <summary>
  1120. /// handles personal status request
  1121. /// </summary>
  1122. /// <param name="msg"> message from the server </param>
  1123. private void HandlePersonalStatus(string msg)
  1124. {
  1125. List<string> elem = new List<string>();
  1126.  
  1127. int[] arr = new int[4] { -4, -6, -6, -4 };
  1128.  
  1129. Helper.convertBytes(arr, elem, msg);
  1130.  
  1131. this.ChangePersonalStatus(elem.ToArray());
  1132. }
  1133.  
  1134. private void HandleGetRooms(string msg)
  1135. {
  1136. List<string> elem = new List<string>();
  1137.  
  1138. int numberOfRooms = int.Parse(msg.Substring(0, 4));
  1139.  
  1140. int[] arr = new int[numberOfRooms * 2];
  1141.  
  1142. for (int i = 0; i < numberOfRooms; i++)
  1143. {
  1144. arr[i * 2] = -4;
  1145. arr[i * 2 + 1] = 2;
  1146. }
  1147.  
  1148. Helper.convertBytes(arr, elem, msg.Substring(4));
  1149.  
  1150. roomsID.Clear();
  1151. for (int i = 0; i < elem.Count; i += 2)
  1152. {
  1153. this.roomsID.Add(elem[i]);
  1154. elem.RemoveAt(i);
  1155. }
  1156.  
  1157. Invoke((MethodInvoker)(() => this.changeRoomsList(elem)));
  1158. }
  1159.  
  1160. private void HandleGetUsers(string msg)
  1161. {
  1162. if (msg == "0") // in case of the game already start
  1163. {
  1164. MessageBox.Show("The game already start, please refresh");
  1165. return;
  1166. }
  1167.  
  1168. List<string> elem = new List<string>();
  1169.  
  1170. int numberOfUsers = int.Parse(msg.Substring(0, 1));
  1171.  
  1172. int[] arr = new int[numberOfUsers];
  1173.  
  1174. for (int i = 0; i < numberOfUsers; i++)
  1175. {
  1176. arr[i] = 2;
  1177. }
  1178.  
  1179. Helper.convertBytes(arr, elem, msg.Substring(1));
  1180.  
  1181. if (this.Controls.Find("LBLparticipates", false).FirstOrDefault() as Label != null)
  1182. {
  1183. Invoke((MethodInvoker)(() => this.ChangeLBpart(elem)));
  1184. }
  1185. else
  1186. {
  1187. Invoke((MethodInvoker)(() => this.changeUsersInRoom(elem)));
  1188. }
  1189. }
  1190.  
  1191. private void HandleJoinRoon(string msg)
  1192. {
  1193. if (msg.Substring(0, 1) == "0")
  1194. {
  1195. Invoke((MethodInvoker)(() => this.CleanAllControlers(null, EventArgs.Empty)));
  1196. // i create new button so i can identify whether the page should be admin or user presentation
  1197. Invoke((MethodInvoker)(() => this.InitInsideRoom(new PictureBox(), EventArgs.Empty)));
  1198.  
  1199. List<string> elem = new List<string>();
  1200.  
  1201. int[] arr = new int[2] { -2, -2 };
  1202.  
  1203. Helper.convertBytes(arr, elem, msg.Substring(1));
  1204.  
  1205. Invoke((MethodInvoker)(() => this.ChangeLabels(elem)));
  1206. }
  1207. else if (msg.Substring(0, 1) == "1")
  1208. {
  1209. MessageBox.Show("failed - room is full");
  1210. }
  1211. else
  1212. {
  1213. MessageBox.Show("failed - room not exist or other reason");
  1214. }
  1215. }
  1216. }
  1217. }
  1218.  
  1219.  
  1220. // TODO:
  1221. // set font according to resulution
  1222. // remove the size of the Close button from the login and signup pages
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement