Skip to content

Commit

Permalink
Add server check for database on login
Browse files Browse the repository at this point in the history
  • Loading branch information
David52920 committed Mar 12, 2021
1 parent cdbb221 commit 71635ca
Show file tree
Hide file tree
Showing 12 changed files with 264 additions and 81 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ project(":server") {
compile group: 'io.netty', name: 'netty-all', version: '4.1.40.Final'
compile group: 'io.netty', name: 'netty-all', version: '4.1.40.Final-sources'
compile group: 'commons-cli', name: 'commons-cli', version: '1.4'
compile 'com.jcraft:jsch:0.1.54'
}
}

Expand Down
16 changes: 13 additions & 3 deletions client-core/src/com/benberi/cadesim/GameContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public void setTurnDuration(int value) {
public Map<Integer, String> serverInfo = new HashMap<Integer, String>();
public String currentServerRoom = "";
public String accountName = "";
public String accountPassword = "";
public String hostURL;
public int myVesselType;
public Team myTeam;
Expand Down Expand Up @@ -347,10 +348,11 @@ public void sendPacket(OutgoingPacket p) {
* Sends a login packet to the server with the given display name
* @param display The display name
*/
public void sendLoginPacket(String accountName, String display, int ship, int team) {
public void sendLoginPacket(String accountName, String accountPassword, String display, int ship, int team) {
LoginPacket packet = new LoginPacket();
packet.setVersion(Constants.PROTOCOL_VERSION);
packet.setAccountName(accountName);
packet.setAccountPass(accountPassword);
packet.setName(display);
packet.setShip(ship);
packet.setTeam(team);
Expand Down Expand Up @@ -400,7 +402,7 @@ public void connectdb(String username, String password) {
* @throws UnknownHostException
*/
GameContext context = this;
public void connect(final String accountName, final String displayName, String ip, int ship, int team) throws UnknownHostException {
public void connect(final String accountName, final String accountPass,final String displayName, String ip, int ship, int team) throws UnknownHostException {
haveServerResponse = false; // reset for next connect
if(!RandomUtils.validIP(ip) && RandomUtils.validUrl(ip)) {
try {
Expand All @@ -414,7 +416,7 @@ public void connect(final String accountName, final String displayName, String i
@Override
public void onSuccess(Channel channel) {
serverChannel = channel; // initialize the server channel
sendLoginPacket(accountName, displayName, ship, team); // send login packet
sendLoginPacket(accountName, accountPass, displayName, ship, team); // send login packet
myVessel = displayName;
myVesselType = ship;
myTeam = Team.forId(team);
Expand Down Expand Up @@ -851,6 +853,14 @@ public String getAccountName() {
return accountName;
}

public void setAccountPass(String name) {
accountPassword = name;
}

public String getAccountPass() {
return accountPassword;
}

public void setHostURL(String url) {
hostURL = url;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class LoginPacket extends OutgoingPacket {

private String name;
private String accountName;
private String accountPassword;
private int ship;
private int version;
private int team;
Expand All @@ -27,6 +28,9 @@ public void setAccountName(String name) {
this.accountName = name;
}

public void setAccountPass(String pass) {
this.accountPassword = pass;
}

@Override
public void encode() {
Expand All @@ -37,8 +41,10 @@ public void encode() {
writeByteString(name);
if(accountName == null) {
writeByteString("");
writeByteString("");
}else {
writeByteString(accountName);
writeByteString(accountPassword);
}
setLength(getBuffer().readableBytes());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void render(float delta) {
if (connectTask != null && connectTask.isDone() && context.getManager().update() && !isConnected) {
isConnected = true;
try {
context.connect(context.getAccountName(), context.getVesselName(), context.getHostURL(), context.getVesselType(), context.getTeam());
context.connect(context.getAccountName(), context.getAccountPass(), context.getVesselName(), context.getHostURL(), context.getVesselType(), context.getTeam());
} catch (UnknownHostException e) {
e.printStackTrace();
}
Expand Down
108 changes: 56 additions & 52 deletions client-core/src/com/benberi/cadesim/game/screen/LoginScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public class LoginScreen extends AbstractScreen implements InputProcessor {
private String[] url = null;
private AsyncExecutor executor = new AsyncExecutor(4);
private AsyncResult<Void> task;
private TooltipManager tooltipManager = new TooltipManager();

public LoginScreen(GameContext context) {
super();
Expand All @@ -107,7 +108,7 @@ public void buildStage() {
mapEditorButtonStyle = new ImageButtonStyle();
guestButtonStyle = new ImageButtonStyle();
url = null;

tooltipManager.instant();
initTextures();
initGreetings();
message = new Label("", skin);
Expand Down Expand Up @@ -158,30 +159,7 @@ public Void call() {
}
});
}

public void setStatusMessage(String popupMessage) {
message.setText(popupMessage);
message.setPosition(Gdx.graphics.getWidth()/2 - message.getText().length() * 3, 170);
}


public void addStage() {
stage.addActor(accountName);
stage.addActor(password);
stage.addActor(settingTable);
stage.addActor(loginButton);
stage.addActor(buttonMapEditor); // comment to toggle
stage.addActor(buttonGuest); // comment to toggle
stage.addActor(message);
stage.addActor(passwordMode);
}

public Color toRGB(int r, int g, int b) {
float RED = r / 255.0f;
float GREEN = g / 255.0f;
float BLUE = b / 255.0f;
return new Color(RED, GREEN, BLUE, 1);
}


/*
* Greeting list for startup screen
Expand Down Expand Up @@ -246,33 +224,6 @@ public void initTextures() {
guestDisabledDrawable = new TextureRegionDrawable(new TextureRegion(guestButtonDown));
}

/*
* fill selectboxes with appropriate information
*/
public void fillSelectBoxes() {
String[] rooms = Constants.SERVER_ROOMS.values().toArray(new String[Constants.SERVER_ROOMS.size()]);
roomLabel.setItems(rooms);
}

/*
* Initialize properties such as team info/resolution, etc.
*/
public void fillInfo() {
accountName = new TextField("", skin);
if(Constants.USERPROPERTIES.get("user.accountname") != null) {
accountName.setText(Constants.USERPROPERTIES.get("user.accountname"));
}

password = new TextField(Constants.SERVER_CODE, skin);
password.setPasswordCharacter('*');
password.setPasswordMode(true);

accountName.setWidth(200);
password.setWidth(200);
accountName.setMaxLength(Constants.MAX_NAME_SIZE);
password.setMaxLength(Constants.MAX_NAME_SIZE);
}

public void initListeners() {
passwordMode.addListener(new ChangeListener() {
@Override
Expand All @@ -299,6 +250,8 @@ public void run() {
});
}
});
buttonGuest.addListener(new TextTooltip("Login as guest", tooltipManager, skin));
buttonMapEditor.addListener(new TextTooltip("Open map editor", tooltipManager, skin));
buttonMapEditor.addListener(new ChangeListener() {//runs update if there is one before logging in
public void changed(ChangeEvent event, Actor actor) {
Gdx.app.postRunnable(new Runnable() {
Expand Down Expand Up @@ -349,6 +302,57 @@ public void changed(ChangeListener.ChangeEvent event, Actor actor) {
}
});
}


public void setStatusMessage(String popupMessage) {
message.setText(popupMessage);
message.setPosition(Gdx.graphics.getWidth()/2 - message.getText().length() * 3, 170);
}


public void addStage() {
stage.addActor(accountName);
stage.addActor(password);
stage.addActor(settingTable);
stage.addActor(loginButton);
stage.addActor(buttonMapEditor); // comment to toggle
stage.addActor(buttonGuest); // comment to toggle
stage.addActor(message);
stage.addActor(passwordMode);
}

public Color toRGB(int r, int g, int b) {
float RED = r / 255.0f;
float GREEN = g / 255.0f;
float BLUE = b / 255.0f;
return new Color(RED, GREEN, BLUE, 1);
}
/*
* fill selectboxes with appropriate information
*/
public void fillSelectBoxes() {
String[] rooms = Constants.SERVER_ROOMS.values().toArray(new String[Constants.SERVER_ROOMS.size()]);
roomLabel.setItems(rooms);
}

/*
* Initialize properties such as team info/resolution, etc.
*/
public void fillInfo() {
accountName = new TextField("", skin);
if(Constants.USERPROPERTIES.get("user.accountname") != null) {
accountName.setText(Constants.USERPROPERTIES.get("user.accountname"));
}

password = new TextField(Constants.SERVER_CODE, skin);
password.setPasswordCharacter('*');
password.setPasswordMode(true);

accountName.setWidth(200);
password.setWidth(200);
accountName.setMaxLength(Constants.MAX_NAME_SIZE);
password.setMaxLength(Constants.MAX_NAME_SIZE);
}

/*
* Set position of actors in stage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextTooltip;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton.ImageButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
Expand Down Expand Up @@ -688,7 +689,6 @@ public void clicked(InputEvent event, float x, float y) {
whirlSEButton.setDisabled(true);
}
});

whirlSWButton.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
Expand All @@ -700,8 +700,6 @@ public void clicked(InputEvent event, float x, float y) {
whirlSWButton.setDisabled(true);
}
});


whirlNEButton.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
Expand All @@ -713,7 +711,6 @@ public void clicked(InputEvent event, float x, float y) {
whirlNEButton.setDisabled(true);
}
});

whirlNWButton.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
Expand Down Expand Up @@ -758,6 +755,7 @@ public void clicked(InputEvent event, float x, float y) {
windSButton.setDisabled(true);
}
});
flag3Button.addListener(new TextTooltip("3-Point flag", skin));
flag3Button.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
Expand Down Expand Up @@ -813,7 +811,6 @@ public void clicked(InputEvent event, float x, float y) {
smallRockButton.setDisabled(true);
}
});

loadButton.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
Expand All @@ -825,7 +822,6 @@ public void clicked(InputEvent event, float x, float y) {
loadMap();
}
});

addWhirlButton.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
Expand All @@ -847,7 +843,6 @@ public void clicked(InputEvent event, float x, float y) {
saveMap();
}
});

helpButton.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
Expand All @@ -859,7 +854,6 @@ public void clicked(InputEvent event, float x, float y) {
showHelp();
}
});

newButton.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
Expand All @@ -878,7 +872,6 @@ public void clicked(InputEvent event, float x, float y) {
context.exitMapEditor();
}
});

}

public void initButtons() {
Expand Down
5 changes: 4 additions & 1 deletion client-core/src/com/benberi/cadesim/util/ShipSelection.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import com.badlogic.gdx.graphics.Texture;
import com.benberi.cadesim.Constants;
import com.benberi.cadesim.GameContext;

public class ShipSelection {
Expand Down Expand Up @@ -58,7 +59,9 @@ public ShipSelection(GameContext context) {
shipList.add(new Tuple<String, Texture>("War Frigate", warfrig));
shipList.add(new Tuple<String, Texture>("War Galleon", wargal));
shipList.add(new Tuple<String, Texture>("Grand Frigate", grandfrig));
shipList.add(new Tuple<String, Texture>("Black Ship", blackship));
if (Constants.ENABLE_DEVELOPER_FEATURES) {
shipList.add(new Tuple<String, Texture>("Black Ship", blackship));
}
}

public int getCurrentShipAsInt() {
Expand Down
28 changes: 18 additions & 10 deletions client-core/src/com/benberi/cadesim/util/SqlDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.badlogic.gdx.Gdx;
import com.benberi.cadesim.GameContext;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
Expand Down Expand Up @@ -52,15 +53,22 @@ public static void connect(String username, String password, GameContext context
Class.forName(driver);
con = DriverManager.getConnection(url+db, dbUser, dbPasswd);
try{
Statement st = con.createStatement();
String sql = "select * from account where aname=? and password=?";
String query = "select * from account where aname=? and password=?";

int update = st.executeUpdate(sql);
if(update >= 1){
System.out.println("Row is updated.");
}
else{
System.out.println("Row is not updated.");
PreparedStatement stmt = con.prepareStatement(query);
//Parameters
stmt.setString(1, username);
stmt.setString(2, password);
//Execute
ResultSet rs = stmt.executeQuery();
if(rs.next()) {
Gdx.app.postRunnable(new Runnable() {
@Override
public void run() {
context.setAccountPass(password);
ScreenManager.getInstance().showScreen(ScreenEnum.SELECTION, context);
}
});
}
}catch (SQLException s){
System.out.println("SQL statement is not executed!");
Expand Down
Loading

0 comments on commit 71635ca

Please sign in to comment.