views:

56

answers:

1

I call the following function with

Call GameTimer(FormatDate(objLiveCommentary("DateFirstStarted"), "WithTime"), 
               FormatDate(objLiveCommentary("DateSecondStarted"), "WithTime"), 
               "Soccer")

And it prints results as 23, 35, 64, 90. I want to take this result and store it as

CurrentGameTime = 

because I will save CurrentGameTime to my database. How can I do it?

Function GameTimer (FirstStarted, SecondStarted, GameType)

If GameType =   "Soccer"    Then
    DateFirstStarted    = DateDiff("n", FirstStarted, FormatDate(NOW(), "WithTime"))
    DateSecondStarted   = DateDiff("n", SecondStarted, FormatDate(NOW(), "WithTime"))

    If DateFirstStarted <= 45 Then
    Response.Write DateFirstStarted
    ElseIf DateFirstStarted <= 45 Then
    DateFirstStarted
    ElseIf DateSecondStarted <= 45 Then
    Response.Write DateSecondStarted + 45
    ElseIf DateFirstStarted <= 45 Then
    DateFirstStarted
    Else 
    Response.Write "90"
    End If  
End If

End Function
+1  A: 

To return the result of your Function you need to set the Functions return value:

Function GameTimer(FirstStarted, SecondStarted, GameType)
    ...
    GameTimer = 90
End Function

Your GameTimer Function should look something like this:

Function GameTimer(FirstStarted, SecondStarted, GameType)
    Dim result
    result = 90

    If GameType = "Soccer" Then
        DateFirstStarted = DateDiff("n", FirstStarted, FormatDateTime(Now(), 0))
        DateSecondStarted = DateDiff("n", SecondStarted, FormatDateTime(Now(), 0))

        If DateFirstStarted <= 45 Then
            result = DateFirstStarted
        End If

        If DateSecondStarted <= 45 Then
            result = DateSecondStarted + 45
        End If
    End If

    GameTimer = result
End Function

This would work, but it's still not clean code.

First, you should get rid of the GameType because what you actually want, is to determine the length of a period:

Function GameTimer(FirstStarted, SecondStarted, PeriodInMinutes)
    Dim result
    result = PeriodInMinutes * 2

    DateFirstStarted = DateDiff("n", FirstStarted, FormatDateTime(Now(), 0))
    DateSecondStarted = DateDiff("n", SecondStarted, FormatDateTime(Now(), 0))

    If DateFirstStarted <= PeriodInMinutes Then
        result = DateFirstStarted
    End If

    If DateSecondStarted <= PeriodInMinutes Then
        result = DateSecondStarted + PeriodInMinutes
    End If

    GameTimer = result
End Function

Usage

CurrentGameTime = GameTimer(FormatDateTime(objLiveCommentary("DateFirstStarted"), 0),
    FormatDateTime(objLiveCommentary("DateSecondStarted"), 0),
    45)

A next step would be to replace the parameters FirstStarted and SecondStarted by an Array to allow games with thirds and quarters.


Array of period start times

Function GameTimer(Periods, PeriodInMinutes)
    Dim result
    Dim currenttime
    Dim i

    result = 0 '-- preset to zero --'

    For i = 1 To (UBound(Periods))
        currenttime = DateDiff("n", Periods(i), FormatDateTime(Now(), 0))

        If currenttime > 0 Then
            If (currenttime <= PeriodInMinutes * i) Then
                result = currenttime + (PeriodInMinutes * (i - 1))
            Else
                result = PeriodInMinutes * i
            End If
        End If
    Next

    GameTimer = result
End Function

Usage

Dim CurrentGameTime
Dim IceHockeyPeriods(3)

IceHockeyPeriods(1) = "2010-04-15 19:30"
IceHockeyPeriods(2) = "2010-04-15 20:00"
IceHockeyPeriods(3) = "2010-04-15 20:30"

CurrentGameTime = GameTimer(IceHockeyPeriods, 20)

Edit

Refactored to correct the flaw that between periods the full time is returned.

Filburt
This makes a lot of sense. Can you explain a little bit more about replacing FirstStarted and SecondStarted by an array?
zurna
Also, CurrentGameTime = result or CurrentGameTime = GameTimer?
zurna
This is brilliant Filburt. I love the way you planned and organized it. One last question though. If I am not mistaken, it will print " result = PeriodInMinutes * UBound(Periods) '-- preset to max game time --'" between two periods. So between period one and period two of a hockey game, the function will print 60. Can you think of a way to fix that flaw?
zurna
@zurna: You are right - this is a bug. I did fix this by reorganizing the Ifs and distinguishing between period time and inter-period time.
Filburt