Storing NPC health in php variable

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Storing NPC health in php variable

Post by Epiales »

Okay...

I think I have somewhat of a battle system at least started for the arena NPC battles now. Problem I'm going to run into very soon is when I go to loop the battle scenario, I wont 'be able to use the table that holds the NPC's information in them, since EVERY player will be attacking the same NPC's. So I will need to store their hp during the battle so I can determine when they win/lose the fight...

Also...

When the player defeats the NPC, how in the world am I going to tell it that it needs to hit the next row in the database? The code for getting the information and fighting the NPC is below. Upon registering, the first NPC will be placed into the Arena Log Table, that way when they go to the arena, they will see the first NPC to attack. Then when they attack and win, I was going to delete that record from the arena logs, but didn't think of how I would go about inserting the 2nd NCP information in there. Any Ideas?

Code: Select all

<?php 

$sql = "SELECT * FROM arenalogs WHERE userid = '$_SESSION[userid]'";
$user_query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query);
$row = mysqli_fetch_array($user_query, MYSQLI_ASSOC);

$arena_id = $row['userid'];
$arena_char_name = $row['charname'];
$character = $row['charid'];

$sql = "SELECT * FROM characters where id ='$character' ORDER BY id ASC LIMIT $character"; 
$user_query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query);
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {

$playerHp = $user_health;
$enemyHp = $row['hpoints'];

//Player's Attack Turn
?>
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Storing NPC health in php variable

Post by Jackolantern »

The answer to both questions is more tables. For the first problem, you need to have a "Master NPC" table that stores the template of each NPC. When a player starts a battle with one of them, you copy the info from the master list into a battle list table as well as the player fighting them. You will then use the master npc table to populate health and other stats which can then be changed in the battle list table without altering the master table. That way players will have their own copy of the NPC to fight.

For the second, you could have a table that stores "Arena Schedules". You would likely need 4 columns: scheduleID (mostly unused and existing just for indexing purposes), playerID (to reference who the fight will belong to), battleOrder (the number representing the order in which the player will fight the NPCs, with 1 being first, 2 being the second, etc.), and npcID (the ID of the NPC's "Master NPC" table record, to be used for copying them into the battle list table). Once the player is done with a battle, remove that entry from the Arena Schedule table. That way, anytime you need the next battle, you can just

Code: Select all

SELECT * FROM arenaSchedule WHERE playerID = $playerID ORDER BY battleOrder LIMIT 1;
...to get the next battle in the schedule. This is typically called a Many-to-Many join table, since each player can have many NPC battles scheduled, and each NPC can be in the table many times, meaning that the easiest way to represent this data is in a new table that simply combines references to other table records.
The indelible lord of tl;dr
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Storing NPC health in php variable

Post by Epiales »

Jackolantern wrote:The answer to both questions is more tables. For the first problem, you need to have a "Master NPC" table that stores the template of each NPC. When a player starts a battle with one of them, you copy the info from the master list into a battle list table as well as the player fighting them. You will then use the master npc table to populate health and other stats which can then be changed in the battle list table without altering the master table. That way players will have their own copy of the NPC to fight.
Okay, I have a master NPC list table that holds all the stat information for each NPC. When the player registers, it has the very first one display in there from an NPC arena logs list. When they go to arena NPC's I use this line of code to display the first NPC:

Code: Select all

$sql = "SELECT * FROM characters where id ='$character' ORDER BY id ASC LIMIT $character"; 
The LIMIT $character comes from the arena log table that stores the 1st NPC in there, so it will only display the row that contains the users ID, NPC id and so forth.

I'm confused on your "copy the info from the master list into a battle list" I basically have the copy of the first NPC in the battle list (arena logs), but my problem is i still don't know how to place the 2nd NPC into that list once they are done with the one that they start with... the 1st NPC...

Is there a command to simply transfer ROW 2 of the master list into the arena logs where it stores the info for the player? or am I going to have to put EVERY NPC Character into this arena log list upon registration and then simply just delete the first one when done, delete 2nd one when done, and so forth. Will be a lot of rows for every single person?

In my mind, when I started it, I figured I could put the 1st one in the arena logs upon registration, and when they beat it, then put the 2nd one in there and so forth, but I didn't think how I was going to transfer each NPC from the MASTER list into the players log list. Sorry if I'm confusing this, I just can't wrap my head around it.

If I'm reading correctly, I don't need to display the results of the arenalogs upon going to the NPC's but display the MASTER NPC list FIRST and then make the entry into the arena logs?
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
MikuzA
Posts: 395
Joined: Thu Aug 08, 2013 8:57 am

Re: Storing NPC health in php variable

Post by MikuzA »

Hello,

-Never delete anything.
Instead of deletion, put an extra column in your table that you can ignore by just querying.

sort-of-offtopic:

Code: Select all

$sql = "SELECT * FROM characters where id ='$character' ORDER BY id ASC LIMIT $character";
This query doesn't make that much sense, or it does but the LIMIT doesn't. LIMIT is used for limiting the result amount, and $charachter is also matched to ID.

Let's say a table contains 10 rows.
Select * from table; >> displays all the 10 rows.
Select * from table limit 2; >> displays the first 2 rows.

If your table contains a specific id that is.. let's say 4.
'select * from characters where id=4 ORDER BY ID ASC LIMIT 4'
So you are displaying all the chars with id=4 but only the first 4 of them?


back-to-topic:
To understand the background,

You have an NPC-table that exists of enemies that 'all players can attack'?
Does that mean that multiple players can reduce the same enemy npc HP?

Or
you have an 'master' NPC-table that contains the basic information of all the NPCs and when a player initiates an NPC for the first time, an entry is inserted to arenalog where the battle will be handeld? HP removals and stuff. And it will be 'ONE player' vs 'npc' until the fight is done?

I would require to see your database architecture and for you to tell your definitions on why it should be hitting what and when in order to assist you further.

What tables do you have? What rows are being used during battle? Where do you reduce player and NPC hp's? Stuff like that.
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Storing NPC health in php variable

Post by Epiales »

MikuzA wrote:Hello,

-Never delete anything.
Instead of deletion, put an extra column in your table that you can ignore by just querying.

sort-of-offtopic:

Code: Select all

$sql = "SELECT * FROM characters where id ='$character' ORDER BY id ASC LIMIT $character";
This query doesn't make that much sense, or it does but the LIMIT doesn't. LIMIT is used for limiting the result amount, and $charachter is also matched to ID.

Let's say a table contains 10 rows.
Select * from table; >> displays all the 10 rows.
Select * from table limit 2; >> displays the first 2 rows.

If your table contains a specific id that is.. let's say 4.
'select * from characters where id=4 ORDER BY ID ASC LIMIT 4'
So you are displaying all the chars with id=4 but only the first 4 of them?


back-to-topic:
To understand the background,

You have an NPC-table that exists of enemies that 'all players can attack'?
Does that mean that multiple players can reduce the same enemy npc HP?

Or
you have an 'master' NPC-table that contains the basic information of all the NPCs and when a player initiates an NPC for the first time, an entry is inserted to arenalog where the battle will be handeld? HP removals and stuff. And it will be 'ONE player' vs 'npc' until the fight is done?

I would require to see your database architecture and for you to tell your definitions on why it should be hitting what and when in order to assist you further.

What tables do you have? What rows are being used during battle? Where do you reduce player and NPC hp's? Stuff like that.

Okay, thank you... Let me gather some detail in my mind and on paper and come back to this. I've done so much changing around it's pitiful lol... The basics of what I want is to have a list of Arena NPC characters that EACH player will have to defeat... let's say there are 20 NPC's to start with. They will attack NPC one and then the next and then the next. What I can't get my head around is how to implement this with the javascript I am using for popups. I want all of this to be done via the popups instead of pages. I've played many, many games where this is done and I like that style of setup. I am using the JS UI from http://w2ui.com/web/demos/#!popup/popup-1...

I know that it usually works that when they click attack, then it goes to an if isset or a new page and those grab the ID of the NPC they are attacking. This is how I have the members attack system setup, or will have as I'm not finished with it..... But doing it in popups is what is confusing me the most.

I have a master list of NPC's....

Table Characters:

1. id
2. name
3 hpoints
4 attack
5 defense
6 exp (player will receive)
7 location
8 money (player will receive.

I will have a secondary table arenalogs... in this table I will mimic the first table so I can use this table for depletion of hp and so forth without tampering with the main list that all players see and battle....

now upon registration I can put the first NPC into this arenalogs table so that this would be their first NPC they attack..... but this has really been messing me up with displaying the original NPC characters per user.....

not sure the best route to go here, but this is the system I have setup or trying to setup. I could do this without javascript, but I like the way js works with it being that I can get it working.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
MikuzA
Posts: 395
Joined: Thu Aug 08, 2013 8:57 am

Re: Storing NPC health in php variable

Post by MikuzA »

Ok, now I understand. Remember to think 'Database' before thinking 'JavaScript'.
It's much easier to approach when you know the data you have instead of displaying it, since displaying the data can be done in many ways. The data only needs to be ready for displaying.

As Jacko said, make another table.

[ARENAGROUPS]
ID = just a row ID. (Primary Key, Auto_Increment etc)
GROUP_ID = To have several groups of 'arena battles', like in different difficulties. (One value will have multiple rows in it)
NPC_ID = the link to the master_npc. (Foreign Key)
SEQ_ID = to determine the sequence on which order the player is fighting the enemies. (Or, you can use the ID for this, if you wish to have the order as they are inserted to the group)

[ARENALOGS]
ID = just a row ID. (Primary Key, Auto_Increment etc)
PLAYER_ID = Foreign key to Player.
NPC_ID = Foreign key to Master NPC.
GROUP_ID = Link to the GROUP_ID in ARENAGROUPS-table.
NPC_HITPOINTS = What the NPC has hitpoints left.


You could also add the Group Sequence ID to the ARENALOGS if you feel more comfortable with that.
But with the above setup you can get the data you need.

Now let's say that, when a player goes into the ARENA and decides to Attempt for a battle.
What we can do is that we..

A) Insert the whole group into the ARENALOGS. (With SEQ_ID in ARENALOGS)

B) Insert the whole group into the ARENALOGS. (Without SEQ in ARENALOGS)




A) would be probably the easiest to do, since you can just query that

Code: Select all

SELECT * FROM ARENALOGS where PLAYER_ID = $player_id AND NPC_HP <= 0 ORDER BY SEQ_ID ASC
With that you get all the enemies that you still have to defeat in order to finish the group.
just add a LIMIT 1 in the end to get the 'next enemy'.

B) Here you need to join the ARENAGROUP to the query in order to get the correct sequence. (This is not a good approach if you want to have similiar NPC enemies in one group)

Code: Select all

SELECT * FROM ARENALOGS
JOIN ARENAGROUP on ARENALOGS.GROUP_ID = ARENAGROUP.GROUP_ID
WHERE ARENALOGS.PLAYER_ID = $player_id AND NPC_HP <= 0
ORDER BY ARENAGROUP.SEQ_ID ASC
(This is an bad approach in this design, but just an example how you can tackle multiple tables)


I wanted to show you the B-example since you have a lot of data as duplicates in your database, or you are planning at least.
For example, ARENALOGS NAME, I guess that would be the name of the NPC? You can keep that in the MASTER NPC-table and just join it.

The reason for you to want to do this is because in situations that you make a 'typo' or just want to change the name of the NPC. You can just alter the MasterNPC table and it's covered everywhere. Instead of doing a lot of updates in your table.

But of course! Do it as you feel comfortable with, this is an development in progress so I'm just trying to show you some good examples before you get into these silly situtations.
Hope this helps or gives you an idea on how to proceed :)
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Storing NPC health in php variable

Post by Epiales »

WOW, that's a lot for me to plunder through :lol: :lol: Thanks!

I'm not sure what you're referring to as groups and groupID though. There are only 20 characters....now they will be eventually linked to different locations, but not currently.... But I don't think I need to put them into groups, unless I'm confused on that. Each character will be with different stats to make them harder (more hp, bigger atk, bigger def) so when the player can't beat it, they can later when they are stronger. Also Seq ID is confusing me as well, as each NPC will be ordered 1-100 or whatever. So the sequence is fight 1, then fight 2, then fight 3, then fight 4 and so on. If you don't beat 1, then you can't move to number 2.

Anyway, I'm going to read and try and break down what you typed above and see if I can make ends meet lol. Thank you for u're time spent, as I know time can be valuable. You put much thought into it :)

I also might just update this thread with everything I do step by step, as to have it all here in case I stumble into another situation.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Storing NPC health in php variable

Post by Epiales »

Okay, first I have created a table called characters. The image below, you will see the table id's as it's displayed:

Image


When you hover over the action button on the right, it displays the ID of the character in the database.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
MikuzA
Posts: 395
Joined: Thu Aug 08, 2013 8:57 am

Re: Storing NPC health in php variable

Post by MikuzA »

Ah, ok!
Sorry now I understand your Arena :)
You do not need the group_id and you might not even need another table.

Your Arena NPC already have some kind of sequence in the CHARACTERS-table so the sequence is also fine.

So what you could do here is that you do a JOIN QUERY when generating the View you have in your pop-up.

Code: Select all

SELECT * FROM CHARACTERS 
LEFT JOIN ARENALOGS ON ARENALOGS.character_id = CHARACTER.ID AND ARENALOGS.Player_ID = $player_id // A explicit join also using player_id as filter from Arenalogs just for optimization, since I feel that ARENALOGS might get big.
ORDER BY CHARACTER.ID ASC // I guess the 'sequencer' was this?
Now, the above query will display all the characters. However, when we are using 'LEFT JOIN' we also seek the values that do not have references in ARENALOGS.
So the data would be something like this (let's imagine our player has defeated 2 arena NPC's)

ID: 1 NAME: Anthony HP: 50 ARENALOGS.HP: 0
ID: 2 NAME: Carmine HP: 75 ARENALOGs.HP: 0
ID: 3 NAME: Thomas HP: 50 ARENALOGS.HP: NULL // There is no reference to ARENALOGS.HP with char_id -> char_id and player_id -> player_id. Therefore the value is NULL.
ID: 4 NAME: Felix HP: 10 ARENALOGS.HP: NULL // same here.. and so on.

So, what you could do with this information is, that IF Arenalogs.HP == 0, the Action button is disabled/changed to something else.
If it's NULL, then it's clickable.

You dig? 8-)
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Storing NPC health in php variable

Post by Epiales »

MikuzA wrote:Ah, ok!
Sorry now I understand your Arena :)
You do not need the group_id and you might not even need another table.

Your Arena NPC already have some kind of sequence in the CHARACTERS-table so the sequence is also fine.

So what you could do here is that you do a JOIN QUERY when generating the View you have in your pop-up.

Code: Select all

SELECT * FROM CHARACTERS 
LEFT JOIN ARENALOGS ON ARENALOGS.character_id = CHARACTER.ID AND ARENALOGS.Player_ID = $player_id // A explicit join also using player_id as filter from Arenalogs just for optimization, since I feel that ARENALOGS might get big.
ORDER BY CHARACTER.ID ASC // I guess the 'sequencer' was this?
Now, the above query will display all the characters. However, when we are using 'LEFT JOIN' we also seek the values that do not have references in ARENALOGS.
So the data would be something like this (let's imagine our player has defeated 2 arena NPC's)

ID: 1 NAME: Anthony HP: 50 ARENALOGS.HP: 0
ID: 2 NAME: Carmine HP: 75 ARENALOGs.HP: 0
ID: 3 NAME: Thomas HP: 50 ARENALOGS.HP: NULL // There is no reference to ARENALOGS.HP with char_id -> char_id and player_id -> player_id. Therefore the value is NULL.
ID: 4 NAME: Felix HP: 10 ARENALOGS.HP: NULL // same here.. and so on.

So, what you could do with this information is, that IF Arenalogs.HP == 0, the Action button is disabled/changed to something else.
If it's NULL, then it's clickable.

You dig? 8-)
Yes, I kind of understand a bit. I've worked with the join before... I'll tinker with this and see what I can come up with....

Now, what if I enter every character into the Arena Logs table? Just the Id, userid, Charid, Charname, HP..... so if I have 20 characters, upon registration it wold include 20 characters in the database for each user... then I cold select according to number DESC/ASC and limit 1..... this way it would show the 1st NPC when they join game.... then when they defeat it, remove the 1st NPC in Arena Logs and then it will display the 2nd Character and so forth. When they run out of NPC's and row is 0, then just put no more Characters....???

I have already written the code to input information into the database upon signup if this is a reasonable way of doing so? So although that's a lot of entries into the logs, they will always be depleting because they will be removed as players beat them.

But yeah, I will look into what you have mentioned above.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Post Reply

Return to “Advanced Help and Support”