def GateEvent(carNum): print "Player", carNum player = carNum global players global num_finished global race_info # Verify valid player number player = int(player) if player < 0 or player >= Players.MAX_PLAYERS: return # Verify player is joined if players[player] == None: return # Calculate total time timediff = datetime.now() - start_time secs = timediff.seconds + timediff.microseconds / 1000000.0 # Calculate current lap time if len(players[player].laps) != 0: secs -= sum(players[player].laps) print "Player %d finished lap %d of %d in %f seconds" % (player + 1, len(players[player].laps) + 1, race_info.Laps, secs) # Add new lap time try: players[player].laps.append(round(secs, 3)) except Exception as e: print e # If last lap if len(players[player].laps) == race_info.Laps: # This player finished sender.sendAsync("PlayerFinish", player=str(player), time=str(sum(players[player].laps))) print "Player %d finished." % player num_finished += 1 player_infos[player].Finished = 1 if DEBUG: print "# finished: %d, # total: %d" %(num_finished, race_info.JoinedPlayers) # If all players are finished if num_finished == race_info.JoinedPlayers: sender.sendAsync("RaceFinish") race_info.State = FINISHED print "The race is over!" num_finished = 0 # If Player didn't finish elif len(players[player].laps) < race_info.Laps: sender.sendAsync("LapComplete", player=str(player), time=str(secs)) # Update positions try: positions = laps.getPositions(players) player_infos[player].RelativeTime = int(round((sum(players[player].laps) - sum(players[positions[0]].laps)) * 1000, 0)) player_infos[player].CurrentLap += 1 # Update each player for p in range(Players.MAX_PLAYERS): if player_infos[p] != None: player_infos[p].Position = positions.index(p) sender.sendUpdate(player_infos[p], "PlayerInfo" + str(player_infos[player].Number)) print "Sent player info" except Exception as e: print e
def testGetPositions(self): """Check that getPositions functions during a race""" assert laps.getPositions(self.racingPlayers) == [3,0,2,1], "Players sorted into incorrect positions"
def testGetEmptyPositions(self): """Check that getPositions functions when some players are None""" assert laps.getPositions(self.emptyPlayers) == [3,1,0,2], "Players sorted into incorrect positions"
def testGetPositionsStart(self): """Check that getPositions functions when no laps are finished""" assert laps.getPositions(self.startingPlayers) == [0,1,2,3], "getPositions not performing stable sort"