Author Topic: Last Played Games - Most recent Games list  (Read 5232 times)

spud1

  • Full Member
  • ***
  • Posts: 40
    • View Profile
Last Played Games - Most recent Games list
« on: October 03, 2018, 04:06:13 PM »
Hi.  I'm trying to produce a "last played games" romlist for Attract Mode.

I've tried to get this script to work but unsuccessfully: http://forum.attractmode.org/index.php?topic=1843.0

What I have so far in the layout.nut is:

Code: [Select]
// Most recently played games will be listed in the "Last Played Games.txt" romlist
fe.add_transition_callback( "on_transition" );
function on_transition( ttype, var, ttime )
{
 switch ( ttype )
 {
  case Transition.ToGame:
  fe.plugin_command_bg( fe.script_dir + "runcommand-lastplayed.sh", "\"" + fe.game_info( Info.Name ) + ";" + fe.game_info( Info.Title ) + ";" + fe.game_info( Info.Emulator ) + "\"" );
  break;
 }
 return false;
 }
}}

And the executable bash script runcommand-lastplayed.sh (which is in the same folder as layout.nut) is as follows:
Code: [Select]
#!/bin/bash
# get the args
args="$1"

IFS=';' read -r -a array <<< "$args"

filename="/home/pi/.attract/romlists/Last Played Games.txt"
NUMOFLINES=$(wc -l < "$filename")

$(grep -q "${array[0]};${array[1]};${array[2]};;;;;;;;;;;;;;" "$filename")
if [ $? -eq 1 ]; then
if [ $NUMOFLINES -lt 51 ]; then
sed -i -e '$a\'"${array[0]};${array[1]};${array[2]};;;;;;;;;;;;;;" "$filename"
else
sed -i '2d' "$filename"
sed -i -e '$a\'"${array[0]};${array[1]};${array[2]};;;;;;;;;;;;;;" "$filename"
fi
fi
I've also added a "Last Played Games.txt" romlist  in the right location with an initial line at the top so the file is not empty.

However, I can't get this to work.  It adds nothing to the "Last Played Games.txt".  Could anyone help please?  Thanks.
« Last Edit: October 24, 2018, 06:30:30 PM by spud1 »

spud1

  • Full Member
  • ***
  • Posts: 40
    • View Profile
Re: Last Played Games - Most recent Games list
« Reply #1 on: October 07, 2018, 01:53:40 AM »
I've tried all sorts to get this working.  I've updated my Attract Mode to the latest version from 2.3.0.   I've checked that the bash script itself works from the command line, which it does.  The problem seems to be with the layout.nut.    It doesn't seem to pass the arguments to the bash script and fails to invoke it.

I've also tried another script (below) found in the forum (http://forum.attractmode.org/index.php?topic=2420.0) as well to see whether this other script writes to a file as it is intended to do.  It doesn't work for me either.  Could anyone who is using a Raspberry Pi and Retropie make any suggestions please?  It looks to me as though the layout.nut isn't invoking the bash scripts for some reason.  Thanks for any suggestions.

Code: [Select]
fe.add_transition_callback( "gamename_transition" );
function gamename_transition( ttype, var, ttime )
{
        if (( ttype == Transition.EndNavigation )
                || ( ttype == Transition.StartLayout ))
        {
                system( "echo '" + fe.game_info( Info.Name ) + "' > ~/.attract/current_name.txt" );
                system( "echo '" + fe.game_info( Info.Title ) + "' > ~/.attract/current_title.txt" );
                system( "echo '" + fe.game_info( Info.System ) + "' > ~/.attract/current_system.txt" );
        }
        return false;
}

spud1

  • Full Member
  • ***
  • Posts: 40
    • View Profile
Re: Last Played Games - Most recent Games list
« Reply #2 on: October 09, 2018, 02:43:03 AM »
I wound up solving the problem I had by creating a separate plugin rather than including the code in the layout.nut.

The LastPlayedGame.nut plugin looks like this:

Code: [Select]
// This plugin saves the most recently played game to the "Last_Played_Games.txt" romlist
fe.add_transition_callback( "most_recent_game" );
function most_recent_game( ttype, var, ttime )
{
 switch ( ttype )
 {
  case Transition.ToGame:
                system( "echo '" + fe.game_info( Info.Name ) + ";" + fe.game_info( Info.Title ) + ";" + fe.game_info( Info.Emulator ) + ";" + fe.game_info( Info.CloneOf ) + ";" + fe.game_info( Info.Year ) + ";" + fe.game_info( Info.Manufacturer ) + ";" + fe.game_info( Info.Category ) + ";" + fe.game_info( Info.Players ) + ";" + fe.game_info( Info.Rotation ) + ";" + fe.game_info( Info.Control ) + ";" + fe.game_info( Info.Status ) + ";" + fe.game_info( Info.DisplayCount ) + ";" + fe.game_info( Info.DisplayType ) + ";" + fe.game_info( Info.AltRomname ) + ";" + fe.game_info( Info.AltTitle ) + ";" + fe.game_info( Info.Extra ) + ";" + fe.game_info( Info.Buttons ) + "' >> ~/.attract/romlists/Last_Played_Games.txt" ); // Takes the details of the game most recently played and inserts them at the end of the Last_Played_Games.txt romlist
                system( "tac ~/.attract/romlists/Last_Played_Games.txt | awk 'NR==1 {a=$0; next} 1; END{print a}' | tac > ~/.attract/romlists/Last_Played_Games1.txt && rm ~/.attract/romlists/Last_Played_Games.txt && mv ~/.attract/romlists/Last_Played_Games1.txt ~/.attract/romlists/Last_Played_Games.txt" ); // Takes the Game's details and moves them to the start of the romlist
                system( "awk '!/Mode/' ~/.attract/romlists/Last_Played_Games.txt > ~/.attract/romlists/Last_Played_Games1.txt && rm ~/.attract/romlists/Last_Played_Games.txt && mv ~/.attract/romlists/Last_Played_Games1.txt ~/.attract/romlists/Last_Played_Games.txt" ); // Removes any reference in the romlist to a restart of Attract Mode
                system( "awk '!x[$0]++' ~/.attract/romlists/Last_Played_Games.txt > ~/.attract/romlists/Last_Played_Games1.txt && rm ~/.attract/romlists/Last_Played_Games.txt && mv ~/.attract/romlists/Last_Played_Games1.txt ~/.attract/romlists/Last_Played_Games.txt" ); // Removes duplicate entries in the romlist, leaving only the first (and most recent) entry for a particular game
                system( "head -51 ~/.attract/romlists/Last_Played_Games.txt > ~/.attract/romlists/Last_Played_Games1.txt && rm ~/.attract/romlists/Last_Played_Games.txt && mv ~/.attract/romlists/Last_Played_Games1.txt ~/.attract/romlists/Last_Played_Games.txt" ); // Limits the number of entries in the romlist to 51
        return false;
}}

You could safely remove the third "system" line - that is unique to my system.

Don't forget to enable the plugin.  I have set up a "Most Recent Games" display for the "Last_Played_Games.txt" romlist and it is working perfectly for me.
« Last Edit: October 09, 2018, 03:21:48 PM by spud1 »

spud1

  • Full Member
  • ***
  • Posts: 40
    • View Profile
Re: Last Played Games - Most recent Games list
« Reply #3 on: October 12, 2018, 03:13:48 PM »
Updated script:

Code: [Select]
// This plugin saves the most recently played game to the "Last_Played_Games.txt" romlist
fe.add_transition_callback( "most_recent_game" );
function most_recent_game( ttype, var, ttime )
{
 switch ( ttype )
 {
  case Transition.ToGame:
                system( "echo '" + fe.game_info( Info.Name ) + ";" + fe.game_info( Info.Title ) + ";" + fe.game_info( Info.Emulator ) + ";" + fe.game_info( Info.CloneOf ) + ";" + fe.game_info( Info.Year ) + ";" + fe.game_info( Info.Manufacturer ) + ";" + fe.game_info( Info.Category ) + ";" + fe.game_info( Info.Players ) + ";" + fe.game_info( Info.Rotation ) + ";" + fe.game_info( Info.Control ) + ";" + fe.game_info( Info.Status ) + ";" + fe.game_info( Info.DisplayCount ) + ";" + fe.game_info( Info.DisplayType ) + ";" + fe.game_info( Info.AltRomname ) + ";" + fe.game_info( Info.AltTitle ) + ";" + fe.game_info( Info.Extra ) + ";" + fe.game_info( Info.Buttons ) + "' >> ~/.attract/romlists/Last_Played_Games.txt" ); // Takes the details of the game most recently played and inserts them at the end of the Last_Played_Games.txt romlist
                system( "tac ~/.attract/romlists/Last_Played_Games.txt | awk 'NR==1 {a=$0; next} 1; END{print a}' | tac > ~/.attract/romlists/Last_Played_Games1.txt && rm ~/.attract/romlists/Last_Played_Games.txt && mv ~/.attract/romlists/Last_Played_Games1.txt ~/.attract/romlists/Last_Played_Games.txt" ); // Takes the most recently played game and moves it to the start of the romlist
                system( "awk '!/Mode/' ~/.attract/romlists/Last_Played_Games.txt > ~/.attract/romlists/Last_Played_Games1.txt && rm ~/.attract/romlists/Last_Played_Games.txt && mv ~/.attract/romlists/Last_Played_Games1.txt ~/.attract/romlists/Last_Played_Games.txt" ); // Removes any reference in the romlist to a restart of Attract Mode
                system( "awk '!x[$0]++' ~/.attract/romlists/Last_Played_Games.txt > ~/.attract/romlists/Last_Played_Games1.txt && rm ~/.attract/romlists/Last_Played_Games.txt && mv ~/.attract/romlists/Last_Played_Games1.txt ~/.attract/romlists/Last_Played_Games.txt" ); // Removes duplicate entries in the romlist, leaving only the first (and most recent) entry for a particular game
                system( "printf '%s\n' 1m3 3-m1- w q | ed -s ~/.attract/romlists/Last_Played_Games.txt" ); // Swaps the most recently played game at line 1 to line 3 so that the game is shown first (gives it focus) on opening the Most Recently Played Games theme
        return false;
}}

jeffy777

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Last Played Games - Most recent Games list
« Reply #4 on: October 20, 2018, 11:13:44 PM »
Is there a way to make this work on PC?
« Last Edit: October 20, 2018, 11:23:15 PM by jeffy777 »

spud1

  • Full Member
  • ***
  • Posts: 40
    • View Profile
Re: Last Played Games - Most recent Games list
« Reply #5 on: October 21, 2018, 03:43:59 PM »
Unfortunately, I don't know.  I'm using a Raspberry Pi 3B.

spud1

  • Full Member
  • ***
  • Posts: 40
    • View Profile
Re: Last Played Games - Most recent Games list
« Reply #6 on: October 22, 2018, 01:46:58 AM »
Final version of script LastPlayedGames.nut (to be added to /home/pi/.attract/plugins/):

Code: [Select]
// This plugin saves the most recently played game to the "Last_Played_Games.txt" romlist
fe.add_transition_callback( "most_recent_game" );
function most_recent_game( ttype, var, ttime )
{
 switch ( ttype )
 {
  case Transition.ToGame:
                system( "echo '" + fe.game_info( Info.Name ) + ";" + fe.game_info( Info.Title ) + ";" + fe.game_info( Info.Emulator ) + ";" + fe.game_info( Info.CloneOf ) + ";" + fe.game_info( Info.Year ) + ";" + fe.game_info( Info.Manufacturer ) + ";" + fe.game_info( Info.Category ) + ";" + fe.game_info( Info.Players ) + ";" + fe.game_info( Info.Rotation ) + ";" + fe.game_info( Info.Control ) + ";" + fe.game_info( Info.Status ) + ";" + fe.game_info( Info.DisplayCount ) + ";" + fe.game_info( Info.DisplayType ) + ";" + fe.game_info( Info.AltRomname ) + ";" + fe.game_info( Info.AltTitle ) + ";" + fe.game_info( Info.Extra ) + ";" + fe.game_info( Info.Buttons ) + "' >> ~/.attract/romlists/Last_Played_Games.txt" ); // Takes the details of the game most recently played and inserts them at the end of the Last_Played_Games.txt romlist
                system( "tac ~/.attract/romlists/Last_Played_Games.txt | awk 'NR==1 {a=$0; next} 1; END{print a}' | tac > ~/.attract/romlists/Last_Played_Games1.txt && rm ~/.attract/romlists/Last_Played_Games.txt && mv ~/.attract/romlists/Last_Played_Games1.txt ~/.attract/romlists/Last_Played_Games.txt" ); // Takes the most recently played game and moves it to the start of the romlist
                system( "awk '!x[$0]++' ~/.attract/romlists/Last_Played_Games.txt > ~/.attract/romlists/Last_Played_Games1.txt && rm ~/.attract/romlists/Last_Played_Games.txt && mv ~/.attract/romlists/Last_Played_Games1.txt ~/.attract/romlists/Last_Played_Games.txt" ); // Removes duplicate entries in the romlist, leaving only the first (and most recent) entry for a particular game
        return false;
}}

To ensure that the focus is on the most recent game that was played when the Most Recently Played Game Display is first opened, add the following code to the layout.nut of the Display/Theme:

Code: [Select]
// This plugin ensures that when the Most Recent Games Display is opened, the first game in the romlist is in focus
fe.add_transition_callback( "first_game" );
function first_game( ttype, var, ttime )
{
 switch ( ttype )
 {
  case Transition.ToNewList:
    fe.list.index = 0
        return false;
}}

Thanks to keilmillerjr for help on the above script.
« Last Edit: October 22, 2018, 01:51:39 AM by spud1 »