Hey guys, I have some problems regarding looping through and changing more complex objects in stable memory.
I have an Array / List / HashMap / Iter (I’m horrible with types, sorry) in stable memory that I’d like to loop through and change each element. I managed to figure out how to change one separately (code below). But I can’t seem to understand how types work and the compiler just keeps complaining about something no matter what I try.
Relevant Code Snippets
This is what gets appeneded to the previous array each week
private stable var pastWeeksPlayerScores : [[(Principal, PlayerHistoricalStats)]] = [];
This is an Array to which the following is appended every week
private var weekPlayerScores : HashMap.HashMap<Principal, PlayerHistoricalStats> =
HashMap.fromIter(_weekPlayerScores.vals(), 0, Principal.equal, Principal.hash);
What I’m trying to do is loop through pastWeeksPlayerScores
and for each do what is shown below, basically resetting certain parts of the object to 0s. And I need to loop through pastWeeksPlayerScores
and do this since each of those is a list / array / hashmap)
allTimePlayerScores := HashMap.map<Principal, PlayerHistoricalStats, PlayerHistoricalStats>(allTimePlayerScores, Principal.equal, Principal.hash, func (k, v) {
let updatedScores : PlayerHistoricalStats = {
arenaStats = v.arenaStats;
tournamentStats = {
wins : Nat = 0;
losses : Nat = 0;
points : Nat = 0};
botStats = v.botStats;
};
Debug.print("[After]: " # debug_show(updatedScores));
return updatedScores;
});
I hope this makes sense.
Types mentioned above
public type HistoricalStats = {
wins: Nat;
losses: Nat;
points: Nat;
};
public type PlayerHistoricalStats = {
arenaStats: HistoricalStats;
tournamentStats: HistoricalStats;
botStats: HistoricalStats;
};
In a desperate attempt I tried doing this HashMap.map
resetting using pastWeeksPlayerScores[0]
to be able to modify elements one by one, but that didn’t work either.
Any help is greatly appreciated!