Consider the following basic solidity smart contract
event BattleResult(string result,uint playerResult, uint enemyResult);
mapping(address => uint)playerChoice;
function setChoice(uint _number)public {
playerChoice[msg.sender] = _number
}
function battle(address _target) public {
playerValue = playerChoice[msg.sender];
enemyValue = playerChoice[_target];
if(playerValue > enemyValue){
emit Battleresult("win",playerValue,enemyValue);
}
else if(playerValue < enemyValue){
emit Battleresult("lose",playerValue,enemyValue);
}
else if(playerValue == enemyValue){
emit Battleresult("draw",playerValue,enemyValue);
}
}
In this basic example players choose a number and they set up a battle. Don't mind the security measures and the fact that players can check opponent's value before entering a battle.
The problem I have is that when I set up the battle on the frontend, I try to show the player the results (whatever is emited: win, lose or draw). The problem I have is that it is so impossible to filter information just for a specific player and everytime I try to do something I am getting no results in the console.
I tried
SmartcontractInstance.events.BattleResult({}, {fromBlock: 0}, function(error,event){
console.log(event);
})
}
which allows me to get all the past events. However is there a way to get just the result from the specific battle?
For example, should I change my solidity smart contract and for each function add
returns(string,uint,uint)
One thing I tried to do is check the address and see if it matches the sender, then push it into the array and I would simply show the last index (this sounds inefficient but maybe is just my opinion).
let result =[];
SmartcontractInstance.events.BattleResult({}, {fromBlock: 0}, function(error,event){
for(let i=0; i<event.length;i++){
if(event.args.address == account[0]){
result.push(event.args.address);
}
}
})
console.log(result); /// i'm getting an empty array unfortunately