Imports System.ComponentModel
Imports Common
Imports CommonDataLayer
Imports CommonUserInterface
Public Class GenerateGameTask
Implements ITask
Private _BackgroundWorker As BackgroundWorker = New BackgroundWorker
' Some stats for the generation stats
Private _TotalRecords As Integer = 0
Private _UpdateFactor As Integer = 0
Private _GeneratedRecords As Integer = 0
Public Sub New()
_BackgroundWorker.WorkerReportsProgress = True
' Hook up the background worker to the work method
AddHandler _BackgroundWorker.DoWork, AddressOf DoWork
AddHandler _BackgroundWorker.RunWorkerCompleted, AddressOf WorkDone
AddHandler _BackgroundWorker.ProgressChanged, AddressOf ProgressChanged
End Sub
#Region "ITask Interface"
Public Event Progress(ByVal pMessage As String, ByVal pProgress As Integer) Implements ITask.Progress
Public Event Done() Implements ITask.Done
Public Function StartWork() As Boolean Implements ITask.StartWork
If Not _BackgroundWorker.IsBusy Then
_BackgroundWorker.RunWorkerAsync()
End If
End Function
#End Region
Private Function DoWork() As Boolean
' Hook up to the table index events
AddHandler TableIndex.Instance.Progress, AddressOf HandleTableIndexProgress
' Make sure the game tables are initialised
TableIndex.Instance.InitialiseGame()
' The work goes here
CreateGame()
ProgressChanged(Me, New ProgressChangedEventArgs(100, "Game generation complete"))
Return True
End Function
Private Function HandleTableIndexProgress(ByVal sender As Object, ByVal e As TableIndexEventArgs) As Boolean
RaiseEvent Progress(e.Message, e.Progress)
End Function
Private Function ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) As Boolean
RaiseEvent Progress(e.UserState, e.ProgressPercentage)
End Function
Private Function WorkDone(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) As Boolean
RaiseEvent Done()
End Function
#Region "Generation Routines"
Private Function CreateGame() As Boolean
' Set up some geneation parameters
Dim generationParameters As GenerationParameters = New GenerationParameters()
' Calculate the predicted totals for the generation
_TotalRecords = TableIndex.Instance.OrganisationTable.TotalRanksPerWeightClass * TableIndex.Instance.WeightClassTable.Count
' Generate the regional and continental organisations
GenerateOrganisations()
' Generate the boxers in the game
GenerateBoxerPool()
Return True
End Function
''' <summary>
''' This will count the number of records as they are generated.
''' For every 100th of the overall progress, the progress bar will be updated.
''' </summary>
''' <param name="pMessage">The given message</param>
Private Function UpdateProgress(ByVal pMessage As String) As Boolean
_GeneratedRecords += 1
_UpdateFactor += 1
If _UpdateFactor >= (_TotalRecords / 100) Then
_UpdateFactor = 0
ProgressChanged(Me, New ProgressChangedEventArgs((_GeneratedRecords / _TotalRecords * 100), pMessage))
End If
End Function
Private Function GenerateOrganisations() As Boolean
' Generate an organisation for each region
For Each country As Country In TableIndex.Instance.CountryTable
' Id of zero is being used for an unknown country
If country.Id = 0 Then
Continue For
End If
UpdateProgress("Generating organisations")
TableIndex.Instance.OrganisationTable.Add(New Organisation(country.Name, OrganisationTypeEnum.Professional, OrganisationLevelEnum.National, country.Id, 30))
Next
For Each continent As Continent In TableIndex.Instance.ContinentTable
' Id of zero is being used for an unknown continent
If continent.Id = 0 Then
Continue For
End If
UpdateProgress("Generating organisations")
TableIndex.Instance.OrganisationTable.Add(New Organisation(continent.Name, OrganisationTypeEnum.Professional, OrganisationLevelEnum.Continental, continent.Id, 60))
Next
End Function
Private Function GenerateBoxerPool() As Boolean
Dim generationParameters As GenerationParameters = New GenerationParameters()
UpdateProgress("Generating characters")
' Generate boxers for each national organisation
For Each organisation As Organisation In TableIndex.Instance.OrganisationTable
' Ignore continental and international organisations
If organisation.OrganisationLevelId <> OrganisationLevelEnum.National Then
Continue For
End If
' Generate boxers for each weight class
For Each weightClass As WeightClass In TableIndex.Instance.WeightClassTable
' Generate one boxer per rank
For count = 1 To organisation.RanksPerWeightClass
' Preset the generation parameters
generationParameters.PresetBoxer(RandomRoll.Roll(0, (GenerationBoxerPresetEnum.Count - 1)))
' Make sure boxer attributes are generated
generationParameters.SetParameter(New GenerationParameter(Of Boolean)(GenerationParameterNameEnum.GenerateAttributes, True))
' Assign the country and weight class
generationParameters.SetParameter(New GenerationParameter(Of Country)(GenerationParameterNameEnum.AssignCountry, TableIndex.Instance.CountryTable.Row(organisation.ParentId)))
generationParameters.SetParameter(New GenerationParameter(Of WeightClass)(GenerationParameterNameEnum.AssignWeightClass, weightClass))
Dim newCharacter As BoxerCharacter = New BoxerCharacter()
TableIndex.Instance.CharacterTable.Add(newCharacter)
newCharacter.Generate(generationParameters)
Next
Next
Next
End Function
#End Region
End Class