22
33namespace App \Models ;
44
5+ use Exception ;
56use Illuminate \Database \Eloquent \Factories \HasFactory ;
67use Illuminate \Database \Eloquent \Model ;
8+ use Illuminate \Support \Facades \Log ;
79use Illuminate \Support \Str ;
810
911class GameSave extends Model
@@ -68,78 +70,120 @@ public function user()
6870
6971 public function getPlayerData ($ key_name = null )
7072 {
71- // Explode the player data into an array on each return new line character
72- $ playerDataLines = explode ("\r\n" , $ this ->player );
73- $ playerData = [];
74- foreach ($ playerDataLines as $ line ) {
75- $ line = explode ('| ' , $ line );
76- $ playerData [ucfirst ($ line [0 ])] = $ line [1 ];
77- }
73+ try {
74+ // Explode the player data into an array on each return new line character
75+ $ playerDataLines = explode ("\r\n" , $ this ->player );
76+ $ playerData = [];
77+ foreach ($ playerDataLines as $ line ) {
78+ $ line = explode ('| ' , $ line );
79+ $ playerData [ucfirst ($ line [0 ])] = $ line [1 ];
80+ }
7881
79- return $ playerData [$ key_name ] ?? $ playerData ;
82+ return $ playerData [$ key_name ] ?? $ playerData ;
83+ } catch (Exception $ e ) {
84+ // If there is an error, return an empty array and log the error
85+ Log::error ($ e ->getMessage ());
86+
87+ return [];
88+ }
8089 }
8190
8291 public function getPlayerDataDetails (): array
8392 {
84- $ details = [];
85- $ allowed_details = ['Name ' , 'RivalName ' , 'Location ' , 'Money ' , 'HasPokedex ' , 'HasPokegear ' , 'SaveCreated ' , 'Gender ' , 'OT ' , 'Points ' , 'GTSStars ' ];
86- foreach ($ allowed_details as $ detail ) {
87- if ($ detail === 'HasPokedex ' or $ detail === 'HasPokegear ' ) {
88- $ details [$ detail ] = $ this ->getPlayerData ($ detail ) === '1 ' ? trans ('Yes ' ) : trans ('No ' );
93+ try {
94+ $ details = [];
95+ $ allowed_details = ['Name ' , 'RivalName ' , 'Location ' , 'Money ' , 'HasPokedex ' , 'HasPokegear ' , 'SaveCreated ' , 'Gender ' , 'OT ' , 'Points ' , 'GTSStars ' ];
96+ foreach ($ allowed_details as $ detail ) {
97+ if ($ detail === 'HasPokedex ' or $ detail === 'HasPokegear ' ) {
98+ $ details [$ detail ] = $ this ->getPlayerData ($ detail ) === '1 ' ? trans ('Yes ' ) : trans ('No ' );
8999
90- continue ;
100+ continue ;
101+ }
102+ $ details [$ detail ] = $ this ->getPlayerData ($ detail );
91103 }
92- $ details [$ detail ] = $ this ->getPlayerData ($ detail );
93- }
94104
95- return $ details ;
105+ return $ details ;
106+ } catch (Exception $ e ) {
107+ // If there is an error, return an empty array and log the error
108+ Log::error ($ e ->getMessage ());
109+
110+ return [];
111+ }
96112 }
97113
98114 public function getAchievements (): array
99115 {
100- $ earnedAchievements = $ this ->getPlayerData ('EarnedAchievements ' );
116+ try {
117+ $ earnedAchievements = $ this ->getPlayerData ('EarnedAchievements ' );
118+
119+ return explode (', ' , $ earnedAchievements );
120+ } catch (Exception $ e ) {
121+ // If there is an error, return an empty array and log the error
122+ Log::error ($ e ->getMessage ());
101123
102- return explode (', ' , $ earnedAchievements );
124+ return [];
125+ }
103126 }
104127
105128 public function getPokedex (): array
106129 {
107- $ pokedex = $ this ->pokedex ;
108- $ pokedex = explode ("\r\n" , $ pokedex );
109- $ pokedex = array_filter ($ pokedex );
110-
111- return array_map (function ($ item ) {
112- $ item = explode ('| ' , $ item );
113- $ id = str_replace ('{ ' , '' , $ item [0 ]);
114- $ status = (int ) $ item [1 ];
115-
116- return [
117- 'id ' => $ id ,
118- 'name ' => $ this ->getPokemonName ($ id ),
119- 'seen ' => $ status >= 1 ,
120- 'caught ' => $ status >= 2 ,
121- ];
122- }, $ pokedex );
130+ try {
131+ $ pokedex = $ this ->pokedex ;
132+ $ pokedex = explode ("\r\n" , $ pokedex );
133+ $ pokedex = array_filter ($ pokedex );
134+
135+ return array_map (function ($ item ) {
136+ $ item = explode ('| ' , $ item );
137+ $ id = str_replace ('{ ' , '' , $ item [0 ]);
138+ $ status = (int ) $ item [1 ];
139+
140+ return [
141+ 'id ' => $ id ,
142+ 'name ' => $ this ->getPokemonName ($ id ),
143+ 'seen ' => $ status >= 1 ,
144+ 'caught ' => $ status >= 2 ,
145+ ];
146+ }, $ pokedex );
147+ } catch (Exception $ e ) {
148+ // If there is an error, return an empty array and log the error
149+ Log::error ($ e ->getMessage ());
150+
151+ return [];
152+ }
123153 }
124154
125155 // Get all seen pokemon in pokedex
126156 public function getSeenPokemon (): array
127157 {
128- $ pokedex = $ this ->getPokedex ();
158+ try {
159+ $ pokedex = $ this ->getPokedex ();
129160
130- return array_filter ($ pokedex , function ($ item ) {
131- return $ item ['seen ' ];
132- });
161+ return array_filter ($ pokedex , function ($ item ) {
162+ return $ item ['seen ' ];
163+ });
164+ } catch (Exception $ e ) {
165+ // If there is an error, return an empty array and log the error
166+ Log::error ($ e ->getMessage ());
167+
168+ return [];
169+ }
133170 }
134171
135172 // Get all caught pokemon in pokedex
136173 public function getCaughtPokemon (): array
137174 {
138- $ pokedex = $ this ->getPokedex ();
175+ try {
176+ $ pokedex = $ this ->getPokedex ();
139177
140- return array_filter ($ pokedex , function ($ item ) {
141- return $ item ['caught ' ];
142- });
178+ return array_filter ($ pokedex , function ($ item ) {
179+ return $ item ['caught ' ];
180+ });
181+ } catch (Exception $ e ) {
182+ // If there is an error, return an empty array and log the error
183+ Log::error ($ e ->getMessage ());
184+
185+ return [];
186+ }
143187 }
144188
145189 // Get count of seen pokemon in pokedex
@@ -157,77 +201,91 @@ public function getCaughtPokemonCount(): int
157201 // Get statistics
158202 public function getStatistics (): array
159203 {
160- $ statistics = $ this ->statistics ;
161- $ statistics = explode ("\r\n" , $ statistics );
162- $ statistics = array_filter ($ statistics );
163-
164- return array_map (function ($ item ) {
165- $ item = explode (', ' , $ item );
166- $ name = str_replace ('{ ' , '' , $ item [0 ]);
167- // Remove [ and ] and some random number between from the name
168- $ name = preg_replace ('/\[[0-9]+\]/ ' , '' , $ name );
169- $ number = format ('locale-number ' , $ item [1 ]);
170- // Remove the last three characters to remove the .00
171- $ number = substr ($ number , 0 , -3 );
172-
173- return [
174- 'name ' => $ name ,
175- 'value ' => $ number ,
176- ];
177- }, $ statistics );
204+ try {
205+ $ statistics = $ this ->statistics ;
206+ $ statistics = explode ("\r\n" , $ statistics );
207+ $ statistics = array_filter ($ statistics );
208+
209+ return array_map (function ($ item ) {
210+ $ item = explode (', ' , $ item );
211+ $ name = str_replace ('{ ' , '' , $ item [0 ]);
212+ // Remove [ and ] and some random number between from the name
213+ $ name = preg_replace ('/\[[0-9]+\]/ ' , '' , $ name );
214+ $ number = format ('locale-number ' , $ item [1 ]);
215+ // Remove the last three characters to remove the .00
216+ $ number = substr ($ number , 0 , -3 );
217+
218+ return [
219+ 'name ' => $ name ,
220+ 'value ' => $ number ,
221+ ];
222+ }, $ statistics );
223+ } catch (Exception $ e ) {
224+ // If there is an error, return an empty array and log the error
225+ Log::error ($ e ->getMessage ());
226+
227+ return [];
228+ }
178229 }
179230
180231 // Get pokemon in party
181232 public function getParty (): array
182233 {
183- $ party = $ this ->party ;
184- $ party = explode ("\r\n" , $ party );
185- // Split values for party entry into an array
186- $ party = array_map (function ($ item ) {
187- return explode ('}{ ' , $ item );
188- }, $ party );
189- // For each party entry; get the properties and add it to the pokemon in array
190- $ party = array_map (function ($ item ) {
191- $ pokemon = [];
192- $ private_keys = ['IDValue ' ];
193- foreach ($ item as $ property ) {
194- $ property = explode ('"[ ' , $ property );
195- $ key = str_replace ('{ ' , '' , str_replace ('" ' , '' , $ property [0 ]));
196- $ value = str_replace ('] ' , '' , str_replace ('} ' , '' , $ property [1 ]));
197- if (in_array ($ key , $ private_keys )) {
198- continue ;
199- }
200- if ($ key == 'Experience ' ) {
201- $ value = substr (format ('locale-number ' , $ value ), 0 , -3 );
202- }
203- if ($ key == 'Nature ' ) {
204- $ value = $ this ->getNature ($ value );
205- }
206- if ($ key == 'Ability ' ) {
207- $ value = $ this ->getAbility ($ value );
234+ try {
235+ $ party = $ this ->party ;
236+ $ party = explode ("\r\n" , $ party );
237+ // Split values for party entry into an array
238+ $ party = array_map (function ($ item ) {
239+ return explode ('}{ ' , $ item );
240+ }, $ party );
241+ // For each party entry; get the properties and add it to the pokemon in array
242+ $ party = array_map (function ($ item ) {
243+ $ pokemon = [];
244+ $ private_keys = ['IDValue ' ];
245+ foreach ($ item as $ property ) {
246+ $ property = explode ('"[ ' , $ property );
247+ $ key = str_replace ('{ ' , '' , str_replace ('" ' , '' , $ property [0 ]));
248+ $ value = str_replace ('] ' , '' , str_replace ('} ' , '' , $ property [1 ]));
249+ if (in_array ($ key , $ private_keys )) {
250+ continue ;
251+ }
252+ if ($ key == 'Experience ' ) {
253+ $ value = substr (format ('locale-number ' , $ value ), 0 , -3 );
254+ }
255+ if ($ key == 'Nature ' ) {
256+ $ value = $ this ->getNature ($ value );
257+ }
258+ if ($ key == 'Ability ' ) {
259+ $ value = $ this ->getAbility ($ value );
260+ }
261+ if ($ key == 'Pokemon ' ) {
262+ $ pokemon ['PokemonName ' ] = $ this ->getPokemonName ($ value );
263+ }
264+ $ pokemon [$ key ] = $ value ;
208265 }
209- if ($ key == 'Pokemon ' ) {
210- $ pokemon ['PokemonName ' ] = $ this ->getPokemonName ($ value );
211- }
212- $ pokemon [$ key ] = $ value ;
213- }
214- $ url = $ pokemon ['EggSteps ' ] > 0 ? 'https://raw.githubusercontent.com/P3D-Legacy/P3D-Legacy/master/P3D/Content/Pokemon/Egg/Egg_front.png ' : 'https://raw.githubusercontent.com/P3D-Legacy/P3D-Legacy/master/P3D/Content/Pokemon/Sprites/ ' .$ pokemon ['Pokemon ' ].'.png ' ;
215- $ image = imagecrop (imagecreatefromstring (file_get_contents ($ url )), [
216- 'x ' => 0 ,
217- 'y ' => $ pokemon ['isShiny ' ] ? 96 : 0 ,
218- 'width ' => 96 ,
219- 'height ' => 96 ,
220- ]);
221- ob_start (); // Let's start output buffering
222- imagejpeg ($ image ); // This will normally output the image, but because of ob_start(), it won't
223- $ contents = ob_get_contents (); // Instead, output above is saved to $contents
224- ob_end_clean (); // End the output buffer
225- $ pokemon ['Image ' ] = 'data:image/png;base64, ' .base64_encode ($ contents );
226-
227- return $ pokemon ;
228- }, $ party );
229-
230- return $ party ;
266+ $ url = $ pokemon ['EggSteps ' ] > 0 ? 'https://raw.githubusercontent.com/P3D-Legacy/P3D-Legacy/master/P3D/Content/Pokemon/Egg/Egg_front.png ' : 'https://raw.githubusercontent.com/P3D-Legacy/P3D-Legacy/master/P3D/Content/Pokemon/Sprites/ ' .$ pokemon ['Pokemon ' ].'.png ' ;
267+ $ image = imagecrop (imagecreatefromstring (file_get_contents ($ url )), [
268+ 'x ' => 0 ,
269+ 'y ' => $ pokemon ['isShiny ' ] ? 96 : 0 ,
270+ 'width ' => 96 ,
271+ 'height ' => 96 ,
272+ ]);
273+ ob_start (); // Let's start output buffering
274+ imagejpeg ($ image ); // This will normally output the image, but because of ob_start(), it won't
275+ $ contents = ob_get_contents (); // Instead, output above is saved to $contents
276+ ob_end_clean (); // End the output buffer
277+ $ pokemon ['Image ' ] = 'data:image/png;base64, ' .base64_encode ($ contents );
278+
279+ return $ pokemon ;
280+ }, $ party );
281+
282+ return $ party ;
283+ } catch (Exception $ e ) {
284+ // If there is an error, return an empty array and log the error
285+ Log::error ($ e ->getMessage ());
286+
287+ return [];
288+ }
231289 }
232290
233291 // Get nature from int
@@ -465,19 +523,26 @@ public function getAbility($abilityInt): string
465523 // Get the pokemon name from id
466524 public function getPokemonName ($ id ): string
467525 {
468- // convert string id to int id
469- $ id = (int ) $ id ;
470- // get the language file path
471- $ filepath = lang_path ().'/pokemon_ ' .app ()->getLocale ().'.json ' ;
472- // if the file doesn't exist, use the default language
473- if (! file_exists ($ filepath )) {
474- $ filepath = lang_path ().'/pokemon_en.json ' ;
475- }
476- // load pokemon names from json file in the lang folder
477- $ pokemon_names = json_decode (file_get_contents ($ filepath ), true );
478- // get the pokemon name by the id key in json file
479- $ pokemon_names = collect ($ pokemon_names );
526+ try {
527+ // convert string id to int id
528+ $ id = (int ) $ id ;
529+ // get the language file path
530+ $ filepath = lang_path ().'/pokemon_ ' .app ()->getLocale ().'.json ' ;
531+ // if the file doesn't exist, use the default language
532+ if (! file_exists ($ filepath )) {
533+ $ filepath = lang_path ().'/pokemon_en.json ' ;
534+ }
535+ // load pokemon names from json file in the lang folder
536+ $ pokemon_names = json_decode (file_get_contents ($ filepath ), true );
537+ // get the pokemon name by the id key in json file
538+ $ pokemon_names = collect ($ pokemon_names );
539+
540+ return isset ($ pokemon_names ->get ($ id - 1 )['name ' ]) ? $ pokemon_names ->get ($ id - 1 )['name ' ] : '??? ' ;
541+ } catch (Exception $ e ) {
542+ // If there is an error, return an empty array and log the error
543+ Log::error ($ e ->getMessage ());
480544
481- return isset ($ pokemon_names ->get ($ id - 1 )['name ' ]) ? $ pokemon_names ->get ($ id - 1 )['name ' ] : '??? ' ;
545+ return '??? ' ;
546+ }
482547 }
483548}
0 commit comments