public static async Task UpdateUBs(int id) { var collec = DatabaseConnection.GetDb().GetCollection <AvastarObject>("AvastarCollection"); var ava = (await collec.FindAsync(a => a.id == id)).FirstOrDefault(); var ub2List = await UB2Object.UpdateUb2List(ava); var ub3List = await UB3Object.UpdateUb3List(ava); await AvaUBObject.UpdateAvaUbList(ava, ub2List, ub3List); }
public static async Task CreateAva(int id) { var ava = new AvastarObject(); ava.id = id; string metadatastr = ""; using (System.Net.WebClient wc = new System.Net.WebClient()) { try { metadatastr = await wc.DownloadStringTaskAsync("https://avastars.io/metadata/" + id.ToString()); } catch (Exception e) { Console.WriteLine(e.Message); } } if (metadatastr.StartsWith("Invalid")) { return; } var traitJson = JObject.Parse(DiscordKeyGetter.GetFileData("app/create-traits-nosvg.json")); var metadataJson = JObject.Parse(metadatastr); ava.id = id; ava.Gender = (string)metadataJson["attributes"][0]["value"]; ava.Score = (int)metadataJson["attributes"][5]["value"]; ava.traits = new Dictionary <string, string>(); for (int i = 7; i < 19; i++) { ava.traits.Add((string)metadataJson["attributes"][i]["trait_type"], (string)metadataJson["attributes"][i]["value"]); } var disp = AvastarCommands.ReturnTraitDisparity(metadataJson, traitJson); ava.Owner = ""; ava.RarityDistribution = new Dictionary <string, int>(); ava.RarityDistribution.Add("Common", disp[0]); ava.RarityDistribution.Add("Uncommon", disp[1]); ava.RarityDistribution.Add("Rare", disp[2]); ava.RarityDistribution.Add("Epic", disp[3]); ava.RarityDistribution.Add("Legendary", disp[4]); ava.FillTraitDictionary(traitJson, metadataJson); var collec = DatabaseConnection.GetDb().GetCollection <AvastarObject>("AvastarCollection"); await collec.InsertOneAsync(ava); var ub2List = await UB2Object.UpdateUb2List(ava); var ub3List = await UB3Object.UpdateUb3List(ava); await AvaUBObject.UpdateAvaUbList(ava, ub2List, ub3List); }
public static async Task UpdateAvaUbList(AvastarObject ava, List <UB2Object> ub2List, List <UB3Object> ub3List) { var ub2Copy = ub2List; var ub3Copy = ub3List; var avaList = await AvastarObject.GetSeriesList(); var avaUbList = await GetAvaUbList(); var gender = ava.Gender; //Computuing UB2s var ub2Many = ub2List.Where(ub2 => ub2.Match.Count > 1).ToList(); ub2List = ub2List.Where(ub2 => ub2.Match.Count == 1).ToList(); foreach (var ubs in ub2Many) { bool uniqueGender = true; foreach (var token in ubs.Match) { if (token != ava.id && avaList.Where(a => a.id == token).FirstOrDefault().Gender == gender) { uniqueGender = false; break; } } if (uniqueGender) { ub2List.Add(ubs); } } var avaUbObj = new AvaUBObject(ava.id, ub2List); var collec = DatabaseConnection.GetDb().GetCollection <AvaUBObject>("AvaUbCollection"); var avaUbObjFromCollec = await(await collec.FindAsync(auo => auo.id == ava.id)).FirstOrDefaultAsync(); if (avaUbObjFromCollec == null) { await collec.InsertOneAsync(avaUbObj); } else { await collec.FindOneAndReplaceAsync(auo => auo.id == ava.id, avaUbObj); } avaUbList.Add(avaUbObj); foreach (var ub in ub2List) { ub2Copy.Remove(ub); } // ub2Copy contains all combos that are duplicates // get all ID in each combo, find only the ones that are same gender // access avaubdata and remove ID of combo try { foreach (var ub in ub2Copy) { foreach (var id in ub.Match) { var tempAva = avaList.Where(a => a.id == id).FirstOrDefault(); if (tempAva == null) { continue; } if (tempAva.Gender == ava.Gender) { var tempAvaUb = avaUbList.Where(a => a.id == id).FirstOrDefault(); if (tempAvaUb == null) { continue; } if (tempAvaUb.ub2List.Remove(ub.id.ToString())) { var update = Builders <AvaUBObject> .Update.Set(a => a.ub2List, tempAvaUb.ub2List); await collec.UpdateOneAsync(a => a.id == tempAvaUb.id, update); } } } } } catch (Exception e) { Console.WriteLine(e.Message); } //Computing UB3s try { var ub3Many = ub3List.Where(ub3 => ub3.Match.Count > 1).ToList(); ub3List = ub3List.Where(ub3 => ub3.Match.Count == 1).ToList(); foreach (var ubs in ub3Many) { bool uniqueGender = true; foreach (var token in ubs.Match) { if (token != ava.id && avaList.Where(a => a.id == token).FirstOrDefault().Gender == gender) { uniqueGender = false; break; } } if (uniqueGender) { ub3List.Add(ubs); } } } catch (Exception e) { Console.WriteLine(e.Message); } avaUbObj.ub3List = ub3List.Select(a => a.id.ToString()).ToList(); var ub3Update = Builders <AvaUBObject> .Update.Set(a => a.ub3List, avaUbObj.ub3List); await collec.UpdateOneAsync(a => a.id == ava.id, ub3Update); foreach (var ub in ub3List) { ub3Copy.Remove(ub); } foreach (var ub in ub3Copy) { foreach (var id in ub.Match) { var tempAva = avaList.Where(a => a.id == id).FirstOrDefault(); if (tempAva == null) { continue; } if (tempAva.Gender == ava.Gender) { var tempAvaUb = avaUbList.Where(a => a.id == id).FirstOrDefault(); if (tempAvaUb == null) { continue; } if (tempAvaUb.ub3List.Remove(ub.id.ToString())) { var update = Builders <AvaUBObject> .Update.Set(a => a.ub3List, tempAvaUb.ub3List); await collec.UpdateOneAsync(a => a.id == tempAvaUb.id, update); } } } } }