-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #190 from robinje/motd
Add Message of the Day
- Loading branch information
Showing
7 changed files
with
204 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package core | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/dynamodb" | ||
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" | ||
"github.com/google/uuid" | ||
) | ||
|
||
func (k *KeyPair) GetAllMOTDs() ([]*MOTD, error) { | ||
input := &dynamodb.ScanInput{ | ||
TableName: aws.String("motd"), | ||
FilterExpression: aws.String("active = :active"), | ||
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{ | ||
":active": { | ||
BOOL: aws.Bool(true), | ||
}, | ||
}, | ||
} | ||
|
||
result, err := k.db.Scan(input) | ||
if err != nil { | ||
return nil, fmt.Errorf("error scanning MOTDs: %w", err) | ||
} | ||
|
||
var motds []*MOTD | ||
err = dynamodbattribute.UnmarshalListOfMaps(result.Items, &motds) | ||
if err != nil { | ||
return nil, fmt.Errorf("error unmarshalling MOTDs: %w", err) | ||
} | ||
|
||
return motds, nil | ||
} | ||
|
||
// TODO: Rework the 'server.ActiveMotDs' into a map. | ||
func DisplayUnseenMOTDs(server *Server, player *Player) { | ||
if server == nil || player == nil { | ||
Logger.Error("Invalid server or player object") | ||
return | ||
} | ||
|
||
Logger.Info("Displaying MOTDs for player", "playerName", player.Name) | ||
|
||
defaultMOTDID, _ := uuid.Parse("00000000-0000-0000-0000-000000000000") | ||
welcomeDisplayed := false | ||
|
||
// First, look for and display the welcome message | ||
for _, motd := range server.ActiveMotDs { | ||
if motd != nil && motd.ID == defaultMOTDID { | ||
player.ToPlayer <- fmt.Sprintf("\n\r%s\n\r", motd.Message) | ||
welcomeDisplayed = true | ||
break | ||
} | ||
} | ||
|
||
// If no welcome message was found, display a generic one | ||
if !welcomeDisplayed { | ||
player.ToPlayer <- "\n\rWelcome to the game!\n\r" | ||
} | ||
|
||
// Then display other unseen MOTDs | ||
for _, motd := range server.ActiveMotDs { | ||
if motd == nil || motd.ID == defaultMOTDID { | ||
continue | ||
} | ||
|
||
// Check if the player has already seen this MOTD | ||
if !player.SeenMotDs[motd.ID] { | ||
// Display the MOTD to the player | ||
player.ToPlayer <- fmt.Sprintf("\n\r%s\n\r", motd.Message) | ||
|
||
// Mark the MOTD as seen | ||
player.SeenMotDs[motd.ID] = true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import argparse | ||
import uuid | ||
from datetime import datetime | ||
|
||
import boto3 | ||
|
||
|
||
def add_or_update_motd(message, active=True, is_welcome=False): | ||
dynamodb = boto3.resource("dynamodb") | ||
table = dynamodb.Table("motd") | ||
|
||
motd_id = "00000000-0000-0000-0000-000000000000" if is_welcome else str(uuid.uuid4()) | ||
|
||
try: | ||
response = table.update_item( | ||
Key={"motdID": motd_id}, | ||
UpdateExpression="SET #msg = :message, active = :active, createdAt = :created", | ||
ExpressionAttributeNames={"#msg": "message"}, # 'message' is a reserved word in DynamoDB | ||
ExpressionAttributeValues={":message": message, ":active": active, ":created": datetime.now().isoformat()}, | ||
ReturnValues="UPDATED_NEW", | ||
) | ||
print(f"MOTD {'updated' if is_welcome else 'added'} successfully.") | ||
print(f"MOTD ID: {motd_id}") | ||
return response | ||
except Exception as e: | ||
print(f"Error adding/updating MOTD: {str(e)}") | ||
return None | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description="Add or update a Message of the Day (MOTD)") | ||
parser.add_argument("message", type=str, help="The MOTD message") | ||
parser.add_argument("--welcome", action="store_true", help="Set this flag to add/update the welcome message") | ||
parser.add_argument("--inactive", action="store_true", help="Set this flag to make the MOTD inactive") | ||
|
||
args = parser.parse_args() | ||
|
||
add_or_update_motd(args.message, not args.inactive, args.welcome) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters