public void ReplaceMonsterToBones(MonsterDetailsDTO monster, int world, bool force = false) { using (var context = new HugoLANDContext()) { Monstre currMonstre; Item item; currMonstre = context.Monstres.First(m => m.Id == monster.Id); try { context.Monstres.Remove(currMonstre); context.Mondes.Find(world).Items.Add(new Item { Nom = "Bones", Description = "Bones", x = monster.x, y = monster.y, ImageId = 168 }); item = context.Items.Where(x => x.Monde.Id == world && x.x == monster.x && x.y == monster.y).FirstOrDefault(); } catch { return; } int itr = force ? 5 : 1; var currVersion = currMonstre.RowVersion; do { try { context.SaveChanges(); return; } catch (DbUpdateConcurrencyException) { if (itr > 0) { var objContext = ((IObjectContextAdapter)context).ObjectContext; objContext.Refresh(RefreshMode.ClientWins, currMonstre); objContext.Refresh(RefreshMode.ClientWins, item); itr--; } } } while (itr > 0 && currVersion != currMonstre.RowVersion); } }
public int RemoveHealthMonster(MonsterDetailsDTO monster, int heroDamage, bool force = false) { using (var context = new HugoLANDContext()) { Monstre currMonster; try { currMonster = context.Monstres.Find(monster.Id); currMonster.StatPV -= heroDamage; } catch { return(monster.StatPV); } int itr = force ? 5 : 1; var currVersion = currMonster.RowVersion; do { try { context.SaveChanges(); monster.StatPV = currMonster.StatPV; return(monster.StatPV); } catch (DbUpdateConcurrencyException) { if (itr > 0) { var objContext = ((IObjectContextAdapter)context).ObjectContext; objContext.Refresh(RefreshMode.ClientWins, currMonster); itr--; } } } while (itr > 0 && currVersion != currMonster.RowVersion); return(monster.StatPV); } }
private bool damageMonster(int damage, MapTile mapTile, MonsterDetailsDTO monster) { //Do some damage, and remove the monster if its dead bool returnValue = false; //monster not dead //Set the monster health if its not already set int health = MonsterService.RemoveHealthMonster(monster, damage, false); if (health <= 0) { mapTile.ObjectHealth = 0; //Experience is the monsters max health HeroService.AddExp(_gameState.Hero, mapTile.ObjectTile.Health, false); //Remove the monster and replace with bones MonsterService.ReplaceMonsterToBones(monster, _currentWorld.ID, force: true); returnValue = true; //monster is dead } _popups.Add(new textPopup((int)mapTile.Sprite.Location.X + 40, (int)mapTile.Sprite.Location.Y + 20, (damage != 0) ? damage.ToString() : "miss")); return(returnValue); }