Skip to content

Instantly share code, notes, and snippets.

@tchalvak
Created March 22, 2010 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tchalvak/340438 to your computer and use it in GitHub Desktop.
Save tchalvak/340438 to your computer and use it in GitHub Desktop.
diff --git a/deploy/lib/char/Player.class.php b/deploy/lib/char/Player.class.php
index 81f04fe..277c3d8 100644
--- a/deploy/lib/char/Player.class.php
+++ b/deploy/lib/char/Player.class.php
@@ -61,5 +61,9 @@ class Player
public function as_array() {
return (array) $this->vo;
}
+
+ public function get_player_id() {
+ return $this->vo->player_id;
+ }
}
?>
diff --git a/deploy/lib/common/commands.php b/deploy/lib/common/commands.php
index ee4b98f..2d63af6 100644
--- a/deploy/lib/common/commands.php
+++ b/deploy/lib/common/commands.php
@@ -1,5 +1,8 @@
<?php
+// NOTE THAT STATUS CONSTANTS ARE DEFINED PROCEDURALLY BELOW
+
+
// ************************************
// ********** CLASS FUNCTIONS *********
// ************************************
@@ -268,11 +271,11 @@ function addKills($who, $amount) {
// if record exists
$statement = DatabaseConnection::$pdo->prepare("UPDATE levelling_log SET killpoints = killpoints + :amount WHERE uname = :player AND killsdate = now() AND killpoints > 0"); //increase killpoints
$statement->bindValue(':amount', $amount);
- $statement->bindValue(':player', $player);
+ $statement->bindValue(':player', $who);
} else {
$statement = DatabaseConnection::$pdo->prepare("INSERT INTO levelling_log (uname, killpoints, levelling, killsdate) VALUES (:player, :amount, '0', now())"); //create a new record for today
$statement->bindValue(':amount', $amount);
- $statement->bindValue(':player', $player);
+ $statement->bindValue(':player', $who);
}
$statement->execute();
@@ -295,11 +298,11 @@ function subtractKills($who,$amount) {
// if record exists
$statement = DatabaseConnection::$pdo->prepare("UPDATE levelling_log SET killpoints = killpoints - :amount WHERE uname = :player AND killsdate = now() AND killpoints < 0"); //increase killpoints
$statement->bindValue(':amount', $amount);
- $statement->bindValue(':player', $player);
+ $statement->bindValue(':player', $who);
} else {
$statement = DatabaseConnection::$pdo->prepare("INSERT INTO levelling_log (uname, killpoints, levelling, killsdate) VALUES (:player, :amount, '0', now())"); //create a new record for today
$statement->bindValue(':amount', $amount*-1);
- $statement->bindValue(':player', $player);
+ $statement->bindValue(':player', $who);
}
$statement->execute();
@@ -453,6 +456,9 @@ function subtractLevel($who, $amount) {
// ************************************
// TODO: These must be moved to a more visible place,
//and the global status_array as well.
+
+// Magic number and a global in the same few lines!!
+
define("STEALTH", 1);
define("POISON", 2);
define("FROZEN", 4);
@@ -462,29 +468,30 @@ define("SKILL_2", 32);
define("INVITED", 64);
$status_array;
-/* *** Currently unused consider removing ***
-function setStatus($who, $what) {
- //if (!is_numeric($what)) { // *** If the status being sent in isn't a number...
- // (isset($status_array[$what])? '
- //}
-
- DatabaseConnection::getInstance();
- DatabaseConnection::$pdo->query("UPDATE players SET status = $what WHERE uname = '$who'");
- if ($who == get_username()) {
- $_SESSION['status'] = $what;
- if ($what == 0) {
- echo "<br>You have returned to normal.<br>\n";
- } else if ($what == 1) {
- echo "<br>You have been poisoned.<br>\n";
- } else if ($what == 2) {
- echo "<br>You are now stealthed.<br>\n";
- }
- }
+// Return the array of status identifiers.
+function standard_statuses(){
+ return array(
+ 'Stealth'=>STEALTH,
+ 'Poison'=>POISON,
+ 'Frozen'=>FROZEN,
+ 'ClassState'=>CLASS_STATE,
+ 'Skill1'=>SKILL_1,
+ 'Skill2'=>SKILL_2,
+ 'Invited'=>INVITED
+ );
+}
- return $what;
+// Check an input status against a whitelist of those available.
+function is_standard_status($status){
+ $standard_statuses = standard_statuses();
+ if(in_array((int)$status, $standard_statuses, $strict=TRUE)){
+ return true;
+ } else {
+ return false;
+ }
}
-*/
+// Retrieves a player's current status effect.
function getStatus($who) {
global $status_array;
@@ -494,69 +501,62 @@ function getStatus($who) {
$statement->bindValue(':player', $who);
$statement->execute();
$status = $statement->fetchColumn();
-
- if ($who == SESSION::get('username')) {
- $_SESSION['status'] = $status;
+
+ $standard_statuses = standard_statuses();
+
+ $status_array = array();
+ foreach($standard_statuses as $status_name=>$status_id){
+ $status_array[$status_name] = ($status&$status_id ? 1 : 0);
}
-
- $status_array['Stealth'] = ($status&STEALTH ? 1 : 0);
- $status_array['Poison'] = ($status&POISON ? 1 : 0);
- $status_array['Frozen'] = ($status&FROZEN ? 1 : 0);
- $status_array['ClassState'] = ($status&CLASS_STATE ? 1 : 0);
- $status_array['Skill1'] = ($status&SKILL_1 ? 1 : 0);
- $status_array['Skill2'] = ($status&SKILL_2 ? 1 : 0);
- $status_array['Invited'] = ($status&INVITED ? 1 : 0);
return $status_array;
}
-function addStatus($who, $what) { //Takes in the Status in the ALL_CAPS_WORD format seen above for each.
+// Adds or removes one or more status effects.
+function change_status($who, $what, $remove_status=null){
DatabaseConnection::getInstance();
- $statement = DatabaseConnection::$pdo->prepare("SELECT status FROM players WHERE uname = :player");
- $statement->bindValue(':player', $who);
- $statement->execute();
-
- $status = $statement->fetchColumn();
-
- if ($what < 0) {
- return subtractStatus($who, abs($what));
- }
-
- if (!($status & $what)) {
- $statement = DatabaseConnection::$pdo->prepare("UPDATE players SET status = status+:what WHERE uname = :player");
+ if ($remove_status) {
+ $what = abs($what); // Negate to create a positive status number.
+ $statement = DatabaseConnection::$pdo->prepare("UPDATE players SET status = status -( status & :what ) WHERE uname = :player");
$statement->bindValue(':player', $who);
- $statement->bindValue(':what', $what);
+ $statement->bindValue(':what', (int) $what, PDO::PARAM_INT);
$statement->execute();
+ } else {
+ if (!($status & $what)) {
+ $statement = DatabaseConnection::$pdo->prepare("UPDATE players SET status = status + :what WHERE uname = :player");
+ $statement->bindValue(':player', $who, PDO::PARAM_INT);
+ $statement->bindValue(':what', (int) $what, PDO::PARAM_INT);
+ $statement->execute();
+ }
+ }
+ return getStatus($who); // Return the changed status.
+}
- if ($who == get_username()) {
- $_SESSION['status'] += $what;
- }
- }
+// Remove all status effects from a player.
+function wipe_statuses($player){
+ if(!is_numeric($player)){
+ $statuses_to_wipe = STEALTH+POISON+FROZEN+CLASS_STATE;
+ $statement = DatabaseConnection::$pdo->prepare("UPDATE players SET status = status - (status & :statuses) WHERE uname = :player");
+ $statement->bindValue(':player', $player);
+ $statement->bindValue(':statuses', $statuses_to_wipe);
+ $statement->execute();
+ } else {
+ $statuses_to_wipe = STEALTH+POISON+FROZEN+CLASS_STATE;
+ $statement = DatabaseConnection::$pdo->prepare("UPDATE players SET status = status - (status & :statuses) WHERE player_id = :player_id");
+ $statement->bindValue(':player_id', $player);
+ $statement->bindValue(':statuses', $statuses_to_wipe);
+ $statement->execute();
+ }
+}
- return getStatus($who);
+// Adds a certain status effect.
+function addStatus($who, $what) { //Takes in the Status in the ALL_CAPS_WORD format seen above for each.
+ change_status($who, $what);
}
+// Removes a certain status effect.
function subtractStatus($who, $what) { //Takes in the Status in the ALL_CAPS_WORD format seen above for each.
- DatabaseConnection::getInstance();
-
- $statement = DatabaseConnection::$pdo->prepare("SELECT status FROM players WHERE uname = :player");
- $statement->bindValue(':player', $who);
- $statement->execute();
- $status = $statement->fetchColumn();
-
- if ($status&$what) {
- $statement = DatabaseConnection::$pdo->prepare("UPDATE players SET status = status-(:status&:what) WHERE uname = :prepare");
- $statement->bindValue(':player', $who);
- $statement->bindValue(':status', $status);
- $statement->bindValue(':what', $what);
- $statement->execute();
-
- if ($who == get_username()) {
- $_SESSION['status']-=($status&$what);
- }
- }
-
- return getStatus($who);
+ change_status($who, $what, $remove_status=true);
}
// ************************************
diff --git a/deploy/www/attack_mod.php b/deploy/www/attack_mod.php
index 7399648..0a4b60b 100644
--- a/deploy/www/attack_mod.php
+++ b/deploy/www/attack_mod.php
@@ -146,8 +146,8 @@ if (!$AttackLegal->check()) { // *** Checks for error conditions before starting
$target_msg = "DEATH: You have been killed by a stealthed ninja in combat and lost $loot gold on $today!";
$attacker_msg = "You have killed $target in combat and taken $loot gold on $today.";
-
- $target_player->subtractStatus(STEALTH+POISON+FROZEN+CLASS_STATE);
+
+ wipe_statuses($target_player->get_player_id());
sendMessage("A Stealthed Ninja", $target, $target_msg);
sendMessage($target, $attacker, $attacker_msg);
runBountyExchange($attacker, $target); // *** Determines the bounty for normal attacking. ***
diff --git a/deploy/www/shrine_mod.php b/deploy/www/shrine_mod.php
index 6a6bd4b..c8ba01b 100644
--- a/deploy/www/shrine_mod.php
+++ b/deploy/www/shrine_mod.php
@@ -57,7 +57,7 @@ if ($restore == 1) { // *** RESURRECTION SECTION ***
$returning_health = $has_chi? 150 : 100;
setHealth($username, $returning_health);
- subtractStatus($username, STEALTH+POISON+FROZEN+CLASS_STATE);
+ wipe_statuses($username); // Wipes STEALTH+POISON+FROZEN+CLASS_STATE
$final_turns = (string) getTurns($username);
$final_kills = getKills($username);
@@ -72,7 +72,7 @@ if ($restore == 1) { // *** RESURRECTION SECTION ***
subtractTurns($username, $lostTurns); // *** Takes away necessary turns.
setHealth($username, 100);
- subtractStatus($username, STEALTH+POISON+FROZEN+CLASS_STATE);
+ wipe_statuses($username); // Wipes STEALTH+POISON+FROZEN+CLASS_STATE
$final_turns = (string) getTurns($username);
$turn_taking_resurrect = true; // Template display variable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment