Skip to content

Instantly share code, notes, and snippets.

@jakergrossman
Last active April 13, 2020 21:30
Show Gist options
  • Select an option

  • Save jakergrossman/1ad63d8267cfd0b50c245efb919f9e93 to your computer and use it in GitHub Desktop.

Select an option

Save jakergrossman/1ad63d8267cfd0b50c245efb919f9e93 to your computer and use it in GitHub Desktop.
Short plugin for demonstrating SourcePawn experience.
#include <sourcemod>
public Plugin pluginInfo = {
name = "GFL Programming Application Submission Test",
author = "Jake Grossman",
description = "A simple plugin to demonstrate reasonable proficiency",
version = "1.0",
url = "https://jakergrossman.xyz"
}
// current math question
char g_MathProblem[32];
// current answer for math question
char g_MathAnswer[4];
bool g_WaitingForNickname[MAXPLAYERS + 1];
public void OnPluginStart() {
// register commands
RegConsoleCmd("sm_menu", Command_Menu, "Open the plugin menu");
RegConsoleCmd("sm_cancelnick", Command_CancelNick, "Cancel nickname prompt");
}
public Action Command_Menu(int client, int args) {
Menu menu = new Menu(MenuHandler);
menu.SetTitle("GFL Test Plugin");
menu.AddItem("select_nickname", "Enter preferred nickame");
menu.AddItem("print_nickname", "Get stored nickname");
menu.AddItem("math_problem", "Ask math problem to chat");
menu.Display(client, -1);
return Plugin_Handled;
}
public int MenuHandler(Menu menu, MenuAction action, int x, int y) {
if (action == MenuAction_Select) {
char info[32];
bool found = menu.GetItem(y, info, sizeof(info));
if (found) {
if (StrEqual(info, "math_problem")) {
GenerateMathProblem();
PrintToChatAll(g_MathProblem);
}
}
} else if (action == MenuAction_End) {
delete menu;
}
}
void GenerateMathProblem() {
int x, y, operation, answer;
char operationSymbol;
// generate operands
x = GetRandomInt(1, 99);
y = GetRandomInt(1, 99);
operation = GetRandomInt(1, 4); // 1=addition,2=subtraction,3=multiplication,4=division
switch (operation) {
case 1:
{
answer = x + y;
operationSymbol = '+';
}
case 2:
{
answer = x - y;
operationSymbol = '-';
}
case 3:
{
answer = x * y;
operationSymbol = '*';
}
case 4:
{
// regenerate the operands, if needed, until a valid division problem is found
while (x % y != 0) {
x = GetRandomInt(1, 99);
y = GetRandomInt(1, 99);
}
answer = x / y;
operationSymbol = '/';
}
}
// set g_MathAnswer
IntToString(answer, g_MathAnswer, sizeof(g_MathAnswer));
// set g_MathProblem
Format(g_MathProblem, sizeof(g_MathProblem), "Math Problem! What is %d%s%d", x, operationSymbol, y);
}
public Action Command_CancelNick(int client, int args) {
// if nickname prompt is active
if (g_WaitingForNickname[client]) {
g_WaitingForNickname[client] = false;
PrintToChat(client, "Cancelled nickname prompt");
} else {
PrintToChat(client, "The nickname prompt is not active");
}
}
public Action OnClientSayCommand(int client, const char[] command, const char[] text) {
// handle input if there is an active math problem
if (!StrEqual(g_MathProblem, "")) {
// check if answer is correct
if (StrEqual(text, g_MathAnswer)) {
PrintToConsole(client, "Correct!");
char awardMessage[64];
char clientName[255];
GetClientName(client, clientName, sizeof(clientName));
Format(awardMessage, sizeof(awardMessage), "%s has entered the correct answer first!", clientName);
PrintToChatAll(awardMessage);
// reset g_MathProblem and g_MathAnswer
g_MathProblem = "";
g_MathAnswer = "";
}
}
// handle nickname prompt if active
if (g_WaitingForNickname[client]) {
// TODO: Insert text into db
PrintToChat(client, "Successfully changed nickname to %s", text);
// deactivate nickname prompt
g_WaitingForNickname[client] = false;
}
return Plugin_Continue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment