Skip to content

Commit

Permalink
Rule 514.1 & Client/Server Fix (#17)
Browse files Browse the repository at this point in the history
- Risolto problema con la regola 514.1 (non era implementata affatto 🪨 ) [ #3 ]
- Risolti vari problemi che affliggevano il Server Java e NodeJs [ #11 ]
  • Loading branch information
ncvescera authored Dec 29, 2022
2 parents 3e02474 + b39aa63 commit 3e5f26b
Show file tree
Hide file tree
Showing 7 changed files with 946 additions and 639 deletions.
2 changes: 2 additions & 0 deletions mtgengine/src/main/java/com/magicengine/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class Game {
public static final int DURING_TIME_FRAME = 2;
public static final int END_TIME_FRAME = 0;
public static final int ABSENT_TIME_FRAME = -1;
public static final int CLEANUP_TIME_FRAME = 3; //Frame per aspettare la scelta dell'utente alla regola 514.1

// STAGE
public static final int STARTING_STAGE = 0;
Expand All @@ -97,6 +98,7 @@ public class Game {
public static final int CHOOSE_STARTING_PLAYER = 1;
public static final int TAKE_MULLIGAN = 2;
public static final int TAKE_SCRY = 3;
public static final int DISCARD_TO_MAXHANDSIZE = 4;

// MULLIGAN TYPES
public static final int VANCOUVER_MULLIGAN = 1;
Expand Down
20 changes: 19 additions & 1 deletion mtgengine/src/main/java/com/magicengine/GameEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public static void sendToNode(Object toSend, int messageType, int messageSubtype
void exit(String json) {
int idPlayer = gson.fromJson(json, int.class);
for (Player p : game.getPriorityOrder()) {
if (p.getId() == idPlayer) {
if (p.getId() == idPlayer) { //TODO: forse questa condizione deve essere invertita
sendToNode("player " + p.getNickname() + " has disconnected, VICTORY!");
break;
}
Expand Down Expand Up @@ -417,6 +417,24 @@ public void run() {
exit(received);
break;
}

/**
* Questo messaggio permette di uccidere il GameEngine
* e viene utilizzato quando NodeJs viene chiuso (SIGINT) per
* impedire a Java di andare in Loop Infinito
*
* received avrà come valore un numero intero
*
* @author Nicolò Vescera
*/
// JSON "KILL"
received = getJson(cleanJson, "kill");
if (received != null) {
System.out.println("ricevuto kill");
System.out.println(String.format("Killing Game because NodeJs is closed (exit message %s)", received));
break;
}

// JSON "ATTEMPT"
received = getJson(cleanJson, "attempt");
if (received != null) {
Expand Down
22 changes: 21 additions & 1 deletion mtgengine/src/main/java/com/magicengine/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ public class Player implements Target {
//numero dei mulligan usati dal giocatore
private int mulliganCounter=0;

private boolean discarding = false; // true se il giocatore è in fase di scarto
private boolean discarding = false; // true se il giocatore è in fase di scarto mulligan

//Variabili per la gestione della regola 514.1
private boolean discardToMaxHandSize = false; // true se il giocatore deve scartare fino ad avere 7 carte in mano
private boolean beginningDiscardingToMaxHandSize = true; // serve per impedire di creare infinite choiceAnswer

private LinkedList<Integer> attached_by;
private LinkedList<Permanent> attby_perm;
Expand Down Expand Up @@ -496,4 +500,20 @@ public void setDiscarding(boolean discarding) {
this.discarding = discarding;
}

public boolean isDiscardToMaxHandSize() {
return discardToMaxHandSize;
}

public void setDiscardToMaxHandSize(boolean discardToMaxHandSize) {
this.discardToMaxHandSize = discardToMaxHandSize;
}

public boolean isBeginningDiscardingToMaxHandSize() {
return beginningDiscardingToMaxHandSize;
}

public void setBeginningDiscardingToMaxHandSize(boolean beginningDiscardingToMaxHandSize) {
this.beginningDiscardingToMaxHandSize = beginningDiscardingToMaxHandSize;
}

}
17 changes: 17 additions & 0 deletions mtgengine/src/main/java/com/magicengine/Zone.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ public Zone(String nameZone, boolean isPublic,int magicTargetId) {
this.magicTargetId=magicTargetId;
}

/**
* Rimuove una carta dalla Zona prendendo come parametro il magicTargetId della carta da rimuovere.
* @param magicTargetId Id della carta da rimuovere
* @return true se la carta è stata eliminata, altrimenti false
* @author Nicolò Posta, Tommaso Romani, Nicolò Vescera, Fabrizio Fagiolo, Cristian Cosci.
*/
public boolean remove(String magicTargetId) {
int magicTargetIdint = Integer.valueOf(magicTargetId);

for (MagicObject card : this) {
if (card.getMagicTargetId() == magicTargetIdint) {
return remove(card);
}
}

return false;
}

public int getMagicTargetId() {
return magicTargetId;
Expand Down
107 changes: 93 additions & 14 deletions mtgengine/src/main/resources/rules/Rules.drl
Original file line number Diff line number Diff line change
Expand Up @@ -4887,21 +4887,100 @@ end

rule "514.1"
/*
November 19, 2021
First, if the active player�s hand contains more cards than his or her
November 22, 2022
First, if the active players hand contains more cards than his or her
maximum hand size (normally seven), he or she discards enough cards to reduce
his or her hand size to that number. This turn-based action doesn�t use the
stack.*/
his or her hand size to that number. This turn-based action doesnt use the
stack.

Edited by Nicol� Posta, Tommaso Romani, Nicol� Vescera, Fabrizio Fagiolo, Cristian Cosci.
*/
agenda-group "general"
salience 200
salience 300
dialect "mvel"
when
$g:Game(stage == Game.GAME_STAGE, $ac : activePlayer, stepTimeFrame == Game.BEGIN_TIME_FRAME)
eval($g.currentStep.getObject().name=="cleanup")
$p : Player(hand.size < maxHandSize) from $ac.object
$g:Game(stage == Game.GAME_STAGE, $players : players, stepTimeFrame == Game.BEGIN_TIME_FRAME)
eval($g.currentStep.getObject().name=="cleanup")

$p : Player(hand.size > maxHandSize) from $players
then
//passa alla fase di scarto delle carte in eccesso
$p.discardToMaxHandSize = true;
$g.stepTimeFrame = Game.CLEANUP_TIME_FRAME;
System.out.println("514.1 -> discrdToMaxHandSize started");
update($g);
end

rule "514.1 selectHandler"
/*
November 22, 2022
First, if the active players hand contains more cards than his or her
maximum hand size (normally seven), he or she discards enough cards to reduce
his or her hand size to that number. This turn-based action doesnt use the
stack.

Edited by Nicol� Posta, Tommaso Romani, Nicol� Vescera, Fabrizio Fagiolo, Cristian Cosci.
*/
agenda-group "general"
salience 200
dialect "mvel"
when
$g : Game(stage == Game.GAME_STAGE, $players : players, stepTimeFrame == Game.CLEANUP_TIME_FRAME)
$p : Player(discardToMaxHandSize && beginningDiscardingToMaxHandSize) from $players
then
//crea la MakeChoise da inviare al Node
MakeChoice choice = new MakeChoice();
choice.idChoice = Game.DISCARD_TO_MAXHANDSIZE;
choice.choiceText = "Choose " + ($p.getHand().size() - $p.getMaxHandSize()) + " card/s to discard";
for(Card card : $p.hand) {
choice.addOption(card.magicTargetId, card.getNameAsString());
}

//invia la choice al server
GameEngine.sendToNode(choice, Game.CHOICE, Game.MULTIPLE_CHOICE, $p.id);

//Impedisce che questa regola venga avviata molteplici volte mentre il giocatore aspetta di fare la scelta
$p.beginningDiscardingToMaxHandSize = false;
update($g);
end

rule "514.1 answerHandler"
/*
November 22, 2022
First, if the active players hand contains more cards than his or her
maximum hand size (normally seven), he or she discards enough cards to reduce
his or her hand size to that number. This turn-based action doesnt use the
stack.

Edited by Nicol� Posta, Tommaso Romani, Nicol� Vescera, Fabrizio Fagiolo, Cristian Cosci.
*/
agenda-group "general"
salience 100
dialect "mvel"
when
$g : Game(stage == Game.GAME_STAGE, $players : players, stepTimeFrame == Game.CLEANUP_TIME_FRAME)
$p : Player(discardToMaxHandSize && !beginningDiscardingToMaxHandSize) from $players
$ca: ChoiceAnswer(idChoice == Game.DISCARD_TO_MAXHANDSIZE, idPlayer == $p.id)
then
//il giocatore sceglie quale carta scartare
GameEngine.sendToNode("Il giocatore: " + $p.nickname + " sceglie quali carte scartare");

//elimina le carte solo se � stato scelto il numero corretto di carte
if ($ca.getIdOptions().size() == ($p.getHand().size() - $p.getMaxHandSize())) {
for(String cardId : $ca.getIdOptions()) {
$p.hand.remove(cardId);
}
} else {
//avvisa il player della scelta errata della carte da scartare
System.out.println("514.1 -> Il giocatore ha scelto un numero errato di carte !!");
GameEngine.sendToNode("Il giocatore " + $p.nickname + " ha scelto un numero errato di carte !!");
}

//reset delle varibili allo stato iniziale per passare alle fasi successive del gioco
$p.discardToMaxHandSize = false;
$p.beginningDiscardingToMaxHandSize = true;
$g.stepTimeFrame = Game.BEGIN_TIME_FRAME;
retract($ca);
update($p);
update($g);
end

rule "514.2"
Expand Down Expand Up @@ -8060,8 +8139,8 @@ rule "702.31b"
A creature with horsemanship can't be blocked by creatures without horsemanship.
A creature with horsemanship can block a creature with or without horsemanship.

se il bloccate ha horsemanship pu� bloccare creature con o senza horsemanship
un attaccante con horsemanship non pu� essere bloccato da creature senza horsemanship
se il bloccate ha horsemanship pu� bloccare creature con o senza horsemanship
un attaccante con horsemanship non pu� essere bloccato da creature senza horsemanship
*/
dialect "mvel"
no-loop true
Expand All @@ -8085,7 +8164,7 @@ agenda-group "general"
for (Permanent attacker : blocker.blockedCreatures.listReference) {
// ... se almeno 1 attaccante ha shadow ...
if (attacker.checkKeywordAbility("horsemanship")){
// ... il blocco non � valido
// ... il blocco non � valido
System.out.println("Blocco non valido!");
$g.blockValid = false;
}
Expand Down Expand Up @@ -8139,7 +8218,7 @@ agenda-group "general"
boolean typeArtifactCreatureFear = false;
// Per ogni bloccante ...
for (Permanent blocker : $blockers) {
// ... se non � di tipo artefatto o non � di colore nero...
// ... se non � di tipo artefatto o non � di colore nero...
//if (! blocker.getCardType().toString() == "artifact creature" or ! blocker.getColorIndicator("black"))
// ... controllo le creature che blocca ...
for (Permanent attacker : blocker.blockedCreatures.listReference) {
Expand Down
29 changes: 28 additions & 1 deletion mtggameinterface/script/clientL.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,24 @@ window.onload = function() {
socket.on('messaggio', function(data) {
onMessage(data);
});

/**
* Quando il server invia questo messaggio ai client
* vuol dire che un client ha abbandonato o il server NodeJs
* è stato chiuso. Il client deve tornare allo stato iniziale e
* mostra a video il messaggio ricevuto.
*
* @author Fabrizio Fagiolo, Nicolò Vescera
*/
socket.on('clientLeave', function(data) {
disconnectFunction();

//console.log(data);
alert(data);

// ricarica la pagine per resettare lo stato iniziale del client
window.location.reload();
});
};

//BUTTON START GAME
Expand Down Expand Up @@ -368,14 +386,23 @@ window.onload = function() {
};

//BUTTON DISCONNECT
disconnect.onclick = function() {
/**
* Questa funzione fa disconnettere il client e ritorna allo stato iniziale
* Viene avviata quando:
* - il bottone Disconnect viene premuto
* - il server invia un messaggio di disconnect al client
*
* @author Fabrizio Fagiolo, Nicolò Vescera
*/
function disconnectFunction() {
document.getElementById("message").textContent = "disconnected";
console.log("DISCONNECTED");
socket.disconnect();
document.getElementById('nomePlayer').classList.remove("inGamePlayerName");
$('.create_room').show();
$('.in_room').hide();
}
disconnect.onclick = disconnectFunction;

/**************************************************
** OTHER FUNCTIONS **
Expand Down
Loading

0 comments on commit 3e5f26b

Please sign in to comment.