-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgivePoint.cpp
137 lines (119 loc) · 4.61 KB
/
givePoint.cpp
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
/*
* givePoints
* Copyright (C) 2020 Benjamin Jackson
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "bzfsAPI.h"
class givePoints : public bz_Plugin, public bz_CustomSlashCommandHandler
{
public:
virtual const char* Name();
virtual void Init(const char* config);
virtual void Cleanup();
virtual bool SlashCommand(int playerID, bz_ApiString command, bz_ApiString /*message*/, bz_APIStringList *params);
};
BZ_PLUGIN(givePoints)
const char* givePoints::Name()
{
return "givePoints 1.0.0";
}
void givePoints::Init(const char* config)
{
bz_registerCustomSlashCommand("give", this);
}
void givePoints::Cleanup()
{
Flush();
bz_removeCustomSlashCommand("give");
}
double ConvertToNum(std::string message)
{
int messagelength = (int)message.length();
if (messagelength > 0 && messagelength < 6)
{
double messagevalue = 0;
double ten = 1;
for (int i = (messagelength - 1); i >= 0; i--)
{
if (message[i] < '0' || message[i] > '9') // got something other than a number
return 0;
ten *= 10;
messagevalue += (((double)message[i] - '0') / 10) * ten;
}
if (messagevalue)
return messagevalue;
}
return 0;
}
bool givePoints::SlashCommand(int playerID, bz_ApiString command, bz_ApiString /*message*/, bz_APIStringList *params)
{
if (command == "give")
{
bz_BasePlayerRecord *fromPlayer = bz_getPlayerByIndex(playerID);
if (params->size() != 1 && params->size() != 2)
{
bz_sendTextMessage(BZ_SERVER, playerID, "Syntax: 'Player callsign' '#' Number of points to give them, can't be more than you have. If the callsign you are trying to give points has a space use '' at the beginning and end.");
bz_freePlayerRecord(fromPlayer);
return true;
}
bz_BasePlayerRecord* toPlayer;
int playerScore = bz_getPlayerWins(playerID) - bz_getPlayerLosses(playerID);
int score = ConvertToNum(params->get(1));
toPlayer = bz_getPlayerBySlotOrCallsign(params->get(0).c_str());
if (toPlayer)
{
if (toPlayer->team == eObservers)
{
bz_sendTextMessage(BZ_SERVER, playerID, "You can't give points to an observer.");
bz_freePlayerRecord(toPlayer);
return true;
}
if (playerScore > 0 && score > 0 && score <= playerScore && toPlayer->callsign != fromPlayer->callsign)
{
bz_incrementPlayerWins(toPlayer->playerID, score);
bz_sendTextMessagef(BZ_SERVER, toPlayer->playerID, "%s gave %i points to you.", bz_getPlayerCallsign(fromPlayer->playerID), (int)(score));
bz_sendTextMessagef(BZ_SERVER, fromPlayer->playerID, "Gave %s %i points.", bz_getPlayerCallsign(toPlayer->playerID), (int)(score));
bz_incrementPlayerLosses(fromPlayer->playerID, score);
bz_freePlayerRecord(toPlayer);
return true;
}
if (toPlayer->callsign == fromPlayer->callsign)
{
bz_sendTextMessage(BZ_SERVER, playerID, "You are not allowed to give yourself points.");
bz_freePlayerRecord(toPlayer);
return true;
}
if (score > playerScore)
{
bz_sendTextMessage(BZ_SERVER, playerID, "You can't give away more points than you have.");
bz_freePlayerRecord(toPlayer);
return true;
}
else
{
bz_sendTextMessagef(BZ_SERVER, fromPlayer->playerID, "%s is an invalid quantity of points.", params->get(1).c_str());
bz_freePlayerRecord(toPlayer);
}
}
else
{
bz_sendTextMessagef(BZ_SERVER, playerID, "%s does not exist to give points to", params->get(0));
bz_freePlayerRecord(toPlayer);
}
bz_freePlayerRecord(fromPlayer);
return true;
}
return false;
}