public async void CreateRoomAction() { string json = null; try { json = await RoomRequests.PostAsync(_client, _currentSession.GamerInfo, "create"); } catch (NullReferenceException) { Console.WriteLine("\nERROR:\tCheck your internet connection\n\n"); return; } catch (HttpRequestException) { Console.WriteLine("\nERROR:\tCheck your internet connection\n\n"); return; } catch (AggregateException) { Console.WriteLine("\nERROR:\tCheck your internet connection\n\n"); return; } if (json == null) { Console.WriteLine("\nERROR:\tCheck your internet connection\n\n"); return; } var id = JsonConvert.DeserializeObject <int>(json); Console.WriteLine($"\nRoom with id {id} has been created!"); Console.WriteLine("\nWaiting for opponent\n\n"); var result = (await GameRequests.GetGame(_client, id))?.ToArray(); if (result == null) { return; } var opponent1 = result .FirstOrDefault(x => !x.Equals(_currentSession.GamerInfo.UserName)); try { new GameLogic().StartGame(_client, _currentSession.GamerInfo.UserName, opponent1, id); await _client.DeleteAsync($"api/rooms/stop/{id}"); } catch (HttpRequestException) { Console.WriteLine("\nERROR:\tCheck your internet connection\n\n"); } catch (AggregateException) { Console.WriteLine("\nERROR:\tCheck your internet connection\n\n"); } }
public async void QuickSearch() { string json; try { json = await RoomRequests.PostAsync(_client, _currentSession.GamerInfo, "join"); } catch (System.NullReferenceException) { Console.WriteLine("\nERROR:\tCheck your internet connection\n\n"); return; } catch (HttpRequestException) { Console.WriteLine("\nERROR:\tCheck your internet connection\n\n"); return; } catch (AggregateException) { Console.WriteLine("\nERROR:\tCheck your internet connection\n\n"); return; } if (string.IsNullOrEmpty(json)) { Console.WriteLine("\nERROR:\tCheck your internet connection\n\n"); return; } Console.WriteLine("\nWaiting for opponent\n\n"); var id = JsonConvert.DeserializeObject <int>(json); var result = (await GameRequests.GetGame(_client, id))?.ToArray(); if (result == null) { return; } var opponent = result .FirstOrDefault(x => !x.Equals(_currentSession.GamerInfo.UserName)); try { new GameLogic().StartGame(_client, _currentSession.GamerInfo.UserName, opponent, id); await _client.DeleteAsync($"api/rooms/stop/{id}"); } catch (HttpRequestException) { Console.WriteLine("\nERROR:\tCheck your internet connection\n\n"); } catch (AggregateException) { Console.WriteLine("\nERROR:\tCheck your internet connection\n\n"); } }
public void SendRequest(GameRequests request, GameResponse response, Action <string> onSuccess, Action <ResponseCode> onFail, Message message = null, Servers server = Servers.Game) { if (_ws.isConnected) { _commandCode[0] = (byte)server; _commandCode[1] = (byte)request; _commandCode[2] = (byte)ResponseCode.Ok; if (message != null) { string serializedString = JsonWriter.Serialize(message); byte[] messageBuffer = Encoding.UTF8.GetBytes(serializedString); byte[] sendBuffer = new byte[messageBuffer.Length + 3]; Buffer.BlockCopy(_commandCode, 0, sendBuffer, 0, 3); Buffer.BlockCopy(messageBuffer, 0, sendBuffer, 3, messageBuffer.Length); _ws.Send(sendBuffer); this.Log("Send: server = " + server + ", request = " + request + ", message = " + serializedString); } else { _ws.Send(_commandCode); this.Log("Send: server = " + server + ", request = " + request); } Request newRequest = new Request { RequestType = request, OnSuccess = onSuccess, OnFail = onFail }; _waitingRequests.Add(response, newRequest); CountDownTimer timer = _timerProvider.Get(); timer.StartTimer(_responseTimeOut, null, () => OnTimeOut(response)); _timerRequest.Add(response, timer); } else { this.LogError("Connect before send!"); } }
public async void JoinRoomAction() { Console.Write("Enter the id of the desired room: "); if (!int.TryParse(Console.ReadLine(), out var id)) { Console.WriteLine("\nERROR:\tThe only numbers can be entered. Try again\n\n"); return; } else if (id < 1 || id > 1000) { Console.WriteLine("\nERROR:\tIncorrect number. Try again\n\n"); return; } if (RoomRequests.JoinAsync(_client, _currentSession.GamerInfo, id) == null) { return; } var result = (await GameRequests.GetGame(_client, id))?.ToArray(); if (result == null) { return; } var opponent2 = result .FirstOrDefault(x => !x.Equals(_currentSession.GamerInfo.UserName)); try { new GameLogic().StartGame(_client, _currentSession.GamerInfo.UserName, opponent2, id); } catch (HttpRequestException) { Console.WriteLine("\nERROR:\tCheck your internet connection\n\n"); } catch (AggregateException) { Console.WriteLine("\nERROR:\tCheck your internet connection\n\n");; } }
public void StartGame(HttpClient client, string userName, string opponentName, int roomId) { var roundId = 0; while (true) { GamerInfo gamer = new GamerInfo() { UserName = userName }; roundId++; var stopwatch = new Stopwatch(); stopwatch.Start(); PrintPreview(roomId, roundId, userName, opponentName); PostGamerAction(client, userName, roomId, gamer); var roundResult = GetRoundResult(client, userName, roomId, gamer); gamer.OnlineTime += stopwatch.Elapsed; if (roundResult != RoundResult.None) SaveGamerInfo(client, userName, gamer); int num; Console.WriteLine("1.\tContinue"); Console.WriteLine("2.\tExit"); Console.WriteLine("Do you want to continue?"); while (true) { Console.Write("Enter the number: "); if (!int.TryParse(Console.ReadLine(), out num)) Console.WriteLine("The only numbers can be entered. Try again"); else if (num < 1 || num > 3) Console.WriteLine("Incorrect number. Try again"); else break; } Console.WriteLine(); if (num == 2) { var r = client.DeleteAsync($"api/round/{roomId}"); break; } var content = new StringContent(JsonConvert.SerializeObject(userName), Encoding.UTF8, "application/json"); client.PostAsync($"api/round/{roomId}", content); var response = GameRequests.RequestWithTimer(client, $"api/round/{roomId}", 10); if (response.StatusCode != HttpStatusCode.OK) { Console.WriteLine("Unfortunately the second player don`t accept invite!\n\n"); client.DeleteAsync($"api/round/{roomId}"); break; } var json = response.Content.ReadAsStringAsync().Result; var result = JsonConvert.DeserializeObject<bool>(json); if (!result) { Console.WriteLine("Unfortunately the second player don`t accept invite!\n\n"); client.DeleteAsync($"api/round/{roomId}"); break; } } }
protected IEnumerable <Data.Models.GameRequest> GetGameRequests(DateTime date) { return(GameRequests.Where(g => g.Date.Date == date.Date)); }