Example #1
0
        private async Task SaveMatchState()
        {
            var localSettings = ApplicationData.Current.LocalSettings;

            vmMatchState _State = new vmMatchState();

            await TennisMatch.Save("tennis_pebble.xml");

            _State.Fill(TennisMatch);
            await Tennis_Statistics.Helpers.LocalStorage.Save(_State.Serialize(), "tennismatchstate.json", false);

            localSettings.Values[Constants.TennisState] = "1";

            await SendPebbleTennisScore(_State, TennisMatch);
        }
Example #2
0
        /// <summary>
        /// Send Tennis score to Pebble Time
        /// </summary>
        /// <param name="_State"></param>
        /// <returns></returns>
        private async Task SendPebbleTennisScore(vmMatchState _State, vmMatch _Match)
        {
            String GameScore;
            String SetScore;
            String Sets   = "";
            String Status = "1";

            if (_State.Server == 0 || _State.CurrentSetScore.IsTiebreak)
            {
                GameScore = String.Format("{0} - {1}", _State.ScorePlayer1, _State.ScorePlayer2);
            }
            else
            {
                GameScore = String.Format("{0} - {1}", _State.ScorePlayer2, _State.ScorePlayer1);
            }

            SetScore = String.Format("S:{0}-{1} G:{2}-{3}",
                                     _State.TotalSets.Score1, _State.TotalSets.Score2,
                                     _State.CurrentSetScore.Score1, _State.CurrentSetScore.Score2);

            if (_State.Winner == 1)
            {
                GameScore = "WON";
            }
            if (_State.Winner == 2)
            {
                GameScore = "LOST";
            }
            if (_State.Winner != 0)
            {
                Status   = "2";
                Sets     = _Match.Match.PrintableScore().Replace(", ", ";") + ";";
                SetScore = "Completed";
            }

            //Send message
            if (_pc.IsConnected)
            {
                await _pc.Pebble.SendTennisMessage(GameScore, SetScore, Sets, Status);
            }

            //Write debug line
            System.Diagnostics.Debug.WriteLine(String.Format("Tennis score sent: {0}, {1}, {2}, {3}", GameScore, SetScore, Sets, Status));
        }
Example #3
0
        /// <summary>
        /// Tennis handler
        /// </summary>
        private async Task TennisHandler()
        {
            if (!PebbleConnector.IsBackgroundTaskRunningStatusSet(PebbleConnector.Initiator.Tennis))
            {
                return;
            }

            //initialise settings
            var localSettings   = ApplicationData.Current.LocalSettings;
            var roamingSettings = ApplicationData.Current.RoamingSettings;

            if (localSettings.Values.Keys.Contains(Constants.BackgroundTennis) &&
                (bool)localSettings.Values[Constants.BackgroundTennis])
            {
                //Initiate tennis match
                if (TennisMatch == null)
                {
                    TennisMatch = new vmMatch();
                    await TennisMatch.Load("tennis_pebble.xml");

                    if (TennisMatch.Match == null)
                    {
                        System.Diagnostics.Debug.WriteLine("Initialize Tennis Match in background.");
                        vmNewMatch _vmNewMatch = await vmNewMatch.Load();

                        TennisMatch = new vmMatch();
                        TennisMatch.Start(_vmNewMatch);
                    }
                    else
                    {
                        TennisMatch.NewPoint();
                    }

                    vmMatchState _State = new vmMatchState();
                    _State.Fill(TennisMatch);
                    await Tennis_Statistics.Helpers.LocalStorage.Save(_State.Serialize(), "tennismatchstate.json", false);

                    localSettings.Values[Constants.TennisState] = "1";

                    System.Diagnostics.Debug.WriteLine("Tennis Match state stored");

                    _pc.Pebble._protocol.MessageReceived += this.TennisMessageReceived;

                    Guid TennisWatchApp = Guid.Parse(Constants.TennisAppGuid);
                    await _pc.Launch(TennisWatchApp);

                    await SendPebbleTennisScore(_State, TennisMatch);


                    // await SendPebbleTennisScore(_State);
                }
            }

            //A new state requested
            if (localSettings.Values.Keys.Contains(Constants.TennisState) &&
                (string)localSettings.Values[Constants.TennisState] == "2")
            {
                await SaveMatchState();
            }

            //Check for and process command
            if (localSettings.Values.Keys.Contains(Constants.TennisCommand) &&
                TennisMatch != null)
            {
                String Command = (String)localSettings.Values[Constants.TennisCommand];
                System.Diagnostics.Debug.WriteLine("Processing tennis command: " + Command);

                switch (Command)
                {
                case "switch":

                    TennisMatch.cmdSwitchServer.Execute(null);

                    AddToLog("Tennis: switch command");

                    break;

                case "stop":

                    TennisMatch.Terminate();

                    PebbleConnector.ClearBackgroundTaskRunningStatus(PebbleConnector.Initiator.Tennis);

                    AddToLog("Tennis: stop command");

                    break;

                case "suspend":

                    TennisMatch.Pause();

                    PebbleConnector.ClearBackgroundTaskRunningStatus(PebbleConnector.Initiator.Tennis);

                    AddToLog("Tennis: suspend command");

                    break;

                case "extend":

                    TennisMatch.cmdExtendMatch.Execute(null);

                    AddToLog("Tennis: extend command");

                    break;
                }

                localSettings.Values.Remove(Constants.TennisCommand);

                await SaveMatchState();
            }
        }
        /// <summary>
        /// Handler performede every second
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void _Timer_Tick(object sender, object e)
        {
            try
            {
                var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;

                //If new state is present; update window
                if (localSettings.Values.Keys.Contains(Constants.TennisState) &&
                    (string)localSettings.Values[Constants.TennisState] == "1")
                {
                    //Process the match state
                    String JSON = await Tennis_Statistics.Helpers.LocalStorage.Load("tennismatchstate.json");

                    if (JSON.Length > 0)
                    {
                        vmMatchState _newState = vmMatchState.Deserialize(JSON);
                        _newState.StatisticsCollection.Initialize();

                        _vmBinder.Tennis.vmMatch.Fill(_newState);

                        if (localSettings.Values.Keys.Contains(Constants.BackgroundCommunicatieIsRunning))
                        {
                            if ((bool)localSettings.Values[Constants.BackgroundCommunicatieIsRunning])
                            {
                                //_vmBinder.Tennis.vmMatch.Paused = !_vmBinder.Tennis.vmMatch.InProgress;
                            }
                            else
                            {
                                _vmBinder.Tennis.vmMatch.Paused = true;
                            }
                        }
                        else
                        {
                            _vmBinder.Tennis.vmMatch.Paused = true;
                        }


                        localSettings.Values.Remove(Constants.TennisState);

                        _vmBinder.Tennis.NotifyVisibility();

                        //Show the match page
                        ShowPage(MatchGrid);
                    }
                }

                //Check if error occurred
                if (localSettings.Values.Keys.Contains(Constants.BackgroundCommunicatieError) &&
                    (int)localSettings.Values[Constants.BackgroundCommunicatieError] == (int)BCState.ConnectionFailed)
                {
                    HandleConnectionFailed();

                    localSettings.Values[Constants.BackgroundCommunicatieError] = (int)BCState.OK;

                    _vmBinder.Tennis.vmMatch.Paused = true;
                }

                //Check if background is running -> Paused = false
                if (localSettings.Values.Keys.Contains(Constants.BackgroundCommunicatieIsRunning) &&
                    (bool)localSettings.Values[Constants.BackgroundCommunicatieIsRunning])
                {
                    _vmBinder.Tennis.vmMatch.Paused = false;
                }

                //If background task is running and ring is still visible; request for new state
                if (localSettings.Values.Keys.Contains(Constants.BackgroundCommunicatieIsRunning) &&
                    !(bool)localSettings.Values[Constants.BackgroundCommunicatieIsRunning] &&
                    ProgressRing.Visibility == Visibility.Visible)
                {
                    localSettings.Values[Constants.TennisState] = "2";
                }
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine("_Timer_Tick exception: " + exp.Message);
            }
        }