NEW YORK — A federal judge has dismissed a lawsuit by fantasy sports contestants who claimed they were damaged by sign stealing in Major League Baseball.

Five men had sued MLB, MLB Advanced Media, the Houston Astros and the Boston Red Sox in federal court in Manhattan, claiming fraud, violation of consumer-protection laws, negligence, unjust enrichment and deceptive trade practices by teams that violated MLB’s rules against the use of electronics to steal catchers’ signs.

“A sport that celebrates ‘stealing,’ even if only of a base, may not provide the perfect encouragement to scrupulous play,” U.S. District Judge Jed S. Rakoff wrote Friday in a 32-page opinion. “Nor can it be denied that an overweening desire to win may sometimes lead our heroes to employ forbidden substances on their (spit) balls, their (corked) bats, or even their (steroid-consuming) selves. But as Frank Sinatra famously said to Grace Kelly (in the 1956 movie musical “High Society”), “there are rules about such things.”

MLB Commissioner Rob Manfred ruled in January the Astros violated rules against electronic sign-stealing during home games en route to their World Series title in 2017 and again in 2018. He suspended manager AJ Hinch and general manager Jeff Luhnow for one season each, and both were fired by the team. Manfred fined the Astros $5 million, the maximum under MLB rules and stripped the team of its next two first- and second-round draft picks.

Manfred fined the Red Sox in 2017 for using Apple Watches to pass along signals. Boston is being investigated for possible violations of electronic sign-stealing rules in 2018, when it won the World Series.

“In 2017 and thereafter, the Houston Astros, and somewhat less blatantly the Boston Red Sox, shamelessly broke that rule, and thereby broke the hearts of all true baseball fans,” Rakoff wrote. “But did the initial efforts of those teams, and supposedly of Major League Baseball itself, to conceal these foul deeds from the simple sports bettors who wagered on fantasy baseball create cognizable legal claim? On the allegations here made, the answer is no.”

The five men who sued participated in fantasy contests hosted by DraftKings from 2017-19.

“The connection between the alleged harm plaintiffs suffered and defendants’ conduct is simply too attenuated to support any of plaintiffs’ claims for relief,” Rakoff said.