This repository was archived by the owner on Mar 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathddice.db.php
More file actions
217 lines (171 loc) · 4.9 KB
/
Copy pathddice.db.php
File metadata and controls
217 lines (171 loc) · 4.9 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?
/*
CREATE TABLE IF NOT EXISTS `ddice` (
`key` varchar(50) NOT NULL,
`value` varchar(100) DEFAULT NULL,
`type` varchar(100) NOT NULL,
`reg_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`rolled_date` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`key`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- 테이블의 덤프 데이터 `ddice`
--
INSERT INTO `ddice` (`key`, `value`, `type`, `reg_date`, `rolled_date`) VALUES
('123', '111', '555', '2013-09-01 01:15:53', '2013-09-02 01:15:53'),
('1378053745_242', '4_3_4_1', '6_6_6_6', '2013-09-02 01:51:47', '2013-09-02 01:51:47');
*/
// require fix for use
$ddice_db_id = "database_id";
$ddice_db_password = "database_password";
$ddice_db_database = "database_dbname";
// require fix for use
$ddice_conn = mysql_connect('localhost', $ddice_db_id, $ddice_db_password);
mysql_select_db ($ddice_db_database, $ddice_conn);
define("UNREGISTERED", "unregistered");
define("REGISTERED", "registered");
define("ROLLED", "rolled");
class Dice {
public $type;
public $value;
public function roll() {
if ($this -> value) {
throw new Exception("unsupported operation");
}
$this -> value = rand(1, $this -> type);
}
public function create($type, $value = NULL) {
$dice = new Dice();
$dice -> type = intval($type);
if ($value) {
$dice -> value = intval($value);
}
return $dice;
}
}
class DDice {
public $state = UNREGISTERED;
public $key;
public $dices = array();
public function forceToRoll($type_arr) {
if ($this -> state == UNREGISTERED) {
$this -> register($type_arr);
$this -> roll();
} else if($this -> state == REGISTERED){
$this -> roll();
}
}
public function register($type_arr) {
if ($this -> state != UNREGISTERED) {
throw new Exception("unsupported operation, state is : " . $this -> state);
}
for($i = 0; $i < count($type_arr); $i++) {
$this -> dices[] = Dice::create($type_arr[$i]);
}
$this -> nextState();
$to_insert_key = $this -> key;
$generatedType = $this -> generateType();
mysql_query("INSERT INTO `ddice` (`key`, `type`, `reg_date`) VALUES ('$to_insert_key', '$generatedType', NOW())");
}
private function nextState() {
switch ($this -> state) {
case UNREGISTERED:
$this -> state = REGISTERED;
break;
case REGISTERED:
$this -> state = ROLLED;
break;
default:
break;
}
}
private function generateType() {
$type_arr = array();
for($i = 0; $i < count($this -> dices); $i++) {
$dice = $this -> dices[$i];
$type_arr[] = $dice -> type;
}
return implode("_", $type_arr);
}
private function generateValue() {
$value_arr = array();
for($i = 0; $i < count($this -> dices); $i++) {
$dice = $this -> dices[$i];
$value_arr[] = $dice -> value;
}
return implode("_", $value_arr);
}
public function roll() {
if ($this -> state != REGISTERED) {
throw new Exception("unsupported operation, state is : " . $this -> state);
}
for($i = 0; $i < count($this -> dices); $i++) {
$dice = $this -> dices[$i];
$dice -> roll();
}
$this -> nextState();
$to_update_key = $this -> key;
$generatedValue = $this -> generateValue();
mysql_query("UPDATE `ddice` set `value` = '$generatedValue', `rolled_date` = NOW() WHERE `key` = '$to_update_key'");
}
private function createInstance($key, $ret_obj = NULL) {
$ddice = new DDice();
$ddice -> key = $key;
$ddice -> state = UNREGISTERED;
if (!$ret_obj) {
return $ddice;
}
if (isset($ret_obj ->value)) {
$ddice -> state = ROLLED;
} else {
$ddice -> state = REGISTERED;
}
$ddice -> dices = DDice::parse($ret_obj -> type, $ret_obj -> value);
return $ddice;
}
public function get($key) {
$result = mysql_query("SELECT `key`, `type`, `value` FROM `ddice` WHERE `key` = '$key'");
if ( mysql_affected_rows() != 1) {
return DDice::createInstance($key);
}
$ret_obj = mysql_fetch_object($result);
return DDice::createInstance($key, $ret_obj);
}
public function getList($key_arr) {
$key_arr_count = count($key_arr);
if ($key_arr_count < 1) {
return array();
}
for($i = 0; $i < $key_arr_count; $i++) {
$key_arr[$i] = "'$key_arr[$i]'";
}
$keys = implode(",", $key_arr);
$result = mysql_query("SELECT `key`, `type`, `value` FROM `ddice` WHERE `key` in ($keys)");
if (!$result) {
return array();
}
$ret_arr = array();
$size = mysql_affected_rows();
for ($i = 0; $i < $size; $i++) {
$ret_obj = mysql_fetch_object($result);
$ret_arr[] = DDice::createInstance($ret_obj -> key, $ret_obj);
}
return $ret_arr;
}
private function parse($type, $value) {
$type_arr = explode("_", $type);
if (isset($value) && $value) {
$value_arr = explode("_", $value);
$size = min(count($type_arr), count($value_arr));
} else {
$value_arr = array();
$size = count($type_arr);
}
$dice_arr = array();
for($i = 0; $i < $size; $i++) {
$dice_arr[] = Dice::create($type_arr[$i], $value_arr[$i]);
}
return $dice_arr;
}
}
?>