-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTransferGemsAction.cpp
More file actions
140 lines (116 loc) Β· 4.04 KB
/
TransferGemsAction.cpp
File metadata and controls
140 lines (116 loc) Β· 4.04 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
#include "TransferGemsAction.h"
TransferGemsAction::TransferGemsAction(json msg) : Action(ActionType::TRANSFER_GEMS)
{
decodeJSON(msg);
}
void TransferGemsAction::decodeJSON(json msg)
{
if (!msg.contains("source_id") || !msg.contains("x") || !msg.contains("y") || !msg.contains("amount"))
{
is_valid_ = false;
return;
}
source_id_ = msg["source_id"];
int y = msg["y"];
int x = msg["x"];
target_ = Position(x, y);
amount_ = msg["amount"];
}
json TransferGemsAction::encodeJSON()
{
json js;
js["type"] = "transfer_gems";
js["source_id"] = source_id_;
js["x"] = target_.x;
js["y"] = target_.y;
js["amount"] = amount_;
return js;
}
std::string TransferGemsAction::dropMoney(Core *core, Object *srcObj)
{
if (srcObj->getType() != ObjectType::Unit)
return "only units can drop gems on the floor";
if (Board::instance().getObjectPositionById(srcObj->getId()).distance(target_) > 1)
return "invalid drop position";
if (Board::instance().getObjectPositionById(srcObj->getId()) == target_)
return "invalid drop position";
Unit *srcUnit = (Unit *)srcObj;
if (srcUnit->getTeamId() != core->getTeamId())
return "can't drop gems from another team";
if (srcUnit->getActionCooldown() > 0)
return "unit is on cooldown";
if (srcUnit->getBalance() < amount_)
amount_ = srcUnit->getBalance();
if (amount_ <= 0)
return "invalid amount";
srcUnit->setBalance(srcUnit->getBalance() - amount_);
srcUnit->resetActionCooldown();
Board::instance().addObject<GemPile>(GemPile(amount_), target_);
Stats::instance().inc(stat_keys::actions_executed);
Stats::instance().inc(stat_keys::gems_transferred, amount_);
return "";
}
std::string TransferGemsAction::execute(Core *core)
{
if (!is_valid_)
return "invalid input";
Object *srcObj = Board::instance().getObjectById(source_id_);
if (!srcObj)
return "invalid source object";
Object *dstObj = Board::instance().getObjectAtPos(target_);
if (!dstObj)
return dropMoney(core, srcObj);
if (srcObj->getId() == dstObj->getId())
return "can't transfer gems to yourself"; // can't transfer gems to itself
// only active objects can transfer gems
if (srcObj->getType() != ObjectType::Core && srcObj->getType() != ObjectType::Unit)
return "invalid source object type";
if (dstObj->getType() != ObjectType::Core && dstObj->getType() != ObjectType::Unit)
return "invalid destination object type";
// only as-close-together-as-possible objects can transfer gems
Position srcPos = Board::instance().getObjectPositionById(srcObj->getId());
Position dstPos = Board::instance().getObjectPositionById(dstObj->getId());
Position firstEmptyGridCell = findFirstEmptyGridCell(dstPos);
unsigned int maxDist = dstPos.distance(firstEmptyGridCell);
if (srcPos.distance(dstPos) > maxDist)
return "invalid transfer distance";
// cant transfer someone else's gems
if (srcObj->getType() == ObjectType::Core)
{
Core *srcCore = (Core *)srcObj;
if (srcCore->getTeamId() != core->getTeamId())
return "can't transfer gems from another team core";
if (srcCore->getBalance() < amount_)
amount_ = srcCore->getBalance();
if (srcCore->getBalance() <= 0)
return "invalid amount";
srcCore->setBalance(srcCore->getBalance() - amount_);
}
if (srcObj->getType() == ObjectType::Unit)
{
Unit *srcUnit = (Unit *)srcObj;
if (srcUnit->getTeamId() != core->getTeamId())
return "can't transfer gems from another team unit";
if (srcUnit->getActionCooldown() > 0)
return "unit is on cooldown";
srcUnit->resetActionCooldown();
if (srcUnit->getBalance() < amount_)
amount_ = srcUnit->getBalance();
if (srcUnit->getBalance() <= 0)
return "invalid amount";
srcUnit->setBalance(srcUnit->getBalance() - amount_);
}
if (dstObj->getType() == ObjectType::Core)
{
Core *dstCore = (Core *)dstObj;
dstCore->setBalance(dstCore->getBalance() + amount_);
}
if (dstObj->getType() == ObjectType::Unit)
{
Unit *dstUnit = (Unit *)dstObj;
dstUnit->setBalance(dstUnit->getBalance() + amount_);
}
Stats::instance().inc(stat_keys::actions_executed);
Stats::instance().inc(stat_keys::gems_transferred, amount_);
return "";
}