private void ProcessStartResponse(string responseBody) { int dynamiteCount = 0; int pointToWin = 0; int maxRounds = 0; string opponentName = string.Empty; string[] parameters = responseBody.Split(new char[] { '&' }); foreach (var parameter in parameters) { if (parameter.Contains('=')) { string paramName = parameter.Split(new char[] { '=' })[0]; string paramValue = parameter.Split(new char[] { '=' })[1]; switch (paramName.ToLower()) { case "dynamitecount": { dynamiteCount = int.Parse(paramValue); break; } case "pointstowin": { pointToWin = int.Parse(paramValue); break; } case "maxrounds": { maxRounds = int.Parse(paramValue); break; } case "opponentname": { opponentName = paramValue; } break; } } } Console.WriteLine(string.Format("START Opponentname={0} Pointstowin={1} Maxrounds={2} Dynamitecount={3}", opponentName, pointToWin, maxRounds, dynamiteCount)); BotAIClass.SetStartValues(opponentName, pointToWin, maxRounds, dynamiteCount); }
private void Process(HttpListenerContext context) { string body = string.Empty; StreamReader sr = new StreamReader(context.Request.InputStream); using (sr) { body = sr.ReadToEnd(); } switch (context.Request.Url.AbsolutePath.Replace("/", "")) { case "start": { ProcessStartResponse(body); context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.ContentType = "text/plain"; StreamWriter sw = new StreamWriter(context.Response.OutputStream); using (sw) { sw.WriteLine(context.Request.RawUrl); } break; } case "move": { if (context.Request.HttpMethod.ToLower() == "get") { StreamWriter sww = new StreamWriter(context.Response.OutputStream); string responsestr = BotAIClass.GetMove(); Console.WriteLine(string.Format("My move {0}", responsestr)); context.Response.ContentLength64 = responsestr.Length; sww.Write(responsestr); sww.Flush(); sww.Close(); context.Response.OutputStream.Close(); context.Response.Close(); break; } else { BotAIClass.SetLastOpponentsMove(body); Console.WriteLine(string.Format("Their move {0}", body)); context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.ContentType = "text/plain"; StreamWriter sw = new StreamWriter(context.Response.OutputStream); using (sw) { sw.WriteLine(context.Request.RawUrl); } } break; } break; } }