Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
Unique Logos Per Team # (#517)
Browse files Browse the repository at this point in the history
* Functionality prevents users from utilizing the same logo as another user/team or one already in use.

* Users now are provided the option of selecting a unique, or unused logo.

* When a logo is selected the logo is marked as used.

* When a logo is removed from a team (through team deletion, logo change, or otherwise) the logo is readded to the rotation for available logos.

* When a team is imported their logo is set to used.

* Database schema changed to set the default for all logos to unused. The scheme update also sets the admin logo to used.
  • Loading branch information
justinwray authored and gsingh93 committed Jun 6, 2017
1 parent ec996a5 commit 6d4f919
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
4 changes: 2 additions & 2 deletions database/logos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ DROP TABLE IF EXISTS `logos`;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `logos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`used` tinyint(1) DEFAULT 1,
`used` tinyint(1) DEFAULT 0,
`enabled` tinyint(1) DEFAULT 1,
`protected` tinyint(1) DEFAULT 0,
`custom` tinyint(1) DEFAULT 0,
Expand All @@ -23,7 +23,7 @@ CREATE TABLE `logos` (

LOCK TABLES `logos` WRITE;
/*!40000 ALTER TABLE `logos` DISABLE KEYS */;
INSERT INTO `logos` (name, logo, protected, custom) VALUES ('admin', '/static/svg/icons/badges/badge-admin.svg', 1, 0);
INSERT INTO `logos` (name, logo, protected, used, custom) VALUES ('admin', '/static/svg/icons/badges/badge-admin.svg', 1, 1, 0);
INSERT INTO `logos` (name, logo, custom) VALUES ('4chan-2', '/static/svg/icons/badges/badge-4chan-2.svg', 0);
INSERT INTO `logos` (name, logo, custom) VALUES ('4chan', '/static/svg/icons/badges/badge-4chan.svg', 0);
INSERT INTO `logos` (name, logo, custom) VALUES ('8ball', '/static/svg/icons/badges/badge-8ball.svg', 0);
Expand Down
16 changes: 15 additions & 1 deletion src/models/Logo.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ public function getCustom(): bool {
self::invalidateMCRecords();
}

// Set logo as used or unused by passing 1 or 0.
public static async function genSetUsed(
string $logo_name,
bool $used,
): Awaitable<void> {
$db = await self::genDb();
await $db->queryf(
'UPDATE logos SET used = %d WHERE name = %s LIMIT 1',
(int) $used,
$logo_name,
);
self::invalidateMCRecords();
}

// Retrieve a random logo from the table.
public static async function genRandomLogo(): Awaitable<string> {
$all_logos = await self::genAllLogos();
Expand Down Expand Up @@ -148,7 +162,7 @@ public function getCustom(): bool {
$all_enabled_logos = array();
$result =
await $db->queryf(
'SELECT * FROM logos WHERE enabled = 1 AND protected = 0 AND custom = 0',
'SELECT * FROM logos WHERE enabled = 1 AND used = 0 AND protected = 0 AND custom = 0',
);

foreach ($result->mapRows() as $row) {
Expand Down
23 changes: 23 additions & 0 deletions src/models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ protected static function teamFromRow(Map<string, string> $row): Team {
(bool) must_have_idx($team, 'visible'),
);
}
await Logo::genSetUsed(must_have_string($team, 'logo'), true);
}
return true;
}
Expand Down Expand Up @@ -247,6 +248,8 @@ public static function regenerateHash(string $password_hash): bool {
$logo,
);

await Logo::genSetUsed($logo, true);

// Return newly created team_id
$result =
await $db->queryf(
Expand All @@ -256,6 +259,7 @@ public static function regenerateHash(string $password_hash): bool {
$logo,
);

Logo::invalidateMCRecords();
MultiTeam::invalidateMCRecords(); // Invalidate Memcached MultiTeam data.
invariant($result->numRows() === 1, 'Expected exactly one result');
return intval($result->mapRows()[0]['id']);
Expand Down Expand Up @@ -286,6 +290,7 @@ public static function regenerateHash(string $password_hash): bool {
$protected ? 1 : 0,
$visible ? 1 : 0,
);
await Logo::genSetUsed($logo, true);

// Return newly created team_id
$result =
Expand All @@ -296,6 +301,7 @@ public static function regenerateHash(string $password_hash): bool {
$logo,
);

Logo::invalidateMCRecords();
MultiTeam::invalidateMCRecords(); // Invalidate Memcached MultiTeam data.
invariant($result->numRows() === 1, 'Expected exactly one result');
return intval($result->mapRows()[0]['id']);
Expand Down Expand Up @@ -337,13 +343,24 @@ public static function regenerateHash(string $password_hash): bool {
int $team_id,
): Awaitable<void> {
$db = await self::genDb();

// Get and set old logo to unused
$result =
await $db->queryf('SELECT logo FROM teams WHERE id = %d', $team_id);
invariant($result->numRows() === 1, 'Expected exactly one result');
$logo_old = strval($result->mapRows()[0]['logo']);
await Logo::genSetUsed($logo_old, false);

await $db->queryf(
'UPDATE teams SET name = %s, logo = %s , points = %d WHERE id = %d LIMIT 1',
$name,
$logo,
$points,
$team_id,
);
await Logo::genSetUsed($logo, true);

Logo::invalidateMCRecords();
MultiTeam::invalidateMCRecords(); // Invalidate Memcached MultiTeam data.
ActivityLog::invalidateMCRecords('ALL_ACTIVITY'); // Invalidate Memcached ActivityLog data.
}
Expand All @@ -366,6 +383,12 @@ public static function regenerateHash(string $password_hash): bool {
// Delete team.
public static async function genDelete(int $team_id): Awaitable<void> {
$db = await self::genDb();
$result =
await $db->queryf('SELECT logo FROM teams WHERE id = %d', $team_id);
invariant($result->numRows() === 1, 'Expected exactly one result');
$logo = strval($result->mapRows()[0]['logo']);
await Logo::genSetUsed($logo, false);

await $db->queryf(
'DELETE FROM teams WHERE id = %d AND protected = 0 LIMIT 1',
$team_id,
Expand Down

0 comments on commit 6d4f919

Please sign in to comment.