1-
2- using Microsoft . AspNetCore . Http ;
3- using Microsoft . AspNetCore . Mvc ;
4- using CoreAPI . Helpers ;
5- using System . ComponentModel . DataAnnotations ;
6- using System . IO ;
7- using PKHeX . Core ;
8- using System ;
9- using CoreAPI . Models ;
10- using System . Linq ;
11-
12- namespace CoreAPI . Controllers
13- {
14- [ ApiController ]
15- public class PokemonInfoController : ControllerBase
16- {
17- // POST: api/PokemonInfo
18- [ HttpPost ]
19- [ Route ( "api/[controller]" ) ]
20- public PokemonSummary Post ( [ FromForm ] [ Required ] IFormFile pokemon , [ FromForm ] string generation )
21- {
22- using var memoryStream = new MemoryStream ( ) ;
23- pokemon . CopyTo ( memoryStream ) ;
24- byte [ ] data = memoryStream . ToArray ( ) ;
25- PKM pkm ;
26- try
27- {
1+
2+ using Microsoft . AspNetCore . Http ;
3+ using Microsoft . AspNetCore . Mvc ;
4+ using CoreAPI . Helpers ;
5+ using System . ComponentModel . DataAnnotations ;
6+ using System . IO ;
7+ using PKHeX . Core ;
8+ using System ;
9+ using CoreAPI . Models ;
10+ using System . Linq ;
11+
12+ namespace CoreAPI . Controllers
13+ {
14+ [ ApiController ]
15+ public class PokemonInfoController : ControllerBase
16+ {
17+ // POST: api/PokemonInfo
18+ [ HttpPost ]
19+ [ Route ( "api/[controller]" ) ]
20+ public PokemonSummary Post ( [ FromForm ] [ Required ] IFormFile pokemon , [ FromForm ] string generation )
21+ {
22+ using var memoryStream = new MemoryStream ( ) ;
23+ pokemon . CopyTo ( memoryStream ) ;
24+ byte [ ] data = memoryStream . ToArray ( ) ;
25+ PKM pkm ;
26+ try
27+ {
2828 if ( generation == "" || generation == null )
2929 {
3030 pkm = PKMConverter . GetPKMfromBytes ( data ) ;
3131 if ( pkm == null )
3232 {
3333 throw new System . ArgumentException ( "Bad data!" ) ;
34- }
35- }
34+ }
35+ generation = Utils . GetGeneration ( pkm ) ;
36+ }
3637 else
3738 {
39+ Console . WriteLine ( generation ) ;
3840 pkm = Utils . GetPKMwithGen ( generation , data ) ;
3941 if ( pkm == null )
4042 {
4143 throw new System . ArgumentException ( "Bad generation!" ) ;
4244 }
43- }
44- PokemonSummary PS = new PokemonSummary ( pkm , GameInfo . Strings ) ;
45- return PS ;
46- }
47- catch
48- {
49- return null ;
50- }
51- }
52- // POST: api/BasePokemon
53- [ HttpPost ]
54- [ Route ( "api/BasePokemon" ) ]
55- public BasePokemon BasePokemon ( [ FromForm ] [ Required ] string pokemon , [ FromForm ] string form )
56- {
57- if ( ! Enum . GetNames ( typeof ( Species ) ) . Any ( s => s . ToLower ( ) == pokemon ) )
58- {
59- Response . StatusCode = 400 ;
60- return null ;
61- }
62- try
63- {
64- Species s = ( Species ) Enum . Parse ( typeof ( Species ) , pokemon , true ) ;
65- var formNum = 0 ;
66- if ( form != null )
67- {
68- var forms = FormConverter . GetFormList ( ( int ) s , GameInfo . Strings . Types , GameInfo . Strings . forms , GameInfo . GenderSymbolASCII , 8 ) ;
69- formNum = StringUtil . FindIndexIgnoreCase ( forms , form ) ;
70- if ( formNum < 0 || formNum >= forms . Length )
71- {
72- Response . StatusCode = 400 ;
73- return null ;
74- }
75- }
76-
77-
78-
79-
80- var bp = Utils . GetBasePokemon ( ( int ) s , formNum ) ;
81- return bp ;
82- }
83- catch
84- {
85- Response . StatusCode = 400 ;
86- return null ;
87- }
88- }
89- // POST: api/GetForms
90- [ HttpPost ]
91- [ Route ( "api/PokemonForms" ) ]
92- public string [ ] GetPokemonForms ( [ FromForm ] [ Required ] string pokemon )
93- {
94- if ( ! Enum . GetNames ( typeof ( Species ) ) . Any ( s => s . ToLower ( ) == pokemon ) )
95- {
96- Response . StatusCode = 400 ;
97- return null ;
98- }
99- try
100- {
101- Species s = ( Species ) Enum . Parse ( typeof ( Species ) , pokemon , true ) ;
102- Utils . GetFormList ( ( int ) s ) ;
103- return Utils . GetFormList ( ( int ) s ) ;
104- }
105- catch
106- {
107- return null ;
108- }
109- }
110- }
45+ }
46+ Console . WriteLine ( pkm . Species ) ;
47+ if ( ! Utils . PokemonExistsInGeneration ( generation , pkm . Species ) )
48+ {
49+ Response . StatusCode = 400 ;
50+ return null ;
51+ }
52+ PokemonSummary PS = new PokemonSummary ( pkm , GameInfo . Strings ) ;
53+ return PS ;
54+ }
55+ catch
56+ {
57+ return null ;
58+ }
59+ }
60+ // POST: api/BasePokemon
61+ [ HttpPost ]
62+ [ Route ( "api/BasePokemon" ) ]
63+ public BasePokemon BasePokemon ( [ FromForm ] [ Required ] string pokemon , [ FromForm ] string form )
64+ {
65+ if ( ! Enum . GetNames ( typeof ( Species ) ) . Any ( s => s . ToLower ( ) == pokemon ) )
66+ {
67+ Response . StatusCode = 400 ;
68+ return null ;
69+ }
70+ try
71+ {
72+ Species s = ( Species ) Enum . Parse ( typeof ( Species ) , pokemon , true ) ;
73+ var formNum = 0 ;
74+ if ( form != null )
75+ {
76+ var forms = FormConverter . GetFormList ( ( int ) s , GameInfo . Strings . Types , GameInfo . Strings . forms , GameInfo . GenderSymbolASCII , 8 ) ;
77+ formNum = StringUtil . FindIndexIgnoreCase ( forms , form ) ;
78+ if ( formNum < 0 || formNum >= forms . Length )
79+ {
80+ Response . StatusCode = 400 ;
81+ return null ;
82+ }
83+ }
84+
85+
86+
87+
88+ var bp = Utils . GetBasePokemon ( ( int ) s , formNum ) ;
89+ return bp ;
90+ }
91+ catch
92+ {
93+ Response . StatusCode = 400 ;
94+ return null ;
95+ }
96+ }
97+ // POST: api/GetForms
98+ [ HttpPost ]
99+ [ Route ( "api/PokemonForms" ) ]
100+ public string [ ] GetPokemonForms ( [ FromForm ] [ Required ] string pokemon )
101+ {
102+ if ( ! Enum . GetNames ( typeof ( Species ) ) . Any ( s => s . ToLower ( ) == pokemon ) )
103+ {
104+ Response . StatusCode = 400 ;
105+ return null ;
106+ }
107+ try
108+ {
109+ Species s = ( Species ) Enum . Parse ( typeof ( Species ) , pokemon , true ) ;
110+ Utils . GetFormList ( ( int ) s ) ;
111+ return Utils . GetFormList ( ( int ) s ) ;
112+ }
113+ catch
114+ {
115+ return null ;
116+ }
117+ }
118+ }
111119}
0 commit comments