monopoly-shared-model
Version:
Shared model for Monopoly
190 lines • 9.53 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const PropertyToken_1 = require("../model/token/PropertyToken");
const Player_1 = require("../model/player/Player");
const EnumBoxType_1 = require("../model/box/enums/EnumBoxType");
const Estate_1 = require("../model/box/Estate");
const Tax_1 = require("../model/box/Tax");
const Station_1 = require("../model/box/Station");
const PublicService_1 = require("../model/box/PublicService");
const Prison_1 = require("../model/box/Prison");
const GoToPrison_1 = require("../model/box/GoToPrison");
const Start_1 = require("../model/box/Start");
const Free_1 = require("../model/box/Free");
const CommunityChest_1 = require("../model/box/CommunityChest");
const Chance_1 = require("../model/box/Chance");
const Log_1 = require("../model/log/Log");
const CanBuyPropertyAction_1 = require("../model/action/CanBuyPropertyAction");
const EnumActionType_1 = require("../model/action/EnumActionType");
const GoToPrisonAction_1 = require("../model/action/GoToPrisonAction");
const HasAllPropertiesFromGroup_1 = require("../model/action/HasAllPropertiesFromGroup");
const HasPaidRentAction_1 = require("../model/action/HasPaidRentAction");
const HasPaidTaxAction_1 = require("../model/action/HasPaidTaxAction");
const IsInPrisonAction_1 = require("../model/action/IsInPrisonAction");
const IsInSimpleVisitPrisonAction_1 = require("../model/action/IsInSimpleVisitPrisonAction");
const NextPlayerThrowDiceAction_1 = require("../model/action/NextPlayerThrowDiceAction");
const NoAction_1 = require("../model/action/NoAction");
const IsBankruptAction_1 = require("../model/action/IsBankruptAction");
const HasBoughtPropertyAction_1 = require("../model/action/HasBoughtPropertyAction");
const HasNotBoughtPropertyAction_1 = require("../model/action/HasNotBoughtPropertyAction");
const ArriveOnBoxAction_1 = require("../model/action/ArriveOnBoxAction");
const DiceThrow_1 = require("../model/rules/DiceThrow");
const LastActions_1 = require("../model/action/storage/LastActions");
const IsWinnerAction_1 = require("../model/action/IsWinnerAction");
const JailedPlayerThrow_1 = require("../model/rules/JailedPlayerThrow");
const AssertGameEndDTO_1 = require("../model/dto/AssertGameEndDTO");
const HasReceivedMoneyFromBankAction_1 = require("../model/action/HasReceivedMoneyFromBankAction");
const MustPayRentAction_1 = require("../model/action/MustPayRentAction");
const MustPayTaxAction_1 = require("../model/action/MustPayTaxAction");
const MustReceiveMoneyFromBankAction_1 = require("../model/action/MustReceiveMoneyFromBankAction");
const HasThrownDicesAction_1 = require("../model/action/HasThrownDicesAction");
class JsonObjectmapper {
static jsonToPropertyToken(json) {
const token = PropertyToken_1.PropertyToken.emptyObject();
token.fromJson(json);
return token;
}
static jsonToPlayer(json) {
const player = Player_1.Player.emptyObject();
player.fromJson(json);
return player;
}
static jsonToLog(json) {
const log = Log_1.Log.emptyObject();
log.fromJson(json);
return log;
}
static jsonToBox(json) {
const jsonObject = JSON.parse(json);
const type = jsonObject.type;
let box;
if (type === EnumBoxType_1.EnumBoxType.ESTATE) {
box = new Estate_1.Estate(null, null, null, null, null, null);
}
else if (type === EnumBoxType_1.EnumBoxType.TAX) {
box = new Tax_1.Tax(null, null, null);
}
else if (type === EnumBoxType_1.EnumBoxType.STATION) {
box = new Station_1.Station(null, null, null, null, null);
}
else if (type === EnumBoxType_1.EnumBoxType.PUBLIC_SERVICE) {
box = new PublicService_1.PublicService(null, null, null, null, null);
}
else if (type === EnumBoxType_1.EnumBoxType.PRISON) {
box = new Prison_1.Prison(null);
}
else if (type === EnumBoxType_1.EnumBoxType.GO_TO_PRISON) {
box = new GoToPrison_1.GoToPrison(null);
}
else if (type === EnumBoxType_1.EnumBoxType.START) {
box = new Start_1.Start(null, null, null);
}
else if (type === EnumBoxType_1.EnumBoxType.FREE) {
box = new Free_1.Free(null, null);
}
else if (type === EnumBoxType_1.EnumBoxType.COMMUNITY_CHEST) {
box = new CommunityChest_1.CommunityChest(null, null);
}
else if (type === EnumBoxType_1.EnumBoxType.CHANCE) {
box = new Chance_1.Chance(null, null);
}
else {
throw new Error(`Invalid json type in box json. Type found: ${type}`);
}
box.fromJson(json);
return box;
}
static jsonToAction(json) {
const jsonObject = JSON.parse(json);
const type = jsonObject.type;
let action;
if (type === EnumActionType_1.EnumActionType.CAN_BUY_PROPERTY) {
action = new CanBuyPropertyAction_1.CanBuyPropertyAction(null, null, null, null, null);
}
else if (type === EnumActionType_1.EnumActionType.GO_TO_PRISON) {
action = new GoToPrisonAction_1.GoToPrisonAction(null, null);
}
else if (type === EnumActionType_1.EnumActionType.HAS_ALL_PROPERTIES_FROM_GROUP) {
action = new HasAllPropertiesFromGroup_1.HasAllPropertiesFromGroup(null, null, null);
}
else if (type === EnumActionType_1.EnumActionType.HAS_PAID_RENT) {
action = new HasPaidRentAction_1.HasPaidRentAction(null, null, null, null, null);
}
else if (type === EnumActionType_1.EnumActionType.HAS_PAID_TAX) {
action = new HasPaidTaxAction_1.HasPaidTaxAction(null, null, null);
}
else if (type === EnumActionType_1.EnumActionType.HAS_RECEIVED_MONEY_FROM_BANK) {
action = new HasReceivedMoneyFromBankAction_1.HasReceivedMoneyFromBankAction(null, null, null);
}
else if (type === EnumActionType_1.EnumActionType.IS_IN_PRISON) {
action = new IsInPrisonAction_1.IsInPrisonAction(null, null);
}
else if (type === EnumActionType_1.EnumActionType.IS_IN_SIMPLE_VISIT_PRISON) {
action = new IsInSimpleVisitPrisonAction_1.IsInSimpleVisitPrisonAction(null, null);
}
else if (type === EnumActionType_1.EnumActionType.NEXT_PLAYER_MUST_THROW_DICES) {
action = new NextPlayerThrowDiceAction_1.NextPlayerThrowDiceAction(null, null);
}
else if (type === EnumActionType_1.EnumActionType.NO_ACTION) {
action = new NoAction_1.NoAction(null, null);
}
else if (type === EnumActionType_1.EnumActionType.IS_BANKRUPT) {
action = new IsBankruptAction_1.IsBankruptAction(null, null);
}
else if (type === EnumActionType_1.EnumActionType.HAS_BOUGHT_PROPERTY) {
action = new HasBoughtPropertyAction_1.HasBoughtPropertyAction(null, null, null);
}
else if (type === EnumActionType_1.EnumActionType.HAS_NOT_BOUGHT_PROPERTY) {
action = new HasNotBoughtPropertyAction_1.HasNotBoughtPropertyAction(null, null, null);
}
else if (type === EnumActionType_1.EnumActionType.ARRIVE_ON_BOX) {
action = new ArriveOnBoxAction_1.ArriveOnBoxAction(null, null, null, null);
}
else if (type === EnumActionType_1.EnumActionType.IS_WINNER) {
action = new IsWinnerAction_1.IsWinnerAction(null, null);
}
else if (type === EnumActionType_1.EnumActionType.MUST_PAY_RENT) {
action = new MustPayRentAction_1.MustPayRentAction(null, null, null, null, null);
}
else if (type === EnumActionType_1.EnumActionType.MUST_PAY_TAX) {
action = new MustPayTaxAction_1.MustPayTaxAction(null, null, null, null);
}
else if (type === EnumActionType_1.EnumActionType.MUST_RECEIVE_MONEY_FROM_BANK) {
action = new MustReceiveMoneyFromBankAction_1.MustReceiveMoneyFromBankAction(null, null, null);
}
else if (type === EnumActionType_1.EnumActionType.HAS_THROWN_DICES) {
action = new HasThrownDicesAction_1.HasThrownDicesAction(null, null, null, null);
}
else {
throw new Error(`Invalid json type in action json. Type found: ${type}`);
}
action.fromJson(json);
return action;
}
static jsonToDiceThrow(json) {
const diceThrow = new DiceThrow_1.DiceThrow(null, null, null);
diceThrow.fromJson(json);
return diceThrow;
}
static jsonToLastActions(json) {
const actionsResponseAsJson = JSON.parse(json.toString()).actions;
let actions = [];
if (Array.isArray(actionsResponseAsJson)) {
actions = actionsResponseAsJson.map((r) => JsonObjectmapper.jsonToAction(JSON.stringify(r)));
}
const lastActions = new LastActions_1.LastActions(actions);
return lastActions;
}
static jsonToJailedPlayerThrow(json) {
const jailedPlayerThrow = JailedPlayerThrow_1.JailedPlayerThrow.emptyObject();
jailedPlayerThrow.fromJson(json);
return jailedPlayerThrow;
}
static jsonToAssertGameEndDTO(json) {
const assertGameEndDTO = AssertGameEndDTO_1.AssertGameEndDTO.emptyObject();
assertGameEndDTO.fromJson(json);
return assertGameEndDTO;
}
}
exports.JsonObjectmapper = JsonObjectmapper;
//# sourceMappingURL=JsonObjectmapper.js.map