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:
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:
- Leg ID, player name, if he started that leg (1) or not (0), if that leg was won (1) or lost (0)
- Sequence of darts scores separated by a comma (,)
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.
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:
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:
|
|
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
|
|
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:
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] "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] "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] "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] 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:
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] "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!