BlueGit

blue-star tribegen

Files

tribegen / web / src / Classes / Item.php

Item.php

various fixes
timfa Tim · pushed by timfa · 9 months ago
49730f61 History
Item.php 1.8 KiB Download Raw
1<?php
2
3class Item
4{
5 private $id;
6 private $saveId;
7 private $name;
8 private $description;
9 private $itemType;
10 private $statModifications = [];
11
12 public function __construct($id, $saveId, $name, $description, $itemType, $statModifications = [])
13 {
14 $this->id = $id;
15 $this->saveId = $saveId;
16 $this->name = $name;
17 $this->description = $description;
18 $this->itemType = $itemType;
19 $this->statModifications = $statModifications;
20 }
21
22 public static function fromArray($data)
23 {
24 return new self(
25 $data['id'] ?? uniqid('itm_'),
26 $data['save_id'] ?? null,
27 $data['name'] ?? '',
28 $data['description'] ?? '',
29 $data['item_type'] ?? 'generic',
30 $data['stat_modifications'] ?? []
31 );
32 }
33
34 public function toArray()
35 {
36 return [
37 'id' => $this->id,
38 'save_id' => $this->saveId,
39 'name' => $this->name,
40 'description' => $this->description,
41 'item_type' => $this->itemType,
42 'stat_modifications' => $this->statModifications
43 ];
44 }
45
46 public function getId()
47 {
48 return $this->id;
49 }
50
51 public function getName()
52 {
53 return $this->name;
54 }
55
56 public function getDescription()
57 {
58 return $this->description;
59 }
60
61 public function getItemType()
62 {
63 return $this->itemType;
64 }
65
66 public function checkStat($statName, $baseValue)
67 {
68 // Returns modified stat value
69 $modified = $baseValue;
70
71 if (isset($this->statModifications[$statName])) {
72 $modified += $this->statModifications[$statName];
73 }
74
75 return max(0, min(100, $modified));
76 }
77
78 public function getStatModifications()
79 {
80 return $this->statModifications;
81 }
82}