﻿B4A=true
Group=Classes
ModulesStructureVersion=1
Type=Class
Version=11.5
@EndOfDesignText@
'
' Class for Player information record used within the Player Information Database.
'
#Region  Documentation
	'
	' Name......: clsPlayerInfoRec
	' Release...: 4
	' Date......: 30/05/23
	'
	' History
	' Date......: 01/09/22
	' Release...: 1
	' Created by: D Morris (started 11/06/22)
	' Details...: First release to support version tracking
	'
	' Date......: 25/01/23
	' Release...: 2
	' Overview..: Comments added to handle the stop player (see B4XFinishPost when clicking on the player in the to finish list).
	' Amendee...: D M.
	' Details...: Mod: Comments added.
	'
	' Date......: 11/03/23
	' Release...: 3
	' Overview..: Improvements to detecting if player has been tagged
	' Amendee...: D Morris
	' Details...: Added: Public IsTaggedNfcOrManual
	'
	' Date......: 30/05/23
	' Release...: 4 
	' Overview..: Support for calculating the run time and new isStopped method.
	' Amendee...: D Morris
	' Details...: Added: Property runTime.
	'
	' Date......: 
	' Release...: 
	' Overview..:
	' Amendee...: 
	' Details...: 
	'
#End Region  Documentation

#Region  Mandatory Subroutines & Data

Sub Class_Globals
	' Warning: if any these variable are changed - Subs may need modification.
	Public name As String			' Player's name (Stored in displayed format)
	Public nameCompressed As String	' Player's name compressed format (all upper case and whites space removed) This must be unique. 
	Public started As Boolean		' Player started in game (player is finished if started = false and time > 0 and not toBeStarted)
									'  NOTE: Started = true and time > 0 indicates a player time has been stopped - this occurs in
									'			B4XFinishPost clicking on the Players in the finished list.	
	Public startTime As Long		' Player start time (always used DateTime to get or view values).
	Public tagId As String			' Tag ID code (unique to each tag): = "" tag not allocated: = modDragonflyApp.TAG_NOT_AVAILABLE (indecates manually tagged) 
	Public team As Int				' Team number (0 = red, 1 = blue).
	Public time	As Long				' Time value (i.e. time to cover course - always used DateTime to get or view values) 
									'   Note:This value is updated when player finishes (if = 0 players have not completed the coarse)
	Public toBeStarted As Boolean	' Indicates a player has been inserted in the "to be started" list - See Start Players state.
End Sub

' Initialize object
Public Sub Initialize
	
End Sub

' Properties
' Get the run time (if player still running the value is calculated.
public Sub getrunTime As Long
	Dim runTime As Long = time 
	If Not(IsFinished) And time = 0 Then ' Player is still running
		runTime = DateTime.Now - startTime
	End If
	Return runTime
End Sub

#End Region  Mandatory Subroutines & Data

#Region  Public Subroutines

' Clones the player info to a new player info record (deep copy)
Public Sub Clone As clsPlayerInfoRec
	Dim newPlayerInfo As clsPlayerInfoRec : newPlayerInfo.initialize
	newPlayerInfo.name = name
	newPlayerInfo.nameCompressed = nameCompressed
	newPlayerInfo.started = started
	newPlayerInfo.startTime = startTime
	newPlayerInfo.tagId = tagId
	newPlayerInfo.team = team
	newPlayerInfo.time = time
	newPlayerInfo.toBeStarted = toBeStarted
	Return newPlayerInfo
End Sub

' Performs a deep compare of the passed record with this record
' Return True if compares - False if differences 
public Sub Compare(playerRec As clsPlayerInfoRec) As Boolean
	Dim compareOk As Boolean = False
	If name = playerRec.name And nameCompressed = playerRec.nameCompressed Then ' TODO check if nameCompressed should be compared
		If started = playerRec.started And startTime = playerRec.startTime Then ' TODO check if started and startTime should be compared
			If tagId = playerRec.tagId And team = playerRec.team And time = playerRec.time Then ' TODO check if time should be compared. 
				If toBeStarted = playerRec.toBeStarted Then
					compareOk = True					
				End If
			End If
		End If
	End If
	Return compareOk
End Sub

' Compare names (Case insensitve comparison and white spaces removed).
' Returns true if compare (i.e. "AbC 12" = "abc12" returns true) 
Public Sub CompareNames(name1 As String, name2 As String) As Boolean
	Return IIf(Compress(name1) = Compress(name2), True, False)	
End Sub

' Returns a compressed version of the player's name.
' Note: Converted to upper case and white spaces removed.
Public Sub Compress(nameString As String) As String
	Return modString.FilterToAlphaNumeric(nameString.ToUpperCase) ' TODO reduce to one line when tested.
End Sub

' Returns an instance of this object containing the data contained in the specified map.
Public Sub ConvertFromMap(mapInput As Map) As clsPlayerInfoRec
	Dim rtnObj As clsPlayerInfoRec : rtnObj.initialize
	rtnObj.name = mapInput.GetDefault("name", "???")
	rtnObj.nameCompressed = mapInput.GetDefault("nameCompressed", "???")
	rtnObj.started = mapInput.GetDefault("started", False)
	rtnObj.startTime = mapInput.GetDefault("startTime", 0)
	rtnObj.tagId = mapInput.GetDefault("tagId", "")
	rtnObj.team = mapInput.GetDefault("team", 0)
	rtnObj.time = mapInput.GetDefault("time", 0)
	rtnObj.toBeStarted = mapInput.GetDefault("toBeStarted", False)
	Return rtnObj
End Sub

' Checks if player Info Rec is empty
Public Sub IsEmpty  As Boolean
	Return (name = "" And nameCompressed = "" And tagId = "" ) ' may need to check more fields. 
End Sub

' Check if player has finished.
Public Sub IsFinished As Boolean
	Return (IsTaggedNfcOrManual And Not(started) And time > 0 And Not(toBeStarted))
End Sub

' Check if player is ready to start (i.e. tagged And Not started And Not toBeStarted And has Not finished)
Public Sub IsReadyToStart As Boolean
	Return (IsTaggedNfcOrManual And Not(started) And Not(toBeStarted) And time = 0)
End Sub

' Check if player is started - also checks if tagged And Not toBeStarted 
public Sub IsStarted As Boolean
	Return ((started) And Not(toBeStarted) And IsTaggedNfcOrManual)
End Sub

Public Sub IsStopped As Boolean
	Return ( IsStarted And time > 0) 
End Sub

' Checks if player is tagged (by NFC and NOT tagged manually)
Public Sub IsTagged As Boolean
	Return (tagId <> "" And tagId <> modDragonflyApp.TAG_NOT_AVAILABLE)
End Sub

' Checks if player has been manually tagged
Public Sub IsTaggedManually As Boolean
	Return (tagId = modDragonflyApp.TAG_NOT_AVAILABLE)
End Sub

' Check if player has been tagged (by nfc or manually)
Public Sub IsTaggedNfcOrManual As Boolean
	Return (tagId <> "") 
End Sub

'Populates the object with records and returns a cloned record. Note: DM 11/06/22 this was an attempt to deal with initialize() not been overloadable.
' Not the nameCompressed is automatically created.
Public Sub Populate(playerName As String, playerStarted As Boolean, pStartTime As Long, pTagId As String, _
												pTeam As Int, pTime As Long, pToBeStarted As Boolean) As clsPlayerInfoRec
	name = playerName
	nameCompressed = Compress(playerName)
	started = playerStarted
	startTime = pStartTime
	tagId = pTagId
	team = pTeam
	time = pTime
	toBeStarted = pToBeStarted
	Return Clone
End Sub


#End Region  Public Subroutines

#Region  Local Subroutines


#End Region  Local Subroutines