Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server

HoopDream

5.00/5 (4 votes)
17 Nov 2018CPOL3 min read 9K   134  
A brief article on how to utilize NBA Stats API in a WinForm application

Image 1

Introduction

Got involved with this project because I play ESPN/Yahoo Fantasy Sports. This tiny little application allows me to use math to calculate better Fantasy Pre-Draft order, roster lineup, and player scouting.

Technology Used

 

Background

The first thing you should know about this project is that there are multiple documents available for the Python client of NBA statistics located at stats.nba.com. If you want to know more about work you check those out. Second, this work is solely dedicated to retrieve, store, view NBA data. There is no attempt here to provide for updating or editing this data to build your team.

If you're anything like me you have several teams active per year in any number of available leagues. With so many teams active, pretty much every Fantasy team will have completely full starting lineups for each day of the season. The problem is deciding which player to start and which player to bench for the best particular matchups.

Choosing which player to start and which player to bench is never going to be an exact science. However, using skills of building great technology we can utilize tools to better help us to decide.

Particular for this project I am using a formula to try and come up with the best players. The formula used here attempts to Calculate Standard Deviation for each player. This helps me to determine which player will probably give me the best matchup results.

VB
Private Shared Function CalculateStandardDeviation ...
     Dim average As Decimal = values.Average()

     Dim sum As Decimal = CDec(values.Sum(Function(d)
            Math.Pow(CDbl(d) - CDbl(average), 2)))

     Dim result As Decimal =  _
            CDec(Math.Sqrt((CDbl(sum) / (values.Count() - 1))))

     Return IIf(result > 0, result, 1)

 End Function

Workflow

The overall step-by-step process for the application is simple.

  1. Download player stats
  2. Save the player stats
  3. Calculate Player ranking
  4. Display the ranking on screen
  5. Refresh player stats (repeat 1-4)

Download

Newtonsoft.Json is used to download the json dataset. To accomplish this a custom WebService has been implemented. The webservice simply sends a static query to the NBA Stats web service. It recieves and downloads the json dataset which is then translated into .NET class for easy data storage and retrival.

VB
Friend Class WebService

    Private Shared ReadOnly httpClient As New HttpClient()

    Public Shared Sub Start()
        Store.Instance.CreateDatabaseObjects()

        httpClient.DefaultRequestHeaders.Accept.Clear()
        httpClient.DefaultRequestHeaders.Accept.Add( _
                New MediaTypeWithQualityHeaderValue("application/json"))

        DownloadPage().Wait()
    End Sub

    ...
End Class

Storage

Storage was made easier by utilizing the Arkitech Platform Framework (APF) The Arkitech Platform Framework (APF) is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write. APF is a software development framework that helps you build a maintainable business logic layer when buiding Windows Forms (WinForms) Microsoft SQL Server (MSSQL) data-oriented applications.

Database Schema

Using the APF allowed me to focus on designing simple POCO classes for easy data storage.

 

VB
 Public Class Player
     Inherits Entity


     Private _Reports As New List(Of Report)

     Private _Rosters As New List(Of Roster)


     <column(isprimarykey:=true, _="" isdbgenerated:="True)" playerid="" property="" public=""> _
     Public Property IsPicked As Boolean

     <column()>
     Public Overrides Property Name As String
</column()></column(isprimarykey:=true,...)

Summary

Here we bring together information to help guide Fantasy player research and lineup optimization. The power is now in your hands to go off and become the next leaque leader of your NBA Fantasy Leagues.

Points of Interest

Is there an NBA API for free that has live stats?
Yes. For a list of all the json files that NBA releases for each day: Visit data.nba.net [^]

History

Version 0.0.0.1

References

Certain ideas and code examples for this article were sampled using code from one or more of the code samples below.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)