1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

rpg market pricing class

Discussion in 'PHP' started by Jeremy Benson, Mar 24, 2017.

  1. #1
    Hello,

    I'm working on a game I've been developing the past year. I'm getting some materials in the db for alchemy ingredients. The market pricer is giving me haywire prices. When I price a combat mod, like +1 combat, it gives a good low price, but perception mod takes the price into thousands.

    You'll see I've got everything in one file for testing. Trying to add to the price based on how many mods there are, and how many elemental resistances. If price is zero rate by rarity, or armour rating bonus.

    Thanks for reading.

    <?php
    
    include('php/classes/RandomHelper.php');
    
    class MarketHelper{
     
       // price alchemy modded item
     
       function price_alchemy_mod($stat, $mod, $marketPrice)
       {
       
     
           switch($mod)
           {
           
             case 0:
               $marketPrice += 0;
               echo $stat . ' : ' . $mod . '<br>';
             break;
             case 1:
               $marketPrice += mt_rand(35, 78);
               echo $stat . ' : ' . $mod . '<br>';
             break;
             case 2:
               $marketPrice += mt_rand(100, 212);
               echo $stat . ' : ' . $mod . '<br>';
             break;
             case 3:
               $marketPrice += mt_rand(300, 1000);
               echo $stat . ' : ' . $mod . '<br>';
             break;
             case 4:
               $marketPrice += mt_rand(1200, 5000);
               echo $stat . ' : ' . $mod . '<br>';
             break;
             case 5:
               $marketPrice += mt_rand(6000, 10000);
               echo $stat . ' : ' . $mod . '<br>';
             break;       
             case 6:
               $marketPrice += mt_rand(30000, 100000);
               echo $stat . ' : ' . $mod . '<br>';
             break;
           
           
           }
       
         if($mod >= 6)
         {
         
           $marketPrice += mt_rand(500000, 5000000);
           echo 'In above 6: ' .  '<br>';
         
         }
       
         if($marketPrice > 0)
         {
           while($marketPrice % 2 != 0)
           {
             
             $marketPrice++;
             echo 'modding market price to even: ' . $marketPrice . '<br>';
           
           }
         }
       
         return $marketPrice;
     
       }
     
       // price resistance on alchemy item
     
     
    function price_alchemy_resistance($percent, $marketPrice)
       {
         $addedPrice = 0;
        
         switch($percent)
         {
          
           case 0:
             $addedPrice += 0;
             return $addedPrice;
           break;
           case 0.05:
             $addedPrice += 100;
             return $addedPrice;
           break;
           case 0.1:
             $addedPrice += 275;
             return $addedPrice;
           break;
           case 0.15:
             $addedPrice += 1000;
             return $addedPrice;
           break;
           case 0.20:
             $addedPrice += 2000;
             return $addedPrice;
           break;
           case 0.25:
             $addedPrice += 3000;;
             return $addedPrice;
           break;
          
         }
        
         echo 'Added Price: ' . $addedPrice . '<br>';
      
       }
       function price_rarity($rarity, $marketPrice)
       {
       
         
                 switch($rarity)
                 {
                 
                   case 'common':
                     $marketPrice += mt_rand(5, 10);
                     while($marketPrice % 2 != 0)
                     {
                     
                       $marketPrice++;
                     
                     }
                   break;
                   case 'uncommon':
                     $marketPrice += mt_rand(50, 100);
                     while($marketPrice % 2 != 0)
                     {
                     
                       $marketPrice++;
                     
                     }
                   break;
                   case 'unique':
                     $marketPrice += mt_rand(200, 400);
                     while($marketPrice % 2 != 0)
                     {
                     
                       $marketPrice++;
                     
                     }
                   break;
                   case 'rare':
                     $marketPrice += mt_rand(500, 2000);
                     while($marketPrice % 2 != 0)
                     {
                     
                       $marketPrice++;
                     
                     }
                   break;
                   case 'extremely rare':
                     $marketPrice += mt_rand(2000, 5000);
                     while($marketPrice % 2 != 0)
                     {
                     
                       $marketPrice++;
                     
                     }
                   break;
                 
                 }
           
             return $marketPrice;
       
       }
     
     
       function price_armour_rating($rating, $marketPrice)
       {
       
       
         switch($rating)
         {
       
           case 0:
             return $marketPrice += 0;
           case 10:
             return $marketPrice += 15;
           break;
           case 20:
             return $marketPrice += 50;
           break;
           case 30:
             return $marketPrice += 250;
           break;
           case 40:
             return $marketPrice += 1000;
           break;
           case 50:
             return $marketPrice += 1250;
           break;
         
         }
       
       
       }
    }
    
    
    //
    
    // Class End Test Start
    
    
    //
    
    
    
                            $marketHelper = new MarketHelper;
                             $randomHelper = new RandomHelper;
                         
                             mt_srand($randomHelper->make_seed());
    
                             $marketPrice = 0;
                         
                             // Set values
                         
                         
                             $_POST['combat'] = 0;
                             $_POST['magic'] = 0;
                             $_POST['vitality'] = 0;
                             $_POST['perception'] = 1;
                             $_POST['strength'] = 0;
                             $_POST['dexterity'] = 0;
                              $_POST['intelligence'] = 0;
                            $_POST['creativity'] = 0;
                            $_POST['charisma'] = 0;
                        
                             $_POST['holyResistance'] = 0;
                            $_POST['fireResistance'] = 0;
                            $_POST['waterResistance'] = 0;
                             $_POST['iceResistance'] = 0;
                             $_POST['earthResistance'] = 0;
                             $_POST['airResistance'] = 0;
                             $_POST['lightningResistance'] = 0;
                             $_POST['darkResistance'] = 0;
                         
                             $_POST['rarity'] = 'common';
                             $_POST['armourRating'] = 0;
                         
                            // Price all stat mods.
                         
                             $marketPrice += $marketHelper->price_alchemy_mod('combat',$_POST['combat'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_mod('magic',$_POST['magic'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_mod('vitality',$_POST['vitality'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_mod('perception',$_POST['perception'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_mod('strength',$_POST['strength'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_mod('dexterity',$_POST['dexterity'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_mod('intelligence',$_POST['intelligence'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_mod('creativity',$_POST['creativity'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_mod('charisma',$_POST['charisma'], $marketPrice);
                         
                        
                             $marketPrice += $marketHelper->price_alchemy_resistance($_POST['holyResistance'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_resistance($_POST['fireResistance'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_resistance($_POST['waterResistance'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_resistance($_POST['iceResistance'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_resistance($_POST['earthResistance'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_resistance($_POST['airResistance'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_resistance($_POST['lightningResistance'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_resistance($_POST['darkResistance'], $marketPrice);
                        
                
                             // If no mods: Price skill level as base.
                         
                             if($marketPrice <= 0)
                             {
                             
                                 $marketPrice += $marketHelper->price_rarity($_POST['rarity'], $marketPrice);
                                 $marketPrice += $marketHelper->price_armour_rating($_POST['armourRating'], $marketPrice);
                             
                             }
    
                             var_dump($marketPrice);
                         
    
    ?>
    Code (markup):
    Output
    combat : 0
    magic : 0
    vitality : 0
    perception : 1
    modding market price to even: 50
    strength : 0
    dexterity : 0
    intelligence : 0
    creativity : 0
    charisma : 0
    int(1600)

    Output is unexpected because there are no elemental resistance values it should be just 1 stat mod for the item I'm adding. Price should be less than 100gp because of this:

    case 1:
                        $marketPrice += mt_rand(35, 78);
                        echo $stat . ' : ' . $mod . '<br>';
                    break;
    Code (markup):
     
    Last edited: Mar 24, 2017
    Jeremy Benson, Mar 24, 2017 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    I think the problem MIGHT be in the fact you're echoing out results for each case inside the alchemy_resistance()-function. Try assigning that to a variable, say $result, and echo that out after the switch is done instead. It's also very hard to read into that much code - you should have minimized the code you posted (one working function, one not working, and provided some sample data, with expected results, and actual results - that way it would be a lot easier to help).
     
    PoPSiCLe, Mar 24, 2017 IP
  3. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #3
    Sorry about that. I edited it out a bunch of unused functions. Added some output, and expected results. Also edited the code a little.
     
    Jeremy Benson, Mar 24, 2017 IP
  4. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #4
    I've narrowed the bug, but don't know what's causing it. I'm literally down to the code below, from commenting out everything else.

    $marketHelper = new MarketHelper;
                             $randomHelper = new RandomHelper;
                          
                             mt_srand($randomHelper->make_seed());
    
                             $marketPrice = 0;
                          
                             // Set values
                          
                          
                             $_POST['combat'] = 0;
                             $_POST['magic'] = 0;
                             $_POST['vitality'] = 0;
                             $_POST['perception'] = 1;
                             $_POST['strength'] = 0;
                             $_POST['dexterity'] = 0;
                              $_POST['intelligence'] = 0;
                            $_POST['creativity'] = 0;
                            $_POST['charisma'] = 0;
                         
                             $_POST['holyResistance'] = 0;
                            $_POST['fireResistance'] = 0;
                            $_POST['waterResistance'] = 0;
                             $_POST['iceResistance'] = 0;
                             $_POST['earthResistance'] = 0;
                             $_POST['airResistance'] = 0;
                             $_POST['lightningResistance'] = 0;
                             $_POST['darkResistance'] = 0;
                          
                             $_POST['rarity'] = 'common';
                             $_POST['armourRating'] = 0;
                          
                            // Price all stat mods.
                          
                             $marketPrice += $marketHelper->price_alchemy_mod('combat',$_POST['combat'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_mod('magic',$_POST['magic'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_mod('vitality',$_POST['vitality'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_mod('perception',$_POST['perception'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_mod('strength',$_POST['strength'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_mod('dexterity',$_POST['dexterity'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_mod('intelligence',$_POST['intelligence'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_mod('creativity',$_POST['creativity'], $marketPrice);
                             $marketPrice += $marketHelper->price_alchemy_mod('charisma',$_POST['charisma'], $marketPrice);
                          
                              var_dump($marketPrice);
    
    
    // Through the following function
    
    function price_alchemy_mod($stat, $mod, $marketPrice)
        {
         
     
                switch($mod)
                {
                 
                    case 0:
                        $marketPrice += 0;
                        echo $stat . ' : ' . $mod . '<br>';
                    break;
                    case 1:
                        $marketPrice += mt_rand(35, 78);
                        echo $stat . ' : ' . $mod . '<br>';
                    break;
                    case 2:
                        $marketPrice += mt_rand(100, 212);
                        echo $stat . ' : ' . $mod . '<br>';
                    break;
                    case 3:
                        $marketPrice += mt_rand(300, 1000);
                        echo $stat . ' : ' . $mod . '<br>';
                    break;
                    case 4:
                        $marketPrice += mt_rand(1200, 5000);
                        echo $stat . ' : ' . $mod . '<br>';
                    break;
                    case 5:
                        $marketPrice += mt_rand(6000, 10000);
                        echo $stat . ' : ' . $mod . '<br>';
                    break;             
                    case 6:
                        $marketPrice += mt_rand(30000, 100000);
                        echo $stat . ' : ' . $mod . '<br>';
                    break;
                 
                 
                }
         
            if($mod >= 6)
            {
             
                $marketPrice += mt_rand(500000, 5000000);
                echo 'In above 6: ' .  '<br>';
             
            }
         
            if($marketPrice > 0)
            {
                while($marketPrice % 2 != 0)
                {
                     
                    $marketPrice++;
                    echo 'modding market price to even: ' . $marketPrice . '<br>';
                 
                }
            }
         
            return $marketPrice;
     
        }
    Code (markup):

    combat : 0
    magic : 0
    vitality : 0
    perception : 1
    modding market price to even: 44
    strength : 0
    dexterity : 0
    intelligence : 0
    creativity : 0
    charisma : 0
    int(1408)

    Edit: marketPrice is being added to every time price_alchemy_mod() is called, but not sure why.
     
    Last edited: Mar 24, 2017
    Jeremy Benson, Mar 24, 2017 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    Because you're using += and you're not resetting the $marketPrice after you output. Ie - you have marketPrice which you add to the next one, and the next one and so on.

    Change these:
    
    $marketPrice += $marketHelper->price_alchemy_mod('combat',$_POST['combat'], $marketPrice);
    $marketPrice += $marketHelper->price_alchemy_mod('magic',$_POST['magic'], $marketPrice);
    $marketPrice += $marketHelper->price_alchemy_mod('vitality',$_POST['vitality'], $marketPrice);
    $marketPrice += $marketHelper->price_alchemy_mod('perception',$_POST['perception'], $marketPrice);
    $marketPrice += $marketHelper->price_alchemy_mod('strength',$_POST['strength'], $marketPrice);
    $marketPrice += $marketHelper->price_alchemy_mod('dexterity',$_POST['dexterity'], $marketPrice);
    $marketPrice += $marketHelper->price_alchemy_mod('intelligence',$_POST['intelligence'], $marketPrice);
    $marketPrice += $marketHelper->price_alchemy_mod('creativity',$_POST['creativity'], $marketPrice);
    $marketPrice += $marketHelper->price_alchemy_mod('charisma',$_POST['charisma'], $marketPrice);
    
    Code (markup):
    to this:
    
    $marketPrice = $marketHelper->price_alchemy_mod('combat',$_POST['combat'], $marketPrice);
    $marketPrice = $marketHelper->price_alchemy_mod('magic',$_POST['magic'], $marketPrice);
    $marketPrice = $marketHelper->price_alchemy_mod('vitality',$_POST['vitality'], $marketPrice);
    $marketPrice = $marketHelper->price_alchemy_mod('perception',$_POST['perception'], $marketPrice);
    $marketPrice = $marketHelper->price_alchemy_mod('strength',$_POST['strength'], $marketPrice);
    $marketPrice = $marketHelper->price_alchemy_mod('dexterity',$_POST['dexterity'], $marketPrice);
    $marketPrice = $marketHelper->price_alchemy_mod('intelligence',$_POST['intelligence'], $marketPrice);
    $marketPrice = $marketHelper->price_alchemy_mod('creativity',$_POST['creativity'], $marketPrice);
    $marketPrice = $marketHelper->price_alchemy_mod('charisma',$_POST['charisma'], $marketPrice);
    
    Code (markup):
    Which will return the same value as "modding to even" - not entirely sure that's what you want, if it is, good, if not, you'll have to find another way to combine values into one.
     
    PoPSiCLe, Mar 24, 2017 IP