Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using DiscordAdventure.Database;
  7.  
  8.  
  9. //Add UserName check if false update the DB with the new username....
  10.  
  11.  
  12. namespace DiscordAdventure.Data
  13. {
  14.     public static class Data
  15.     {
  16.         public static async Task UpdateUser(ulong UserId, string UserName)
  17.         {
  18.             using (var DbContext = new SQLiteDbContext())
  19.             {
  20.                 User user = DbContext.Users.Find(UserId);
  21.                 if (user == null)
  22.                 {
  23.                     return;
  24.                 }
  25.                 user.Username = UserName;
  26.                 DbContext.Update(user);
  27.                 await DbContext.SaveChangesAsync();
  28.             }
  29.         }
  30.  
  31.         public static string GetUserInventory(ulong UserId)
  32.         {
  33.             using (var DbContext = new SQLiteDbContext())
  34.             {
  35.                 var userItems = DbContext.ItemUsers.Where(x => x.User_Id == UserId).ToList();
  36.                 var GetUserInventoryOutput = "";
  37.  
  38.                 foreach (ItemUser item in userItems) {
  39.                     Item dbItem = DbContext.Items.Find(item.Item_Id);
  40.                     ulong index = Convert.ToUInt32(dbItem.Id);
  41.                     var itemdes = DbContext.Items.Where(x => x.Id == index).ToString();
  42.                     string description = DbContext.Items.Where(x => x.Id == dbItem.Id).Select(x => x.Description).FirstOrDefault();
  43.                     GetUserInventoryOutput += dbItem.Name + ": " + item.Amount + "\nDescription: "
  44.                         + description + "\nID (Used to sell Items): "+ index +"\n \n";
  45.                 }
  46.                 return GetUserInventoryOutput;
  47.             }
  48.         }
  49.  
  50.         public static int GetUser(ulong UserId)
  51.         {
  52.             using (var DbContext = new SQLiteDbContext())
  53.             {
  54.                 if (DbContext.Users.Where(x => x.UserId == UserId).Count() < 1)
  55.                     return 0;
  56.                 Console.WriteLine("Successfully catched data from the Database!");
  57.                 return DbContext.Users.Where(x => x.UserId == UserId).Select(x => x.Money).FirstOrDefault();
  58.                
  59.             }
  60.         }
  61.         public static string GetHouse(ulong UserId)
  62.         {
  63.             using (var DbContext = new SQLiteDbContext())
  64.             {
  65.                 if (DbContext.Users.Where(x => x.UserId == UserId).Count() < 1)
  66.                     return "";
  67.                 Console.WriteLine("Successfully catched data from the Database!");
  68.                 return DbContext.Users.Where(x => x.UserId == UserId).Select(x => x.House).FirstOrDefault();
  69.  
  70.             }
  71.         }
  72.  
  73.  
  74.         public static async Task SaveUser(ulong UserId, int Amount, string UserName)
  75.         {
  76.             using (var DbContext = new SQLiteDbContext())
  77.             {
  78.                 if (DbContext.Users.Where(x => x.UserId == UserId).Count() < 1) {
  79.                     //Create a User in the DB
  80.                     DbContext.Users.Add(new User
  81.                     {
  82.                         UserId = UserId,
  83.                         Money = 100 + Amount,
  84.                         Username = UserName,
  85.                         House = null
  86.                     });
  87.  
  88.                     DbContext.ItemUsers.Add(new ItemUser {
  89.                         User_Id = UserId,
  90.                         Item_Id = 1,
  91.                         Amount = 1
  92.                     });
  93.                 }
  94.                 else
  95.                 {
  96.                     User Current = DbContext.Users.Where(x => x.UserId == UserId).FirstOrDefault();
  97.                     Current.Money += Amount;
  98.                     DbContext.Users.Update(Current);
  99.                     Console.WriteLine("Successfully modified the Database!");
  100.                 }
  101.                 await DbContext.SaveChangesAsync();
  102.             }
  103.         }
  104.         public static async Task JoinHouse(ulong UserId, string HouseSet, string UserName)
  105.         {
  106.             using (var DbContext = new SQLiteDbContext())
  107.             {
  108.                 if (DbContext.Users.Where(x => x.UserId == UserId).Count() < 1)
  109.                 {
  110.                     //Create a User in the DB
  111.                     DbContext.Users.Add(new User
  112.                     {
  113.                         UserId = UserId,
  114.                         Money = 100,
  115.                         Username = UserName,
  116.                         House = HouseSet
  117.                     });
  118.  
  119.                     DbContext.ItemUsers.Add(new ItemUser
  120.                     {
  121.                         User_Id = UserId,
  122.                         Item_Id = 1,
  123.                         Amount = 1
  124.                     });
  125.                 }
  126.                 else
  127.                 {
  128.                     User Current = DbContext.Users.Where(x => x.UserId == UserId).FirstOrDefault();
  129.                     Current.House = HouseSet;
  130.                     DbContext.Users.Update(Current);
  131.                     Console.WriteLine("Successfully modified the Database!");
  132.                 }
  133.                 await DbContext.SaveChangesAsync();
  134.             }
  135.         }
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement