Exemplo n.º 1
0
        public static async Task <SignalRConnectionInfo> GetSignalRInfo(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = Constants.Version + "/multiplayerconnect")] HttpRequest req,
            [SignalRConnectionInfo(HubName = "multiplayer")] SignalRConnectionInfo connectionInfo)
        {
            var counter = await StatsFunctions.GetDayCounter();

            if (counter.Messages > Constants.DAILY_MSGS_MAX)
            {
                return(null);
            }
            else
            {
                return(connectionInfo);
            }
        }
Exemplo n.º 2
0
        public static async Task SendMessage(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = Constants.Version + "/sendgamemessage")] HttpRequest req,
            [SignalR(HubName = "multiplayer")] IAsyncCollector <SignalRMessage> signalRMessages, ILogger log)
        {
            //Read query info
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var    message     = JsonConvert.DeserializeObject <GameMessage>(requestBody);

            await signalRMessages.AddAsync(
                new SignalRMessage
            {
                Target    = message.Type,
                Arguments = new[] { message }
            });

            await StatsFunctions.IncrementDayCounter(1);
        }
Exemplo n.º 3
0
        public static async Task <IActionResult> TokBlitzRaceGameOver(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = Constants.Version + "/tokblitzracegameover")] HttpRequest req,
            [SignalR(HubName = "multiplayer")] IAsyncCollector <SignalRMessage> signalRMessages, ILogger log)
        {
            //Read query info
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var    info        = JsonConvert.DeserializeObject <TokBlitzRace>(requestBody);

            bool forfeit = false;

            if (info.ForfeitingPlayer != null)
            {
                info.Loser    = info.ForfeitingPlayer;
                info.LoserId  = info.ForfeiterId;
                info.Winner   = (info.ForfeitingPlayer.PlayerNumber == 1) ? info.Players[0]: info.Players[1];
                info.WinnerId = info.Winner.ConnectionId;

                forfeit = true;
            }
            else
            {
                if (info.FinishedFirst.PlayerNumber == 1)
                {
                    info.Player1Points += Constants.FINISHED_FIRST_BONUS;
                    if (info.Player1PointsPerRound != null)
                    {
                        info.Player1PointsPerRound.Add(Constants.FINISHED_FIRST_BONUS);
                    }
                }
                else if (info.FinishedFirst.PlayerNumber == 2)
                {
                    info.Player2Points += Constants.FINISHED_FIRST_BONUS;
                    if (info.Player2PointsPerRound != null)
                    {
                        info.Player2PointsPerRound.Add(Constants.FINISHED_FIRST_BONUS);
                    }
                }

                info.Winner   = (info.Player1Points > info.Player2Points) ? info.Players[0] : info.Players[1];
                info.WinnerId = info.Winner.ConnectionId;

                info.Loser   = (info.Player1Points > info.Player2Points) ? info.Players[1] : info.Players[0];
                info.LoserId = info.Loser.ConnectionId;
                if (info.Player1Points == info.Player2Points)
                {
                    info.Winner = new GamePlayer()
                    {
                        PlayerNumber = 0, UserId = "Tie", UserName = "******", UserPhoto = "Tie"
                    }
                }
                ;
            }

            // update the points and others

            tokblitzTeamClass getwinner = new tokblitzTeamClass();

            getwinner.Id          = info.Winner.ConnectionId;
            getwinner.GamesPlayed = 1;
            getwinner.Wins        = 1;
            getwinner.TeamPoints  = 2;


            tokblitzTeamClass getloser = new tokblitzTeamClass();

            getloser.Id          = info.Loser.ConnectionId;
            getloser.GamesPlayed = 1;
            getloser.Losses      = 1;
            getloser.TeamPoints  = (forfeit == true) ? 0 : 1;

            tokblitzTeamClass getwinnerOld = await Api <tokblitzTeamClass> .GetItemAsync(getwinner.Id, Constants.PkRequest(getwinner.Id));

            tokblitzTeamClass getwinnerTosend = TeamClassUpdater <tokblitzTeamClass> .TeamUpdater(getwinner, getwinnerOld);

            tokblitzTeamClass getLoserOld = await Api <tokblitzTeamClass> .GetItemAsync(getloser.Id, Constants.PkRequest(getloser.Id));

            tokblitzTeamClass getLoserTosend = TeamClassUpdater <tokblitzTeamClass> .TeamUpdater(getloser, getLoserOld);


            await Api <tokblitzTeamClass> .UpdateItemAsyncSaveGames(getwinner.Id, getwinnerTosend, Constants.PkRequest(getwinner.Id));

            await Api <tokblitzTeamClass> .UpdateItemAsyncSaveGames(getloser.Id, getLoserTosend, Constants.PkRequest(getloser.Id));


            // creates a rematch record after the game
            RematchRecord rematchRecord = new RematchRecord();
            Guid          newId;

            newId            = Guid.NewGuid();
            rematchRecord.Id = newId.ToString();
            rematchRecord.Pk = newId.ToString();
            rematchRecord.Player1ConnectionId = info.Players[0].ConnectionId.ToString();
            rematchRecord.Player2ConnectionId = info.Players[1].ConnectionId.ToString();
            rematchRecord.Player1Id           = info.Players[0].UserId.ToString();
            rematchRecord.Player2Id           = info.Players[1].UserId.ToString();

            await signalRMessages.AddAsync(
                new SignalRMessage
            {
                Target    = "tokblitzracegameover",
                Arguments = new[] { info }
            });


            await StatsFunctions.IncrementDayCounter(1);


            await Api <TokBlitzRace> .CreateItemAsync(info, Constants.PkRequest(info.Id));

            await Api <RematchRecord> .CreateItemAsync(rematchRecord, Constants.PkRequest(rematchRecord.Id));

            await signalRMessages.AddAsync(
                new SignalRMessage
            {
                Target    = "rematch_record",
                Arguments = new[] { rematchRecord }
            });



            return(new OkObjectResult(info));
        }