Example #1
0
        /// <summary>
        /// Returns <see cref="ApiResponse"/> with string representation of <see cref="MatchJson"/> in body.
        /// </summary>
        /// <param name="endpoint"></param>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        public ApiResponse GetMatch(string endpoint, DateTime timestamp)
        {
            using (var context = new GameStatsDbDataContext())
            {
                var matchRecord = (from server in context.Servers
                                   where server.endpoint == endpoint
                                   join match in context.Matches
                                   on server.id equals match.server_id
                                   where match.timestamp == timestamp
                                   select match)
                                  .FirstOrDefault();
                if (matchRecord == null)
                {
                    return(new ApiResponse(HttpStatusCode.NotFound));
                }

                var scoreboardRecords = (from playerInMatch in context.PlayersInMatches
                                         where playerInMatch.match_id == matchRecord.id
                                         orderby playerInMatch.player_rank
                                         select playerInMatch);

                List <ScoreboardRecordJson> _scoreboard =
                    scoreboardRecords
                    .Select(record => new ScoreboardRecordJson
                {
                    Deaths = record.deaths,
                    Frags  = record.frags,
                    Kills  = record.kills,
                    Name   = (from player in context.Players
                              where player.id == record.player_id
                              select player.name)
                             .First()
                }).ToList();

                MatchJson _match = new MatchJson
                {
                    FragLimit   = matchRecord.frag_limit,
                    Scoreboard  = _scoreboard,
                    TimeElapsed = matchRecord.time_elapsed,
                    TimeLimit   = matchRecord.time_limit,
                    GameMode    = (from gm in context.GameModes
                                   where gm.id == matchRecord.gm_id
                                   select gm.name)
                                  .First(),
                    Map = (from map in context.Maps
                           where map.id == matchRecord.map_id
                           select map.name)
                          .First()
                };
                return(new ApiResponse(JsonConvert.SerializeObject(_match, Formatting.Indented)));
            }
        }
Example #2
0
            public override bool Equals(object obj)
            {
                if (obj == null)
                {
                    return(false);
                }
                MatchJson objAsMatchJson = obj as MatchJson;

                if (objAsMatchJson == null)
                {
                    return(false);
                }
                return(this.Map == objAsMatchJson.Map &&
                       this.GameMode == objAsMatchJson.GameMode &&
                       this.FragLimit == objAsMatchJson.FragLimit &&
                       this.TimeLimit == objAsMatchJson.TimeLimit &&
                       this.TimeElapsed == objAsMatchJson.TimeElapsed &&
                       this.Scoreboard.SequenceEqual(objAsMatchJson.Scoreboard));
            }
Example #3
0
        public ApiResponse PutMatch(string endpoint, MatchJson match, DateTime timestamp)
        {
            using (var context = new GameStatsDbDataContext())
            {
                var server = context.Servers.FirstOrDefault(s => s.endpoint == endpoint);
                if (server == null)
                {
                    // there was not advertise-request with such endpoint
                    return(new ApiResponse(endpoint + " server is not announced.", HttpStatusCode.BadRequest));
                }

                // get players' ids to use it later
                List <int> playersIds = match.Scoreboard.Select(pl => addPlayer(pl.Name, context)).ToList();

                // insert new match

                // check game mode
                // find its id by name
                int gm_id;
                var gm = context.GameModes
                         .FirstOrDefault(gm_ => gm_.name == match.GameMode);
                if (gm == null)
                {
                    return(new ApiResponse("Unknown game mode: " + match.GameMode, HttpStatusCode.BadRequest));
                }
                else
                {
                    gm_id = gm.id;
                    // does the sever support this game mode?
                    bool isBadGm = (from gmOnServer in context.GameModesOnServers
                                    where gmOnServer.gm_id == gm_id && gmOnServer.server_id == server.id
                                    select gm_id)
                                   .Count() == 0;
                    if (isBadGm)
                    {
                        return(new ApiResponse("Game mode " + match.GameMode + " is not supported on server: " + endpoint,
                                               HttpStatusCode.BadRequest));
                    }
                }
                Matches newMatch = new Matches
                {
                    server_id     = server.id,
                    timestamp     = timestamp,
                    map_id        = addMap(match.Map, context),
                    gm_id         = gm_id,
                    frag_limit    = match.FragLimit,
                    time_limit    = match.TimeLimit,
                    time_elapsed  = match.TimeElapsed,
                    players_count = playersIds.Count(),
                    winner_id     = playersIds.First()
                };
                // save
                context.Matches.InsertOnSubmit(newMatch);
                context.SubmitChanges();

                // insert scoreboard
                int matchId = newMatch.id;
                int rank    = 1;
                var newScoreboardRecords =
                    match.Scoreboard
                    .Zip(playersIds,
                         (player, playerId) => new PlayersInMatches
                {
                    match_id    = matchId,
                    player_id   = playerId,
                    player_rank = rank++,
                    frags       = player.Frags,
                    kills       = player.Kills,
                    deaths      = player.Deaths
                });
                // save
                context.PlayersInMatches.InsertAllOnSubmit(newScoreboardRecords);
                context.SubmitChanges();

                return(new ApiResponse());
            }
        }