Exemple #1
0
        public async static Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "put", Route = "pong")] HttpRequestMessage req, TraceWriter log)
        {
            log.Info("Pong recieved from device.");

            HttpResponseMessage rtnResponse = null;

            try
            {
                // Get request body, should contain deviceId and accessToken
                bpPong pongData = await req.Content.ReadAsAsync <bpPong>();

                log.Info($"Pong received from device {pongData.deviceId}.");

                GameStateManager gameState = new GameStateManager(); // get game state
                if (gameState.IsRunning)                             // if game isn't running
                {
                    if (pongData.success == false)                   // last device failed to respond in time, remove it
                    {
                        gameState.RemoveDevice(pongData.deviceId);
                    }

                    // select next device to ping and prepare response (-1 if this is the last device, aka the winner)
                    bpDevice randomDevice = gameState.GetRandomDevice();
                    log.Info($"Sending initial ping to device {randomDevice.deviceId}.");
                    SendToDevice.Ping(randomDevice, (gameState.DeviceCount > 1 ? 5000 : -1));
                    if (gameState.DeviceCount <= 1) // game is over, stop game
                    {
                        gameState.StopGame();
                    }
                }

                // respond to request
                rtnResponse = req.CreateResponse(HttpStatusCode.OK);
            }
            catch (RuntimeBinderException ex)
            {
                //  "name property doesn't exist in request body
                rtnResponse = req.CreateResponse(HttpStatusCode.BadRequest, @"Please pass the device ID and status of the 'ping' in the body of your request: { ""deviceID"" : ""value"", ""status"" : true|false }");
            }

            return(rtnResponse);
        }
Exemple #2
0
        public async static Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "put", Route = "game")] HttpRequestMessage req, TraceWriter log)
        {
            log.Info("StartGame received a request.");

            HttpResponseMessage rtnResponse = req.CreateResponse(HttpStatusCode.OK);

            // we'll only do this if we're not already doing start notifications
            if (notificationTask == null || notificationTask.Status != TaskStatus.Running)
            {
                GameStateManager gameState = new GameStateManager(); // get game state
                if (!gameState.IsRunning)                            // if game isn't running
                {
                    List <bpDevice> deviceList = gameState.GetDeviceList();
                    // if we have more than 1 device
                    if (deviceList.Count > 1)
                    {
                        gameState.StartGame();

                        // start device notification background task
                        notificationTask = new Task(() =>
                        {
                            log.Info("StartGame: Starting device notification.");

                            // get list of devices

                            pingTasks = new List <Task>();
                            foreach (bpDevice device in deviceList)
                            {
                                pingTasks.Add(Task.Run(() =>
                                {
                                    log.Info($"StartGame: Notifying device {device.deviceId} of start.");
                                    SendToDevice.Start(device);
                                }));
                            }
                            Task.WaitAll(pingTasks.ToArray());

                            Thread.Sleep(3000); // wait three seconds after all pings are complete

                            bpDevice randomDevice = gameState.GetRandomDevice();
                            log.Info($"StartGame: Sending initial ping to device {randomDevice.deviceId}.");
                            SendToDevice.Ping(randomDevice, 5000);
                        });

                        // spin off a thread to send the pings in the background so we can return immediately to caller
                        notificationTask.Start();
                    }
                    else
                    {
                        log.Info($"StartGame: Only one device. Don't you have any friends?");
                    }
                }
                else
                {
                    log.Info("StartGame: Game already running. So I'm going to ignore this.");
                }
            }
            else
            {
                log.Info("StartGame: Notifications currently running. So I'm going to ignore this.");
            }

            return(rtnResponse);
        }