Commit b795bd4e authored by mohammadmahdihn's avatar mohammadmahdihn Committed by soldier.mh.2001
Browse files

chertopert

parent 6297193a
......@@ -9,6 +9,15 @@ import java.util.ArrayList;
public class MainClient {
private static String token;
public static String getToken() {
return token;
}
public static void setToken(String token) {
MainClient.token = token;
}
private static Socket socket;
private static DataInputStream dataInputStream;
......@@ -42,25 +51,14 @@ public class MainClient {
private static void restoreDatabase() throws Exception {
createResourceFileIfNeeded();
CreateNewCard.deserialize();
//think about it later:
AllCardsInitiator.fillAllCards();
AllCardsInitiator.setPrices();
MonsterCard.deserialize();
SpellCard.deserialize();
TrapCard.deserialize();
User.deserialize();
}
private static void createResourceFileIfNeeded() throws IOException {
ArrayList<File> files= new ArrayList<>();
files.add(new File("src/UserOutput.json"));
files.add(new File("src/MonsterCardsOutput.json"));
files.add(new File("src/SpellCardsOutput.json"));
files.add(new File("src/TrapCardsOutput.json"));
files.add(new File("src/DecksOutput.json"));
files.add(new File("src/NewMonsterOutput.json"));
files.add(new File("src/NewTrapOutput.json"));
files.add(new File("src/NewSpellOutput.json"));
File cardsFolder = new File("src/ExportedCards");
cardsFolder.mkdir();
for (File file : files)
......
......@@ -16,6 +16,7 @@ import java.util.Random;
public class User {
@Expose
private String username;
@Expose
......
import java.io.DataInputStream;
import java.io.DataOutputStream;
import model.MonsterCard;
import model.SpellCard;
import model.TrapCard;
import model.User;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -9,19 +14,56 @@ public class MainServer {
private ServerSocket serverSocket;
public static void main(String[] args){
public static void main(String[] args) {
MainServer server = new MainServer();
try {
restoreDatabase();
} catch (Exception e) {
e.printStackTrace();
}
server.initializeNetwork();
}
private static void restoreDatabase() throws Exception {
createResourceFileIfNeeded();
// CreateNewCard.deserialize();
// AllCardsInitiator.fillAllCards();
// AllCardsInitiator.setPrices();
MonsterCard.deserialize();
SpellCard.deserialize();
TrapCard.deserialize();
User.deserialize();
}
private static void createResourceFileIfNeeded() throws IOException {
ArrayList<File> files = new ArrayList<>();
files.add(new File("src/UserOutput.json"));
files.add(new File("src/MonsterCardsOutput.json"));
files.add(new File("src/SpellCardsOutput.json"));
files.add(new File("src/TrapCardsOutput.json"));
files.add(new File("src/DecksOutput.json"));
// files.add(new File("src/NewMonsterOutput.json"));
// files.add(new File("src/NewTrapOutput.json"));
// files.add(new File("src/NewSpellOutput.json"));
for (File file : files)
if (!file.exists()) {
file.createNewFile();
FileWriter writer = new FileWriter(file);
writer.write("[]");
writer.close();
}
}
public void initializeNetwork() {
try {
serverSocket = new ServerSocket(7877);
while(true){
Socket socket = serverSocket.accept();
while (true) {
Socket socket = serverSocket.accept(); //stop here while there is no client to connect
startNewThread(socket);
}
} catch(Exception e){
} catch (Exception e) {
e.printStackTrace();
}
}
......@@ -30,69 +72,41 @@ public class MainServer {
new Thread(() -> {
try {
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
while (true) {
String input = dataInputStream.readUTF();
String result = process(input);
Object result = process(input);
if (result.equals("")) break;
dataOutputStream.writeUTF(result);
dataOutputStream.flush();
objectOutputStream.writeObject(result);
objectOutputStream.flush();
}
dataInputStream.close();
objectOutputStream.close();
socket.close();
} catch (Exception e){
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
private static String process(String input) {
private static Object process(String input) {
Matcher[] matchers = getCommandMatchers(input);
if(matchers[2].find()) {
return matchers[2].group("username") + " " + matchers[2].group("password") + " " + matchers[2].group("nickname");
} return "nashod";
if (matchers[0].find()) {
return matchers[0].group("username") + " " + matchers[0].group("password") + " " + matchers[0].group("nickname");
}
return "nashod";
}
private static Matcher[] getCommandMatchers(String command) {
Pattern patternForExit = Pattern.compile("^menu exit$");
Pattern patternForShowCurrentMenu = Pattern.compile("^menu show-current$");
Pattern patternForCreateUser1 = Pattern.compile("^user create -u (?<username>.+?) -p (?<password>.+?) -n (?<nickname>.+?)$");
Pattern patternForCreateUser2 = Pattern.compile("^user create -u (?<username>.+?) -n (?<nickname>.+?) -p (?<password>.+?)$");
Pattern patternForCreateUser3 = Pattern.compile("^user create -p (?<password>.+?) -u (?<username>.+?) -n (?<nickname>.+?)$");
Pattern patternForCreateUser4 = Pattern.compile("^user create -p (?<password>.+?) -n (?<nickname>.+?) -u (?<username>.+?)$");
Pattern patternForCreateUser5 = Pattern.compile("^user create -n (?<nickname>.+?) -p (?<password>.+?) -u (?<username>.+?)$");
Pattern patternForCreateUser6 = Pattern.compile("^user create -n (?<nickname>.+?) -u (?<username>.+?) -p (?<password>.+?)$");
Pattern patternForCreateUser7 = Pattern.compile("^user create --username (?<username>.+?) --password (?<password>.+?) --nickname (?<nickname>.+?)$");
Pattern patternForCreateUser8 = Pattern.compile("^user create --username (?<username>.+?) --nickname (?<nickname>.+?) --password (?<password>.+?)$");
Pattern patternForCreateUser9 = Pattern.compile("^user create --password (?<password>.+?) --username (?<username>.+?) --nickname (?<nickname>.+?)$");
Pattern patternForCreateUser10 = Pattern.compile("^user create --password (?<password>.+?) --nickname (?<nickname>.+?) --username (?<username>.+?)$");
Pattern patternForCreateUser11 = Pattern.compile("^user create --nickname (?<nickname>.+?) --password (?<password>.+?) --username (?<username>.+?)$");
Pattern patternForCreateUser12 = Pattern.compile("^user create --nickname (?<nickname>.+?) --username (?<username>.+?) --password (?<password>.+?)$");
Pattern patternForLoginUser1 = Pattern.compile("^user login --username (?<username>.+?) --password (?<password>.+?)$");
Pattern patternForLoginUser2 = Pattern.compile("^user login --password (?<password>.+?) --username (?<username>.+?)$");
Pattern patternForLoginUser3 = Pattern.compile("^user login -u (?<username>.+?) -p (?<password>.+?)$");
Pattern patternForLoginUser4 = Pattern.compile("^user login -p (?<password>.+?) -u (?<username>.+?)$");
Pattern patternForEnterAnotherMenu = Pattern.compile("^menu enter (Duel|Scoreboard|Deck|Import/Export|Shop|Profile)$");
Pattern patternForLoginUser1 = Pattern.compile("^user login -u (?<username>.+?) -p (?<password>.+?)$");
Matcher[] commandMatchers = new Matcher[19];
commandMatchers[0] = patternForExit.matcher(command);
commandMatchers[1] = patternForShowCurrentMenu.matcher(command);
commandMatchers[2] = patternForCreateUser1.matcher(command);
commandMatchers[3] = patternForCreateUser2.matcher(command);
commandMatchers[4] = patternForCreateUser3.matcher(command);
commandMatchers[5] = patternForCreateUser4.matcher(command);
commandMatchers[6] = patternForCreateUser5.matcher(command);
commandMatchers[7] = patternForCreateUser6.matcher(command);
commandMatchers[8] = patternForCreateUser7.matcher(command);
commandMatchers[9] = patternForCreateUser8.matcher(command);
commandMatchers[10] = patternForCreateUser9.matcher(command);
commandMatchers[11] = patternForCreateUser10.matcher(command);
commandMatchers[12] = patternForCreateUser11.matcher(command);
commandMatchers[13] = patternForCreateUser12.matcher(command);
commandMatchers[14] = patternForLoginUser1.matcher(command);
commandMatchers[15] = patternForLoginUser2.matcher(command);
commandMatchers[16] = patternForLoginUser3.matcher(command);
commandMatchers[17] = patternForLoginUser4.matcher(command);
commandMatchers[18] = patternForEnterAnotherMenu.matcher(command);
commandMatchers[0] = patternForCreateUser1.matcher(command);
commandMatchers[1] = patternForLoginUser1.matcher(command);
return commandMatchers;
}
......
package model;
import java.util.ArrayList;
import java.util.Collections;
public class Scoreboard {
ArrayList<User> users;
public Scoreboard(ArrayList<User> users) {
this.users = users;
}
public String toString() {
StringBuilder scoreboardToStringBuilder = new StringBuilder();
sortUsers();
int upperTeamRanking = 1;
int upperTeamScore = -1;
for(int i=1; i <= users.size(); i++){
if(users.get(i-1).getScore() == upperTeamScore){
scoreboardToStringBuilder.append(upperTeamRanking);
} else {
upperTeamRanking = i;
upperTeamScore = users.get(i-1).getScore();
scoreboardToStringBuilder.append(i);
}
scoreboardToStringBuilder.append("- ");
scoreboardToStringBuilder.append(users.get(i-1).getNickname());
scoreboardToStringBuilder.append(": ");
scoreboardToStringBuilder.append(users.get(i-1).getScore());
scoreboardToStringBuilder.append("\n");
}
return scoreboardToStringBuilder.toString();
}
private void sortUsers() {
for (int i = 0; i < users.size(); i++) {
for (int j = 0; j < users.size() - 1; j++) {
if (users.get(j).getScore() < users.get(j + 1).getScore()) {
Collections.swap(users, j, j + 1);
} else if (users.get(j).getScore() == users.get(j + 1).getScore() &&
Utilities.compareAlphabetical(users.get(j).getNickname(), users.get(j + 1).getNickname()) > 0) {
Collections.swap(users, j, j + 1);
}
}
}
}
}
package model.enums;
import org.model.MonstersDescriptions;
public enum MonsterPower {
NONE,
......@@ -21,31 +20,7 @@ public enum MonsterPower {
EXPLODER_DRAGON,
TERRATIGER_THE_EMPOWERED_WARRIOR,
THE_TRICKY,
RITUAL;
public String description;
static {
COMMAND_KNIGHT.description = MonstersDescriptions.commandKnight;
YOMI_SHIP.description = MonstersDescriptions.yomiShip;
SUIJIN.description = MonstersDescriptions.suijin;
MAN_EATER_BUG.description = MonstersDescriptions.manEaterBug;
GATE_GUARDIAN.description = MonstersDescriptions.gateGuardian;
SCANNER.description = MonstersDescriptions.scanner;
CRAB_TURTLE.description = MonstersDescriptions.crabTurtle;
SKULL_GUARDIAN.description = MonstersDescriptions.skullGuardian;
MARSHMALLON.description = MonstersDescriptions.marshmallon;
BEAST_KING_BARBAROS.description = MonstersDescriptions.beastKing;
TEXCHANGER.description = MonstersDescriptions.texChanger;
THE_CALCULATOR.description = MonstersDescriptions.theCalculator;
MIRAGE_DRAGON.description = MonstersDescriptions.mirageDragon;
HERALD_OF_CREATION.description = MonstersDescriptions.heraldOfTheCreation;
EXPLODER_DRAGON.description = MonstersDescriptions.exploderDragon;
TERRATIGER_THE_EMPOWERED_WARRIOR.description = MonstersDescriptions.terraTigerTheEmpoweredWarrior;
THE_TRICKY.description = MonstersDescriptions.theTricky;
}
RITUAL,
}
package model.enums;
import org.model.SpellsDescription;
public enum SpellEffect {
MONSTER_REBORN,
TERRAFORMING,
......@@ -25,32 +23,5 @@ public enum SpellEffect {
BLACK_PENDANT,
UNITED_WE_STAND,
MAGNUM_SHIELD,
ADVANCED_RITUAL_ART;
public String description;
static {
MONSTER_REBORN.description = SpellsDescription.monsterReborn;
TERRAFORMING.description = SpellsDescription.terrafoming;
POT_OF_GREED.description = SpellsDescription.potOfGreed;
RAIGEKI.description = SpellsDescription.raigeki;
CHANGEOFHEART.description = SpellsDescription.changeOfHearts;
SWORDS_OF_REVEALING_LIGHT.description = SpellsDescription.swordOfRevealingLight;
HARPIES_FEATHER_DUSTER.description = SpellsDescription.harpies;
DARK_HOLE.description = SpellsDescription.darkHole;
SUPPLY_SQUAD.description = SpellsDescription.supplySquad;
SPELL_ABSORPTION.description = SpellsDescription.spellAbsorption;
MESSENGER_OF_PEACE.description = SpellsDescription.messengerOfPeace;
TWIN_TWISTERS.description = SpellsDescription.twinTwister;
MYSTICAL_SPACE_TYPHOON.description = SpellsDescription.mysticalSpaceTyphoon;
RING_OF_DEFENCE.description = SpellsDescription.ringOfDefence;
YAMI.description = SpellsDescription.yami;
FOREST.description = SpellsDescription.forest;
CLOSED_FOREST.description = SpellsDescription.closedForest;
UMIIRUKA.description = SpellsDescription.umiiruka;
SWORD_OF_DARK_DESTRUCTION.description = SpellsDescription.swordOfDestruction;
BLACK_PENDANT.description = SpellsDescription.blackPendant;
UNITED_WE_STAND.description = SpellsDescription.unitedWeStand;
MAGNUM_SHIELD.description = SpellsDescription.magnumShield;
ADVANCED_RITUAL_ART.description = SpellsDescription.advancedRitualArt;
}
ADVANCED_RITUAL_ART,
}
package model.enums;
import org.model.TrapsDescription;
public enum TrapEffect {
TRAP_HOLE,
MIRROR_FORCE,
......@@ -13,18 +11,5 @@ public enum TrapEffect {
SOLEMN_WARNING,
MAGIC_JAMMER,
CALL_OF_THE_HAUNTED;
public String description;
static {
TRAP_HOLE.description = TrapsDescription.trapHole;
MIRROR_FORCE.description = TrapsDescription.mirrorForce;
MAGIC_CYLINDER.description = TrapsDescription.magicCylinder;
MIND_CRUSH.description = TrapsDescription.mindCrush;
TORRENTIAL_TRIBUTE.description = TrapsDescription.torrentialTribute;
TIME_SEAL.description = TrapsDescription.timeSeal;
NEGATE_ATTACK.description = TrapsDescription.negateAttack;
SOLEMN_WARNING.description = TrapsDescription.solemnWarning;
MAGIC_JAMMER.description = TrapsDescription.magicJammer;
CALL_OF_THE_HAUNTED.description = TrapsDescription.callOfTheHaunted;
}
}
package serverController;
import model.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LoginMenuController {
private void controlLoginUserCommand(String username, String password) throws Exception {
if (User.isUsernameAvailable(username) || !User.getUserByUsername(username).getPassword().equals(password)) {
// loginMenuView.usernameAndPasswordDidNotMatch();
} else {
// new MainMenuController(User.getUserByUsername(username)).run();
}
}
//TODO
public String controlCreateUserCommand(String username, String password, String nickname) {
if (!User.isUsernameAvailable(username)) {
return "usernameExists";
} else if (!User.isNicknameAvailable(nickname)) {
return "nicknameExists";
} else {
new User(username, password, nickname);
return "userCreated";
}
}
//TODO
public void saveDatabase() {
// CreateNewCard.serialize();
MonsterCard.serialize();
SpellCard.serialize();
TrapCard.serialize();
Deck.serialize();
User.serialize();
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment