yaz/commit

cleanup some code

authorMichiel Hildebrand
Sat Feb 5 21:26:08 2011 +0100
committerMichiel Hildebrand
Sat Feb 5 21:26:08 2011 +0100
commitb2d6e4ef9a7db77954abfabfda0da9bc94252eab
treeec861087b39210e59f5b5482d3c4dd29f70fb75e
parent6802815012e1becb24ad953c7d651a28a11e881f
Diff style: patch stat
diff --git a/applications/yaz_game.pl b/applications/yaz_game.pl
index 4575153..51b9579 100644
--- a/applications/yaz_game.pl
+++ b/applications/yaz_game.pl
@@ -1,6 +1,6 @@
 :- module(yaz_game,
 	  [ http_yaz_game/1,
-	    assert_match/5
+	    update_tag_score/4
 	  ]).
 
 :- use_module(library(http/http_dispatch)).
@@ -32,8 +32,6 @@
 	   'Interval in which tags can be matched (in miliseconds)').
 :- setting(max_player_count, integer, 8,
 	   'Maximum number of players before auto starting a game').
-:- setting(ac, boolean, false,
-	   'Use autocompletion').
 :- setting(request_interval, integer, 2000,
 	   'Interval between requests to the server (in miliseconds)').
 :- setting(video_buffer_time, integer, 1,
@@ -88,19 +86,59 @@ http_yaz_game(Request) :-
 	;   var(Game),
 	    nonvar(URL)
 	->  create_game(URL, User, Game, [video(Video),title(Title)]),
-	    html_waiting_page(Game, URL, User)
+	    active_players(Game, Players),
+	    html_waiting_page(Game, URL, User, Players)
 	;   join_game(Game, User),
 	    game_video_start(Game, URL, PlayHead),
-	    PlayerObj = {player:P, name:Name, score:Score},
-	    findall(PlayerObj, active_player(Game, P, Name, Score), Players),
-	    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),
-	    pairs_values(Tags2, Tags),
+	    active_players(Game, Players),
+	    user_tags(Game, User, Tags),
 	    html_game_page(Game, URL, User, PlayHead, Players, Tags)
 	).
 
+%%	active_player(+Game, -Players)
+%
+%	Players are all users that joined Game.
+
+
+active_players(Game, Players) :-
+	PlayerObj = {player:P, name:Name, score:Score},
+	findall(PlayerObj, active_player(Game, P, Name, Score), Players).
+
+active_player(Game, Player, Name, Score) :-
+	user_process_joined(Game, Player),
+	player_score(Game, Player, Score), % this would be retracted if the user has left the game
+	display_label(Player, Name).
+
+
+%%	user_tags(+Game, +User, -Tags)
+%
+%	Tags are all tag entries by User in Game.
+
+user_tags(Game, User, Tags) :-
+	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),
+	pairs_values(Tags2, Tags).
+
+user_tag(Game, User, Annotation, Tag, Time, Match) :-
+ 	rdf(Annotation, pprime:creator, User, Game),
+	rdf(Annotation, rdf:value, literal(Tag)),
+	rdf(Annotation, pprime:videoPlayhead, literal(Time), _),
+	(   matched_json_term(Annotation, Match)
+	->  true
+	;   Match = @false
+	).
+
+matched_json_term(Annotation, json([score=Score, multiplier=Multiplier])) :-
+  	rdf(Annotation, pprime:score, literal(Score)),
+	rdf(Annotation, pprime:multiplier, literal(Multiplier)).
+
+
+		 /*******************************
+		 *	    HTML		*
+		 *******************************/
+
 %%	html_home_page(+Channels)
 %
 %	Emit html page with a list of video channels.
@@ -174,15 +212,14 @@ html_new_game -->
 		  ])).
 
 
-%%	html_waiting_page(+Game, +URL, +User)
+%%	html_waiting_page(+Game, +URL, +User, +Players)
 %
 %	Emit html page with a list of video channels.
 
-html_waiting_page(Game, URL, User) :-
+html_waiting_page(Game, URL, User, Players) :-
 	display_label(URL, Title),
 	video_source(URL, Video),
 	http_link_to_id(serve_video_frame, [url(Video),time(5)], Frame),
-	findall({player:P,name:N}, active_player(Game, P, N, _), Players),
 	reply_html_page(yaz,
 			[ title(['YAZ tagging game - ', Title])
 			],
@@ -377,6 +414,7 @@ new_player_score(Game, Player) :-
 	),
 	assert(player_score(Game, Player, 0)).
 
+
 %%	game_video_start(+Game, -URL, -PlayHead) is det
 %
 %	True if PlayHead is the current time of the Video. The PlayHead
@@ -396,6 +434,7 @@ game_video_start(Game, URL, PlayHead) :-
 	).
 game_video_start(_Game, '', 0). % for testing
 
+
 %%	join_game(+Game, +Player) is det
 %
 %	Asserts that Player has joined game.
@@ -408,18 +447,6 @@ join_game(Game, Player) :-
 	    new_player_score(Game, Player)
 	).
 
-%%	quit_game(+Game, +Player)
-%
-%	Assert quit event.
-%
-%	@TBD fix this!!!
-
-quit_game(Game, Player) :-
-	left_game(Game, Player),
-	!.
-quit_game(Game, Player) :-
-	debug(game, 'Quit game ~w by ~w', [Game, Player]).
-
 
 %%	add_tag(+Game, +Player, +Tag, +PlayHeadTime, -AnnotationId)
 %
@@ -430,75 +457,36 @@ add_tag(Game, Player, Tag, PlayHead, AnnotationId) :-
  	create_video_annotation(URL, literal(Tag), PlayHead, Player, AnnotationId),
 	debug(game, 'Added tag ~w at time ~w by ~w (~w)', [Tag, PlayHead, Player, AnnotationId]).
 
-%%	active_player(+Game, -Player, -Name, Score)
-%
-%	Player is active in Game.
-
-active_player(Game, Player, Name, Score) :-
-	user_process_joined(Game, Player),
-	player_score(Game, Player, Score), % this would be retracted if the user has left the game
-	display_label(Player, Name).
-
-%%	left_game(+Game, -Player)
-%
-%	Player has stopped playing Game.
-
-left_game(Game, Player) :-
-	rdf(QuitEvent, rdf:type, pprime:'QuitGame', Game),
- 	rdf(QuitEvent, sem:hasActor, Player, Game).
-
 
-%%	user_tag(+Game, +User, -Annotation, -Tag, -Match)
-%
-%	Tag is entered by User during Game.
-
-user_tag(Game, User, Annotation, Tag, Time, Match) :-
- 	rdf(Annotation, pprime:creator, User, Game),
-	rdf(Annotation, rdf:value, literal(Tag)),
-	rdf(Annotation, pprime:videoPlayhead, literal(Time), _),
-	matched_json_term(Annotation, Match).
-
-matched_json_term(Annotation, json([type=Match, score=Score, order=Order])) :-
-	rdf(Annotation, pprime:match, literal(Match)),
-	!,
-	rdf(Annotation, pprime:score, literal(Score)),
-	rdf(Annotation, pprime:order, literal(Order)).
-matched_json_term(_,  @false).
+		 /*******************************
+		 *	    data services      	*
+		 *******************************/
 
-%%	user_matched_tag(+Game, +User, +PlayHead, +Interval,
-%%	-Annotation, -Match, -Score)
-%
-%	Tag is entered by User during Game.
+:- http_handler(yaz('waitingdata'), http_waiting_data, []).
+:- http_handler(yaz('gamedata'), http_game_data, []).
+:- http_handler(yaz('gameaddtag'), http_game_add_tag, []).
 
-user_matched_tag(Game, User, Playhead, Interval, Annotation, Tag, Time, Match) :-
-	Start is Playhead-Interval,
-  	rdf(Annotation, pprime:videoPlayhead, literal(between(Start,Playhead), _), Game),
-	rdf(Annotation, pprime:creator, User),
-	rdf(Annotation, rdf:value, literal(Tag)),
-	rdf(Annotation, pprime:videoPlayhead, literal(Time), _),
-	matched_json_term(Annotation, Match).
 
-%%	tag_match(+Game, +User, +Tag, +VideoTime, +Interval,
-%%	-Annotation, -Player, -Diff)
+%%	http_waiting_data(+Request)
 %
-%	Returns players that entered the same Tag within Interval of
-%	Time.
-
-match_tag(Game, User, Tag, Playhead, Interval, Annotation, Player, Match, Diff) :-
-	Start is Playhead-Interval,
-	tag_match_(Tag, Game, Annotation, Match),
-  	rdf(Annotation, pprime:videoPlayhead, literal(between(Start,Playhead), Time0)),
-	rdf(Annotation, pprime:creator, Player),
-	User \== Player,
-	literal_to_number(Time0, Time),
-	Diff is Playhead-Time.
-
-tag_match_(Tag, Game, Annotation, exact) :-
-	rdf(Annotation, rdf:value, literal(Tag), Game).
-
+%	Return JSON object with waiting players in game.
 
-:- http_handler(yaz('gamedata'), http_game_data, []).
-:- http_handler(yaz('gameaddtag'), http_game_add_tag, []).
+http_waiting_data(Request) :-
+ 	http_parameters(Request,
+			[  game(Game,
+				[uri, description('URL of current Game')])
+ 			]),
+	(   rdf(Game, opmv:wasStartedAt, _)
+	->  reply_json(json([start= @true]))
+	;   PlayerObj = json([player=P, name=N]),
+	    findall(PlayerObj, active_player(Game, P, N, _), Players),
+	    length(Players, Count),
+	    setting(max_player_count, Max),
+	    (   Count >= Max
+	    ->  reply_json(json([start= @true]))
+	    ;   reply_json(json([players=Players]))
+	    )
+	).
 
 
 %%	http_game_add_tag(+Request)
@@ -517,10 +505,70 @@ http_game_add_tag(Request) :-
 			       [description('Optionally a new tag can be added')])
 			]),
 	setting(match_interval, Interval),
- 	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])).
+ 	add_tag(Game, User, Tag, Playhead, Id),
+	match_existing_tags(Game, User, Id, Tag, Playhead, Interval, Matches),
+	length(Matches, C),
+	rdf_transaction(update_scores(Matches, 0, C, Game)),
+ 	reply_json(json([id=Id])).
+
+
+%%	match_existing_tags(+Game, +User, +Tag, +Playhead, +Interval,
+%%	-Matches)
+%
+%	Returns all matches with tag within Interval of Playhead.
+%	Matches are sorted by time.
+
+match_existing_tags(Game, User, Id, Tag, Playhead, Interval, Matches) :-
+	findall(Time-match(Entry, Player, Type),
+		matching_tag(Game, User, Tag, Playhead, Interval, Entry, Player, Type, Time),
+		Matches0),
+	(   Matches0 = []
+	->  Matches = []
+	;   keysort([Playhead-match(Id,User,exact)|Matches0], Matches1),
+	    pairs_values(Matches1, Matches)
+	).
+
+matching_tag(Game, User, Tag, Playhead, Interval, Id, Player, Match, Time) :-
+	Start is Playhead-Interval,
+	tag_match(Tag, Game, Id, Match),
+  	rdf(Id, pprime:videoPlayhead, literal(between(Start,Playhead), Time)),
+	rdf(Id, pprime:creator, Player),
+	User \== Player.
+
+tag_match(Tag, Game, Annotation, exact) :-
+	rdf(Annotation, rdf:value, literal(Tag), Game).
+
+
+%%	update_scores(+Matches, +Current, +Total, +Game)
+%
+%	Update player scores.
+
+update_scores([], _, _, _).
+update_scores([match(Id,Player,Match)|Ms], N, Count, Game) :-
+	N1 is N + 1,
+	match_points(Match, MPoints),
+	order_count_multiplier(N1, Count, Multiplier),
+	Points is MPoints*Multiplier,
+ 	update_tag_score(Id, Game, Points, Multiplier),
+	update_player_score(Game, Player, Points),
+	update_scores(Ms, N1, Count, Game).
+
+update_player_score(Game, Player, Points) :-
+	player_score(Game, Player, OldScore),
+ 	retractall(player_score(Game, Player, OldScore)),
+	NewScore is OldScore+Points,
+	assert(player_score(Game, Player, NewScore)).
+
+match_points(exact, 50).
+
+order_count_multiplier(Order, Count, X) :-
+	X is 4/(Order*2) + ((Count-2)/2).
+
+update_tag_score(Annotation, Game, Score, Multiplier) :-
+ 	rdf_retractall(Annotation, pprime:score, _),
+	rdf_retractall(Annotation, pprime:multiplier, _),
+ 	rdf_assert(Annotation, pprime:score, literal(Score), Game),
+	rdf_assert(Annotation, pprime:multiplier, literal(Multiplier), Game).
 
 
 %%	http_game_data(+Request)
@@ -547,102 +595,23 @@ http_game_data(Request) :-
 		Tags),
 	reply_json(json([user=User, players=Players, tags=Tags])).
 
-
-%%	match_existing_tags(+Game, +User, +Tag, +Playhead, +Interval,
-%%	-Matches)
-%
-%	Returns all matching tag within Interval of Playhead
-
-match_existing_tags(Game, User, Tag, Playhead, Interval, Matches) :-
-	findall(Diff-match(Annotation, Player, Match, Diff),
-		match_tag(Game, User, Tag, Playhead, Interval, Annotation, Player, Match, Diff),
-		Matches0),
-	keysort(Matches0, Matches1),
-	reverse(Matches1, Matches2),
-	pairs_values(Matches2, Matches).
-
-
-%%	update_scores(+Matches, +Annotation, +Game, +User)
+%%	user_matched_tag(+Game, +User, +PlayHead, +Interval,
+%%	-Annotation, -Match, -Score)
 %
-%	Find matching tags within interval of PlayHead and update player
-%	scores.
-
-update_scores([], _, _, _) :- !.
-update_scores(Matches, Annotation, Game, User) :-
-	length(Matches, C0),
-	Total is C0+1,
-	% score of newly added tag
-	maplist(user_match_points, Matches, Pairs0),
-	keysort(Pairs0, Pairs1),
-	reverse(Pairs1, [_-Match|_]),
- 	tag_match_points(Match, Total, 0, Total, Points),
-	rdf_transaction(assert_match(Annotation, Game, Match, Points, Total)),
-	update_player_score(Game, User, Points),
-	% scores of other matches
-	rdf_transaction(update_player_scores(Matches, 1, Total, Game)).
-
-update_player_scores([], _, _, _).
-update_player_scores([match(Annotation,Player,Match,Diff)|Ms], N, Count, Game) :-
-	N1 is N + 1,
-  	tag_match_points(Match, N, Diff, Count, Points),
-	assert_match(Annotation, Game, Match, Points, N),
-	update_player_score(Game, Player, Points),
-	update_player_scores(Ms, N1, Count, Game).
-
-update_player_score(Game, Player, Points) :-
-	player_score(Game, Player, OldScore),
-	retractall(player_score(Game, Player, OldScore)),
-	NewScore is OldScore+Points,
-	assert(player_score(Game, Player, NewScore)).
-
-user_match_points(match(_,_,Match,_), Points-Match) :-
-	(   Match = generic
-	->  match_points(specific, Points)
-	;   match_points(Match, Points)
-	).
-
-tag_match_points(Match, Order, _Diff, _Total, Points) :-
-	match_points(Match, MatchPoints),
-  	(   Order = 1
-	->  Points is MatchPoints*2
-	;   Points = MatchPoints
-	).
-
-match_points(exact, 50).
-match_points(stem, 50).
-match_points(specific, 100).
-match_points(generic, 20).
-match_points(related, 20).
-
-assert_match(Annotation, Game, Match, Score, Order) :-
-	rdf_assert(Annotation, pprime:match, literal(Match), Game),
-	rdf_assert(Annotation, pprime:score, literal(Score), Game),
-	rdf_assert(Annotation, pprime:order, literal(Order), Game).
-
+%	Tag is entered by User during Game.
 
-:- http_handler(yaz('waitingdata'), http_waiting_data, []).
+user_matched_tag(Game, User, Playhead, Interval, Id, Tag, Time, Match) :-
+	Start is Playhead-Interval,
+  	rdf(Id, pprime:videoPlayhead, literal(between(Start,Playhead), Time), Game),
+	rdf(Id, pprime:creator, User),
+ 	matched_json_term(Id, Match),
+	rdf(Id, rdf:value, literal(Tag)).
 
-%%	http_waiting_data(+Request)
-%
-%	Return JSON object with waiting players in game.
 
-http_waiting_data(Request) :-
- 	http_parameters(Request,
-			[  game(Game,
-				[uri, description('URL of current Game')])
- 			]),
-	(   rdf(Game, opmv:wasStartedAt, _)
-	->  reply_json(json([start= @true]))
-	;   PlayerObj = json([player=P, name=N]),
-	    findall(PlayerObj, active_player(Game, P, N, _), Players),
-	    length(Players, Count),
-	    setting(max_player_count, Max),
-	    (   Count >= Max
-	    ->  reply_json(json([start= @true]))
-	    ;   reply_json(json([players=Players]))
-	    )
-	).
 
+		 /*******************************
+		 *       store channnels	*
+		 *******************************/
 
 %%	assert_channel_info
 %
diff --git a/web/js/game/input.js b/web/js/game/input.js
index 841d2d3..9229c4e 100644
--- a/web/js/game/input.js
+++ b/web/js/game/input.js
@@ -96,10 +96,8 @@ YUI.add('game-input', function(Y) {
 		
 		_setStyle : function(node, match) {
 			if(match) {
-				node.addClass(match.type);
-				if(match.order===1) {
-					node.addClass("first");
-				}
+				var type = (match.multiplier>=2) ? "first" : "exact";
+				node.addClass(type);
 				if(match.score) {
 					node.one('.score').setContent(match.score);
 				}