This page looks best with JavaScript enabled

Players, date, ID and more of a match/set/leg

How to get this information with specific functions

 ·  🕘 5 min read  ·  🤖 Matteo Miotto

Having seen in this post how to create a match, today we analyze all the functions that allow us to manage the saved object of class match2p and its subclasses (don’t worry if you don’t remember them, this scheme will help you).

SaveMatch

If, once the game is over, you decide to save the result (the match2p-class object you created) you could easily save the entire environment in an .RData file; but there is a more practical way: to use the function `saveMatch`.
The advantages of this function lie in 3 main features:

  • Save the desired game and not the entire environment, thanks to the two inputs to be provided: which game and the destination file
  • Create a text file readable even on machines that don't have R
  • Take up less space than the .RData file containing only the match object
  • But how does it occupy less space? Using a precise pattern that contains only the necessary characters; in fact, for each leg of each player two lines are created:

    1. Leg ID, player name, if he started that leg (1) or not (0), if that leg was won (1) or lost (0)
    2. Sequence of darts scores separated by a comma (,)
    Match file structure
    Figure 1: Leg structure in the saved file

    For example, using the command saveMatch(match, file = "filename.txt") the file filename.txt will be created and it will contain the list of legs as shown in the figure 1.

    LoadMatch

    Obviously, we need a function that transforms files saved with the previous function into objects of type match2p by loading them to R. This function is called loadMatch. Its peculiarity is that it can either have the name of the file as input or have no input and a dialog box will come out through which to select the file.

    As in the case of the creation of the match, for subsequent uses, it is strongly recommended to run the command by saving the result in a variable: var <- loadMatch(input)

    Useful functions

    Let’s move to the functions that allow us to obtain information from the match/set/leg. As a reminder, here is the outline of the class structure:

    Scheme of the classes
    Figure 2: Scheme of the structure of the classes and their nesting

    With this scheme in mind, let’s create various objects of different classes and analyze what information we can derive through specific functions. With the loadMatch function I load a file of a match (class match2p), then I create class objects match1p, set1p and leg1p:

    1
    2
    3
    4
    
    m2p <- loadMatch("match.txt") # full match
    m1p <- m2p@p1match            # match of player 1
    s1p <- m1p@sets[[1]]          # first set of player 1
    l1p <- s1p@legs[[1]]          # first leg of first set of player 1
    

    Show

    The show function is the implicit function that R uses when using a variable name as a command. Each class has a different way of presenting itself, albeit with some common characteristics:

    Match2p

    1
    2
    
    m2p # or
    show(m2p)
    
    Date: 2021-08-27 
    Players: matteo, simone 
     
    Match won by matteo 2-0

    As you can see, they all contain the date and player(s) of the input variable. What changes is the message below them:

  • match2p: who is the winner of the match and the score of the sets
  • match1p: match won/lost, the score of the sets and the average of the 3 darts
  • set1p: set won/lost, the score of the legs and the average of the 3 darts
  • leg1p: leg won/lost. In case of victory it is shown in how many arrows it is closed
  • getPlayers

    If you want to know which player(s) an object refers to (who played that match/set/leg), use the getPlayers(object) function. It will return a vector containing the name(s) of the player(s).

    Per esempio:

    1
    
    getPlayers(m2p)
    
    [1] "matteo" "simone"

    getDate

    To get the date on which a certain event took place, the getDate(object) function will provide it in the format YYYY-MM-DD (character).

    In this case:

    1
    
    getDate(m1p)
    
    [1] "2021-08-27"

    getWinner

    To find the winner of a match (to be used only with objects of class match2p) use the function getWinner(match).

    In this match:

    1
    
    getWinner(m2p)
    
    [1] "matteo"

    getWin

    For the other classes, you can find out if that match/set/leg has been won or lost using the function getWin(object).

    Let's see if the leg l1p was won or not:

    1
    
    getWin(l1p)
    
    [1] 1

    getID

    Here we are at today’s last function: getID. It can be used with all classes as an object, and will return the ID of that object.
    But, why is the ID so important? The ID is the unique identifier of a match/set/leg; it can be used to see which legs belong to the same game or set, to link two leg1p objects (in case they are the same leg but from different players).

    An important thing to note is the structure of the ID:

  • the match ID consists of the letter "m" followed by the match date in YYYYMMDD format, the ":" symbol and the match start time, in HHMMSS format
  • the set ID is made up of the corresponding match ID, followed by the letter "s" and the set number in the match
  • the leg ID is made up of the corresponding set ID, followed by the letter "l" and the number of the leg in the set
  • It follows that from the ID of the leg it is possible to obtain both the ID of the set and of the match of which it is taken from.

    An example:

    1
    
    getID(l1p)
    
    [1] "m20210827:161940s1l1"

    Conclusions

    In this post we have seen some useful functions to get various information from the objects of the package classes. But among this information the statistics of the match, the set or the leg are missing; these can be obtained with other functions, which I will describe in a separate post.
    Stay tuned!

    Share on
    Support the author with

    Matteo Miotto
    WRITTEN BY
    Matteo Miotto
    Genomic Data Science master student

    What's on this Page