Matt Barnes scored a big win in court today against his ex-wife Gloria Govan -- the court awarded him sole physical and legal custody of their twin 10-year-old boys ... TMZ Sports has learned. 

The two have been at war for years -- and today, Matt obtained an 18-month restraining order against Gloria claiming she's a physical danger to him. 

The restraining order was granted through May 2020 -- but does not extend to the kids because the judge doesn't believe Gloria poses a danger to them. 

During Thursday's hearing, the judge ruled Gloria will get regular visitation -- including every other weekend from Friday until Monday morning. She is also allowed to have dinner with the kids every Wednesday evening.