This repository has been archived by the owner on Mar 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBInitializer.cs
741 lines (615 loc) · 24.7 KB
/
DBInitializer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
#region
using pkmnWildLife.Data;
using PokeApiNet;
using Ability = PokeApiNet.Ability;
using Item = PokeApiNet.Item;
using Move = PokeApiNet.Move;
using Pokemon = PokeApiNet.Pokemon;
using Type = PokeApiNet.Type;
#endregion
namespace pkmnWildLife;
public class DBInitializer
{
public static async Task InitializeDB(ApplicationDbContext context)
{
var apiclient = new PokeApiClient();
await TransferTypes(context, apiclient);
await TransferMoveClasses(context, apiclient);
await TransferMoves(context, apiclient);
await TransferTraits(context);
await TransferAbilities(context, apiclient);
await TransferMon(context, apiclient);
// await TransferItems(context, apiclient);
await TransferLearnsets(context, apiclient);
}
private static async Task TransferTypes(ApplicationDbContext context, PokeApiClient apiclient)
{
if (context.Types.Any())
{
Console.WriteLine("There already are Types");
return;
}
var tps = new List<Data.Type>();
await foreach (var TypeR in apiclient.GetAllNamedResourcesAsync<Type>())
{
var Type = await apiclient.GetResourceAsync(TypeR);
tps.Add(
new Data.Type
{
ID = Type.Name,
Name = Type.Names.FirstOrDefault(n => n.Language.Name == "en").Name,
Name_DE = Type.Names.FirstOrDefault(n => n.Language.Name == "de")?.Name
}
);
}
context.AddRange(tps);
await context.SaveChangesAsync();
var relations = new List<DamageRelations>();
foreach (var t in context.Types)
{
var Type = await apiclient.GetResourceAsync<Type>(t.ID);
var doublefrom = new List<Data.Type>();
foreach (var df in Type.DamageRelations.DoubleDamageFrom)
doublefrom.Add(context.Types.FirstOrDefault(d => d.ID == df.Name));
var doubleto = new List<Data.Type>();
foreach (var df in Type.DamageRelations.DoubleDamageTo)
doubleto.Add(context.Types.FirstOrDefault(d => d.ID == df.Name));
var halfto = new List<Data.Type>();
foreach (var df in Type.DamageRelations.HalfDamageTo)
halfto.Add(context.Types.FirstOrDefault(d => d.ID == df.Name));
var halffrom = new List<Data.Type>();
foreach (var df in Type.DamageRelations.HalfDamageFrom)
halffrom.Add(context.Types.FirstOrDefault(d => d.ID == df.Name));
var noto = new List<Data.Type>();
foreach (var df in Type.DamageRelations.NoDamageTo)
noto.Add(context.Types.FirstOrDefault(d => d.ID == df.Name));
var nofrom = new List<Data.Type>();
foreach (var df in Type.DamageRelations.NoDamageFrom)
nofrom.Add(context.Types.FirstOrDefault(d => d.ID == df.Name));
var dr = new DamageRelations();
relations.Add(
new DamageRelations
{
ID = $"relations_{t.ID}",
Type = t,
doubleDamageFrom = doublefrom,
doubleDamageTo = doubleto,
halfDamageFrom = halffrom,
halfDamageTo = halfto,
noDamageFrom = nofrom,
noDamageTo = noto
}
);
}
context.AddRange(relations);
await context.SaveChangesAsync();
}
private static async Task TransferMoveClasses(
ApplicationDbContext context,
PokeApiClient apiclient
)
{
if (context.MoveClasses.Any())
{
Console.WriteLine("There already are MoveClasses");
return;
}
var movecls = new List<MoveClass>();
await foreach (var mc in apiclient.GetAllNamedResourcesAsync<MoveDamageClass>())
{
var m = await apiclient.GetResourceAsync(mc);
movecls.Add(
new MoveClass
{
ID = m.Name,
Name = m.Names.FirstOrDefault(n => n.Language.Name == "en").Name,
Name_DE = m.Names.FirstOrDefault(n => n.Language.Name == "de")?.Name
}
);
Console.WriteLine(m.Name);
}
context.AddRange(movecls);
await context.SaveChangesAsync();
}
private static async Task TransferItems(ApplicationDbContext context, PokeApiClient apiclient)
{
if (context.Items.Any())
{
Console.WriteLine("There already are Items");
return;
}
var itms = new List<Data.Item>();
await foreach (var i in apiclient.GetAllNamedResourcesAsync<Item>())
{
var Item = await apiclient.GetResourceAsync(i);
string[] undesirables =
{
"dynamax-crystals",
"sandwhich-ingredients",
"tm-materials",
"picnic",
"species-candies",
"all-machines",
"all-mail",
"plot-advancement"
};
if (undesirables.Contains(Item.Category.Name))
continue;
var ID = $"{Item.Name}_{Item.Id}";
var Name =
Item.Names.FirstOrDefault(n => n.Language.Name == "en") != null
? Item.Names.FirstOrDefault(n => n.Language.Name == "en").Name
: Item.Name;
var Name_DE = Item.Names.FirstOrDefault(n => n.Language.Name == "de")?.Name;
var Effect =
Item.EffectEntries.FirstOrDefault(n => n.Language.Name == "de") != null
? Item.EffectEntries.FirstOrDefault(n => n.Language.Name == "de").Effect
: Item.EffectEntries.FirstOrDefault(n => n.Language.Name == "en") != null
? Item.EffectEntries.FirstOrDefault(n => n.Language.Name == "en").Effect
: "No Data";
itms.Add(
new Data.Item
{
ID = ID,
Name = Name,
Name_DE = Name_DE,
Effect = Effect
}
);
}
context.AddRange(itms);
await context.SaveChangesAsync();
}
private static async Task TransferAbilities(
ApplicationDbContext context,
PokeApiClient apiclient
)
{
if (context.AbilityDex.Where(m => !m.IsTrait).Any())
{
Console.WriteLine("There already are Abilities");
return;
}
var abs = new List<Data.Ability>();
var it = 0;
await foreach (var i in apiclient.GetAllNamedResourcesAsync<Ability>())
{
var Item = await apiclient.GetResourceAsync(i);
var ID = Item.Name;
var Name =
Item.Names.FirstOrDefault(n => n.Language.Name == "en") != null
? Item.Names.FirstOrDefault(n => n.Language.Name == "en").Name
: Item.Name;
var Name_DE = Item.Names.FirstOrDefault(n => n.Language.Name == "de")?.Name;
var Effect =
Item.EffectEntries.FirstOrDefault(n => n.Language.Name == "en") != null
? Item.EffectEntries.FirstOrDefault(n => n.Language.Name == "en").Effect
: Item.FlavorTextEntries.FirstOrDefault(n => n.Language.Name == "en") != null
? Item
.FlavorTextEntries.FirstOrDefault(n => n.Language.Name == "en")
.FlavorText
: "No Entry";
var ShortEffect =
Item.EffectEntries.FirstOrDefault(n => n.Language.Name == "en") != null
? Item.EffectEntries.FirstOrDefault(n => n.Language.Name == "en").ShortEffect
: Item.FlavorTextEntries.FirstOrDefault(n => n.Language.Name == "en") != null
? Item
.FlavorTextEntries.FirstOrDefault(n => n.Language.Name == "en")
.FlavorText
: "No Entry";
// var moves_old = Helpers.csv2ab("abilities_old.csv");
/* if (moves_old.Where(mn => mn.ability == Name).Any())
{
var mn = moves_old.Where(m => m.ability == Name).FirstOrDefault();
Effect = mn.effect;
}
*/
abs.Add(
new Data.Ability
{
ID = ID,
Name = Name,
Name_DE = Name_DE,
Effect = Effect,
ShortEffect = ShortEffect,
IsTrait = false
}
);
it++;
Console.WriteLine($"Ability {it}");
}
context.AddRange(abs);
await context.SaveChangesAsync();
}
private static async Task TransferTraits(ApplicationDbContext context)
{
if (context.AbilityDex.Where(m => m.IsTrait).Any())
{
Console.WriteLine("There already are Traits");
return;
}
var traits = Helpers.Csv2Trait("traits.csv");
var trs = new List<Data.Ability>();
foreach (var tr in traits)
trs.Add(
new Data.Ability
{
ID = tr.Name.ToLower().Normalize() + "_trait",
Effect = tr.effect,
Name = tr.Name,
Requirement = tr.Requirement,
IsTrait = true,
Order = tr.Requirement.Contains("Grade")
? int.Parse(tr.Requirement.Replace("Grade ", ""))
: 999
}
);
context.AddRange(trs);
await context.SaveChangesAsync();
}
private static async Task TransferMoves(ApplicationDbContext context, PokeApiClient apiclient)
{
if (context.MoveDex.Any())
{
Console.WriteLine("There already are Moves");
return;
}
var types = context.Types.ToList();
var mvs = new List<Data.Move>();
var dclass = context.MoveClasses.ToList();
var it = 0;
await foreach (var i in apiclient.GetAllNamedResourcesAsync<Move>())
{
var m = await apiclient.GetResourceAsync(i);
var ID = m.Name;
var Name =
m.Names.FirstOrDefault(n => n.Language.Name == "en") != null
? m.Names.FirstOrDefault(n => n.Language.Name == "en").Name
: m.Name;
var Name_DE = m.Names.FirstOrDefault(n => n.Language.Name == "de")?.Name;
var Effect =
m.EffectEntries.FirstOrDefault(n => n.Language.Name == "de") != null
? m.EffectEntries.FirstOrDefault(n => n.Language.Name == "de").Effect
: m.EffectEntries.FirstOrDefault(n => n.Language.Name == "en") != null
? m
.EffectEntries.FirstOrDefault(n => n.Language.Name == "en")
.Effect.Replace("1/16", "gradD4")
.Replace("1/8", "(2*Grad)D4")
: m.FlavorTextEntries.FirstOrDefault(n => n.Language.Name == "en") != null
? m
.FlavorTextEntries.FirstOrDefault(n => n.Language.Name == "en")
.FlavorText
: "No Data";
if (ID == "ingrain")
Effect +=
" \n während verwurzler aktiv ist, kannst du statt anzugreifen, dich deine normale bewegungsreichweite"
+ " bewegen ohne das verwurzler aufgehoben wird, rennen ist nicht möglich.";
var ShortEffect =
m.EffectEntries.FirstOrDefault(n => n.Language.Name == "de") != null
? m.EffectEntries.FirstOrDefault(n => n.Language.Name == "de").ShortEffect
: m.EffectEntries.FirstOrDefault(n => n.Language.Name == "en") != null
? m
.EffectEntries.FirstOrDefault(n => n.Language.Name == "en")
.ShortEffect.Replace("1/16", "gradD4")
.Replace("1/8", "(2*Grad)D4")
: m.FlavorTextEntries.FirstOrDefault(n => n.Language.Name == "en") != null
? m
.FlavorTextEntries.FirstOrDefault(n => n.Language.Name == "en")
.FlavorText
: "No Data";
if (!m.Priority.Equals(0))
{
var prio = $"\n\n Has a priority of {m.Priority}.";
ShortEffect += prio;
Effect += prio;
}
Effect = accuracy_string_en(m.Accuracy) + Effect;
ShortEffect = accuracy_string_en(m.Accuracy) + Effect;
var Target = m.Target.Name;
var DamageDice = StrengthToDice(m.Power, 0.8);
var MType = types.FirstOrDefault(e => e.ID == m.Type.Name);
var DamageClass = dclass.FirstOrDefault(e => e.ID == m.DamageClass.Name);
mvs.Add(
new Data.Move
{
ID = ID,
Name = Name,
Name_DE = Name_DE,
Effect = Effect,
ShortEffect = ShortEffect,
type = MType,
Target = Target,
DamageDice = DamageDice,
MoveClass = DamageClass
}
);
it++;
Console.WriteLine($"Move {it}");
}
context.AddRange(mvs);
await context.SaveChangesAsync();
}
private static async Task TransferMon(ApplicationDbContext context, PokeApiClient apiclient)
{
if (context.Pokedex.Any())
{
Console.WriteLine("There already are Pokemon in the Index");
return;
}
var abilities = context.AbilityDex.ToArray();
var types = context.Types.ToArray();
var pokes = new List<Data.Pokemon>();
var i = 0;
await foreach (var p in apiclient.GetAllNamedResourcesAsync<Pokemon>())
{
var poke = await apiclient.GetResourceAsync(p);
var species = apiclient.GetResourceAsync(poke.Species).Result;
var ID = poke.Name;
var Order = poke.Order;
var Dex = species.Order;
var Name = species.Names.FirstOrDefault(n => n.Language.Name == "en").Name;
var image = poke.Sprites.Other.OfficialArtwork.FrontDefault;
if (string.IsNullOrEmpty(image))
image = poke.Sprites.Other.Home.FrontDefault;
if (string.IsNullOrEmpty(image))
image = poke.Sprites.FrontDefault;
var Name_DE = species.Names.FirstOrDefault(n => n.Language.Name == "de")?.Name;
var form = apiclient
.GetResourceAsync(poke.Forms[0])
.Result.Names.FirstOrDefault(n => n.Language.Name == "en")
?.Name;
var form_de = apiclient
.GetResourceAsync(poke.Forms[0])
.Result.Names.FirstOrDefault(n => n.Language.Name == "de")
?.Name;
if (
form_de != null
&& (
form_de.Contains("Gigadynamax")
|| form_de.Contains("Totem")
|| ID.Contains("mimigma-busted")
|| ID.Contains("cramorant-")
|| ID.Contains("koraidon-")
|| ID.Contains("miraidon-")
)
)
continue;
if ((Name == "Pikachu") & !string.IsNullOrEmpty(form))
continue;
if (
form_de != null
&& (
Name_DE == "Kikugi"
|| form_de.Contains("Westliches")
|| form_de.Contains("Frühling")
|| ID == "flabebe"
|| ID == "floette"
|| ID == "florges"
|| Name_DE == "Xerneas"
|| ID == "sinistea"
|| ID == "poltageist"
|| ID == "alcremie"
)
)
{
form = "";
form_de = "";
}
if (Name_DE == Name)
Name_DE = null;
var Abilities = new List<Data.Ability>();
foreach (var a in poke.Abilities)
{
var ab = abilities.FirstOrDefault(w => w.ID == a.Ability.Name);
Abilities.Add(ab);
}
if (Name_DE == "Quajutsu")
Abilities.Add(abilities.FirstOrDefault(w => w.Name == "Battle Bond"));
if (ID.Contains("power-construct"))
continue;
if (ID.Contains("zygarde"))
Abilities.Add(abilities.FirstOrDefault(w => w.ID == "power-construct"));
if (ID.Contains("own-tempo"))
continue;
if (ID == "rockruff")
Abilities.Add(abilities.FirstOrDefault(w => w.ID == "own-tempo"));
if (ID == "minior-blue")
{
ID = "minior";
form_de = "";
form = "";
}
if (ID == "minior-blue-meteor")
{
ID = "minior-meteor";
form_de = "Meteno (Meteor)";
form = "Minior (Meteor)";
}
if (ID.Contains("minior-") & (ID != "minior-meteor"))
continue;
var Type1 = types.FirstOrDefault(w => w.ID == poke.Types[0].Type.Name);
var Type2 =
poke.Types.Count == 2
? types.FirstOrDefault(w => w.ID == poke.Types[1].Type.Name)
: null;
var HEALTH = StatToInt(poke.Stats[0].BaseStat, 2);
var isFOCKINGSPECIALMATE = species.IsLegendary || species.IsMythical;
var DEF_NERF = isFOCKINGSPECIALMATE ? 0.9 : 1;
var ATK_BUFF = isFOCKINGSPECIALMATE ? 1 : 1.1;
var ATK = StatToInt(poke.Stats[1].BaseStat, ATK_BUFF);
var DEF = StatToInt(poke.Stats[2].BaseStat, DEF_NERF) + (isFOCKINGSPECIALMATE ? 3 : 6);
var SP_ATK = StatToInt(poke.Stats[3].BaseStat, ATK_BUFF);
;
var SP_DEF =
StatToInt(poke.Stats[4].BaseStat, DEF_NERF) + (isFOCKINGSPECIALMATE ? 3 : 6);
var SPEED = StatToInt(poke.Stats[5].BaseStat);
;
pokes.Add(
new Data.Pokemon
{
ID = ID,
Dex = Dex,
Name = Name,
Name_DE = Name_DE,
ImageLink = image,
Form = form,
Form_DE = form_de,
Abilities = Abilities,
Type1 = Type1,
Type2 = Type2,
HEALTH = HEALTH,
ATK = ATK,
DEF = DEF,
SP_DEF = SP_DEF,
SP_ATK = SP_ATK,
SPEED = SPEED
}
);
i++;
Console.WriteLine($"pokemon {i}");
}
context.AddRange(pokes);
await context.SaveChangesAsync();
}
public static async Task TransferLearnsets(
ApplicationDbContext context,
PokeApiClient apiclient
)
{
if (context.Learnsets.Any())
{
Console.WriteLine("There already are learnsets");
return;
}
var moves_local = context.MoveDex.ToArray();
var pokemon = context.Pokedex.ToArray();
var p = 0;
foreach (var poke in pokemon)
{
var id = poke.ID;
if (id == "minior")
id = "minior-blue";
if (id == "minior-meteor")
id = "minior-blue-meteor";
var moves = apiclient.GetResourceAsync<Pokemon>(id).Result.Moves;
var learnset = new List<Learnset>();
var m_it = 0;
foreach (var m in moves)
{
var vergd = m.VersionGroupDetails;
var move = moves_local.FirstOrDefault(mv => mv.ID == m.Move.Name);
var meth_it = 0;
foreach (var vers in vergd)
{
var how = vers.MoveLearnMethod.Name;
var level = int.Parse(Math.Ceiling((double)vers.LevelLearnedAt / 5).ToString());
if (how != "level-up")
level = int.MaxValue;
var l = new Learnset
{
ID = Guid.NewGuid().ToString(),
move = move,
how = how,
level = level,
mon = poke
};
var checker = learnset.FirstOrDefault(l => l.move == move && l.how == how);
if (checker != null)
{
if (checker.level > level)
learnset.Remove(checker);
else
continue;
}
learnset.Add(l);
meth_it++;
Console.WriteLine(
$"MLP ({meth_it}/{vergd.Count()})/({m_it}/{moves.Count})/({p}/{pokemon.Length} {poke.Name})"
);
}
m_it++;
Console.WriteLine($"LP ({m_it}/{moves.Count})/({p}/{pokemon.Length} {poke.Name})");
}
context.AddRange(learnset);
p++;
Console.WriteLine($"P ({p}/{pokemon.Length} {poke.Name})");
await context.SaveChangesAsync();
}
}
public static int StatToInt(int stat, double? nerf = null)
{
var calc = stat;
if (nerf.HasValue)
calc = Convert.ToInt32(Math.Ceiling(nerf.Value * calc));
return Convert.ToInt32(calc * ((double)18 / 200));
}
public static string? StrengthToDice(int? strength, double? nerf = null)
{
if (!strength.HasValue)
return null;
var calc = strength.Value / 10;
if (nerf.HasValue)
calc = Convert.ToInt32(Math.Ceiling(nerf.Value * calc));
switch (calc)
{
case <= 3:
return "1d4";
case 4:
return "1d6";
case 5:
return "1d8";
case 6:
return "1d10";
case 7:
return "2d6";
case 8:
return "3d4";
case 9:
return "2d8";
case 10:
return "4d4";
case 11:
return "2d10";
case 12:
return "2d10";
case 13:
return "2d12";
case 14:
return "4d6";
case 15:
return "4d6";
case 16:
return "4d6";
case 17:
return "4d6";
case 18:
return "4d8";
case 19:
return "4d8";
case 20:
return "4d8";
case 21:
return "2d20";
case 22:
return "4d10";
case 23:
return "4d10";
case 24:
return "4d10";
case 25:
return "4d10";
default:
return "1d2";
}
}
public static string accuracy_string_en(int? accuracy)
{
if (accuracy.HasValue)
{
if (accuracy == 100)
return "";
return $"This move has an {accuracy}% chance to succeed. ";
}
return "";
}
}