import com.fasterxml.jackson.databind.ObjectMapper;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import java.io.*;
import java.util.ArrayList;
public class Bot extends TelegramLongPollingBot {
ArrayList<User> userList = new ArrayList<>();
int st =0;
@Override
public String getBotUsername() {
return "BullsAndCowsGameBot";
}
@Override
public String getBotToken() {
return "1657936521:FklGeE3LZdgsFEy0FSx63lvMdBBe7Ze0PLr";
}
@Override
public void onUpdateReceived(Update update) {
long senderID = update.getMessage().getChatId();
User senderUser = new User(0,"NULL");
boolean isNewUser = true;
for (int i = 0; i < userList.size(); i++)
if (senderID == userList.get(i).userId) {
isNewUser = false;
senderUser = userList.get(i);
}
if (isNewUser == true) {
userList.add(new User(update.getMessage().getChatId(), update.getMessage().getChat().getUserName()));
save();
SendMessage(senderID, "You are a new player!");
SendMessage(senderID, "/find");
senderUser.state = 0;
// MyGame game = new MyGame();
// game.start(senderID, senderID);
}
if (update.getMessage().getText().toLowerCase().equals("/cancel") && senderUser.state == 1){
senderUser.isFinding = false;
senderUser.state = 0;
SendMessage(senderID, "You canceled the search.");
return;
}
if (senderUser.state == 1)
SendMessage(senderID, "While searching, you can only once use the /cancel command to cancel the search.");
if (update.getMessage().getText().toLowerCase().equals("/find") && isNewUser == false && senderUser.state == 0) {
senderUser.isFinding = true;
senderUser.state = 1;
SendMessage(senderID,"Вы начали поиск...");
int counter = 0;
for (int i = 0; i < userList.size(); i++)
if (userList.get(i).isFinding == true)
counter++;
if (counter == 2) {
User player1 = new User(), player2 = new User();
boolean player1Finded = false;
for (int i = 0; i < userList.size(); i++)
if (userList.get(i).isFinding == true )
if (player1Finded == false) {
player1 = userList.get(i);
player1Finded = true;
} else {
player2 = userList.get(i);
break;
}
SendMessage(player1.userId,"Opponent finded: " + player2.userName);
SendMessage(player2.userId,"Opponent finded: " + player1.userName);
player1.isFinding = false;
player1.state = 2;
player2.isFinding = false;
player2.state = 2;
System.out.println(player1.isFinding + "" + player1.userId);
System.out.println(player2.isFinding + "" + player2.userId);
MyGame game = new MyGame();
game.start(player1.userId, player2.userId);
}
}
if (isNewUser == false && senderUser.state == 0)
OldUserUpdate(senderUser,senderID,update);
}
public void OldUserUpdate(User user, long id, Update update){
if(user.userId == update.getMessage().getChatId())
SendMessage(id, "Unknown command! Use /help to see the list of all commands.");
}
public void save(){
ObjectMapper mapper = new ObjectMapper();
try (FileWriter fileWriter = new FileWriter("users.json")) {
mapper.writerWithDefaultPrettyPrinter().writeValue(fileWriter,userList);
//mapper.writeValue(fileWriter, userList);
//String ab = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(userList);
//System.out.println(ab);
} catch (IOException e) {
e.printStackTrace();
}
}
public void SendMessage(long id, String mess ) {
SendMessage sendMess = new SendMessage();
sendMess.setChatId(id+"");
sendMess.setText(mess);
try {
execute((sendMess));
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
}