# Live scoring Improvements
## How live scoring works
The live scoring system is connected to a real time database and has a listener for any updates. Each event contains its type and the message to display.
![[Pasted image 20231121075035.png]]
The event type provides basic info to the timeline on how to display the event. The "yellowCard" type displays a yellow card on the timeline etc. The whistle event also controls the timer for the app and contains three extra fields of information to handle the timer.
![[Pasted image 20231121080324.png]]
The whistle field sets the status of the timer, if the whistle is start then the timer is set to continue counting down. Stop stops the countdown.
The time field contains the current time of the match in seconds.
The timestamp contains the accurate time of when the whistle was blown.
When the match is processed the most recent whistle event is parsed to the timer handler, the timer is set to the time value and the countdown status set accordingly. However, this will only provide an accurate timer for users who had the live scoring open at the time of the update event.
![[Pasted image 20231121082324.png]]
The match is processed: 1) when the match is first loaded 2) when an update is sent. So, if a user opens a match 10 seconds after a whistle is blown they will see a 10 second delay on their timer until the next whistle update. On the next update they will see the timer unnaturally jump to its new setting. The time stamp is used to deal with this. It takes the time difference between the current time and the event time and removes the difference from the timer. This provides all users with an accurate timer regardless of when they began to follow the match.
![[Pasted image 20231121082858.png]]
## Timer going negative
- Timer going negative
- Timer starting unprompted
- Yellow cards not displaying
Despite the timer having a negative check it was still often going negative at the end of a chukker. This mainly happened after closing the app and reopening it at the end of a chukker. Let's explore:
- Happening on close/reopen of app
- Bypassing the negative check
After some debugging the problem becomes clear. As the app re focuses it calls a function to sort the timer, calculating the time delta and setting the timer. This is because temporarily closing the app will pause the timer causing it to unsync.
This function calls a secondary time handling function which circumvents the negative check. I redirected this to call the primary function
## Timer starting unprompted
After about a 2 minutes of the timer being stopped it will restart itself.
This is because of a poll timer that defaults to starting the timer if it fails. I removed the default
## Yellow cards not displaying
Yellow cards not displaying on the timeline.
Part API issue and part App issue. API was not handling the timer value in the card event. App was expecting fields that card events don't have.
# Sound of the week
Cub - Wunderhorse