Skip to content

Instantly share code, notes, and snippets.

@lprsd
Created August 28, 2021 22:18
Show Gist options
  • Save lprsd/d2ea08e6cd2e5a45d7d28641c30486fe to your computer and use it in GitHub Desktop.
Save lprsd/d2ea08e6cd2e5a45d7d28641c30486fe to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract GamePrediction {
address public owner;
// owner = msg.sender;
struct Game {
string name;
bool isActive;
bool didWin; // 1 is win 0 os loss. Valid only for inActive;
// uint betSize;
uint potSize;
address[] winPredictors;
address[] losePredictors;
}
Game[] public activeGames;
function newGame(string memory name) public returns (Game[] memory) {
address[] memory winPredictors;
address[] memory losePredictors;
activeGames.push(Game(name,true,true,0,winPredictors,losePredictors));
return activeGames;
}
function markGameOver(uint gamei,bool didWin) public returns (uint, address[] memory) {
activeGames[gamei].isActive = false;
address[] memory correctPredictors;
if(didWin){
correctPredictors = activeGames[gamei].winPredictors;
}else{
correctPredictors = activeGames[gamei].losePredictors;
}
uint winAmount;
winAmount = activeGames[gamei].potSize/correctPredictors.length;
//send amounts to all win addresses
delete activeGames[gamei];
return (winAmount,correctPredictors);
}
function makePrediction(uint gamei,bool willWin) public {
if(willWin){
activeGames[gamei].winPredictors.push(msg.sender);
} else{
activeGames[gamei].losePredictors.push(msg.sender);
}
}
function getActiveGames() public view returns (Game[] memory) {
return activeGames;
}
function getLatestGame() public view returns (Game memory) {
return activeGames[0];
}
function countActiveGames() public view returns (uint) {
return activeGames.length;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment