METEOR JS: Actions with multiple users - node.js

I have 2 chat rooms, in every room I have 5 users and input field , where every user can enter some number and submit it to validate on server, if number equal (for example) to 5 than users in this room win, and users in other room lose, so I have 5 users that win and 5 that lose.
So, what I want: when one of team win, than redirect users that wins to some WIN page, and at the same time redirect users that lose to LOSE page. And !THIS IMPORTANT! if some users for example was offline but his team won or lose , than when the next time user log in show LOSE or WIN page which depends on whether his team won or not.
So I see 3 problems for which I do not find a solution:
Redirect group of users (I use Iron Router)
Track when one of team win (track for all users that play game) (Blaze if unsuitable)
If some team wins and user was offline , hen the next time user log in show LOSE or WIN page
I know that a lot of questions and they are very extensive, but could not find the solution, help somebody !

It might be simpler to track this at the group level and then just have a template helper that tracks the group's win/loss status. You wouldn't even need a session variable and all participants would get the correct notification instantly or deferred. Ex:
HTML:
<template name="wrapper">
{{#if state.inProgress}}
{{> gameTemplate}}
{{else}}
{{#if state.won}}
{{> winnnerTemplate}}
{{else}}
{{> loserTemplate}}
{{/if}}
{{/if}}
</template>
JS:
Template.wrapper.helpers({
state: function(){
var myGroupId = Meteor.user().myGroupId;
var outcome = Groups.find({ _id: myGroupId }).outcome;
return { inProgress: ( outcome === 'in progress'), won: ( outcome === 'won' ) };
}
});
Here I'm returning an object from the helper instead of separate variables for inProgress and won to avoid searching Groups twice.
I've also modeled each user as belonging to one group with the groupId being a key in the user object. This obviously allows you to have any number of people in each group as well as any number of groups.
What I've left out is the server side logic that sets the outcome key in the Group documents. You'll have to set one group to won and the other to lost in the same server method.

You have a design problem.
and 2. If you want to track the "groups" (if they lose or win, redirect to win or lose) in your app, you have to create a group collection to manage the "states" of your game(I think your app is like a game).
with the group collection you can show the information to the members, i.e. if I'm in a group even if I was offline when I login in your app I will be able to see what is in my groups records.
Its a matter of design and know how manage the data and the events in meteor

Related

Handling different user types and data access in react

I am working on a React application with two user types: Buyer and Seller. A Seller can access some (not all) data from the user through a search bar using the user's email. So for example, if a buyer wants to know what interests the buyer to buy, and he has the user's email and did a search query it can show the user's interest if and only if the user made these data available to show if looked up.
Now, I have two user types, and one of the users Seller is getting access to info from the other Buyer. How to implement that the right way? And how to make sure the search is working properly. Any ideas? Thanks!
You can really only achieve this properly through a database, what you want to do is have a user table that contains all of the users connected to your app, then, one of the fields you would have for the user is AccountType, there, you would declare a 0 for a buyer or a 1 for a seller. For the search bar you would use a FlatList that contains the data you want to display, since your question is so broad I can't really provide you with examples, but if you read this you should be able to make what you want:
For the database (using MySql): https://dev.mysql.com/doc/refman/8.0/en/
For the FlatList and SearchBar: https://www.geeksforgeeks.org/how-to-add-searchbar-in-react-native/ - https://reactnative.dev/docs/flatlist

nodejs notification system

I'm doing a notification system for my website.
A notification systeme like facebook. Or stackoverflow.
I have 2 problems.
How store in db ? I can store ALL notifications in the user document ? or in a document apart (because i think monogdb is limited for size in a document ?) Or, store intelligently ? (using inc, or a value (see: true/false) in db, with query sophisticated)
How do for brought at the page ? For exemple, when i click in a link in my inbox for stackoverflow, i'm redirect to the page. But me, i have a system that is multipage for exemple: I have 100 friends. There are listed 30 per page. So when i click on the notification i can't redirect to the because it's impossible to know the good page (users can be removed).
Thank you very much !
And if you have another ideas, tell me. Thanks.
EDIT:
(sorry for my english, i'm french)
For the first problem, i realize that i have to wait the time comes to choose my structure. Because my notification is .. a little complicated, so advance to the feeling.
For the second, i solved the problem. I explain:
(I take the exemple of friends because it's easy to undestand.)
I stored my data like this:
{
friends: [
{_id: xxxxx, ts: xxxx},
{_id: xxxxx, ts: xxxx}
]
}
Imagine i display all friends: 30 per page.
The problems are:
when i want to display all friends i cant sort using mongo. (a little problem)
If i want to lead a user to this list (30 per page) at a special friend, always keeping the sort by ts. I can't know the page. The uniq solution is to take all document.
But: veryyy bad in performance.
So, i store like this:
{
friends: {
xxxx: {ts:xxx},
xxxx: {ts:xxx}
}
}
Know i can sort the document, with use skip and limit.
So if i want a portion, i do not need to take all documents.
To know the page, i just do the number of < or > to the ts, i have for exemple 11 friends who are > to the ts of the friends that i want, and do a count for all friends (ex: 50 friends) with 50 and 11, i can guess the page.
Is this solution is good ?
- i need a count
- a query to know the number of > or <
and i can take the page where is listed the friend, keeping the sort ts
You can don't understand why i use a count. I need because they are not store in the same docment.
2 EDIT:
The problem with this solution, is that i need to make query object and update object outside of the mongo query (ex: for do friends.xxxxxx: {$exists:true}
ps: And what advantages are to use ts instead of date for mongodb ?
I'm using ts but i think i will store date, and no ts.
3 EDIT
I will do like Sammaye. Store in separate document. Take a look at: http://mongly.com/Multiple-Collections-Versus-Embedded-Documents/#1 and http://openmymind.net/2012/1/30/MongoDB-Embedded-Documents-vs-Multiple-Collections/
#Stennie make a pretty complete answer.
However recently I did a similar thing in PHP for my website. The first thing to understand is whether you are doing a notification system or a wall (the two are very different), it seems unclear to me and I am not sure what you mean by:
How do for brought at the page ? For exemple, when i click in a link
in my inbox for stackoverflow, i'm redirect to the page. But me, i
have a system that is multipage for exemple: I have 100 friends. There
are listed 30 per page. So when i click on the notification i can't
redirect to the because it's impossible to know the good page (users
can be removed).
That is not very good English and is very confusing when I read it. If you can expand on that I am sure people can answer better.
For a notification system I found that a large collection of notification objects also worked. So I had a schema like:
{
_id: {},
to_user: ObjectId{},
user_id: ObjectId{}, // Originating user
custom_text: "has posted a new comment on your wall post",
read: false,
ts: MongoDate()
}
And this would literally be the document I have to produce notifications. Each time a user commits an action that generates a notification it writes a new row to the DB with to_user being populated each time with each user needing to be notified. As for multiple users commiting the same action I actually convert the user_id field in a list of OjbectId's so I can say:
Sam, Dan and Mike all commented on your wall post
I then query by ts storing the last ts that the user looked at in their row allowing me to do a range based query on the newest notifications each time. This works quite well for sharding and querying in my personal experience.
Hope it helps,
Whether to embed or link is a common question for data modelling in MongoDB. If your number of notifications is going to be unbounded, you are likely going to be better saving these in a separate collection.
The current 16Mb document limit actually isn't as much as an issue as some other considerations:
A performance issue you may encounter by including all notifications in a single document is that fast-growing documents may also need to be relocated in the database more frequently (see Padding Factor).
You may want to be applying multiple updates to a document (such as setting a "read" flag on notifications) in a very short period of time, which means more contention for updating the same document (see Atomic Operations).
In order to implement paging you can use limit() in combination with a range query or skip(). A range query (eg. based on an indexed notificationDate) will make more effective use of indexes and perform better than skip() as your collection grows.

Need recommendations for pattern for now.js object sharing in group

I have an application where I want to use node.js and now.js to share the state of an object within a nowjs "group" across any number of clients.
As an example of what I'm trying to do, let's say that within the multiroom chat example that comes with now.js, there could be any number of rooms, with the ID chosen by the user. If the user knows the ID, they can enter (or be the first to "create") that room. There is a "history" object that is maintained for each room once it is created and communication starts happening. The history object contains a property or two, and an array of comment objects, each with a user property, a comment property, and a datetime property. When a new user connects, they should immediately have access to the history.
Is there a way to store a group-scoped object variable on the server to sync with? Am I thinking about this in the wrong way? Am I crazy or a little slow?
OK...I got it.
I have a new function createHistory(groupid) on the server side, which returns a new history object if it doesn't already exist when the user enters the room(group). This would occur if this user just created the room.
This history object employs add, remove and list methods. Since I need to get at the list from the client, it's important that I use a callback like the following:
api.get = (callback) ->
callback(_history)
I assign the result:
everyone.getGroup(this.now.groupid).now.history = createHistory(this.now.groupid)
Now I can add history at the server:
api.add = (time,user,text) ->
_history.items.push {time:time, user:user,text:text}
And get to it from my client:
now.history.get (history) ->
for item in history
#do something...
The only drawback is once the room is empty, the history evaporates...I think. Although I suppose you could check for the last disconnect and persist it somewhere if you wanted.

Form data for iHotelier

I developed a website for a resort and put a reservation form on the home page. I am trying to get the Arrival, Departure, Room #, Adult # & Child # to go from the from to the iHotelier page.
The home page with the reservation form is: http://blueharborresort.com
The iHotelier page is: https://booking.ihotelier.com/istay/istay.jsp?hotelid=76029
Recently came across this website that details exactly what you're asking about.
http://hummelinteractive.com/ihotelier-by-travelclick-how-to-post-data-to-their-reservation-engine
....
The main thing to know is that the ihotelier application accepts GET data in a certain formation as follows:
DateIn
DateOut
Adults
Children
Rooms
HotelID
LanguageID
....

Creating a view of users who have created nodes

Basically i want to creeate a block diaplay view,which displays a list of all the users thata have posted some nodes on the drupal website.
Oddly enough thinking about this right now it could be a little tricky. You have two possible solutions off the top of my head.
1 - Create a new view of item type Node. Your row style will obviously be set to Fields. Under which Fields to pull select the User group and then tick off the User: Name checkbox. Set your Items to display setting to 0 for unlimited results.
Under the preview you should get a ton of results looking something like:
Name: John Doe
Name: Mary Jane
Name: John Doe
Name: Anonymous
What you're seeing is the authors of all the nodes posted in your system. There will be duplication because a user in your system could be the author of multiple nodes. Unfortunately you can't just tick off the Distinct: Yes option because this only applies to nodes and not the users.
How to deal with the duplicate user name results tho? Custom theme your view by creating a custom template under Theme: information. Inside the template write some PHP code which intercepts the row results from the View query before it renders and only render distinct user names from the results. You'd have to write the logic though to determine whether a user name has already been added.
As simple as creating a new custom array, adding each row result (user name) to array but first checking to see whether it already exists in your custom array - if it does then toss it and move on to the next user name. At the end you'll have an array filled with distinct user names who have posted on your site.
voila! It works. It's not elegant but it definitely will work if built this way.
2 - Alternatively maybe you can get this module working to accomplish the same thing in a less complicated manner: http://drupal.org/project/views_customfield but I have never used it so I cannot comment on it.
Good luck. Hope that helps.
My solution was to:
Create a view of people
Add a UID field (and any other fields you want)
Create a theme.tpl.php file for the Row Style
Do a DB call on each loop through the row to search for nodes created by the supplied UID.
Here is what I have in my semanticviews-view-fields-VIEWNAME.tpl.php
<?php
//Query the Drupal DB for nodes associated to the supplied UID
$existing_nid = db_query("SELECT nid FROM {node} WHERE (type = :ctype) AND uid = :uid", array("ctype" => "CONTENT_TYPE", "uid" => $fields['uid']->content))->fetchField();
//If the supplied UID created content of the supplied type, then show their name
if ($existing_nid != FALSE) {
echo "Name:" . $fields['name']->content;
}
?>
This way only UID's that have content associated to it in the DB will be printed out, and those that don't, won't.
Hope that helps!

Resources