Beispiel #1
0
        protected object JSONGetTeam(string data, Dictionary <string, object> sessionData)
        {
            Team team = null;

            if (Controller.Tournament != null)
            {
                string             id         = data;
                RoundRobinTeamData rrTeamData = Controller.Tournament.getTeamDataById(id);
                if (rrTeamData != null)
                {
                    team = rrTeamData.Team;
                }
            }
            TeamWebData teamData = null;

            if (team != null)
            {
                teamData = new TeamWebData(team);
            }

            return(teamData);
        }
Beispiel #2
0
        protected object JSONSetGameResult(string data, Dictionary <string, object> sessionData)
        {
            Dictionary <string, object> resultData;

            if (data == null)
            {
                throw new HttpException(400, "Bad Request");
            }
            try
            {
                resultData = (Dictionary <string, object>)jsonSerializer.DeserializeObject(data);
            }
            catch
            {
                throw new HttpException(400, "Bad Request");
            }
            if (resultData == null || !resultData.ContainsKey("GameId") || !(resultData["GameId"] is int))
            {
                throw new HttpException(400, "Bad Request");
            }
            int id = (int)resultData["GameId"];

            if (Controller.Tournament != null && Controller.Tournament.GamesById != null)
            {
                Game game = null;
                if (Controller.Tournament.GamesById.ContainsKey(id))
                {
                    game = Controller.Tournament.GamesById[id];
                }

                if (game != null)
                {
                    if (game.ScoreKeeper == null || game.ScoreKeeper.AssociatedAccessCode != getAccessCode(sessionData))
                    {
                        throw new HttpException(403, "Forbidden");
                    }

                    bool shouldTriggerGameResultChanged = false;
                    try
                    {
                        if (game.IsConfirmed)
                        {
                            game.IsConfirmed = false;
                            shouldTriggerGameResultChanged = true;
                        }
                        if (resultData.ContainsKey("TeamGameResults"))
                        {
                            #region Team game results
                            object teamGameResults = resultData["TeamGameResults"];
                            if (teamGameResults != null)
                            {
                                if (teamGameResults is Dictionary <string, object> )
                                {
                                    foreach (KeyValuePair <string, object> pair in (Dictionary <string, object>)teamGameResults)
                                    {
                                        if (!(pair.Value is Dictionary <string, object>))
                                        {
                                            throw new HttpException(400, "Bad Request");
                                        }

                                        Dictionary <string, object> teamGameResultData = (Dictionary <string, object>)pair.Value;
                                        string teamId = pair.Key;
                                        if (!game.TeamGameResults.ContainsKey(teamId))
                                        {
                                            throw new HttpException(404, "Not Found");
                                        }

                                        TeamGameResult teamGameResult = game.TeamGameResults[teamId];
                                        if (teamGameResult == null)
                                        {
                                            throw new HttpException(404, "Not Found");
                                        }


                                        if (teamGameResultData.ContainsKey("NumPoints"))
                                        {
                                            if (teamGameResultData["NumPoints"] != null)
                                            {
                                                if (teamGameResultData["NumPoints"] is int)
                                                {
                                                    int numPoints = (int)teamGameResultData["NumPoints"];
                                                    if (teamGameResult.NumPoints != numPoints)
                                                    {
                                                        teamGameResult.NumPoints       = numPoints;
                                                        shouldTriggerGameResultChanged = true;
                                                    }
                                                }
                                                else
                                                {
                                                    throw new HttpException(400, "Bad Request");
                                                }
                                            }
                                        }

                                        if (teamGameResultData.ContainsKey("NumFouls"))
                                        {
                                            if (teamGameResultData["NumFouls"] != null)
                                            {
                                                if (teamGameResultData["NumFouls"] is int)
                                                {
                                                    int numFouls = (int)teamGameResultData["NumFouls"];
                                                    if (teamGameResult.NumFouls != numFouls)
                                                    {
                                                        teamGameResult.NumFouls        = numFouls;
                                                        shouldTriggerGameResultChanged = true;
                                                    }
                                                }
                                                else
                                                {
                                                    throw new HttpException(400, "Bad Request");
                                                }
                                            }
                                        }

                                        if (teamGameResultData.ContainsKey("WonGame"))
                                        {
                                            if (teamGameResultData["WonGame"] != null)
                                            {
                                                if (teamGameResultData["WonGame"] is bool)
                                                {
                                                    bool wonGame = (bool)teamGameResultData["WonGame"];
                                                    if (teamGameResult.WonGame != wonGame)
                                                    {
                                                        teamGameResult.WonGame         = wonGame;
                                                        shouldTriggerGameResultChanged = true;
                                                    }
                                                }
                                                else
                                                {
                                                    throw new HttpException(400, "Bad Request");
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    throw new HttpException(400, "Bad Request");
                                }
                            }
                            #endregion
                        }

                        if (resultData.ContainsKey("WinningTeam"))
                        {
                            object winningTeam = resultData["WinningTeam"];
                            if (winningTeam != null)
                            {
                                RoundRobinTeamData teamData = Controller.Tournament.getTeamDataById(winningTeam.ToString().Trim());
                                if (teamData != null)
                                {
                                    if (game.WinningTeam != teamData.Team)
                                    {
                                        game.WinningTeam = teamData.Team;
                                        shouldTriggerGameResultChanged = true;
                                    }
                                }
                                else
                                {
                                    throw new HttpException(404, "Not Found");
                                }
                            }
                        }
                    }
                    finally
                    {
                        if (shouldTriggerGameResultChanged)
                        {
                            Controller.TriggerGameResultChanged(game);
                        }
                    }
                }
                else
                {
                    throw new HttpException(404, "Not Found");
                }
            }
            else
            {
                throw new HttpException(404, "Not Found");
            }

            return(null);
        }