public JsonMajorCollege getScoreThroughYears(string collegeId, string majorId) { List <JsonScore> jsonScores = new List <JsonScore>(); double[] years = _majorCollegeRepository.GetPastYearsTrainData(collegeId, majorId); foreach (var y in years) { JsonScore jc = new JsonScore(); jc.year = Convert.ToInt32(y); MajorCollege majorCollegeScore = this._majorCollegeRepository.GetMajorCollegesByCollegeCodeAndMajorAndYear(collegeId, majorId, y).Result; jc.groupCode = majorCollegeScore.groupCode; jc.score = majorCollegeScore.score; jsonScores.Add(jc); } var collegeName = this._collegeRepository.findByCode(collegeId).Result.name; var majorName = this._majorRepository.findByCode(majorId).Result.name; JsonMajorCollege jsonMajorCollege = new JsonMajorCollege(); jsonMajorCollege.collegeCode = collegeId; jsonMajorCollege.collegeName = collegeName; jsonMajorCollege.majorCode = majorId; jsonMajorCollege.majorName = majorName; jsonMajorCollege.scores = jsonScores; return(jsonMajorCollege); }
public static void RegisterRoute(ServerTcpListener server) { server.EndPointApi.RegisterEndPoint("GET", "^/score$", (IRequestContext httpRequest) => { try { NpgsqlConnection conn = DbHelper.ConnectObj(); conn.Open(); string querystringUnionAllCards = @$ "select win,tie,lose,elo,LoginName from Scoreboard join users on scoreboard.LoginName_fk = users.LoginName order by elo desc,win desc,tie desc,lose asc"; using (NpgsqlCommand getScorebaord = new NpgsqlCommand(querystringUnionAllCards, conn)) { NpgsqlDataReader readergetScorebaord = getScorebaord.ExecuteReader(); List <JsonScore> Scoreboard = new List <JsonScore>(); while (readergetScorebaord.Read()) { var Score = new JsonScore(); Score.Win = readergetScorebaord[0].ToString(); Score.Tie = readergetScorebaord[1].ToString(); Score.Lose = readergetScorebaord[2].ToString(); Score.Elo = readergetScorebaord[3].ToString(); Score.LoginName = readergetScorebaord[4].ToString(); if ((Int32.Parse(Score.Lose) == 0 && Int32.Parse(Score.Tie) == 0)) { if (Int32.Parse(Score.Win) > 0) { Score.WLTratio = 1; } else { Score.WLTratio = 0; } } else { Score.WLTratio = (Int32.Parse(Score.Win) / (Int32.Parse(Score.Lose) + Int32.Parse(Score.Tie))); } Scoreboard.Add(Score); } httpRequest.ReponseHandler.SendDefaultMessage(httpRequest.Stream, "200", JsonConvert.SerializeObject(Scoreboard, Formatting.Indented)); conn.Close(); return(200); } } catch { httpRequest.ReponseHandler.SendDefaultStatus(httpRequest.Stream, "400"); return(400); } }); }
public void ScoreModelUpdateRankingTest() { IScoreModel model = new ScoreModel(); List <string> list = new List <string> { "19 MARK", "51 LUKE", "23 JOHN" }; IScore <string, int> newScore = new JsonScore("MICHAEL", 45); List <string> newList = new List <string> { "51 LUKE", "45 MICHAEL", "23 JOHN" }; Assert.Equal(newList, model.UpdateRanking(list, newScore)); list.Clear(); newList.Clear(); newScore = new JsonScore("MICHAEL", 55); list.Add("19 MARK"); list.Add("52 LUKE"); list.Add("27 JOHN"); newList.Add("55 MICHAEL"); newList.Add("52 LUKE"); newList.Add("27 JOHN"); Assert.Equal(model.UpdateRanking(list, newScore), newList); list.Clear(); newList.Clear(); newScore = new JsonScore("GUNTHER", 20); list.Add("21 JOEY"); list.Add("25 ROSS"); list.Add("30 CHANDLER"); newList.Add("30 CHANDLER"); newList.Add("25 ROSS"); newList.Add("21 JOEY"); Assert.Equal(model.UpdateRanking(list, newScore), newList); }
public void JSonScoreBuilderTest() { IScore <string, int> score = new JsonScore("Phoebe", 300); IScore <string, int> score1 = new JsonScoreBuilder() .NameFromString("Phoebe") .Score(300) .Build(); Assert.Equal(score, score1); IScore <String, int> score2 = new JsonScoreBuilder() .NameFromString("PHOEBE") .Score(300) .Build(); Assert.NotEqual(score, score2); IScore <String, int> score3 = new JsonScoreBuilder() .NameFromString("John") .Score(300) .Build(); Assert.NotEqual(score, score3); }
public static void RegisterRoute(ServerTcpListener server) { server.EndPointApi.RegisterEndPoint("GET", "^/stats$", (IRequestContext httpRequest) => { try { httpRequest.Headers.TryGetValue("Authorization", out string token); if (!Regex.IsMatch(token, "^Basic (.*)-mtcgToken$")) { httpRequest.ReponseHandler.SendDefaultStatus(httpRequest.Stream, "400"); return(400); } string querystring = @$ "select LoginName from users where LoginName='{Regex.Match(token, " ^ Basic(.*) - mtcgToken$ ").Groups[1].Value}'"; NpgsqlConnection conn = DbHelper.ConnectObj(); conn.Open(); using (NpgsqlCommand command = new NpgsqlCommand(querystring, conn)) { NpgsqlDataReader reader = command.ExecuteReader(); if (reader.HasRows == false) { httpRequest.ReponseHandler.SendDefaultStatus(httpRequest.Stream, "401"); conn.Close(); return(401); } reader.Read(); string UserID = reader[0].ToString(); reader.Close(); string querystringUnionAllCards = @$ "select win,tie,lose,elo from Scoreboard where LoginName_fk = '{UserID}'"; using (NpgsqlCommand getStats = new NpgsqlCommand(querystringUnionAllCards, conn)) { NpgsqlDataReader readergetStats = getStats.ExecuteReader(); if (readergetStats.HasRows == false) { httpRequest.ReponseHandler.SendDefaultStatus(httpRequest.Stream, "404"); return(404); } readergetStats.Read(); var Score = new JsonScore(); Score.Win = readergetStats[0].ToString(); Score.Tie = readergetStats[1].ToString(); Score.Lose = readergetStats[2].ToString(); Score.Elo = readergetStats[3].ToString(); Score.LoginName = UserID; if ((Int32.Parse(Score.Lose) == 0 && Int32.Parse(Score.Tie) == 0)) { if (Int32.Parse(Score.Win) > 0) { Score.WLTratio = 1; } else { Score.WLTratio = 0; } } else { Score.WLTratio = (Int32.Parse(Score.Win) / (Int32.Parse(Score.Lose) + Int32.Parse(Score.Tie))); } httpRequest.ReponseHandler.SendDefaultMessage(httpRequest.Stream, "200", JsonConvert.SerializeObject(Score, Formatting.Indented)); conn.Close(); return(200); } } }