Skip to content

Commit

Permalink
Fix #2: Prize Count Should Not Reset When Winner Leaves
Browse files Browse the repository at this point in the history
Also adds support for the X-Forwarded-For address header.
  • Loading branch information
Daniel LaBarge committed Mar 7, 2017
1 parent 4f4d901 commit a058379
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 30 deletions.
2 changes: 1 addition & 1 deletion app/Server/Commands/PickRandomWinner.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function run()

$winner->type(Connection::WINNER);
$winner->prize($prize);
$prize->winner($winner->uuid());
$prize->winner($winner->uuid())->awarded(true);

$everyone = $this->dispatcher()->connections();

Expand Down
12 changes: 12 additions & 0 deletions app/Server/Contracts/Prize.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,16 @@ public function sponsor($sponsor = null);
* @return string|self
*/
public function winner($uuid = null);

/**
* Get or set the awarded status of the prize.
*
* @example awarded() ==> bool
* awarded($state) ==> self
*
* @param bool $state
*
* @return bool|self
*/
public function awarded($state = null);
}
38 changes: 21 additions & 17 deletions app/Server/Entities/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Connection implements ConnectionInterface, Arrayable, Jsonable, JsonSerial

protected $admin;
protected $email;
protected $ip_address;
protected $name;
protected $notifications;
protected $prize;
Expand All @@ -28,7 +29,6 @@ class Connection implements ConnectionInterface, Arrayable, Jsonable, JsonSerial
protected $timestamp;
protected $type;
protected $uuid;
protected $ipAddress;

/**
* Inject a Ratchet connection as the proxy of this connection.
Expand All @@ -40,11 +40,13 @@ public function __construct(SocketInterface $instance)
$this->socket($instance);
$this->timestamp(Carbon::now());
$this->uuid(Uuid::uuid4()->toString());
$this->ipAddress($instance->remoteAddress);
$this->type(ConnectionInterface::ANONYMOUS);
$this->notifications(new Notifications());
$this->subscriptions(new Topics());
$this->admin(false);

$header = $instance->WebSocket->request->getHeader('x-forwarded-for');
$this->ipAddress($header ? $header->__toString() : $instance->remoteAddress);
}

/**
Expand Down Expand Up @@ -163,13 +165,13 @@ public function name($name = null)
* @example ipAddress() ==> string
* ipAddress($name) ==> self
*
* @param string $ipAddress
* @param string $ip_address
*
* @return string|self
*/
public function ipAddress($ipAddress = null)
public function ipAddress($ip_address = null)
{
return $this->property(__FUNCTION__, $ipAddress);
return $this->property(snake_case(__FUNCTION__), $ip_address);
}

/**
Expand Down Expand Up @@ -268,17 +270,19 @@ public function notifications(Notifications $notifications = null)
public function toArray()
{
return array_filter([
'uuid' => $this->uuid,
'resource_id' => $this->socket->resourceId,
'type' => $this->type,
'admin' => $this->admin,
'name' => $this->name,
'email' => $this->email,
'notifications' => $this->notifications->toArray(),
'subscriptions' => $this->subscriptions->toArray(),
'prize' => $this->prize ? $this->prize->toArray() : null,
'ipAddress' => $this->ipAddress,
'timestamp' => $this->timestamp->timestamp,
]);
'admin' => $this->admin(),
'email' => $this->email(),
'ip_address' => $this->ipAddress(),
'name' => $this->name(),
'notifications' => $this->notifications()->toArray(),
'prize' => $this->prize() ? $this->prize()->toArray() : null,
'resource_id' => $this->socket()->resourceId,
'subscriptions' => $this->subscriptions()->toArray(),
'timestamp' => $this->timestamp()->timestamp,
'type' => $this->type(),
'uuid' => $this->uuid(),
], function ($value) {
return ((is_array($value) || is_string($value)) && ! empty($value)) || ! is_null($value);
});
}
}
2 changes: 1 addition & 1 deletion app/Server/Entities/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public function timestamp(Carbon $timestamp = null)
public function toArray()
{
return array_filter([
'uuid' => $this->uuid(),
'sender' => $this->sender(),
'timestamp' => $this->timestamp()->timestamp,
'uuid' => $this->uuid(),
]);
}
}
29 changes: 24 additions & 5 deletions app/Server/Entities/Prize.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Prize implements PrizeInterface, Arrayable, Jsonable, JsonSerializable
{
use FluentProperties, JsonHelpers;

protected $awarded = false;
protected $name;
protected $sponsor;
protected $uuid;
Expand Down Expand Up @@ -92,6 +93,21 @@ public function winner($uuid = null)
return $this->property(__FUNCTION__, $uuid);
}

/**
* Get or set the awarded status of the prize.
*
* @example awarded() ==> bool
* awarded($state) ==> self
*
* @param bool $state
*
* @return bool|self
*/
public function awarded($state = null)
{
return $this->property(__FUNCTION__, $state);
}

/**
* Get the instance as an array.
*
Expand All @@ -100,10 +116,13 @@ public function winner($uuid = null)
public function toArray()
{
return array_filter([
'uuid' => $this->uuid,
'name' => $this->name,
'sponsor' => $this->sponsor,
'winner' => $this->winner ? $this->winner : null,
]);
'awarded' => $this->awarded(),
'name' => $this->name(),
'sponsor' => $this->sponsor(),
'uuid' => $this->uuid(),
'winner' => $this->winner() ? $this->winner() : null,
], function ($value) {
return ((is_array($value) || is_string($value)) && ! empty($value)) || ! is_null($value);
});
}
}
6 changes: 3 additions & 3 deletions app/Server/Entities/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ public function unsubscribe(Connection $connection)
public function toArray()
{
return array_filter([
'uuid' => $this->uuid,
'name' => $this->name,
'subscriptions' => $this->subscriptions->toArray(),
'name' => $this->name(),
'subscriptions' => $this->subscriptions()->toArray(),
'uuid' => $this->uuid(),
]);
}
}
3 changes: 3 additions & 0 deletions app/Server/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ public function password($password = null)
*/
public function start()
{
// Render the start time
$this->start = Carbon::now();
$this->broker()->log('Server started at: '.$this->start->timestamp);

// Demonstration of a timer where the server keeps time
$this->loop()->addPeriodicTimer(1, function () {
Expand All @@ -94,6 +96,7 @@ public function start()

// Start the actual loop: starts blocking
$this->loop()->run();
$this->broker()->log('Server stopped at: '.Carbon::now()->timestamp);

return $this;
}
Expand Down
6 changes: 3 additions & 3 deletions resources/assets/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ const app = new Vue({
return (this.prizesTotal - this.prizesWon) + ' / ' + this.prizesTotal;
},
prizesWon() {
return _.filter(this.prizes, prize => !_.isEmpty(prize.winner)).length;
return _.filter(this.prizes, prize => prize.awarded === true).length;
},
prizesTotal() {
return this.prizes.length;
Expand Down Expand Up @@ -190,7 +190,7 @@ const app = new Vue({
uuid: '',
name: "Anonymous",
email: 'Not Available',
ipAddress: '127.0.0.1',
ip_address: '127.0.0.1',
timestamp: 0,
type: 'anonymous',
resource_id: ''
Expand Down Expand Up @@ -219,7 +219,7 @@ const app = new Vue({
'name': 'Name',
'email': 'Email',
'uuid': 'Connection',
//'ipAddress': 'IP Address',
'ip_address': 'IP Address',
'timestamp': 'Time',
'type': 'Status'
}
Expand Down

0 comments on commit a058379

Please sign in to comment.