Skip to content

Commit

Permalink
keeping track of which card is in what zone and using that info to up…
Browse files Browse the repository at this point in the history
…date properties
  • Loading branch information
Zomis committed Aug 23, 2015
1 parent c34fa4b commit 2501406
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions src/game_board/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function GameboardController(CardshifterServerAPI, $scope, $timeout, $rootScope,
}
};

$scope.cardZones = {}; // contains information about what card is where.
$scope.actions = [];
$scope.doingAction = false;
$scope.playerInfos = playerInfos;
Expand All @@ -35,7 +36,7 @@ function GameboardController(CardshifterServerAPI, $scope, $timeout, $rootScope,
"card": storeCard,
"zoneChange": moveCard,
"targets": setTargets,
"update": updatePlayerProperties
"update": updateProperties
};

CardshifterServerAPI.setMessageListener(function(message) {
Expand Down Expand Up @@ -204,7 +205,9 @@ function GameboardController(CardshifterServerAPI, $scope, $timeout, $rootScope,
if(playerInfos[player].id === zone.owner) {
var newEntities = {};
for(var i = 0, length = zone.entities.length; i < length; i++) {
newEntities[zone.entities[i]] = {}; // setup each ID to be an key holding an object to store card info
var entityId = zone.entities[i];
newEntities[entityId] = {}; // setup each ID to be an key holding an object to store card info
$scope.cardZones[entityId] = zone.id;
}
zone.entities = newEntities;
zone.length = function () {
Expand Down Expand Up @@ -268,6 +271,7 @@ function GameboardController(CardshifterServerAPI, $scope, $timeout, $rootScope,

var card = src.entities[message.entity];
delete src.entities[message.entity];
$scope.cardZones[message.entity] = message.destinationZone;
dest.entities[message.entity] = card;
} catch(e) {
/*
Expand Down Expand Up @@ -301,13 +305,18 @@ function GameboardController(CardshifterServerAPI, $scope, $timeout, $rootScope,
}

/*
* Updates a players properties based on the message received.
* Updates properties based on the message received.
*
* @param toUpdate:UpdateMessage -- The information on what to update
*
*/
function updatePlayerProperties(toUpdate) {
findPlayer(toUpdate.id).properties[toUpdate.key] = toUpdate.value;
function updateProperties(toUpdate) {
var entity = findEntity(toUpdate.id);
if (!entity) {
console.log('entity not found: ' + toUpdate.id);
return;
}
entity.properties[toUpdate.key] = toUpdate.value;
}

/*
Expand Down Expand Up @@ -341,12 +350,34 @@ function GameboardController(CardshifterServerAPI, $scope, $timeout, $rootScope,
*/
function findPlayer(id) {
if(id === playerInfos.user.id) {
return playerInfos.user;
return playerInfos.user;
} else if(id === playerInfos.opponent.id) {
return playerInfos.opponent;
}
return null;
}

/*
* Finds and returns an entity based on an ID
*
* @param id:number -- The ID of the entity
* @param Object -- playerInfos.user or playerInfos.opponent
* -- a card object
* -- null, if no entity with that ID was found
*/
function findEntity(id) {
var player = findPlayer(id);
if (player) {
return player;
} else {
var zoneId = $scope.cardZones[id];
var zone = findZone(zoneId);
if (zone.entities[id]) {
return zone.entities[id];
}
}
return null;
}
}

module.exports = GameboardController;

0 comments on commit 2501406

Please sign in to comment.