Skip to content

Commit b2961af

Browse files
- Removed truncation and BBCode removal code from backend
- Made game description field 50 characters longer in database to account for autogenerated tournament suffix
1 parent cd5400b commit b2961af

4 files changed

Lines changed: 9 additions & 174 deletions

File tree

deploy/database/schema.game.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ CREATE TABLE game (
1717
last_winner_id SMALLINT UNSIGNED,
1818
tournament_id SMALLINT UNSIGNED,
1919
tournament_round_number SMALLINT UNSIGNED,
20-
description VARCHAR(255) NOT NULL,
20+
description VARCHAR(305) NOT NULL,
2121
chat TEXT,
2222
previous_game_id MEDIUMINT UNSIGNED,
2323
FOREIGN KEY (previous_game_id) REFERENCES game(id)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE game MODIFY description VARCHAR(305) NOT NULL;

src/engine/BMInterfaceTournament.php

Lines changed: 7 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -504,20 +504,20 @@ protected function generate_new_games(BMTournament $tournament) {
504504
);
505505

506506
$roundDescription = 'Tournament Round ' . $gameData['roundNumber'];
507-
$tournDescription = $this->truncate_tournament_description(
508-
$tournament->description,
509-
$roundDescription
510-
);
511-
if ('' != $tournDescription) {
512-
$roundDescription = $tournDescription . ', ' . $roundDescription;
507+
$tournDescription = $tournament->description;
508+
509+
if ('' == trim($tournDescription)) {
510+
$tournDescription = $roundDescription;
511+
} else {
512+
$tournDescription = $tournDescription . ' ' . $roundDescription;
513513
}
514514

515515
$interfaceResponse = $this->game()->create_game_from_button_ids(
516516
array($gameData['playerId1'], $gameData['playerId2']),
517517
array($gameData['buttonId1'], $gameData['buttonId2']),
518518
$buttonNames,
519519
$tournament->gameMaxWins,
520-
$roundDescription,
520+
$tournDescription,
521521
NULL,
522522
0, // needs to be non-null, but also a non-player ID
523523
TRUE,
@@ -535,86 +535,6 @@ protected function generate_new_games(BMTournament $tournament) {
535535
}
536536
}
537537

538-
/**
539-
* Truncate a tournament description so that the tournament round number can be appended
540-
* without exceeding the max length of the field in the database.
541-
*
542-
* This also aggressively removes BBCode markup if there is the possibility of
543-
* breaking BBCode through truncation.
544-
*
545-
* @param string $description
546-
*/
547-
protected function truncate_tournament_description(
548-
$tournDescription,
549-
$roundDescription
550-
) {
551-
// check if appending ", " followed by the tournament round description
552-
// would cause the auto-generated game description to exceed the maximum
553-
// length allowed for the tournament description
554-
if (strlen($tournDescription) + 2 + strlen($roundDescription) >
555-
ApiSpec::TOURNAMENT_DESCRIPTION_MAX_LENGTH) {
556-
// try to strip out non-essential BBCode first
557-
$strippedDescription = $this->strip_nonessential_bbcode($tournDescription);
558-
} else {
559-
$strippedDescription = $tournDescription;
560-
}
561-
562-
if (strlen($strippedDescription) + 2 + strlen($roundDescription) >
563-
ApiSpec::TOURNAMENT_DESCRIPTION_MAX_LENGTH) {
564-
// truncate the tournament description so that there is space for
565-
// "..., " followed by the tournament round description
566-
$truncDescription = substr(
567-
$strippedDescription,
568-
0,
569-
ApiSpec::TOURNAMENT_DESCRIPTION_MAX_LENGTH - 5 - strlen($roundDescription)
570-
) . '...';
571-
} else {
572-
$truncDescription = $strippedDescription;
573-
}
574-
575-
return($truncDescription);
576-
}
577-
578-
/**
579-
* Strip non-essential BBCode from a string
580-
*
581-
* @param string $text
582-
* @return string
583-
*/
584-
protected function strip_nonessential_bbcode($text) {
585-
$tags = array(
586-
'b',
587-
'i',
588-
'u',
589-
's',
590-
'code',
591-
'quote',
592-
'forum'
593-
);
594-
595-
// this regular expression is
596-
// \[ match opening square bracket
597-
// \/? match optional forward slash
598-
// (?: non-capturing group 1 start
599-
// tag1|tag2|... match one of the tags
600-
// ) non-capturing group 1 end
601-
// (?: non-capturing group 2 start
602-
// = match a literal equal character
603-
// [^]]+? match at least one character that is not a
604-
// close square bracket, as few times as possible
605-
// (note that the closing bracket doesn't need to
606-
// be escaped directly after the ^ in the character group)
607-
// )? non-capturing group 2 end, this group is optional
608-
// \] match closing square bracket
609-
$strippedText = preg_replace(
610-
'#\[\/?(?:' . implode('|', $tags) . ')(?:=[^]]+?)?\]#',
611-
'',
612-
$text
613-
);
614-
615-
return $strippedText;
616-
}
617-
618538
/**
619539
* Most of the tournament saving logic
620540
*

test/src/engine/BMInterfaceTournamentTest.php

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -19,90 +19,4 @@ public function test_create_tournament(
1919

2020
}
2121

22-
/**
23-
* @covers BMInterfaceTournament::truncate_tournament_description
24-
*/
25-
public function test_truncate_tournament_description() {
26-
$reflection = new ReflectionMethod($this->object, 'truncate_tournament_description');
27-
$reflection->setAccessible(true);
28-
29-
$roundDescription = 'Tournament Round 1';
30-
31-
$description = 'short';
32-
$shortDescription = $reflection->invoke($this->object, $description, $roundDescription);
33-
$this->assertEquals($description, $shortDescription, 'Short descriptions should not be truncated');
34-
35-
$description = '12345678901234567890123456789012345678901234567890' .
36-
'12345678901234567890123456789012345678901234567890' .
37-
'12345678901234567890123456789012345678901234567890' .
38-
'12345678901234567890123456789012345678901234567890' .
39-
'12345678901234567890123456789012345678901234567890' .
40-
'12345678901234567890123456789012345678901234567890';
41-
$shortDescription = $reflection->invoke($this->object, $description, $roundDescription);
42-
$this->assertEquals(
43-
'12345678901234567890123456789012345678901234567890' .
44-
'12345678901234567890123456789012345678901234567890' .
45-
'12345678901234567890123456789012345678901234567890' .
46-
'12345678901234567890123456789012345678901234567890' .
47-
'12345678901234567890123456789012...',
48-
$shortDescription,
49-
'Long text descriptions should be truncated appropriately'
50-
);
51-
52-
$description = '[forum=1,6]text[/forum]56789012345678901234567890' .
53-
'[forum=1,6]text[/forum]56789012345678901234567890' .
54-
'[forum=1,6]text[/forum]56789012345678901234567890' .
55-
'[forum=1,6]text[/forum]56789012345678901234567890' .
56-
'[forum=1,6]text[/forum]56789012345678901234567890' .
57-
'12345678901234567890123456789012345678901234567890';
58-
$shortDescription = $reflection->invoke($this->object, $description, $roundDescription);
59-
$this->assertEquals(
60-
'text56789012345678901234567890' .
61-
'text56789012345678901234567890' .
62-
'text56789012345678901234567890' .
63-
'text56789012345678901234567890' .
64-
'text56789012345678901234567890' .
65-
'12345678901234567890123456789012345678901234567890',
66-
$shortDescription,
67-
'Markup should be removed even with no late BBCode'
68-
);
69-
70-
$description = '[forum=1,6]text[/forum]56789012345678901234567890' .
71-
'[forum=1,6]text[/forum]56789012345678901234567890' .
72-
'[forum=1,6]text[/forum]56789012345678901234567890' .
73-
'[forum=1,6]text[/forum]56789012345678901234567890' .
74-
'1234567890[forum=1,6]text[/forum]45678901234567890' .
75-
'12345678901234567890123456789012345678901234567890';
76-
$shortDescription = $reflection->invoke($this->object, $description, $roundDescription);
77-
$this->assertEquals(
78-
'text56789012345678901234567890' .
79-
'text56789012345678901234567890' .
80-
'text56789012345678901234567890' .
81-
'text56789012345678901234567890' .
82-
'1234567890text45678901234567890' .
83-
'12345678901234567890123456789012345678901234567890',
84-
$shortDescription,
85-
'Markup should be removed with late BBCode'
86-
);
87-
}
88-
89-
/**
90-
* @covers BMInterfaceTournament::strip_nonessential_bbcode
91-
*/
92-
public function test_strip_nonessential_bbcode() {
93-
$reflection = new ReflectionMethod($this->object, 'strip_nonessential_bbcode');
94-
$reflection->setAccessible(true);
95-
96-
$text = '[button=Abe Caine] is [b][i]very[/i] annoying[/b] ' .
97-
'[spoiler]according to [player=tasha][/spoiler], ' .
98-
'see [forum=1335,32790]this forum thread[/forum]';
99-
$strippedText = $reflection->invoke($this->object, $text);
100-
$this->assertEquals(
101-
'[button=Abe Caine] is very annoying ' .
102-
'[spoiler]according to [player=tasha][/spoiler], ' .
103-
'see this forum thread',
104-
$strippedText,
105-
'BBCode stripping should be correct'
106-
);
107-
}
10822
}

0 commit comments

Comments
 (0)