Skip to content

Commit

Permalink
Expose guild war kills
Browse files Browse the repository at this point in the history
  • Loading branch information
DSpeichert committed Jul 19, 2014
1 parent 8fccd45 commit de73730
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 5 deletions.
17 changes: 17 additions & 0 deletions DevAAC/Models/GuildWar.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class GuildWar extends \Illuminate\Database\Eloquent\Model {

protected $guarded = array('id');

protected $appends = array('guild1_kills', 'guild2_kills');

public function getStartedAttribute()
{
$date = new DateTime();
Expand Down Expand Up @@ -100,4 +102,19 @@ public function guild2()
{
return $this->belongsTo('DevAAC\Models\Guild', 'guild2');
}

public function kills()
{
return $this->hasMany('DevAAC\Models\GuildwarKill', 'warid');
}

public function getGuild1KillsAttribute()
{
return GuildwarKill::where('warid', $this->id)->where('killerguild', $this->guild1)->count();
}

public function getGuild2KillsAttribute()
{
return GuildwarKill::where('warid', $this->id)->where('killerguild', $this->guild2)->count();
}
}
84 changes: 84 additions & 0 deletions DevAAC/Models/GuildwarKill.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* DevAAC
*
* Automatic Account Creator by developers.pl for TFS 1.0
*
*
* LICENSE: Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* @package DevAAC
* @author Daniel Speichert <daniel@speichert.pl>
* @author Wojciech Guziak <wojciech@guziak.net>
* @copyright 2014 Developers.pl
* @license http://opensource.org/licenses/MIT MIT
* @version master
* @link https://github.com/DevelopersPL/DevAAC
*/

namespace DevAAC\Models;

use DevAAC\Helpers\DateTime;

// https://github.com/illuminate/database/blob/master/Eloquent/Model.php
// https://github.com/otland/forgottenserver/blob/master/schema.sql

/**
* @SWG\Model(required="['id','killer','target','killerguild','targetguild','warid','time']")
*/
class GuildwarKill extends \Illuminate\Database\Eloquent\Model {
/**
* @SWG\Property(name="id", type="integer")
* @SWG\Property(name="killer", type="string")
* @SWG\Property(name="target", type="string")
* @SWG\Property(name="killerguild", type="integer")
* @SWG\Property(name="targetguild", type="integer")
* @SWG\Property(name="warid", type="integer")
* @SWG\Property(name="time", type="DateTime::ISO8601")
*/

public $timestamps = false;

protected $guarded = array('id');

public function getTimeAttribute()
{
$date = new DateTime();
$date->setTimestamp($this->attributes['time']);
return $date;
}

public function setTimeAttribute($d)
{
if($d instanceof \DateTime)
$this->attributes['time'] = $d->getTimestamp();
elseif((int)$d != (string)$d) { // it's not a UNIX timestamp
$dt = new DateTime($d);
$this->attributes['time'] = $dt->getTimestamp();
} else // it is a UNIX timestamp
$this->attributes['time'] = $d;
}

public function killerGuild()
{
return $this->belongsTo('DevAAC\Models\Guild', 'killerguild');
}

public function targetGuild()
{
return $this->belongsTo('DevAAC\Models\Guild', 'targetguild');
}
}
28 changes: 23 additions & 5 deletions DevAAC/routes/guilds.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,40 @@
* basePath="/api/v1",
* resourcePath="/guilds",
* @SWG\Api(
* path="/guilds/wars/:id",
* path="/guilds/wars/{id}",
* description="Operations on guilds",
* @SWG\Operation(
* summary="Get guild war based on war ID",
* notes="",
* method="GET",
* type="GuildWar",
* nickname="getGuildWarByID"
* nickname="getGuildWarByID",
* @SWG\Parameter( name="id",
* description="ID of GuildWar that needs to be fetched",
* paramType="path",
* required=true,
* type="integer"),
* @SWG\Parameter( name="embed",
* description="Pass kills to embed",
* paramType="query",
* required=false,
* type="string list separated by comma"),
* @SWG\ResponseMessage(code=404, message="GuildWar not found")
* )
* )
* )
*/
$DevAAC->get(ROUTES_API_PREFIX.'/guilds/wars/:id', function() use($DevAAC) {
$guildwars = GuildWar::all();
$DevAAC->get(ROUTES_API_PREFIX.'/guilds/wars/:id', function($id) use($DevAAC) {
$req = $DevAAC->request;
$war = GuildWar::findOrFail($id);

$embedded = explode(',', $req->get('embed'));

if (in_array('kills', $embedded))
$war->kills;

$DevAAC->response->headers->set('Content-Type', 'application/json');
$DevAAC->response->setBody($guildwars->toJson(JSON_PRETTY_PRINT));
$DevAAC->response->setBody($war->toJson(JSON_PRETTY_PRINT));
});

/**
Expand Down

0 comments on commit de73730

Please sign in to comment.