Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/messages #100

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions services/demux/database/migrations/0050_messages.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE IF NOT EXISTS "${schema^}"."messages" (
"id" SERIAL PRIMARY KEY,
"pet_id" INTEGER NOT NULL REFERENCES "${schema^}"."pets",
"message" TEXT NOT NULL,
"created_at" TIMESTAMP NOT NULL,
"created_block" BIGINT NOT NULL,
"created_trx" TEXT NOT NULL,
"created_eosacc" TEXT NOT NULL,
"_dmx_created_at" TIMESTAMP DEFAULT current_timestamp NOT NULL
);
23 changes: 23 additions & 0 deletions services/demux/src/updaters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,25 @@ const battleattack = async (db: any, payload: any, blockInfo: BlockInfo) => {

}

const messagefrom = async (db: any, payload: any, blockInfo: BlockInfo) => {

console.info("\n\n==== Message From ====")
console.info("\n\nUpdater Payload >>> \n", payload)
console.info("\n\nUpdater Block Info >>> \n", blockInfo)

const data = {
pet_id: payload.data.pet_id,
message: payload.data.message,
created_block: blockInfo.blockNumber,
created_trx: payload.transactionId,
created_at: blockInfo.timestamp,
created_eosacc: payload.authorization[0].actor,
}

const res = await db.messages.insert(data)
console.info("DB State Result >>> ", res)
}

const updaters = [
{
actionType: "monstereosio::createpet",
Expand Down Expand Up @@ -340,6 +359,10 @@ const updaters = [
actionType: "monstereosio::battleattack",
updater: battleattack,
},
{
actionType: "monstereosio::messagefrom",
updater: messagefrom,
},
]

export { updaters }
3 changes: 3 additions & 0 deletions services/eos-dev/contracts/monstereosio/include/pet/pet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ class pet : public eosio::contract {
void signup ( name user );
void transfer ( uint64_t sender, uint64_t receiver );

// messaging
void messagefrom (uuid petid, string message);

private:

/* ****************************************** */
Expand Down
15 changes: 15 additions & 0 deletions services/eos-dev/contracts/monstereosio/monstereosio.abi
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,17 @@
"type": "uuid"
}
]
},{
"name": "messagefrom",
"base": "",
"fields": [{
"name": "pet_id",
"type": "uuid"
},{
"name": "message",
"type": "string"
}
]
},{
"name": "feedpet",
"base": "",
Expand Down Expand Up @@ -620,6 +631,10 @@
"name": "destroypet",
"type": "destroypet",
"ricardian_contract": "---\ntitle: Destroy Monster\nsummary: Delete and destroy a Monster forever\nicon: https://monstereos.io/favicon.png#e6479a7f15b9f19775b09703a5973af41e6e6c0eefbe0c09b9f032a286248b74\n---\n\n## Destroy Terms & Conditions\n\nI, the owner of the pet {{pet_id}}, am destroying this monster\nbecause I no longer have interest in taking care of it.\n\nI understand that this action cannot be undone, the monster will\nbe cruelty destroyed and no longer be accessible or recreated by\nanyone.\n\nI understand that monsters actions are not reversible after the\n{{$transaction.delay_sec}} seconds or other delay as configured\nby my own permissions.\n\nIf this action fails to be irreversibly confirmed for technical issues\nor failure, I agree that I need to attempt to submit this action again,\nand also the subsequent interactions that I could possibly being submitted\nin this interval.\n\n"
},{
"name": "messagefrom",
"type": "messagefrom",
"ricardian_contract": ""
},{
"name": "techrevive",
"type": "techrevive",
Expand Down
1 change: 1 addition & 0 deletions services/eos-dev/contracts/monstereosio/src/pet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "pet.admin.cpp"
#include "pet.battle.cpp"
#include "pet.market.cpp"
#include "pet.messages.cpp"

using namespace utils;
using namespace types;
Expand Down
8 changes: 8 additions & 0 deletions services/eos-dev/contracts/monstereosio/src/pet.messages.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

void pet::messagefrom(uuid petid, string message) {
eosio_assert(message.length() <= 255, "E404|message longer than 255");
auto pc = _get_pet_config();
auto pet = pets.get(petid, "E404|Invalid petid");
require_auth(pet.owner);
eosio_assert(_is_alive(pet, pc), "E404|dead monsters can't message");
}
1 change: 1 addition & 0 deletions services/eos-node/config-full-node.ini
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ mongodb-filter-on = monstereosio:changebatami:
mongodb-filter-on = monstereosio:changebatama:
mongodb-filter-on = monstereosio:migrate:
mongodb-filter-on = monstereosio:transfer:
mongodb-filter-on = monstereosio:messagefrom:

# peer-key =
# peer-private-key =
Expand Down
1 change: 1 addition & 0 deletions services/eos-node/config-main-node.ini
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ mongodb-filter-on = monstereosio:changebatami:
mongodb-filter-on = monstereosio:changebatama:
mongodb-filter-on = monstereosio:migrate:
mongodb-filter-on = monstereosio:transfer:
mongodb-filter-on = monstereosio:messagefrom:

# peer-key =
# peer-private-key =
Expand Down
33 changes: 32 additions & 1 deletion services/frontend/src/modules/pages/HomeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,38 @@ import get3dModel from "../monsters/monster3DMatrix"
import {
monsterModelSrc,
} from "../monsters/monsters"
import MessageBoard from "../shared/MessageBoard"
import MessageSenderModal from "../shared/MessageSenderModal"

const PAGE_WELCOME = process.env.REACT_APP_PAGE_WELCOME || "A Monster Tamagotchi and Battle game for EOS blockchain!"

const HomeScreen = (props: any) => {
interface Props {
scatter:any,
identity:any
}

interface ReactState {
showMessageSender:boolean,
requiresMsgUpdate:boolean
}

class HomeScreen extends React.Component<Props, ReactState> {
public state = {showMessageSender:false, requiresMsgUpdate:true}

public render() {
const {showMessageSender, requiresMsgUpdate} = this.state

const monster3dModel = get3dModel(105) // DEVIL

const messageSenderClosed = (updateRequested:boolean) => {
this.setState({showMessageSender:false, requiresMsgUpdate:updateRequested})
}

const doShowMessageSender = () => {
this.setState({showMessageSender:true})
}


return (<PageContainer>
<div>
<Monster3DProfile
Expand All @@ -28,7 +53,13 @@ const HomeScreen = (props: any) => {
/>
</div>
<p className="home-monster">{PAGE_WELCOME}</p>
<MessageBoard requiresMsgUpdate={requiresMsgUpdate}/><br/>
<a className="button" onClick={doShowMessageSender}>Send your own message</a>
{showMessageSender && <MessageSenderModal
closeModal = {messageSenderClosed}
/>}
</PageContainer>)
}
}

export default HomeScreen
100 changes: 100 additions & 0 deletions services/frontend/src/modules/shared/MessageBoard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import * as React from "react"
import * as moment from "moment"
import { MonsterProps, monsterImageSrc } from "../monsters/monsters"
import { Query } from "react-apollo"
import gql from "graphql-tag"

export interface Message {
monster:MonsterProps,
message:string
}


const MessageCard = (props:any) => {
const sinceText = moment.parseZone(props.message.createdAt).fromNow()
const url = "https://2d.monstereos.io" + monsterImageSrc(props.message.petByPetId.typeId)
const trxLink = "https://bloks.io/transaction/" + props.message.createdTrx
return(
<div key={props.message.id}>
<img src={url} width="48px" height="48px"/><br/>
<em>{props.message.message}</em><br/>
{props.message.petByPetId.petName} owned by {props.message.petByPetId.owner}<br/>
sent by {props.message.createdEosacc} <a href={trxLink}>{sinceText}</a>
</div>
)
}


export const QUERY_MESSAGES = gql`
query LatestMessages($limit: Int!, $offset: Int!) {
allMessages(
first: $limit,
offset: $offset,
orderBy: ID_DESC,
) {
edges {
node {
id
message
createdAt
createdTrx
createdEosacc
petByPetId {
petName
typeId
owner
}
}
}
}
}
`

interface Props {
requiresMsgUpdate:boolean
}

class MessageBoard extends React.Component<Props, {}> {

public render() {
const {requiresMsgUpdate} = this.props
const variables = {
limit: 3,
offset: 0
}

return <div className="container">
<Query query={QUERY_MESSAGES} variables={variables}>
{({error, data, loading, refetch}) => {
if (requiresMsgUpdate) {
// tslint:disable-next-line:no-console
console.log("refetching")
setTimeout(()=>refetch(variables), 500)
}
if (error) {
return (<span>{error.toString()} {JSON.stringify(error)}</span>)
}
if (loading || !data || !data.allMessages) {
return <span>
<i className="fa fa-spin fa-spinner" /> Loading... Our servers are Syncing with the Chain
</span>
}
const messages = data.allMessages.edges

return <div>
{messages.map(({node}:any, index:number) => {
return(
<MessageCard
key = {node.id}
message = {node}
/>
)})}
</div>
}
}
</Query>
</div>
}
}

export default MessageBoard
Loading