yaz/commit

fix scoring

authorMichiel Hildebrand
Thu Feb 10 12:37:58 2011 +0100
committerMichiel Hildebrand
Thu Feb 10 12:37:58 2011 +0100
commit43c1fe6fb10d6d7bdf3fd7b2017c348d16af09a5
tree5fd39ee30f10bf6ca8323accff5935224b48a61f
parent99274d88caa655b8d9660b4ed0353fc211180d2c
Diff style: patch stat
diff --git a/api/video_frames.pl b/api/video_frames.pl
index d5b837d..42edbbc 100644
--- a/api/video_frames.pl
+++ b/api/video_frames.pl
@@ -13,6 +13,7 @@
 :- use_module(library(semweb/rdf_db)).
 :- use_module(user(user_db)).
 :- use_module(library(yaz_util)).
+:- use_module(library(thread_pool)).
 
 % Windows: 'cmd.exe /C ffmpeg.exe'
 :- setting(frame_program, atom, ffmpeg,
@@ -28,6 +29,8 @@
 http:location(video, root(video), []).
 user:file_search_path(video, videos).
 
+:- thread_pool_create(video, 20, []).
+
 :- http_handler(root(videoframe), serve_video_frame, []).
 :- http_handler(video(.), serve_video, [spawn(video), prefix]).
 
diff --git a/applications/yaz_game.pl b/applications/yaz_game.pl
index 37ed4da..c2b6062 100644
--- a/applications/yaz_game.pl
+++ b/applications/yaz_game.pl
@@ -2,7 +2,11 @@
 	  [ http_yaz_game/1,
 	    match_score/5,
 	    update_tag_score/4,
-	    active_player/4
+	    active_players/2,
+	    active_player/4,
+	    game_player_score/3,
+	    set_game_player_score/3,
+	    update_player_score/3
 	  ]).
 
 :- use_module(library(http/http_dispatch)).
@@ -687,3 +691,9 @@ cond_object_assert(_,_,_,_).
 
 
 :- assert_channel_info.
+
+
+game_player_score(Game, User, Score) :-
+	player_score(Game, User, Score).
+set_game_player_score(Game, User, Score) :-
+	assert(player_score(Game, User, Score)).
diff --git a/applications/yaz_mgarden.pl b/applications/yaz_mgarden.pl
index 932fb00..3612d99 100644
--- a/applications/yaz_mgarden.pl
+++ b/applications/yaz_mgarden.pl
@@ -26,15 +26,18 @@
 
 :- use_module(components(label)).
 :- use_module(components(yaz_page)).
+:- use_module(applications(yaz_game)).
 
 :- http_handler(yaz(mgarden), http_yaz_mgarden, []).
 :- http_handler(yaz('data/confirmmatch'), http_yaz_api_confirm_match, []).
 :- http_handler(yaz('data/mgarden'), http_yaz_api_mgarden_data, []).
 
-
 :- setting(request_interval, integer, 2000,
 	   'Interval between requests to the server (in miliseconds)').
 
+:- dynamic
+	player_score/3.
+
 %%	http_yaz_mgarden(+Request)
 %
 %	Emit the a video player with a tag carousel running along side
@@ -53,24 +56,29 @@ http_yaz_mgarden(Request) :-
 				[default(CurrentUser),
 				 description('When set only annotations created by this user are shown')])
   			]),
+	process_video(Process, Video),
 	Options = [process(Process),
 		   user(User)
   		  ],
-	(   var(Video),
-	    var(Process)
-	->  fail % proper error
-	;   var(Video)
-	->  rdf(Process,opmv:used,Video)
-	;   true
-	),
 	create_user_process(CurrentUser,
 			    [rdf:type=pprime:'mgarden',
 			     opmv:used=Video
 			    ], _GardenProcess),
+	set_player_score(User, Process),
 	video_annotations(Video, As0, Options),
 	sort_by_arg(As0, 2, As),
  	tag_matches(As, User, Process, 10000, Annotations),
- 	html_video_page(Process, Video, CurrentUser, Annotations, 0, Options).
+	active_players(Process, Players),
+ 	html_video_page(Process, Video, CurrentUser, Players, Annotations, 0, Options).
+
+process_video(Process, Video) :-
+	(   var(Video),
+	    var(Process)
+	->  fail % proper error
+	;   var(Video)
+	->  rdf(Process,opmv:used,Video)
+	;   true
+	).
 
 
 tag_matches([], _, _, _, []).
@@ -141,15 +149,15 @@ tagentry_in_interval(Game, User, Tag, Time, Interval, E, Tag1) :-
 %
 %	Emit an HTML page with a video player and a tag carousel.
 
-html_video_page(Game, Video, User, Annotations, StartTime, Options) :-
+html_video_page(Game, Video, User, Players, Annotations, StartTime, Options) :-
 	reply_html_page(yaz,
 			[ title(['YAZ - ', Video])
 			],
 			[ \html_requires(css('player.css')),
- 			  div(class('video-results'),
+  			  div(class('video-results'),
 			      \html_video_page_containers(Video, Options)),
 			 script(type('text/javascript'),
-				\html_video_page_yui(Game, Video, User, Annotations, StartTime, Options))
+				\html_video_page_yui(Game, Video, User, Players, Annotations, StartTime, Options))
 			]).
 
 html_video_page_containers(Video, _Options) -->
@@ -159,25 +167,28 @@ html_video_page_containers(Video, _Options) -->
  	       div(id(video),
 		   [ div(id(tagplayer), []),
 		     div(id(videoplayer), [])
-		   ])
+		   ]),
+	       div(id(players), [])
     	     ]).
 
-html_video_page_yui(Game, Video, User, Annotations, StartTime, _Options) -->
+html_video_page_yui(Game, Video, User, Players, Annotations, StartTime, _Options) -->
 	{ video_source(Video, Src),
  	  http_absolute_location(js('videoplayer/'), FilePath, []),
 	  http_absolute_location(js('videoplayer/videoplayer.js'), VideoPlayer, []),
 	  http_absolute_location(js('tagcarousel/tagcarousel.js'), TagCarousel, []),
+	  http_absolute_location(js('game/players.js'), GamePlayers, []),
 	  setting(request_interval, RequestInterval),
   	  annotation_to_json(Annotations, JSONTags)
    	},
 	html_requires(js('videoplayer/swfobject.js')),
- 	js_yui3([{modules:{'video-player':{fullpath:VideoPlayer},
+ 	js_yui3([{modules:{'game-players':{fullpath:GamePlayers},
+			   'video-player':{fullpath:VideoPlayer},
 			   'tag-carousel':{fullpath:TagCarousel}
   			  }}
 		],
 		[node,event,widget,anim,
 		 'querystring-stringify-simple','io','json',
-  		 'video-player','tag-carousel'
+  		 'video-player','tag-carousel','game-players'
  		],
 		[ \js_new(videoPlayer,
 			  'Y.mazzle.VideoPlayer'({filepath:FilePath,
@@ -195,6 +206,11 @@ html_video_page_yui(Game, Video, User, Annotations, StartTime, _Options) -->
 						  confirm:symbol(true),
 						  topIndent:symbol(false)
  						 })),
+		  \js_new(gamePlayers,
+			  'Y.mazzle.GamePlayers'({container:'#players',
+						  user:User,
+						  players:Players
+ 						 })),
     		  'var oldTime;\n',
  		  \js_call('videoPlayer.render'('#videoplayer')),
 		  \js_call('tagCarousel.render'('#tagplayer')),
@@ -219,7 +235,8 @@ js_support_functions(Game, User) -->
 	js_function_decl(handleResponse, [id, o],
 			 \[
 '    var r = Y.JSON.parse(o.responseText);
-     tagCarousel.updateMatch(r);\n'
+     tagCarousel.updateMatch(r.confirm);
+     gamePlayers.set("players", r.players);\n'
 			  ]).
 
 js_tag_select -->
@@ -276,27 +293,22 @@ http_yaz_api_confirm_match(Request) :-
 			  match(Match,
 				[description('type of the match')])
 			]),
- 	debug(yaz(update), 'confirm ~w match between ~w and ~w',
-	      [Match, Source, Target]),
- 	(   confirmed(Match, Game, Action, Source, Target, Id),
-	    rdf(Id, pprime:creator, User)
-	->  true
-	;   rdf_bnode(Id),
-	    rdfh_transaction((rdfh_assert(Id, pprime:action, literal(Action)),
-			      rdfh_assert(Id, pprime:match, literal(Match)),
-			      rdfh_assert(Id, pprime:process, Game),
-			      rdfh_assert(Id, pprime:creator, User),
-			      rdfh_assert(Id, pprime:matchSource, Source),
-			      rdfh_assert(Id, pprime:matchTarget, Target)))
-	),
-  	reply_json(json([id=Id
-  			])).
+ 	debug(game, '~w confirm ~w match between ~w and ~w',
+	      [User, Match, Source, Target]),
+  	rdf_bnode(Id),
+	rdfh_transaction((rdfh_assert(Id, pprime:action, literal(Action)),
+			  rdfh_assert(Id, pprime:match, literal(Match)),
+			  rdfh_assert(Id, pprime:process, Game),
+			  rdfh_assert(Id, pprime:creator, User),
+			  rdfh_assert(Id, pprime:matchSource, Source),
+			  rdfh_assert(Id, pprime:matchTarget, Target))),
+  	reply_json(json([id=Id])).
 
 
 confirmed(specific, Game, Action, Source, Target, Id) :-
 	!,
-	(   confirmed_(generic, Game, Action, Source, Target, Id)
-	;   confirmed_(specific, Game, Action, Target, Source, Id)
+	(   confirmed_(specific, Game, Action, Source, Target, Id)
+	;   confirmed_(generic, Game, Action, Target, Source, Id)
 	).
 confirmed(generic, Game, Action, Source, Target, Id) :-
 	!,
@@ -318,9 +330,9 @@ confirmed_(Match, Game, Action, Source, Target, Id) :-
 match_score(stem, 75).
 match_score(synonym, 100).
 match_score(specific, 150).
-match_score(generic, 125).
+match_score(generic, 100).
 match_score(related, 75).
-
+match_score(sibling, 125).
 
 %%	http_yaz_api_mgarden_data(+Request)
 %
@@ -334,13 +346,49 @@ http_yaz_api_mgarden_data(Request) :-
 			       [description('URL of the game')])
 			]),
 	current_user_process(Process),
+
 	Obj = json([id=Id, score=Score]),
 	findall(Obj, user_confirmed(User, Process, Game, Id, Score), Confirmed),
-  	reply_json(Confirmed).
+ 	rdf_transaction(update_scores(Confirmed, Process, Game, User)),
+
+	PlayerObj = json([player=P, name=N, score=S]),
+	findall(PlayerObj,
+		active_player(Game, P, N, S),
+		Players),
+  	reply_json(json([confirm=Confirmed, players=Players])).
 
 user_confirmed(User, Process, Game, Id, Score) :-
 	rdf(Id, pprime:creator, User, Process),
 	confirmed_(Match, Game, Action, Source, Target, Id),
+	\+ rdf(Id, pprime:score, _),
 	confirmed(Match, Game, Action, Source, Target, Id1),
 	\+ rdf(Id1, pprime:creator, User),
 	match_score(Match, Score).
+
+update_scores([], _, _,_).
+update_scores([json([id=Id, score=Score])|T], Process, Game, User) :-
+	rdf(Id, pprime:matchSource, Source),
+  	update_garden_tag_score(Id, Source, Process, Score),
+	update_player_score(Game, User, Score),
+	update_scores(T, Process, Game, User).
+
+update_garden_tag_score(Confirm, Entry, Process, Score) :-
+	(   rdf(Confirm, pprime:score, literal(CS))
+ 	->  S is CS+Score
+ 	;   rdf(Entry, pprime:score, literal(ES))
+ 	->  S is ES+Score
+ 	;   S = Score
+ 	),
+  	rdf_retractall(Confirm, pprime:score, _),
+  	rdf_assert(Confirm, pprime:score, literal(S), Process).
+
+
+
+		 /*******************************
+		 *	       players		*
+		 *******************************/
+
+set_player_score(User, Game) :-
+	game_player_score(Game, User, _).
+set_player_score(User, Game) :-
+	set_game_player_score(Game, User, 0).
diff --git a/web/css/player.css b/web/css/player.css
index a401d41..caa7d16 100644
--- a/web/css/player.css
+++ b/web/css/player.css
@@ -15,6 +15,7 @@
 }
 #video {
 	overflow: hidden;
+	float: left;
 }
 /* tag list */
 
@@ -185,3 +186,34 @@ td {
 	text-align: left;
 	font-size: 125%;
 }
+
+
+#players {
+	float: left;
+}
+
+/* players */
+ul.game-players {
+	margin: 0;
+	padding: 0;
+}
+.game-players li {
+	list-style: none;
+	margin: 1px 0;
+	padding: 4px 8px;
+	overflow: auto;
+	font-size: 150%;
+}
+.game-players li.hidden {
+	display: none;
+}
+.game-players li .count {
+	float: left;
+	padding-right: 5px;
+}
+.game-players li .name {
+	float: left;
+}
+.game-players li .score {
+	float: right;
+}
\ No newline at end of file