forked from andz138/Team4_AnimalSim_Repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_data_handler.php
48 lines (40 loc) · 1.33 KB
/
user_data_handler.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
<?php
function updateUserFile($userId, $happiness, $hunger, $energy, $score) {
$filename = 'user_data.txt';
$userExists = false;
if (file_exists($filename)) {
$lines = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
$parts = explode(",", $line);
if ($parts[0] == $userId) {
$tempLines[] = "$userId,$happiness,$hunger,$energy,$score";
$userExists = true;
} else {
$tempLines[] = $line;
}
}
}
if (!$userExists) {
$tempLines[] = "$userId,$happiness,$hunger,$energy,$score";
}
file_put_contents($filename, implode("\n", $tempLines));
}
function getUserData($userId) {
$filename = 'user_data.txt';
if (file_exists($filename)) {
$lines = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
$parts = explode(",", $line);
if ($parts[0] == $userId) {
return [
'happiness' => intval($parts[1]),
'hunger' => intval($parts[2]),
'energy' => intval($parts[3]),
'score' => intval($parts[4])
];
}
}
}
return null; //incase no user match
}
?>