Skip to content
Permalink
Browse files

Extensions/Commentary

The Commentary-Extension provides now complete basic functionality on
both server and client side. All scenes can be equipped with a
commentary-section, providing individual chats based on
per-scene-configuration per extension.
  • Loading branch information...
Vassyli committed Apr 6, 2016
1 parent 0120db5 commit 5057e01167a0e5960fb4f24aa5432d1011499427
@@ -4,24 +4,57 @@
use App\Http\Controllers\Controller;
use NewLoGD\{Application, Auth};
use NewLoGD\Exceptions\{invalidData, RequestForbidden};
use Extensions\Commentary\Database\Comment;
class CommentaryController extends Controller {
public function all() {
$comments = Application::getEntityManager()->getRepository(Comment::class)->findBy(["section" => "village"], ["createdAt" => "ASC"]);
$return = [];
public function count() {
$numOfComments = Comment::countAll("village");
return ["count" => $numOfComments];
}
public function all($page = 0, $limit = 30) {
$section = $this->getCurrentCharacter()->get("ext.Commentary.section", "village");
$comments = Comment::getBySection($section, $page*$limit, $limit);
$numOfComments = Comment::countAll($section);
$return = ["count" => $numOfComments, "comments" => []];
foreach($comments as $comment) {
$return[] = [
"author" => $comment->getAuthor()->getName(),
"authorid" => $comment->getAuthor()->getId(),
"comment" => $comment->getFinalComment(),
$author = $comment->getAuthor();
$return["comments"][] = [
"author" => is_null($author) ? NULL : $author->getName(),
"authorid" => is_null($author) ? NULL : $author->getId(),
"line" => $comment->getFinalComment(),
"date" => $comment->getCreatedAt()->getTimestamp(),
"section" => $comment->getSection(),
];
}
return $return;
}
public function post() {
$character = $this->getCurrentCharacter();
$section = $character->get("ext.Commentary.section", NULL);
if($section === NULL) {
throw new RequestForbidden();
}
try {
$comment = Comment::create([
"author" => $character,
"section" => $section,
"line" => $_POST["comment"]??"",
"maxlength" => $character->get("ext.Commentary.maxlength", 255),
]);
return "OK?";
}
catch(\Exception $e) {
throw new invalidData($e->getMessage());
}
}
}
@@ -11,7 +11,7 @@
"author" => "Basilius Sauter",
"routes" => [
[GET, "", "#CommentaryController@all"],
[GET, "/page_{id}_{id}", "#CommentaryController@page"],
[GET, "/page_{id}_{id}", "#CommentaryController@all"],
[POST, "", "#CommentaryController@post"],
],
];
@@ -3,12 +3,26 @@
namespace Extensions\Commentary;
use Database\CharacterScene;
use NewLoGD\ChangeableScene;
class Scene {
public function change(CharacterScene $c_scene) {
$c_scene->addParagraph("Some people are talking to each other.");
$c_scene->addParagraph("@{Commentary|List|comment}");
$c_scene->addParagraph("@{Commentary|SimpleForm|name:comment|maxlength:255}");
$c_scene->addAction();
class Scene extends ChangeableScene {
public function change() {
$this->addParagraph("Some people are talking to each other.");
$this->getCurrentCharacter()->set("ext.Commentary.section", $this->getExtension()->get("section", "village"));
$this->getCurrentCharacter()->set("ext.Commentary.maxlength", $this->getExtension()->get("maxlength", 255));
$this->addWidget(self::WIDGET_LIST, [
"reversed",
]);
$this->addWidget(self::WIDGET_SIMPLEFORM, [
"text" => "Chat with other people",
"submit" => "Submit",
"name" => "comment",
"maxlength" => 255
]);
#$this->addParagraph("@{Commentary|List|comment}");
#$this->addParagraph("@{Commentary|SimpleForm|name:comment|maxlength:255}");
#$this->addAction();
}
}
@@ -2,6 +2,11 @@
namespace Extensions\Commentary\Database;
use Database\Character;
use NewLoGD\Application;
use function NewLoGD\Helper\normalizeLineBreaks;
/**
* ORM for Character table
* @Entity
@@ -49,13 +54,13 @@ class Comment {
private $createdAt;
public function __construct() {
$this->createdAt = new DateTime("now");
$this->createdAt = new \DateTime("now");
}
public function getId() { return $this->id; }
public function getAuthor() { return $this->author; }
public function setAuthor(Character $character) { $this->character = $character; }
public function setAuthor(Character $author) { $this->author = $author; }
public function getBody() : string { return $this->body; }
public function setBody(string $body) { $this->body = normalizeLineBreaks($body); }
@@ -69,9 +74,14 @@ public function setSection(string $section) { $this->section = $section; }
public function getCreatedAt() { return $this->createdAt; }
public function getFinalComment() {
$author = $this->getAuthor();
if(is_null($author)) {
return $this->getBody();
}
switch($this->getEmote()) {
case self::EMOTE_ENV:
return $this->getBody();
return " (". $this->getAuthor(). ")" . $this->getBody();
break;
case self::EMOTE_3RD:
return $this->getAuthor()->getName() . " " .$this->getBody();
@@ -82,4 +92,52 @@ public function getFinalComment() {
break;
}
}
public static function getBySection($section, $offset = NULL, $limit = NULL) {
return Application::getEntityManager()->getRepository(Comment::class)->findBy(["section" => $section], ["createdAt" => "DESC"], $limit, $offset);
}
public static function countAll($section) {
$query = Application::getEntityManager()->createQueryBuilder()
->select("COUNT(t.id)")
->from(self::class, "t")
->where("t.section = :section")
->setParameter("section", $section)
->getQuery();
return $query->getSingleScalarResult();
}
public static function create($variables) {
if($variables["line"] === "") {
throw new \Exception("Commentary line should not be empty.");
}
$comment = new Comment();
$comment->setSection($variables["section"]);
$comment->setAuthor($variables["author"]);
$comment->setLine($variables["line"], $variables["maxlength"]);
Application::getEntityManager()->persist($comment);
}
public function setLine(string $line, int $maxlength) {
if(\mb_substr($line, 0, 3) === "/me") {
$this->setEmote(self::EMOTE_3RD);
$start = 3;
$end = $maxlength+3;
}
elseif(\mb_substr($line, 0, 2) === "/X") {
$this->setEmote(self::EMOTE_ENV);
$start = 3;
$end = $maxlength+2;
}
else {
$this->setEmote(self::EMOTE_NONE);
$start = 0;
$end = $maxlength;
}
$this->setBody(\trim(\mb_substr($line, $start, $end)));
}
}
@@ -41,8 +41,8 @@ public function __construct(Application $app, HttpResponse $response) {
$this->response = $response;
}
protected function callback($callback, $args) {
if(is_callable($callback)) {
protected function callback($callback, $args) {
if(\is_object($callback) && $callback instanceof \Closure) {
if(empty($args)) {
$args = array_merge([$this, $callback], $args);
}
@@ -18,8 +18,18 @@ class SceneExtension {
* @Id @Column(type="string")
*/
private $extension;
/**
* @var array Extension configuration
* @Column(type="json_array")
*/
private $config = [];
public function getScene() { return $this->scene; }
public function getExtension() { return $this->extension; }
public function getConfig() : array { return $this->config; }
public function setConfig(array $config) { $this->config = $config; }
public function set($key, $value) { $this->config[$key] = $value; }
public function get($key, $default = NULL) { return $this->config[$key]??$default;}
}
Oops, something went wrong.

0 comments on commit 5057e01

Please sign in to comment.
You can’t perform that action at this time.