-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass.user.php
172 lines (142 loc) · 5.57 KB
/
class.user.php
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
<?php
class User {
// volatile variables that do not get permanently recorded
public $state, $authenticated = false, $messages = [], $room;
// non-volatile variables that get saved to user record
protected $meta, $userfile;
public function performAction(Command $command) {
return $this->state->perform($command);
}
public static function load($name) {
$file = "users/" . User::pathify($name) . ".json";
if( file_exists($file) ) {
$instance = new self();
$instance->meta = json_decode(file_get_contents($file), true);
$instance->userfile = $file;
$instance->state = new StandingState();
return $instance;
}
else {
throw new Exception('User does not exist');
}
}
public function setRoom(Room $room) {
$this->room = $room;
}
public function save() {
return file_put_contents($this->userfile, json_encode($this->meta, JSON_PRETTY_PRINT));
}
public function authenticate($password) {
if( password_verify($password, $this->meta['password']) ) {
$this->authenticated = true;
return true;
}
else {
return false;
}
}
public static function create($name, $class, $race, $attributes, $password, $email_address, $starting_room) {
$instance = new self();
$file = "users/" . User::pathify($name) . ".json";
$instance->meta['name'] = $name;
$instance->meta['class'] = 'cleric';
$instance->meta['race'] = 'human';
$instance->meta['attributes'] = $attributes;
$instance->meta['skill_points'] = 0;
$instance->meta['health'] = 100; // 0-100 (but modifiers could take above 100)
$instance->meta['total_health'] = 100;
$instance->meta['mana'] = 0;
$instance->meta['total_mana'] = 0;
$instance->meta['movement'] = 25;
$instance->meta['total_movement'] = 25;
$instance->meta['hunger'] = 0; // 0-5 with 0 no hunger, 3 hungry, 5 starving
$instance->meta['thirst'] = 0; // 0-5 with 0 no thirst, 3 parched, 5 dehydrated
$instance->meta['temperature'] = 96.7; // in farenheit
$instance->meta['alignment'] = 0;
$instance->meta['wearables'] = [];
$instance->meta['room'] = $starting_room;
$instance->meta['wallet'] = 100; // starting coins
$instance->meta['carrying'] = [];
$instance->meta['weight'] = 175; // in pounds
$instance->meta['o2'] = 99; // blood's oxygen level
$instance->meta['password'] = password_hash($password, PASSWORD_DEFAULT);
$instance->meta['email'] = $email_address;
$instance->meta['created'] = time();
$instance->meta['last_login'] = time();
$instance->state = new StandingState();
$instance->userfile = $file;
$instance->save();
return $instance;
}
public function name() { return $this->meta['name']; }
public function class() { return $this->meta['class']; }
public function race() { return $this->meta['race']; }
public function base() { return $this->meta['attributes']; }
public function room() { return $this->meta['room']; }
public function prompt() {
return "<H:{$this->meta['health']}/{$this->meta['total_health']} "
. "M:{$this->meta['mana']}/{$this->meta['total_mana']} "
. "MV:{$this->meta['movement']}/{$this->meta['total_movement']} "
. "G:{$this->meta['wallet']}> ";
}
private static function pathify($file) {
/** https://stackoverflow.com/a/2021729/4508285 */
// Remove anything which isn't a word, whitespace, number
// or any of the following caracters -_~,;[]().
// If you don't need to handle multi-byte characters
// you can use preg_replace rather than mb_ereg_replace
// Thanks @Łukasz Rysiak!
$file = mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', strtolower($file));
// Remove any runs of periods (thanks falstro!)
return mb_ereg_replace("([\.]{2,})", '', $file);
}
}
class UserStates {
public function perform(Command $command) { }
}
class StandingState extends UserStates {
public function perform(Command $command) {
parent::perform($command);
switch(true) {
case $command instanceof LookCommand:
case $command instanceof MoveCommand:
return $command->perform();
break;
default:
return "You cannot perform that action while standing\r\n";
}
}
}
class RestingState extends UserStates {
public function perform(Command $command) {
parent::perform($command);
switch(true) {
case $command instanceof LookCommand:
return $command->perform();
break;
default:
return "You cannot perform that action while resting\r\n";
}
}
}
class SleepingState extends UserStates {
public function perform(Command $command) {
parent::perform($command);
switch(true) {
default:
return "You cannot perform that action while sleeping\r\n";
}
}
}
class IncapacitatedState extends UserStates {
public function perform(Command $command) {
parent::perform($command);
switch(true) {
case $command instanceof LookCommand:
$command->perform();
break;
default:
return "You cannot perform that action while incapacitated\r\n";
}
}
}