Initial commit
This commit is contained in:
3
Java/ChessGame/src/main/java/META-INF/MANIFEST.MF
Normal file
3
Java/ChessGame/src/main/java/META-INF/MANIFEST.MF
Normal file
@@ -0,0 +1,3 @@
|
||||
Manifest-Version: 1.0
|
||||
Main-Class: de.craftix.server.MainServer
|
||||
|
||||
48
Java/ChessGame/src/main/java/de/craftix/game/Chess.java
Normal file
48
Java/ChessGame/src/main/java/de/craftix/game/Chess.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package de.craftix.game;
|
||||
|
||||
import de.craftix.engine.EngineSettings;
|
||||
import de.craftix.engine.GameEngine;
|
||||
import de.craftix.engine.render.Sprite;
|
||||
import de.craftix.game.scenes.Scenes;
|
||||
import de.craftix.server.MainServer;
|
||||
import de.craftix.server.ServerConnection;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.io.File;
|
||||
|
||||
public class Chess extends GameEngine {
|
||||
public static final String root = System.getenv("APPDATA") + File.separator + "ChessGame" + File.separator;
|
||||
static { new File(root).mkdirs(); }
|
||||
|
||||
public static final Color menuBackground = Color.GRAY;
|
||||
public static final Font menuFont = new Font("Arial", Font.PLAIN, 30);
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
//TODO: REMOVE THE FIRST TWO LINES
|
||||
new Thread(() -> MainServer.main(args)).start();
|
||||
Thread.sleep(1000);
|
||||
|
||||
ServerConnection.connect();
|
||||
SoundManager.setMusicVolume(0);
|
||||
EngineSettings.setResizable(false);
|
||||
EngineSettings.printSystemLog(false);
|
||||
EngineSettings.setWindowIcon(Sprite.load("icon.png"));
|
||||
EngineSettings.setAntialiasing(true);
|
||||
EngineSettings.setFullscreenKey(KeyEvent.VK_F11);
|
||||
EngineSettings.setCloseKey(KeyEvent.VK_ESCAPE);
|
||||
setup(1280, 720, "Schach Online", new Chess(), 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialise() {
|
||||
if (!UserManagement.isLoggedIn()) setScene(Scenes.loginScene);
|
||||
else setScene(Scenes.menuScene);
|
||||
SoundManager.initialise();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
ServerConnection.disconnect();
|
||||
}
|
||||
}
|
||||
33
Java/ChessGame/src/main/java/de/craftix/game/Figure.java
Normal file
33
Java/ChessGame/src/main/java/de/craftix/game/Figure.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package de.craftix.game;
|
||||
|
||||
import de.craftix.engine.render.Sprite;
|
||||
import de.craftix.engine.render.SpriteMap;
|
||||
|
||||
public enum Figure {
|
||||
KING(0, 0, 6, "König"),
|
||||
QUEEN(1, 1, 7, "Königin"),
|
||||
BISHOP(2, 2, 8, "Läufer"),
|
||||
KNIGHT(3, 3, 9, "Pferd"),
|
||||
ROOK(4, 4, 10, "Turm"),
|
||||
PAWN(5, 5, 11, "Bauer");
|
||||
|
||||
private static final SpriteMap img = new SpriteMap(6, Sprite.load("figures.png"), 95, 95);
|
||||
|
||||
private final int id;
|
||||
private final int texIDWhite;
|
||||
private final int texIDBlack;
|
||||
private final String name;
|
||||
|
||||
Figure(int id, int texIDWhite, int texIDBlack, String name) {
|
||||
this.id = id;
|
||||
this.texIDWhite = texIDWhite;
|
||||
this.texIDBlack = texIDBlack;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getID() { return id; }
|
||||
public Sprite getSprite(int color) { return color == 0 ? img.getSprite(texIDWhite) : img.getSprite(texIDBlack); }
|
||||
public String getName() { return name; }
|
||||
|
||||
public static Figure getByID(int id) { return values()[id]; }
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package de.craftix.game;
|
||||
|
||||
import de.craftix.engine.GameEngine;
|
||||
import de.craftix.engine.var.Mathf;
|
||||
import de.craftix.engine.var.Sound;
|
||||
|
||||
public class SoundManager {
|
||||
private static boolean initialised = false;
|
||||
private static int masterVolume = 100;
|
||||
private static int musicVolume = 100;
|
||||
|
||||
private static Sound backgroundMusic;
|
||||
|
||||
public static void initialise() {
|
||||
backgroundMusic = new Sound(GameEngine.loadFile("backgroundmusic.wav"));
|
||||
|
||||
initialised = true;
|
||||
|
||||
backgroundMusic.getClip().loop(Integer.MAX_VALUE);
|
||||
|
||||
updateSounds();
|
||||
backgroundMusic.play();
|
||||
}
|
||||
|
||||
private static void updateSounds() {
|
||||
if (!initialised) return;
|
||||
float musicVolume = Mathf.map(SoundManager.musicVolume * masterVolume, 0, 10000, 0, 100);
|
||||
|
||||
backgroundMusic.setVolume(musicVolume);
|
||||
}
|
||||
|
||||
public static void setMasterVolume(int volume) { masterVolume = volume; updateSounds(); }
|
||||
public static void setMusicVolume(int volume) { musicVolume = volume; updateSounds(); }
|
||||
|
||||
public static int getMasterVolume() { return masterVolume; }
|
||||
public static int getMusicVolume() { return musicVolume; }
|
||||
|
||||
}
|
||||
28
Java/ChessGame/src/main/java/de/craftix/game/Stats.java
Normal file
28
Java/ChessGame/src/main/java/de/craftix/game/Stats.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package de.craftix.game;
|
||||
|
||||
import de.craftix.server.Packet;
|
||||
import de.craftix.server.ServerConnection;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Stats implements Serializable {
|
||||
|
||||
public int gamesPlayed;
|
||||
public int wins;
|
||||
public int looses;
|
||||
public int figuresStolen;
|
||||
public int pawnReachEnd;
|
||||
|
||||
public Stats() {
|
||||
this.gamesPlayed = 0;
|
||||
this.wins = 0;
|
||||
this.looses = 0;
|
||||
this.figuresStolen = 0;
|
||||
this.pawnReachEnd = 0;
|
||||
}
|
||||
|
||||
public void save() {
|
||||
ServerConnection.sendPacket(new Packet(4, UserManagement.user.getUUID(), this));
|
||||
}
|
||||
|
||||
}
|
||||
25
Java/ChessGame/src/main/java/de/craftix/game/User.java
Normal file
25
Java/ChessGame/src/main/java/de/craftix/game/User.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package de.craftix.game;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.UUID;
|
||||
|
||||
public class User implements Serializable {
|
||||
|
||||
private final UUID uuid;
|
||||
private final UUID serverID;
|
||||
private final String name;
|
||||
private final Stats stats;
|
||||
|
||||
public User(UUID uuid, String name, UUID serverID, Stats stats) {
|
||||
this.uuid = uuid;
|
||||
this.name = name;
|
||||
this.serverID = serverID;
|
||||
this.stats = stats;
|
||||
}
|
||||
|
||||
public UUID getUUID() { return uuid; }
|
||||
public UUID getServerID() { return serverID; }
|
||||
public String getName() { return name; }
|
||||
public Stats getStats() { return stats; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package de.craftix.game;
|
||||
|
||||
import de.craftix.game.configs.UserConfig;
|
||||
import de.craftix.server.Packet;
|
||||
import de.craftix.server.ServerConnection;
|
||||
|
||||
public class UserManagement {
|
||||
public static User user;
|
||||
|
||||
private static void setUserData(Packet packet, String username, String password) {
|
||||
if ((boolean) packet.getData()[0]) {
|
||||
UserConfig.password.setValue(password);
|
||||
UserConfig.username.setValue(username);
|
||||
UserConfig.manager.saveConfig();
|
||||
user = (User) packet.getData()[1];
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isLoggedIn() {
|
||||
String username = UserConfig.username.getValue();
|
||||
String password = UserConfig.password.getValue();
|
||||
|
||||
Packet answer = ServerConnection.sendPacket(new Packet(1, username, password));
|
||||
setUserData(answer, username, password);
|
||||
|
||||
return (boolean) answer.getData()[0];
|
||||
}
|
||||
|
||||
public static boolean login(String username, String password) {
|
||||
password = UserConfig.encode(password);
|
||||
|
||||
Packet answer = ServerConnection.sendPacket(new Packet(1, username, password));
|
||||
setUserData(answer, username, password);
|
||||
|
||||
return (boolean) answer.getData()[0];
|
||||
}
|
||||
|
||||
public static boolean register(String username, String password) {
|
||||
password = UserConfig.encode(password);
|
||||
|
||||
Packet answer = ServerConnection.sendPacket(new Packet(2, username, password));
|
||||
setUserData(answer, username, password);
|
||||
|
||||
return (boolean) answer.getData()[0];
|
||||
}
|
||||
|
||||
public static boolean changePassword(String password) {
|
||||
password = UserConfig.encode(password);
|
||||
|
||||
Packet answer = ServerConnection.sendPacket(new Packet(3, user, password));
|
||||
setUserData(answer, user.getName(), password);
|
||||
|
||||
return (boolean) answer.getData()[0];
|
||||
}
|
||||
|
||||
public static void logout() {
|
||||
UserConfig.password.setValue("");
|
||||
UserConfig.username.setValue("");
|
||||
UserConfig.manager.saveConfig();
|
||||
user = null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package de.craftix.game.configs;
|
||||
|
||||
import de.craftix.engine.var.configuration.utils.Configurable;
|
||||
import de.craftix.engine.var.configuration.utils.ConfigurationManager;
|
||||
import de.craftix.game.Chess;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
public class UserConfig {
|
||||
|
||||
public static Configurable<String> username = new Configurable<>("user.username", "");
|
||||
public static Configurable<String> password = new Configurable<>("user.password", "");
|
||||
|
||||
public static ConfigurationManager manager = new ConfigurationManager(new File(Chess.root + "userdata.yml"), username, password);
|
||||
|
||||
public static String decode(String password) { return new String(Base64.getDecoder().decode(password)); }
|
||||
public static String encode(String password) { return Base64.getEncoder().encodeToString(password.getBytes(StandardCharsets.UTF_8)); }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package de.craftix.game.scenes;
|
||||
|
||||
import de.craftix.engine.InputManager;
|
||||
import de.craftix.engine.ui.UIAlignment;
|
||||
import de.craftix.engine.ui.UIManager;
|
||||
import de.craftix.engine.ui.elements.UIButton;
|
||||
import de.craftix.engine.ui.elements.UIText;
|
||||
import de.craftix.engine.ui.elements.UITextBox;
|
||||
import de.craftix.engine.var.Scene;
|
||||
import de.craftix.engine.var.Transform;
|
||||
import de.craftix.engine.var.Vector2;
|
||||
import de.craftix.engine.var.Dimension;
|
||||
import de.craftix.game.Chess;
|
||||
import de.craftix.game.UserManagement;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class LoginScene extends Scene implements ActionListener {
|
||||
|
||||
private UITextBox username;
|
||||
private UITextBox password;
|
||||
private UIButton login;
|
||||
private UIButton register;
|
||||
private UIText error;
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
InputManager.setCursor(Cursor.DEFAULT_CURSOR);
|
||||
setBackgroundColor(Chess.menuBackground);
|
||||
|
||||
UIText title = new UIText("Login / Register", new Font("Arial", Font.BOLD, 50), Color.WHITE, new Transform(new Vector2(0, 150)), UIAlignment.CENTER);
|
||||
username = new UITextBox("Username", new Transform(new Vector2(0, 50), new Dimension(400, 60)), UIAlignment.CENTER, UITextBox.Type.TEXT);
|
||||
password = new UITextBox("Password", new Transform(new Vector2(0, -50), new Dimension(400, 60)), UIAlignment.CENTER, UITextBox.Type.PASSWORD);
|
||||
login = new UIButton("Login", new Transform(new Vector2(-110, -150), new Dimension(185, 40)), UIAlignment.CENTER);
|
||||
register = new UIButton("Register", new Transform(new Vector2(110, -150), new Dimension(185, 40)), UIAlignment.CENTER);
|
||||
error = new UIText("Error Message", new Font("Arial", Font.PLAIN, 20), Color.RED, new Transform(new Vector2(0, 100)), UIAlignment.CENTER);
|
||||
|
||||
username.setFont(Chess.menuFont);
|
||||
password.setFont(Chess.menuFont);
|
||||
login.setFont(Chess.menuFont);
|
||||
register.setFont(Chess.menuFont);
|
||||
|
||||
username.setMaxlength(30);
|
||||
password.setMaxlength(30);
|
||||
|
||||
login.setClickListener(this);
|
||||
register.setClickListener(this);
|
||||
|
||||
error.setVisible(false);
|
||||
|
||||
UIManager manager = getUIManager();
|
||||
manager.addElement(title);
|
||||
manager.addElement(username);
|
||||
manager.addElement(password);
|
||||
manager.addElement(login);
|
||||
manager.addElement(register);
|
||||
manager.addElement(error);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String username = this.username.getText();
|
||||
String password = this.password.getText();
|
||||
boolean success = false;
|
||||
if (e.getSource() == login) success = UserManagement.login(username, password);
|
||||
if (e.getSource() == register) success = UserManagement.register(username, password);
|
||||
|
||||
if (success) Chess.setScene(Scenes.menuScene);
|
||||
else {
|
||||
if (e.getSource() == login)
|
||||
error.setText("Username oder Passwort stimmt nicht überein!");
|
||||
else if (e.getSource() == register)
|
||||
error.setText("Dieser Username existiert schon!");
|
||||
error.setVisible(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package de.craftix.game.scenes;
|
||||
|
||||
import de.craftix.engine.InputManager;
|
||||
import de.craftix.engine.ui.UIAlignment;
|
||||
import de.craftix.engine.ui.UIManager;
|
||||
import de.craftix.engine.ui.elements.UIButton;
|
||||
import de.craftix.engine.ui.elements.UIText;
|
||||
import de.craftix.engine.var.Dimension;
|
||||
import de.craftix.engine.var.Scene;
|
||||
import de.craftix.engine.var.Transform;
|
||||
import de.craftix.engine.var.Vector2;
|
||||
import de.craftix.game.Chess;
|
||||
import de.craftix.game.UserManagement;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class MenuScene extends Scene implements ActionListener {
|
||||
|
||||
private UIButton play;
|
||||
private UIButton settings;
|
||||
private UIButton profile;
|
||||
private UIButton close;
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
InputManager.setCursor(Cursor.DEFAULT_CURSOR);
|
||||
setBackgroundColor(Chess.menuBackground);
|
||||
|
||||
UIText title = new UIText("Schach Online", new Font("Arial", Font.BOLD, 80), Color.WHITE, new Transform(new Vector2(0, 200)), UIAlignment.CENTER);
|
||||
UIText user = new UIText("Angemeldet als: " + UserManagement.user.getName(), new Font("Arial", Font.PLAIN, 15), Color.WHITE, new Transform(new Vector2(0, 120)), UIAlignment.CENTER);
|
||||
|
||||
play = new UIButton("Spielen", new Transform(new Vector2(0, 70), new Dimension(300, 60)), UIAlignment.CENTER);
|
||||
play.setFont(Chess.menuFont);
|
||||
play.setClickListener(this);
|
||||
|
||||
settings = new UIButton("Einstellungen", new Transform(new Vector2(0, 0), new Dimension(300, 60)), UIAlignment.CENTER);
|
||||
settings.setFont(Chess.menuFont);
|
||||
settings.setClickListener(this);
|
||||
|
||||
profile = new UIButton("Profil", new Transform(new Vector2(-77, -70), new Dimension(148, 60)), UIAlignment.CENTER);
|
||||
profile.setFont(Chess.menuFont);
|
||||
profile.setClickListener(this);
|
||||
|
||||
close = new UIButton("Beenden", new Transform(new Vector2(77, -70), new Dimension(148, 60)), UIAlignment.CENTER);
|
||||
close.setFont(Chess.menuFont);
|
||||
close.setClickListener(this);
|
||||
|
||||
UIManager manager = getUIManager();
|
||||
manager.removeElements();
|
||||
manager.addElement(title);
|
||||
manager.addElement(user);
|
||||
manager.addElement(play);
|
||||
manager.addElement(settings);
|
||||
manager.addElement(profile);
|
||||
manager.addElement(close);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (e.getSource() == close) Chess.shutdown();
|
||||
if (e.getSource() == play) Chess.setScene(Scenes.playScene);
|
||||
if (e.getSource() == settings) Chess.setScene(Scenes.settingsScene);
|
||||
if (e.getSource() == profile) Chess.setScene(Scenes.profileScene);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package de.craftix.game.scenes;
|
||||
|
||||
import de.craftix.engine.var.Scene;
|
||||
import de.craftix.game.Chess;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class PlayScene extends Scene implements ActionListener {
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
setBackgroundColor(Chess.menuBackground);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package de.craftix.game.scenes;
|
||||
|
||||
import de.craftix.engine.InputManager;
|
||||
import de.craftix.engine.render.Screen;
|
||||
import de.craftix.engine.render.Sprite;
|
||||
import de.craftix.engine.ui.UIAlignment;
|
||||
import de.craftix.engine.ui.UIManager;
|
||||
import de.craftix.engine.ui.elements.UIButton;
|
||||
import de.craftix.engine.ui.elements.UIText;
|
||||
import de.craftix.engine.ui.elements.UITextBox;
|
||||
import de.craftix.engine.var.Dimension;
|
||||
import de.craftix.engine.var.Scene;
|
||||
import de.craftix.engine.var.Transform;
|
||||
import de.craftix.engine.var.Vector2;
|
||||
import de.craftix.game.Chess;
|
||||
import de.craftix.game.UserManagement;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class ProfileScene extends Scene implements ActionListener {
|
||||
|
||||
private UIButton changePassword;
|
||||
private UIButton logout;
|
||||
private UIButton back;
|
||||
|
||||
private UITextBox password;
|
||||
private UIButton confirmChange;
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
InputManager.setCursor(Cursor.DEFAULT_CURSOR);
|
||||
setBackgroundColor(Chess.menuBackground);
|
||||
|
||||
//Stats
|
||||
UIText statsTitle = new UIText("Statistiken:", new Font("Arial", Font.BOLD, 30), Color.WHITE, new Transform(new Vector2(0, 200)), UIAlignment.CENTER);
|
||||
|
||||
changePassword = new UIButton("Passwort ändern", new Transform(new Vector2(-180, 120), new Dimension(300, 60)), UIAlignment.BOTTOM_RIGHT);
|
||||
changePassword.setFont(Chess.menuFont);
|
||||
changePassword.setClickListener(this);
|
||||
|
||||
logout = new UIButton("Ausloggen", new Transform(new Vector2(-180, 50), new Dimension(300, 60)), UIAlignment.BOTTOM_RIGHT);
|
||||
logout.setFont(Chess.menuFont);
|
||||
logout.setClickListener(this);
|
||||
|
||||
back = new UIButton("Zurück", new Transform(new Vector2(90, 50), new Dimension(150, 60)), UIAlignment.BOTTOM_LEFT);
|
||||
back.setFont(Chess.menuFont);
|
||||
back.setClickListener(this);
|
||||
|
||||
password = new UITextBox("Neues Passwort", new Transform(new Vector2(-200, 50), new Dimension(380, 60)), UIAlignment.BOTTOM, UITextBox.Type.PASSWORD);
|
||||
password.setFont(Chess.menuFont);
|
||||
password.setMaxlength(30);
|
||||
password.setVisible(false);
|
||||
|
||||
confirmChange = new UIButton("Bestätigen", new Transform(new Vector2(80, 50), new Dimension(150, 60)), UIAlignment.BOTTOM);
|
||||
confirmChange.setFont(Chess.menuFont);
|
||||
confirmChange.setVisible(false);
|
||||
confirmChange.setClickListener(this);
|
||||
|
||||
UIManager manager = getUIManager();
|
||||
manager.removeElements();
|
||||
manager.addElement(statsTitle);
|
||||
manager.addElement(changePassword);
|
||||
manager.addElement(logout);
|
||||
manager.addElement(back);
|
||||
manager.addElement(password);
|
||||
manager.addElement(confirmChange);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (e.getSource() == back) Chess.setScene(Scenes.menuScene);
|
||||
if (e.getSource() == logout) {
|
||||
UserManagement.logout();
|
||||
Chess.setScene(Scenes.loginScene);
|
||||
}
|
||||
if (e.getSource() == changePassword) {
|
||||
boolean visible = !password.isVisible();
|
||||
password.setVisible(visible);
|
||||
confirmChange.setVisible(visible);
|
||||
}
|
||||
if (e.getSource() == confirmChange) {
|
||||
String password = this.password.getText();
|
||||
boolean result = UserManagement.changePassword(password);
|
||||
JOptionPane.showMessageDialog(Screen.getDisplay(), result ? "Passwort geändert" : "Passwort konnte nicht geändert werden", "ChessGame", result ? JOptionPane.INFORMATION_MESSAGE : JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package de.craftix.game.scenes;
|
||||
|
||||
public final class Scenes {
|
||||
private Scenes() {}
|
||||
|
||||
public static final LoginScene loginScene = new LoginScene();
|
||||
public static final MenuScene menuScene = new MenuScene();
|
||||
public static final PlayScene playScene = new PlayScene();
|
||||
public static final ProfileScene profileScene = new ProfileScene();
|
||||
public static final SettingsScene settingsScene = new SettingsScene();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package de.craftix.game.scenes;
|
||||
|
||||
import de.craftix.engine.InputManager;
|
||||
import de.craftix.engine.render.Screen;
|
||||
import de.craftix.engine.ui.UIAlignment;
|
||||
import de.craftix.engine.ui.UIManager;
|
||||
import de.craftix.engine.ui.elements.UIButton;
|
||||
import de.craftix.engine.ui.elements.UISlider;
|
||||
import de.craftix.engine.ui.elements.UIText;
|
||||
import de.craftix.engine.var.Dimension;
|
||||
import de.craftix.engine.var.Scene;
|
||||
import de.craftix.engine.var.Transform;
|
||||
import de.craftix.engine.var.Vector2;
|
||||
import de.craftix.game.Chess;
|
||||
import de.craftix.game.SoundManager;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class SettingsScene extends Scene implements ActionListener {
|
||||
UISlider masterVolume;
|
||||
UISlider musicVolume;
|
||||
UIButton back;
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
InputManager.setCursor(Cursor.DEFAULT_CURSOR);
|
||||
setBackgroundColor(Chess.menuBackground);
|
||||
Font font = new Font("Arial", Font.BOLD, 20);
|
||||
|
||||
UIText sounds = new UIText("Sounds", new Font("Arial", Font.BOLD, 30), Color.WHITE, new Transform(new Vector2(0, 250)), UIAlignment.CENTER);
|
||||
UIText mvText = new UIText("Gesamtlautstärke", font, Color.WHITE, new Transform(new Vector2(-130, 200)), UIAlignment.CENTER);
|
||||
UIText muvText = new UIText("Musik", font, Color.WHITE, new Transform(new Vector2(-130, 150)), UIAlignment.CENTER);
|
||||
|
||||
masterVolume = new UISlider(true, new Dimension(20), new Transform(new Vector2(70, 200), new Dimension(200, 10)), UIAlignment.CENTER);
|
||||
musicVolume = new UISlider(true, new Dimension(20), new Transform(new Vector2(70, 150), new Dimension(200, 10)), UIAlignment.CENTER);
|
||||
|
||||
masterVolume.setValue(SoundManager.getMasterVolume());
|
||||
musicVolume.setValue(SoundManager.getMusicVolume());
|
||||
|
||||
masterVolume.setValueChangedListener(this);
|
||||
musicVolume.setValueChangedListener(this);
|
||||
|
||||
back = new UIButton("Zurück", new Transform(new Vector2(90, 50), new Dimension(150, 60)), UIAlignment.BOTTOM_LEFT);
|
||||
back.setFont(Chess.menuFont);
|
||||
back.setClickListener(this);
|
||||
|
||||
UIManager manager = getUIManager();
|
||||
manager.removeElements();
|
||||
manager.addElement(sounds);
|
||||
manager.addElement(mvText);
|
||||
manager.addElement(muvText);
|
||||
manager.addElement(masterVolume);
|
||||
manager.addElement(musicVolume);
|
||||
manager.addElement(back);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (e.getSource() == back) Chess.setScene(Scenes.menuScene);
|
||||
else if (e.getSource() == masterVolume)
|
||||
SoundManager.setMasterVolume((int) masterVolume.getValue());
|
||||
else if (e.getSource() == musicVolume)
|
||||
SoundManager.setMusicVolume((int) musicVolume.getValue());
|
||||
}
|
||||
}
|
||||
223
Java/ChessGame/src/main/java/de/craftix/server/Client.java
Normal file
223
Java/ChessGame/src/main/java/de/craftix/server/Client.java
Normal file
@@ -0,0 +1,223 @@
|
||||
package de.craftix.server;
|
||||
|
||||
import de.craftix.engine.Logger;
|
||||
import de.craftix.game.Stats;
|
||||
import de.craftix.game.User;
|
||||
import de.craftix.game.configs.UserConfig;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Client implements Serializable {
|
||||
public transient ObjectInputStream in;
|
||||
public transient ObjectOutputStream out;
|
||||
public transient Socket socket;
|
||||
public transient Thread thread;
|
||||
public InetAddress address;
|
||||
public Logger logger;
|
||||
public UUID uuid;
|
||||
|
||||
public Client(Socket socket) throws IOException {
|
||||
this.socket = socket;
|
||||
this.address = socket.getInetAddress();
|
||||
this.out = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
|
||||
out.flush();
|
||||
this.in = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
|
||||
logger = new Logger(address.getHostAddress());
|
||||
MainServer.clients.add(this);
|
||||
logger.info("Connected.");
|
||||
|
||||
while (uuid == null) {
|
||||
UUID ram = UUID.randomUUID();
|
||||
if (ram.equals(MainServer.serverUUID)) continue;
|
||||
boolean equals = false;
|
||||
for (Client c : MainServer.clients) {
|
||||
if (c.uuid != null && c.uuid.equals(ram)) {
|
||||
equals = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!equals)
|
||||
uuid = ram;
|
||||
}
|
||||
|
||||
thread = new Thread(() -> {
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
try {
|
||||
packetHandler(awaitPacket());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
}
|
||||
|
||||
public Packet awaitPacket() {
|
||||
try {
|
||||
return (Packet) in.readObject();
|
||||
} catch (Exception e) {
|
||||
disconnect();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void sendPacket(Packet packet) {
|
||||
try {
|
||||
out.writeObject(packet);
|
||||
out.flush();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void disconnect() {
|
||||
thread.interrupt();
|
||||
try {
|
||||
in.close();
|
||||
out.close();
|
||||
socket.close();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
MainServer.clients.remove(this);
|
||||
logger.info("Disconnected.");
|
||||
}
|
||||
|
||||
private void packetHandler(Packet p) throws Exception {
|
||||
if (p == null) return;
|
||||
//HandlePackets
|
||||
if (p.getType() == 0) { //Ping
|
||||
sendPacket(new Packet(0));
|
||||
}
|
||||
if (p.getType() == 1) { //Login
|
||||
String username = (String) p.getData()[0];
|
||||
String password = (String) p.getData()[1];
|
||||
if (!checkPassword(password)) {
|
||||
sendPacket(new Packet(3, false));
|
||||
return;
|
||||
}
|
||||
ResultSet rs = MainServer.sql.getData("SELECT * FROM Users WHERE Username = \"" + username + "\"");
|
||||
if (rs.next()) {
|
||||
String realPass = rs.getString("Password");
|
||||
if (realPass.equals(password)) {
|
||||
UUID uuid = UUID.fromString(rs.getString("UUID"));
|
||||
sendPacket(new Packet(1, true, new User(uuid, username, this.uuid, getStats(uuid))));
|
||||
}else {
|
||||
sendPacket(new Packet(1, false));
|
||||
}
|
||||
}else {
|
||||
sendPacket(new Packet(1, false));
|
||||
}
|
||||
}
|
||||
if (p.getType() == 2) { //Register
|
||||
String username = (String) p.getData()[0];
|
||||
String password = (String) p.getData()[1];
|
||||
if (!checkPassword(password)) {
|
||||
sendPacket(new Packet(3, false));
|
||||
return;
|
||||
}
|
||||
UUID uuid = UUID.randomUUID();
|
||||
Object[] users = getUsers();
|
||||
List<UUID> uuids = (List<UUID>) users[0];
|
||||
List<String> names = (List<String>) users[1];
|
||||
while (uuids.contains(uuid))
|
||||
uuid = UUID.randomUUID();
|
||||
|
||||
if (names.contains(username)) {
|
||||
sendPacket(new Packet(2, false));
|
||||
return;
|
||||
}
|
||||
|
||||
MainServer.sql.insert("INSERT INTO Users (UUID, Username, Password) VALUES (\"" + uuid + "\", \"" + username + "\", \"" + password + "\")");
|
||||
saveStats(uuid, new Stats());
|
||||
|
||||
sendPacket(new Packet(2, true, new User(uuid, username, this.uuid, new Stats())));
|
||||
}
|
||||
if (p.getType() == 3) { //Change Password
|
||||
User user = (User) p.getData()[0];
|
||||
String password = (String) p.getData()[1];
|
||||
if (!checkPassword(password)) {
|
||||
sendPacket(new Packet(3, false));
|
||||
return;
|
||||
}
|
||||
MainServer.sql.insert("DELETE FROM Users WHERE UUID = \"" + user.getUUID() + "\"");
|
||||
MainServer.sql.insert("INSERT INTO Users (UUID, Username, Password) VALUES (\"" + user.getUUID() + "\", \"" + user.getName() + "\", \"" + password + "\")");
|
||||
sendPacket(new Packet(3, true, user));
|
||||
}
|
||||
if (p.getType() == 4) { //Save Stats
|
||||
UUID user = (UUID) p.getData()[0];
|
||||
Stats stats = (Stats) p.getData()[1];
|
||||
saveStats(user, stats);
|
||||
sendPacket(new Packet(0));
|
||||
}
|
||||
}
|
||||
|
||||
private boolean checkPassword(String password) {
|
||||
password = UserConfig.decode(password);
|
||||
return password.length() > 8 && !password.contains(" ");
|
||||
}
|
||||
|
||||
private Object[] getUsers() {
|
||||
ArrayList<UUID> uuids = new ArrayList<>();
|
||||
ArrayList<String> users = new ArrayList<>();
|
||||
ArrayList<String> pass = new ArrayList<>();
|
||||
try {
|
||||
ResultSet rs = MainServer.sql.getData("SELECT * FROM Users");
|
||||
while (rs.next()) {
|
||||
uuids.add(UUID.fromString(rs.getString("UUID")));
|
||||
users.add(rs.getString("Username"));
|
||||
pass.add(rs.getString("Password"));
|
||||
}
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
return new Object[] { uuids, users, pass };
|
||||
}
|
||||
|
||||
private Stats getStats(UUID user) {
|
||||
try {
|
||||
Stats stats = new Stats();
|
||||
ResultSet rs = MainServer.sql.getData("SELECT * FROM Stats WHERE UUID = \"" + user + "\"");
|
||||
while (rs.next()) {
|
||||
String name = rs.getString("StatName");
|
||||
int value = rs.getInt("StatValue");
|
||||
|
||||
switch (name) {
|
||||
case "GamesPlayed":
|
||||
stats.gamesPlayed = value;
|
||||
break;
|
||||
case "Wins":
|
||||
stats.wins = value;
|
||||
break;
|
||||
case "Looses":
|
||||
stats.looses = value;
|
||||
break;
|
||||
case "FiguresStolen":
|
||||
stats.figuresStolen = value;
|
||||
break;
|
||||
case "PawnReachEnd":
|
||||
stats.pawnReachEnd = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return stats;
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
return new Stats();
|
||||
}
|
||||
|
||||
private void saveStats(UUID user, Stats stats) {
|
||||
String syntax = "INSERT INTO Stats (UUID, StatName, StatValue) VALUES (\"" + uuid + "\", \"";
|
||||
|
||||
MainServer.sql.insert("DELETE FROM Stats WHERE UUID = \"" + user + "\";");
|
||||
MainServer.sql.insert(syntax + "GamesPlayed\", " + stats.gamesPlayed + ");");
|
||||
MainServer.sql.insert(syntax + "Wins\", " + stats.wins + ");");
|
||||
MainServer.sql.insert(syntax + "Looses\", " + stats.looses + ");");
|
||||
MainServer.sql.insert(syntax + "FiguresStolen\", " + stats.figuresStolen + ");");
|
||||
MainServer.sql.insert(syntax + "PawnReachEnd\", " + stats.pawnReachEnd + ");");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package de.craftix.server;
|
||||
|
||||
import de.craftix.engine.Logger;
|
||||
import de.craftix.engine.var.MySQL;
|
||||
|
||||
import java.net.ServerSocket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.UUID;
|
||||
|
||||
public class MainServer {
|
||||
public static final int PORT = 23010;
|
||||
public static final String IP = "localhost"; //"213.136.89.237";
|
||||
|
||||
public static UUID serverUUID = UUID.randomUUID();
|
||||
public static ServerSocket socket;
|
||||
public static ArrayList<Client> clients = new ArrayList<>();
|
||||
public static Logger logger;
|
||||
|
||||
public static MySQL sql;
|
||||
|
||||
public static void main(String[] args) {
|
||||
sql = new MySQL("213.136.89.237", 3306, "ChessGame", "ChessGame", "/3s.Qkt]IZQrcwxg");
|
||||
sql.connect();
|
||||
createTables();
|
||||
try {
|
||||
logger = new Logger("Server");
|
||||
socket = new ServerSocket(PORT);
|
||||
logger.info("Server started");
|
||||
logger.info("Server ready for connections");
|
||||
|
||||
while (!Thread.currentThread().isInterrupted())
|
||||
new Client(socket.accept());
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
}
|
||||
|
||||
private static void createTables() {
|
||||
sql.insert("CREATE TABLE IF NOT EXISTS Users (UUID VARCHAR(100), Username VARCHAR(100), Password VARCHAR(100))");
|
||||
sql.insert("CREATE TABLE IF NOT EXISTS Stats (UUID VARCHAR(100), StatName VARCHAR(20), StatValue INT(100))");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
18
Java/ChessGame/src/main/java/de/craftix/server/Packet.java
Normal file
18
Java/ChessGame/src/main/java/de/craftix/server/Packet.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package de.craftix.server;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Packet implements Serializable {
|
||||
|
||||
private final byte type;
|
||||
private final Object[] data;
|
||||
|
||||
public Packet(int type, Object... data) {
|
||||
this.type = (byte) type;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public int getType() { return type; }
|
||||
public Object[] getData() { return data; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package de.craftix.server;
|
||||
|
||||
import de.craftix.engine.Logger;
|
||||
import de.craftix.game.Chess;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
public class ServerConnection {
|
||||
public static Socket socket;
|
||||
public static ObjectInputStream in;
|
||||
public static ObjectOutputStream out;
|
||||
public static InetSocketAddress address;
|
||||
public static Logger logger;
|
||||
|
||||
public static void connect() {
|
||||
try {
|
||||
address = new InetSocketAddress(MainServer.IP, MainServer.PORT);
|
||||
socket = new Socket();
|
||||
logger = new Logger("ServerConnection");
|
||||
logger.info("Connecting to Server...");
|
||||
socket.connect(address, 10000);
|
||||
logger.info("connected");
|
||||
out = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
|
||||
out.flush();
|
||||
in = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
|
||||
}catch (Exception e) {
|
||||
Chess.throwError(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Packet sendPacket(Packet packet) {
|
||||
try {
|
||||
out.writeObject(packet);
|
||||
out.flush();
|
||||
return (Packet) in.readObject();
|
||||
}catch (Exception e) { Chess.throwError(e); }
|
||||
return new Packet(0, null);
|
||||
}
|
||||
|
||||
public static void disconnect() {
|
||||
try {
|
||||
in.close();
|
||||
out.close();
|
||||
socket.close();
|
||||
}catch (Exception ignored) {}
|
||||
logger.info("Disconnected.");
|
||||
}
|
||||
|
||||
public static int getPing() {
|
||||
long startTime = System.currentTimeMillis();
|
||||
Packet ping = sendPacket(new Packet(0));
|
||||
long endTime = System.currentTimeMillis();
|
||||
return (int) (endTime - startTime);
|
||||
}
|
||||
}
|
||||
|
||||
BIN
Java/ChessGame/src/main/resources/backgroundmusic.wav
Normal file
BIN
Java/ChessGame/src/main/resources/backgroundmusic.wav
Normal file
Binary file not shown.
BIN
Java/ChessGame/src/main/resources/figures.png
Normal file
BIN
Java/ChessGame/src/main/resources/figures.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 39 KiB |
BIN
Java/ChessGame/src/main/resources/icon.ico
Normal file
BIN
Java/ChessGame/src/main/resources/icon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 168 KiB |
BIN
Java/ChessGame/src/main/resources/icon.png
Normal file
BIN
Java/ChessGame/src/main/resources/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 126 KiB |
Reference in New Issue
Block a user