First code

This commit is contained in:
thiesyy 2019-10-24 22:18:58 +02:00
parent 6940fa89fd
commit 951d10c127
No known key found for this signature in database
GPG Key ID: 3560E52BC0B3CA40
4 changed files with 344 additions and 280 deletions

View File

@ -1,280 +0,0 @@
package de.Thiesyy.FridayForFutureBot.Core;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import de.mrbesen.telegram.TelegramAPI;
import de.mrbesen.telegram.event.events.UserSendMessageEvent;
import de.mrbesen.telegram.objects.TMessage;
import de.mrbesen.telegram.objects.TUser;
public class FridayForFutureBot {
private long mainchatid = -1;
private boolean heistrunning = false;
private int heistid = 0;
public static FridayForFutureBot instance;
private HashMap<Long, Integer> coins = new HashMap<Long, Integer>();
private HashMap<Long, String> codename = new HashMap<Long, String>();
private int MAXHEISTSIZE = 5;
private int Minsize = 1;
private static Long HeistOwner = 0L;
private boolean Heistprerun = false;
private boolean HeistInAction = false;
private ArrayList<String> codenames = new ArrayList<String>();
private File configfile = new File("Config.cfg");
public TelegramAPI tgapi;
private String apikey = "0";
public static void main(String[] args) {
instance = new FridayForFutureBot();
}
public FridayForFutureBot() {
initcodenames();
readApiKey();
tgapi = new TelegramAPI(apikey, "FriForFutBot");
tgapi.setLongpolling(true);
tgapi.setHelpText("Im ready.");
tgapi.getEventManager().registerEvent(UserSendMessageEvent.class, this::handleMessages);
tgapi.setUpdateInterval(60);
tgapi.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
}
private void initcodenames() {
codenames.add("Shakira");
codenames.add("Albina");
codenames.add("Camilla");
codenames.add("Avitus");
codenames.add("Lysander");
codenames.add("Isidore");
codenames.add("Bruno");
codenames.add("Jarl");
codenames.add("Bozo");
codenames.add("Boss");
}
public int getCoins(long id) {
if (!coins.containsKey(id))
setCoins(id, 1000);
int Coins = coins.get(id);
if (Coins < 5) {
coins.remove(id);
setCoins(id, 1000);
tgapi.sendAsync(mainchatid, "i Refilled your Coins.");
}
return Coins;
}
public void setCoins(long id, int amount) {
if (coins.containsKey(id))
coins.remove(id);
coins.put(id, amount);
}
public void handleMessages(Object obj) {
UserSendMessageEvent e = (UserSendMessageEvent) obj;
TUser user = e.getUser();
TMessage msgo = e.getMessage();
String msg = msgo.getText().toLowerCase();
long cid = msgo.getChatID();
if (msg.contains("klimaschutz")) {
tgapi.sendAsync(cid, "Klimaschutz ist sehr wichtig Genossinnen und Genossen!");
} else if (msg.startsWith(".roulette")) {
int a = new Random().nextInt(6);
if (a == 0)
tgapi.sendAsync(cid, "Der Revolver hat geschossen. Du bist tot.");
else
tgapi.sendAsync(cid, "*click*, Du hast überlebt.");
} else if (msg.startsWith(".help")) {
tgapi.sendAsync(cid, ".help - help\n.roulette - play roulette\nKlimaschutz\n.chatid\n.coins\n.heist");
} else if (msg.startsWith(".chatid")) {
tgapi.sendAsync(cid, user.getID() + " ist deine ChatID.");
} else if (msg.startsWith(".coins")) {
mainchatid = cid;
System.out.println(user.getName());
tgapi.sendAsync(cid, "Du hast: " + getCoins(user.getID()) + " Coins.");
} else if (msg.startsWith(".heist")) {
mainchatid = cid;
if (!heistrunning) {
if (!Heistprerun && !HeistInAction) {
HeistOwner = user.getID();
joinHeist(HeistOwner);
heistid++;
tgapi.sendAsync(cid,
"Du hast einen Überfall gestartet. Andere können mit '.heist' dem Überfall beitreten. Sende noch einmal '.heist' um den Überfall zu starten! Dein Deckname: "
+ getCodename(HeistOwner));
heistrunning = true;
Heistprerun = true;
new Thread(new Runnable() {
@Override
public void run() {
int gid = heistid;
Long owner = HeistOwner;
try {
Thread.sleep(1000*60*5);
} catch (InterruptedException e) {
}
if(owner.equals(HeistOwner) && gid == heistid) {
tgapi.sendAsync(cid, "Es sind nicht genug leute dem Überfall beigetreten.");
HeistOwner = 0L;
HeistInAction = false;
Heistprerun = false;
heistrunning = false;
codename.clear();
}
}
}).start();
}
} else {
if (Heistprerun) {
if(user.getID() == HeistOwner) {
if(codename.size() > Minsize) {
Heistprerun = false;
HeistInAction = true;
tgapi.sendAsync(cid, "Der Überfall startet nun.");
runHeist();
}
else {
tgapi.sendAsync(cid, "Es sind zuwenig Leute dabei.");
}
}else {
if(!isinHeist(user.getID())) {
if(codename.size() > MAXHEISTSIZE) {
tgapi.sendAsync(cid, "Der Überfall ist bereits voll.");
}else {
joinHeist(user.getID());
tgapi.sendAsync(cid,
"Du hast dich dem Überfall angeschlossen. Dein Deckname: " + getCodename(user.getID()));
}
}else {
tgapi.sendAsync(cid, "du bist schon dabei!");
}
}
} else
tgapi.sendAsync(cid, "Der Überfall läuft bereits!");
}
}
System.out.println(msg);
}
private boolean isinHeist(Long id) {
return codename.containsKey(id);
}
private void joinHeist(Long id) {
String cnme = unusedCodename();
System.out.println(cnme);
codename.put(id, cnme);
}
private void runHeist() {
new Thread(new Runnable() {
@Override
public void run() {
boolean gudboi = false;
Long problemmaker = 0L;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
String names = "";
Set<Entry<Long, String>> es = codename.entrySet();
for (Entry<Long, String> etry : es) {names+=etry.getValue()+",";}
tgapi.sendAsync(mainchatid, names+ " haben die Bank betreten!");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
tgapi.sendAsync(mainchatid, "Sie zücken ihre Waffen und bedrohen die Angestellten.");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
if(new Random().nextBoolean()) {
tgapi.sendAsync(mainchatid, "Oh nein! Ein Wachmann ist in der Bank und er hat eine Waffe!");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
problemmaker = getRandomHeister();
gudboi = true;
tgapi.sendAsync(mainchatid, getCodename(problemmaker) + " wurde angeschossen und ist auf der Stelle tot. Er verliert 500 Coins!");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
tgapi.sendAsync(mainchatid, "Der Rest bekommt das volle Geld des überfalls. der Teil des gestorbenen wurde von der Polizei genommen.");
}else {
tgapi.sendAsync(mainchatid, "Der Überfall läuft super! Sie holen sich ihr Geld, und hauen ab!");
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
tgapi.sendAsync(mainchatid, "Der Überfall ist beendet.");
for (Entry<Long, String> etry : es) {setCoins(etry.getKey(), getCoins(etry.getKey())+200);}
if(gudboi)
setCoins(problemmaker, getCoins(problemmaker)-700);
HeistOwner = 0L;
HeistInAction = false;
Heistprerun = false;
heistrunning = false;
codename.clear();
}
}).start();;
}
private Long getRandomHeister() {
ArrayList<Long> list = new ArrayList<Long>();
Set<Entry<Long, String>> es = codename.entrySet();
for (Entry<Long, String> etry : es) {list.add(etry.getKey());}
return list.get(new Random().nextInt(list.size()));
}
private String unusedCodename() {
for (String cdnme : codenames) {
Set<Entry<Long, String>> es = codename.entrySet();
boolean ok = true;
for (Entry<Long, String> etry : es) {
if (cdnme.equalsIgnoreCase(etry.getValue()))
ok = false;
}
if (ok)
return cdnme;
}
return "Error";
}
private String getCodename(Long id) {
return codename.get(id);
}
public void readApiKey() {
if (!configfile.exists())
try {
configfile.createNewFile();
} catch (IOException e) {
}
try {
Scanner s = new Scanner(configfile);
if (s.hasNextLine())
apikey = s.nextLine();
} catch (FileNotFoundException e) {
}
}
}

View File

@ -0,0 +1,250 @@
package de.Thiesyy.FridaysForFuture.Core;
import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.request.SendMessage;
import de.Thiesyy.FridaysForFuture.Utils.DB;
public class FridaysForFuture {
public DB Dbmgr;
private String dbpasswd;
// HEIST
private HashMap<Long, String> names = new HashMap<Long, String>();
private ArrayList<Long> users = new ArrayList<Long>();
private ArrayList<Long> shot = new ArrayList<Long>();
private enum HState {
NotRunning, Preparation, Running
};
HState heiststate = HState.NotRunning;
public void joinHeist(Long id, Long plid, String name) {
switch (heiststate) {
case NotRunning:
startHeist(id, plid, name);
break;
case Preparation:
HeistJoin(id, plid, name);
break;
case Running:
HeistError(id, name);
break;
}
}
public void LoadDB() {
try {
ResultSet get = Dbmgr.query("SELECT * FROM spamforfuturebot.coins WHERE 1;");
get.beforeFirst();
while(get.next()) {
Long pid = Long.parseLong(get.getString("id"));
int coinsp = Integer.parseInt(get.getString("coins"));
System.out.println("CoinDB: " + pid +"="+coinsp);
coins.put(pid, coinsp);
}
}catch(Exception e) {
//e.printStackTrace();
}
}
public void printcoins(Long chat, Long id) {
tbot.execute(new SendMessage(chat, "Du hast " + GetMoney(id) + "$"));
}
private void startHeist(Long id, Long plid, String name) {
tbot.execute(new SendMessage(id,
name + " hat einen Überfall gestartet! Tretet bei mit /heist ! Ihr habt noch 120 Sekunden."));
heiststate = HState.Preparation;
users.add(plid);
names.put(plid, name);
startHeistThread(id);
}
private void HeistJoin(Long id, Long plid, String name) {
if (!users.contains(plid)) {
tbot.execute(new SendMessage(id, name + " ist dem Überfall beigetreten, trete auch du bei mit /heist !"));
users.add(plid);
names.put(plid, name);
} else {
tbot.execute(new SendMessage(id, name + ", du bist schon im überfall <3"));
}
}
public void printdgag(Long id) {
tbot.execute(new SendMessage(id, "Die DigitalAG kann unter digitalag@thiesyy.de kontaktiert werden."));
}
private void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
}
}
private void startHeistThread(Long id) {
new Thread(new Runnable() {
@Override
public void run() {
int Money = new Random().nextInt(6000);
sleep(1000 * 60);
tbot.execute(new SendMessage(id, "Ihr habt noch 60 Sekunden um den Überfall beizutreten."));
sleep(1000 * 60);
tbot.execute(new SendMessage(id, "Die Zeit ist abgelaufen."));
sleep(1000 * 3);
heiststate = HState.Running;
tbot.execute(new SendMessage(id, "Der Überfall wurde geschlossen. Es geht los!"));
sleep(1000 * 5);
for (Long pid : users) {
takeMoney(pid, 3000 / users.size());
}
tbot.execute(
new SendMessage(id, "Jedem spieler wurden " + 3000 / users.size() + " Dollar eingezogen."));
sleep(1000 * 5);
String Players = "";
for (Long id : users) {
Players += names.get(id) + ", ";
}
Players = Players.substring(0, Players.length() - 2);
if (users.size() == 1)
tbot.execute(new SendMessage(id, Players + " stürmt die Bank!"));
else
tbot.execute(new SendMessage(id, Players + " stürmen die Bank!"));
sleep(1000 * 6);
if (new Random().nextInt(3) != 1) {
tbot.execute(new SendMessage(id, "Wie meistens ist ein Wachmann in der Bank!"));
sleep(1000 * 3);
int AmountShot = new Random().nextInt(users.size()+1);
for (int i = 0; i != AmountShot; i++) {
boolean run = false;
Long shotplayer = users.get(new Random().nextInt(users.size()));
while (shot.contains(shotplayer))
shotplayer = users.get(new Random().nextInt(users.size()));
tbot.execute(new SendMessage(id, names.get(shotplayer) + " wurde angeschossen! oh nein!"));
shot.add(shotplayer);
sleep(1000 * 5);
}
sleep(1000 * 5);
if (AmountShot == 0)
tbot.execute(new SendMessage(id,
"Der Wachmann ist aber Dumm und hat nicht gemerkt, dass die Bank überfallen wurde."));
else
tbot.execute(new SendMessage(id, "Alle angeschossenen bekommen kein Geld!"));
} else {
tbot.execute(new SendMessage(id, "Alles läuft super, und alle bekommen die Volle Beute!"));
}
for (Long pid : users) {
if (!shot.contains(pid)) {
String name = names.get(pid);
tbot.execute(new SendMessage(id, name + " hat " + Money + "$ bekommen!"));
addMoney(pid, Money);
sleep(1000 * 3);
}
}
users.clear();
names.clear();
coins.clear();
heiststate = HState.NotRunning;
shot.clear();
}
}).start();
}
private void HeistError(Long id, String name) {
tbot.execute(new SendMessage(id, name + ", du kannst nicht beitreten, da der Überfall bereits läuft."));
}
// HEIST
// Money
private static int Defaultmoney = 10000;
private HashMap<Long, Integer> coins = new HashMap<Long, Integer>();
private void addMoney(Long pid, int Money) {
int Coins = GetMoney(pid);
System.out.println(Coins+"add");
if(coins.containsKey(pid))
coins.remove(pid);
coins.put(pid, (Coins + Money));
System.out.println((Coins + Money) + " add2");
refreshCoinDB(pid);
}
private void takeMoney(Long pid, int Money) {
int Coins = GetMoney(pid);
System.out.println(Coins+"take");
if (Coins <= Money) {
Coins = Money+1000;
System.out.println(Coins+"adadd");
}
if(coins.containsKey(pid))
coins.remove(pid);
coins.put(pid, Coins - Money);
System.out.println(Coins-Money);
refreshCoinDB(pid);
}
private int GetMoney(Long pid) {
if(coins.containsKey(pid))
return coins.get(pid);
return Defaultmoney;
}
private void refreshCoinDB(Long pid) {
try {
//System.out.println(pid+ " " + coins.get(pid));
ResultSet get = Dbmgr.query("SELECT * FROM spamforfuturebot.coins WHERE id = '" + pid + "';");
if (get.next())
Dbmgr.update("DELETE FROM spamforfuturebot.coins WHERE id = '"+pid+"'");
Dbmgr.update("INSERT INTO spamforfuturebot.coins(id,coins) VALUES('"+pid+"','"+coins.get(pid)+"');");
} catch (SQLException e) {
e.printStackTrace();
}
}
// Money
public static FridaysForFuture fff;
public TelegramBot tbot;
private String Apikey;
private File apikeyfile;
public FridaysForFuture() throws Exception {
apikeyfile = new File("Config.cfg");
ReadApiKey();
tbot = new TelegramBot(Apikey);
tbot.setUpdatesListener(new MessageHandler());
fff = this;
Dbmgr = new DB(dbpasswd);
LoadDB();
}
public void ReadApiKey() throws Exception {
if (!apikeyfile.exists())
apikeyfile.createNewFile();
Scanner s = new Scanner(apikeyfile);
if (s.hasNextLine())
Apikey = s.nextLine();
if (s.hasNextLine())
dbpasswd = s.nextLine();
}
public static void main(String[] args) {
try {
new FridaysForFuture();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,31 @@
package de.Thiesyy.FridaysForFuture.Core;
import java.util.List;
import com.pengrad.telegrambot.UpdatesListener;
import com.pengrad.telegrambot.model.Update;
public class MessageHandler implements UpdatesListener {
@Override
public int process(List<Update> updates) {
for(Update update : updates) {
try {
System.out.println(update.message().text().toLowerCase());
if(update.message().text().toLowerCase().startsWith("/heist")) {
FridaysForFuture.fff.joinHeist(update.message().chat().id(),update.message().from().id()+0L, update.message().from().firstName());
}
else if(update.message().text().toLowerCase().startsWith("/coins")) {
FridaysForFuture.fff.printcoins(update.message().chat().id(),update.message().from().id()+0L);
}
else if(update.message().text().toLowerCase().startsWith("/digitalag")) {
FridaysForFuture.fff.printdgag(update.message().chat().id());
}
}catch(Exception e) {
}
}
return UpdatesListener.CONFIRMED_UPDATES_ALL;
}
}

View File

@ -0,0 +1,63 @@
package de.Thiesyy.FridaysForFuture.Utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.cj.jdbc.exceptions.MysqlDataTruncation;
public class DB{
private Connection con;
private String server = "mrbesen.de";
private int port = 3306;
private String user = "thiesyy";
private String pw;
private String db = "spamforfuturebot";
public DB(String pw) {
this.pw = pw;
connect(false);
}
public boolean isconnected() throws SQLException {
return !con.isClosed() && con != null;
}
private void connect(boolean selectdb) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");//Treiber laden try this driver: com.mysql.cj.jdbc.Driver
//verbinden
con = DriverManager.getConnection("jdbc:mysql://" + server + ":" + port + "/" + (selectdb ? db : "") + "?serverTimezone=UTC&useSSL=false" ,user,pw);
}catch (ClassNotFoundException | SQLException e) {
}
}
public ResultSet query(String q) {
try {
if(con.isClosed()) {
connect(true);
}
return con.prepareStatement(q).executeQuery();
} catch (SQLException e) {
}
return null;//ERROR!
}
public void update(String q) {
try {
if(con.isClosed()) {
connect(true);
}
con.prepareStatement(q).executeUpdate();
} catch (MysqlDataTruncation ignore) {
} catch (SQLException e) {
}
}
}