Skip to content

Instantly share code, notes, and snippets.

@jeffrymorris
Created December 4, 2020 05:45
Show Gist options
  • Save jeffrymorris/e13278a5f83227b8efa50b5f34d07eed to your computer and use it in GitHub Desktop.
Save jeffrymorris/e13278a5f83227b8efa50b5f34d07eed to your computer and use it in GitHub Desktop.
coucbase-txn-write
if (monsterNewHitPoints <= 0)
{
// Monster is killed. The remove is just for demoing, and a more realistic example would set a
// "dead" flag or similar.
await ctx.RemoveAsync(monster).ConfigureAwait(false);
// The player earns experience for killing the monster
var experienceForKillingMonster =
monsterContent.GetValue("experienceWhenKilled").ToObject<int>();
var playerExperience = playerContent.GetValue("experiance").ToObject<int>();
var playerNewExperience = playerExperience + experienceForKillingMonster;
var playerNewLevel = CalculateLevelForExperience(playerNewExperience);
_logger.LogInformation(
"Monster {monsterId} was killed. Player {playerId} gains {experienceForKillingMonster} experience, now has level {playerNewLevel}.",
monsterId, playerId, experienceForKillingMonster, playerNewLevel);
playerContent["experience"] = playerNewExperience;
playerContent["level"] = playerNewLevel;
await ctx.ReplaceAsync(player, playerContent).ConfigureAwait(false);
}
else
{
_logger.LogInformation("Monster {monsterId} is damaged but alive.", monsterId);
// Monster is damaged but still alive
monsterContent["hitpoints"] = monsterNewHitPoints;
await ctx.ReplaceAsync(monster, monsterContent).ConfigureAwait(false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment