FridaysForSpamBot/src/de/Thiesyy/FridaysForFuture/Core/FridaysForFuture.java
2019-10-24 23:34:44 +02:00

248 lines
6.8 KiB
Java

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) {
}
}
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(users.size()*4500);
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/users.size() + "$ bekommen!"));
addMoney(pid, Money/users.size());
sleep(1000 * 3);
}
}
users.clear();
names.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);
if(coins.containsKey(pid))
coins.remove(pid);
coins.put(pid, (Coins + Money));
refreshCoinDB(pid);
}
private void takeMoney(Long pid, int Money) {
int geld = GetMoney(pid);
if (geld <= Money) {
geld = Money+Defaultmoney;
}
if(coins.containsKey(pid))
coins.remove(pid);
coins.put(pid, geld - Money);
refreshCoinDB(pid);
}
private int GetMoney(Long pid) {
if(coins.containsKey(pid))
return coins.get(pid);
return Defaultmoney;
}
private void refreshCoinDB(Long pid) {
try {
ResultSet get = Dbmgr.query("SELECT * FROM spamforfuturebot.coins WHERE id = '" + pid + "';");
if (get.next()) {
Dbmgr.update("UPDATE spamforfuturebot.coins SET coins = '"+coins.get(pid)+"' WHERE id = '"+pid+"';");
}else {
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();
}
}
}