/// <summary> /// 吃干粮 /// </summary> /// <returns>The food.</returns> /// <param name="num">Number.</param> public int EatFood(int num) { int eatNum = 0; db = OpenDb(); SqliteDataReader sqReader = db.ExecuteQuery("select Id, Data, AreaFoodNum from UserDatasTable where BelongToRoleId = '" + currentRoleId + "'"); if (sqReader.Read()) { int userDataId = sqReader.GetInt32(sqReader.GetOrdinal("Id")); UserData user = JsonManager.GetInstance().DeserializeObject <UserData>(sqReader.GetString(sqReader.GetOrdinal("Data"))); user.AreaFood.Num = sqReader.GetInt32(sqReader.GetOrdinal("AreaFoodNum")); user.AreaFood.Num = user.AreaFood.Num > user.AreaFood.MaxNum ? user.AreaFood.MaxNum : user.AreaFood.Num; if (user.AreaFood.Num < user.AreaFood.MaxNum) { eatNum = user.AreaFood.MaxNum - user.AreaFood.Num; eatNum = eatNum <= num ? eatNum : num; user.AreaFood.Num += eatNum; //更新当前干粮 db.ExecuteQuery("Update UserDatasTable set Data = '" + JsonManager.GetInstance().SerializeObjectDealVector(user) + "', AreaFoodNum = " + user.AreaFood.Num + " where Id = " + userDataId); AreaMainPanelCtrl.MakeUpdateFoods(user.AreaFood.Num); } } db.CloseSqlConnection(); return(eatNum); }
/// <summary> /// 吃干粮 /// </summary> /// <param name="id">Identifier.</param> /// <param name="num">Number.</param> public void Eat(int id, int num) { int eatNum = 0; db = OpenDb(); SqliteDataReader sqReader = db.ExecuteQuery("select Id, Data, AreaFoodNum from UserDatasTable where BelongToRoleId = '" + currentRoleId + "'"); if (sqReader.Read()) { int userDataId = sqReader.GetInt32(sqReader.GetOrdinal("Id")); UserData user = JsonManager.GetInstance().DeserializeObject <UserData>(sqReader.GetString(sqReader.GetOrdinal("Data"))); user.AreaFood.Num = sqReader.GetInt32(sqReader.GetOrdinal("AreaFoodNum")); user.AreaFood.Num = user.AreaFood.Num > user.AreaFood.MaxNum ? user.AreaFood.MaxNum : user.AreaFood.Num; if (user.AreaFood.Num < user.AreaFood.MaxNum) { eatNum = user.AreaFood.MaxNum - user.AreaFood.Num; eatNum = eatNum <= num ? eatNum : num; user.AreaFood.Num += eatNum; num -= eatNum; if (num > 0) { //减掉吃掉的干粮辎重 db.ExecuteQuery("update BagTable set Num = " + num + " where Id = " + id); } else { //删除干粮辎重 db.ExecuteQuery("delete from BagTable where Id = " + id); } //更新当前干粮 db.ExecuteQuery("Update UserDatasTable set Data = '" + JsonManager.GetInstance().SerializeObjectDealVector(user) + "', AreaFoodNum = " + user.AreaFood.Num + " where Id = " + userDataId); AreaMainPanelCtrl.MakeUpdateFoods(user.AreaFood.Num); } else { AlertCtrl.Show("目前体力充沛不需要进食!"); } } db.CloseSqlConnection(); if (eatNum > 0) { Statics.CreatePopMsg(Vector3.zero, string.Format("补充了{0}个干粮", eatNum), Color.green, 30); GetBagPanelData(); } }