public Game() { InitializeComponent(); Initialisator.startInit(myPanel, roolsPanel, complete, onlineUsers, waiters, chat, message, send, password_tb, timerLabel, priority); if (File.Exists("cookie.bin")) { StreamReader reader = new StreamReader("cookie.bin"); login_tb.Text = reader.ReadLine(); userInfo[0] = login_tb.Text; password_tb.Text = reader.ReadLine(); userInfo[1] = password_tb.Text; reader.Dispose(); } fieldPaper = new int[10, 10]; /*ContextMenuStrip contextMenuOnlineUsers = new ContextMenuStrip(); * contextMenuOnlineUsers.Opening += contextMenuOnlineUsers_Opening; * ToolStripMenuItem offerToPlay = new ToolStripMenuItem("Предложить сыграть"); * offerToPlay.Click += offerToPlay_Click; * offerToPlay.BackColor = Color.Black; * offerToPlay.ForeColor = Color.White; * contextMenuOnlineUsers.Items.Add(offerToPlay); * waiters.ContextMenuStrip = contextMenuOnlineUsers;*/ }
private void complete_Click(object sender, EventArgs e) { ShipsObserver shipsObserver = new ShipsObserver(fieldPaper); try { shipsObserver.observe(); roolsPanel.Visible = false; field.Enabled = false; waitTimer = new Timer(); waitTimer.Interval = 1000; waitTimer.Tick += waitTimer_Tick; waitPanel = Initialisator.waitPanelInit(); this.Controls.Add(waitPanel); waitTimer.Start(); } catch (Exception exp) { MessageBox.Show(exp.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void play_Click(object sender, EventArgs e) { UserInformationComparator uifConparator = new UserInformationComparator(login_tb.Text, password_tb.Text); try { uifConparator.compare(); } catch (Exception exp) { issueLabel.Text = exp.Message; issueLabel.Visible = true; } userInfo = Connection.signup(login_tb.Text, password_tb.Text); if (userInfo[0].CompareTo(login_tb.Text) != 0) { issueLabel.Text = userInfo[0]; issueLabel.Visible = true; } else { if (remember.Checked) { StreamWriter writer = new StreamWriter("cookie.bin"); writer.WriteLine(login_tb.Text); writer.WriteLine(password_tb.Text); writer.Dispose(); } userLogin.Text = "Пользователь: " + userInfo[0]; infoCountOfWins.Text = "Побед: " + userInfo[1]; Initialisator.initAfterEntering(myPanel, roolsPanel, onlineUsers, waiters, chat, message, send); this.Controls.Remove(enter_panel); generalTimer = new Timer(); generalTimer.Interval = 2000; generalTimer.Tick += generalTimer_Tick; generalTimer.Start(); } }
void waitTimer_Tick(object sender, EventArgs e) { //долбим сервер в ожидании противника string response = Connection.setUserWait(userInfo[0]); if (response.CompareTo("OK") != 0 && response.CompareTo("AGAIN") != 0 && response.CompareTo("ERROR") != 0) { rivalInfo = new string[2]; dynamic jsonObj = JObject.Parse(response); if (jsonObj.firstPlayer == null) { rivalInfo[0] = jsonObj.secondPlayer; rivalInfo[1] = Initialisator.getCountOfWinsIntoJArray(online_users, (string)jsonObj.secondPlayer); stepPriority = true; } else { rivalInfo[0] = jsonObj.firstPlayer; rivalInfo[1] = rivalCountOfWins.Text += Initialisator.getCountOfWinsIntoJArray(online_users, (string)jsonObj.firstPlayer); stepPriority = false; } this.Controls.Remove(waitPanel); Initialisator.rivalPanelInit(rivalPanel, rivalField, rivalLogin, rivalCountOfWins); Initialisator.priorityLabelInit(priority, stepPriority); this.Controls.Add(rivalPanel); timerLabel.Visible = true; rivalField.Paint += field_Paint; rivalField.MouseClick += rivalField_MouseClick; rivalField.Enabled = stepPriority; gameId = jsonObj.id; gameTimer = new Timer(); gameTimer.Interval = 1000; gameTimer.Tick += gameTimer_Tick; gameTimer.Start(); waitTimer.Stop(); } }
void gameTimer_Tick(object sender, EventArgs e) { if (timer > 0) { if (!stepPriority) //если мы походили { if (myStep != null) //если мы только что походили { int status = Connection.getStepStatus(stepNumber, gameId); //узнаем статус нашего попадания if (status != 0) { rivalFieldPaper[myStep[0], myStep[1]] = status; if (status == 2) //если мы не попали { rivalField.Enabled = false; //даём походить противнику } int countOfKilledShips = Connection.getCountOfKilledShips(userInfo[0], gameId); if (countOfKilledShips == countOfCubs) // если мы выйграли { Connection.setUserWin(userInfo[0]); MessageBox.Show("Поздравляем! Вы выйграли!", "Победа", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); this.Controls.Remove(rivalPanel); roolsPanel.Visible = true; fieldPaper = new int[10, 10]; field.Refresh(); userInfo[1] = (int.Parse(userInfo[1]) + 1).ToString(); infoCountOfWins.Text = "Побед: " + userInfo[1]; gameTimer.Stop(); timerLabel.Visible = false; priority.Visible = false; } else { stepPriority = true; //ходим снова Initialisator.priorityLabelInit(priority, stepPriority); stepNumber++; timer = 60; myStep = null; rivalField.Refresh(); } } } else // если была не наша очередь и мы ждем хода противника { int[] rivalStep = Connection.getLastStep(stepNumber, gameId); if (rivalStep != null) { if (fieldPaper[rivalStep[0], rivalStep[1]] == 1) //если у нас тут корабль { Connection.setStepStatus(stepNumber, gameId, 3); //сообщить противнику, что он попал fieldPaper[rivalStep[0], rivalStep[1]] = 3; } else { Connection.setStepStatus(stepNumber, gameId, 2);//сообщить противнику, что он промазал fieldPaper[rivalStep[0], rivalStep[1]] = 2; rivalField.Enabled = true; stepPriority = true; Initialisator.priorityLabelInit(priority, stepPriority); } int countOfKilledShips = Connection.getCountOfKilledShips(rivalInfo[0], gameId); if (countOfKilledShips == countOfCubs) // если противник выйграл { Connection.setUserLouse(userInfo[0]); MessageBox.Show("Увы! Вы проиграли!", "Поражение", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); this.Controls.Remove(rivalPanel); roolsPanel.Visible = true; fieldPaper = new int[10, 10]; field.Refresh(); userInfo[1] = (int.Parse(userInfo[1]) - 1).ToString(); infoCountOfWins.Text = "Побед: " + userInfo[1]; gameTimer.Stop(); timerLabel.Visible = false; priority.Visible = false; } else { stepNumber++; timer = 60; field.Refresh(); } } } } } else { if (stepPriority)//если мы не походили { Connection.setUserLouse(userInfo[0]); MessageBox.Show("Нужно следить за временем!", "Техническое поражение", MessageBoxButtons.OK, MessageBoxIcon.Information); } else // если не походил противник, тут 2 случая: или противник просто замешкался, либо он отключился.... { string timediff = Connection.getLastActivity(rivalInfo[0], DateTime.Now.ToString("HH:mm:ss")); string[] timearray = timediff.Split(':'); if (int.Parse(timearray[0]) > 0 || int.Parse(timearray[1]) > 0 || int.Parse(timearray[2]) > 30) { Connection.setUserLouse(userInfo[0]); } //если юзер вылетел, то поражение сам себе он уже не поставит,... делаем это за него Connection.setUserWin(userInfo[0]); MessageBox.Show("Вы выйграли!", "Техническая победа", MessageBoxButtons.OK, MessageBoxIcon.Information); } this.Controls.Remove(rivalPanel); roolsPanel.Visible = true; fieldPaper = new int[10, 10]; field.Refresh(); userInfo[1] = (int.Parse(userInfo[1]) - 1).ToString(); infoCountOfWins.Text = "Побед: " + userInfo[1]; gameTimer.Stop(); timerLabel.Visible = false; priority.Visible = false; } timer--; timerLabel.Text = "00:" + timer; }