00001 <?
00009 class BaseRPSGame extends MyObject
00010 {
00015 public static $winMatrix = array(
00016 'rock' => array(
00017 'rock' => 'tie',
00018 'paper' => 'defense',
00019 'scissors' => 'offense'
00020 ),
00021 'paper' => array(
00022 'rock' => 'offense',
00023 'paper' => 'tie',
00024 'scissors' => 'defense'
00025 ),
00026 'scissors' => array(
00027 'rock' => 'defense',
00028 'paper' => 'offense',
00029 'scissors' => 'tie'
00030 ),
00031 );
00032
00036 public $Attacker;
00037
00041 public $Defender;
00042
00046 public function __construct($data = null, $table = 'janken')
00047 {
00048 parent::__construct($data, $table);
00049
00050 $this->english = 'game';
00051 }
00052
00056 protected function getDeletePageXml()
00057 {
00058 return '';
00059 }
00060
00064 protected function getEditPageXml()
00065 {
00066 return '';
00067 }
00068
00076 public function getPagesXml()
00077 {
00078 $xml = parent::getPagesXml();
00079
00080 $xml .= <<<XML
00081 <page name="challenge">
00082 <param name="opponent_id" type="int" required="1"/>
00083 <param name="attack" required="1">
00084 <option value="rock"/>
00085 <option value="paper"/>
00086 <option value="scissors"/>
00087 </param>
00088 </page>
00089 <page name="defend">
00090 <param name="id" type="int" required="1"/>
00091 <param name="defense">
00092 <option value="rock"/>
00093 <option value="paper"/>
00094 <option value="scissors"/>
00095 </param>
00096 </page>
00097 <page name="taunt">
00098 <param name="id" type="int" required="1"/>
00099 </page>
00100 XML;
00101 return $xml;
00102 }
00103
00107 public function initChallengePage()
00108 {
00109 global $me;
00110
00111 $this->assertLogin();
00112
00113 $this->pageTitle = "Rock Paper Scissors Challenge";
00114
00115 if ($this->params('opponent_id') == $me->id)
00116 throw new PageError('You cannot attack yourself.');
00117 }
00118
00122 public function drawChallengePage()
00123 {
00124 global $me;
00125
00126
00127 $this->Attacker = new User($me->id);
00128 $this->Defender = new User($this->params('opponent_id'));
00129
00130
00131 $this->initiator_id = $me->id;
00132 $this->opponent_id = $this->params('opponent_id');
00133 $this->offense = $this->params('attack');
00134 $this->winner = 'none';
00135 $this->save();
00136
00137
00138 $this->Defender->addAlert($this->getUrl(".view?id=$this->id"), $me->getName() . " challenged you to Rock Paper Scissors");
00139
00140 echo "<p>You challenged " . $this->Defender->getName(true) . " to a rock paper scissors game. Now you have to wait for them to respond.</p>";
00141
00142 $form = $this->createTauntForm();
00143 $form->draw();
00144 }
00145
00149 public function initTauntPage()
00150 {
00151 global $me;
00152
00153 $this->assertLogin();
00154 $this->id = $this->params('id');
00155
00156 if ($this->initiator_id != $me->id)
00157 throw new PageError('You can only taunt if you attack!');
00158
00159 $this->pageTitle = "Rock Paper Scissors Trash Talk";
00160 }
00161
00165 public function drawTauntPage()
00166 {
00167 $form = $this->createTauntForm();
00168
00169 if ($form->isSubmittedAndValid())
00170 {
00171 $this->taunt = $form->getData('taunt');
00172 $this->save();
00173
00174 Util::redirect($this->getUrl(".view?id=$this->id"));
00175 }
00176
00177 $form->drawIfNeeded();
00178 }
00179
00185 public function createTauntForm()
00186 {
00187 $form = new Form();
00188 $form->action = $this->getUrl(".taunt?id=$this->id");
00189
00190 $form->add('LabelField', 'help', array(
00191 'title' => 'Taunt',
00192 'text' => 'Enter a message in the field below and it will be shown to them when they respond to your attack.'
00193 ));
00194 $form->add('TextField', 'taunt', array(
00195 'title' => '',
00196 'required' => true,
00197 'size' => '100%'
00198 ));
00199 $form->addSubmit('Taunt Them!');
00200
00201 return $form;
00202 }
00203
00207 public function initDefendPage()
00208 {
00209 global $me;
00210
00211
00212 $this->assertLogin();
00213 $this->initViewPage();
00214
00215
00216 if ($this->opponent_id != $me->id)
00217 throw new PageError('You can only defend your own games.');
00218 if ($this->winner != 'none')
00219 throw new PageError('This game is already over. You must accept the outcome.');
00220
00221
00222 $this->defense = $this->params('defense');
00223 $this->winner = self::$winMatrix[$this->offense][$this->defense];
00224 $this->save();
00225
00226
00227 $this->Attacker->addAlert($this->getUrl(".view?id=$this->id"), "A rock paper scissors game has ended.");
00228
00229
00230 Util::redirect($this->getUrl(".view?id=$this->id"));
00231 }
00232
00236 public function canView()
00237 {
00238 return true;
00239 }
00240
00244 public function canEdit()
00245 {
00246 return false;
00247 }
00248
00252 public function canDelete()
00253 {
00254 return false;
00255 }
00256
00263 public function drawChallengeBox($userId)
00264 {
00265 $user = new User($userId);
00266 ?>
00267 <div class="rpsChallenge">
00268 <div id="rpsChallengeText">Challenge <?=$user->getName(true);?></div>
00269 <div class="rpsChallengeImages">
00270 <?=$this->getLink(".challenge?opponent_id=$userId&attack=rock", '<img src="lib/img/rps/rock.gif"/>');?>
00271 <?=$this->getLink(".challenge?opponent_id=$userId&attack=paper", '<img src="lib/img/rps/paper.gif"/>');?>
00272 <?=$this->getLink(".challenge?opponent_id=$userId&attack=scissors", '<img src="lib/img/rps/scissors.gif"/>');?>
00273 </div>
00274 </div>
00275 <?
00276 }
00277
00281 public function initViewPage()
00282 {
00283 parent::initViewPage();
00284
00285 $this->pageTitle = 'Rock Paper Scissors Fight';
00286 }
00287
00291 public function drawViewPage()
00292 {
00293 global $me;
00294
00295
00296 if ($this->initiator_id == $me->id)
00297 $attacker = 'You';
00298 else
00299 $attacker = $this->Attacker->getName(true);
00300
00301
00302 if ($this->opponent_id == $me->id)
00303 $defender = 'you';
00304 else
00305 $defender = $this->Defender->getName(true);
00306
00307
00308 if ($this->taunt)
00309 echo "<p>$attacker said, \"<i>" . Linkify::bbcode($this->taunt) . "</i>\" and attacked $defender.</p>";
00310 else
00311 echo "<p>$attacker attacked $defender.</p>";
00312
00313 if ($this->winner == 'none')
00314 {
00315 if ($this->opponent_id == $me->id)
00316 $this->drawDefendBox();
00317 else
00318 echo "<p>No winner yet!</p>";
00319 }
00320 else
00321 {
00322 $this->drawFightBox();
00323
00324 if ($this->winner == 'offense')
00325 echo "<p>The winner is <b>$attacker</b>!</p>";
00326 else if ($this->winner == 'defense')
00327 echo "<p>The winner is <b>$defender</b>!</p>";
00328 else
00329 echo "<p>It was a tie!</p>";
00330
00331 if ($this->initiator_id == $me->id)
00332 {
00333 echo "<h3>Fight again!</h3>";
00334 $this->drawChallengeBox($this->opponent_id);
00335 }
00336
00337 if ($this->opponent_id == $me->id)
00338 {
00339 echo "<h3>Fight again!</h3>";
00340 $this->drawChallengeBox($this->initiator_id);
00341 }
00342 }
00343 }
00344
00348 public function drawDefendBox()
00349 {
00350 ?>
00351 <div class="rpsDefend">
00352 <div id="rpsDefendText">Defend Yourself!</div>
00353 <div class="rpsDefendImages">
00354 <?=$this->getLink(".defend?id=$this->id&defense=rock", '<img src="lib/img/rps/rock.gif"/>');?>
00355 <?=$this->getLink(".defend?id=$this->id&defense=paper", '<img src="lib/img/rps/paper.gif"/>');?>
00356 <?=$this->getLink(".defend?id=$this->id&defense=scissors", '<img src="lib/img/rps/scissors.gif"/>');?>
00357 </div>
00358 </div>
00359 <?
00360
00361 }
00362
00366 public function drawFightBox()
00367 {
00368 ?>
00369 <div class="rpsFight">
00370 <img src="lib/img/rps/<?=$this->offense?>.gif"/> <b>vs.</b> <img src="lib/img/rps/<?=$this->defense?>.gif"/>
00371 </div>
00372 <?
00373 }
00374
00396 public function getUserStats($userId)
00397 {
00398 $stats = array(
00399 'offwin' => 'n/a',
00400 'offloss' => 'n/a',
00401 'offtie' => 'n/a',
00402 'offpercent' => 'n/a',
00403 'defwin' => 'n/a',
00404 'defloss' => 'n/a',
00405 'deftie' => 'n/a',
00406 'defpercent' => 'n/a',
00407 'totwin' => 'n/a',
00408 'totloss' => 'n/a',
00409 'tottie' => 'n/a',
00410 'totpercent' => 'n/a',
00411 );
00412
00413
00414 $rs = dbQuery("
00415 SELECT count(winner) AS wincount, winner
00416 FROM $this->tableName
00417 WHERE initiator_id = '$userId'
00418 GROUP BY winner
00419 ");
00420 while ($ar = dbFetchAssoc($rs))
00421 {
00422 if ($ar['winner'] == 'offense')
00423 $stats['offwin'] = (int)$ar['wincount'];
00424 else if ($ar['winner'] == 'defense')
00425 $stats['offloss'] = (int)$ar['wincount'];
00426 else if ($ar['winner'] == 'tie')
00427 $stats['offtie'] = (int)$ar['wincount'];
00428 }
00429
00430
00431 $rs = dbQuery("
00432 SELECT count(winner) AS wincount, winner
00433 FROM $this->tableName
00434 WHERE opponent_id = '$userId'
00435 GROUP BY winner
00436 ");
00437 while ($ar = dbFetchAssoc($rs))
00438 {
00439 if ($ar['winner'] == 'offense')
00440 $stats['defloss'] = (int)$ar['wincount'];
00441 else if ($ar['winner'] == 'defense')
00442 $stats['defwin'] = (int)$ar['wincount'];
00443 else if ($ar['winner'] == 'tie')
00444 $stats['deftie'] = (int)$ar['wincount'];
00445 }
00446
00447 $stats['totloss'] = (int)($stats['defloss'] + $stats['offloss']);
00448 $stats['totwin'] = (int)($stats['defwin'] + $stats['offwin']);
00449 $stats['tottie'] = (int)($stats['deftie'] + $stats['offtie']);
00450
00451 return $stats;
00452 }
00453
00457 public function lookupData($deep = true)
00458 {
00459 parent::lookupData($deep);
00460
00461 $this->Attacker = new User();
00462 $this->Attacker->load($this->initiator_id, false);
00463
00464 $this->Defender = new User();
00465 $this->Defender->load($this->opponent_id, false);
00466 }
00467
00468 public function getDataToCache($deep = true)
00469 {
00470 $data = parent::getDataToCache($deep);
00471
00472 $data['attacker'] = $this->Attacker->getDataToCache(false);
00473 $data['defender'] = $this->Defender->getDataToCache(false);
00474
00475 return $data;
00476 }
00477
00478 public function setDataFromCache($data, $deep = true)
00479 {
00480 parent::setDataFromCache($data);
00481
00482
00483 $this->Attacker = new User();
00484 $this->Attacker->load($data['attacker'], false);
00485
00486 $this->Defender = new User();
00487 $this->Defender->load($data['defender'], false);
00488 }
00489
00495 public function getCreateFieldsArray()
00496 {
00497 $data = parent::getCreateFieldsArray();
00498
00499
00500 unset($data['add_date']);
00501 unset($data['edit_date']);
00502 unset($data['user_id']);
00503
00504
00505 $data['initiator_id'] = 'initiator_id int(11) default 0 not null';
00506 $data['opponent_id'] = 'opponent_id int(11) default 0 not null';
00507 $data['offense'] = "offense enum('rock','paper','scissors')";
00508 $data['defense'] = "defense enum('rock','paper','scissors')";
00509 $data['winner'] = "winner enum('offense','defense','tie','none')";
00510 $data['taunt'] = "taunt varchar(255) default 0 not null";
00511
00512 return $data;
00513 }
00514
00520 public function getCreateIndexesArray()
00521 {
00522 $data = parent::getCreateIndexesArray();
00523
00524
00525 unset($data['add_date']);
00526 unset($data['edit_date']);
00527 unset($data['user_id']);
00528
00529
00530 $data['initiator_id'] = 'key(initiator_id)';
00531 $data['opponent_id'] = 'key(opponent_id)';
00532 $data['winner'] = 'key(winner)';
00533
00534 return $data;
00535 }
00536 }
00537 ?>