Answer the question
In order to leave comments, you need to log in
How to make a lobby for a gaming telegram bot?
As part of the study of TelegramAPI, I want to make a simple bot for playing Bulls and Cows.
Players are connected to the lobby and transferred to the class with the game. How to save lobbies and interact with them further?
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
public class Main {
public static void main(String[] args) {
try {
TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class);
botsApi.registerBot(new Bot());
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
}
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();
}
}
}
import com.fasterxml.jackson.annotation.JsonIgnore;
public class User {
public long userId;
public String userName;
public int state;
@JsonIgnore
public boolean isFinding;
public User(long id, String name){
userId = id;
username = name;
isFinding = false;
}
}
public class MyGame extends Bot {
public void MyGame() {
}
// public void start() {}
}
Answer the question
In order to leave comments, you need to log in
Can you explain what a "lobby" is? It's the developers, not the players.
Most people are too lazy to go to Google to look for it, you need it.
Well look. I will not write the code, I will write the algorithm.
The lobby is basically a waiting room.
Obviously you need to create a queue (simplest and most logical FIFO). I do not know how many players you plan to use there, I will assume that 5 (change it to your own if necessary). Further, 5 players are typed in the search, create a lobby object, throw a list of players into the constructor there. Well, then, depending on what you want in the lobby, then you do it. You have a lobby object, with a list of players. Any action is available.
To prevent lobbies from overlapping, you need a unique ID value for each lobby.
It is also desirable to implement multithreading, but here if you know how (this is a separate large and not the easiest topic).
PS I also recommend trying to connect the database to your code right now. All data must be stored in a database. It doesn't matter if it's a training project or (even more so) a "combat" one. Otherwise, later, when your code is many times larger, there will be big problems with refactoring (more precisely, gemorno).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question