All perfect praise be to Allaah, The Lord of the Worlds. I testify that there is none worthy of worship except Allaah, and that Muhammad is His slave and Messenger. We ask Allaah to exalt his mention as well as that of his family and all his companions.
Masturbation is a bad practice and unpleasant habit. It harms one's religion since it is forbidden, and it has injurious side effects on one's body as confirmed by medical doctors. Among the bad effects are the person feels depression, escapes from his responsibilities and could likely lead to sexual impotence.
Therefore, you have to fear Allaah and hasten to make repentance. Know that giving up addictions is hard for those who want to give them up for the sake of other than Allaah, but it is much easier for those who give up them for the sake of Allaah.
Allaah says (which means): {As for he who gives and fears Allaah and believes in the best [reward], we will ease him toward ease.}[92:5-7]. Allaah also says (which means): {And those who strive for Us-We will surely guide them to Our ways. And indeed, Allaah is with the doers of good.}[29:69].
For more details on this topic, please read the Fatwa: 81150.
As for your prayers, they are valid if you perform them fulfilling their conditions; one of which is to perform them in the state of complete purity. One's fasting is not affected if one does not masturbate during his fast. However, if one masturbates while he is fasting, his fast becomes null. So, one has to repent and make up the day according to the preponderant opinion of Muslim scholars.
Allaah knows best.