# Distance recorder V1.00 22-04-2016 -Initial release # ################################################################################################################################################# CatchEvent OnMSO( $userName, $text ) # Player event $idxOfFirtsSpace = indexOf( $text, " "); IF( $idxOfFirtsSpace == -1 ) THEN $command = $text; $argv = ""; ELSE $command = subStr( $text,0,$idxOfFirtsSpace ); $argv = trim( subStr( $text,$idxOfFirtsSpace ) ); ENDIF SWITCH( $command ) CASE "!dstboard": Distance_board( $KeyFlags,$id ); BREAK; CASE "!leave": Leave( $KeyFlags,$id ); BREAK; ENDSWITCH EndCatchEvent CatchEvent OnLapperStart() OnLapperStart_Distance(); EndCatchEvent CatchEvent OnConnect( $userName ) # Player event OnConnect_Retr_distance(); EndCatchEvent Sub OnLapperStart_Distance() ### Declare global variables ### GlobalVar $distance_version; GlobalVar $distance_pl_id; ### End ### ### Set version number ### $distance_version="V1.00"; ### End ### ### Load stored values and give them a default setting if no stored value has been found ### $distance_pl_id = GetStoredValue( "DISTANCE_PL_ID" ); IF ($distance_pl_id == "") THEN $distance_pl_id = 0; ENDIF EndSub Sub OnConnect_Retr_distance() ### Set userName variable ### $userName = GetCurrentPlayerVar( "UserName" ); ### End ### ### Search if user already has a record in the database ### FOR ($i=0;$i<$distance_pl_id;$i=$i+1) IF (GetStoredValue( "dist_username_" . $i ) == $userName ) THEN SetCurrentPlayerVar( "distance_driven",GetStoredValue( "dist_drvn_" . $i ) ); BREAK; ENDIF ENDFOR ### End ### ### When use isn't found, set distance driven to 0 and save username and distance driven into the database ### ### Raise the distance_pl_id by 1 and store new value into the dabase ### IF ( GetCurrentPlayerVar( "distance_driven" ) == "" && $userName != "" ) THEN SetCurrentPlayerVar( "distance_driven", 0 ); SetStoredValue( "dist_username_" . $distance_pl_id, $userName ); SetStoredValue( "dist_drvn_" . $distance_pl_id, GetCurrentPlayerVar( "distance_driven" ) ); $distance_pl_id = $distance_pl_id+1; SetStoredValue( "DISTANCE_PL_ID", ToNum( $distance_pl_id ) ); ENDIF ### End ### EndSub Sub Leave( $KeyFlags,$id ) ### Set userName variable ### $userName = GetCurrentPlayerVar( "UserName" ); ### End ### ### Calculate driven distance by combining session distance and already stored distance ### $driven_distance = GetPlayerVar( $userName , "SessDist" ) + GetPlayerVar( $userName , "distance_driven" ); ### End ### ### Search user and update record in the database ### FOR ($j=0;$j<$distance_pl_id;$j=$j+1) IF (GetStoredValue( "dist_username_" . $j ) == $userName ) THEN SetStoredValue( "dist_drvn_" . $j, $driven_distance ); BREAK; ENDIF ENDFOR ### End ### ### Kick user out of server, to simulate a disconnect (disconnect event can't be used to get session distance, since session is already distroyed by then ### cmdLFS("/kick " . $userName ); ### End ### EndSub