Permalink
Browse files

destroy game if creator leaves, check for color uniqueness

  • Loading branch information...
cobexer committed May 14, 2012
1 parent 77be4a3 commit f7e1e80b8475bdc00037377e9a6e6b3a681b573f
Showing with 80 additions and 28 deletions.
  1. +80 −28 websocket-server/WebSocketGameServer.php
@@ -29,10 +29,17 @@ public function onNewUser($socket) {
}
public function newGame(WebSocketGameUser $creator) {
- $game = $this->games[] = new GameState($creator);
+ $game = $this->games[] = new GameState($this, $creator);
return $game;
}
+ public function destroyGame(GameState $game) {
+ $index = array_search($game, $this->games, true);
+ if (false !== $index) {
+ array_splice($this->games, $index, 1);
+ }
+ }
+
public function getOpenGames() {
$games = array();
foreach($this->games as $g) {
@@ -61,6 +68,11 @@ public function __construct(WebSocketGameServer $gameServer, $socket) {
function getName() {
return $this->name;
}
+
+ function getColor() {
+ return $this->color;
+ }
+
function onMessage(WebSocketMessage $msg) {
if ($msg->isText()) {
$msgObj = json_decode($msg->data, true);
@@ -76,7 +88,9 @@ function onMessage(WebSocketMessage $msg) {
}
break;
case 'init':
- $this->updatePlayer($msgObj);
+ $this->name = $msgObj['name'];
+ $this->color = $msgObj['color'];
+ $this->method = $msgObj['method'];
$player = $this->getUserObj();
$player['type'] = 'init';
$this->send($player);
@@ -122,6 +136,7 @@ function onMessage(WebSocketMessage $msg) {
public function onDisconnected($success) {
if ($this->game) {
$this->game->leave($this);
+ $this->game = null;
}
}
@@ -134,9 +149,7 @@ public function getUserObj() {
}
public function updatePlayer($newData) {
- if ($this->game) {
- $old = $this->getUserObj();
- }
+ $old = $this->getUserObj();
if (isset($newData['name'])) {
$this->name = $newData['name'];
}
@@ -146,24 +159,30 @@ public function updatePlayer($newData) {
if (isset($newData['method'])) {
$this->method = $newData['method'];
}
+ $msgObj = array(
+ 'type' => 'updatePlayer',
+ 'oldPlayer' => $old,
+ 'newPlayer' => $this->getUserObj()
+ );
+ $msg = json_encode($msgObj);
if ($this->game) {
- $msgObj = array(
- 'type' => 'updatePlayer',
- 'oldPlayer' => $old,
- 'newPlayer' => $this->getUserObj()
- );
- $msg = json_encode($msgObj);
foreach($this->game->getUsers() as $u) {
$u->send($msg);
}
}
+ else {
+ $this->send($msg);
+ }
}
}
class GameState {
+ private static $availableColors = array('red', 'green', 'blue', 'orange', 'yellow', 'lime');
+ private $gameServer;
private $users;
private $creator;
- public function __construct(WebSocketGameUser $creator) {
+ public function __construct(WebSocketGameServer $gameServer, WebSocketGameUser $creator) {
+ $this->gameServer = $gameServer;
$this->users = array($creator);
$this->creator = $creator;
}
@@ -172,6 +191,46 @@ public function isFull() {
return count($this->users) === 3;
}
+ public function checkPlayer(WebSocketGameUser $user, $makeUnique = false) {
+ $newName = $user->getName();
+ // make sure the new user's name is unique
+ do {
+ $nameDuplicated = false;
+ foreach($this->users as $u) {
+ if ($user !== $u && 0 === strcmp($u->getName(), $newName)) {
+ if (!$makeUnique) {
+ return false;
+ }
+ $newName .= '_';
+ $nameDuplicated = true;
+ break;
+ }
+ }
+ }
+ while($nameDuplicated);
+ $availableColors = self::$availableColors;
+ $takenColors = array();
+ foreach($this->users as $u) {
+ if ($u !== $user) {
+ $takenColors[$u->getColor()] = true;
+ }
+ }
+ $newColor = $user->getColor();
+ while (isset($takenColors[$newColor])) {
+ if (!$makeUnique) {
+ return false;
+ }
+ $newColor = self::$availableColors[mt_rand(0, count(self::$availableColors) - 1)];
+ }
+ if (0 !== strcmp($newName, $user->getName()) || 0 !== strcmp($newColor, $user->getColor())) {
+ $user->updatePlayer(array(
+ 'name' => $newName,
+ 'color' => $newColor
+ ));
+ }
+ return true;
+ }
+
public function join(WebSocketGameUser $user) {
if ($this->isFull()) {
$user->send(array(
@@ -183,22 +242,7 @@ public function join(WebSocketGameUser $user) {
if (false === array_search($user, $this->users, true)) {
$this->users[] = $user;
- $newName = $user->getName();
- // make sure the new user's name is unique
- do {
- $nameDuplicated = false;
- foreach($this->users as $u) {
- if (0 === strcmp($u->getName(), $newName)) {
- $newName += '_';
- $nameDuplicated = true;
- break;
- }
- }
- }
- while($nameDuplicated);
- if (0 !== strcmp($newName, $user->getName())) {
- $user->updatePlayer(array('name' => $newName));
- }
+ $this->checkPlayer($user, true);
$msg = json_encode(array(
'type' => 'join',
'player' => $user->getUserObj()
@@ -228,6 +272,14 @@ public function leave(WebSocketGameUser $user) {
'player' => $user->getUserObj()
));
array_splice($this->users, $pos, 1);
+ if ($this->creator === $user) {
+ // the creator left, destroy this game
+ $this->gameServer->destroyGame($this);
+ $msg = json_encode(array(
+ 'type' => 'destroyGame',
+ 'reason' => 'Creator left'
+ ));
+ }
foreach($this->users as $u) {
if ($user !== $u) {
$u->send($msg);

0 comments on commit f7e1e80

Please sign in to comment.