-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.php
149 lines (128 loc) · 4.5 KB
/
Main.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
<?
/**
* Fight Your Friends!
*
* @author Thomas Steinke (http://github.com/Epiphane)
*
* This is under the WTFPL License (http://www.wtfpl.net/). Mutilate it,
* extend it, whatever, but if you come across something fun or neat
* I'd love to hear more about it and maybe add it to my own version.
* Contact me at exyphnos@gmail.com :)
*
* This is a cool little game to fight your friends on Slack or other (coming soon™)
* chat clients.
*
* You can either include this file (fight.php) and use it, or include slack.php
* and it will handle the re-parameterizing for Slack
*/
/*
* COUPLE NOTES:
*
* 1) If you have a custom __autoload function, comment out the line that
* includes autoload.php
* 2) Make sure you create a config.php file that defines:
* - FIGHT_DB_HOST
* - FIGHT_DB_USER
* - FIGHT_DB_PASS
* - FIGHT_DB_NAME
* - NO_AUTOLOAD (if you don't want to include this version of autoload)
*
*/
namespace Fight;
require_once __DIR__ . "/config.php";
if (!defined(NO_AUTOLOAD)) {
require_once __DIR__ . "/autoload.php";
}
use Fight\Model\FightUserModel;
use Fight\Controller\FightController;
use Fight\Controller\FightUserController;
use Fight\Attachment\FightErrorMessage;
use Fight\Attachment\FightMessage;
use Fight\Attachment\FightData;
class Main
{
/**
* Respond to command (from API, SlackWrapper, whatever)
*
* @param method - The method to call (fight, status, equip, etc)
* @param params - Additional parameters
* Ex [
* "text" => "<@USLACKBOT>,
* "user_id" => "U0B2QPTNU",
* "team_id" => "T0B2LSLP6",
* "channel_id" => "C0CS03RK4",
* "user_name" => "thomassteinke" ** Optional
* ]
* @return array - [
* status => HTTP status,
* data => array of FightAttachments (could be FightErrorAttachment)
* ]
*/
public static function main($method, $params) {
$user = FightUserController::findUser($params["team_id"], $params["user_id"]);
if ($params["user_name"] && $params["user_name"] !== $user->alias->slack_name) {
$user->alias->update(["slack_name" => $params["user_name"]]);
}
// Make sure the method is fine
if (!self::isMethod($method)) {
return self::packageData(400, [
new FightErrorMessage("Command `" . $method . "` isn't available")
]);
}
try {
$argv = explode(" ", $method . " " . $params["text"]);
$argc = count($argv);
$fight = FightController::findFight($user, $params["channel_id"]);
$method .= "_";
$result = FightController::$method($argc, $argv, $user, $fight, $params);
return self::packageData(200, $result, $user);
}
catch (Exception $e) {
if ($e->getCode() === 200) {
$message = new FightMessage("warning", $e->getMessage());
}
else {
$message = new FightErrorMessage($e->getMessage());
}
return self::packageData($e->getCode(), $message, $user);
}
}
public static function login($params) {
if (!$params["email"]) {
return self::packageData(400, "Email is required.", null);
}
$result = [];
$user = FightUserModel::findByEmail($params["email"]);
if (!$params["password"]) {
$result[] = new FightData(($user && $user->password) ? 1 : 0);
}
else if (!$user) {
return self::packageData(400, new FightErrorMessage("No user found for " . $params["email"]));
}
else {
$md5 = md5($params["password"]);
if ($md5 === $user->password) {
$result[] = new FightData($user->user_id);
}
else if (!$user->password) {
return self::packageData(400, new FightErrorMessage("No account set up for this email yet"));
}
else {
return self::packageData(400, new FightErrorMessage("No match found for this email and password"));
}
}
return self::packageData(200, $result);
}
public static function packageData($status, $data, $user) {
if (!is_array($data)) $data = [$data];
return [
"status" => $status,
"data" => $data,
"user" => $user
];
}
public static function isMethod($method) {
if ($method === "login") return true;
return is_callable(["Fight\Controller\FightController", $method . "_"]);
}
}