Commit 8ff70ead authored by Prieur Maxime's avatar Prieur Maxime 🎵

Clean Server : Un Thread est lancé coté serveur au debut d'une partie qui...

Clean Server : Un Thread est lancé coté serveur au debut d'une partie qui clean le server apres le temps d'une partie, il est interrompue lors d'un quit propre. Le clean est aussi ajouté lorsque on appuie sur la croix.
parent 62810051
......@@ -8,6 +8,7 @@ import java.util.Iterator;
import java.util.List;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import bombermandistribue.bomberman.entities.Entity;
import bombermandistribue.bomberman.entities.Message;
......@@ -26,7 +27,7 @@ import bombermandistribue.bomberman.level.Level;
import server.ServerGame;
public class Board implements IRender {
private static final long serialVersionUID = 1L;
public int _width, _height;
protected Level _level;
protected Game _game;
......@@ -42,13 +43,13 @@ public class Board implements IRender {
private int _time = Game.TIME;
private int _lives = Game.LIVES;
private Running _running;
private int _winner = -1;
private ServerGame _serverGame;
private int _playerNum;
private List<Message> _pseudo = new ArrayList<Message>();
public Board(Game game,
protected Board(Game game,
Keyboard input,
Screen screen,
ServerGame serverGame,
......@@ -61,6 +62,8 @@ public class Board implements IRender {
_screen = screen;
_serverGame = serverGame;
try{
_running = new RunningIMPL();
_serverGame.addRunningGame(_running);
_pseudo.add(new Message(serverGame.getPlayerID(0), 0, 0, 1, Color.white, 14));
_pseudo.add(new Message(serverGame.getPlayerID(1), 0, 0, 1, Color.white, 14));
} catch (RemoteException e){
......@@ -318,7 +321,6 @@ public class Board implements IRender {
if(e != null) {
return e;
}
}
return null;
......@@ -508,4 +510,21 @@ public class Board implements IRender {
return this._screenToShow == 1;
}
public void endRunningState(){
try{
_serverGame.endRunningState();
} catch (RemoteException e){
e.printStackTrace();
}
}
public boolean getRunningState(){
try{
//return _serverGame.getRunningState();
return _running.getState();
} catch (RemoteException e){
e.printStackTrace();
}
return false;
}
}
......@@ -2,6 +2,8 @@ package bombermandistribue.bomberman;
import java.util.concurrent.TimeUnit;
import java.rmi.RemoteException;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
......@@ -55,7 +57,6 @@ public class Game extends Canvas {
protected int _screenDelay = SCREENDELAY;
private Keyboard _input;
private boolean _running = false;
private boolean _paused = true;
private Board _board;
......@@ -72,8 +73,7 @@ public class Game extends Canvas {
_frame.setTitle(_title);
screen = new Screen(WIDTH, HEIGHT);
_input = new Keyboard();
_board = new Board(this, _input, screen, this._frame._serverGame, this._frame._playerNum, level);
_board = new Board(this, _input, screen, this._frame._serverGame, this._frame._playerNum, level);
addKeyListener(_input);
}
......@@ -125,8 +125,6 @@ public class Game extends Canvas {
}
public void start() {
_running = true;
long lastTime = System.nanoTime();
long timer = System.currentTimeMillis();
final double ns = 1000000000.0 / 60.0; //nanosecond, 60 frames per second
......@@ -134,7 +132,7 @@ public class Game extends Canvas {
int frames = 0;
int updates = 0;
requestFocus();
while(_running) {
while(_board.getRunningState()) {
long now;
do {
now = System.nanoTime();
......@@ -159,7 +157,7 @@ public class Game extends Canvas {
} catch (InterruptedException e) {
e.printStackTrace();
}
_running = false;
_board.endRunningState();
}
} else {
renderGame();
......@@ -186,6 +184,7 @@ public class Game extends Canvas {
| Getters & Setters
|--------------------------------------------------------------------------
*/
public static int getPlayerSpeed() {
return playerSpeed;
}
......@@ -232,16 +231,15 @@ public class Game extends Canvas {
}
public void run() {
_running = true;
_paused = false;
}
public void stop() {
_running = false;
_board.endRunningState();
}
public boolean isRunning() {
return _running;
return _board.getRunningState();
}
public boolean isPaused() {
......
package bombermandistribue.bomberman;
import java.rmi.RemoteException;
import java.rmi.Remote;
public interface Running extends Remote {
boolean getState() throws RemoteException;
void setState(boolean state) throws RemoteException;
}
\ No newline at end of file
package bombermandistribue.bomberman;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class RunningIMPL extends UnicastRemoteObject implements Running {
private static final long serialVersionUID = 1L;
private boolean state;
protected RunningIMPL() throws RemoteException{
this.state = true;
}
public boolean getState() throws RemoteException{
return this.state;
}
public void setState(boolean state) throws RemoteException{
this.state = state;
}
}
\ No newline at end of file
......@@ -50,8 +50,8 @@ public class Screen {
int color = entity.getSprite().getPixel(x + y * entity.getSprite().getSize());
if(color != _transparentColor)
_pixels[xa + ya * _width] = color;
//else
// _pixels[xa + ya * _width] = below.getPixel(x + y * below.getSize());
else
_pixels[xa + ya * _width] = below.getPixel(x + y * below.getSize());
}
}
}
......
package bombermandistribue.bomberman.gui;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.*;
import bombermandistribue.bomberman.Game;
import bombermandistribue.bomberman.gui.menu.Menu;
......@@ -24,6 +24,14 @@ public class Frame extends JFrame {
public int _playerNum;
public ServerGame _serverGame;
public Frame(ServerGame serverGame, int playerNum, int level) {
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
JOptionPane.showMessageDialog(null,"You left the game...");
_game.getBoard().endRunningState();
}
});
setJMenuBar(new Menu(this));
_serverGame = serverGame;
_playerNum = playerNum;;
......@@ -39,7 +47,6 @@ public class Frame extends JFrame {
add(_containerpane);
setResizable(false);
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
......
......@@ -72,17 +72,12 @@ public class FileLevel extends Level {
_board.addEntitie(pos, new WallTile(x_pixel, y_pixel, Sprite.wall));
break;
case 'b':
System.out.println("add Level entity :"+c);
LayeredEntity layer = new LayeredEntity(x_pixel,y_pixel,
new GrassTile(x_pixel, y_pixel, Sprite.ground),
new BrickTile(x_pixel, y_pixel, Sprite.brick));
if(_board.isPowerupUsed(x_pixel, y_pixel, _level) == false) {
System.out.print("On ajoute le powerup");
if(_board.isPowerupUsed(x_pixel, y_pixel, _level) == false)
layer.addBeforeTop(new PowerupBombs(x_pixel, y_pixel, _level, Sprite.powerup_bombs));
} else {
System.out.print("Pas ajouté :'(");
}
_board.addEntitie(pos, layer);
break;
case 's':
......
package server;
import java.util.concurrent.atomic.AtomicBoolean;
import java.rmi.RemoteException;
public class RunningTimeThread implements Runnable {
private Thread worker;
private AtomicBoolean running = new AtomicBoolean(false);
private int interval;
private ServerGame server;
private boolean stopped;
protected RunningTimeThread(ServerGame server, int gameDuration){
this.interval = gameDuration;
this.server = server;
}
public void interrupt() {
running.set(false);
worker.interrupt();
}
boolean isRunning() {
return running.get();
}
boolean isStopped() {
return stopped;
}
public void run() {
running.set(true);
stopped = false;
while (running.get()) {
try {
Thread.sleep(interval);
running.set(false);
} catch (InterruptedException e){
Thread.currentThread().interrupt();
running.set(false);
}
}
try{
server.resetServer();
} catch (RemoteException e){
e.printStackTrace();
}
stopped = true;
}
}
\ No newline at end of file
......@@ -4,6 +4,7 @@ import java.rmi.Remote;
import java.rmi.RemoteException;
import client.ClientGame;
import bombermandistribue.bomberman.Running;
public interface ServerGame extends Remote{
void sendBroadcastMessage(String message) throws RemoteException;
......@@ -18,4 +19,7 @@ public interface ServerGame extends Remote{
PlayerPosition getMove(int playerNum) throws RemoteException;
String getPlayerID(int playerNum) throws RemoteException;
int getChosenLevel() throws RemoteException;
boolean getRunningState() throws RemoteException;
void endRunningState() throws RemoteException;
void addRunningGame(Running state)throws RemoteException;
}
\ No newline at end of file
......@@ -4,21 +4,27 @@ import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.concurrent.ThreadLocalRandom;
import java.util.Arrays;
import client.ClientGame;
import bombermandistribue.bomberman.Running;
public class ServerGameImpl extends UnicastRemoteObject implements ServerGame {
private static final long serialVersionUID = 1L;
private ArrayList<ClientGame> clientGames;
private ArrayList<Running> runningGames;
private Boolean gameRunning = false;
private String gameId;
private int level;
private PlayerPosition playerTwoPos = new PlayerPosition(0, 0, 0);
private PlayerPosition playerOnePos = new PlayerPosition(0, 0, 0);
private int[] playerBomb = new int[2];
private Boolean runningState = false;
private Thread controlThread;
protected ServerGameImpl() throws RemoteException {
this.clientGames = new ArrayList<ClientGame>();
this.runningGames = new ArrayList<Running>();
this.gameId = "0";
}
......@@ -62,6 +68,9 @@ public class ServerGameImpl extends UnicastRemoteObject implements ServerGame {
public void startGame() throws RemoteException {
this.gameRunning = true;
this.controlThread = new Thread(new RunningTimeThread(this, 10000));
this.controlThread.start();
}
public int getChosenLevel() throws RemoteException {
......@@ -112,15 +121,34 @@ public class ServerGameImpl extends UnicastRemoteObject implements ServerGame {
}
public synchronized void resetServer() throws RemoteException{
this.runningState = false;
this.clientGames.clear();
this.gameRunning = false;
this.playerBomb[0] = 0;
this.playerBomb[1] = 0;
this.playerOnePos = new PlayerPosition(0,0,0);
this.playerTwoPos = new PlayerPosition(0,0,0);
//this.runningGames.clear();
}
public synchronized String getPlayerID(int playerNum) throws RemoteException{
return clientGames.get(playerNum).getUserName();
}
public synchronized boolean getRunningState() throws RemoteException{
return this.runningState;
}
public synchronized void endRunningState() throws RemoteException{
int i = 0;
while (i < runningGames.size()){
runningGames.get(i++).setState(false);
}
//this.runningState = false;
//this.controlThread.interrupt();
}
public synchronized void addRunningGame(Running state) throws RemoteException{
this.runningGames.add(state);
}
}
Markdown is supported
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