Skip to content
This repository has been archived by the owner on Dec 10, 2022. It is now read-only.

Commit

Permalink
Merge branch 'database-improvements-#21' into develop
Browse files Browse the repository at this point in the history
* database-improvements-#21:
  Revert "Minor: speed up battle move & catching waits slightly" - no longer needed now that AWS local cache augmentation is shipping
  Minor improvements to file lock debugging
  Update spawn and file locking default behaviour now that file locking is ready
  Resolve a couple of extra bugs that file locking introduced
  • Loading branch information
tdmalone committed Jun 27, 2017
2 parents bdbb1a4 + b362738 commit 2dab69e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 40 deletions.
8 changes: 3 additions & 5 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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
);

/**
Expand Down
8 changes: 6 additions & 2 deletions lib/filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!
16 changes: 10 additions & 6 deletions src/battles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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 = [];

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
(
Expand Down
4 changes: 2 additions & 2 deletions src/catching.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

}
Expand Down Expand Up @@ -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 );

Expand Down
25 changes: 0 additions & 25 deletions src/spawns.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down

0 comments on commit 2dab69e

Please sign in to comment.