<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ClourWars.Web.Code;
using CloudWars.Common;
using CloudWars.SpaceBattle;
using CloudWars.Common.Other;
using ClourWars.Web.ServerClientEntities;
using Microsoft.Security.Application;
using CloudWars.Web.Code;
using Needletail.Mvc;
using Needletail.Mvc.Communications;

namespace ClourWars.Game.Controllers
{
    public class GameController : Controller
    {
        
        public ActionResult Play(Guid matchId)
        {
            if (!ProfileHelper.UserHasProfile(ProfileHelper.GetIdentity))
            {
                return RedirectToAction("CreateProfile", "Player");
            }

            ViewBag.MatchId = matchId;
            return PartialView();
        }


        [Authorize]
        public ActionResult OnlinePlayers()
        {
            if (!ProfileHelper.UserHasProfile(ProfileHelper.GetIdentity))
            {
                return RedirectToAction("CreateProfile", "Player");
            }
            return PartialView(GameHelper.GetOnlinePlayers(ProfileHelper.GetIdentity));
        }

        [Authorize]
        public ActionResult GetNearByPlayers()
        {
            if (!ProfileHelper.UserHasProfile(ProfileHelper.GetIdentity))
            {
                return RedirectToAction("CreateProfile", "Player");
            }
            return PartialView(GameHelper.GetNearbyPlayers(ProfileHelper.GetIdentity));
        }


        [Authorize]
        public ActionResult GetAllPlayers()
        {
            if (!ProfileHelper.UserHasProfile(ProfileHelper.GetIdentity))
            {
                return RedirectToAction("CreateProfile", "Player");
            }
            return PartialView(GameHelper.GetOnlinePlayers(ProfileHelper.GetIdentity));
        }


        [Authorize]
        [HttpPost]
        public ActionResult MyChallenges()
        {
            if (!ProfileHelper.UserHasProfile(ProfileHelper.GetIdentity))
            {
                return RedirectToAction("CreateProfile", "Player");
            }
            return PartialView(GameHelper.GetMyChallenges(ProfileHelper.GetIdentity));
        }

        
        [Authorize]
        [HttpPost]
        public ActionResult MyMatches()
        {
            if (!ProfileHelper.UserHasProfile(ProfileHelper.GetIdentity))
            {
                return RedirectToAction("CreateProfile", "Player");
            }
            return PartialView(GameHelper.GetMyMatches(ProfileHelper.GetIdentity));
        }


        #region Game play code, these methods are called from the game


        //use DI for this or resolve this using web.config
        IFactory factory = new SpaceBattleFactory();


        /// <summary>
        /// Challenge a player
        /// </summary>
        [HttpPost]
        public TwoWayResult ChallengePlayer(Guid playerId)
        {
            string clientId = factory.GetPlayerPresence().GetClientId(playerId);
            Guid fromId = factory.GetPlayerPresence().GetPlayerId(Context.ConnectionId);
            factory.GetGame().ChallengePlayer(fromId, playerId);
            //inform the player that has a new challenge
            //Clients[clientId].newChallenge();
            dynamic call = new ClientCall { CallerId = Context.ConnectionId, ClientId = clientId };
            call.game.newChallenge(); 
            return new TwoWayResult(call);
        }

        /// <summary>
        /// Initialize a player in the server
        /// </summary>
        /// <param name="liveId">id of the player</param>
        /// <param name="latitude">latitude position</param>
        /// <param name="longitude">longitude position</param>
        [HttpPost]
        public void Initialize(string liveId, string latitude, string longitude)
        {
            factory.GetPlayerPresence().PlayerIsOnLine(liveId, Context.ConnectionId, double.Parse(latitude), double.Parse(longitude));
        }

        /// <summary>
        /// The challenge was accepted
        /// </summary>
        [HttpPost]
        public TwoWayResult AcceptChallenge(Guid challengeId)
        {
            //get the challenge
            var match = factory.GetGame().AcceptChallenge(challengeId);
            //inform the challenger that the challenge was accepted
            string clientId = factory.GetPlayerPresence().GetClientId(match.Item2);
            
            if (!string.IsNullOrWhiteSpace(clientId))
            {
                //Clients[clientId].challengeAccepted(match.Item1);
                //The client accepted the challenge                
                dynamic call1 = new ClientCall { CallerId = Context.ConnectionId, ClientId = clientId };
                call1.game.challengeAccepted(match.Item1);<commit_msg>small fix<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ClourWars.Web.Code;
using CloudWars.Common;
using CloudWars.SpaceBattle;
using CloudWars.Common.Other;
using ClourWars.Web.ServerClientEntities;
using Microsoft.Security.Application;
using CloudWars.Web.Code;
using Needletail.Mvc;
using Needletail.Mvc.Communications;

namespace ClourWars.Game.Controllers
{
    public class GameController : Controller
    {
        
        public ActionResult Play(Guid matchId)
        {
            if (!ProfileHelper.UserHasProfile(ProfileHelper.GetIdentity))
            {
                return RedirectToAction("CreateProfile", "Player");
            }

            ViewBag.MatchId = matchId;
            return PartialView();
        }


        [Authorize]
        public ActionResult OnlinePlayers()
        {
            if (!ProfileHelper.UserHasProfile(ProfileHelper.GetIdentity))
            {
                return RedirectToAction("CreateProfile", "Player");
            }
            return PartialView(GameHelper.GetOnlinePlayers(ProfileHelper.GetIdentity));
        }

        [Authorize]
        public ActionResult GetNearByPlayers()
        {
            if (!ProfileHelper.UserHasProfile(ProfileHelper.GetIdentity))
            {
                return RedirectToAction("CreateProfile", "Player");
            }
            return PartialView(GameHelper.GetNearbyPlayers(ProfileHelper.GetIdentity));
        }


        [Authorize]
        public ActionResult GetAllPlayers()
        {
            if (!ProfileHelper.UserHasProfile(ProfileHelper.GetIdentity))
            {
                return RedirectToAction("CreateProfile", "Player");
            }
            return PartialView(GameHelper.GetOnlinePlayers(ProfileHelper.GetIdentity));
        }


        [Authorize]
        [HttpPost]
        public ActionResult MyChallenges()
        {
            if (!ProfileHelper.UserHasProfile(ProfileHelper.GetIdentity))
            {
                return RedirectToAction("CreateProfile", "Player");
            }
            return PartialView(GameHelper.GetMyChallenges(ProfileHelper.GetIdentity));
        }

        
        [Authorize]
        [HttpPost]
        public ActionResult MyMatches()
        {
            if (!ProfileHelper.UserHasProfile(ProfileHelper.GetIdentity))
            {
                return RedirectToAction("CreateProfile", "Player");
            }
            return PartialView(GameHelper.GetMyMatches(ProfileHelper.GetIdentity));
        }


        #region Game play code, these methods are called from the game


        //use DI for this or resolve this using web.config
        IFactory factory = new SpaceBattleFactory();


        /// <summary>
        /// Challenge a player
        /// </summary>
        [HttpPost]
        public TwoWayResult ChallengePlayer(Guid playerId)
        {
            string clientId = factory.GetPlayerPresence().GetClientId(playerId);
            Guid fromId = factory.GetPlayerPresence().GetPlayerId(Context.ConnectionId);
            factory.GetGame().ChallengePlayer(fromId, playerId);
            //inform the player that has a new challenge
            //Clients[clientId].newChallenge();
            dynamic call = new ClientCall { CallerId = Context.ConnectionId, ClientId = clientId };
            call.game.newChallenge(); 
            return new TwoWayResult(call);
        }

        /// <summary>
        /// Initialize a player in the server
        /// </summary>
        /// <param name="liveId">id of the player</param>
        /// <param name="latitude">latitude position</param>
        /// <param name="longitude">longitude position</param>
        [HttpPost]
        public void Initialize(string liveId, string latitude, string longitude)
        {
            latitude = string.IsNullOrWhiteSpace(latitude) ? "1" : latitude;
            longitude = string.IsNullOrWhiteSpace(longitude) ? "1" : longitude;
            factory.GetPlayerPresence().PlayerIsOnLine(liveId, Context.ConnectionId, double.Parse(latitude), double.Parse(longitude));
        }

        /// <summary>
        /// The challenge was accepted
        /// </summary>
        [HttpPost]
        public TwoWayResult AcceptChallenge(Guid challengeId)
        {
            //get the challenge
            var match = factory.GetGame().AcceptChallenge(challengeId);
            //inform the challenger that the challenge was accepted
            string clientId = factory.GetPlayerPresence().GetClientId(match.Item2);
            
            if (!string.IsNullOrWhiteSpace(clientId))
            {
                //Clients[clientId].challengeAccepted(match.Item1);
                //The client accepted the challenge                
                dynamic call1 = new ClientCall { CallerId = Context.ConnectionId, ClientId = clientId };
                call1.game.challengeAccepted(match.Item1);
{'fraction_non_alphanumeric': 0.09384284397090462, 'fraction_numerical': 0.001229382235426698, 'mean_word_length': 2.093155893536122, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3423607', 'n_tokens_mistral': 2488, 'n_tokens_neox': 2385, 'n_words': 496}