webgame /lib/abilitylib.php

Language PHP Lines 197
MD5 Hash 8255cbc6c48c4873eb6cd09f4cee2728 Estimated Cost $3,608 (why?)
Repository https://bitbucket.org/mikel3377/webgame.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php

include('validation.php');

    /*Ability apply functions. These get called when $ability->Apply() is called
    Each one has identical arguments:
        $ability is the ability that's being used. It has Params, which is some array of the ability parameters
        $game is a gamedata object, or something
        $unitrow is an array of unit data (retrived by mysql_fetch_array off of a mysql_query)
        $newParams is whatever the Apply function needs to make the ability happend (e.g. for moving, it should have new coords)
        $updateList should be modified to include whatever new updates need to be stored when the ability is used
    */
    function lifeauraApply($ability, $game, $unitrow, $newParams, &$updateList)
    {
        $range = $ability->Params[0];
        $power = $ability->Params[1];
        if ($power >= 0){ // healing aura on own units
            $newUnits = GetMyUnits($unitrow['ownerid'], $game->ID);
        } else { // damage aura on enemies
            $newUnits = GetTheirUnits($unitrow['ownerid'], $game->ID);
        }
        $affected = AllCellsInRange($game,$unitrow['x'],$unitrow['y'],$range,TRUE);
        while ($row = mysql_fetch_array($newUnits)){
            if ( isset( $affected[$row['x']][$row['y']] ) ){
                DamageApply(-1*$power, 0, $row['hp'], $unitrow['id'], $row['id'], $row['maxhp'], $game->ID, $updateList);
            }
        }
        return;
    }
    
    function moveApply($ability, $game, $unitrow, $newParams, &$updateList)
    {
        $x = $newParams['x'];
        $y = $newParams['y'];
        $unitid = $unitrow['id'];
        $user = Application::GetCurrentUser();
        $gameid = $game->ID;
        $userid = $user->ID;

        if (! ValidateMove($userid, $unitrow, $game, $x, $y)){
            die(json_encode( array( 'status' => 'Move validation failed') ));
            return; 
        }

        $query = "UPDATE units SET x=$x, y=$y, moves=0 WHERE id=$unitid";
        mysql_query($query) or die(json_encode( array( 'status' => mysql_error())));

        $query = "INSERT INTO gameupdates (gameid, type, param1, param2, param3) VALUES ($gameid, 1, $unitid, $x, $y)";
        mysql_query( $query ) or die( array( 'status' => mysql_error()) );

        $row=array('type' => 1, 'gameid' => $game->ID, 'id' => 1, 'param1' => $unitid, 'param2' => $x, 'param3' => $y); //NOTE THE UPDATE ID IS ALWAYS 1 HERE!!!!
        $updateList[] = GetUpdateObject($row);
        return; 
        
    }
    
    function attackApply($ability, $game, $unitrow, $newParams, &$updateList){
        $targx = $newParams['x'];
        $targy = $newParams['y'];
        $gameid = $game->ID;
        $targ = mysql_fetch_array(GetUnitByLoc($targx, $targy, $gameid));
        $user = Application::GetCurrentUser();
        $userid = $user->ID;
        $targid = $targ['id'];
        $unitid = $unitrow['id'];
        
        $s = GameDataService::GetInstance();
        $map = $s->GetGameMap( $game );
        
        if (! ValidateAttack($userid, $unitrow, $gameid, $targ)){
            die(json_encode( array( 'status' => 'Move validation failed') ));
            return;
        }
        
        $at = UnitConfiguration::GetByID( (int)$unitrow['type']  );
        $tt = UnitConfiguration::GetByID( (int)$targ['type']  );
        $t = $map->Cells[$unitrow['x']][$unitrow['y']]->Type;
        $tile = TileConfiguration::GetByID( $t );
        
        $defense = $tt->Defense;
        $attack = $at->AttackPower;
        
        $defense = $defense + $tile->DefenseBonus;

        DamageApply($attack, $defense, (int)$targ['hp'], $unitid, $targid, $targ['maxhp'], $gameid, $updateList);
    }
    
    function spawnunitApply($ability, $game, $unitrow, $newParams, &$updateList){
        $unitid = $unitrow['id'];
        $ownerid = $unitrow['ownerid'];
        $type = $newParams['param1'];
        $tarx = $newParams['x'];
        $tary = $newParams['y'];
        $user = Application::GetCurrentUser();
        $userid = $user->ID;
        $gameid = $game->ID;
        $t = UnitConfiguration::GetByID( $type );
        
        $moneyrow = mysql_fetch_array(GetMyMoney($userid, $gameid));
        $money = $moneyrow['resources'];
        
        if( $money < $t->ResourceCost)
        {
            echo json_encode( array( 'status' => "Not enough resources") );
            return;
        }
        //TODO: move this to validation and do more validation
        
        $money = $money - $t->ResourceCost;
        $query = "UPDATE usergames SET resources = $money WHERE gameid = $gameid AND userid = $userid";
        mysql_query( $query ) or die( array( 'status' => mysql_error()) );
        
        $query = "UPDATE units SET actionused = TRUE WHERE id = $unitid";
        mysql_query( $query ) or die( array( 'status' => mysql_error()) );
        
        $id = MySql::CreateUnit( $gameid, $ownerid, $tarx, $tary, $type );
        
        $query = "UPDATE units SET actionused = TRUE, moves = 0 WHERE id = $id";
        mysql_query( $query );
        
        $query = "INSERT INTO gameupdates (gameid, type, param1 ) VALUES ($gameid, 4, $id )";
        mysql_query($query) or die( array( 'status' => mysql_error()) );
        
        $row=array('type' => 4, 'gameid' => $gameid, 'id' => 1, 'param1' => $id); //NOTE THE UPDATE ID IS ALWAYS 1 HERE!!!!
        $updateList[] = GetUpdateObject($row);
    }
    
    //a few database utility functions - maybe these should go somewhere else like dbconn?

    function GetAllUnits($gameid){
        $query = "SELECT * FROM units WHERE gameid=$gameid";
        $result = mysql_query( $query ) or die( array( 'status' => mysql_error()) );
        return $result;
    }
    
    function GetMyUnits($userid, $gameid, $dead = FALSE){
        if ($dead){
            $query = "SELECT * FROM units WHERE ownerid=$userid AND gameid=$gameid";
        } else {
            $query = "SELECT * FROM units WHERE ownerid=$userid AND gameid=$gameid AND hp > 0";
        }
        $result = mysql_query( $query ) or die( array( 'status' => mysql_error()) );
        return $result;
    }
    
    function GetTheirUnits($userid, $gameid, $dead = FALSE){
        if ($dead){
            $query = "SELECT * FROM units WHERE ownerid != $userid AND gameid=$gameid";
        } else {
            $query = "SELECT * FROM units WHERE ownerid != $userid AND gameid=$gameid AND hp > 0";
        }
        $result = mysql_query( $query ) or die( array( 'status' => mysql_error()) );
        return $result; 
    }
    
    function GetThisUnit($unitid){
        $query = "SELECT * FROM units WHERE id = $unitid";
        $result = mysql_query( $query ) or die( array( 'status' => mysql_error()) );
        return $result;
    }
    
    function GetUnitByLoc($x, $y, $gameid){
        $query = "SELECT * FROM units WHERE x = $x AND y = $y AND gameid = $gameid";
        $result = mysql_query( $query ) or die( array( 'status' => mysql_error()) );
        return $result;
    }
    
    function GetMyMoney($userid, $gameid){
        $query = "SELECT resources FROM usergames WHERE gameid = $gameid AND userid = $userid";
        $result = mysql_query( $query ) or die( array( 'status' => mysql_error()) );
        return $result;
    }
    
    
    //a few other utility functions
    function DamageApply($attack, $defense, $curhp, $unitid, $targid, $maxhp, $gameid, &$updateList){
        if ($curhp > 0){
            $damage = $attack-$defense;
            if ($attack > 0) {
                $damage = max($damage,0);
            }
            $newhp = max( min( $maxhp, $curhp - $damage ), 0 );

            $query = "UPDATE units AS Targ, units AS Att
                        SET Targ.hp = $newhp, Att.actionused = TRUE, Att.moves = 0
                        WHERE Att.id = $unitid AND Targ.id = $targid";
            mysql_query( $query ) or die( json_encode( array( 'status' => mysql_error()) ) );

            $query = "INSERT INTO gameupdates (gameid, type, param1, param2, param3) VALUES ($gameid, 2, $unitid, $targid, $damage)";
            mysql_query( $query ) or die( array( 'status' => mysql_error()) );

            $row=array('type' => 2, 'gameid' => $gameid, 'id' => 1, 'param1' => $unitid, 'param2' => $targid, 'param3' => $damage); //NOTE THE UPDATE ID IS ALWAYS 1 HERE!!!!
            $updateList[] = GetUpdateObject($row);
        }
    }
?>
Back to Top