public void PostMove(MoveRequest request, int gameId, int matchId) { string requestJSON = JsonSerializer.SerializeToJSON<MoveRequest>(request); var requestConfig = new ServerRequestConfig(); requestConfig.Url = string.Format("{0}/play.php", ConfigurationManager.AppSettings["CentralServerUrl"]); requestConfig.RequestData = requestJSON; requestConfig.GameId = gameId; requestConfig.MatchId = matchId; requestConfig.ResponseAction = new Action<string, int>(PostMoveCompleted); this.PerformServerRequest(requestConfig); }
private void PerformServerRequest(ServerRequestConfig config) { BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += (sender, args) => { var workerConfig = (ServerRequestConfig)args.Argument; //"http://centralserver.codeketeers.com/ServerPairing.php?challenge=James&from=Anthony" JavaScriptSerializer dataSerailizer = new JavaScriptSerializer(); string queryString = string.Join("&", dataSerailizer.Deserialize<Dictionary<string, string>>(config.RequestData).Where(kv => !string.IsNullOrEmpty(kv.Value)).Select(kv => string.Format("{0}={1}", kv.Key, HttpUtility.UrlEncode(kv.Value != null ? kv.Value : string.Empty)))); string fullUrl = string.Format("{0}?{1}", config.Url, queryString); Logger.Instance.Log("ServerRequest", string.Format("GameId:{0}|MatchId:{1}|Request:{2}", workerConfig.GameId, workerConfig.MatchId, fullUrl), JsonSerializer.SerializeToJSON(workerConfig)); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fullUrl); request.ContentLength = 0; var httpResponse = (HttpWebResponse)request.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var result = streamReader.ReadToEnd(); Logger.Instance.Log("ServerResponse", string.Format("GameId:{0}|MatchId:{1}|Request:{2}", workerConfig.GameId, workerConfig.MatchId, fullUrl), result); workerConfig.ResponseAction(result, workerConfig.MatchId); } }; worker.RunWorkerAsync(config); worker.RunWorkerCompleted += (cs, ce) => { if (ce.Error != null) { Exception ex = ce.Error; while (ex.InnerException != null) { ex = ex.InnerException; } Logger.Instance.Log("CentralServerCommunicationError", string.Format("GameId:{0}|MatchId:{1}|Error:{2}",config.GameId,config.MatchId, ex.Message), ce.Error.StackTrace); using (IGameDataService dataService = new GameDataService()) { dataService.EndGame(config.GameId, null); dataService.Save(); Match match = dataService.GetMatch(config.MatchId, null); CentralServerSession session = dataService.GetCentralServerSession(null, null, config.GameId); Player tttdPlayer = dataService.GetPlayer(match.PlayerOneId); MoveRequest challengeRequest = new MoveRequest(); challengeRequest.GameId = session.CentralServerGameId.Value; challengeRequest.PlayerName = tttdPlayer.PlayerName; challengeRequest.X = 0; challengeRequest.Y = 0; challengeRequest.Flags = CentralServerCommunicationChannel.GetStatus(StatusFlag.ChallengeMove); CentralServerCommunicationChannel.Instance.PostMove(challengeRequest, match.CurrentGameId.Value, match.MatchId); } } }; }
void ICommunicationChannel.ChallengePlayer(int matchId) { using (IGameDataService dataService = new GameDataService()) { //We need to create a session even if the challenge isn't accepted. Match match = dataService.GetMatch(matchId, null); dataService.CreateCentralServerSession(match.CurrentGameId.Value); Player player = dataService.GetPlayer(match.PlayerOneId); Player opponent = dataService.GetPlayer(match.PlayerTwoId); Game game = dataService.GetGame(match.CurrentGameId.Value); var requestData = new ChallengeRequest(); requestData.PlayerName = player.PlayerName; requestData.OpponentName = opponent.PlayerName; string requestJSON = JsonSerializer.SerializeToJSON<ChallengeRequest>(requestData); var requestConfig = new ServerRequestConfig(); requestConfig.Url = string.Format("{0}/ServerPairing.php", ConfigurationManager.AppSettings["CentralServerUrl"]); requestConfig.RequestData = requestJSON; requestConfig.GameId = game.GameId; requestConfig.MatchId = game.MatchId; requestConfig.ResponseAction = new Action<string, int>(ChallengePlayerCompleted); this.PerformServerRequest(requestConfig); } }