From 85f5c8219fdd204c68fb288fd6810ae8bd5c7604 Mon Sep 17 00:00:00 2001 From: "Tim Malone (Mac)" Date: Tue, 27 Jun 2017 08:55:54 +1000 Subject: [PATCH 1/4] Resolve a couple of extra bugs that file locking introduced --- src/battles.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/battles.php b/src/battles.php index 7a2ddaf..7b4f34a 100755 --- a/src/battles.php +++ b/src/battles.php @@ -680,8 +680,6 @@ function slackemon_end_battle( $battle_hash, $reason, $user_id = USER_ID ) { // Tally up and apply the battle stats for the user function slackemon_complete_battle( $battle_result, $battle_hash, $user_id = USER_ID, $award_xp_to_user = true, $send_response_to_user = true ) { - $is_desktop = 'desktop' === slackemon_get_player_menu_mode( $user_id ); - // Get the battle data, including from a 'complete' battle in case a user has already run this function $battle_data = slackemon_get_battle_data( $battle_hash, true ); @@ -718,6 +716,7 @@ function slackemon_complete_battle( $battle_result, $battle_hash, $user_id = USE function slackemon_complete_battle_for_winner( $battle_data, $user_id, $award_xp_to_user ) { + $is_desktop = 'desktop' === slackemon_get_player_menu_mode( $user_id ); $pokemon_experience_message = ''; // What's the experience & effort points gained from the opponent's fainted Pokemon? @@ -1033,6 +1032,7 @@ function slackemon_complete_battle_for_winner( $battle_data, $user_id, $award_xp function slackemon_complete_battle_for_loser( $battle_data, $user_id, $award_xp_to_user ) { + $is_desktop = 'desktop' === slackemon_get_player_menu_mode( $user_id ); $pokemon_experience_message = ''; $battle_pokemon_by_ts = []; @@ -1317,7 +1317,7 @@ function slackemon_do_battle_move( $move_name, $battle_hash, $action, $first_mov // Update and save the battle data, relinquishing the lock on the battle file $battle_data->turn = $opponent_id; - slackemon_save_battle_data( $battle_data, $battle_hash, true ); + slackemon_save_battle_data( $battle_data, $battle_hash, 'battle', true ); // Notify the user $last_move_notice = 'You ' . $move_message; @@ -1671,10 +1671,14 @@ function slackemon_get_battle_attachments( $battle_hash, $user_id, $battle_stage $attachments = [ // Opponent's Pokemon - slackemon_get_battle_pokemon_attachment( $opponent_pokemon, $opponent_id, $battle_hash, 'opponent', $opponent_pretext ), + slackemon_get_battle_pokemon_attachment( + $opponent_pokemon, $opponent_id, $battle_hash, 'opponent', $opponent_pretext + ), // User's Pokemon - slackemon_get_battle_pokemon_attachment( $user_pokemon, $user_id, $battle_hash, 'user', $user_pretext ), + slackemon_get_battle_pokemon_attachment( + $user_pokemon, $user_id, $battle_hash, 'user', $user_pretext + ), // Last move notice (if applicable) ( From 15844634d8bc839bab3120d46ca8f135cccaa143 Mon Sep 17 00:00:00 2001 From: "Tim Malone (Mac)" Date: Tue, 27 Jun 2017 08:56:17 +1000 Subject: [PATCH 2/4] Update spawn and file locking default behaviour now that file locking is ready --- config.php | 8 +++----- src/spawns.php | 25 ------------------------- 2 files changed, 3 insertions(+), 30 deletions(-) diff --git a/config.php b/config.php index fde2791..f4cb9a2 100755 --- a/config.php +++ b/config.php @@ -110,9 +110,7 @@ define( 'SLACKEMON_FLEE_TIME_LIMIT', getenv( 'SLACKEMON_FLEE_TIME_LIMIT' ) ?: MINUTE_IN_SECONDS * 5 ); // Roughly how many chances there are of a spawn each hour. -// HOWEVER, because only one Pokemon will ever spawn at once, this is not a true chance and is *heavily* weighted -// by whatever the flee time limit is set to above. -define( 'SLACKEMON_HOURLY_SPAWN_RATE', getenv( 'SLACKEMON_HOURLY_SPAWN_RATE' ) ?: 20 ); +define( 'SLACKEMON_HOURLY_SPAWN_RATE', getenv( 'SLACKEMON_HOURLY_SPAWN_RATE' ) ?: 10 ); // At each spawn, the chance out of 100 of spawning an item instead of a Pokemon. define( 'SLACKEMON_ITEM_SPAWN_CHANCE', getenv( 'SLACKEMON_ITEM_SPAWN_CHANCE' ) ?: 5 ); @@ -183,10 +181,10 @@ define( $var, filter_var( getenv( $var ), FILTER_VALIDATE_BOOLEAN ) ?: false ); } -// In addition, 'file locking' is very rudimentary at the moment, so it's currently disabled by default. +// In addition, 'file locking' is new, so there's a variable that can be used to disable it if it causes issues define( 'SLACKEMON_ENABLE_FILE_LOCKING', - filter_var( getenv( 'SLACKEMON_ENABLE_FILE_LOCKING' ), FILTER_VALIDATE_BOOLEAN ) ?: false + filter_var( getenv( 'SLACKEMON_ENABLE_FILE_LOCKING' ), FILTER_VALIDATE_BOOLEAN ) ?: true ); /** diff --git a/src/spawns.php b/src/spawns.php index aa55d8a..6dd4884 100755 --- a/src/spawns.php +++ b/src/spawns.php @@ -7,15 +7,6 @@ function slackemon_maybe_spawn( $trigger = [] ) { - // Don't generate a new spawn while another is still active - $most_recent_spawn = slackemon_get_most_recent_spawn(); - if ( $most_recent_spawn && $most_recent_spawn->ts >= time() - SLACKEMON_FLEE_TIME_LIMIT ) { - slackemon_spawn_debug( 'Shouldn\'t spawn as last spawn was too recent, but will proceed for spawn debugging...' ); - if ( ! SLACKEMON_SPAWN_DEBUG ) { - return false; - } - } - $spawn_randomizer = random_int( 1, ceil( MINUTE_IN_SECONDS / SLACKEMON_HOURLY_SPAWN_RATE ) ); $should_spawn = 1 === $spawn_randomizer; @@ -33,22 +24,6 @@ function slackemon_maybe_spawn( $trigger = [] ) { } // Function slackemon_maybe_spawn -function slackemon_get_most_recent_spawn() { - global $data_folder; - - $spawns = slackemon_get_files_by_prefix( $data_folder . '/spawns/', 'store' ); - - if ( ! count( $spawns ) ) { - return false; - } - - $most_recent_spawn = array_pop( $spawns ); - - $data = slackemon_file_get_contents( $most_recent_spawn, 'store' ); - return json_decode( $data ); - -} // Function slackemon_get_most_recent_spawn - function slackemon_spawn( $trigger = [], $region = false, $timestamp = false, $pokedex_id = false ) { // Default region From 88b3ee23a1b1ec4600fca0639f631647d11e49ae Mon Sep 17 00:00:00 2001 From: "Tim Malone (Mac)" Date: Tue, 27 Jun 2017 08:56:27 +1000 Subject: [PATCH 3/4] Minor improvements to file lock debugging --- lib/filesystem.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/filesystem.php b/lib/filesystem.php index d94aa05..4e50bc9 100755 --- a/lib/filesystem.php +++ b/lib/filesystem.php @@ -770,8 +770,12 @@ function slackemon_lock_debug( $message, $force_debug = false ) { return; } - slackemon_error_log( $message ); + // Log a message, including the function that initially caused the lock. + // (We go to the 5th level due to this function, the lock function, the file reading function, + // and the player/battle data wrapping function). + $debug_backtrace = debug_backtrace( null, 5 ); + slackemon_error_log( $message . ' (' . $debug_backtrace[4]['function'] . ')' ); -} +} // Function slackemon_lock_debug // The end! From b362738f7e735dde2a08f4c6553e7797ee79c39e Mon Sep 17 00:00:00 2001 From: "Tim Malone (Mac)" Date: Tue, 27 Jun 2017 10:14:57 +1000 Subject: [PATCH 4/4] Revert "Minor: speed up battle move & catching waits slightly" - no longer needed now that AWS local cache augmentation is shipping This reverts commit cf2b7b4295a467ca54f632ee770e0728f52c1196. --- src/battles.php | 2 +- src/catching.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/battles.php b/src/battles.php index 7b4f34a..a1b25e0 100755 --- a/src/battles.php +++ b/src/battles.php @@ -1370,7 +1370,7 @@ function slackemon_do_battle_move( $move_name, $battle_hash, $action, $first_mov // If neither Pokemon hasn't fainted, go ahead and move! if ( $user_pokemon->hp && $opponent_pokemon->hp ) { - sleep( 1 ); // Wait before the computer moves... + sleep( 2 ); // Wait before the computer moves... // Before we move, should we flee? // This doubles the chance of staying compared to a standard catch, plus increases more depending on diff --git a/src/catching.php b/src/catching.php index a4343c6..911e677 100755 --- a/src/catching.php +++ b/src/catching.php @@ -33,7 +33,7 @@ function slackemon_get_catch_message( $spawn_ts, $action, $from_battle = false, if ( 'catch' === $force_battle_result ) { // Don't wait here - it's obvious from a battle ending that this is going to be a catch } else { - sleep( 3 ); + sleep( 5 ); } } @@ -446,7 +446,7 @@ function slackemon_start_catch_battle( $spawn_ts, $action, $user_id = USER_ID ) ], RESPONSE_URL ); // Wild Pokemon gets to move first - sleep( 2 ); // Wait before the computer moves... + sleep( 4 ); // Wait before the computer moves... $move = slackemon_get_best_move( $invitee_pokemon, $inviter_pokemon ); slackemon_do_battle_move( $move->name, $battle_hash, $action, true, $invitee_id );