More about weapons. I already commented
If you think you need weapons to protect yourself, and you don't already habitually carry such weapons and are accustomed to using them, then the choice of location is wrong.
The answer from @Machavity also pointed out that sprays and tazers will be ineffective against a group, and carrying firearms may be problematic.
There is another problem with using weapons. Suppose you make the first strike, or a panic attack, against a perceived enemy, and injure or possibly kill them. You are now in deep trouble, and although you might be able to get off any charges you will have a difficult ordeal ahead, perhaps in custody, especially as a foreigner in another country.
What then if you wait to be attacked first, so you have a legitimate claim of self-defense? You may not even get a chance to deploy your weapon. You can assume that any attacker will know what they are doing, and be better at it than you.
No, the main purpose of carrying a weapon would be to demonstrate to others that you will not be an easy target. If you must carry a weapon it should be visible, a deterrent, not some vague plan that you might be able to somehow beat off an attacker.
The question is asking how to deal with a situation that is already too late for a good outcome, for anyone except a movie hero.
My answer is that it must never come to that. It is true that this was an extreme and rare situation, but the fact that there was no warning for Morocco does not make it a safe place, especially in remote areas.
A school friend of mine, John Charles Elcoate and his girlfriend Wendy Margaret Mills, were murdered on the road to India in 1971. They were backpacking and sleeping rough, and were killed for their possessions, as I heard it at the time.
There is hardly a place in the world where there isn't a ne'er-do-well looking for an opportunity. The same principles apply as always. Do not make yourself an easy target for sneak thieves, con artists, muggers, rapists or even murderers. All of them want to pick the weakest victim they can find, to minimise the risk to themselves, and maximise their chance of success. This was apparently confirmed by the Morocco attackers: they picked the easiest target.
So I recommend that if you can afford to take accommodation, do so. In a remote area, travel in a party. Save the solo camping adventures for places that are not so wild and lawless.
And in any case, what mindset do you want during the trip? To be constantly on your guard against (possibly imagined) threats, or to have a relaxed and carefree trip – apart from any personal challenges you may have set yourself? There is an obvious answer to that.