yaz/commit

split game data handler into one handler for fetching game and one for adding tags

authorMichiel Hildebrand
Sat Feb 5 17:06:06 2011 +0100
committerMichiel Hildebrand
Sat Feb 5 17:06:06 2011 +0100
commitfa6f1ab32e97ecb1a158d8d10fa5bb82a2857801
tree3c6d95be476c33b57dd4453014d2ec9893af19c7
parent31eb6e24477197914d2aefc875fe1eb690c4c386
Diff style: patch stat
diff --git a/applications/yaz_game.pl b/applications/yaz_game.pl
index b0710d1..984c26d 100644
--- a/applications/yaz_game.pl
+++ b/applications/yaz_game.pl
@@ -25,6 +25,8 @@
 :- use_module(components(label)).
 :- use_module(components(yaz_page)).
 
+:- use_module(api(video_frames)).
+
 :- setting(match_interval, integer, 10000,
 	   'Interval in which tags can be matched (in miliseconds)').
 :- setting(max_player_count, integer, 8,
@@ -42,10 +44,16 @@
 :- rdf_meta
         cond_object_assert(r,r,o,r).
 
+channel('http://g.bbcredux.com/programme/bbcone/2011-02-05/12-00-00',
+	video('bbc_news_05022011.flv'),
+        'BBC News 5 Feb 2011 12:00',
+        1101).
+/*
 channel('http://g.bbcredux.com/programme/bbcone/2010-11-21/22-00-00',
 	'http://g.bbcredux.com/programme/5542126155790075626/download/12340-1295719806-48faefbd0b0f9057050098ef47daf3a5/flash.flv',
 	'BBC News 21 Nov 2010 22:00',
 	1101).
+*/
 
 
 :- http_handler(yaz(game), http_yaz_game, []).
@@ -84,7 +92,7 @@ http_yaz_game(Request) :-
 	    game_video_start(Game, URL, PlayHead),
 	    PlayerObj = {player:P, name:Name, score:Score},
 	    findall(PlayerObj, active_player(Game, P, Name, Score), Players),
-	    TagObj = json([annotation=A, label=Tag, time=Time, match=Match]),
+	    TagObj = json([id=A, label=Tag, time=Time, match=Match]),
 	    findall(Time-TagObj, user_tag(Game, User, A, Tag, Time, Match), Tags0),
 	    keysort(Tags0, Tags1),
 	    reverse(Tags1, Tags2),
@@ -117,8 +125,9 @@ html_home_page(Channels) :-
  			]).
 
 html_channels([]) --> !.
-html_channels([channel(URL, Video, Title)|Vs]) -->
-	{ http_link_to_id(serve_video_frame, [url(Video),time(5)], Frame),
+html_channels([channel(URL, Video0, Title)|Vs]) -->
+	{ http_absolute_location(Video0, Video, []),
+	  http_link_to_id(serve_video_frame, [url(Video),time(5)], Frame),
 	  (   waiting_game(URL, _Game, Players)
 	  ->  length(Players, Count)
 	  ;   Count = 0,
@@ -263,7 +272,6 @@ html_page_yui(Game, URL, User, PlayHead, Players, Tags) -->
 	  http_absolute_location(js('videoplayer/videoplayer.js'), VideoPlayer, []),
 	  http_absolute_location(js('game/input.js'), GameInput, []),
 	  http_absolute_location(js('game/players.js'), GamePlayers, []),
-	  http_location_by_id(http_game_data, DataServer),
 	  setting(request_interval, RequestInterval),
 	  video_source(URL, Video)
 	},
@@ -296,19 +304,33 @@ html_page_yui(Game, URL, User, PlayHead, Players, Tags) -->
 						  user:User,
 						  players:Players
 						 })),
-		  \js_support_functions(Game, User, DataServer),
+		  \js_support_functions(Game, User),
 		  \js_call('Y.later'(RequestInterval, symbol('Y'),
 				     symbol(fetchData), symbol({}), symbol(true))),
 		  \js_call('videoPlayer.render'('#videoplayer')),
-		  \js_input_method
+		  \js_yui3_on(gameInput, addTag, addTag)
  		]).
 
-js_support_functions(Game, User, DataServer) -->
+js_support_functions(Game, User) -->
+	{ http_location_by_id(http_game_add_tag, AddTag),
+	  http_location_by_id(http_game_data, GameData)
+	},
+
+	js_function_decl(addTag, [e],
+			 \[
+'   var node = e.node,
+	data = {game:"',Game,'",user:"',User,'",playhead:videoPlayer.getTime()*1000};
+    data.tag = e.label;
+    Y.io("',AddTag,'", {data: data,
+			on: {success:function(i,o) {
+			      gameInput._tagNodes[Y.JSON.parse(o.responseText).id]=node}},
+		       });\n'
+			  ]),
+
 	js_function_decl(fetchData, [e],
 			 \[
 '   var data = {game:"',Game,'",user:"',User,'",playhead:videoPlayer.getTime()*1000};
-    if(e.tag) {data.tag=e.tag;}
-    Y.io("',DataServer,'", {data: data,
+    Y.io("',GameData,'", {data: data,
 			    on: {success:handleResponse}
 			   });\n'
 			  ]),
@@ -316,44 +338,10 @@ js_support_functions(Game, User, DataServer) -->
 	js_function_decl(handleResponse, [id, o],
 			 \[
 '    var r = Y.JSON.parse(o.responseText);
-    gamePlayers.updatePlayers(r.players);
+    gamePlayers.set("players", r.players);
     gameInput.updateTags(r.tags);\n'
 			  ]).
 
-js_input_method -->
-	{ setting(ac, true) },
-	!,
-	html(['gameInput.set("autoAddTag", false);\n',
-	      \js_freebase_suggest
-	     ]).
-js_input_method -->
-	js_yui3_on(gameInput, enter, fetchData).
-
-js_freebase_suggest -->
-	html_requires('http://freebaselibs.com/static/suggest/1.2.1/suggest.min.css'),
-	html_requires(js('jquery/jquery-1.4.2.min.js')),
-   	html_requires('http://freebaselibs.com/static/suggest/1.2.1/suggest.min.js'),
- 	html(\[
-'$("#taginput").suggest({parent:"#suggest",
-			 soft:true,
-			 required:true,
-			 nomatch:"",
-			 status:["","","",""],
-			 flyout:false})\n',
-'.bind("fb-select", function(e, data) {
-     var tag = 	{label:data.name, uri:data.id};
-     fetchData(tag);
-     gameInput.addTag(tag);
-})\n',
-'.bind("fb-required", function(e) {
-    if($("#taginput:focus").length>0) {
-	var tag = {label:$("#taginput").val()};
-        $(".fbs-pane").css("display", "none");
-        fetchData(tag);
-        gameInput.addTag(tag);}
- });\n'
-	      ]).
-
 
 		 /*******************************
 		 *	    game events		*
@@ -509,12 +497,14 @@ tag_match_(Tag, Game, Annotation, exact) :-
 
 
 :- http_handler(yaz('gamedata'), http_game_data, []).
+:- http_handler(yaz('gameaddtag'), http_game_add_tag, []).
 
-%%	http_game_data(+Request)
+
+%%	http_game_add_tag(+Request)
 %
 %	Return JSON object with players in a game.
 
-http_game_data(Request) :-
+http_game_add_tag(Request) :-
  	http_parameters(Request,
 			[  game(Game,
 				[uri, description('URL of current Game')]),
@@ -523,26 +513,37 @@ http_game_data(Request) :-
 			   playhead(Playhead,
 				[number, description('Current time of the video play head (in miliseconds)')]),
 			   tag(Tag,
-			       [optional(true),
-				description('Optionally a new tag can be added')])
+			       [description('Optionally a new tag can be added')])
 			]),
 	setting(match_interval, Interval),
-	(   nonvar(Tag)
- 	->  add_tag(Game, User, Tag, Playhead, AnnotationId),
-	    match_existing_tags(Game, User, Tag, Playhead, Interval, Matches),
-	    update_scores(Matches, AnnotationId, Game, User)
- 	;   true
-	),
+ 	add_tag(Game, User, Tag, Playhead, AnnotationId),
+	match_existing_tags(Game, User, Tag, Playhead, Interval, Matches),
+	update_scores(Matches, AnnotationId, Game, User),
+ 	reply_json(json([id=AnnotationId])).
+
+
+%%	http_game_data(+Request)
+%
+%	Return JSON object with players in a game.
+
+http_game_data(Request) :-
+ 	http_parameters(Request,
+			[  game(Game,
+				[uri, description('URL of current Game')]),
+			   user(User,
+				[uri, description('URL of current User')]),
+			   playhead(Playhead,
+				[number, description('Current time of the video play head (in miliseconds)')])
+ 			]),
+	setting(match_interval, Interval),
 	PlayerObj = json([player=P, name=Name, score=Score]),
 	findall(PlayerObj,
 		active_player(Game, P, Name, Score),
 		Players),
-
-	TagObj = json([annotation=A, label=Label, time=Time, match=Match]),
+	TagObj = json([id=A, label=Label, time=Time, match=Match]),
 	findall(TagObj,
 		user_matched_tag(Game, User, Playhead, Interval, A, Label, Time, Match),
 		Tags),
-
 	reply_json(json([user=User, players=Players, tags=Tags])).
 
 
@@ -647,7 +648,8 @@ http_waiting_data(Request) :-
 %	Store info of channel videos in RDF.
 
 assert_channel_info :-
-	rdf_transaction((   channel(URL, Video, Title, Duration),
+	rdf_transaction((   channel(URL, Video0, Title, Duration),
+			    http_absolute_location(Video0, Video, []),
 			    add_video(URL, Video, Title, Duration),
  			    fail
 			;   true
@@ -659,7 +661,7 @@ assert_channel_info :-
 
 add_video(URL, Video, Title, Duration) :-
 	%rdf_retractall(URL, _, _),
-	cond_object_assert(URL, pprime:source, Video, video),
+ 	cond_object_assert(URL, pprime:source, Video, video),
 	cond_object_assert(URL, dc:title, literal(Title), video),
 	cond_object_assert(URL, pprime:duration, literal(Duration), video).
 
diff --git a/lib/yaz_util.pl b/lib/yaz_util.pl
index ad5f994..640840d 100644
--- a/lib/yaz_util.pl
+++ b/lib/yaz_util.pl
@@ -28,6 +28,7 @@
 :- use_module(library(http/http_dispatch)).
 :- use_module(library(http/json)).
 :- use_module(library(http/json_convert)).
+:- use_module(library(http/http_path)).
 
 :- rdf_meta
         is_annotation_event(r),
diff --git a/web/js/game/input.js b/web/js/game/input.js
index 31b5513..8571840 100644
--- a/web/js/game/input.js
+++ b/web/js/game/input.js
@@ -17,16 +17,7 @@ YUI.add('game-input', function(Y) {
 			value: null
 		},
 		tags: {
-			value: [],
-			setter: function(val, attr) {
-				if(Y.Lang.isArray(val)) {
-					return val;
-				} else {
-					var tags = this.get("tags");
-					tags.unshift(val);
-					return tags;
-				}
-			}
+			value: []
 		}
 	};
 
@@ -37,6 +28,7 @@ YUI.add('game-input', function(Y) {
 	Y.extend(GameInput, Y.Base, {
 
 		initializer: function() {
+			this._tagNodes = {};			
 			if(this.get("input")) {
 				this.input = Y.one(this.get("input"));
 				this.input.on("keydown", this._onKeyDown, this);
@@ -53,17 +45,12 @@ YUI.add('game-input', function(Y) {
 		
 		updateTags : function(tags) {
 			for(var i=0; i < tags.length; i++) {
-				var tag = tags[i],
-					node = this._tagNode(tag.annotation);
-					
-				if(!node) {
-					this._addTag(tag);
-				} else if(tag.match) {
-					var match = tag.match;
-					if(match.score) {
-						node.one('.score').setContent(match.score);
+				var tag = tags[i];
+				if(tag.id) {
+					var node = this._tagNodes[tag.id];
+					if(node) {
+						this._setStyle(node, tag.match);
 					}
-					this._setStyle(node, match);
 				}
 			}
 		},
@@ -82,13 +69,13 @@ YUI.add('game-input', function(Y) {
 			var tags = this.get("tags");
  			if(tags) {
 				for(var i=0; i < tags.length; i++) {
-					var tag = tags[i];
-					tag.node = Y.Node.create(this.formatTag(tag));
-					this._setStyle(tag.node, tag.match);
-					this.list.prepend(tag.node);
+					var tag = tags[i],
+						tagNode = this._addTag(tag);
+					if(tag.id) {
+						this._tagNodes[tag.id] = tagNode;
+					}
 				}
 			}
-			this.set("tags", tag);
 		},
 		
 		formatTag : function(tag) {
@@ -99,10 +86,10 @@ YUI.add('game-input', function(Y) {
 		},
 		
 		_addTag : function(tag) {
-			tag.node = Y.Node.create(this.formatTag(tag));
-			this.set("tags", tag);
-			this._setStyle(tag.node, tag.match);
-			this.list.prepend(tag.node);
+			var tagNode = Y.Node.create(this.formatTag(tag));
+			this._setStyle(tagNode, tag.match);
+			this.list.prepend(tagNode);
+			return tagNode;
 		},
 		
 		_setStyle : function(node, match) {
@@ -111,23 +98,18 @@ YUI.add('game-input', function(Y) {
 				if(match.order===1) {
 					node.addClass("first");
 				}
-			}
-		},
-		
-		_tagNode : function(id) {
-			var tags = this.get("tags");
-			for (var i=0; i < tags.length; i++) {
-				if(tags[i].annotation === id) {
-					return tags[i].node;
+				if(match.score) {
+					node.one('.score').setContent(match.score);
 				}
 			}
 		},
 		
 		_onKeyDown : function(e) {
 			if(e.charCode === 13) {
-				var tag = {tag:this.input.get("value")};
-				this.input.set("value", "");
-				this.fire("enter", tag);
+				var label = this.input.get("value"),
+					node = this._addTag({label:label});
+				this.input.set("value", "");				
+				this.fire("addTag", {label:label, node:node});
 			}
 		}
 	});