Package com.aqpproject.network.game

Source Code of com.aqpproject.network.game.NetworkProtocol

/*
* AQP Project
* http://http://code.google.com/p/aqp-project/
* Alexandre Gomez - Clément Troesch - Fabrice Latterner
*/
package com.aqpproject.network.game;

import com.aqpproject.game.Singleton;
import com.aqpproject.worldmodel.Control;
import com.aqpproject.worldmodel.game.state.RaceGameState;
import java.util.StringTokenizer;

/**
*
* @author Alexandre
*
*/
import com.aqpproject.game.Singleton;
import com.aqpproject.tools.Vector2D;
import com.aqpproject.worldmodel.data.Item;
import com.aqpproject.worldmodel.game.entity.WEBomb;
import com.aqpproject.worldmodel.game.entity.WECar;
import com.aqpproject.worldmodel.game.entity.WEMine;
import com.aqpproject.worldmodel.game.entity.WEMissile;
import com.aqpproject.worldmodel.game.state.*;
import java.util.ArrayList;
import java.util.Set;

/**
*
* @author Alexandre, Fabrice
*/
/*
* + * Cette classe est une procédure qui va être traité lors de la connection +
* * serveur/client. Celle ci est présent à titre d'exemple et doit être modifié
* + * pour s'adapter à nos besoin +
*/
public class NetworkProtocol {

    public NetworkProtocol(int idPlayer) {
        m_idPlayer = idPlayer;
        m_listKeyboard = new ArrayList<>();
        m_listKeyboard.add(Control.Z);
        m_listKeyboard.add(Control.S);
        m_listKeyboard.add(Control.Q);
        m_listKeyboard.add(Control.D);
        m_listKeyboard.add(Control.SPACE);
        m_listKeyboard.add(Control.SHIFT_LEFT);
        m_listKeyboard.add(Control.GP_UP);
        m_listKeyboard.add(Control.GP_DOWN);
        m_listKeyboard.add(Control.GP_LEFT);
        m_listKeyboard.add(Control.GP_RIGHT);
        m_listKeyboard.add(Control.GP_A);
        m_listKeyboard.add(Control.GP_B);
        m_listKeyboard.add(Control.GP_X);
        m_listKeyboard.add(Control.GP_Y);
        m_listKeyboard.add(Control.GP_BACK);
        m_listKeyboard.add(Control.GP_R2);
        m_listKeyboard.add(Control.GP_L2);

    }

    public String processInput(String theInput) {

        String theOutput = null;
        if (state == INITIALISATION) {
            theOutput = "0;En attente de l'hote de la partie...";
            state = WAITING;
        } else if (state == WAITING) {
            if (Singleton.getNetwork().getSendMessage(m_idPlayer)) {
                String selected_map = Singleton.getOptionsController().getMap();
                theOutput = "2;" + selected_map;
                //   Singleton.getNetwork().setSendMessage(m_idPlayer, false);
            }

            if (theInput != null) {
                StringTokenizer st = new StringTokenizer(theInput, ";");
                String id = st.nextToken();
                switch (Integer.parseInt(id)) {
                    //Récupération nom du joueur
                    case 2:
                        String pseudo = Singleton.getNetwork().getPlayersID().get(m_idPlayer);
                        if (st.hasMoreTokens()) {
                            pseudo = st.nextToken();
                        }
                        Singleton.getNetwork().setPlayerName(m_idPlayer, pseudo);
                        break;
                    //Récupération de la voiture
                    case 3:
                        String car = st.nextToken();
                        Singleton.getNetwork().setPlayerCar(m_idPlayer, car);
                        break;
                    case 4:
                        theOutput = "99";
                }
            }
            if (Singleton.getNetwork().getMustStart()) {
                //Server info
                Singleton.getNetwork().setPlayerName(0, Singleton.getOptionsController().getPseudo());
                Singleton.getNetwork().setPlayerCar(0, Singleton.getOptionsController().getCar());


                int nbPlayers = Singleton.getNetwork().getNbPlayers();
                ArrayList<String> car_list = new ArrayList<>();
                ArrayList<String> pseudo_list = new ArrayList<>();
                ArrayList<Integer> pos_list = new ArrayList<>();
                ArrayList<String> id_list = new ArrayList<>();

                //Creation of bots
                if (Singleton.getNetwork().getPlayersCar().get(7) == null || Singleton.getNetwork().getPlayersCar().get(7).equals("")) {
                    for (int i = nbPlayers; i < 8; i++) {
                        String car = Singleton.getRandomController().getCar();
                        String name = Singleton.getRandomController().getName();
                        Singleton.getNetwork().setBot(car, name, i);
                    }
                }

                for (int i = 0; i < 8; i++) {
                    car_list.add(Singleton.getNetwork().getPlayersCar().get(i));
                    pseudo_list.add(Singleton.getNetwork().getPlayersName().get(i));
                    id_list.add(Singleton.getNetwork().getPlayersID().get(i));
                    pos_list.add(i + 1);
                }



                theOutput = "1;" + "8" + ";" + id_list.get(m_idPlayer);
                for (int i = 0; i < 8; i++) {
                    theOutput += ";" + id_list.get(i) + ";" + car_list.get(i) + ";" + pseudo_list.get(i) + ";" + pos_list.get(i);
                }
                Singleton.getWorldModel().setNextState(new RaceGameState("MULTI", "player1", Singleton.getOptionsController().getMap(), pos_list, pseudo_list, id_list, car_list));

                state = START;
            }
        } else if (state == START) {

            if (theInput != null) {
                StringTokenizer st = new StringTokenizer(theInput, ";");
                st.nextToken();
                st.nextToken();
                String player = Singleton.getNetwork().getPlayersID().get(m_idPlayer);
                for (int i = 0; i < m_listKeyboard.size(); i++) {
                    if (Integer.parseInt(st.nextToken()) == 1) {
                        Singleton.getWorldModel().control(m_listKeyboard.get(i), player);
                    }
                }
            }

            //Send coordinate
            try {
                int nbPlayers = Singleton.getNetwork().getNbPlayers();
                theOutput = "3";
                ArrayList<WECar> car_list = ((RaceGameState) Singleton.getWorldModel().getState()).getCars();
                for (int i = 1; i <= 8; i++) {
                    String name = "player" + i;
                    float posX = Singleton.getVisualisation().getActor("player" + i).x;
                    float posY = Singleton.getVisualisation().getActor("player" + i).y;
                    float rot = Singleton.getVisualisation().getActor("player" + i).rotation;
                    float velX = Singleton.getPhysics().getLinearVelocity("player" + i).x;
                    float velY = Singleton.getPhysics().getLinearVelocity("player" + i).y;
                    boolean isPoweredDown = car_list.get(i - 1).getPowerDown();
                    boolean isBoosted = car_list.get(i - 1).getBoost();
                    boolean isColliding = car_list.get(i - 1).getColliding();
                    boolean isStatic = car_list.get(i - 1).getIsStaticContact();
                    Vector2D point = car_list.get(i - 1).getContactPoint();
                    Item it = car_list.get(i - 1).getInventory().peek();
                    String type;
                    Integer num_item;

                    if (it == null) {
                        type = "null";
                        num_item = 0;
                    } else {
                        type = it.getType().toString();
                        switch (type) {
                            case "BOMB":
                                type = "Bomb";
                                break;
                            case "MISSILE":
                                type = "Missile";
                                break;
                            case "MINE":
                                type = "Mine";
                                break;
                            case "POWERDOWN":
                                type = "PowerDown";
                                break;
                            case "BOOST":
                                type = "Boost";
                                break;
                            case "TROLL":
                                type = "Troll";
                                break;
                        }
                        num_item = Integer.parseInt(it.getName().substring(4));

                    }
                    theOutput += ";" + name + ";" + posX + ";" + posY + ";" + rot + ";" + velX + ";" + velY + ";" + isPoweredDown + ";" + isBoosted + ";" + isColliding + ";" + point.x + ";" + point.y + ";" + isStatic + ";" + type + ";" + num_item;
                }
                WECar troll_emitter = ((RaceGameState) Singleton.getWorldModel().getState()).getTrollEmitter();
                String troll_emitter_name = "null";
                if (troll_emitter != null) {
                    troll_emitter_name = troll_emitter.getName();
                }

                boolean isTrolling = ((RaceGameState) Singleton.getWorldModel().getState()).getIsTrolling();

                //Items
                ArrayList<String> itemList = new ArrayList<>();
                for (String s : ((RaceGameState) Singleton.getWorldModel().getState()).getWorldEntities().keySet()) {
                    itemList.add(s);
                }
                ArrayList<String> missileList = new ArrayList<>();
                ArrayList<String> bombList = new ArrayList<>();
                ArrayList<String> mineList = new ArrayList<>();
                ArrayList<String> dynamicObjectList = new ArrayList<>();
                for (String s : itemList) {
                    if (s.startsWith("Missile_") && !s.endsWith("_Explosion")) {
                        missileList.add(s);
                    }
                    if (s.startsWith("Bomb_") && !s.endsWith("_Explosion")) {
                        bombList.add(s);
                    }
                    if (s.startsWith("Mine_") && !s.endsWith("_Explosion")) {
                        mineList.add(s);
                    }
                    if (s.startsWith("dynamic")) {
                        dynamicObjectList.add(s);
                    }
                }
                //Missiles
                theOutput += ";" + missileList.size();
                for (String s : missileList) {
                    Vector2D itemPos = Singleton.getPhysics().getBodyPosition(s);
                    float itemRot = Singleton.getVisualisation().getActor(s).rotation;
                    String emitter = ((WEMissile) ((RaceGameState) (Singleton.getWorldModel().getState())).getWorldEntities().get(s)).getEmitter().getName();
                    theOutput += ";" + s + ";" + itemPos.x + ";" + itemPos.y + ";" + itemRot + ";" + emitter;
                }
                //Bombs
                theOutput += ";" + bombList.size();
                for (String s : bombList) {
                    Vector2D itemPos = Singleton.getPhysics().getBodyPosition(s);
                    float itemRot = Singleton.getVisualisation().getActor(s).rotation;
                    String emitter = ((WEBomb) ((RaceGameState) (Singleton.getWorldModel().getState())).getWorldEntities().get(s)).getEmitter().getName();
                    theOutput += ";" + s + ";" + itemPos.x + ";" + itemPos.y + ";" + itemRot + ";" + emitter;
                }
                //Mines
                theOutput += ";" + mineList.size();
                for (String s : mineList) {
                    Vector2D itemPos = Singleton.getPhysics().getBodyPosition(s);
                    float itemRot = Singleton.getVisualisation().getActor(s).rotation;
                    theOutput += ";" + s + ";" + itemPos.x + ";" + itemPos.y + ";" + itemRot;
                }
                //Dynamic objects
                theOutput += ";" + dynamicObjectList.size();
                for (String s : dynamicObjectList) {
                    Vector2D itemPos = Singleton.getPhysics().getBodyPosition(s);
                    float itemRot = Singleton.getVisualisation().getActor(s).rotation;
                    theOutput += ";" + s + ";" + itemPos.x + ";" + itemPos.y + ";" + itemRot;
                }
                //Troll
                theOutput += ";" + isTrolling + ";" + troll_emitter_name;

                //Arrival list
                ArrayList<String> nameList = new ArrayList<>();
                for (String s : ((RaceGameState) Singleton.getWorldModel().getState()).getFinishNames()) {
                    nameList.add(s);
                }
                ArrayList<Long> timeList = new ArrayList<>();
                for (Long l : ((RaceGameState) Singleton.getWorldModel().getState()).getFinishTimes()) {
                    timeList.add(l);
                }
                int nbArrival = ((RaceGameState) Singleton.getWorldModel().getState()).getFinishTimes().size();

                theOutput += ";" + nbArrival;
                for (int i = 0; i < nbArrival; i++) {
                    theOutput += ";" + nameList.get(i) + ";" + timeList.get(i);
                }

            } catch (Exception e) {
                System.out.println(e.getMessage());
            }

        }

        return theOutput;
    }
    private static final int INITIALISATION = 0;
    private static final int WAITING = 1;
    private static final int START = 2;
    private int state = INITIALISATION;
    private int m_idPlayer;
    ArrayList<Control> m_listKeyboard;
}
TOP

Related Classes of com.aqpproject.network.game.NetworkProtocol

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.