Downloads containing academy_networking.asc

Downloads
Name Author Game Mode Rating
TSF with JJ2+ Only: Academy Superjazz Battle N/A Download file

File preview

namespace acNetworking {

	void doReceive(jjSTREAM &in packet, int clientID) {
		int8 senderPlayerID;
		packet.pop(senderPlayerID);
		
		if (jjIsServer && clientID != jjPlayers[senderPlayerID].clientID) {
			int8 potentialHackingPlayerID = jjPlayersWithClientID(clientID)[0].playerID;
			jjAlert("|Warning! Potential hacking attempt from client of player ID "
					+ (potentialHackingPlayerID + 1) + "!", true);
			return;
		}
		
		uint8 packetType;
		packet.pop(packetType);
		
		switch (packetType) {
			case PACKET_AOE:
			{
				uint8 spellID;
				int8 currentCasterID;
				packet.pop(spellID);
				packet.pop(currentCasterID);
				if (jjIsServer) {
					sendAoePacket(senderPlayerID, spellID, currentCasterID);
				}
				acSpells::doAOE(jjPlayers[currentCasterID], jjPlayers[senderPlayerID], spellID);
			}
			break;
			case PACKET_EFFECT:
			{
				uint8 effectID;
				int8 targetPlayerID;
				uint duration;
				packet.pop(effectID);
				packet.pop(targetPlayerID);
				packet.pop(duration);
				if (jjIsServer) {
					sendEffectPacket(senderPlayerID, effectID, targetPlayerID, duration);
				}
				acSpells::doEffect(effectID, targetPlayerID, duration);
			}
			break;
			case PACKET_MASS_EFFECT:
			{
				uint8 effectID;
				uint duration;
				int8[] targets;
				int8 currentCasterID;
				packet.pop(effectID);
				packet.pop(duration);
				packet.pop(currentCasterID);
				while (!packet.isEmpty()) {
					int8 target;
					packet.pop(target);
					targets.insertLast(target);
				}
				if (jjIsServer) {
					sendMassEffectPacket(senderPlayerID, effectID, targets, duration, currentCasterID);
				}
				acSpells::doMassEffect(effectID, targets, duration);
			}
			break;
			case PACKET_CHANNELING_STARTED:
			{
				uint8 spellEnumValue;
				packet.pop(spellEnumValue);
				if (debugModeOn) jjAlert("Channeling started");
				if (jjIsServer) {
					sendChannelingStartedPacket(senderPlayerID, spellEnumValue);
					string channeledSpellKey = acSpells::getSpellKeyByEnumValue(spellEnumValue);
					if (channeledSpellKey != "") {
						players[senderPlayerID].setChanneledSpellKey(channeledSpellKey);
					}
				}
				else {
					string channeledSpellKey = acSpells::getSpellKeyByEnumValue(spellEnumValue);
					if (channeledSpellKey != "" && !jjPlayers[senderPlayerID].isLocal) {
						players[senderPlayerID].setChanneledSpellKey(channeledSpellKey);
					}
				}
			}
			break;
			case PACKET_CHANNELING_STOPPED:
			{
				if (jjIsServer) {
					sendChannelingStoppedPacket(senderPlayerID);
					players[senderPlayerID].unSetChanneledSpellKey();
				}
				else {
					players[senderPlayerID].unSetChanneledSpellKey();
				}
			}
			break;
			case PACKET_PLAYER_DEAD:
			{
				if (debugModeOn) jjAlert("Received");
				if (jjIsServer) {
					sendPlayerDeadPacket(senderPlayerID);
				}
				acEngine::doDeadPlayer(senderPlayerID);
			}
			break;
			case PACKET_WALL_OF_FIRE:
			{
				float xOrigin;
				float yOrigin;
				int height;
				packet.pop(xOrigin);
				packet.pop(yOrigin);
				packet.pop(height);
				
				if (jjIsServer) {
					sendWallOfFirePacket(senderPlayerID, xOrigin, yOrigin, height);
				}
				
				Spell@ spell = cast<Spell@>(spells["I"]);
				uint duration = (spell.baseDuration + players[senderPlayerID].spellDurationBonus) * SECOND;
				acSpells::doWallOfFire(jjPlayers[senderPlayerID], spell, xOrigin, yOrigin, height, duration);
			}
			break;
			case PACKET_CHAIN_LIGHTNING_SERVER:
			{
				int8 currentCasterID;
				int8[] targets;
				packet.pop(currentCasterID);
				while (!packet.isEmpty()) {
					int8 target;
					packet.pop(target);
					targets.insertLast(target);
				}
				if (currentCasterID != senderPlayerID) {
					acUtils::addMagicMirrorAnimation(currentCasterID);
				}
				acUtils::addMagicMirrorAnimationOnTargetsWithMagicMirror(targets);
				array<ChainLightningTarget@> chainLightningTargets = acSpells::doChainLightning(currentCasterID, targets);
				sendChainLightningPacketToClients(currentCasterID, chainLightningTargets);
				chainLightningTargetGroups.insertLast(chainLightningTargets);
			}
			break;
			case PACKET_CHAIN_LIGHTNING_CLIENT:
			{
				array<ChainLightningTarget@> chainLightningTargets;
				while (!packet.isEmpty()) {
					int8 id;
					int8 parentID;
					int8 playerID;
					packet.pop(id);
					packet.pop(parentID);
					packet.pop(playerID);
					chainLightningTargets.insertLast(ChainLightningTarget(id, playerID, 0, CHAIN_LIGHTNING_DEFAULT_DURATION, parentID));
				}
				chainLightningTargetGroups.insertLast(chainLightningTargets);
			}
			break;
			case PACKET_CAPTURE_GEM_MINE:
			{
				int8 gemMineId;
				packet.pop(gemMineId);
				if (jjIsServer) {
					sendCaptureGemMinePacket(senderPlayerID, gemMineId);
				}
				GemMine@ gemMine = gemMineId == 1 ? gemMines[1] : gemMines[0];
				if (gemMine.ownerID == jjLocalPlayers[0].playerID) {
					jjAlert("Your " + gemMine.name + " gem mine was captured by "
							+ jjPlayers[senderPlayerID].nameUnformatted + "!",
							false, STRING::MEDIUM);
				}
				gemMine.capture(senderPlayerID);
			}
			break;
			case PACKET_SKILL_LEARNED:
			{
				uint8 skill;
				packet.pop(skill);
				acUtils::alertOfLearnedSkill(jjPlayers[senderPlayerID].nameUnformatted, SKILL(skill));
			}
			break;
			case PACKET_SKILL_DAMAGE:
			{
				if (jjIsServer) {
					sendSkillPacket(senderPlayerID, packetType);
				}
				players[senderPlayerID].spellDamageBonus++;
			}
			break;
			case PACKET_SKILL_DURATION:
			{
				if (jjIsServer) {
					sendSkillPacket(senderPlayerID, packetType);
				}
				players[senderPlayerID].spellDurationBonus += 10;
			}
			break;
			case PACKET_SPELL_LEARNED:
			{
				string spellKey;
				packet.pop(spellKey);
				acUtils::alertOfLearnedSpell(jjPlayers[senderPlayerID].nameUnformatted,
						cast<Spell@>(spells[spellKey]).name);
			}
			break;
			case PACKET_MAGIC_MIRROR_ANIMATION:
			{
				array<int8> targets;
				int i = 0;
				while (!packet.isEmpty()) {
					int8 playerID;
					packet.pop(playerID);
					targets.insertLast(playerID);
					i++;
				}
				if (jjIsServer) {
					sendMagicMirrorAnimationPacket(senderPlayerID, targets);
				}
				acUtils::addMagicMirrorAnimationOnTargetsWithMagicMirror(targets);
			}
			break;
			case PACKET_REQ_SYNC_DATA:
			{
				sendSkillData(clientID);
				sendEffectData(clientID);
				sendWallOfFireData(clientID);
				sendGemMineOwnerData(clientID);
				sendDemoModeData(clientID);
			}
			break;
			case PACKET_SYNC_EFFECTS:
			{
				while (!packet.isEmpty()) {
					int8 effectPlayerID;
					uint8 effectID;
					uint duration;
					packet.pop(effectPlayerID);
					packet.pop(effectID);
					packet.pop(duration);
					acSpells::doEffect(effectID, effectPlayerID, duration, false);
				}
			}
			break;
			case PACKET_SYNC_SKILLS:
			{
				while (!packet.isEmpty()) {
					int8 skillPlayerID;
					packet.pop(skillPlayerID);
					packet.pop(players[skillPlayerID].spellDamageBonus);
					packet.pop(players[skillPlayerID].magicResist);
				}
			}
			break;
			case PACKET_SYNC_WALLS_OF_FIRE:
			{
				while (!packet.isEmpty()) {
					int8 wallPlayerID;
					float xOrigin;
					float yOrigin;
					int8 height;
					uint duration;
					packet.pop(wallPlayerID);
					packet.pop(xOrigin);
					packet.pop(yOrigin);
					packet.pop(height);
					packet.pop(duration);
					jjPLAYER@ caster = jjPlayers[wallPlayerID];
					Spell@ spell = cast<Spell@>(spells["I"]);
					acSpells::doWallOfFire(caster, spell, xOrigin, yOrigin, height, duration);
				}
			}
			break;
			case PACKET_SYNC_GEM_MINES:
			{
				while (!packet.isEmpty()) {
					int8 gemMineId;
					int8 ownerID;
					int lockElapsed;
					packet.pop(gemMineId);
					packet.pop(ownerID);
					packet.pop(lockElapsed);
					gemMines[gemMineId].ownerID = ownerID;
					gemMines[gemMineId].lockElapsed = lockElapsed;
				}
			}
			break;
			case PACKET_SYNC_DEMO_MODE:
			{
				packet.pop(demoModeOn);
				if (demoModeOn) ownSpells = acInit::loadSpells();
				else acUtils::disableDemoMode();
			}
			break;
		}
	}
	void sendAoePacket(int8 senderPlayerID, uint8 spellID, int8 currentCasterID) {
		jjSTREAM packet;
		packet.push(senderPlayerID);
		packet.push(uint8(PACKET_AOE));
		packet.push(spellID);
		packet.push(currentCasterID);
		jjSendPacket(packet);
	}
	
	void sendCaptureGemMinePacket(int8 playerID, int8 gemMineId) {
		jjSTREAM packet;
		packet.push(playerID);
		packet.push(uint8(PACKET_CAPTURE_GEM_MINE));
		packet.push(gemMineId);
		jjSendPacket(packet, -jjPlayers[playerID].clientID);
	}
	
	void sendChainLightningPacketToServer(int8 playerID, int8 currentCasterID, int8[] targets) {
		jjSTREAM packet;
		packet.push(playerID);
		packet.push(uint8(PACKET_CHAIN_LIGHTNING_SERVER));
		packet.push(currentCasterID);
		for (uint i = 0; i < targets.length; i++) {
			packet.push(targets[i]);
		}
		jjSendPacket(packet);
	}

	void sendChainLightningPacketToClients(int8 playerID, array<ChainLightningTarget@> chainLightningTargets) {
		jjSTREAM packet;
		packet.push(playerID);
		packet.push(uint8(PACKET_CHAIN_LIGHTNING_CLIENT));
		for (uint i = 0; i < chainLightningTargets.length; i++) {
			ChainLightningTarget@ target = chainLightningTargets[i];
			packet.push(target.id);
			packet.push(target.parentID);
			packet.push(target.playerID);
		}
		jjSendPacket(packet);
	}
	
	void sendChannelingStartedPacket(int8 senderPlayerID, uint8 spellEnumValue) {
		if (jjGameState == GAME::STOPPED) return;
		jjSTREAM packet;
		packet.push(senderPlayerID);
		packet.push(uint8(PACKET_CHANNELING_STARTED));
		packet.push(spellEnumValue);
		jjSendPacket(packet);
	}

	void sendChannelingStoppedPacket(int8 senderPlayerID) {
		jjSTREAM packet;
		packet.push(senderPlayerID);
		packet.push(uint8(PACKET_CHANNELING_STOPPED));
		jjSendPacket(packet);
	}
	
	void sendDemoModeData(int clientID) {
		jjSTREAM packet;
		packet.push(jjLocalPlayers[0].playerID);
		packet.push(uint8(PACKET_SYNC_DEMO_MODE));
		packet.push(demoModeOn);
		jjSendPacket(packet, clientID);
	}
	
	void sendEffectData(int clientID) {
		jjSTREAM packet;
		packet.push(jjLocalPlayers[0].playerID);
		packet.push(uint8(PACKET_SYNC_EFFECTS));
		for (uint i = 0; i < 32; i++) {
			jjPLAYER@ play = jjPlayers[i];
			if (play.isInGame) {
				Player@ asPlayer = players[play.playerID];
				for (uint j = 0; j < asPlayer.activeEffects.length; j++) {
					Effect@ effect = asPlayer.activeEffects[j];
					packet.push(play.playerID);
					packet.push(uint8(effect.enumValue));
					packet.push(effect.elapsed);
				}
			}
		}
		jjSendPacket(packet, clientID);
	}

	void sendEffectPacket(int8 senderPlayerID, uint8 effectID, int8 targetPlayerID, uint duration) {
		jjSTREAM packet;
		packet.push(senderPlayerID); //This is always jjLocalPlayers[0].playerID, unless it is a forwarded packet by the server
		packet.push(uint8(PACKET_EFFECT));
		packet.push(effectID);
		packet.push(targetPlayerID);
		packet.push(duration);
		jjSendPacket(packet);
	}
	
	void sendGemMineOwnerData(int clientID) {
		jjSTREAM packet;
		packet.push(int8(0));
		packet.push(uint8(PACKET_SYNC_GEM_MINES));
		for (uint i = 0; i < gemMines.length(); i++) {
			packet.push(gemMines[i].id);
			packet.push(gemMines[i].ownerID);
			packet.push(gemMines[i].lockElapsed);
		}
		jjSendPacket(packet, clientID);
	}
	
	void sendMagicMirrorAnimationPacket(int8 senderPlayerID, int8[] targets) {
		jjSTREAM packet;
		packet.push(senderPlayerID);
		packet.push(uint8(PACKET_MAGIC_MIRROR_ANIMATION));
		for (uint i = 0; i < targets.length(); i++) {
			packet.push(targets[i]);
		}
		jjSendPacket(packet);
	}
	
	void sendMassEffectPacket(int8 senderPlayerID, uint8 effectID, int8[] targets, uint duration,
			int8 currentCasterID) {
		jjSTREAM packet;
		packet.push(senderPlayerID);
		packet.push(uint8(PACKET_MASS_EFFECT));
		packet.push(effectID);
		packet.push(duration);
		packet.push(currentCasterID);
		for (uint i = 0; i < targets.length; i++) {
			packet.push(targets[i]);
		}
		jjSendPacket(packet);
	}

	void sendPlayerDeadPacket(int8 senderPlayerID) {
		jjSTREAM packet;
		packet.push(senderPlayerID);
		packet.push(uint8(PACKET_PLAYER_DEAD));
		jjSendPacket(packet);
	}

	void sendReqSyncDataPacket() {
		jjSTREAM packet;
		packet.push(jjLocalPlayers[0].playerID);
		packet.push(uint8(PACKET_REQ_SYNC_DATA));
		jjSendPacket(packet);
	}

	void sendSkillData(int clientID) {
		jjSTREAM packet;
		packet.push(jjLocalPlayers[0].playerID);
		packet.push(uint8(PACKET_SYNC_SKILLS));
		for (uint i = 0; i < 32; i++) {
			jjPLAYER@ play = jjPlayers[i];
			if (play.isInGame) {
				Player@ asPlayer = players[play.playerID];
				packet.push(play.playerID);
				packet.push(asPlayer.spellDamageBonus);
				packet.push(asPlayer.magicResist);
			}
		}
		jjSendPacket(packet, clientID);
	}
	
	void sendSkillPacket(int8 senderPlayerID, uint8 skill) {
		jjSTREAM packet;
		packet.push(senderPlayerID);
		packet.push(skill);
		jjSendPacket(packet);
	}
	
	void sendSkillLearnedPacket(int8 senderPlayerID, uint8 skill) {
		jjSTREAM packet;
		packet.push(senderPlayerID);
		packet.push(uint8(PACKET_SKILL_LEARNED));
		packet.push(skill);
		jjSendPacket(packet);
	}
	
	void sendSpellLearnedPacket(int8 senderPlayerID, string spellKey) {
		jjSTREAM packet;
		packet.push(senderPlayerID);
		packet.push(uint8(PACKET_SPELL_LEARNED));
		packet.push(spellKey);
		jjSendPacket(packet);
	}

	void sendWallOfFireData(int clientID) {
		jjSTREAM packet;
		packet.push(jjLocalPlayers[0].playerID);
		packet.push(uint8(PACKET_SYNC_WALLS_OF_FIRE));
		for (uint i = 0; i < activeWallsOfFire.length; i++) {
			WallOfFire@ wallOfFire = activeWallsOfFire[i];
			packet.push(wallOfFire.caster.playerID);
			packet.push(wallOfFire.xOrigin);
			packet.push(wallOfFire.yOrigin);
			packet.push(wallOfFire.height);
			packet.push(wallOfFire.elapsed);
		}
		jjSendPacket(packet, clientID);
	}
	
	void sendWallOfFirePacket(int8 playerID, float xOrigin, float yOrigin, int height) {
		jjSTREAM packet;
		packet.push(playerID);
		packet.push(uint8(PACKET_WALL_OF_FIRE));
		packet.push(xOrigin);
		packet.push(yOrigin);
		packet.push(height);
		jjSendPacket(packet);
	}
}