DEV Community

Cover image for GameManager SmartContract deployed on XinFin XDC Network.
MahaLakshmi Perumal
MahaLakshmi Perumal

Posted on

GameManager SmartContract deployed on XinFin XDC Network.

Deployed #solidity #smartcontract for gamemanager on the energy saving and cost efficient XinFin XDC Hybrid Blockchain Network.

Use the code:
pragma solidity ^0.4.18;
contract GameManager {
modifier onlyOwner {
require(msg.sender == owner);
_;
}
modifier onlyServer {
require(msg.sender == server);
_;
}
event GameJoined(uint indexed rounds, address indexed user, uint playerPos);
event GameStarted(uint indexed rounds, address byWhom, uint numPlayers, uint timestamp);
event GameFinalized(uint indexed rounds, address user, uint numPlayers);
event Voted(uint indexed rounds, address user, bytes32 currStateHash, bytes32 newStateHash, uint currPlayer, uint numVoted);
event ServerNeeded();
uint public currPlayerIndex;
mapping (uint => address) public userPosition;
mapping (address => bool) public usersInGame;
mapping (address => uint) public balances;
mapping (address => bool) public hasVoted;
address public owner;
bytes32 public currStateHash;
uint public numStateVerified;
bool public gameInProgress;
bool public callTheServer;
bool public playersVoted;
uint public creationTime;
address public server;
uint public submitStateStartTime;
uint public numGamesPlayed;
uint public rounds;
uint VOTE_PERIOD = 5 minutes;
uint32 ONE_PLAY = 1000;
uint FEE = 200;
uint MIN_PLAYERS = 5;
uint MAX_PLAYERS = 25;
uint PRICE_TO_ENTER = 10 ** 15;
function GameManager() public {
owner = msg.sender;
gameInProgress = false;
playersVoted = false;
callTheServer = false;
numStateVerified = 0;
creationTime = now;
currPlayerIndex = 0;
rounds = 0;
}
function joinGame() public payable {
require(msg.value >= PRICE_TO_ENTER);
// A user can't enter a game while the game is ongoing
require(gameInProgress == false);
// A user can't enter a game twice with the same address
require(usersInGame[msg.sender] == false);
// We can't have more than 25 players?
require(currPlayerIndex <= 25);
// remember which position in the array did the player get
userPosition[currPlayerIndex] = msg.sender;
usersInGame[msg.sender] = true;
// Update the current position in the array
currPlayerIndex++;
// trigger game start if we reached the required num. of players
if (currPlayerIndex >= MIN_PLAYERS) {
gameInProgress = true;
GameStarted(rounds, msg.sender, currPlayerIndex, now);
}
GameJoined(rounds, msg.sender, currPlayerIndex);
}
// What happens if some of the players don't vote??
function gameEnds(uint32[] state, uint position) public {
// The msg.sender is sending us the position where he is
require(userPosition[position] == msg.sender);
// The person who votes, must be one of the players
require(usersInGame[msg.sender] == true);
// TODO: figure out how to reset this check on game end
// A player can vote only once
//require(hasVoted[msg.sender] == false);
bytes32 stateHash = keccak256(state);
Voted(rounds, msg.sender, currStateHash, stateHash, currPlayerIndex, numStateVerified);
// we remove the user from game, so he can join in the next one
usersInGame[msg.sender] = false;
if (currStateHash == 0x0) {
currStateHash = stateHash;
submitStateStartTime = now;
numStateVerified++;
return;
}
if (currStateHash == stateHash) {
numStateVerified++;
hasVoted[msg.sender] = true;
// The last one
if (numStateVerified >= currPlayerIndex) {
playersVoted = true;
submitState(state);
}
} else {
// Bad stuff somone is cheating, server will punish the cheater and give the money to the poor
callTheServer = true;
ServerNeeded();
}
}
// Call this if after n blocks not all players have voted
// This will be callabale by anyone and we'll reward the player for the call
function finalizeGame(uint32[] state) public {
require(now >= (submitStateStartTime + VOTE_PERIOD));
// TODO: check for rounding error && possible attacks
// If majority of the verified has votes take that as true
if (numStateVerified > (currPlayerIndex / 2)) {
bytes32 stateHash = keccak256(state);
// A user gave a wrong score
if(stateHash != currStateHash) {
return;
}
submitState(state);
// reward the player for calling this function
// gameToken.transfer(msg.sender, REWARD_FOR_FINALIZE);
} else {
// who you gonna call when a cheater appears?
callTheServer = true;
ServerNeeded();
}
}
// Somebody tried the cheat the server will punish the bad
// and reward the good
function judgment(uint32[] state) public onlyServer {
require(callTheServer == true);
submitState(state);
}
function submitState(uint32[] state) internal {
require(playersVoted == true);
for(uint i = 0; i < currPlayerIndex; ++i) {
balances[userPosition[i]] += state[i];
usersInGame[userPosition[i]] = false;
}
numGamesPlayed++;
GameFinalized(rounds, msg.sender, currPlayerIndex);
newGameSession();
}
function withdrawWins() public {
require(balances[msg.sender] > 0);
msg.sender.transfer(balances[msg.sender]);
}
function newGameSession() internal {
gameInProgress = false;
playersVoted = false;
callTheServer = false;
numStateVerified = 0;
currPlayerIndex = 0;
currStateHash = 0x0;
rounds++;
}
function setServer(address _server) public onlyOwner {
server = _server;
}

Top comments (16)

Collapse
 
crony_service_20de64b3254 profile image
Crony Service

I’m motivated with all the surpassing and also preachy record which you supply in such tiny timing. top DryBar comedians

Collapse
 
crony_service_20de64b3254 profile image
Crony Service

Once i received onto your website even though getting consideration basically somewhat tad submits. Pleasurable strategy for upcoming, I am bookmarking at the same time acquire kinds end rises upwards. Modern furniture in Lahore Pakistan

Collapse
 
crony_service_20de64b3254 profile image
Crony Service

I prefer the particular distribute. It is actually outstanding to learn someone verbalize from the cardiovascular system and in addition top quality using this important material could be simply experienced. bridal bed designs in lahore

Collapse
 
audrey_blundel_ba1fc6a9f7 profile image
Audrey blundel

It's exciting to see the GameManager SmartContract deployed on the XinFin XDC Network. This move is a big step toward enhancing the gaming ecosystem with blockchain, offering improved scalability and cost-efficiency. The XDC network's unique blend of public and private features could really benefit decentralized applications by ensuring faster transactions without compromising security.

On a related note, for anyone struggling with understanding blockchain concepts or smart contracts while working on their assignments, seeking Programming Assignment Help can be a game-changer. It can provide the guidance needed to grasp complex topics and implement effective solutions, especially when dealing with cutting-edge technologies like the XinFin XDC Network

Collapse
 
kennethmarek profile image
KennethMarek

Your article is excellent. I'd want to share something similar. A $1 minimum deposit casino is a gaming establishment that allows players to play and win for as little as $1. A single dollar, that's right. Because of the low deposit threshold, you'll have a chance to earn real money while playing games with absolutely little risk. For additional information, go to AUcasimile Bank transfers, eWallets, credit and debit cards, and cryptocurrency payments are all available on most sites. To avoid additional fees, several websites make the $1 deposit casino Paysafe method the default.

Collapse
 
crony_service_20de64b3254 profile image
Crony Service

Interesting post. I Have Been wondering about this issue, so thanks for posting. Pretty cool post. meg steedle actress biography

Collapse
 
sns_autoservices_11f0257 profile image
SNS Auto Services L.L.C

At SNS Auto Services L.L.C, we live and breathe cars. Our experienced team is committed to delivering high-quality services to ensure your vehicle looks and performs at its best. From the moment you step into our garage, you’ll experience our dedication to excellence and customer satisfaction. We strive to create a welcoming environment where car enthusiasts and owners alike can feel at home. Our goal is to create a space where car lovers can connect and where every vehicle receives the care and attention it deserves. Join us in celebrating the beauty and power of automobiles. Trust SNS Auto Garage for all your car services, let us help you keep your vehicle in pristine condition.

Collapse
 
dark_attitude_26f6d8427c9 profile image
Dark Attitude

Your blog post is very interesting. Your level of thinking is good and the clarity of writing is excellent. I enjoyed so much to read this post
mens gothic clothing

Collapse
 
crony_service_20de64b3254 profile image
Crony Service
Collapse
 
crony_service_20de64b3254 profile image
Crony Service

Hello! Good stuff, please keep us posted when you post again something like that! cleaning services sparks nv

Collapse
 
crony_service_20de64b3254 profile image
Crony Service

Some comments may only be visible to logged-in visitors. Sign in to view all comments.