changeset 998:474a46e240ff

plugin tictactoe: now has a timeout of inactivity
author David Demelier <markand@malikania.fr>
date Fri, 12 Feb 2021 10:09:46 +0100
parents 0db97f67f31f
children c50f954d8c67
files CHANGES.md plugins/tictactoe/tictactoe.7 plugins/tictactoe/tictactoe.js
diffstat 3 files changed, 41 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/CHANGES.md	Thu Feb 11 22:14:47 2021 +0100
+++ b/CHANGES.md	Fri Feb 12 10:09:46 2021 +0100
@@ -21,6 +21,10 @@
 - New `plugin-template` and `plugin-path` command which are synonyms of
   `plugin-config` but for templates and paths respectively.
 
+plugins:
+
+- tictactoe: now has a timeout in case of inactivity.
+
 misc:
 
 - Split irccd-api manual page into individual irccd-api-<module> for a better
--- a/plugins/tictactoe/tictactoe.7	Thu Feb 11 22:14:47 2021 +0100
+++ b/plugins/tictactoe/tictactoe.7	Fri Feb 12 10:09:46 2021 +0100
@@ -35,8 +35,7 @@
 section:
 .Pp
 .Bd -literal
-[plugins]
-tictactoe = ""
+plugin tictactoe
 .Ed
 .\" USAGE
 .Sh USAGE
@@ -45,11 +44,9 @@
 .Ar "x y"
 where x targets the column and y the row.
 .Pp
-To verify target opponent, this plugins first requests the names on the channel
-to ensures a valid player.
-.Pp
 If one of the players leaves the channel (either by kick or part) the game is
-aborted silently.
+aborted silently. If the player don't play in the delay of five minutes, the
+game is aborted to let another game starts.
 .Pp
 Example when starting a game:
 .Bd -literal -offset Ds
@@ -99,9 +96,7 @@
 .Sh TEMPLATES
 The
 .Nm
-plugin supports the following templates in
-.Va [templates.tictactoe]
-section:
+plugin supports the following templates:
 .Bl -tag -width 8n -offset Ds
 .It Va draw
 When the game ended with no winner.
@@ -111,6 +106,8 @@
 .It Va running
 The game is already running. Keywords:
 .Em origin .
+.It Va timeout
+The player didn't play within its turn after a certain amount of time.
 .It Va turn
 Message sent when current player change.
 .It Va used
--- a/plugins/tictactoe/tictactoe.js	Thu Feb 11 22:14:47 2021 +0100
+++ b/plugins/tictactoe/tictactoe.js	Fri Feb 12 10:09:46 2021 +0100
@@ -28,15 +28,17 @@
 // Modules.
 var Plugin = Irccd.Plugin;
 var Util = Irccd.Util;
+var Timer = Irccd.Timer;
 
 // Formats.
 Plugin.templates = {
-	"draw":         "nobody won",
-	"invalid":      "#{nickname}, please select a valid opponent",
-	"running":      "#{nickname}, the game is already running",
-	"turn":         "#{nickname}, it's your turn",
-	"used":         "#{nickname}, this square is already used",
-	"win":          "#{nickname}, congratulations, you won!"
+	"draw":         "Nobody won.",
+	"invalid":      "#{nickname}, please select a valid opponent.",
+	"running":      "#{nickname}, the game is already running.",
+	"turn":         "#{nickname}, it's your turn.",
+	"used":         "#{nickname}, this square is already used.",
+	"win":          "#{nickname}, congratulations, you won!",
+	"timeout":      "Aborted due to #{nickname} inactivity."
 };
 
 /**
@@ -176,7 +178,7 @@
 
 	var channels = server.info().channels;
 	var ch;
-	
+
 	for (var i = 0; i < channels.length; ++i) {
 		if (channels[i].name === channel) {
 			ch = channels[i];
@@ -192,11 +194,26 @@
 }
 
 /**
+ * Function called when a timeout occured.
+ *
+ * @param game the game to destroy
+ */
+Game.timeout = function (game)
+{
+	var kw = Game.keywords(game.server, game.channel);
+
+	kw.nickname = game.players[game.player];
+	game.server.message(game.channel, Util.format(Plugin.templates.timeout, kw));
+	Game.remove(game.server, game.channel);
+}
+
+/**
  * Show the game grid and the next player line.
  */
 Game.prototype.show = function ()
 {
 	var kw = Game.keywords(this.server, this.channel);
+	var self = this;
 
 	// nickname is the current player.
 	kw.nickname = this.players[this.player];
@@ -212,6 +229,12 @@
 		this.server.message(this.channel, Util.format(Plugin.templates.draw, kw));
 	else
 		this.server.message(this.channel, Util.format(Plugin.templates.turn, kw));
+
+	// Create a timer in case of inactivity (5 minutes).
+	this.timer = new Irccd.Timer(Irccd.Timer.Single, 300000, function () {
+		Game.timeout(self);
+	});
+	this.timer.start();
 }
 
 /**
@@ -246,6 +269,7 @@
 		return false;
 	}
 
+	this.timer.stop();
 	this.grid[row][column] = this.player === 0 ? 'x' : 'o';
 
 	// Do not change if game is finished.