public int UpgradeUnitLevel(int unitId, int exp, int maxLevel) { var(data, unit) = GetUnit(unitId); if (data.Stars < 1) { return(0); } var levels = GetSortedLevels(); if (data.Level == levels.Count) { return(data.Level); } var newExp = data.Exp + exp; var max = levels.Last().ExpMin; if (newExp > max) { newExp = max; } while (data.Level < levels.Count) { if (data.Level == maxLevel) { newExp = 0; break; } var levelData = levels[data.Level]; if (newExp >= levelData.ExpMin) { data.Level += 1; LogicLog.UpdateUnitLevel(unitId, data.Level, exp); } else { break; } } data.Exp = newExp; return(data.Level); }