6.22.2006

Short String Summary with MYSQL

I got this snippet from a friend...thanks Andrew!

SELECT CONCAT(SUBSTRING(content,1,300),'...') FROM foo

The above snippets selects the first 300 characters(1-300) from the foo.content column, and then appends '...' to it.

Handy!

6.08.2006

CGI Cookies

Assuming that we're inside a module or something, and this is being passed to us as arguments:

Prelimary



use CGI;
use strict;
my $query = new CGI;
my $cookie;

...

sub writeCookie {
my($expires,$name,$contents,$rememberBoolean) = @_;
if($rememberBoolean) {
$expires = "+6M"; ## give them a 6 month remembered period
} else {
$expires = ""; ## delete the cookie after the browser has been closed
}
$cookie = $query->cookie(
-name=>$name,
-value=>$contents,
-expires->$expires
);
print $query->header(-cookie=>[$cookie]);
}

6.07.2006

Villager Class


#include <iostream>
#include <stdlib.h>

using namespace std;

class Villager {
public:
// constructors
Villager(string name);
Villager(string name, string gender);
void setHealth(int max, int cur);
void setDefaults();
// destructors
~Villager();
void die();

// methods
int move();
int speak() const;
void heal();
void heal(int healAmount);
void takeDamage();

// other

// accessors
int getMoveSpeed() const;
bool getSleeping() const;
bool getMoving() const;
int getAttraction() const;
void setAttraction(int newAttraction);
string getName() const;
string getGender() const;
int getHealth() const;
int getHealthRemaining() const;
void printGender() const;

private:

// member variables
bool itsSleeping;
bool itsMoving;
int itsMoveSpeed;
int itsHeight;
int itsEmotion;
signed int itsAttraction;
string itsGender;
int itsMaxHealth;
int itsCurrentHealth;
string itsName;
bool itsDead;
protected:
};

// constructor with name argument
Villager::Villager(string name) {
itsName = name;
// no gender supplied? Hermaphrodite.
itsGender = "Hermaphrodite";
setDefaults();
setHealth(12,12);
}

// constructor with name and gender arguments
Villager::Villager(string name, string gender) {
itsName = name;
// standardize gender assignments using some conditionals.
if(gender == "female" || gender == "Female") {
gender = "Girl";
}
if(gender == "male" || gender == "Male") {
gender = "Boy";
}
itsGender = gender;
setHealth(28,4);
setDefaults();
}

// destructor
Villager::~Villager() {
// if they're moving when we destroy them, make sure they come to a stop.
// but only do this if they AREN'T dead yet.
if(itsMoving && !itsDead) {
move();
}
}

// check whether villager is sleeping or not.
bool Villager::getSleeping() const {
return itsSleeping;
}

// set new attraction levels
void Villager::setAttraction(int newAttraction) {
itsAttraction += newAttraction;
}

// get the villager's attraction level
int Villager::getAttraction() const {
return itsAttraction;
}

// get the villager's movement speed
int Villager::getMoveSpeed() const {
return itsMoveSpeed;
}

// get whether the villager is moving or not
bool Villager::getMoving() const {
return itsMoving;
}

// toggle whether the villager is moving or not
int Villager::move() {
if(!itsMoving) {
itsMoving = true;
cout << getName() << " is on the move." << endl;
} else {
itsMoving = false;
cout << getName() << " comes to a stop." << endl;
}
}

// get the villager's name
string Villager::getName() const {
return itsName;
}

// get the villager's gender
string Villager::getGender() const {
return itsGender;
}

// make the villager speak
int Villager::speak() const {
cout << getName() << "(" << getHealthRemaining() << "/" << getHealth() << ") says 'Hello.'" << endl;
}

// print out the villager's gender
void Villager::printGender() const {
cout << getName() << "(" << getHealthRemaining() << "/" << getHealth() << ") is a " << getGender() << "." << endl;
}

// get the max health for the villager
int Villager::getHealth() const {
return itsMaxHealth;
}

// get the remaining health for the villager
int Villager::getHealthRemaining() const {
return itsCurrentHealth;
}

// set the default health value for the villager
void Villager::setHealth(int max, int cur) {
itsMaxHealth = max;
itsCurrentHealth = cur;
itsDead = false;
}

// default heal method, for villagers to regain health
void Villager::heal() {
if((itsMaxHealth - itsCurrentHealth) >= 2) {
cout << getName() << " healed for 2 Hit Points." << endl;
itsCurrentHealth += 2;
} else {
cout << getName() << " was unable to heal!" << endl;
}
}

// overloaded heal method, for supplied healing amounts.
void Villager::heal(int healAmount) {
if((itsMaxHealth - itsCurrentHealth) >= healAmount) {
cout << getName() << " healed for " << healAmount << " Hit Points." << endl;
itsCurrentHealth += healAmount;
} else {
cout << getName() << " was unable to heal!" << endl;
}
}

// death method; villager has died!
void Villager::die() {
cout << getName() << " is dead!" << endl;
itsDead = true;
// need a way to call a destructor.
// ~Villager();
}

// damage taking class, for villagers to lose health
void Villager::takeDamage() {
if((itsCurrentHealth - 1) > 0 && !itsDead) {
itsCurrentHealth -= 1;
cout << getName() << " took 1 point of damage! "<< "(" << getHealthRemaining() << "/" << getHealth() << ")" << endl;
} else if(!itsDead) {
itsCurrentHealth -= 1;
die();
}
}

// setting the default values, so that we don't have any zombie villagers.
void Villager::setDefaults() {
itsDead = false;
itsMoving = false;
itsSleeping = false;
}