Skip to content

Commit

Permalink
Code cleanup + Client/Server (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
dederobert committed Feb 27, 2019
1 parent 904675b commit 1c2d1df
Show file tree
Hide file tree
Showing 10 changed files with 298 additions and 0 deletions.
104 changes: 104 additions & 0 deletions app/src/main/java/fr/m2ihm/a1819/shi_fu_me/models/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package fr.m2ihm.a1819.shi_fu_me.models;

import android.support.annotation.NonNull;

/**
* Représente les données d'une parties
* @version 1.0.0
*/
public class Game {

public enum GameType{
SINGLEPLAYER,
MULTIPLAYER;
}

/**
* Choix de l'utilisateur
*/
private Choice playerChoice = Choice.UNSET;

/**
* Choix de l'adversaire
*/
private Choice advChoice = Choice.UNSET;

/**
* Score du joueur
*/
private int playerScore = 0;

/**
* Score de l'adversaire
*/
private int advScore = 0;

/**
* Créer un jeu
*/
public Game() {}

/**
* Met à jours les score en fonction des choix
*/
public void updateScore() {
if (this.playerChoice.win(this.advChoice))
this.playerScore++;
else if(this.advChoice.win(this.playerChoice))
this.advScore++;
}

/**
* Obtiens le choix du joueur
* @return Choix du joueur
*/
public @NonNull Choice getPlayerChoice() {
return playerChoice;
}

/**
* Définie le choix du joueur
* @param playerChoice Choix du joueur
*/
public void setPlayerChoice(@NonNull Choice playerChoice) {
this.playerChoice = playerChoice;
}

/**
* Obtiens le choix de l'adversaire
* @return Choix de l'adversaire
*/
public @NonNull Choice getAdvChoice() {
return advChoice;
}

/**
* Définie le choix du joueur
* @param advChoice Choix de l'adversaire
*/
public void setAdvChoice(@NonNull Choice advChoice) {
this.advChoice = advChoice;
}

/**
* Obtiens le score du joueur
* @return Score du joueur
*/
public int getPlayerScore() {
return playerScore;
}

/**
* Obtiens le score de l'adversaire
* @return Le score de l'adversaire
*/
public int getAdvScore() {
return advScore;
}


public void resetScore() {
this.playerScore = 0;
this.advScore = 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package fr.m2ihm.a1819.shi_fu_me.p2p;

import android.content.Context;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ServerSideClient extends Common{
private Socket socket;
private boolean running = true;
private boolean runningOnServerSide = false;

ServerSideClient(Context context, Socket socket) {
super(context);
this.socket = socket;
}

@Override
public void run() {
super.run();
try {
this.setPrintWriter(new PrintWriter(socket.getOutputStream(), true));
this.setBufferedReader(new BufferedReader(new InputStreamReader(socket.getInputStream())));

String response;

response = this.getBufferedReader().readLine();
runningOnServerSide = MessageHeader.HELLO_SERVERSIDE.checkResponse(response);
Log.d("[Server]", "Hello reçut \"" + response + '"');


do {
response = this.getBufferedReader().readLine(); //On recoit le choix du client
if (MessageHeader.END.checkResponse(response)) {running = false; continue;} //Check si on recoit fin de connection
this.getPrintWriter().println(MessageHeader.INFOS + "[opp_choice: , your_score: , opp_score: ]"); //On envoie les infos
} while (running);

} catch (IOException e) {
e.printStackTrace();
}

}

public Socket getSocket() {
return socket;
}

public void setSocket(Socket socket) {
this.socket = socket;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package fr.m2ihm.a1819.shi_fu_me.utils;

/**
* Classe représentant des ressource drawable d'android utilisé pour être rendue par un {@link Drawer}
* @version 1.0.0
*/
public class AndroidDrawableResource extends DrawableResource<Integer> {

/**
* Créer une ressource android
* @param ressourceId Identifiant android de la ressource
*/
public AndroidDrawableResource(int ressourceId) {
super(ressourceId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package fr.m2ihm.a1819.shi_fu_me.utils;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
* Classe représentant une {@link AppCompatImageView} qui peut rendre un objet de type {@link AndroidDrawableResource}
* @version 1.0.0
*/
public class AndroidImageViewDrawer implements Drawer<AndroidDrawableResource> {


private final AppCompatImageView imageView;

public AndroidImageViewDrawer(AppCompatImageView imageView) {
this.imageView = imageView;
}


@Override
public void drawResource(AndroidDrawableResource resource) {
this.imageView.setImageResource(resource.getResource());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package fr.m2ihm.a1819.shi_fu_me.utils;

import fr.m2ihm.a1819.shi_fu_me.models.Choice;
import fr.m2ihm.a1819.shi_fu_me.models.Game;

public class ComputerOpponent implements Opponent {

@Override
public Choice getChoice() {
return Choice.randomChoice();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package fr.m2ihm.a1819.shi_fu_me.utils;

/**
* Représente une resource qui pourra être utilisé par un objet de type {@link Drawer} afin d'être rendue
* @param <T> Type de ressource stocké
* @version 1.0.0
*/
public abstract class DrawableResource<T> {
/**
* Ressource utilisé pour être rendue
*/
private T resource;

/**
* Créer une ressource
* @param resource Ressource à utiliser
*/
DrawableResource(T resource) {
this.resource = resource;
}

/**
* Obtiens la ressource
* @return La ressource à rendre
*/
public T getResource() {
return resource;
}

/**
* Définie la ressource
* @param resource La ressource à être rendue
*/
public void setResource(T resource) {
this.resource = resource;
}

}
13 changes: 13 additions & 0 deletions app/src/main/java/fr/m2ihm/a1819/shi_fu_me/utils/Drawer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fr.m2ihm.a1819.shi_fu_me.utils;

/**
* Classe utilisé pour rendre un objet de type {@link DrawableResource}
* @param <T> Type de {@link DrawableResource} utilisé pour être rendue
*/
public interface Drawer<T extends DrawableResource> {
/**
* Rend la ressource
* @param resource Ressource à rendre
*/
public void drawResource(T resource);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fr.m2ihm.a1819.shi_fu_me.utils;

import fr.m2ihm.a1819.shi_fu_me.models.Choice;

public interface Opponent {
public Choice getChoice();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fr.m2ihm.a1819.shi_fu_me.utils;

import android.support.annotation.NonNull;

import fr.m2ihm.a1819.shi_fu_me.models.Game;

public final class OpponentFactory {
public static @NonNull Opponent getOpponent(final Game.GameType gameType) {
switch (gameType) {
case MULTIPLAYER: return new PlayerOpponent();
case SINGLEPLAYER: default: return new ComputerOpponent();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fr.m2ihm.a1819.shi_fu_me.utils;

import fr.m2ihm.a1819.shi_fu_me.models.Choice;

public class PlayerOpponent implements Opponent {
@Override
public Choice getChoice() {
//TODO Get the player choice
return null;
}
}

0 comments on commit 1c2d1df

Please sign in to comment.