BlueGit

blue-star tribegen

Files

tribegen / web / src / Classes / Item.php

Item.php

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