Skip to content

Instantly share code, notes, and snippets.

@kamyker
Last active March 19, 2024 20:08
Show Gist options
  • Save kamyker/08449c42cbd9e3d1e64261c2f31ad8cc to your computer and use it in GitHub Desktop.
Save kamyker/08449c42cbd9e3d1e64261c2f31ad8cc to your computer and use it in GitHub Desktop.
Persistent Player Statistics, Verse doc rewritten (untested)
var PlayerStatsMap:weak_map(player, player_stats_table) = map{}
player_stats_table := class<final><persistable>:
Version:int = 0
Score:player_stat = player_stat{}
Wins:player_stat = player_stat{}
Losses:player_stat = player_stat{}
ClonePlayerStatsTable<constructor>(OldTable:player_stats_table)<transacts> := player_stats_table:
Version := OldTable.Version
Score := OldTable.Score
Wins := OldTable.Wins
Losses := OldTable.Losses
player_stat := class<final><persistable>:
CurrentValue:int = 0
HighestValue:int = 0
GetUpdated(NewValue:int)<transacts>:player_stat=
player_stat:
CurrentValue := NewValue
HighestValue := if. NewValue > HighestValue then NewValue else HighestValue
player_stats_managerr := class():
GetPlayerStats(Agent:agent)<decides><transacts>:player_stats_table=
Player := player[Agent]
PlayerStatsMap[Player]
InitializeAllPlayers(Players:[]player):void =
for (Player : Players):
InitializePlayer(Player)
InitializePlayer(Player:player):void=
if:
not PlayerStatsMap[Player]
set PlayerStatsMap[Player] = player_stats_table{}
IncreaseWins(Agent:agent):void=
if:
PlayerStatsTable := GetPlayerStats[Agent]
WinStat := PlayerStatsTable.Wins
set PlayerStatsMap[player[Agent]] = player_stats_table:
ClonePlayerStatsTable<constructor>(PlayerStatsTable)
Wins := WinStat.GetUpdated(WinStat.CurrentValue+1)
IncreaseLosses(Agent:agent):void=
if:
PlayerStatsTable := GetPlayerStats[Agent]
LossesStat := PlayerStatsTable.Losses
set PlayerStatsMap[player[Agent]] = player_stats_table:
ClonePlayerStatsTable<constructor>(PlayerStatsTable)
Losses := LossesStat.GetUpdated(LossesStat.CurrentValue+1)
RecordScore(Agent:agent, Score:int):void=
if:
PlayerStatsTable := GetPlayerStats[Agent]
ScoreStat := PlayerStatsTable.Score
set PlayerStatsMap[player[Agent]] = player_stats_table:
ClonePlayerStatsTable<constructor>(PlayerStatsTable)
Score := ScoreStat.GetUpdated(Score)
# MakePlayerStatsTable<constructor>(OldTable:player_stats_table)<transacts> := player_stats_table:
# Version := OldTable.Version
# Score := OldTable.Score
# Wins := OldTable.Wins
# Losses := OldTable.Losses
#
# player_stat := class<final><persistable>:
# CurrentValue:int = 0
# HighestValue:int = 0
# SetHighestValue(NewValue:int)<transacts>:int=
# if (NewValue > HighestValue):
# NewValue
# else:
# HighestValue
# MakeUpdatedPlayerStat<constructor>(OldStats:player_stat, NewValue:int)<transacts> := player_stat:
# CurrentValue := NewValue
# HighestValue := OldStats.SetHighestValue(NewValue)
# stat_type := class<computes><unique><abstract>:
# DebugString():string
# StatType := module:
# score_stat<public> := class<computes><unique>(stat_type):
# DebugString<override>():string = "Score"
# win_stat<public> := class<computes><unique>(stat_type):
# DebugString<override>():string = "Win"
# loss_stat<public> := class<computes><unique>(stat_type):
# DebugString<override>():string = "Loss"
# Score<public>:score_stat = score_stat{}
# Win<public>:win_stat = win_stat{}
# Loss<public>:loss_stat = loss_stat{}
# player_stats_manager := class():
# GetPlayerStats(Agent:agent)<decides><transacts>:player_stats_table=
# var PlayerStats:player_stats_table = player_stats_table{}
# if:
# Player := player[Agent]
# PlayerStatsTable := PlayerStatsMap[Player]
# set PlayerStats = MakePlayerStatsTable(PlayerStatsTable)
# PlayerStats
# InitializeAllPlayers(Players:[]player):void =
# for (Player : Players):
# InitializePlayer(Player)
# InitializePlayer(Player:player):void=
# if:
# not PlayerStatsMap[Player]
# set PlayerStatsMap[Player] = player_stats_table{}
# RecordPlayerStat(Agent:agent, Stat:stat_type, ?Score:int = 0):void=
# if:
# Player := player[Agent]
# PlayerStatsTable := PlayerStatsMap[Player]
# if(Stat = StatType.Score):
# ScoreStat := PlayerStatsTable.Score
# set PlayerStatsMap[Player] = player_stats_table:
# MakePlayerStatsTable<constructor>(PlayerStatsTable)
# Score := MakeUpdatedPlayerStat(ScoreStat, Score)
# else if(Stat = StatType.Win):
# WinsStat := PlayerStatsTable.Wins
# set PlayerStatsMap[Player] = player_stats_table:
# MakePlayerStatsTable<constructor>(PlayerStatsTable)
# Wins := MakeUpdatedPlayerStat(WinsStat, WinsStat.CurrentValue+1)
# else if(Stat = StatType.Loss):
# LossesStat := PlayerStatsTable.Losses
# set PlayerStatsMap[Player] = player_stats_table:
# MakePlayerStatsTable<constructor>(PlayerStatsTable)
# Losses := MakeUpdatedPlayerStat(LossesStat, LossesStat.CurrentValue+1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment