2016-09-25 15:33:20 +02:00
/ *
* This file is part of TelegramBots .
*
2016-09-28 00:31:34 +02:00
* TelegramBots is free software : you can redistribute it and / or modify
2016-09-25 15:33:20 +02:00
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
2016-09-28 00:31:34 +02:00
* TelegramBots is distributed in the hope that it will be useful ,
2016-09-25 15:33:20 +02:00
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2016-09-28 00:31:34 +02:00
* along with TelegramBots . If not , see < http : //www.gnu.org/licenses/>.
2016-09-25 15:33:20 +02:00
* /
2018-07-08 01:41:21 +02:00
package org.telegram.telegrambots.meta.api.methods.games ;
2016-09-25 15:33:20 +02:00
2016-11-17 02:41:12 +01:00
import com.fasterxml.jackson.annotation.JsonProperty ;
import com.fasterxml.jackson.core.type.TypeReference ;
2016-09-25 15:33:20 +02:00
2020-11-01 23:46:36 +01:00
import lombok.AllArgsConstructor ;
import lombok.Builder ;
import lombok.EqualsAndHashCode ;
import lombok.Getter ;
import lombok.NoArgsConstructor ;
import lombok.NonNull ;
import lombok.RequiredArgsConstructor ;
import lombok.Setter ;
import lombok.ToString ;
2018-07-08 01:41:21 +02:00
import org.telegram.telegrambots.meta.api.methods.BotApiMethod ;
import org.telegram.telegrambots.meta.api.objects.games.GameHighScore ;
2019-06-08 21:33:28 +02:00
import org.telegram.telegrambots.meta.api.objects.ApiResponse ;
2018-07-08 01:41:21 +02:00
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException ;
import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException ;
2016-09-25 15:33:20 +02:00
import java.io.IOException ;
import java.util.ArrayList ;
/ * *
* @author Ruben Bermudez
* @version 2 . 4
2019-06-08 21:33:28 +02:00
* Use this method to get data for high score tables .
2016-09-28 00:29:44 +02:00
* Will return the score of the specified user and several of his neighbors in a game .
* On success , returns an Array of GameHighScore objects .
*
2020-11-01 23:46:36 +01:00
* @apiNote This method will currently return scores for the target user ,
2016-09-29 00:44:03 +02:00
* plus two of his closest neighbors on each side . Will also return the top three users
* if the user and his neighbors are not among them .
2016-09-28 00:29:44 +02:00
* Please note that this behavior is subject to change .
2016-09-29 00:44:03 +02:00
*
2016-09-25 15:33:20 +02:00
* /
2020-11-01 23:46:36 +01:00
@EqualsAndHashCode ( callSuper = false )
@Getter
@Setter
@ToString
@NoArgsConstructor
@RequiredArgsConstructor
@AllArgsConstructor
@Builder
2016-09-25 15:33:20 +02:00
public class GetGameHighScores extends BotApiMethod < ArrayList < GameHighScore > > {
2016-09-28 00:29:44 +02:00
public static final String PATH = " getGameHighScores " ;
2016-09-25 15:33:20 +02:00
private static final String CHATID_FIELD = " chat_id " ;
private static final String MESSAGEID_FIELD = " message_id " ;
private static final String INLINE_MESSAGE_ID_FIELD = " inline_message_id " ;
private static final String USER_ID_FIELD = " user_id " ;
2016-11-17 02:41:12 +01:00
@JsonProperty ( CHATID_FIELD )
2016-09-25 15:33:20 +02:00
private String chatId ; ///< Optional Required if inline_message_id is not specified. Unique identifier for the target chat (or username of the target channel in the format @channelusername)
2016-11-17 02:41:12 +01:00
@JsonProperty ( MESSAGEID_FIELD )
2016-09-25 15:33:20 +02:00
private Integer messageId ; ///< Optional Required if inline_message_id is not specified. Unique identifier of the sent message
2016-11-17 02:41:12 +01:00
@JsonProperty ( INLINE_MESSAGE_ID_FIELD )
2016-09-25 15:33:20 +02:00
private String inlineMessageId ; ///< Optional Required if chat_id and message_id are not specified. Identifier of the inline message
2016-11-17 02:41:12 +01:00
@JsonProperty ( USER_ID_FIELD )
2020-11-01 23:46:36 +01:00
@NonNull
2016-09-28 00:29:44 +02:00
private Integer userId ; ///<Target user id
2016-09-25 15:33:20 +02:00
@Override
2016-11-17 02:41:12 +01:00
public String getMethod ( ) {
2016-09-25 15:33:20 +02:00
return PATH ;
}
@Override
2016-11-17 02:41:12 +01:00
public ArrayList < GameHighScore > deserializeResponse ( String answer ) throws TelegramApiRequestException {
try {
ApiResponse < ArrayList < GameHighScore > > result = OBJECT_MAPPER . readValue ( answer ,
new TypeReference < ApiResponse < ArrayList < GameHighScore > > > ( ) { } ) ;
if ( result . getOk ( ) ) {
return result . getResult ( ) ;
} else {
throw new TelegramApiRequestException ( " Error getting game high scores " , result ) ;
2016-09-25 15:33:20 +02:00
}
2016-11-17 02:41:12 +01:00
} catch ( IOException e ) {
throw new TelegramApiRequestException ( " Unable to deserialize response " , e ) ;
2016-09-25 15:33:20 +02:00
}
}
@Override
public void validate ( ) throws TelegramApiValidationException {
if ( userId = = null ) {
throw new TelegramApiValidationException ( " UserId parameter can't be empty " , this ) ;
}
if ( inlineMessageId = = null ) {
if ( chatId = = null ) {
throw new TelegramApiValidationException ( " ChatId parameter can't be empty if inlineMessageId is not present " , this ) ;
}
if ( messageId = = null ) {
throw new TelegramApiValidationException ( " MessageId parameter can't be empty if inlineMessageId is not present " , this ) ;
}
} else {
if ( chatId ! = null ) {
throw new TelegramApiValidationException ( " ChatId parameter must be empty if inlineMessageId is provided " , this ) ;
}
if ( messageId ! = null ) {
throw new TelegramApiValidationException ( " MessageId parameter must be empty if inlineMessageId is provided " , this ) ;
}
}
}
}