Example: Evire Player Stats Library
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
library EvirePlayerStats {
struct PlayerStats {
uint256 gamesPlayed;
uint256 gamesWon;
uint256 totalScore;
uint256 highestScore;
uint256 rank;
mapping(string => uint256) achievements;
}
function initialize(PlayerStats storage self) internal {
self.gamesPlayed = 0;
self.gamesWon = 0;
self.totalScore = 0;
self.highestScore = 0;
self.rank = 0;
}
function recordGame(PlayerStats storage self, uint256 score, bool isWin) internal {
self.gamesPlayed += 1;
self.totalScore += score;
if (isWin) {
self.gamesWon += 1;
}
if (score > self.highestScore) {
self.highestScore = score;
}
// Add logic to update rank based on the game's ranking algorithm
}
function addAchievement(PlayerStats storage self, string memory achievement, uint256 value) internal {
self.achievements[achievement] = value;
}
function getPlayerStats(PlayerStats storage self) internal view returns (
uint256 gamesPlayed,
uint256 gamesWon,
uint256 totalScore,
uint256 highestScore,
uint256 rank
) {
return (
self.gamesPlayed,
self.gamesWon,
self.totalScore,
self.highestScore,
self.rank
);
}
function getAchievement(PlayerStats storage self, string memory achievement) internal view returns (uint256) {
return self.achievements[achievement];
}
}
Example of usage:
Last updated